使用 PBO 进行有限差分组时出错

Error when finite differencing group with PBO

所以我正在尝试对一组进行有限差分,但我 运行 陷入了错误

  File "/usr/local/lib/python2.7/site-packages/openmdao/core/system.py", line 531, in fd_jacobian
    target_input[idx] += step
TypeError: '_ByObjWrapper' object does not support indexing

我已经追踪到它试图对 pass_by_obj 的变量进行有限差分这一事实。当您对一个组件进行有限差分时,有限差分和 pbo 起作用,但当您对一个组进行有限差分时,它就会失败。这是一个例子:

import numpy as np
from openmdao.api import Group, Problem, Component, ScipyGMRES, ExecComp, IndepVarComp, NLGaussSeidel

class C1(Component):
    def __init__(self):
        super(C1, self).__init__()
        self.add_param('x', shape=1)
        self.add_param('B', val=0, pass_by_obj=True)
        self.add_output('y', shape=1)
        self.fd_options['force_fd'] = True

    def solve_nonlinear(self, params, unknowns, resids):
        unknowns['y'] = 4.0*params['x']*params['B']

class C2(Component):
    def __init__(self):
        super(C2, self).__init__()
        self.add_param('y', shape=1)
        self.add_output('z', shape=1)
        self.fd_options['force_fd'] = True

    def solve_nonlinear(self, params, unknowns, resids):
        unknowns['z'] = 2.0*params['y']

class FDGroup(Group):
    def __init__(self):
        super(FDGroup, self).__init__()
        self.add('c1', C1(), promotes=['*'])
        self.add('c2', C2(), promotes=['*'])
        self.fd_options['force_fd'] = True # Comment and then it works

class RootGroup(Group):
    def __init__(self):
        super(RootGroup, self).__init__()
        self.add('x', IndepVarComp('x', 0.0), promotes=['*'])
        self.add('B', IndepVarComp('B', 0, pass_by_obj=True), promotes=['*'])
        self.add('fd_group', FDGroup(), promotes=['*'])


p = Problem()
p.root = RootGroup()
p.setup(check=False)
p['x'] = 1.5
p['B'] = 2
p.run()
test_grad = open('test_grad.txt', 'w')
total_gradients = p.check_total_derivatives(out_stream=test_grad)

print
print "Derivative 1 - FWD", total_gradients['z', 'x']['J_fwd']
print "Derivative 1 - FD", total_gradients['z', 'x']['J_fd']

print p['z']

感谢您的错误报告和测试。我有办法解决这个问题。拉取请求已启动,可能会在一天内被接受,但如果您想早点尝试,可以从我的分支中拉下来。

https://github.com/OpenMDAO/OpenMDAO/pull/482