首页 > 解决方案 > check_totals 在 OpenMDAO 中写入一个大向量

问题描述

我想检查输出相对于大量输入的总导数,但我不想检查数组每个成员的导数,因为数组太大,而且复杂跨数组每个成员的步骤(或有限差异)将花费太长时间。有没有办法只检查数组的单个成员?

或者,有没有办法在整个数组中为 check_totals 执行方向导数?此功能似乎仅适用于 check_partials?

标签: openmdao

解决方案


从 OpenMDAO 版本 3.1.1 开始,我们没有对总数进行定向检查,但这是一个好主意,当我们找到最佳方法时,我们可能会实施它。

作为目前的解决方法,我认为对模型进行定向导数的最简单方法是通过创建一个在某个随机方向上采取“步骤”的组件来临时修改模型,然后将其插入到组件前面宽输入。我在这里整理了一个简单的例子:

import numpy as np
import openmdao.api as om

n = 50

class DirectionalComp(om.ExplicitComponent):

    def setup(self):
        self.add_input('x', 1.0)
        self.add_output('y', np.ones(n))
        self.A = -1.0 + 2.0 * np.random.random(n)

        self.declare_partials('y', 'x', rows=np.arange(n), cols=np.repeat(0, n), val=self.A)

    def compute(self, inputs, outputs, discrete_inputs=None, discrete_outputs=None):
        x = inputs['x']
        outputs['y'] = x * self.A


prob = om.Problem()
model = prob.model

# Add something like this
model.add_subsystem('p', om.IndepVarComp('x', 1.0))
model.add_subsystem('direction', DirectionalComp())

model.connect('p.x', 'direction.x')
model.connect('direction.y', 'comp.x')

model.add_design_var('p.x')

# Old Model
model.add_subsystem('comp', om.ExecComp('y = 2.0*x', x=np.ones((n, )), y=np.ones((n, ))))

model.add_constraint('comp.y', lower=0.0)

prob.setup()
prob.run_model()
totals = prob.check_totals()

推荐阅读