Skip to content

Commit

Permalink
Merge branch 'master' into sphinx
Browse files Browse the repository at this point in the history
  • Loading branch information
kmarsteller committed Jan 10, 2018
2 parents 130b347 + ec30823 commit dac61ba
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 99 deletions.
6 changes: 5 additions & 1 deletion openmdao/components/meta_model_structured.py
Expand Up @@ -624,7 +624,11 @@ def compute(self, inputs, outputs):
fill_value=None,
spline_dim_error=False)

val = self.interps[out_name](pt)
try:
val = self.interps[out_name](pt)
except ValueError as err:
raise ValueError("Error interpolating output '%s' in %s:\n%s" %
(out_name, self.pathname, str(err)))
outputs[out_name] = val

def compute_partials(self, inputs, partials):
Expand Down
21 changes: 16 additions & 5 deletions openmdao/components/tests/test_meta_model_structured.py
Expand Up @@ -755,11 +755,9 @@ def setUp(self):
x, y, z = params
outs = mapdata.output_data
z = outs[0]
ivc.add_output('x', x[
'default'], units=x['units'])
ivc.add_output('x', x['default'], units=x['units'])
ivc.add_output('y', y['default'], units=y['units'])
ivc.add_output('z', z[
'default'], units=z['units'])
ivc.add_output('z', z['default'], units=z['units'])

model.add_subsystem('des_vars', ivc, promotes=["*"])

Expand All @@ -770,7 +768,7 @@ def setUp(self):

for out in outs:
comp.add_output(out['name'], out['default'], out['values'])

model.add_subsystem('comp', comp, promotes=["*"])
self.prob = Problem(model)
self.prob.setup()
Expand Down Expand Up @@ -807,6 +805,19 @@ def test_deriv4(self):
self.prob['z'] = 2.5
self.run_and_check_derivs(self.prob)

def test_raise_interp_error(self):
# muck with the grid to trigger an error in the interp call
self.prob.model.comp.interps['f'].grid = []

# verify that the error is raised by the meta model comp with
# information to help locate the error
with self.assertRaises(Exception) as context:
self.run_and_check_derivs(self.prob)
self.assertEqual(str(context.exception),
"Error interpolating output 'f' in comp:\n"
"The requested sample points xi have dimension 3, "
"but this RegularGridInterp has dimension 0")

def test_training_gradient(self):
model = Group()
ivc = IndepVarComp()
Expand Down
11 changes: 6 additions & 5 deletions openmdao/core/tests/test_connections.py
Expand Up @@ -278,7 +278,6 @@ def test_diff_conn_input_units_w_src(self):
class TestConnectionsPromoted(unittest.TestCase):

def test_inp_inp_promoted_no_src(self):
raise unittest.SkipTest("connected inputs w/o src not supported yet")

p = Problem(model=Group())
root = p.model
Expand All @@ -293,12 +292,14 @@ def test_inp_inp_promoted_no_src(self):
C3 = G4.add_subsystem("C3", ExecComp('y=x*2.0'), promotes=['x'])
C4 = G4.add_subsystem("C4", ExecComp('y=x*2.0'), promotes=['x'])

p.setup(check=False)
p.setup()
p.final_setup()

# setting promoted name should set both inputs mapped to that name
p['G3.x'] = 999.
self.assertEqual(C3._inputs['x'], 999.)
self.assertEqual(C4._inputs['x'], 999.)
with self.assertRaises(Exception) as context:
p['G3.x'] = 999.
self.assertEqual(str(context.exception),
"The promoted name G3.x is invalid because it refers to multiple inputs: [G3.G4.C3.x, G3.G4.C4.x] that are not connected to an output variable.")

def test_inp_inp_promoted_w_prom_src(self):
p = Problem(model=Group())
Expand Down
85 changes: 52 additions & 33 deletions openmdao/core/tests/test_getset_vars.py
Expand Up @@ -207,15 +207,18 @@ def test_with_promotion_errors(self):
# -------------------------------------------------------------------

