这是关于迭代值和openMDAO问题的上一个问题的附加组件。我希望能够使用top.run()计算值。然后检查更改。然而,之前的建议似乎不起作用。
我发现在修改或运行问题时,prob.root.unknowns并没有真正改变。
class TestObj(Component):
def __init__(self):
Component.__init__(self)
self.add_param('x',5.0)
self.add_output('y',5.0)
def solve_nonlinear(self,params,unknowns,resids):
unknowns['y']=params['x']
top = Problem()
root = Group()
top.root=root
root.add('split',IndepVarComp('x',2.0),promotes=['*'])
root.add('test',TestObj(),promotes=['*'])
top.setup()
top.root.list_connections()
print 'starting'
print top['x']
print top['y']
for k,v in top.root.unknowns.iteritems():
print k
print v['val']
top.run()
print 'after first solve'
print top['x']
print top['y']
for k,v in top.root.unknowns.iteritems():
print k
print v['val']
top['x']=33.0
print 'after second solve'
print top['x']
print top['y']
for k,v in top.root.unknowns.iteritems():
print k
print v['val']
top.run()
print 'after third solve'
print top['x']
print top['y']
for k,v in top.root.unknowns.iteritems():
print k
print v['val']
答案 0 :(得分:1)
vec_wrapper上的迭代器返回键上的迭代器和元数据字典。 ' val'您在元数据中访问的属性实际上是初始值。
试试这个,你会发现它正在改变:
from openmdao.api import Component, Problem, Group, IndepVarComp
class TestObj(Component):
def __init__(self):
Component.__init__(self)
self.add_param('x',5.0)
self.add_output('y',5.0)
def solve_nonlinear(self,params,unknowns,resids):
unknowns['y']=params['x']
top = Problem()
root = Group()
top.root=root
root.add('split',IndepVarComp('x',2.0),promotes=['*'])
root.add('test',TestObj(),promotes=['*'])
top.setup()
top.root.list_connections()
print 'starting'
print top['x']
print top['y']
for k,v in root.unknowns.iteritems():
print k
print top.root.unknowns[k]
top.run()
print 'after first solve'
print top['x']
print top['y']
for k,v in root.unknowns.iteritems():
print k
print top.root.unknowns[k]
top['x']=33.0
print 'after second solve'
print top['x']
print top['y']
for k,v in top.root.unknowns.iteritems():
print k
print top.root.unknowns[k]
top.run()
print 'after third solve'
print top['x']
print top['y']
for k,v in top.root.unknowns.iteritems():
print k
print top.root.unknowns[k]