def test_add_duplicate(self): prob = Problem() root = prob.root = SellarDerivatives() prob.driver = MySimpleDriver() prob.driver.add_desvar('z', lower=-100.0, upper=100.0) prob.driver.add_objective('obj') prob.driver.add_constraint('con1', upper=0.0) # Add duplicate desvar with self.assertRaises(RuntimeError) as cm: prob.driver.add_desvar('z', lower=-50.0, upper=49.0) msg = "Desvar 'z' already exists." raised_error = str(cm.exception) self.assertEqual(msg, raised_error) # Add duplicate constraint with self.assertRaises(RuntimeError) as cm: prob.driver.add_constraint('con1', upper=0.0) msg = "Constraint 'con1' already exists." raised_error = str(cm.exception) self.assertEqual(msg, raised_error) # Add duplicate objective with self.assertRaises(RuntimeError) as cm: prob.driver.add_objective('obj') msg = "Objective 'obj' already exists." raised_error = str(cm.exception) self.assertEqual(msg, raised_error)
def test_Sellar_SLSQP(self): prob = Problem() prob.root = SellarDerivatives() prob.driver = ScipyOptimizer() prob.driver.options['optimizer'] = 'SLSQP' prob.driver.options['tol'] = 1.0e-8 prob.driver.add_desvar('z', lower=np.array([-10.0, 0.0]), upper=np.array([10.0, 10.0])) prob.driver.add_desvar('x', lower=0.0, upper=10.0) prob.driver.add_objective('obj') prob.driver.add_constraint('con1', upper=0.0) prob.driver.add_constraint('con2', upper=0.0) prob.driver.options['disp'] = False prob.setup(check=False) prob.run() assert_rel_error(self, prob['z'][0], 1.9776, 1e-3) assert_rel_error(self, prob['z'][1], 0.0, 1e-3) assert_rel_error(self, prob['x'], 0.0, 1e-3)
def test_eq_ineq_error_messages(self): prob = Problem() root = prob.root = SellarDerivatives() prob.driver = MySimpleDriver() # Don't try this at home, kids prob.driver.supports['equality_constraints'] = False with self.assertRaises(RuntimeError) as cm: prob.driver.add_constraint('con1', ctype='eq') self.assertEqual( str(cm.exception), "Driver does not support equality constraint 'con1'.") # Don't try this at home, kids prob.driver.supports['inequality_constraints'] = False with self.assertRaises(RuntimeError) as cm: prob.driver.add_constraint('con1', ctype='ineq') self.assertEqual( str(cm.exception), "Driver does not support inequality constraint 'con1'.")
def test_sellar_derivs(self): prob = Problem() prob.root = SellarDerivatives() prob.root.ln_solver = LinearGaussSeidel() prob.root.ln_solver.options['maxiter'] = 10 prob.root.ln_solver.options['atol'] = 1e-12 prob.root.ln_solver.options['rtol'] = 1e-12 #prob.root.ln_solver.options['iprint'] = 1 prob.root.nl_solver.options['atol'] = 1e-12 prob.setup(check=False) prob.run() # Just make sure we are at the right answer assert_rel_error(self, prob['y1'], 25.58830273, .00001) assert_rel_error(self, prob['y2'], 12.05848819, .00001) indep_list = ['x', 'z'] unknown_list = ['obj', 'con1', 'con2'] Jbase = {} Jbase['con1'] = {} Jbase['con1']['x'] = -0.98061433 Jbase['con1']['z'] = np.array([-9.61002285, -0.78449158]) Jbase['con2'] = {} Jbase['con2']['x'] = 0.09692762 Jbase['con2']['z'] = np.array([1.94989079, 1.0775421 ]) Jbase['obj'] = {} Jbase['obj']['x'] = 2.98061392 Jbase['obj']['z'] = np.array([9.61001155, 1.78448534]) J = prob.calc_gradient(indep_list, unknown_list, mode='fwd', return_format='dict') for key1, val1 in Jbase.items(): for key2, val2 in val1.items(): assert_rel_error(self, J[key1][key2], val2, .00001) J = prob.calc_gradient(indep_list, unknown_list, mode='rev', return_format='dict') for key1, val1 in Jbase.items(): for key2, val2 in val1.items(): assert_rel_error(self, J[key1][key2], val2, .00001) # Cheat a bit so I can twiddle mode OptionsDictionary.locked = False prob.root.fd_options['form'] = 'central' J = prob.calc_gradient(indep_list, unknown_list, mode='fd', return_format='dict') for key1, val1 in Jbase.items(): for key2, val2 in val1.items(): assert_rel_error(self, J[key1][key2], val2, .00001) # Obviously this test doesn't do much right now, but I need to verify # we don't get a keyerror here. J = prob.calc_gradient(indep_list, unknown_list, mode='fd', return_format='array')
def test_eq_ineq_error_messages(self): prob = Problem() root = prob.root = SellarDerivatives() prob.driver = MySimpleDriver() with self.assertRaises(RuntimeError) as cm: prob.driver.add_constraint('con1') self.assertEqual( str(cm.exception), "Constraint 'con1' needs to define lower, upper, or equals.") with self.assertRaises(RuntimeError) as cm: prob.driver.add_constraint('con1', lower=0.0, upper=1.1, equals=2.2) self.assertEqual( str(cm.exception), "Constraint 'con1' cannot be both equality and inequality.") # Don't try this at home, kids prob.driver.supports['two_sided_constraints'] = False with self.assertRaises(RuntimeError) as cm: prob.driver.add_constraint('con1', lower=0.0, upper=1.1) self.assertEqual(str(cm.exception), "Driver does not support 2-sided constraint 'con1'.") # Don't try this at home, kids prob.driver.supports['equality_constraints'] = False with self.assertRaises(RuntimeError) as cm: prob.driver.add_constraint('con1', equals=0.0) self.assertEqual( str(cm.exception), "Driver does not support equality constraint 'con1'.") # Don't try this at home, kids prob.driver.supports['inequality_constraints'] = False with self.assertRaises(RuntimeError) as cm: prob.driver.add_constraint('con1', upper=0.0) self.assertEqual( str(cm.exception), "Driver does not support inequality constraint 'con1'.")
def test_no_desvar_bound(self): prob = Problem() root = prob.root = SellarDerivatives() prob.driver = MySimpleDriver() prob.driver.add_desvar('z') prob.setup(check=False) meta = prob.driver._desvars['z'] self.assertLess(meta['lower'], -1e12) self.assertGreater(meta['upper'], 1e12)
def test_sellar_derivs(self): prob = Problem() prob.root = SellarDerivatives() prob.root.nl_solver = Newton() prob.setup(check=False) prob.run() assert_rel_error(self, prob['y1'], 25.58830273, .00001) assert_rel_error(self, prob['y2'], 12.05848819, .00001) # Make sure we aren't iterating like crazy self.assertLess(prob.root.nl_solver.iter_count, 8)
def test_sellar_derivs_with_Lin_GS(self): prob = Problem() prob.root = SellarDerivatives() prob.root.nl_solver = Newton() prob.root.ln_solver = LinearGaussSeidel() prob.root.ln_solver.options['maxiter'] = 2 prob.setup(check=False) prob.run() assert_rel_error(self, prob['y1'], 25.58830273, .00001) assert_rel_error(self, prob['y2'], 12.05848819, .00001) # Make sure we aren't iterating like crazy self.assertLess(prob.root.nl_solver.iter_count, 8)
def test_unsupported_multiple_obj(self): prob = Problem() prob.root = SellarDerivatives() prob.driver = MySimpleDriver() prob.driver.add_desvar('z', lower=-100.0, upper=100.0) prob.driver.add_objective('obj') # Add duplicate objective with self.assertRaises(RuntimeError) as cm: prob.driver.add_objective('x') msg = "Attempted to add multiple objectives to a driver that does not " \ "support multiple objectives." raised_error = str(cm.exception) self.assertEqual(msg, raised_error)
def test_mydriver(self): prob = Problem() root = prob.root = SellarDerivatives() prob.driver = MySimpleDriver() prob.driver.add_desvar('z', lower=-100.0, upper=100.0) prob.driver.add_objective('obj') prob.driver.add_constraint('con1', upper=0.0) prob.driver.add_constraint('con2', upper=0.0) prob.setup(check=False) prob.run() obj = prob['obj'] self.assertLess(obj, 28.0)
def test_sellar_analysis_error(self): prob = Problem() prob.root = SellarDerivatives() prob.root.ln_solver = LinearGaussSeidel() prob.root.ln_solver.options['maxiter'] = 2 prob.root.ln_solver.options['err_on_maxiter'] = True prob.root.ln_solver.options['atol'] = 1e-12 prob.root.ln_solver.options['rtol'] = 1e-12 prob.setup(check=False) prob.run() # Just make sure we are at the right answer assert_rel_error(self, prob['y1'], 25.58830273, .00001) assert_rel_error(self, prob['y2'], 12.05848819, .00001) indep_list = ['x', 'z'] unknown_list = ['obj', 'con1', 'con2'] Jbase = {} Jbase['con1'] = {} Jbase['con1']['x'] = -0.98061433 Jbase['con1']['z'] = np.array([-9.61002285, -0.78449158]) Jbase['con2'] = {} Jbase['con2']['x'] = 0.09692762 Jbase['con2']['z'] = np.array([1.94989079, 1.0775421]) Jbase['obj'] = {} Jbase['obj']['x'] = 2.98061392 Jbase['obj']['z'] = np.array([9.61001155, 1.78448534]) try: J = prob.calc_gradient(indep_list, unknown_list, mode='fwd', return_format='dict') except AnalysisError as err: self.assertEqual( str(err), "Solve in '': LinearGaussSeidel FAILED to converge after 2 iterations" ) else: self.fail("expected AnalysisError")
def test_sellar_derivs(self): prob = Problem() prob.root = SellarDerivatives() prob.root.nl_solver = Newton() prob.setup(check=False) prob.run() assert_rel_error(self, prob['y1'], 25.58830273, .00001) assert_rel_error(self, prob['y2'], 12.05848819, .00001) # Make sure we aren't iterating like crazy self.assertLess(prob.root.nl_solver.iter_count, 8) # Make sure we only call apply_linear on 'heads' nd1 = prob.root.d1.execution_count nd2 = prob.root.d2.execution_count if prob.root.d1._run_apply == True: self.assertEqual(nd1, 2 * nd2) else: self.assertEqual(2 * nd1, nd2)