def test_set_checks_shape(self): model = Group() indep = model.add_subsystem('indep', IndepVarComp()) indep.add_output('num') indep.add_output('arr', shape=(10, 1)) prob = Problem(model) prob.setup() msg = "Incompatible shape for '.*': Expected (.*) but got (.*)" # check valid scalar value new_val = -10. prob['indep.num'] = new_val assert_rel_error(self, prob['indep.num'], new_val, 1e-10) # check bad scalar value bad_val = -10*np.ones((10)) prob['indep.num'] = bad_val with assertRaisesRegex(self, ValueError, msg): prob.final_setup() prob._initial_condition_cache = {} # check assign scalar to array arr_val = new_val*np.ones((10, 1)) prob['indep.arr'] = new_val prob.final_setup() assert_rel_error(self, prob['indep.arr'], arr_val, 1e-10) # check valid array value new_val = -10*np.ones((10, 1)) prob['indep.arr'] = new_val assert_rel_error(self, prob['indep.arr'], new_val, 1e-10) # check bad array value bad_val = -10*np.ones((10)) with assertRaisesRegex(self, ValueError, msg): prob['indep.arr'] = bad_val # check valid list value new_val = new_val.tolist() prob['indep.arr'] = new_val assert_rel_error(self, prob['indep.arr'], new_val, 1e-10) # check bad list value bad_val = bad_val.tolist() with assertRaisesRegex(self, ValueError, msg): prob['indep.arr'] = bad_val
def test_no_promotion_errors(self): """ Tests for error-handling for invalid variable names and keys. """ g = Group(assembled_jac_type='dense') g.linear_solver = DirectSolver(assemble_jac=True) g.add_subsystem('c', ExecComp('y=2*x')) p = Problem() model = p.model model.add_subsystem('g', g) p.setup() # ------------------------------------------------------------------- msg = 'Variable name "{}" not found.' # inputs with assertRaisesRegex(self, KeyError, msg.format('x')): p['x'] = 5.0 p.final_setup() p._initial_condition_cache = {} with assertRaisesRegex(self, KeyError, msg.format('x')): p['x'] # outputs with assertRaisesRegex(self, KeyError, msg.format('y')): p['y'] = 5.0 p.final_setup() p._initial_condition_cache = {} with assertRaisesRegex(self, KeyError, msg.format('y')): p['y'] msg = 'Variable name "{}" not found.' inputs, outputs, residuals = g.get_nonlinear_vectors() # inputs with assertRaisesRegex(self, KeyError, msg.format('x')): inputs['x'] = 5.0 with assertRaisesRegex(self, KeyError, msg.format('x')): inputs['x'] with assertRaisesRegex(self, KeyError, msg.format('g.c.x')): inputs['g.c.x'] = 5.0 with assertRaisesRegex(self, KeyError, msg.format('g.c.x')): inputs['g.c.x'] # outputs with assertRaisesRegex(self, KeyError, msg.format('y')): outputs['y'] = 5.0 with assertRaisesRegex(self, KeyError, msg.format('y')): outputs['y'] with assertRaisesRegex(self, KeyError, msg.format('g.c.y')): outputs['g.c.y'] = 5.0 with assertRaisesRegex(self, KeyError, msg.format('g.c.y')): outputs['g.c.y'] msg = r'Variable name pair \("{}", "{}"\) not found.' jac = g.linear_solver._assembled_jac # d(output)/d(input) with assertRaisesRegex(self, KeyError, msg.format('y', 'x')): jac['y', 'x'] = 5.0 with assertRaisesRegex(self, KeyError, msg.format('y', 'x')): jac['y', 'x'] # allow absolute keys now # with assertRaisesRegex(self, KeyError, msg.format('g.c.y', 'g.c.x')): # jac['g.c.y', 'g.c.x'] = 5.0 # with assertRaisesRegex(self, KeyError, msg.format('g.c.y', 'g.c.x')): # deriv = jac['g.c.y', 'g.c.x'] # d(output)/d(output) with assertRaisesRegex(self, KeyError, msg.format('y', 'y')): jac['y', 'y'] = 5.0 with assertRaisesRegex(self, KeyError, msg.format('y', 'y')): jac['y', 'y']
def test_no_promotion_errors(self): """ Tests for error-handling for invalid variable names and keys. """ c = ExecComp('y=2*x') g = Group() g.add_subsystem('c', c) model = Group() model.add_subsystem('g', g) p = Problem(model) p.setup(check=False) # ------------------------------------------------------------------- msg = 'Variable name "{}" not found.' # inputs with assertRaisesRegex(self, KeyError, msg.format('x')): p['x'] = 5.0 p.final_setup() p._initial_condition_cache = {} with assertRaisesRegex(self, KeyError, msg.format('x')): self.assertEqual(p['x'], 5.0) # outputs with assertRaisesRegex(self, KeyError, msg.format('y')): p['y'] = 5.0 p.final_setup() p._initial_condition_cache = {} with assertRaisesRegex(self, KeyError, msg.format('y')): self.assertEqual(p['y'], 5.0) msg = 'Variable name "{}" not found.' inputs, outputs, residuals = g.get_nonlinear_vectors() # inputs with assertRaisesRegex(self, KeyError, msg.format('x')): inputs['x'] = 5.0 with assertRaisesRegex(self, KeyError, msg.format('x')): self.assertEqual(inputs['x'], 5.0) with assertRaisesRegex(self, KeyError, msg.format('g.c.x')): inputs['g.c.x'] = 5.0 with assertRaisesRegex(self, KeyError, msg.format('g.c.x')): self.assertEqual(inputs['g.c.x'], 5.0) # outputs with assertRaisesRegex(self, KeyError, msg.format('y')): outputs['y'] = 5.0 with assertRaisesRegex(self, KeyError, msg.format('y')): self.assertEqual(outputs['y'], 5.0) with assertRaisesRegex(self, KeyError, msg.format('g.c.y')): outputs['g.c.y'] = 5.0 with assertRaisesRegex(self, KeyError, msg.format('g.c.y')): self.assertEqual(outputs['g.c.y'], 5.0) msg = 'Variable name pair \("{}", "{}"\) not found.' with g.jacobian_context() as jac: # d(output)/d(input) with assertRaisesRegex(self, KeyError, msg.format('y', 'x')): jac['y', 'x'] = 5.0 with assertRaisesRegex(self, KeyError, msg.format('y', 'x')): self.assertEqual(jac['y', 'x'], 5.0) with assertRaisesRegex(self, KeyError, msg.format('g.c.y', 'g.c.x')): jac['g.c.y', 'g.c.x'] = 5.0 with assertRaisesRegex(self, KeyError, msg.format('g.c.y', 'g.c.x')): self.assertEqual(jac['g.c.y', 'g.c.x'], 5.0) # d(output)/d(output) with assertRaisesRegex(self, KeyError, msg.format('y', 'y')): jac['y', 'y'] = 5.0 with assertRaisesRegex(self, KeyError, msg.format('y', 'y')): self.assertEqual(jac['y', 'y'], 5.0) with assertRaisesRegex(self, KeyError, msg.format('g.c.y', 'g.c.y')): jac['g.c.y', 'g.c.y'] = 5.0 with assertRaisesRegex(self, KeyError, msg.format('g.c.y', 'g.c.y')): self.assertEqual(jac['g.c.y', 'g.c.y'], 5.0)
def test_no_promotion_errors(self): """ Tests for error-handling for invalid variable names and keys. """ g = Group(assembled_jac_type='dense') g.linear_solver = DirectSolver(assemble_jac=True) g.add_subsystem('c', ExecComp('y=2*x')) p = Problem() model = p.model model.add_subsystem('g', g) p.setup() # ------------------------------------------------------------------- msg = '\'Group (<model>): Variable "{}" not found.\'' # inputs with self.assertRaises(KeyError) as ctx: p['x'] = 5.0 self.assertEqual(str(ctx.exception), msg.format('x')) p._initial_condition_cache = {} with self.assertRaises(KeyError) as ctx: p['x'] self.assertEqual(str(ctx.exception), msg.format('x')) # outputs with self.assertRaises(KeyError) as ctx: p['y'] = 5.0 self.assertEqual(str(ctx.exception), msg.format('y')) p._initial_condition_cache = {} with self.assertRaises(KeyError) as ctx: p['y'] self.assertEqual(str(ctx.exception), msg.format('y')) p.final_setup() msg = "Group (g): Variable name '{}' not found." inputs, outputs, residuals = g.get_nonlinear_vectors() # inputs for vname in ['x', 'g.c.x']: with self.assertRaises(KeyError) as cm: inputs[vname] = 5.0 self.assertEqual(cm.exception.args[0], f"Group (g): Variable name '{vname}' not found.") with self.assertRaises(KeyError) as cm: inputs[vname] self.assertEqual(cm.exception.args[0], f"Group (g): Variable name '{vname}' not found.") # outputs for vname in ['y', 'g.c.y']: with self.assertRaises(KeyError) as cm: outputs[vname] = 5.0 self.assertEqual(cm.exception.args[0], f"Group (g): Variable name '{vname}' not found.") with self.assertRaises(KeyError) as cm: outputs[vname] self.assertEqual(cm.exception.args[0], f"Group (g): Variable name '{vname}' not found.") msg = r'Variable name pair \("{}", "{}"\) not found.' jac = g.linear_solver._assembled_jac # d(output)/d(input) with self.assertRaisesRegex(KeyError, msg.format('y', 'x')): jac['y', 'x'] = 5.0 with self.assertRaisesRegex(KeyError, msg.format('y', 'x')): jac['y', 'x'] # allow absolute keys now # with self.assertRaisesRegex(KeyError, msg.format('g.c.y', 'g.c.x')): # jac['g.c.y', 'g.c.x'] = 5.0 # with self.assertRaisesRegex(KeyError, msg.format('g.c.y', 'g.c.x')): # deriv = jac['g.c.y', 'g.c.x'] # d(output)/d(output) with self.assertRaisesRegex(KeyError, msg.format('y', 'y')): jac['y', 'y'] = 5.0 with self.assertRaisesRegex(KeyError, msg.format('y', 'y')): jac['y', 'y']
def test_no_promotion_errors(self): """ Tests for error-handling for invalid variable names and keys. """ g = Group(assembled_jac_type='dense') g.linear_solver = DirectSolver(assemble_jac=True) g.add_subsystem('c', ExecComp('y=2*x')) p = Problem() model = p.model model.add_subsystem('g', g) p.setup(check=False) # ------------------------------------------------------------------- msg = 'Variable name "{}" not found.' # inputs with assertRaisesRegex(self, KeyError, msg.format('x')): p['x'] = 5.0 p.final_setup() p._initial_condition_cache = {} with assertRaisesRegex(self, KeyError, msg.format('x')): p['x'] # outputs with assertRaisesRegex(self, KeyError, msg.format('y')): p['y'] = 5.0 p.final_setup() p._initial_condition_cache = {} with assertRaisesRegex(self, KeyError, msg.format('y')): p['y'] msg = 'Variable name "{}" not found.' inputs, outputs, residuals = g.get_nonlinear_vectors() # inputs with assertRaisesRegex(self, KeyError, msg.format('x')): inputs['x'] = 5.0 with assertRaisesRegex(self, KeyError, msg.format('x')): inputs['x'] with assertRaisesRegex(self, KeyError, msg.format('g.c.x')): inputs['g.c.x'] = 5.0 with assertRaisesRegex(self, KeyError, msg.format('g.c.x')): inputs['g.c.x'] # outputs with assertRaisesRegex(self, KeyError, msg.format('y')): outputs['y'] = 5.0 with assertRaisesRegex(self, KeyError, msg.format('y')): outputs['y'] with assertRaisesRegex(self, KeyError, msg.format('g.c.y')): outputs['g.c.y'] = 5.0 with assertRaisesRegex(self, KeyError, msg.format('g.c.y')): outputs['g.c.y'] msg = r'Variable name pair \("{}", "{}"\) not found.' jac = g.linear_solver._assembled_jac # d(output)/d(input) with assertRaisesRegex(self, KeyError, msg.format('y', 'x')): jac['y', 'x'] = 5.0 with assertRaisesRegex(self, KeyError, msg.format('y', 'x')): jac['y', 'x'] # allow absolute keys now # with assertRaisesRegex(self, KeyError, msg.format('g.c.y', 'g.c.x')): # jac['g.c.y', 'g.c.x'] = 5.0 # with assertRaisesRegex(self, KeyError, msg.format('g.c.y', 'g.c.x')): # deriv = jac['g.c.y', 'g.c.x'] # d(output)/d(output) with assertRaisesRegex(self, KeyError, msg.format('y', 'y')): jac['y', 'y'] = 5.0 with assertRaisesRegex(self, KeyError, msg.format('y', 'y')): jac['y', 'y']