I am having trouble accessing variables that are implicitly linked through multiple layers of groups. According to the documentation:
In new OpenMDAO, Groups are NOT Components and do not have their own variables. Variables can be promoted to the Group level by passing the promotes arg to the add call, e.g.,
group = Group() group.add('comp1', Times2(), promotes=['x'])
This will allow the variable x that belongs to comp1 to be accessed via group.params[‘x’].
However, when I try to access variables of sub-sub-groups I am getting errors. Please see example below that shows a working and non-working example:
from openmdao.api import Component, Group, Problem
import numpy as np
class Times2(Component):
def __init__(self):
super(Times2, self).__init__()
self.add_param('x', 1.0, desc='my var x')
self.add_output('y', 2.0, desc='my var y')
def solve_nonlinear(self, params, unknowns, resids):
unknowns['y'] = params['x'] * 2.0
def linearize(self, params, unknowns, resids):
J = {}
J[('y', 'x')] = np.array([2.0])
return J
class PassGroup1(Group):
def __init__(self):
super(PassGroup1, self).__init__()
self.add('t1', Times2(), promotes=['*'])
class PassGroup2(Group):
def __init__(self):
super(PassGroup2, self).__init__()
self.add('g1', PassGroup1(), promotes=['*'])
prob = Problem(root=Group())
prob.root.add('comp', PassGroup2(), promotes=['*'])
prob.setup()
prob.run()
# this works
print prob.root.comp.g1.t1.params['x']
# this does not
print prob.root.params['x']
Could you explain why this does not work, and how I can make variables available to the top level without a knowledge of the lower level groups?