Skip to content

Commit

Permalink
add equivalent unit conversion check and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wright committed Sep 17, 2020
1 parent 67cf416 commit ca9f117
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
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)
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):
p.setup()


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

0 comments on commit ca9f117

Please sign in to comment.