Search code examples
pythonopenmdao

Linking IndepVarComps from two Groups in OpenMDAO 1.x


I don't understand the error:

NameError: Source 'x' cannot be connected to target 'AB.x': Target must be a parameter but 'AB.x' is an unknown.

I have read through this response, but am still missing something.

I wrote a simple problem to more easily capture the issue:

from openmdao.api import Group, Component, IndepVarComp, Problem

class A(Component):
    def __init__(self):
        super(A, self).__init__()
        self.add_param('x', val=0.0)
        self.add_param('y', val=0.0)
        self.add_output('z', val=0.0)
    def solve_nonlinear(self, params, unknowns, resids):
        unknowns['z'] = params['x'] + params['y']

class B(Component):
    def __init__(self):
        super(B, self).__init__()
        self.add_param('x', val=0.0)
        self.add_param('y', val=0.0)
        self.add_output('z', val=0.0)
    def solve_nonlinear(self, params, unknowns, resids):
        unknowns['z'] = 2*params['x'] - params['y']

class AB(Group):
    def __init__(self):
        super(AB, self).__init__()
        self.add('A', A())
        self.add('B', B())
        self.add('x', IndepVarComp('x', val=0.0), promotes=['*'])
        self.add('y', IndepVarComp('y', val=0.0), promotes=['*'])
        self.connect('x', ['A.x', 'B.x'])
        self.connect('y', ['A.y', 'B.y'])

class C(Component):
    def __init__(self):
        super(C, self).__init__()
        self.add_param('x', val=0.0)
        self.add_param('y', val=0.0)
        self.add_output('z', val=0.0)
    def solve_nonlinear(self, params, unknowns, resids):
        unknowns['z'] = 3*params['x'] - 2*params['y']

class ABC(Group):
    def __init__(self):
        super(ABC, self).__init__()
        self.add('AB', AB())
        self.add('C', C())
        self.add('x', IndepVarComp('x', val=0.0), promotes=['*'])
        self.add('y', IndepVarComp('y', val=0.0), promotes=['*'])
        self.connect('x', ['AB.x', 'C.x'])
        self.connect('y', ['AB.y', 'C.y'])

prob = Problem()
prob.root = ABC()
prob.setup()
prob.run()

I prefer to use the IndepVarComp method of linking Component variables together with explicit connect statements over the use of implicit connections via promotes because I can more easily see the connections in large problems and have a nice listing of all the input variables. It would be helpful to link two IndepVarComp variables together across multiple Groups as I build larger and larger problems.

Many thanks for your thoughts and time.


Solution

  • In group 'AB', the promoted variable 'x' is already connected to a source:

        self.add('x', IndepVarComp('x', val=0.0), promotes=['*'])
        self.connect('x', ['A.x', 'B.x'])
    

    In the top group 'ABC', that same 'x' variable is 'AB.x', but you try to connect it again to another indepvarcomp:

        self.add('x', IndepVarComp('x', val=0.0), promotes=['*'])
        self.connect('x', ['AB.x', 'C.x'])
    

    I would recommend removing the IndepVarComp and connection down inside of 'AB' to solve this problem.

    To be more clear, you can't connect a component input up to 2 different sources at the same time. If those two sources have 2 different values, then which one is correct? It is ambiguous, so we don't allow it.