prob.root.unknowns 的值不会随着求解而改变

The values of prob.root.unknowns do not change with solve

这是对先前关于迭代 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']

vec_wrapper returns 键上的迭代器和元数据字典上的迭代器。您在元数据中访问的 '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]