msg1 = 'Variable name "{}" not found.'
msg2 = ('The promoted name "{}" is invalid because it is non-unique. '
'Access the value from the connected output variable "{}" instead.')
msg2 = "The promoted name x is invalid because it refers to multiple inputs: [g.c2.x ,g.c3.x]. Access the value from the connected output variable x instead."

inputs, outputs, residuals = g.get_nonlinear_vectors()

# inputs
with assertRaisesRegex(self, KeyError, msg2.format('x', 'x')):
with self.assertRaises(Exception) as context:
inputs['x'] = 5.0
with assertRaisesRegex(self, KeyError, msg2.format('x', 'x')):
self.assertEqual(str(context.exception), msg2)
with self.assertRaises(Exception) as context:
self.assertEqual(inputs['x'], 5.0)
self.assertEqual(str(context.exception), msg2)

with assertRaisesRegex(self, KeyError, msg1.format('g.c2.x')):
inputs['g.c2.x'] = 5.0
with assertRaisesRegex(self, KeyError, msg1.format('g.c2.x')):
Expand All @@ -231,10 +234,14 @@ def test_with_promotion_errors(self):
with g.jacobian_context() as jac:

# d(outputs)/d(inputs)
with assertRaisesRegex(self, KeyError, msg2.format('x', 'x')):
with self.assertRaises(Exception) as context:
jac['y', 'x'] = 5.0
with assertRaisesRegex(self, KeyError, msg2.format('x', 'x')):
self.assertEqual(str(context.exception), msg2)

with self.assertRaises(Exception) as context:
self.assertEqual(jac['y', 'x'], 5.0)
self.assertEqual(str(context.exception), msg2)

with assertRaisesRegex(self, KeyError, msg1.format('g.c2.y', 'g.c2.x')):
jac['g.c2.y', 'g.c2.x'] = 5.0
with assertRaisesRegex(self, KeyError, msg1.format('g.c2.y', 'g.c2.x')):
Expand Down Expand Up @@ -268,44 +275,49 @@ def test_nested_promotion_errors(self):

# -------------------------------------------------------------------

msg1 = ('The promoted name "{}" is invalid because it is non-unique. '
'Access the value from the connected output variable instead.')
msg1 = "The promoted name g.x is invalid because it refers to multiple inputs: [g.c2.x, g.c3.x] that are not connected to an output variable."

# inputs (g.x is not connected)
with assertRaisesRegex(self, KeyError, msg1.format('g.x')):
#with assertRaisesRegex(self, RuntimeError, msg1.format('g.x')):
with self.assertRaises(Exception) as context:
p['g.x'] = 5.0
p.final_setup()

self.assertEqual(str(context.exception), msg1)

# Repeat test for post final_setup when vectors are allocated.
p = Problem(model)
p.setup(check=False)
p.final_setup()

# -------------------------------------------------------------------

msg1 = ('The promoted name "{}" is invalid because it is non-unique. '
'Access the value from the connected output variable instead.')

# inputs (g.x is not connected)
with assertRaisesRegex(self, KeyError, msg1.format('g.x')):
with self.assertRaises(Exception) as context:
p['g.x'] = 5.0
p.final_setup()
self.assertEqual(str(context.exception), msg1)

# Start from a clean state again
p = Problem(model)
p.setup(check=False)

with assertRaisesRegex(self, KeyError, msg1.format('g.x')):
with self.assertRaises(Exception) as context:
self.assertEqual(p['g.x'], 5.0)
self.assertEqual(str(context.exception), msg1)

msg2 = "The promoted name x is invalid because it refers to multiple inputs: [g.c2.x, g.c3.x] that are not connected to an output variable."

with g.jacobian_context() as jac:

# d(outputs)/d(inputs)
with assertRaisesRegex(self, KeyError, msg1.format('x')):
with self.assertRaises(Exception) as context:
jac['y', 'x'] = 5.0
self.assertEqual(str(context.exception), msg2)

with assertRaisesRegex(self, KeyError, msg1.format('x')):
with self.assertRaises(Exception) as context:
self.assertEqual(jac['y', 'x'], 5.0)
self.assertEqual(str(context.exception), msg2)

