所以我试图在一个组中有限差异而且我遇到了错误
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']
答案 0 :(得分:1)
感谢您提供错误报告和测试。我已经解决了这个问题。拉取请求已启动,可能会在一天内被接受,但如果您想提前尝试,请将其从我的分支机构中删除。