def test_explicit_connection_errors(self): class A(Component): def __init__(self): super(A, self).__init__() self.add_state('x', 0) class B(Component): def __init__(self): super(B, self).__init__() self.add_param('x', 0) prob = Problem() prob.root = Group() prob.root.add('A', A()) prob.root.add('B', B()) prob.root.connect('A.x', 'B.x') prob.setup(check=False) expected_error_message = ( "Source 'A.y' cannot be connected to target 'B.x': " "'A.y' does not exist.") prob = Problem() prob.root = Group() prob.root.add('A', A()) prob.root.add('B', B()) prob.root.connect('A.y', 'B.x') with self.assertRaises(ConnectError) as cm: prob.setup(check=False) self.assertEqual(str(cm.exception), expected_error_message) expected_error_message = ( "Source 'A.x' cannot be connected to target 'B.y': " "'B.y' does not exist.") prob = Problem() prob.root = Group() prob.root.add('A', A()) prob.root.add('B', B()) prob.root.connect('A.x', 'B.y') with self.assertRaises(ConnectError) as cm: prob.setup(check=False) self.assertEqual(str(cm.exception), expected_error_message) expected_error_message = ( "Source 'A.x' cannot be connected to target 'A.x': " "Target must be a parameter but 'A.x' is an unknown.") prob = Problem() prob.root = Group() prob.root.add('A', A()) prob.root.add('B', B()) prob.root.connect('A.x', 'A.x') with self.assertRaises(ConnectError) as cm: prob.setup(check=False) self.assertEqual(str(cm.exception), expected_error_message)
def test_incompatible_connections(self): class BadComp(Component): def __init__(self): super(BadComp, self).__init__() self.add_param('x2', 100.0, units='m') self.add_output('x3', 100.0) # Explicit Connection prob = Problem() prob.root = Group() prob.root.add('src', SrcComp()) prob.root.add('dest', BadComp()) prob.root.connect('src.x2', 'dest.x2') with self.assertRaises(TypeError) as cm: prob.setup(check=False) expected_msg = "Unit 'degC' in source 'src.x2' is incompatible with unit 'm' in target 'dest.x2'." self.assertEqual(str(cm.exception), expected_msg) # Implicit Connection prob = Problem() prob.root = Group() prob.root.add('src', SrcComp(), promotes=['x2']) prob.root.add('dest', BadComp(),promotes=['x2']) with self.assertRaises(TypeError) as cm: prob.setup(check=False) expected_msg = "Unit 'degC' in source 'x2' is incompatible with unit 'm' in target 'x2'." self.assertEqual(str(cm.exception), expected_msg)
def test_explicit_connection_errors(self): class A(Component): def __init__(self): super(A, self).__init__() self.add_state('x', 0) class B(Component): def __init__(self): super(B, self).__init__() self.add_param('x', 0) prob = Problem() prob.root = Group() prob.root.add('A', A()) prob.root.add('B', B()) prob.root.connect('A.x', 'B.x') prob.setup(check=False) expected_error_message = ("Source 'A.y' cannot be connected to target 'B.x': " "'A.y' does not exist.") prob = Problem() prob.root = Group() prob.root.add('A', A()) prob.root.add('B', B()) prob.root.connect('A.y', 'B.x') with self.assertRaises(ConnectError) as cm: prob.setup(check=False) self.assertEqual(str(cm.exception), expected_error_message) expected_error_message = ("Source 'A.x' cannot be connected to target 'B.y': " "'B.y' does not exist.") prob = Problem() prob.root = Group() prob.root.add('A', A()) prob.root.add('B', B()) prob.root.connect('A.x', 'B.y') with self.assertRaises(ConnectError) as cm: prob.setup(check=False) self.assertEqual(str(cm.exception), expected_error_message) expected_error_message = ("Source 'A.x' cannot be connected to target 'A.x': " "Target must be a parameter but 'A.x' is an unknown.") prob = Problem() prob.root = Group() prob.root.add('A', A()) prob.root.add('B', B()) prob.root.connect('A.x', 'A.x') with self.assertRaises(ConnectError) as cm: prob.setup(check=False) self.assertEqual(str(cm.exception), expected_error_message)
def test_read4(self): # Variables on same line as header namelist1 = "Testing\n" + \ " \n" + \ "&OPTION boolvar = T, arrayvar = 3.5, 7.76, 1.23\n" + \ "/\n" outfile = open(self.filename, 'w') outfile.write(namelist1) outfile.close() top = Problem() top.root = Group() top.root.add('my_comp', VarComponent()) top.setup(check=False) top.run() sb = Namelist(top.root.my_comp) sb.set_filename(self.filename) sb.parse_file() sb.load_model() self.assertEqual(top['my_comp.boolvar'], True) self.assertEqual(top['my_comp.arrayvar'][0], 3.5) namelist1 = "Testing\n" + \ " \n" + \ "$OPTION boolvar = T, arrayvar = 3.5, 7.76, 1.23, $END\n" outfile = open(self.filename, 'w') outfile.write(namelist1) outfile.close() top = Problem() top.root = Group() top.root.add('my_comp', VarComponent()) top.setup(check=False) top.run() sb = Namelist(top.root.my_comp) sb.set_filename(self.filename) sb.parse_file() sb.load_model() self.assertEqual(top['my_comp.boolvar'], True) self.assertEqual(top['my_comp.arrayvar'][0], 3.5)
def test_simple_matvec(self): class VerificationComp(SimpleCompDerivMatVec): def jacobian(self, params, unknowns, resids): raise RuntimeError("Derivative functions on this comp should not run.") def apply_linear(self, params, unknowns, dparams, dunknowns, dresids, mode): raise RuntimeError("Derivative functions on this comp should not run.") sub = Group() sub.add('mycomp', VerificationComp()) prob = Problem() prob.root = Group() prob.root.add('sub', sub) prob.root.add('x_param', IndepVarComp('x', 1.0)) prob.root.connect('x_param.x', "sub.mycomp.x") sub.fd_options['force_fd'] = True prob.setup(check=False) prob.run() J = prob.calc_gradient(['x_param.x'], ['sub.mycomp.y'], mode='fwd', return_format='dict') assert_rel_error(self, J['sub.mycomp.y']['x_param.x'][0][0], 2.0, 1e-6) J = prob.calc_gradient(['x_param.x'], ['sub.mycomp.y'], mode='rev', return_format='dict') assert_rel_error(self, J['sub.mycomp.y']['x_param.x'][0][0], 2.0, 1e-6)
def test_record_derivs_dicts(self): if OPT is None: raise unittest.SkipTest("pyoptsparse is not installed") if OPTIMIZER is None: raise unittest.SkipTest( "pyoptsparse is not providing SNOPT or SLSQP") prob = Problem() prob.root = SellarDerivativesGrouped() prob.driver = pyOptSparseDriver() prob.driver.options['optimizer'] = 'SLSQP' prob.driver.opt_settings['ACC'] = 1e-9 prob.driver.options['print_results'] = False 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.add_recorder(self.recorder) self.recorder.options['record_metadata'] = False self.recorder.options['record_derivs'] = True prob.setup(check=False) prob.run() prob.cleanup() hdf = h5py.File(self.filename, 'r') deriv_group = hdf['rank0:SLSQP|1']['Derivs'] self.assertEqual(deriv_group.attrs['success'], 1) self.assertEqual(deriv_group.attrs['msg'], '') J1 = deriv_group['Derivatives'] 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]) for key1, val1 in Jbase.items(): for key2, val2 in val1.items(): assert_rel_error(self, J1[key1][key2][:], val2, .00001) hdf.close()
def test_simple_deriv_xfer(self): prob = Problem(impl=impl) prob.root = FanInGrouped() prob.setup(check=False) prob.root.comp3.dpmat[None]['x1'] = 7. prob.root.comp3.dpmat[None]['x2'] = 11. prob.root._transfer_data(mode='rev', deriv=True) if not MPI or self.comm.rank == 0: self.assertEqual(prob.root.sub.comp1.dumat[None]['y'], 7.) if not MPI or self.comm.rank == 1: self.assertEqual(prob.root.sub.comp2.dumat[None]['y'], 11.) prob.root.comp3.dpmat[None]['x1'] = 0. prob.root.comp3.dpmat[None]['x2'] = 0. self.assertEqual(prob.root.comp3.dpmat[None]['x1'], 0.) self.assertEqual(prob.root.comp3.dpmat[None]['x2'], 0.) prob.root._transfer_data(mode='fwd', deriv=True) self.assertEqual(prob.root.comp3.dpmat[None]['x1'], 7.) self.assertEqual(prob.root.comp3.dpmat[None]['x2'], 11.)
def test_bad_size(self): class BadComp(SimpleArrayComp): def jacobian(self, params, unknowns, resids): """Analytical derivatives""" J = {} J[('y', 'x')] = np.zeros((3, 3)) return J prob = Problem() prob.root = Group() prob.root.add('comp', BadComp()) prob.root.add('p1', ParamComp('x', np.ones([2]))) prob.root.connect('p1.x', 'comp.x') prob.setup(check=False) prob.run() try: data = prob.check_partial_derivatives(out_stream=None) except Exception as err: msg = "Jacobian in component 'comp' between the" + \ " variables 'x' and 'y' is the wrong size. " + \ "It should be 2 by 2" self.assertEqual(str(err), msg) else: self.fail("Error expected")
def test_full_model_fd_double_diamond_grouped(self): prob = Problem() prob.root = ConvergeDivergeGroups() prob.setup(check=False) prob.run() prob.root.fd_options['force_fd'] = True param_list = ['sub1.comp1.x1'] unknown_list = ['comp7.y1'] J = prob.calc_gradient(param_list, unknown_list, mode='fwd', return_format='dict') assert_rel_error(self, J['comp7.y1']['sub1.comp1.x1'][0][0], -40.75, 1e-6) prob.root.fd_options['form'] = 'central' J = prob.calc_gradient(param_list, unknown_list, mode='fwd', return_format='dict') assert_rel_error(self, J['comp7.y1']['sub1.comp1.x1'][0][0], -40.75, 1e-6)
def test_fan_in_serial_sets(self): prob = Problem(impl=impl) prob.root = FanInGrouped() prob.root.ln_solver = LinearGaussSeidel() prob.root.sub.ln_solver = LinearGaussSeidel() # Parallel Groups prob.driver.add_param('p1.x1') prob.driver.add_param('p2.x2') prob.driver.add_objective('comp3.y') prob.setup(check=False) prob.run() param_list = ['p1.x1', 'p2.x2'] unknown_list = ['comp3.y'] J = prob.calc_gradient(param_list, unknown_list, mode='rev', return_format='dict') assert_rel_error(self, J['comp3.y']['p1.x1'][0][0], -6.0, 1e-6) assert_rel_error(self, J['comp3.y']['p2.x2'][0][0], 35.0, 1e-6) J = prob.calc_gradient(param_list, unknown_list, mode='fwd', return_format='dict') assert_rel_error(self, J['comp3.y']['p1.x1'][0][0], -6.0, 1e-6) assert_rel_error(self, J['comp3.y']['p2.x2'][0][0], 35.0, 1e-6)
def test_fan_out_serial_sets(self): prob = Problem(impl=impl) prob.root = FanOutGrouped() prob.root.ln_solver = LinearGaussSeidel() prob.root.sub.ln_solver = LinearGaussSeidel() # Parallel Groups prob.driver.add_param('p.x') prob.driver.add_constraint('c2.y') prob.driver.add_constraint('c3.y') prob.setup(check=False) prob.run() unknown_list = ['c2.y', 'c3.y'] param_list = ['p.x'] J = prob.calc_gradient(param_list, unknown_list, mode='fwd', return_format='dict') assert_rel_error(self, J['c2.y']['p.x'][0][0], -6.0, 1e-6) assert_rel_error(self, J['c3.y']['p.x'][0][0], 15.0, 1e-6) J = prob.calc_gradient(param_list, unknown_list, mode='rev', return_format='dict') assert_rel_error(self, J['c2.y']['p.x'][0][0], -6.0, 1e-6) assert_rel_error(self, J['c3.y']['p.x'][0][0], 15.0, 1e-6)
def test_fan_out_parallel_sets(self): prob = Problem(impl=impl) prob.root = FanOutGrouped() prob.root.ln_solver = LinearGaussSeidel() prob.root.sub.ln_solver = LinearGaussSeidel() # need to set mode to rev before setup. Otherwise the sub-vectors # for the parallel set vars won't get allocated. prob.root.ln_solver.options['mode'] = 'rev' prob.root.sub.ln_solver.options['mode'] = 'rev' # Parallel Groups prob.driver.add_param('p.x') prob.driver.add_constraint('c2.y') prob.driver.add_constraint('c3.y') prob.driver.parallel_derivs(['c2.y','c3.y']) self.assertEqual(prob.driver.outputs_of_interest(), [('c2.y','c3.y')]) prob.setup(check=False) prob.run() unknown_list = ['c2.y', 'c3.y'] param_list = ['p.x'] J = prob.calc_gradient(param_list, unknown_list, mode='rev', return_format='dict') assert_rel_error(self, J['c2.y']['p.x'][0][0], -6.0, 1e-6) assert_rel_error(self, J['c3.y']['p.x'][0][0], 15.0, 1e-6) J = prob.calc_gradient(param_list, unknown_list, mode='fwd', return_format='dict') assert_rel_error(self, J['c2.y']['p.x'][0][0], -6.0, 1e-6) assert_rel_error(self, J['c3.y']['p.x'][0][0], 15.0, 1e-6)
def test_only_params_recorded(self): prob = Problem() prob.root = ConvergeDiverge() prob.driver.add_recorder(self.recorder) self.recorder.options['record_params'] = True self.recorder.options['record_resids'] = False self.recorder.options['record_unknowns'] = False prob.setup(check=False) t0, t1 = run_problem(prob) self.recorder.close() coordinate = ['Driver', (1,)] expected_params = [ ("comp1.x1", 2.0), ("comp2.x1", 8.0), ("comp3.x1", 6.0), ("comp4.x1", 4.0), ("comp4.x2", 21.0), ("comp5.x1", 46.0), ("comp6.x1", -93.0), ("comp7.x1", 36.8), ("comp7.x2", -46.5) ] self.assertIterationDataRecorded(((coordinate, (t0, t1), expected_params, None, None),), self.eps)
def test_single_diamond_grouped(self): prob = Problem() prob.root = SingleDiamondGrouped() prob.root.ln_solver = ScipyGMRES() prob.setup(check=False) prob.run() param_list = ['p.x'] unknown_list = ['comp4.y1', 'comp4.y2'] J = prob.calc_gradient(param_list, unknown_list, mode='fwd', return_format='dict') assert_rel_error(self, J['comp4.y1']['p.x'][0][0], 25, 1e-6) assert_rel_error(self, J['comp4.y2']['p.x'][0][0], -40.5, 1e-6) J = prob.calc_gradient(param_list, unknown_list, mode='rev', return_format='dict') assert_rel_error(self, J['comp4.y1']['p.x'][0][0], 25, 1e-6) assert_rel_error(self, J['comp4.y2']['p.x'][0][0], -40.5, 1e-6) J = prob.calc_gradient(param_list, unknown_list, mode='fd', return_format='dict') assert_rel_error(self, J['comp4.y1']['p.x'][0][0], 25, 1e-6) assert_rel_error(self, J['comp4.y2']['p.x'][0][0], -40.5, 1e-6)
def test_converge_diverge_groups(self): prob = Problem() prob.root = ConvergeDivergeGroups() prob.root.ln_solver = ScipyGMRES() prob.setup(check=False) prob.run() # Make sure value is fine. assert_rel_error(self, prob['comp7.y1'], -102.7, 1e-6) param_list = ['p.x'] unknown_list = ['comp7.y1'] J = prob.calc_gradient(param_list, unknown_list, mode='fwd', return_format='dict') assert_rel_error(self, J['comp7.y1']['p.x'][0][0], -40.75, 1e-6) J = prob.calc_gradient(param_list, unknown_list, mode='rev', return_format='dict') assert_rel_error(self, J['comp7.y1']['p.x'][0][0], -40.75, 1e-6) J = prob.calc_gradient(param_list, unknown_list, mode='fd', return_format='dict') assert_rel_error(self, J['comp7.y1']['p.x'][0][0], -40.75, 1e-6)
def test_fan_in_grouped(self): prob = Problem() prob.root = FanInGrouped() prob.root.ln_solver = ScipyGMRES() param_list = ['p1.x1', 'p2.x2'] unknown_list = ['comp3.y'] prob.setup(check=False) prob.run() J = prob.calc_gradient(param_list, unknown_list, mode='fwd', return_format='dict') assert_rel_error(self, J['comp3.y']['p1.x1'][0][0], -6.0, 1e-6) assert_rel_error(self, J['comp3.y']['p2.x2'][0][0], 35.0, 1e-6) J = prob.calc_gradient(param_list, unknown_list, mode='rev', return_format='dict') assert_rel_error(self, J['comp3.y']['p1.x1'][0][0], -6.0, 1e-6) assert_rel_error(self, J['comp3.y']['p2.x2'][0][0], 35.0, 1e-6)
def test_double_arraycomp(self): # Mainly testing a bug in the array return for multiple arrays group = Group() group.add('x_param1', ParamComp('x1', np.ones((2))), promotes=['*']) group.add('x_param2', ParamComp('x2', np.ones((2))), promotes=['*']) group.add('mycomp', DoubleArrayComp(), promotes=['*']) prob = Problem() prob.root = group prob.root.ln_solver = ScipyGMRES() prob.setup(check=False) prob.run() Jbase = group.mycomp.JJ J = prob.calc_gradient(['x1', 'x2'], ['y1', 'y2'], mode='fwd', return_format='array') diff = np.linalg.norm(J - Jbase) assert_rel_error(self, diff, 0.0, 1e-8) J = prob.calc_gradient(['x1', 'x2'], ['y1', 'y2'], mode='fd', return_format='array') diff = np.linalg.norm(J - Jbase) assert_rel_error(self, diff, 0.0, 1e-8) J = prob.calc_gradient(['x1', 'x2'], ['y1', 'y2'], mode='rev', return_format='array') diff = np.linalg.norm(J - Jbase) assert_rel_error(self, diff, 0.0, 1e-8)
def test_index_error_messages_con(self): prob = Problem() prob.root = Group() prob.root.fd_options['force_fd'] = True prob.root.ln_solver.options['mode'] = 'auto' prob.root.add('myparams', ParamComp('x', np.zeros(4))) prob.root.add('rosen', Rosenbrock(4)) prob.root.connect('myparams.x', 'rosen.x') prob.driver = MySimpleDriver() prob.driver.add_param('myparams.x') prob.driver.add_constraint('rosen.xxx', indices=[4]) prob.setup(check=False) # Make sure we can't do this with self.assertRaises(IndexError) as cm: prob.run() msg = "Index for constraint 'rosen.xxx' is out of bounds. " msg += "Requested index: [4], " msg += "Parameter shape: (4,)." raised_error = str(cm.exception) raised_error = raised_error.replace('(4L,', '(4,') self.assertEqual(msg, raised_error)
def test_Sellar_state_SLSQP(self): prob = Problem() prob.root = SellarStateConnection() prob.driver = ScipyOptimizer() prob.driver.options['optimizer'] = 'SLSQP' prob.driver.options['tol'] = 1.0e-8 prob.driver.add_param('z', low=np.array([-10.0, 0.0]), high=np.array([10.0, 10.0])) prob.driver.add_param('x', low=0.0, high=10.0) prob.driver.add_objective('obj') prob.driver.add_constraint('con1') prob.driver.add_constraint('con2') 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_only_resids_recorded(self): prob = Problem() prob.root = ConvergeDiverge() prob.driver.add_recorder(self.recorder) self.recorder.options['record_params'] = False self.recorder.options['record_unknowns'] = False self.recorder.options['record_resids'] = True prob.setup(check=False) t0, t1 = run_problem(prob) prob.cleanup() # closes recorders coordinate = [0, 'Driver', (1, )] expected_resids = [ ("comp1.y1", 0.0), ("comp1.y2", 0.0), ("comp2.y1", 0.0), ("comp3.y1", 0.0), ("comp4.y1", 0.0), ("comp4.y2", 0.0), ("comp5.y1", 0.0), ("comp6.y1", 0.0), ("comp7.y1", 0.0), ("p.x", 0.0) ] self.assertIterationDataRecorded(((coordinate, (t0, t1), None, None, expected_resids),), self.eps)
def test_multilevel_record(self): prob = Problem() prob.root = ExampleGroup() prob.root.G2.G1.nl_solver.add_recorder(self.recorder) prob.driver.add_recorder(self.recorder) self.recorder.options["record_params"] = True self.recorder.options["record_resids"] = True prob.setup(check=False) t0, t1 = run_problem(prob) prob.cleanup() # closes recorders solver_coordinate = ["Driver", (1,), "root", (1,), "G2", (1,), "G1", (1,)] g1_expected_params = [("C2.x", 5.0)] g1_expected_unknowns = [("C2.y", 10.0)] g1_expected_resids = [("C2.y", 0.0)] g1_expected = (g1_expected_params, g1_expected_unknowns, g1_expected_resids) driver_coordinate = ["Driver", (1,)] driver_expected_params = [("G3.C3.x", 10.0)] driver_expected_unknowns = [("G2.C1.x", 5.0), ("G2.G1.C2.y", 10.0), ("G3.C3.y", 20.0), ("G3.C4.y", 40.0)] driver_expected_resids = [("G2.C1.x", 0.0), ("G2.G1.C2.y", 0.0), ("G3.C3.y", 0.0), ("G3.C4.y", 0.0)] expected = [] expected.append((solver_coordinate, (t0, t1), g1_expected_params, g1_expected_unknowns, g1_expected_resids)) expected.append( (driver_coordinate, (t0, t1), driver_expected_params, driver_expected_unknowns, driver_expected_resids) ) self.assertIterationDataRecorded(expected, self.eps)
def test_sublevel_record(self): prob = Problem() prob.root = ExampleGroup() prob.root.G2.G1.nl_solver.add_recorder(self.recorder) self.recorder.options['record_params'] = True self.recorder.options['record_resids'] = True prob.setup(check=False) t0, t1 = run_problem(prob) prob.cleanup() # closes recorders coordinate = [0, 'Driver', (1,), "root", (1,), "G2", (1,), "G1", (1,)] expected_params = [ ("C2.x", 5.0) ] expected_unknowns = [ ("C2.y", 10.0) ] expected_resids = [ ("C2.y", 0.0) ] self.assertIterationDataRecorded(((coordinate, (t0, t1), expected_params, expected_unknowns, expected_resids),), self.eps)
def test_simple_matvec(self): class VerificationComp(SimpleCompDerivMatVec): def jacobian(self, params, unknowns, resids): raise RuntimeError( "Derivative functions on this comp should not run.") def apply_linear(self, params, unknowns, dparams, dunknowns, dresids, mode): raise RuntimeError( "Derivative functions on this comp should not run.") sub = Group() sub.add('mycomp', VerificationComp()) prob = Problem() prob.root = Group() prob.root.add('sub', sub) prob.root.add('x_param', ParamComp('x', 1.0)) prob.root.connect('x_param.x', "sub.mycomp.x") sub.fd_options['force_fd'] = True prob.setup(check=False) prob.run() J = prob.calc_gradient(['x_param.x'], ['sub.mycomp.y'], mode='fwd', return_format='dict') assert_rel_error(self, J['sub.mycomp.y']['x_param.x'][0][0], 2.0, 1e-6) J = prob.calc_gradient(['x_param.x'], ['sub.mycomp.y'], mode='rev', return_format='dict') assert_rel_error(self, J['sub.mycomp.y']['x_param.x'][0][0], 2.0, 1e-6)
def test_driver_param_indices_slsqp_force_fd(self): """ Test driver param indices with ScipyOptimizer SLSQP and force_fd=True """ prob = Problem() prob.root = SellarStateConnection() prob.root.fd_options['force_fd'] = True prob.driver = ScipyOptimizer() prob.driver.options['optimizer'] = 'SLSQP' prob.driver.options['tol'] = 1.0e-8 prob.driver.add_desvar('z', low=np.array([-10.0]), high=np.array([10.0]),indices=[0]) prob.driver.add_desvar('x', low=0.0, high=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['z'][1] = 0.0 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_driver_param_indices_snopt_force_fd_shift(self): """ Testt driver param indices with pyOptSparse and force_fd=True """ prob = Problem() prob.root = SellarStateConnection() prob.root.fd_options['force_fd'] = True prob.driver.add_desvar('z', low=np.array([-10.0, -10.0]), high=np.array([10.0, 10.0]), indices=[1]) prob.driver.add_desvar('x', low=0.0, high=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['z'][1] = 0.0 prob.run() J = prob.calc_gradient(['x', 'z'], ['obj'], mode='fd', return_format='array') assert_rel_error(self, J[0][1], 1.78402, 1e-3)
def test_Sellar_state_SLSQP(self): """ Baseline Sellar test case without specifying indices. """ prob = Problem() prob.root = SellarStateConnection() prob.driver = ScipyOptimizer() prob.driver.options['optimizer'] = 'SLSQP' prob.driver.options['tol'] = 1.0e-8 prob.driver.add_desvar('z', low=np.array([-10.0, 0.0]), high=np.array([10.0, 10.0])) prob.driver.add_desvar('x', low=0.0, high=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_includes_and_excludes(self): prob = Problem() prob.root = ConvergeDiverge() prob.driver.add_recorder(self.recorder) self.recorder.options['includes'] = ['comp1.*'] self.recorder.options['excludes'] = ["*.y2"] prob.setup(check=False) prob.run() expected_params = [ ("comp1.x1", 2.0) ] expected_unknowns = [ ("comp1.y1", 8.0) ] expected_resids = [ ("comp1.y1", 0.0) ] expected = (expected_params, expected_unknowns, expected_resids) self.assertDatasetEquals( [(['Driver', (1,)], expected)], self.eps )
def test_driver_param_indices_snopt(self): """ Test driver param indices with pyOptSparse and force_fd=False """ prob = Problem() prob.root = SellarStateConnection() prob.root.fd_options['force_fd'] = False prob.driver = pyOptSparseDriver() prob.driver.add_desvar('z', low=np.array([-10.0]), high=np.array([10.0]),indices=[0]) prob.driver.add_desvar('x', low=0.0, high=10.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['z'][1] = 0.0 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_includes_and_excludes(self): prob = Problem() prob.root = ConvergeDiverge() prob.driver.add_recorder(self.recorder) self.recorder.options['includes'] = ['comp1.*'] self.recorder.options['excludes'] = ["*.y2"] self.recorder.options['record_params'] = True self.recorder.options['record_resids'] = True prob.setup(check=False) t0, t1 = run_problem(prob) prob.cleanup() # closes recorders coordinate = [0, 'Driver', (1,)] expected_params = [ ("comp1.x1", 2.0) ] expected_unknowns = [ ("comp1.y1", 8.0) ] expected_resids = [ ("comp1.y1", 0.0) ] self.assertIterationDataRecorded(((coordinate, (t0, t1), expected_params, expected_unknowns, expected_resids),), self.eps)
def test_only_params_recorded(self): prob = Problem() prob.root = ConvergeDiverge() prob.driver.add_recorder(self.recorder) self.recorder.options["record_params"] = True self.recorder.options["record_resids"] = False self.recorder.options["record_unknowns"] = False prob.setup(check=False) t0, t1 = run_problem(prob) prob.cleanup() # closes recorders coordinate = ["Driver", (1,)] expected_params = [ ("comp1.x1", 2.0), ("comp2.x1", 8.0), ("comp3.x1", 6.0), ("comp4.x1", 4.0), ("comp4.x2", 21.0), ("comp5.x1", 46.0), ("comp6.x1", -93.0), ("comp7.x1", 36.8), ("comp7.x2", -46.5), ] self.assertIterationDataRecorded(((coordinate, (t0, t1), expected_params, None, None),), self.eps)
def test_converge_diverge_groups(self): prob = Problem() prob.root = Group() prob.root.add('sub', ConvergeDivergeGroups()) param_list = ['sub.p.x'] unknown_list = ['sub.comp7.y1'] prob.setup(check=False) prob.run() J = prob.calc_gradient(param_list, unknown_list, mode='fwd', return_format='dict') assert_rel_error(self, J['sub.comp7.y1']['sub.p.x'][0][0], -40.75, 1e-6) J = prob.calc_gradient(param_list, unknown_list, mode='rev', return_format='dict') assert_rel_error(self, J['sub.comp7.y1']['sub.p.x'][0][0], -40.75, 1e-6)
def test_only_unknowns_recorded(self): prob = Problem() prob.root = ConvergeDiverge() prob.driver.add_recorder(self.recorder) prob.setup(check=False) t0, t1 = run_problem(prob) prob.cleanup() # closes recorders coordinate = [0, 'Driver', (1, )] expected_unknowns = [ ("comp1.y1", 8.0), ("comp1.y2", 6.0), ("comp2.y1", 4.0), ("comp3.y1", 21.0), ("comp4.y1", 46.0), ("comp4.y2", -93.0), ("comp5.y1", 36.8), ("comp6.y1", -46.5), ("comp7.y1", -102.7), ("p.x", 2.0) ] self.assertIterationDataRecorded(((coordinate, (t0, t1), None, expected_unknowns, None),), self.eps)
def test_metadata_recorded(self): prob = Problem(impl=impl) prob.root = FanInGrouped() rec = DumpRecorder(out=self.filename) rec.options['record_metadata'] = True rec.options['includes'] = ['p1.x1', 'p2.x2', 'comp3.y'] prob.driver.add_recorder(rec) prob.setup(check=False) rec.close() with open(self.expected_filename, 'r') as dumpfile: params = iteritems(prob.root.params) unknowns = iteritems(prob.root.unknowns) self.assertEqual("Metadata:\n", dumpfile.readline()) self.assertEqual("Params:\n", dumpfile.readline()) for name, metadata in params: fmat = " {0}: {1}\n".format(name, metadata) self.assertEqual(fmat, dumpfile.readline()) self.assertEqual("Unknowns:\n", dumpfile.readline()) for name, metadata in unknowns: fmat = " {0}: {1}\n".format(name, metadata) self.assertEqual(fmat, dumpfile.readline())
def test_fan_out_serial_sets(self): prob = Problem(impl=impl) prob.root = FanOutGrouped() prob.root.ln_solver = LinearGaussSeidel() prob.root.sub.ln_solver = LinearGaussSeidel() # Parallel Groups prob.driver.add_desvar('p.x') prob.driver.add_constraint('c2.y', upper=0.0) prob.driver.add_constraint('c3.y', upper=0.0) prob.setup(check=False) prob.run() unknown_list = ['c2.y', 'c3.y'] indep_list = ['p.x'] J = prob.calc_gradient(indep_list, unknown_list, mode='fwd', return_format='dict') assert_rel_error(self, J['c2.y']['p.x'][0][0], -6.0, 1e-6) assert_rel_error(self, J['c3.y']['p.x'][0][0], 15.0, 1e-6) J = prob.calc_gradient(indep_list, unknown_list, mode='rev', return_format='dict') assert_rel_error(self, J['c2.y']['p.x'][0][0], -6.0, 1e-6) assert_rel_error(self, J['c3.y']['p.x'][0][0], 15.0, 1e-6)
def test_fan_in_serial_sets(self): prob = Problem(impl=impl) prob.root = FanInGrouped() prob.root.ln_solver = LinearGaussSeidel() prob.root.sub.ln_solver = LinearGaussSeidel() # Parallel Groups prob.driver.add_desvar('p1.x1') prob.driver.add_desvar('p2.x2') prob.driver.add_objective('comp3.y') prob.setup(check=False) prob.run() indep_list = ['p1.x1', 'p2.x2'] unknown_list = ['comp3.y'] J = prob.calc_gradient(indep_list, unknown_list, mode='rev', return_format='dict') assert_rel_error(self, J['comp3.y']['p1.x1'][0][0], -6.0, 1e-6) assert_rel_error(self, J['comp3.y']['p2.x2'][0][0], 35.0, 1e-6) J = prob.calc_gradient(indep_list, unknown_list, mode='fwd', return_format='dict') assert_rel_error(self, J['comp3.y']['p1.x1'][0][0], -6.0, 1e-6) assert_rel_error(self, J['comp3.y']['p2.x2'][0][0], 35.0, 1e-6)
def test_array2D_index_connection(self): group = Group() group.add('x_param', IndepVarComp('x', np.ones((2, 2))), promotes=['*']) sub = group.add('sub', Group(), promotes=['*']) sub.add('mycomp', ArrayComp2D(), promotes=['x', 'y']) group.add('obj', ExecComp('b = a')) group.connect('y', 'obj.a', src_indices=[3]) prob = Problem() prob.root = group prob.root.ln_solver = LinearGaussSeidel() prob.setup(check=False) prob.run() J = prob.calc_gradient(['x'], ['obj.b'], mode='fwd', return_format='dict') Jbase = prob.root.sub.mycomp._jacobian_cache assert_rel_error(self, Jbase[('y', 'x')][3][0], J['obj.b']['x'][0][0], 1e-8) assert_rel_error(self, Jbase[('y', 'x')][3][1], J['obj.b']['x'][0][1], 1e-8) assert_rel_error(self, Jbase[('y', 'x')][3][2], J['obj.b']['x'][0][2], 1e-8) assert_rel_error(self, Jbase[('y', 'x')][3][3], J['obj.b']['x'][0][3], 1e-8) J = prob.calc_gradient(['x'], ['obj.b'], mode='rev', return_format='dict') Jbase = prob.root.sub.mycomp._jacobian_cache assert_rel_error(self, Jbase[('y', 'x')][3][0], J['obj.b']['x'][0][0], 1e-8) assert_rel_error(self, Jbase[('y', 'x')][3][1], J['obj.b']['x'][0][1], 1e-8) assert_rel_error(self, Jbase[('y', 'x')][3][2], J['obj.b']['x'][0][2], 1e-8) assert_rel_error(self, Jbase[('y', 'x')][3][3], J['obj.b']['x'][0][3], 1e-8)
def test_calc_gradient_multiple_params(self): prob = Problem() prob.root = FanIn() prob.setup(check=False) prob.run() param_list = ['p1.x1', 'p2.x2'] unknown_list = ['comp3.y'] # check that calc_gradient returns proper dict value when mode is 'fwd' J = prob.calc_gradient(param_list, unknown_list, mode='fwd', return_format='dict') np.testing.assert_almost_equal(J['comp3.y']['p2.x2'], np.array([[ 35.]])) np.testing.assert_almost_equal(J['comp3.y']['p1.x1'], np.array([[ -6.]])) # check that calc_gradient returns proper array value when mode is 'fwd' J = prob.calc_gradient(param_list, unknown_list, mode='fwd', return_format='array') np.testing.assert_almost_equal(J, np.array([[-6., 35.]])) # check that calc_gradient returns proper dict value when mode is 'rev' J = prob.calc_gradient(param_list, unknown_list, mode='rev', return_format='dict') np.testing.assert_almost_equal(J['comp3.y']['p2.x2'], np.array([[ 35.]])) np.testing.assert_almost_equal(J['comp3.y']['p1.x1'], np.array([[ -6.]])) # check that calc_gradient returns proper array value when mode is 'rev' J = prob.calc_gradient(param_list, unknown_list, mode='rev', return_format='array') np.testing.assert_almost_equal(J, np.array([[-6., 35.]])) # check that calc_gradient returns proper dict value when mode is 'fd' J = prob.calc_gradient(param_list, unknown_list, mode='fd', return_format='dict') np.testing.assert_almost_equal(J['comp3.y']['p2.x2'], np.array([[ 35.]])) np.testing.assert_almost_equal(J['comp3.y']['p1.x1'], np.array([[ -6.]])) # check that calc_gradient returns proper array value when mode is 'fd' J = prob.calc_gradient(param_list, unknown_list, mode='fd', return_format='array') np.testing.assert_almost_equal(J, np.array([[-6., 35.]]))
def test_solver_record(self): prob = Problem() prob.root = ConvergeDiverge() prob.root.nl_solver.add_recorder(self.recorder) self.recorder.options['record_params'] = True self.recorder.options['record_resids'] = True prob.setup(check=False) t0, t1 = run_problem(prob) prob.cleanup() # closes recorders coordinate = [0, 'Driver', (1, ), "root", (1, )] expected_params = [("comp1.x1", 2.0), ("comp2.x1", 8.0), ("comp3.x1", 6.0), ("comp4.x1", 4.0), ("comp4.x2", 21.0), ("comp5.x1", 46.0), ("comp6.x1", -93.0), ("comp7.x1", 36.8), ("comp7.x2", -46.5)] expected_unknowns = [("comp1.y1", 8.0), ("comp1.y2", 6.0), ("comp2.y1", 4.0), ("comp3.y1", 21.0), ("comp4.y1", 46.0), ("comp4.y2", -93.0), ("comp5.y1", 36.8), ("comp6.y1", -46.5), ("comp7.y1", -102.7), ("p.x", 2.0)] expected_resids = [("comp1.y1", 0.0), ("comp1.y2", 0.0), ("comp2.y1", 0.0), ("comp3.y1", 0.0), ("comp4.y1", 0.0), ("comp4.y2", 0.0), ("comp5.y1", 0.0), ("comp6.y1", 0.0), ("comp7.y1", 0.0), ("p.x", 0.0)] self.assertIterationDataRecorded( ((coordinate, (t0, t1), expected_params, expected_unknowns, expected_resids), ), self.eps)
def test_metadata_recorded(self): prob = Problem(impl=impl) prob.root = FanInGrouped() rec = DumpRecorder(out=self.filename) rec.options["record_metadata"] = True rec.options["includes"] = ["p1.x1", "p2.x2", "comp3.y"] prob.driver.add_recorder(rec) prob.setup(check=False) prob.cleanup() with open(self.expected_filename, "r") as dumpfile: params = iteritems(prob.root.params) unknowns = iteritems(prob.root.unknowns) self.assertEqual("Metadata:\n", dumpfile.readline()) self.assertEqual("Params:\n", dumpfile.readline()) for name, metadata in params: fmat = " {0}: {1}\n".format(name, metadata) self.assertEqual(fmat, dumpfile.readline()) self.assertEqual("Unknowns:\n", dumpfile.readline()) for name, metadata in unknowns: fmat = " {0}: {1}\n".format(name, metadata) self.assertEqual(fmat, dumpfile.readline())
def test_unsupported_array(self): top = Problem() top.root = Group() top.root.add('my_comp', VarComponent()) top.setup(check=False) top.run() sb = Namelist(top.root.my_comp) top['my_comp.arrayvar'] = zeros([2, 2, 2], dtype=numpy_float32) sb.set_filename(self.filename) sb.add_group('Test') sb.add_var("arrayvar") try: sb.generate() except RuntimeError as err: self.assertEqual(str(err), "Don't know how to handle array of" + \ " 3 dimensions") else: self.fail('RuntimeError expected')
def test_index_error_messages_con(self): prob = Problem() prob.root = Group() prob.root.fd_options['force_fd'] = True prob.root.ln_solver.options['mode'] = 'auto' prob.root.add('myparams', IndepVarComp('x', np.zeros(4))) prob.root.add('rosen', Rosenbrock(4)) prob.root.connect('myparams.x', 'rosen.x') prob.driver = MySimpleDriver() prob.driver.add_desvar('myparams.x') prob.driver.add_constraint('rosen.xxx', upper=0.0, indices=[4]) prob.setup(check=False) # Make sure we can't do this with self.assertRaises(IndexError) as cm: prob.run() msg = "Index for constraint 'rosen.xxx' is out of bounds. " msg += "Requested index: [4], " msg += "shape: (4,)." raised_error = str(cm.exception) raised_error = raised_error.replace('(4L,', '(4,') self.assertEqual(msg, raised_error)
def test_2Darray_read(self): namelist1 = "Testing\n" + \ "$GROUP\n" + \ " arrayvartwod(1,1) = 12, 24, 36\n" + \ " arrayvartwod(1,2) = 33, 66, 99\n" + \ "$END\n" outfile = open(self.filename, 'w') outfile.write(namelist1) outfile.close() top = Problem() top.root = Group() top.root.add('my_comp', VarComponent()) top.setup(check=False) top.run() sb = Namelist(top.root.my_comp) sb.set_filename(self.filename) sb.parse_file() sb.load_model() # Unchanged self.assertEqual(top['my_comp.arrayvartwod'][0][0], 12) self.assertEqual(top['my_comp.arrayvartwod'][1][2], 99)
def test_2Darray_write(self): top = Problem() top.root = Group() top.root.add('my_comp', VarComponent()) top.setup(check=False) top.run() sb = Namelist(top.root.my_comp) top['my_comp.arrayvar'] = zeros([3, 2], dtype=numpy_float32) top['my_comp.arrayvar'][0, 1] = 3.7 top['my_comp.arrayvar'][2, 0] = 7.88 sb.set_filename(self.filename) sb.add_group('Test') sb.add_var("arrayvar") sb.generate() f = open(self.filename, 'r') contents = f.read() compare = "\n" + \ "&Test\n" + \ " arrayvar(1,1) = 0.0, 3.700000047683716, \n" + \ "arrayvar(1,2) = 0.0, 0.0, \n" + \ "arrayvar(1,3) = 7.880000114440918, 0.0, \n" + \ "/\n" self.assertEqual(contents, compare)
def test_read3(self): # Parse a single group in a deck with non-unique group names namelist1 = "Testing\n" + \ "$GROUP\n" + \ " intvar = 99\n" + \ "$END\n" + \ "$GROUP\n" + \ " floatvar = 3.5e-23\n" + \ "$END\n" outfile = open(self.filename, 'w') outfile.write(namelist1) outfile.close() top = Problem() top.root = Group() top.root.add('my_comp', VarComponent()) top.setup(check=False) top.run() sb = Namelist(top.root.my_comp) sb.set_filename(self.filename) sb.parse_file() sb.load_model(single_group=1) # Unchanged self.assertEqual(top['my_comp.intvar'], 333) # Changed self.assertEqual(top['my_comp.floatvar'], 3.5e-23)
def test_only_resids_recorded(self): prob = Problem() prob.root = ConvergeDiverge() prob.driver.add_recorder(self.recorder) self.recorder.options["record_params"] = False self.recorder.options["record_unknowns"] = False self.recorder.options["record_resids"] = True prob.setup(check=False) t0, t1 = run_problem(prob) prob.cleanup() # closes recorders coordinate = ["Driver", (1,)] expected_resids = [ ("comp1.y1", 0.0), ("comp1.y2", 0.0), ("comp2.y1", 0.0), ("comp3.y1", 0.0), ("comp4.y1", 0.0), ("comp4.y2", 0.0), ("comp5.y1", 0.0), ("comp6.y1", 0.0), ("comp7.y1", 0.0), ("p.x", 0.0), ] self.assertIterationDataRecorded(((coordinate, (t0, t1), None, None, expected_resids),), self.eps)
def test_too_few_procs(self): size = 3 group = Group() group.add('P', IndepVarComp('x', numpy.ones(size))) group.add('C1', DistribExecComp(['y=2.0*x'], arr_size=size, x=numpy.zeros(size), y=numpy.zeros(size))) group.add('C2', ExecComp(['z=3.0*y'], y=numpy.zeros(size), z=numpy.zeros(size))) prob = Problem(impl=impl) prob.root = group prob.root.ln_solver = LinearGaussSeidel() prob.root.connect('P.x', 'C1.x') prob.root.connect('C1.y', 'C2.y') try: prob.setup(check=False) except Exception as err: self.assertEqual(str(err), "This problem was given 1 MPI processes, " "but it requires between 2 and 2.") else: if MPI: # pragma: no cover self.fail("Exception expected")
def test_src_indices_error(self): size = 3 group = Group() group.add('P', IndepVarComp('x', numpy.ones(size))) group.add('C1', DistribExecComp(['y=2.0*x'], arr_size=size, x=numpy.zeros(size), y=numpy.zeros(size))) group.add('C2', ExecComp(['z=3.0*y'], y=numpy.zeros(size), z=numpy.zeros(size))) prob = Problem(impl=impl) prob.root = group prob.root.ln_solver = LinearGaussSeidel() prob.root.connect('P.x', 'C1.x') prob.root.connect('C1.y', 'C2.y') prob.driver.add_desvar('P.x') prob.driver.add_objective('C1.y') try: prob.setup(check=False) except Exception as err: self.assertEqual(str(err), "'C1.y' is a distributed variable and may not be used as a " "design var, objective, or constraint.") else: if MPI: # pragma: no cover self.fail("Exception expected")
def test_array_values(self): prob = Problem() prob.root = Group() prob.root.add('pc', ParamComp('x', np.zeros((2, 3)), units='degC'), promotes=['x']) prob.root.add('uc', UnitComp(shape=(2, 3), param_name='x', out_name='x_out', units='degF'), promotes=['x', 'x_out']) prob.setup(check=False) prob.run() assert_rel_error(self, prob['x_out'], np.array([[32., 32., 32.], [32., 32., 32.]]), 1e-6) param_list = ['x'] unknown_list = ['x_out'] # Forward Mode J = prob.calc_gradient(param_list, unknown_list, mode='fwd', return_format='dict') assert_rel_error(self, J['x_out']['x'], 1.8 * np.eye(6), 1e-6) # Reverse Mode J = prob.calc_gradient(param_list, unknown_list, mode='rev', return_format='dict') assert_rel_error(self, J['x_out']['x'], 1.8 * np.eye(6), 1e-6)
def test_only_unknowns_recorded(self): prob = Problem() prob.root = ConvergeDiverge() prob.driver.add_recorder(self.recorder) prob.setup(check=False) t0, t1 = run_problem(prob) prob.cleanup() # closes recorders coordinate = ["Driver", (1,)] expected_unknowns = [ ("comp1.y1", 8.0), ("comp1.y2", 6.0), ("comp2.y1", 4.0), ("comp3.y1", 21.0), ("comp4.y1", 46.0), ("comp4.y2", -93.0), ("comp5.y1", 36.8), ("comp6.y1", -46.5), ("comp7.y1", -102.7), ("p.x", 2.0), ] self.assertIterationDataRecorded(((coordinate, (t0, t1), None, expected_unknowns, None),), self.eps)
def test_calc_gradient_multiple_params(self): prob = Problem() prob.root = FanIn() prob.setup(check=False) prob.run() param_list = ['p1.x1', 'p2.x2'] unknown_list = ['comp3.y'] # check that calc_gradient returns proper dict value when mode is 'fwd' J = prob.calc_gradient(param_list, unknown_list, mode='fwd', return_format='dict') np.testing.assert_almost_equal(J['comp3.y']['p2.x2'], np.array([[35.]])) np.testing.assert_almost_equal(J['comp3.y']['p1.x1'], np.array([[-6.]])) # check that calc_gradient returns proper array value when mode is 'fwd' J = prob.calc_gradient(param_list, unknown_list, mode='fwd', return_format='array') np.testing.assert_almost_equal(J, np.array([[-6., 35.]])) # check that calc_gradient returns proper dict value when mode is 'rev' J = prob.calc_gradient(param_list, unknown_list, mode='rev', return_format='dict') np.testing.assert_almost_equal(J['comp3.y']['p2.x2'], np.array([[35.]])) np.testing.assert_almost_equal(J['comp3.y']['p1.x1'], np.array([[-6.]])) # check that calc_gradient returns proper array value when mode is 'rev' J = prob.calc_gradient(param_list, unknown_list, mode='rev', return_format='array') np.testing.assert_almost_equal(J, np.array([[-6., 35.]])) # check that calc_gradient returns proper dict value when mode is 'fd' J = prob.calc_gradient(param_list, unknown_list, mode='fd', return_format='dict') np.testing.assert_almost_equal(J['comp3.y']['p2.x2'], np.array([[35.]])) np.testing.assert_almost_equal(J['comp3.y']['p1.x1'], np.array([[-6.]])) # check that calc_gradient returns proper array value when mode is 'fd' J = prob.calc_gradient(param_list, unknown_list, mode='fd', return_format='array') np.testing.assert_almost_equal(J, np.array([[-6., 35.]]))
def test_subsolver_doesnt_record_metadata(self): prob = Problem() prob.root = ExampleGroup() prob.root.G2.G1.nl_solver.add_recorder(self.recorder) self.recorder.options['record_metadata'] = False prob.setup(check=False) prob.cleanup() # closes recorders self.assertMetadataRecorded(None)
def test_driver_doesnt_record_metadata(self): prob = Problem() prob.root = ConvergeDiverge() prob.driver.add_recorder(self.recorder) self.recorder.options['record_metadata'] = False prob.setup(check=False) prob.cleanup() # closes recorders self.assertMetadataRecorded(None)
def test_root_solver_doesnt_record_metadata(self): prob = Problem() prob.root = ConvergeDiverge() prob.root.nl_solver.add_recorder(self.recorder) self.recorder.options['record_metadata'] = False prob.setup(check=False) self.recorder.close() self.assertMetadataRecorded(None)
def test_unknown_vec(self): top = Problem() top.root = Group() top.root.add('comp', ArrayComp2D(), promotes=['x', 'y']) top.root.add('p1', IndepVarComp('x', np.array([[1.0, 2.0], [3.0, 4.0]])), promotes=['x']) top.root.add('comp2', SimpleComp()) top.root.add('p2', IndepVarComp('x', 3.0)) top.root.connect('p2.x', 'comp2.x') top.setup(check=False) top.run() unknowns = ComplexStepSrcVecWrapper(top.root.comp.unknowns) # Unknowns are always complex y = unknowns['y'] self.assertTrue(y.dtype == np.complex) self.assertEquals(y[0, 1], 46.0 + 0j) # Set an unknown unknowns['y'][0, 1] = 13.0 + 17.0j self.assertEquals(unknowns['y'][0, 1], 13.0 + 17.0j) # Extract flat var cval = unknowns.flat('y') self.assertEquals(cval[1], 13.0 + 17.0j) self.assertEquals(cval.shape[0], 4) unknowns = ComplexStepSrcVecWrapper(top.root.comp2.unknowns) # Unknowns are always complex y = unknowns['y'] self.assertTrue(y.dtype == np.complex) self.assertEquals(y, 6.0 + 0j) # Set an unknown unknowns['y'] = 13.0 + 17.0j self.assertEquals(unknowns['y'], 13.0 + 17.0j) # Extract flat var cval = unknowns.flat('y') self.assertEquals(cval, 13.0 + 17.0j) self.assertEquals(cval.shape[0], 1) # Make sure all other functions work for coverage self.assertEquals(len(unknowns), 1) self.assertTrue('y' in unknowns) plist = [z for z in unknowns] self.assertEquals(plist, ['y']) self.assertEquals(unknowns.keys(), top.root.comp2.unknowns.keys()) self.assertEquals(unknowns.metadata('y'), top.root.comp2.unknowns.metadata('y')) plist1 = [z for z in unknowns.iterkeys()] plist2 = [z for z in top.root.comp2.unknowns.iterkeys()]
def test_multilevel_record(self): # FIXME: this test fails with the csv recorder self.recorder.close() raise unittest.SkipTest('This is not supported by the csv recorder yet.') prob = Problem() prob.root = ExampleGroup() prob.root.G2.G1.nl_solver.add_recorder(self.recorder) prob.driver.add_recorder(self.recorder) self.recorder.options['record_params'] = True self.recorder.options['record_resids'] = True prob.setup(check=False) t0, t1 = run_problem(prob) self.recorder.close() solver_coordinate = ['Driver', (1,), "root", (1,), "G2", (1,), "G1", (1,)] g1_expected_params = [ ("C2.x", 5.0) ] g1_expected_unknowns = [ ("C2.y", 10.0) ] g1_expected_resids = [ ("C2.y", 0.0) ] # g1_expected = (g1_expected_params, g1_expected_unknowns, g1_expected_resids) driver_coordinate = ['Driver', (1,)] driver_expected_params = [ ("G3.C3.x", 10.0) ] driver_expected_unknowns = [ ("G2.C1.x", 5.0), ("G2.G1.C2.y", 10.0), ("G3.C3.y", 20.0), ("G3.C4.y", 40.0), ] driver_expected_resids = [ ("G2.C1.x", 0.0), ("G2.G1.C2.y", 0.0), ("G3.C3.y", 0.0), ("G3.C4.y", 0.0), ] expected = [] expected.append((solver_coordinate, (t0, t1), g1_expected_params, g1_expected_unknowns, g1_expected_resids)) expected.append((driver_coordinate, (t0, t1), driver_expected_params, driver_expected_unknowns, driver_expected_resids)) self.assertIterationDataRecorded(expected, self.eps)
def test_sellar_derivs(self): prob = Problem() prob.root = SellarDerivatives() prob.root.ln_solver = LinearGaussSeidel() prob.root.ln_solver.options['maxiter'] = 4 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) param_list = ['x', 'z'] param_list = ['x'] 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(param_list, unknown_list, mode='fwd', return_format='dict') print(J) #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(param_list, unknown_list, mode='rev', return_format='dict') print(J) #for key1, val1 in Jbase.items(): #for key2, val2 in val1.items(): #assert_rel_error(self, J[key1][key2], val2, .00001) prob.root.fd_options['form'] = 'central' J = prob.calc_gradient(param_list, unknown_list, mode='fd', return_format='dict') print(J) #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(param_list, unknown_list, mode='fd', return_format='array')
def test_subsolver_records_metadata(self): prob = Problem() prob.root = ExampleGroup() prob.root.G2.G1.nl_solver.add_recorder(self.recorder) self.recorder.options['record_metadata'] = True prob.setup(check=False) prob.cleanup() # closes recorders expected_params = list(iteritems(prob.root.params)) expected_unknowns = list(iteritems(prob.root.unknowns)) expected_resids = list(iteritems(prob.root.resids)) self.assertMetadataRecorded((expected_params, expected_unknowns, expected_resids))