def test_feature_check_totals_manual(self): prob = Problem() prob.model = SellarDerivatives() prob.model.nonlinear_solver = NonlinearBlockGS() prob.setup() prob.run_model() # manually specify which derivatives to check prob.check_totals(of=['obj', 'con1'], wrt=['x', 'z'])
def test_RK4_issue(self): n = 60 m = 20 LDs = [5233.5, 5294.5, 5356.5, 5417.5, 5478.5, 5537.5] r_e2b_I0s = [np.array([4505.29362, -3402.16069, -3943.74582, 4.1923899, -1.56280012, 6.14347427]), np.array([-1005.46693, -597.205348, -6772.86532, -0.61047858, -7.54623146, 0.75907455]), np.array([4401.10539, 2275.95053, -4784.13188, -5.26605537, -1.08194926, -5.37013745]), np.array([-4969.91222, 4624.84149, 1135.9414, 0.1874654, -1.62801666, 7.4302362]), np.array([-235.021232, 2195.72976, 6499.79919, -2.55956031, -6.82743519, 2.21628099]), np.array([-690.314375, -1081.78239, -6762.90367, 7.44316722, 1.19745345, -0.96035904])] i = 0 init = { 'LD': LDs[i], 'r_e2b_I0': r_e2b_I0s[i] } prob = Problem() prob.model.add_subsystem('pt', CADRE(n=n, m=m, initial_inputs=init), promotes=['*']) prob.setup(check=verbose) prob.run_model() inputs = ['CP_gamma'] outputs = ['Data'] # check partials # partials = prob.check_partials() # out_stream=None) # assert_check_partials(partials) # check totals if verbose: J = prob.check_totals(of=outputs, wrt=inputs) else: J = prob.check_totals(of=outputs, wrt=inputs, out_stream=None) for outp in outputs: for inp in inputs: Jn = J[outp, inp]['J_fd'] Jf = J[outp, inp]['J_fwd'] if verbose: np.set_printoptions(threshold='nan') print(np.nonzero(Jn)) print(np.nonzero(Jf)) diff = abs(Jf - Jn) assert_rel_error(self, diff.max(), 0.0, 1e-4)
def test_feature_check_totals_manual(self): from openmdao.api import Problem, NonlinearBlockGS from openmdao.test_suite.components.sellar import SellarDerivatives prob = Problem() prob.model = SellarDerivatives() prob.model.nonlinear_solver = NonlinearBlockGS() prob.setup() prob.run_model() # manually specify which derivatives to check prob.check_totals(of=['obj', 'con1'], wrt=['x', 'z'])
def test_rk_with_time_invariant(self): np.random.seed(1) indeps = IndepVarComp() rktest = RKTest(NTIME) indeps.add_output('yi', np.random.random((2, 3))) indeps.add_output('yv', np.random.random((2, NTIME))) indeps.add_output('x0', np.random.random((2, ))) prob = Problem() prob.model.add_subsystem('indeps', indeps, promotes=['*']) prob.model.add_subsystem('rktest', rktest, promotes=['*']) prob.setup() prob.run_model() # check partials partials = prob.check_partials(out_stream=None) assert_check_partials(partials, atol=6e-5, rtol=6e-5) # check totals inputs = ['yi', 'yv', 'x0'] outputs = ['x'] J = prob.check_totals(of=outputs, wrt=inputs, out_stream=None) for outp in outputs: for inp in inputs: Jn = J[outp, inp]['J_fd'] Jf = J[outp, inp]['J_fwd'] diff = abs(Jf - Jn) assert_rel_error(self, diff.max(), 0.0, 6e-5)
def test_iimplicit(self): # Testing that our scale/unscale contexts leave the output vector in the correct state when # linearize is called on implicit components. prob = Problem() model = prob.model inputs_comp = IndepVarComp() inputs_comp.add_output('x1_u', val=1.0) model.add_subsystem('p', inputs_comp) mycomp = model.add_subsystem('comp', MyImplicitComp()) model.connect('p.x1_u', 'comp.x2_u') model.linear_solver = DirectSolver() model.nonlinear_solver = NewtonSolver() model.nonlinear_solver.options['atol'] = 1e-12 model.nonlinear_solver.options['rtol'] = 1e-12 model.add_design_var('p.x1_u', lower=-11, upper=11) model.add_constraint('p.x1_u', upper=3.3) model.add_objective('comp.x3_u') model.add_objective('comp.x3_s') prob.setup() prob.run_model() totals = prob.check_totals(compact_print=True, out_stream=None) for (of, wrt) in totals: assert_rel_error(self, totals[of, wrt]['abs error'][0], 0.0, 1e-7)
def test_rk_with_time_invariant(self): np.random.seed(1) indeps = IndepVarComp() rktest = RKTest(NTIME) indeps.add_output('yi', np.random.random((2, 3))) indeps.add_output('yv', np.random.random((2, NTIME))) indeps.add_output('x0', np.random.random((2,))) prob = Problem() prob.model.add_subsystem('indeps', indeps, promotes=['*']) prob.model.add_subsystem('rktest', rktest, promotes=['*']) prob.setup() prob.run_model() # check partials partials = prob.check_partials(out_stream=None) assert_check_partials(partials, atol=6e-5, rtol=6e-5) # check totals inputs = ['yi', 'yv', 'x0'] outputs = ['x'] J = prob.check_totals(of=outputs, wrt=inputs, out_stream=None) for outp in outputs: for inp in inputs: Jn = J[outp, inp]['J_fd'] Jf = J[outp, inp]['J_fwd'] diff = abs(Jf - Jn) assert_rel_error(self, diff.max(), 0.0, 6e-5)
def test_feature_check_totals_cs(self): prob = Problem() prob.model = SellarDerivatives() prob.model.nonlinear_solver = NonlinearBlockGS() prob.model.add_design_var('x', lower=-100, upper=100) prob.model.add_design_var('z', lower=-100, upper=100) prob.model.add_objective('obj') prob.model.add_constraint('con1', upper=0.0) prob.model.add_constraint('con2', upper=0.0) prob.setup(force_alloc_complex=True) # We don't call run_driver() here because we don't # actually want the optimizer to run prob.run_model() # check derivatives with complex step and a larger step size. prob.check_totals(method='cs', step=1.0e-1)
def test_feature_check_totals_from_driver(self): prob = Problem() prob.model = SellarDerivatives() prob.model.nonlinear_solver = NonlinearBlockGS() prob.model.add_design_var('x', lower=-100, upper=100) prob.model.add_design_var('z', lower=-100, upper=100) prob.model.add_objective('obj') prob.model.add_constraint('con1', upper=0.0) prob.model.add_constraint('con2', upper=0.0) prob.setup() # We don't call run_driver() here because we don't # actually want the optimizer to run prob.run_model() # check derivatives of all obj+constraints w.r.t all design variables prob.check_totals()
def test_cs_around_broyden_compute_jac_dense(self): # Basic sellar test. prob = Problem() model = prob.model sub = model.add_subsystem('sub', Group(), promotes=['*']) model.add_subsystem('px', IndepVarComp('x', 1.0), promotes=['x']) model.add_subsystem('pz', IndepVarComp('z', np.array([5.0, 2.0])), promotes=['z']) sub.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2']) sub.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2']) model.add_subsystem('obj_cmp', ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', z=np.array([0.0, 0.0]), x=0.0), promotes=['obj', 'x', 'z', 'y1', 'y2']) model.add_subsystem('con_cmp1', ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1']) model.add_subsystem('con_cmp2', ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2']) sub.nonlinear_solver = BroydenSolver() sub.linear_solver = DirectSolver() model.linear_solver = DirectSolver() prob.model.add_design_var('x', lower=-100, upper=100) prob.model.add_design_var('z', lower=-100, upper=100) prob.model.add_objective('obj') prob.model.add_constraint('con1', upper=0.0) prob.model.add_constraint('con2', upper=0.0) prob.setup(check=False, force_alloc_complex=True) prob.set_solver_print(level=0) prob.run_model() sub.nonlinear_solver.options['compute_jacobian'] = True totals = prob.check_totals(method='cs', out_stream=None) for key, val in iteritems(totals): assert_rel_error(self, val['rel error'][0], 0.0, 1e-6)
def test_feature_check_totals_suppress(self): from openmdao.api import Problem, NonlinearBlockGS from openmdao.test_suite.components.sellar import SellarDerivatives prob = Problem() prob.model = SellarDerivatives() prob.model.nonlinear_solver = NonlinearBlockGS() prob.model.add_design_var('x', lower=-100, upper=100) prob.model.add_design_var('z', lower=-100, upper=100) prob.model.add_objective('obj') prob.model.add_constraint('con1', upper=0.0) prob.model.add_constraint('con2', upper=0.0) prob.setup() # We don't call run_driver() here because we don't # actually want the optimizer to run prob.run_model() # check derivatives of all obj+constraints w.r.t all design variables totals = prob.check_totals(suppress_output=True) print(totals)
class TestCADRE(unittest.TestCase): def setup(self, compname, inputs, state0): # create instance of component type try: comp = eval('%s(NTIME)' % compname) except TypeError: try: comp = eval('%s()' % compname) except TypeError: comp = eval('%s(NTIME, 300)' % compname) # collect metadata for component inputs prob = Problem(comp) prob.setup() prob.final_setup() self.inputs_dict = {} for name, meta in prob.model.list_inputs(units=True, out_stream=None): self.inputs_dict[name.split('.')[-1]] = meta # create independent vars for each input, initialized with random values indep = IndepVarComp() for item in inputs + state0: shape = self.inputs_dict[item]['value'].shape units = self.inputs_dict[item]['units'] indep.add_output(item, np.random.random(shape), units=units) # setup problem for test self.prob = Problem() self.prob.model.add_subsystem('indep', indep, promotes=['*']) self.prob.model.add_subsystem('comp', comp, promotes=['*']) self.prob.setup() def compare_derivatives(self, var_in, var_out, rel_error=False): # check totals J = self.prob.check_totals(of=var_out, wrt=var_in, out_stream=None) for outp in var_out: for inp in var_in: Jn = J[outp, inp]['J_fd'] Jf = J[outp, inp]['J_fwd'] if rel_error: diff = np.nan_to_num(abs(Jf - Jn) / Jn) else: diff = abs(Jf - Jn) assert_rel_error(self, diff.max(), 0.0, 1e-3) # check partials # FIXME: several components fail check_partials # partials = self.prob.check_partials() # partials = self.prob.check_partials(compact_print=True, method='cs') # partials = self.prob.check_partials(out_stream=None) # assert_check_partials(partials, atol=1e-3, rtol=1e-3) def test_Attitude_Angular(self): compname = 'Attitude_Angular' inputs = ['O_BI', 'Odot_BI'] outputs = ['w_B'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Attitude_AngularRates(self): compname = 'Attitude_AngularRates' inputs = ['w_B'] outputs = ['wdot_B'] state0 = [] self.setup(compname, inputs, state0) self.prob.model.comp.h = 0.01 self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Attitude_Attitude(self): compname = 'Attitude_Attitude' inputs = ['r_e2b_I'] outputs = ['O_RI'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Attitude_Roll(self): compname = 'Attitude_Roll' inputs = ['Gamma'] outputs = ['O_BR'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Attitude_RotationMtx(self): compname = 'Attitude_RotationMtx' inputs = ['O_BR', 'O_RI'] outputs = ['O_BI'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Attitude_RotationMtxRates(self): compname = 'Attitude_RotationMtxRates' inputs = ['O_BI'] outputs = ['Odot_BI'] state0 = [] self.setup(compname, inputs, state0) self.prob.model.comp.h = 0.01 self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Attitude_Sideslip(self): compname = 'Attitude_Sideslip' inputs = ['r_e2b_I', 'O_BI'] outputs = ['v_e2b_B'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Attitude_Torque(self): compname = 'Attitude_Torque' inputs = ['w_B', 'wdot_B'] outputs = ['T_tot'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_BatterySOC(self): compname = 'BatterySOC' inputs = ['P_bat', 'temperature'] outputs = ['SOC'] state0 = ['iSOC'] self.setup(compname, inputs, state0) self.prob.model.comp.h = 0.01 self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_BatteryPower(self): compname = 'BatteryPower' inputs = ['SOC', 'temperature', 'P_bat'] outputs = ['I_bat'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_BatteryConstraints(self): compname = 'BatteryConstraints' inputs = ['I_bat', 'SOC'] outputs = ['ConCh', 'ConDs', 'ConS0', 'ConS1'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_BsplineParameters(self): compname = 'BsplineParameters' inputs = ['CP_P_comm', 'CP_gamma', 'CP_Isetpt'] outputs = ['P_comm', 'Gamma', 'Isetpt'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Comm_DataDownloaded(self): compname = 'Comm_DataDownloaded' inputs = ['Dr'] outputs = ['Data'] state0 = ['Data0'] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Comm_AntRotation(self): compname = 'Comm_AntRotation' inputs = ['antAngle'] outputs = ['q_A'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Comm_AntRotationMtx(self): compname = 'Comm_AntRotationMtx' inputs = ['q_A'] outputs = ['O_AB'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Comm_BitRate(self): compname = 'Comm_BitRate' inputs = ['P_comm', 'gain', 'GSdist', 'CommLOS'] outputs = ['Dr'] state0 = [] np.random.seed(1001) self.setup(compname, inputs, state0) # These need to be a certain magnitude so it doesn't blow up shape = self.inputs_dict['P_comm']['value'].shape self.prob['P_comm'] = np.ones(shape) shape = self.inputs_dict['GSdist']['value'].shape self.prob['GSdist'] = np.random.random(shape) * 1e3 self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Comm_Distance(self): compname = 'Comm_Distance' inputs = ['r_b2g_A'] outputs = ['GSdist'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Comm_EarthsSpin(self): compname = 'Comm_EarthsSpin' inputs = ['t'] outputs = ['q_E'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Comm_EarthsSpinMtx(self): compname = 'Comm_EarthsSpinMtx' inputs = ['q_E'] outputs = ['O_IE'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Comm_GainPattern(self): compname = 'Comm_GainPattern' inputs = ['azimuthGS', 'elevationGS'] outputs = ['gain'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Comm_GSposEarth(self): compname = 'Comm_GSposEarth' inputs = ['lon', 'lat', 'alt'] outputs = ['r_e2g_E'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Comm_GSposECI(self): compname = 'Comm_GSposECI' inputs = ['O_IE', 'r_e2g_E'] outputs = ['r_e2g_I'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Comm_LOS(self): compname = 'Comm_LOS' inputs = ['r_b2g_I', 'r_e2g_I'] outputs = ['CommLOS'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Comm_VectorAnt(self): compname = 'Comm_VectorAnt' inputs = ['r_b2g_B', 'O_AB'] outputs = ['r_b2g_A'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Comm_VectorBody(self): compname = 'Comm_VectorBody' inputs = ['r_b2g_I', 'O_BI'] outputs = ['r_b2g_B'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Comm_VectorECI(self): compname = 'Comm_VectorECI' inputs = ['r_e2g_I', 'r_e2b_I'] outputs = ['r_b2g_I'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Comm_VectorSpherical(self): compname = 'Comm_VectorSpherical' inputs = ['r_b2g_A'] outputs = ['azimuthGS', 'elevationGS'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Orbit_Dynamics(self): compname = 'Orbit_Dynamics' inputs = ['r_e2b_I0'] outputs = ['r_e2b_I'] state0 = [] self.setup(compname, inputs, state0) self.prob.model.comp.h = 0.01 self.prob['r_e2b_I0'][:3] = np.random.random((3)) * 1e6 self.prob['r_e2b_I0'][3:] = np.random.random((3)) * 1e5 self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Orbit_Initial(self): compname = 'Orbit_Initial' inputs = ['altPerigee', 'altApogee', 'RAAN', 'Inc', 'argPerigee', 'trueAnomaly'] outputs = ['r_e2b_I0'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Power_CellVoltage(self): compname = 'Power_CellVoltage' inputs = ['LOS', 'temperature', 'exposedArea', 'Isetpt'] outputs = ['V_sol'] state0 = [] self.setup(compname, inputs, state0) shape = self.inputs_dict['temperature']['value'].shape self.prob['temperature'] = np.random.random(shape) * 40 + 240 shape = self.inputs_dict['exposedArea']['value'].shape self.prob['exposedArea'] = np.random.random(shape) * 1e-4 shape = self.inputs_dict['Isetpt']['value'].shape self.prob['Isetpt'] = np.random.random(shape) * 1e-2 self.prob.run_model() self.compare_derivatives(inputs, outputs, rel_error=True) def test_Power_SolarPower(self): compname = 'Power_SolarPower' inputs = ['V_sol', 'Isetpt'] outputs = ['P_sol'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Power_Total(self): compname = 'Power_Total' inputs = ['P_sol', 'P_comm', 'P_RW'] outputs = ['P_bat'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_ReactionWheel_Motor(self): compname = 'ReactionWheel_Motor' inputs = ['T_RW', 'w_B', 'w_RW'] outputs = ['T_m'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_ReactionWheel_Power(self): compname = 'ReactionWheel_Power' inputs = ['w_RW', 'T_RW'] outputs = ['P_RW'] state0 = [] np.random.seed(1001) self.setup(compname, inputs, state0) self.prob['T_RW'] = np.random.random(self.prob['T_RW'].shape) * 1e-1 self.prob['w_RW'] = np.random.random(self.prob['w_RW'].shape) * 1e-1 self.prob.run_model() self.compare_derivatives(inputs, outputs, rel_error=True) def test_ReactionWheel_Torque(self): compname = 'ReactionWheel_Torque' inputs = ['T_tot'] outputs = ['T_RW'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_ReactionWheel_Dynamics(self): compname = 'ReactionWheel_Dynamics' inputs = ['w_B', 'T_RW'] outputs = ['w_RW'] # keep these at zeros state0 = [] # ['w_RW0'] self.setup(compname, inputs, state0) self.prob.model.comp.h = 0.01 shape = self.inputs_dict['w_B']['value'].shape self.prob['w_B'] = np.random.random(shape) * 1e-4 shape = self.inputs_dict['T_RW']['value'].shape self.prob['T_RW'] = np.random.random(shape) * 1e-9 self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Solar_ExposedArea(self): compname = 'Solar_ExposedArea' inputs = ['finAngle', 'azimuth', 'elevation'] outputs = ['exposedArea'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Sun_LOS(self): compname = 'Sun_LOS' inputs = ['r_e2b_I', 'r_e2s_I'] outputs = ['LOS'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Sun_PositionBody(self): compname = 'Sun_PositionBody' inputs = ['O_BI', 'r_e2s_I'] outputs = ['r_e2s_B'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Sun_PositionECI(self): compname = 'Sun_PositionECI' inputs = ['t', 'LD'] outputs = ['r_e2s_I'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_Sun_PositionSpherical(self): compname = 'Sun_PositionSpherical' inputs = ['r_e2s_B'] outputs = ['azimuth', 'elevation'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs+state0, outputs) def test_ThermalTemperature(self): compname = 'ThermalTemperature' inputs = ['exposedArea', 'cellInstd', 'LOS', 'P_comm'] outputs = ['temperature'] state0 = ['T0'] self.setup(compname, inputs, state0) self.prob.model.comp.h = 0.01 self.prob.run_model() self.compare_derivatives(inputs+state0, outputs)
def test_in_driver(self): # This test assures that the driver is correctly seeing unscaled (physical) data. prob = Problem() model = prob.model inputs_comp = IndepVarComp() inputs_comp.add_output('x1_u_u', val=1.0) inputs_comp.add_output('x1_u_s', val=1.0) inputs_comp.add_output('x1_s_u', val=1.0, ref=3.0) inputs_comp.add_output('x1_s_s', val=1.0, ref=3.0) inputs_comp.add_output('ox1_u_u', val=1.0) inputs_comp.add_output('ox1_u_s', val=1.0) inputs_comp.add_output('ox1_s_u', val=1.0, ref=3.0) inputs_comp.add_output('ox1_s_s', val=1.0, ref=3.0) model.add_subsystem('p', inputs_comp) mycomp = model.add_subsystem('comp', MyComp()) model.connect('p.x1_u_u', 'comp.x2_u_u') model.connect('p.x1_u_s', 'comp.x2_u_s') model.connect('p.x1_s_u', 'comp.x2_s_u') model.connect('p.x1_s_s', 'comp.x2_s_s') driver = prob.driver = MyDriver() model.add_design_var('p.x1_u_u', lower=-11, upper=11) model.add_design_var('p.x1_u_s', ref=7.0, lower=-11, upper=11) model.add_design_var('p.x1_s_u', lower=-11, upper=11) model.add_design_var('p.x1_s_s', ref=7.0, lower=-11, upper=11) # easy constraints for basic check model.add_constraint('p.x1_u_u', upper=3.3) model.add_constraint('p.x1_u_s', upper=3.3, ref=13.0) model.add_constraint('p.x1_s_u', upper=3.3) model.add_constraint('p.x1_s_s', upper=3.3, ref=13.0) # harder to calculate constraints model.add_constraint('comp.x3_u_u', upper=3.3) model.add_constraint('comp.x3_u_s', upper=3.3, ref=17.0) model.add_constraint('comp.x3_s_u', upper=3.3) model.add_constraint('comp.x3_s_s', upper=3.3, ref=17.0) model.add_objective('p.ox1_u_u') model.add_objective('p.ox1_u_s', ref=15.0) model.add_objective('p.ox1_s_u') model.add_objective('p.ox1_s_s', ref=15.0) prob.setup() prob.run_driver() # Parameter values assert_rel_error(self, driver.param_vals['p.x1_u_u'], 1.0) assert_rel_error(self, driver.param_vals['p.x1_u_s'], 1.0/7.0) assert_rel_error(self, driver.param_vals['p.x1_s_u'], 1.0) assert_rel_error(self, driver.param_vals['p.x1_s_s'], 1.0/7.0) assert_rel_error(self, driver.param_meta['p.x1_u_u']['upper'], 11.0) assert_rel_error(self, driver.param_meta['p.x1_u_s']['upper'], 11.0/7.0) assert_rel_error(self, driver.param_meta['p.x1_s_u']['upper'], 11.0) assert_rel_error(self, driver.param_meta['p.x1_s_s']['upper'], 11.0/7.0) assert_rel_error(self, driver.con_meta['p.x1_u_u']['upper'], 3.3) assert_rel_error(self, driver.con_meta['p.x1_u_s']['upper'], 3.3/13.0) assert_rel_error(self, driver.con_meta['p.x1_s_u']['upper'], 3.3) assert_rel_error(self, driver.con_meta['p.x1_s_s']['upper'], 3.3/13.0) assert_rel_error(self, driver.con_vals['p.x1_u_u'], 1.0) assert_rel_error(self, driver.con_vals['p.x1_u_s'], 1.0/13.0) assert_rel_error(self, driver.con_vals['p.x1_s_u'], 1.0) assert_rel_error(self, driver.con_vals['p.x1_s_s'], 1.0/13.0) assert_rel_error(self, driver.obj_vals['p.ox1_u_u'], 1.0) assert_rel_error(self, driver.obj_vals['p.ox1_u_s'], 1.0/15.0) assert_rel_error(self, driver.obj_vals['p.ox1_s_u'], 1.0) assert_rel_error(self, driver.obj_vals['p.ox1_s_s'], 1.0/15.0) J = model.comp.J assert_rel_error(self, driver.sens_dict['comp.x3_u_u']['p.x1_u_u'][0][0], J[0, 0]) assert_rel_error(self, driver.sens_dict['comp.x3_u_s']['p.x1_u_u'][0][0], J[1, 0] / 17.0) assert_rel_error(self, driver.sens_dict['comp.x3_s_u']['p.x1_u_u'][0][0], J[2, 0]) assert_rel_error(self, driver.sens_dict['comp.x3_s_s']['p.x1_u_u'][0][0], J[3, 0] / 17.0) assert_rel_error(self, driver.sens_dict['comp.x3_u_u']['p.x1_u_s'][0][0], J[0, 1] * 7.0) assert_rel_error(self, driver.sens_dict['comp.x3_u_s']['p.x1_u_s'][0][0], J[1, 1] / 17.0 * 7.0) assert_rel_error(self, driver.sens_dict['comp.x3_s_u']['p.x1_u_s'][0][0], J[2, 1] * 7.0) assert_rel_error(self, driver.sens_dict['comp.x3_s_s']['p.x1_u_s'][0][0], J[3, 1] / 17.0 * 7.0) assert_rel_error(self, driver.sens_dict['comp.x3_u_u']['p.x1_s_u'][0][0], J[0, 2]) assert_rel_error(self, driver.sens_dict['comp.x3_u_s']['p.x1_s_u'][0][0], J[1, 2] / 17.0) assert_rel_error(self, driver.sens_dict['comp.x3_s_u']['p.x1_s_u'][0][0], J[2, 2]) assert_rel_error(self, driver.sens_dict['comp.x3_s_s']['p.x1_s_u'][0][0], J[3, 2] / 17.0) assert_rel_error(self, driver.sens_dict['comp.x3_u_u']['p.x1_s_s'][0][0], J[0, 3] * 7.0) assert_rel_error(self, driver.sens_dict['comp.x3_u_s']['p.x1_s_s'][0][0], J[1, 3] / 17.0* 7.0) assert_rel_error(self, driver.sens_dict['comp.x3_s_u']['p.x1_s_s'][0][0], J[2, 3] * 7.0) assert_rel_error(self, driver.sens_dict['comp.x3_s_s']['p.x1_s_s'][0][0], J[3, 3] / 17.0 * 7.0) totals = prob.check_totals(compact_print=True, out_stream=None) for (of, wrt) in totals: assert_rel_error(self, totals[of, wrt]['abs error'][0], 0.0, 1e-7)
n_random=N_RANDOM, distribution_matrix=co_mat, locations=SCLOC_Q), promotes_inputs=['mu']) # xi will be of size N_SAMPLES p.model.add_subsystem('multi_point', Group()) for i in range(N_SAMPLES): name = 'R{}'.format(i) p.model.multi_point.add_subsystem(name, Rosenbrock_OMDAO(size=N_RANDOM)) p.model.connect('perturb.xi', 'multi_point.{}.rv'.format(name), src_indices=[(i,0), (i,1)]) # p.model.connect('{}.fval'.format(name), 'stochastic_cloc.fval{}'.format(i)) p.model.add_subsystem('stochastic_colloc', StochasticCollocation(degree=DEGREE, n_random=N_RANDOM, weights=SCLOC_W), promotes_outputs=['mu_j']) for i in range(N_SAMPLES): name = 'R{}'.format(i) p.model.connect('multi_point.{}.fval'.format(name), 'stochastic_colloc.fval{}'.format(i)) # p.model.connect('stochastic_colloc.fval', 'multi_point.{}.fval'.format(name), src_indices=[i]) p.setup() p.run_model() p.check_totals(of=['mu_j'], wrt=['mu']) #Compute the total derivative of the partial val = p.compute_totals(of=['mu_j'], wrt=['mu']) print('partial val = ', val)
def test_in_driver(self): # This test assures that the driver is correctly seeing unscaled (physical) data. prob = Problem() model = prob.model inputs_comp = IndepVarComp() inputs_comp.add_output('x1_u_u', val=1.0) inputs_comp.add_output('x1_u_s', val=1.0) inputs_comp.add_output('x1_s_u', val=1.0, ref=3.0) inputs_comp.add_output('x1_s_s', val=1.0, ref=3.0) inputs_comp.add_output('ox1_u_u', val=1.0) inputs_comp.add_output('ox1_u_s', val=1.0) inputs_comp.add_output('ox1_s_u', val=1.0, ref=3.0) inputs_comp.add_output('ox1_s_s', val=1.0, ref=3.0) model.add_subsystem('p', inputs_comp) mycomp = model.add_subsystem('comp', MyComp()) model.connect('p.x1_u_u', 'comp.x2_u_u') model.connect('p.x1_u_s', 'comp.x2_u_s') model.connect('p.x1_s_u', 'comp.x2_s_u') model.connect('p.x1_s_s', 'comp.x2_s_s') driver = prob.driver = MyDriver() model.add_design_var('p.x1_u_u', lower=-11, upper=11) model.add_design_var('p.x1_u_s', ref=7.0, lower=-11, upper=11) model.add_design_var('p.x1_s_u', lower=-11, upper=11) model.add_design_var('p.x1_s_s', ref=7.0, lower=-11, upper=11) # easy constraints for basic check model.add_constraint('p.x1_u_u', upper=3.3) model.add_constraint('p.x1_u_s', upper=3.3, ref=13.0) model.add_constraint('p.x1_s_u', upper=3.3) model.add_constraint('p.x1_s_s', upper=3.3, ref=13.0) # harder to calculate constraints model.add_constraint('comp.x3_u_u', upper=3.3) model.add_constraint('comp.x3_u_s', upper=3.3, ref=17.0) model.add_constraint('comp.x3_s_u', upper=3.3) model.add_constraint('comp.x3_s_s', upper=3.3, ref=17.0) model.add_objective('p.ox1_u_u') model.add_objective('p.ox1_u_s', ref=15.0) model.add_objective('p.ox1_s_u') model.add_objective('p.ox1_s_s', ref=15.0) prob.setup() prob.run_driver() # Parameter values assert_rel_error(self, driver.param_vals['p.x1_u_u'], 1.0) assert_rel_error(self, driver.param_vals['p.x1_u_s'], 1.0 / 7.0) assert_rel_error(self, driver.param_vals['p.x1_s_u'], 1.0) assert_rel_error(self, driver.param_vals['p.x1_s_s'], 1.0 / 7.0) assert_rel_error(self, driver.param_meta['p.x1_u_u']['upper'], 11.0) assert_rel_error(self, driver.param_meta['p.x1_u_s']['upper'], 11.0 / 7.0) assert_rel_error(self, driver.param_meta['p.x1_s_u']['upper'], 11.0) assert_rel_error(self, driver.param_meta['p.x1_s_s']['upper'], 11.0 / 7.0) assert_rel_error(self, driver.con_meta['p.x1_u_u']['upper'], 3.3) assert_rel_error(self, driver.con_meta['p.x1_u_s']['upper'], 3.3 / 13.0) assert_rel_error(self, driver.con_meta['p.x1_s_u']['upper'], 3.3) assert_rel_error(self, driver.con_meta['p.x1_s_s']['upper'], 3.3 / 13.0) assert_rel_error(self, driver.con_vals['p.x1_u_u'], 1.0) assert_rel_error(self, driver.con_vals['p.x1_u_s'], 1.0 / 13.0) assert_rel_error(self, driver.con_vals['p.x1_s_u'], 1.0) assert_rel_error(self, driver.con_vals['p.x1_s_s'], 1.0 / 13.0) assert_rel_error(self, driver.obj_vals['p.ox1_u_u'], 1.0) assert_rel_error(self, driver.obj_vals['p.ox1_u_s'], 1.0 / 15.0) assert_rel_error(self, driver.obj_vals['p.ox1_s_u'], 1.0) assert_rel_error(self, driver.obj_vals['p.ox1_s_s'], 1.0 / 15.0) J = model.comp.J assert_rel_error(self, driver.sens_dict['comp.x3_u_u']['p.x1_u_u'][0][0], J[0, 0]) assert_rel_error(self, driver.sens_dict['comp.x3_u_s']['p.x1_u_u'][0][0], J[1, 0] / 17.0) assert_rel_error(self, driver.sens_dict['comp.x3_s_u']['p.x1_u_u'][0][0], J[2, 0]) assert_rel_error(self, driver.sens_dict['comp.x3_s_s']['p.x1_u_u'][0][0], J[3, 0] / 17.0) assert_rel_error(self, driver.sens_dict['comp.x3_u_u']['p.x1_u_s'][0][0], J[0, 1] * 7.0) assert_rel_error(self, driver.sens_dict['comp.x3_u_s']['p.x1_u_s'][0][0], J[1, 1] / 17.0 * 7.0) assert_rel_error(self, driver.sens_dict['comp.x3_s_u']['p.x1_u_s'][0][0], J[2, 1] * 7.0) assert_rel_error(self, driver.sens_dict['comp.x3_s_s']['p.x1_u_s'][0][0], J[3, 1] / 17.0 * 7.0) assert_rel_error(self, driver.sens_dict['comp.x3_u_u']['p.x1_s_u'][0][0], J[0, 2]) assert_rel_error(self, driver.sens_dict['comp.x3_u_s']['p.x1_s_u'][0][0], J[1, 2] / 17.0) assert_rel_error(self, driver.sens_dict['comp.x3_s_u']['p.x1_s_u'][0][0], J[2, 2]) assert_rel_error(self, driver.sens_dict['comp.x3_s_s']['p.x1_s_u'][0][0], J[3, 2] / 17.0) assert_rel_error(self, driver.sens_dict['comp.x3_u_u']['p.x1_s_s'][0][0], J[0, 3] * 7.0) assert_rel_error(self, driver.sens_dict['comp.x3_u_s']['p.x1_s_s'][0][0], J[1, 3] / 17.0 * 7.0) assert_rel_error(self, driver.sens_dict['comp.x3_s_u']['p.x1_s_s'][0][0], J[2, 3] * 7.0) assert_rel_error(self, driver.sens_dict['comp.x3_s_s']['p.x1_s_s'][0][0], J[3, 3] / 17.0 * 7.0) totals = prob.check_totals(compact_print=True, out_stream=None) for (of, wrt) in totals: assert_rel_error(self, totals[of, wrt]['abs error'][0], 0.0, 1e-7)
ref_area_m2=ref_area_m2, Wac_1e6_N=Wac_1e6_N, Mach_mode=Mach_mode, propulsion_model=propulsion_model, aerodynamics_model=aerodynamics_model)) prob.model.add_subsystem('p_CLt', IndepVarComp('CLt', np.zeros((36, ))), promotes=['*']) prob.model.add_subsystem('p_CDt', IndepVarComp('CDt', np.zeros((36, ))), promotes=['*']) prob.setup(vector_class=PETScVector) prob['CLt'] = np.array([ 0.27891953, 0.27740675, 0.50567129, 0.50411111, 0.67178486, 0.67072796, 0.20625611, 0.2048599, 0.45098623, 0.44937616, 0.65085738, 0.6492935, 0.12143678, 0.11953893, 0.39299682, 0.39062201, 0.63840812, 0.63517673, 0.12498458, 0.12133426, 0.38719886, 0.38262164, 0.63962429, 0.63352078, 0.12739122, 0.12252601, 0.38887416, 0.38271344, 0.65587936, 0.64779034, 0.13010178, 0.12376715, 0.40004596, 0.39172921, 0.67063932, 0.66241266]) prob['CDt'] = np.array([ 0.01245633, 0.01265379, 0.02219869, 0.02234761, 0.04310465, 0.04315868, 0.01059043, 0.01074938, 0.01776348, 0.01788232, 0.03369645, 0.03376599, 0.00980034, 0.00998945, 0.01497374, 0.01511459, 0.02877744, 0.02887994, 0.00996479, 0.0103015, 0.01479722, 0.01504052, 0.02780155, 0.02791219, 0.01017521, 0.01059275, 0.01503608, 0.01532366, 0.02923256, 0.02926247, 0.01062982, 0.01110888, 0.01586912, 0.01614131, 0.03669549, 0.03639307]) prob.run_model() print('CLt', prob['CLt']) print('CDt', prob['CDt']) prob.check_totals()
class TestCADRE(unittest.TestCase): def setup(self, compname, inputs, state0): # create instance of component type try: comp = eval('%s(NTIME)' % compname) except TypeError: try: comp = eval('%s()' % compname) except TypeError: comp = eval('%s(NTIME, 300)' % compname) # collect metadata for component inputs prob = Problem(comp) prob.setup() prob.final_setup() self.inputs_dict = {} for name, meta in prob.model.list_inputs(units=True, out_stream=None): self.inputs_dict[name.split('.')[-1]] = meta # create independent vars for each input, initialized with random values indep = IndepVarComp() for item in inputs + state0: shape = self.inputs_dict[item]['value'].shape units = self.inputs_dict[item]['units'] indep.add_output(item, np.random.random(shape), units=units) # setup problem for test self.prob = Problem() self.prob.model.add_subsystem('indep', indep, promotes=['*']) self.prob.model.add_subsystem('comp', comp, promotes=['*']) self.prob.setup() def compare_derivatives(self, var_in, var_out, rel_error=False): # check totals J = self.prob.check_totals(of=var_out, wrt=var_in, out_stream=None) for outp in var_out: for inp in var_in: Jn = J[outp, inp]['J_fd'] Jf = J[outp, inp]['J_fwd'] if rel_error: diff = np.nan_to_num(abs(Jf - Jn) / Jn) else: diff = abs(Jf - Jn) assert_rel_error(self, diff.max(), 0.0, 1e-3) # check partials # FIXME: several components fail check_partials #partials = self.prob.check_partials() partials = self.prob.check_partials(compact_print=True, method='cs') #partials = self.prob.check_partials(out_stream=None) # Only check relative assert_check_partials(partials, atol=np.inf, rtol=5e-3) def test_Attitude_Angular(self): compname = 'Attitude_Angular' inputs = ['O_BI', 'Odot_BI'] outputs = ['w_B'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Attitude_AngularRates(self): compname = 'Attitude_AngularRates' inputs = ['w_B'] outputs = ['wdot_B'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Attitude_Attitude(self): compname = 'Attitude_Attitude' inputs = ['r_e2b_I'] outputs = ['O_RI'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Attitude_Roll(self): compname = 'Attitude_Roll' inputs = ['Gamma'] outputs = ['O_BR'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Attitude_RotationMtx(self): compname = 'Attitude_RotationMtx' inputs = ['O_BR', 'O_RI'] outputs = ['O_BI'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Attitude_RotationMtxRates(self): compname = 'Attitude_RotationMtxRates' inputs = ['O_BI'] outputs = ['Odot_BI'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Attitude_Sideslip(self): compname = 'Attitude_Sideslip' inputs = ['r_e2b_I', 'O_BI'] outputs = ['v_e2b_B'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Attitude_Torque(self): compname = 'Attitude_Torque' inputs = ['w_B', 'wdot_B'] outputs = ['T_tot'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_BatterySOC(self): compname = 'BatterySOC' inputs = ['P_bat', 'temperature'] outputs = ['SOC'] state0 = ['iSOC'] self.setup(compname, inputs, state0) self.prob.model.comp.h = 0.01 self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_BatteryPower(self): compname = 'BatteryPower' inputs = ['SOC', 'temperature', 'P_bat'] outputs = ['I_bat'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_BatteryConstraints(self): compname = 'BatteryConstraints' inputs = ['I_bat', 'SOC'] outputs = ['ConCh', 'ConDs', 'ConS0', 'ConS1'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_BsplineParameters(self): compname = 'BsplineParameters' inputs = ['CP_P_comm', 'CP_gamma', 'CP_Isetpt'] outputs = ['P_comm', 'Gamma', 'Isetpt'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Comm_DataDownloaded(self): compname = 'Comm_DataDownloaded' inputs = ['Dr'] outputs = ['Data'] state0 = ['Data0'] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Comm_AntRotation(self): compname = 'Comm_AntRotation' inputs = ['antAngle'] outputs = ['q_A'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Comm_AntRotationMtx(self): compname = 'Comm_AntRotationMtx' inputs = ['q_A'] outputs = ['O_AB'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Comm_BitRate(self): compname = 'Comm_BitRate' inputs = ['P_comm', 'gain', 'GSdist', 'CommLOS'] outputs = ['Dr'] state0 = [] np.random.seed(1001) self.setup(compname, inputs, state0) # These need to be a certain magnitude so it doesn't blow up shape = self.inputs_dict['P_comm']['value'].shape self.prob['P_comm'] = np.ones(shape) shape = self.inputs_dict['GSdist']['value'].shape self.prob['GSdist'] = np.random.random(shape) * 1e3 self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Comm_Distance(self): compname = 'Comm_Distance' inputs = ['r_b2g_A'] outputs = ['GSdist'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Comm_EarthsSpin(self): compname = 'Comm_EarthsSpin' inputs = ['t'] outputs = ['q_E'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Comm_EarthsSpinMtx(self): compname = 'Comm_EarthsSpinMtx' inputs = ['q_E'] outputs = ['O_IE'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Comm_GainPattern(self): compname = 'Comm_GainPattern' inputs = ['azimuthGS', 'elevationGS'] outputs = ['gain'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Comm_GSposEarth(self): compname = 'Comm_GSposEarth' inputs = ['lon', 'lat', 'alt'] outputs = ['r_e2g_E'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Comm_GSposECI(self): compname = 'Comm_GSposECI' inputs = ['O_IE', 'r_e2g_E'] outputs = ['r_e2g_I'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Comm_LOS(self): compname = 'Comm_LOS' inputs = ['r_b2g_I', 'r_e2g_I'] outputs = ['CommLOS'] state0 = [] self.setup(compname, inputs, state0) # needs to be negative to test not trivial branch. self.prob['r_e2g_I'] = -self.prob['r_e2g_I'] self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Comm_VectorAnt(self): compname = 'Comm_VectorAnt' inputs = ['r_b2g_B', 'O_AB'] outputs = ['r_b2g_A'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Comm_VectorBody(self): compname = 'Comm_VectorBody' inputs = ['r_b2g_I', 'O_BI'] outputs = ['r_b2g_B'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Comm_VectorECI(self): compname = 'Comm_VectorECI' inputs = ['r_e2g_I', 'r_e2b_I'] outputs = ['r_b2g_I'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Comm_VectorSpherical(self): compname = 'Comm_VectorSpherical' inputs = ['r_b2g_A'] outputs = ['azimuthGS', 'elevationGS'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Orbit_Dynamics(self): compname = 'Orbit_Dynamics' inputs = ['r_e2b_I0'] outputs = ['r_e2b_I'] state0 = [] self.setup(compname, inputs, state0) self.prob.model.comp.h = 0.01 self.prob['r_e2b_I0'][:3] = np.random.random((3)) * 1e6 self.prob['r_e2b_I0'][3:] = np.random.random((3)) * 1e5 self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Orbit_Initial(self): compname = 'Orbit_Initial' inputs = [ 'altPerigee', 'altApogee', 'RAAN', 'Inc', 'argPerigee', 'trueAnomaly' ] outputs = ['r_e2b_I0'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Power_CellVoltage(self): compname = 'Power_CellVoltage' inputs = ['LOS', 'temperature', 'exposedArea', 'Isetpt'] outputs = ['V_sol'] state0 = [] self.setup(compname, inputs, state0) shape = self.inputs_dict['temperature']['value'].shape self.prob['temperature'] = np.random.random(shape) * 40 + 240 shape = self.inputs_dict['exposedArea']['value'].shape self.prob['exposedArea'] = np.random.random(shape) * 1e-4 shape = self.inputs_dict['Isetpt']['value'].shape self.prob['Isetpt'] = np.random.random(shape) * 1e-2 self.prob.run_model() self.compare_derivatives(inputs, outputs, rel_error=True) def test_Power_SolarPower(self): compname = 'Power_SolarPower' inputs = ['V_sol', 'Isetpt'] outputs = ['P_sol'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Power_Total(self): compname = 'Power_Total' inputs = ['P_sol', 'P_comm', 'P_RW'] outputs = ['P_bat'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_ReactionWheel_Motor(self): compname = 'ReactionWheel_Motor' inputs = ['T_RW', 'w_B', 'w_RW'] outputs = ['T_m'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_ReactionWheel_Power(self): compname = 'ReactionWheel_Power' inputs = ['w_RW', 'T_RW'] outputs = ['P_RW'] state0 = [] np.random.seed(1001) self.setup(compname, inputs, state0) self.prob['T_RW'] = np.random.random(self.prob['T_RW'].shape) * 1e-1 self.prob['w_RW'] = np.random.random(self.prob['w_RW'].shape) * 1e-1 self.prob.run_model() self.compare_derivatives(inputs, outputs, rel_error=True) def test_ReactionWheel_Torque(self): compname = 'ReactionWheel_Torque' inputs = ['T_tot'] outputs = ['T_RW'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_ReactionWheel_Dynamics(self): compname = 'ReactionWheel_Dynamics' inputs = ['w_B', 'T_RW'] outputs = ['w_RW'] # keep these at zeros state0 = [] # ['w_RW0'] self.setup(compname, inputs, state0) self.prob.model.comp.h = 0.01 shape = self.inputs_dict['w_B']['value'].shape self.prob['w_B'] = np.random.random(shape) * 1e-4 shape = self.inputs_dict['T_RW']['value'].shape self.prob['T_RW'] = np.random.random(shape) * 1e-9 self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Solar_ExposedArea(self): compname = 'Solar_ExposedArea' inputs = ['finAngle', 'azimuth', 'elevation'] outputs = ['exposedArea'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Sun_LOS(self): np.random.seed(1001) compname = 'Sun_LOS' inputs = ['r_e2b_I', 'r_e2s_I'] outputs = ['LOS'] state0 = [] self.setup(compname, inputs, state0) # needs to be negative to test not trivial branch. self.prob['r_e2s_I'] -= 5800 self.prob['r_e2b_I'] += 5800 self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Sun_PositionBody(self): compname = 'Sun_PositionBody' inputs = ['O_BI', 'r_e2s_I'] outputs = ['r_e2s_B'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Sun_PositionECI(self): compname = 'Sun_PositionECI' inputs = ['t', 'LD'] outputs = ['r_e2s_I'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_Sun_PositionSpherical(self): compname = 'Sun_PositionSpherical' inputs = ['r_e2s_B'] outputs = ['azimuth', 'elevation'] state0 = [] self.setup(compname, inputs, state0) self.prob.run_model() self.compare_derivatives(inputs + state0, outputs) def test_ThermalTemperature(self): compname = 'ThermalTemperature' inputs = ['exposedArea', 'cellInstd', 'LOS', 'P_comm'] outputs = ['temperature'] state0 = ['T0'] self.setup(compname, inputs, state0) self.prob.model.comp.h = 0.01 self.prob.run_model() self.compare_derivatives(inputs + state0, outputs)
def test_guess_nonlinear_complex_step(self): class ImpWithInitial(ImplicitComponent): """ An implicit component to solve the quadratic equation: x^2 - 4x + 3 (solutions at x=1 and x=3) """ def setup(self): self.add_input('a', val=1.) self.add_input('b', val=-4.) self.add_input('c', val=3.) self.add_output('x', val=0.) self.declare_partials(of='*', wrt='*') def apply_nonlinear(self, inputs, outputs, residuals): a = inputs['a'] b = inputs['b'] c = inputs['c'] x = outputs['x'] residuals['x'] = a * x**2 + b * x + c def linearize(self, inputs, outputs, partials): a = inputs['a'] b = inputs['b'] c = inputs['c'] x = outputs['x'] partials['x', 'a'] = x**2 partials['x', 'b'] = x partials['x', 'c'] = 1.0 partials['x', 'x'] = 2 * a * x + b def guess_nonlinear(self, inputs, outputs, resids): if outputs._data.dtype == np.complex: raise RuntimeError( 'Vector should not be complex when guess_nonlinear is called.' ) # Default initial state of zero for x takes us to x=1 solution. # Here we set it to a value that will take us to the x=3 solution. outputs['x'] = 5.0 prob = Problem() model = prob.model = Group() indep = IndepVarComp() indep.add_output('a', 1.0) indep.add_output('b', -4.0) indep.add_output('c', 3.0) model.add_subsystem('p', indep) model.add_subsystem('comp', ImpWithInitial()) model.add_subsystem('fn', ExecComp(['y = .03*a*x*x - .04*a*a*b*x - c'])) model.connect('p.a', 'comp.a') model.connect('p.a', 'fn.a') model.connect('p.b', 'fn.b') model.connect('p.c', 'fn.c') model.connect('comp.x', 'fn.x') model.nonlinear_solver = NewtonSolver() model.nonlinear_solver.options['rtol'] = 1e-12 model.nonlinear_solver.options['atol'] = 1e-12 model.nonlinear_solver.options['maxiter'] = 15 model.linear_solver = ScipyKrylov() prob.setup(force_alloc_complex=True) prob.run_model() assert_rel_error(self, prob['comp.x'], 3.) totals = prob.check_totals(of=['fn.y'], wrt=['p.a'], method='cs', out_stream=None) for key, val in iteritems(totals): assert_rel_error(self, val['rel error'][0], 0.0, 1e-9)
model.add_design_var('revenue:x1') model.add_design_var('revenue:y1') model.add_design_var('revenue:x2') model.add_design_var('revenue:y2') model.add_design_var('revenue:z1') model.add_objective('profit') model.add_constraint('g_demand', upper=0.0) model.add_constraint('g_aircraft_new', upper=0.0) model.add_constraint('g_aircraft_exist', upper=0.0) prob.setup() prob.run() print('Revenue') print('---------') print('pax_flt', prob['pax_flt']) print('revenue', prob['revenue']) print('tot_pax', prob['tot_pax']) print('Profit/Con') print('------------') print('profit', prob['profit']) print('g_demand', prob['g_demand']) print('g_aircraft_new', prob['g_aircraft_new']) print('g_aircraft_exist', prob['g_aircraft_exist']) prob.check_partials(step=1.0e-5, compact_print=True) #prob.check_partials(comps=['revenue'], compact_print=True) prob.check_totals(compact_print=True, step=1e-1)
def test_guess_nonlinear_complex_step(self): class ImpWithInitial(ImplicitComponent): """ An implicit component to solve the quadratic equation: x^2 - 4x + 3 (solutions at x=1 and x=3) """ def setup(self): self.add_input('a', val=1.) self.add_input('b', val=-4.) self.add_input('c', val=3.) self.add_output('x', val=0.) self.declare_partials(of='*', wrt='*') def apply_nonlinear(self, inputs, outputs, residuals): a = inputs['a'] b = inputs['b'] c = inputs['c'] x = outputs['x'] residuals['x'] = a * x ** 2 + b * x + c def linearize(self, inputs, outputs, partials): a = inputs['a'] b = inputs['b'] c = inputs['c'] x = outputs['x'] partials['x', 'a'] = x ** 2 partials['x', 'b'] = x partials['x', 'c'] = 1.0 partials['x', 'x'] = 2 * a * x + b def guess_nonlinear(self, inputs, outputs, resids): if outputs._data.dtype == np.complex: raise RuntimeError('Vector should not be complex when guess_nonlinear is called.') # Default initial state of zero for x takes us to x=1 solution. # Here we set it to a value that will take us to the x=3 solution. outputs['x'] = 5.0 prob = Problem() model = prob.model = Group() indep = IndepVarComp() indep.add_output('a', 1.0) indep.add_output('b', -4.0) indep.add_output('c', 3.0) model.add_subsystem('p', indep) model.add_subsystem('comp', ImpWithInitial()) model.add_subsystem('fn', ExecComp(['y = .03*a*x*x - .04*a*a*b*x - c'])) model.connect('p.a', 'comp.a') model.connect('p.a', 'fn.a') model.connect('p.b', 'fn.b') model.connect('p.c', 'fn.c') model.connect('comp.x', 'fn.x') model.nonlinear_solver = NewtonSolver() model.nonlinear_solver.options['rtol'] = 1e-12 model.nonlinear_solver.options['atol'] = 1e-12 model.nonlinear_solver.options['maxiter'] = 15 model.linear_solver = ScipyKrylov() prob.setup(force_alloc_complex=True) prob.run_model() assert_rel_error(self, prob['comp.x'], 3.) totals = prob.check_totals(of=['fn.y'], wrt=['p.a'], method='cs', out_stream=None) for key, val in iteritems(totals): assert_rel_error(self, val['rel error'][0], 0.0, 1e-9)