# -------------------------------------------------------------------

Expand All @@ -314,78 +326,85 @@ def test_nested_promotion_errors(self):
p.setup(check=False)
p.final_setup()

with assertRaisesRegex(self, KeyError, msg1.format('g.x')):
with self.assertRaises(Exception) as context:
self.assertEqual(p['g.x'], 5.0)
self.assertEqual(str(context.exception), msg1)

with g.jacobian_context() as jac:

# d(outputs)/d(inputs)
with assertRaisesRegex(self, KeyError, msg1.format('x')):
with self.assertRaises(Exception) as context:
jac['y', 'x'] = 5.0
self.assertEqual(str(context.exception), msg2)

with assertRaisesRegex(self, KeyError, msg1.format('x')):
with self.assertRaises(Exception) as context:
self.assertEqual(jac['y', 'x'], 5.0)
self.assertEqual(str(context.exception), msg2)

# -------------------------------------------------------------------

msg1 = "The promoted name g.x is invalid because it refers to multiple inputs: [g.c2.x ,g.c3.x]. Access the value from the connected output variable x instead."

# From here, 'g.x' has a valid source.
model.connect('x', 'g.x')

p = Problem(model)
p.setup(check=False)

msg2 = ('The promoted name "{}" is invalid because it is non-unique. '
'Access the value from the connected output variable "{}" instead.')

# inputs (g.x is connected to x)
p['g.x'] = 5.0
with assertRaisesRegex(self, KeyError, msg2.format('g.x', 'x')):
with self.assertRaises(Exception) as context:
p.final_setup()
self.assertEqual(str(context.exception), msg1)

# Repeat test for post final_setup when vectors are allocated.
p = Problem(model)
p.setup(check=False)
p.final_setup()

msg2 = ('The promoted name "{}" is invalid because it is non-unique. '
'Access the value from the connected output variable "{}" instead.')

# inputs (g.x is connected to x)
with assertRaisesRegex(self, KeyError, msg2.format('g.x', 'x')):
with self.assertRaises(Exception) as context:
p['g.x'] = 5.0
self.assertEqual(str(context.exception), msg1)

# Final test, the getitem
p = Problem(model)
p.setup(check=False)

with assertRaisesRegex(self, KeyError, msg2.format('g.x', 'x')):
with self.assertRaises(Exception) as context:
self.assertEqual(p['g.x'], 5.0)
self.assertEqual(str(context.exception), msg1)

with g.jacobian_context() as jac:

# d(outputs)/d(inputs)
with assertRaisesRegex(self, KeyError, msg1.format('x')):
with self.assertRaises(Exception) as context:
jac['y', 'x'] = 5.0
self.assertEqual(str(context.exception), msg2)

with assertRaisesRegex(self, KeyError, msg1.format('x')):
with self.assertRaises(Exception) as context:
self.assertEqual(jac['y', 'x'], 5.0) # Start from a clean state again
self.assertEqual(str(context.exception), msg2)

# Repeat test for post final_setup when vectors are allocated.
p = Problem(model)
p.setup(check=False)
p.final_setup()

with assertRaisesRegex(self, KeyError, msg2.format('g.x', 'x')):
with self.assertRaises(Exception) as context:
self.assertEqual(p['g.x'], 5.0)
self.assertEqual(str(context.exception), msg1)

with g.jacobian_context() as jac:

# d(outputs)/d(inputs)
with assertRaisesRegex(self, KeyError, msg1.format('x')):
with self.assertRaises(Exception) as context:
jac['y', 'x'] = 5.0
self.assertEqual(str(context.exception), msg2)

with assertRaisesRegex(self, KeyError, msg1.format('x')):
with self.assertRaises(Exception) as context:
self.assertEqual(jac['y', 'x'], 5.0)
self.assertEqual(str(context.exception), msg2)


if __name__ == '__main__':
Expand Down

0 comments on commit dac61ba

Please sign in to comment.