Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added capability for IVCs to be instantiated with a list of variables #3197

Closed
wants to merge 9 commits into from
10 changes: 10 additions & 0 deletions openmdao/core/indepvarcomp.py
Expand Up @@ -39,6 +39,16 @@ def __init__(self, name=None, val=1.0, **kwargs):
if isinstance(name, str):
super().add_output(name, val, **kwargs)

elif isinstance(name, (list, tuple)):
for tup in name:
if len(tup) == 2:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to verify here that tup is actually a tuple, or at least not a string. Otherwise in cases where the user gives you a list/tuple of strings, you'll get weird error messages. I could see a user just providing a list of strings and assuming those variables would all have default values.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, I've modified this and pushed up!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the docstring for name needs to be updated for these new possibilities...

Also maybe some kind of error checking on the tup values (i.e. tup[0] must be a string, tup[2] must be a dict?)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Steve, I've updated the docstring and added type checking!

super().add_output(tup[0], tup[1], **kwargs)
elif len(tup) == 3:
super().add_output(tup[0], tup[1], **tup[2])
else:
raise ValueError("Each entry in the list of tuples must be of the form "
"(name, value) or (name, value, keyword_dict).")

elif name is None:
pass

Expand Down
34 changes: 34 additions & 0 deletions openmdao/core/tests/test_indep_var_comp.py
Expand Up @@ -105,6 +105,40 @@ def test_add_output(self):
assert_near_equal(prob.get_val('indep_var_1'), 1.0)
assert_near_equal(prob.get_val('indep_var_2'), 2.0)

def test_tuple_ivc(self):
"""Define one independent variable using a tuple."""

ivcs = [
('indep_var', 1.0),
('indep_var2', 2.0),
]

comp = om.IndepVarComp(ivcs)

prob = om.Problem()
prob.model.add_subsystem('comp', comp, promotes=['*'])
prob.setup(check=False)

assert_near_equal(prob.get_val('indep_var'), 1.0)
assert_near_equal(prob.get_val('indep_var2'), 2.0)

def test_tuple_ivc_kwargs(self):
"""Define one independent variable using a tuple with additional options."""

ivcs = [
('indep_var', 1.0, {'units': 'm'}),
('indep_var2', 2.0, {'units': 'm'}),
]

comp = om.IndepVarComp(ivcs)

prob = om.Problem()
prob.model.add_subsystem('comp', comp, promotes=['*'])
prob.setup(check=False)

assert_near_equal(prob.get_val('indep_var', units='m'), 1.0)
assert_near_equal(prob.get_val('indep_var2', units='m'), 2.0)

def test_promote_glob_no_inputs(self):
p = om.Problem()
p.model.add_subsystem('indep',
Expand Down