Mike McWilliam
Mike McWilliam

Reputation: 176

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

This is an add-on to a previous question about iterating the values in and openMDAO problem. I want to be able to calculate values with top.run(). Then inspect the changes. However the previous advice does not seem to work.

I have found that prob.root.unknowns does not really change when the problem is modified or ran.

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']

Upvotes: 0

Views: 28

Answers (1)

Justin Gray
Justin Gray

Reputation: 5710

The iterator over a vec_wrapper returns an iterator over the keys, and the meta-data dictionary. The 'val' attribute you're accessing in the meta-data is actually the initial value.

Try this instead, and you'll see that it is changing:

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]

Upvotes: 1

Related Questions