def test_overrides(self): prob = Problem() prob.root = Group() prob.root.ln_solver = ScipyGMRES() prob.root.add('comp', SimpleImplicitComp()) prob.root.add('p1', IndepVarComp('x', 0.5)) prob.root.connect('p1.x', 'comp.x') prob.root.comp.deriv_options['check_type'] = 'fd' prob.root.comp.deriv_options['check_step_size'] = 1e25 prob.setup(check=False) prob.run() # This should override the bad stepsize. opts = {'check_step_size' : 1e-6} mystream = StringIO() data = prob.check_partial_derivatives(out_stream=mystream, global_options=opts) for key1, val1 in iteritems(data): for key2, val2 in iteritems(val1): assert_rel_error(self, val2['abs error'][0], 0.0, 1e-5) assert_rel_error(self, val2['abs error'][1], 0.0, 1e-5) assert_rel_error(self, val2['abs error'][2], 0.0, 1e-5) assert_rel_error(self, val2['rel error'][0], 0.0, 1e-5) assert_rel_error(self, val2['rel error'][1], 0.0, 1e-5) assert_rel_error(self, val2['rel error'][2], 0.0, 1e-5) prob = Problem() prob.root = Group() prob.root.ln_solver = ScipyGMRES() prob.root.add('comp', SimpleImplicitComp()) prob.root.add('p1', IndepVarComp('x', 0.5)) prob.root.connect('p1.x', 'comp.x') prob.root.comp.deriv_options['check_type'] = 'fd' prob.root.comp.deriv_options['check_step_size'] = 1e25 prob.setup(check=False) prob.run() mystream = StringIO() opts = {'check_form' : 'central'} data = prob.check_partial_derivatives(out_stream=mystream, global_options=opts, compact_print=True) text = mystream.getvalue() self.assertTrue('fd:central' in text) self.assertTrue('complex_step' not in text) mystream = StringIO() opts = {'check_type' : 'cs'} data = prob.check_partial_derivatives(out_stream=mystream, global_options=opts, compact_print=True) text = mystream.getvalue() self.assertTrue('fd:central' not in text) self.assertTrue('complex step' in text)
def test_alloc_jacobian(self): # Testing the helper function p = Problem() root = p.root = Group() root.add('comp1', ExecComp(["y[0]=x[0]*2.0+x[1]*7.0", "y[1]=x[0]*5.0-x[1]*3.0+x[2]*1.5"], x=np.zeros(3), y=np.zeros(2))) root.add('comp2', SimpleImplicitComp()) root.ln_solver.options['maxiter'] = 2 p.setup(check=False) # Explciit J = root.comp1.alloc_jacobian() self.assertTrue(len(J) == 1) self.assertTrue(('y', 'x') in J) self.assertTrue(J[('y', 'x')].shape == (2,3)) # Implicit J = root.comp2.alloc_jacobian() self.assertTrue(len(J) == 4) self.assertTrue(('y', 'x') in J) self.assertTrue(('y', 'z') in J) self.assertTrue(('z', 'x') in J) self.assertTrue(('z', 'z') in J) self.assertTrue(J[('y', 'x')].shape == (1, 1)) self.assertTrue(J[('y', 'z')].shape == (1, 1)) self.assertTrue(J[('z', 'x')].shape == (1, 1)) self.assertTrue(J[('z', 'z')].shape == (1, 1)) p.run()
def test_simple_implicit_complex_step(self): prob = Problem() prob.root = Group() prob.root.ln_solver = ScipyGMRES() prob.root.add('comp', SimpleImplicitComp()) prob.root.add('p1', IndepVarComp('x', 0.5)) prob.root.connect('p1.x', 'comp.x') prob.root.comp.fd_options['step_size'] = 1.0e4 prob.root.comp.fd_options['form'] = 'complex_step' prob.setup(check=False) prob.run() data = prob.check_partial_derivatives(out_stream=None) for key1, val1 in iteritems(data): for key2, val2 in iteritems(val1): assert_rel_error(self, val2['abs error'][0], 0.0, 1e-5) assert_rel_error(self, val2['abs error'][1], 0.0, 1e-5) assert_rel_error(self, val2['abs error'][2], 0.0, 1e-5) assert_rel_error(self, val2['rel error'][0], 0.0, 1e-5) assert_rel_error(self, val2['rel error'][1], 0.0, 1e-5) assert_rel_error(self, val2['rel error'][2], 0.0, 1e-5)
def test_simple_implicit_check_options(self): prob = Problem() prob.root = Group() prob.root.ln_solver = ScipyGMRES() prob.root.add('comp', SimpleImplicitComp()) prob.root.add('p1', IndepVarComp('x', 0.5)) prob.root.connect('p1.x', 'comp.x') # Not using this prob.root.deriv_options['step_size'] = 1e4 # Using these prob.root.deriv_options['check_form'] = 'central' prob.root.deriv_options['check_step_size'] = 1e-3 prob.setup(check=False) prob.run() data = prob.check_total_derivatives(out_stream=None) for key, val in iteritems(data): assert_rel_error(self, val['abs error'][0], 0.0, 1e-5) assert_rel_error(self, val['abs error'][1], 0.0, 1e-5) assert_rel_error(self, val['abs error'][2], 0.0, 1e-5) assert_rel_error(self, val['rel error'][0], 0.0, 1e-5) assert_rel_error(self, val['rel error'][1], 0.0, 1e-5) assert_rel_error(self, val['rel error'][2], 0.0, 1e-5)
def test_conflicting_promoted_state_vars(self): # verify we get an error if we have conflicting promoted state variables root = Group() comp1 = SimpleImplicitComp() comp2 = SimpleImplicitComp() root.add('c1', comp1, promotes=['z']) # promote the state, z root.add('c2', comp2, promotes=['z']) # promote the state, z, again.. BAD prob = Problem(root) with self.assertRaises(RuntimeError) as err: prob.setup(check=False) expected_msg = "'': promoted name 'z' matches multiple unknowns: ('c1.z', 'c2.z')" self.assertEqual(str(err.exception), expected_msg)
def __init__(self): super(TestProb, self).__init__() self.root = root = Group() root.add('c1', SimpleArrayComp()) root.add('p1', ParamComp('p', 1 * np.ones(2))) root.connect('p1.p', 'c1.x') root.add('ci1', SimpleImplicitComp()) root.add('pi1', ParamComp('p', 1.)) root.connect('pi1.p', 'ci1.x')
def __init__(self): super(TestProb, self).__init__() self.root = root = Group() root.ln_solver = ScipyGMRES() root.add('c1', SimpleArrayComp()) root.add('p1', IndepVarComp('p', 1*np.ones(2))) root.connect('p1.p','c1.x') root.add('ci1', SimpleImplicitComp()) root.add('pi1', IndepVarComp('p', 1.)) root.connect('pi1.p','ci1.x') root.add('pjunk', IndepVarComp('pj', np.ones((2,2)))) root.add('junk', ExecComp('y=x', x=np.zeros((2,2)), y=np.zeros((2,2)))) root.connect('pjunk.pj', 'junk.x')
def test_simple_implicit(self): prob = Problem() prob.root = Group() prob.root.add('comp', SimpleImplicitComp()) prob.root.add('p1', ParamComp('x', 0.5)) prob.root.connect('p1.x', 'comp.x') prob.setup(check=False) prob.run() data = prob.check_total_derivatives(out_stream=None) for key, val in iteritems(data): assert_rel_error(self, val['abs error'][0], 0.0, 1e-5) assert_rel_error(self, val['abs error'][1], 0.0, 1e-5) assert_rel_error(self, val['abs error'][2], 0.0, 1e-5) assert_rel_error(self, val['rel error'][0], 0.0, 1e-5) assert_rel_error(self, val['rel error'][1], 0.0, 1e-5) assert_rel_error(self, val['rel error'][2], 0.0, 1e-5)
def test_simple_implicit_Jacobian(self): # Tests that we can correctly handle user-defined Jacobians. # Now with a comp that has a state. params = {} params['x'] = 0.5 unknowns = {} unknowns['y'] = 0.0 unknowns['z'] = 0.0 resids = {} resids['z'] = 0.0 mycomp = SimpleImplicitComp() # Run model so we can calc derivatives around the solved state mycomp.solve_nonlinear(params, unknowns, resids) mycomp._jacobian_cache = mycomp.linearize(params, unknowns, resids) J = mycomp._jacobian_cache # Forward dparams = {} dparams['x'] = np.array([1.3]) dunknowns = {} dunknowns['z'] = np.array([2.5]) dresids = {} dresids['y'] = np.array([0.0]) dresids['z'] = np.array([0.0]) mycomp.apply_linear(params, unknowns, dparams, dunknowns, dresids, 'fwd') target = J[('y', 'x')]*dparams['x'] + J[('y', 'z')]*dunknowns['z'] diff = abs(dresids['y'] - target).max() self.assertAlmostEqual(diff, 0.0, places=3) target = J[('z', 'x')]*dparams['x'] + J[('z', 'z')]*dunknowns['z'] diff = abs(dresids['z'] - target).max() self.assertAlmostEqual(diff, 0.0, places=3) # Reverse dparams = {} dparams['x'] = np.array([0.0]) dunknowns = {} dunknowns['z'] = np.array([0.0]) dresids = {} dresids['y'] = np.array([1.5]) dresids['z'] = np.array([2.3]) mycomp.apply_linear(params, unknowns, dparams, dunknowns, dresids, 'rev') target = J[('y', 'x')]*dresids['y'] + J[('z', 'x')]*dresids['z'] diff = abs(dparams['x'] - target).max() self.assertAlmostEqual(diff, 0.0, places=3) target = J[('y', 'z')]*dresids['y'] + J[('z', 'z')]*dresids['z'] diff = abs(dunknowns['z'] - target).max() self.assertAlmostEqual(diff, 0.0, places=3)
def test_list_states(self): top = Problem() root = top.root = Group() sub = root.add('sub', Group()) sub.add('comp', SimpleImplicitComp()) sub.ln_solver = ScipyGMRES() top.setup(check=False) top['sub.comp.z'] = 7.7 stream = cStringIO() root.list_states(stream=stream) self.assertTrue('sub.comp.z' in stream.getvalue()) self.assertTrue('Value: 7.7' in stream.getvalue()) self.assertTrue('Residual: 0.0' in stream.getvalue()) self.assertTrue('States in model:' in stream.getvalue()) stream = cStringIO() sub.list_states(stream=stream) self.assertTrue('comp.z' in stream.getvalue()) self.assertTrue('Value: 7.7' in stream.getvalue()) self.assertTrue('Residual: 0.0' in stream.getvalue()) self.assertTrue('sub.comp.z' not in stream.getvalue()) self.assertTrue('States in sub:' in stream.getvalue()) top = Problem() root = top.root = ExampleGroupWithPromotes() top.setup(check=False) stream = cStringIO() root.list_states(stream=stream) self.assertTrue('No states in model.' in stream.getvalue()) stream = cStringIO() root.G2.list_states(stream=stream) self.assertTrue('No states in G2.' in stream.getvalue()) class ArrayImplicitComp(Component): """ Needed to test arrays here. """ def __init__(self): super(ArrayImplicitComp, self).__init__() # Params self.add_param('x', np.zeros((3, 1))) # Unknowns self.add_output('y', np.zeros((3, 1))) # States self.add_state('z', 2.0 * np.ones((3, 1)), lower=1.5, upper=np.array([2.5, 2.6, 2.7])) def solve_nonlinear(self, params, unknowns, resids): pass def apply_nonlinear(self, params, unknowns, resids): """ Don't solve; just calculate the residual.""" pass def linearize(self, params, unknowns, resids): """Analytical derivatives.""" pass top = Problem() root = top.root = Group() root.add('comp', ArrayImplicitComp()) root.ln_solver.options['maxiter'] = 2 top.setup(check=False) stream = cStringIO() root.list_states(stream=stream) base = 'States in model:\n\ncomp.z\nValue: [[ 2.]\n [ 2.]\n [ 2.]]\nResidual: [[ 0.]\n [ 0.]\n [ 0.]]' self.assertTrue(base in stream.getvalue())