所以我试图在一组之间进行有限差分,但我遇到了错误
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']