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

add equivalent unit conversion check and tests #1690

Merged
merged 2 commits into from Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions openmdao/core/group.py
Expand Up @@ -3196,8 +3196,14 @@ def _resolve_ambiguous_input_meta(self):
tmeta = abs2meta[tgt] if tgt in abs2meta else all_abs2meta[tgt]
tunits = tmeta['units'] if 'units' in tmeta else None
if 'units' not in gmeta and sunits != tunits:
errs.add('units')
metadata.add('units')
try:
test_conv = unit_conversion(sunits, tunits)
Copy link
Member

Choose a reason for hiding this comment

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

Since PhysicalUnit objects understand if they're equivalent or not, we could do this a little more cleanly:

if _find_unit(sunits) != _find_unit(tunits):
   errs.add('units')
   metadata.add('units')

_find_unit is found in openmdao.utils.units

if abs(test_conv[0] - 1.0) > 1e-9 or abs(test_conv[1] - 0.0) > 1e-9:
errs.add('units')
metadata.add('units')
except TypeError:
errs.add('units')
metadata.add('units')
if 'value' not in gmeta:
if tval.shape == sval.shape:
if _has_val_mismatch(tunits, tval, sunits, sval):
Expand Down
37 changes: 37 additions & 0 deletions openmdao/core/tests/test_units.py
Expand Up @@ -880,6 +880,43 @@ def setup(self):
#self.assertTrue(iter_count < 20)
#self.assertTrue(not np.isnan(prob['sub.cc2.y']))

def test_promotes_equivalent_units(self):
# multiple Group.set_input_defaults calls at same tree level with conflicting units args
p = om.Problem()

g1 = p.model.add_subsystem("G1", om.Group(), promotes_inputs=['x'])
g1.add_subsystem("C1", om.ExecComp("y = 2. * x * z",
x={'value': 5.0, 'units': 'm/s/s'},
y={'value': 1.0, 'units': None},
z={'value': 1.0, 'units': 'W'}),
promotes_inputs=['x', 'z'])
g1.add_subsystem("C2", om.ExecComp("y = 3. * x * z",
x={'value': 5.0, 'units': 'm/s**2'},
y={'value': 1.0, 'units': None},
z={'value': 1.0, 'units': 'J/s'}),
promotes_inputs=['x', 'z'])
# converting m/s/s to m/s**2 is allowed
p.setup()

def test_promotes_non_equivalent_units(self):
# multiple Group.set_input_defaults calls at same tree level with conflicting units args
p = om.Problem()

g1 = p.model.add_subsystem("G1", om.Group(), promotes_inputs=['x'])
g1.add_subsystem("C1", om.ExecComp("y = 2. * x * z",
x={'value': 5.0, 'units': 'J/s/s'},
y={'value': 1.0, 'units': None},
z={'value': 1.0, 'units': 'W'}),
promotes_inputs=['x', 'z'])
g1.add_subsystem("C2", om.ExecComp("y = 3. * x * z",
x={'value': 5.0, 'units': 'm/s**2'},
y={'value': 1.0, 'units': None},
z={'value': 1.0, 'units': 'J/s'}),
promotes_inputs=['x', 'z'])
# trying to convert J/s/s to m/s**2 should cause Incompatible units TypeError exception
with self.assertRaises(TypeError):
Copy link
Member

Choose a reason for hiding this comment

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

We should check the contents of the error message here as well.

p.setup()


if __name__ == "__main__":
unittest.main()