def __init__(self, body, sc, method, nb_seg, order, solver, ode_class, ode_kwargs, ph_name, snopt_opts=None, rec_file=None): NLP.__init__(self, body, sc, method, nb_seg, order, solver, snopt_opts=snopt_opts, rec_file=rec_file) # Transcription object if self.method == 'gauss-lobatto': self.transcription = GaussLobatto(num_segments=self.nb_seg, order=self.order, compressed=True) elif self.method == 'radau-ps': self.transcription = Radau(num_segments=self.nb_seg, order=self.order, compressed=True) elif self.method == 'runge-kutta': self.transcription = RungeKutta(num_segments=self.nb_seg, order=self.order, compressed=True) else: raise ValueError('method must be either gauss-lobatto or radau-ps') # Phase object self.phase = self.trajectory.add_phase(ph_name, Phase(ode_class=ode_class, ode_init_kwargs=ode_kwargs, transcription=self.transcription)) self.phase_name = ''.join(['traj.', ph_name]) # discretization nodes self.state_nodes = None self.control_nodes = None self.t_all = None self.t_state = None self.t_control = None self.idx_state_control = None # time of flight self.tof = None
def test_solver_defects_single_phase_reverse_propagation(self): prob = Problem() num_seg = 5 seg_ends, _ = lgl(num_seg + 1) # First phase: normal operation. transcription = Radau(num_segments=5, order=5, segment_ends=seg_ends, compressed=True) phase0 = Phase(ode_class=BatteryODE, transcription=transcription) traj_p0 = prob.model.add_subsystem('phase0', phase0) traj_p0.set_time_options(fix_initial=True, fix_duration=True) traj_p0.set_state_options('state_of_charge', fix_initial=True, fix_final=False, solve_segments=True) prob.setup() prob['phase0.t_initial'] = 0 prob['phase0.t_duration'] = -1.0 * 3600 prob['phase0.states:state_of_charge'][:] = 0.63464982 prob.set_solver_print(level=0) prob.run_model() soc0 = prob['phase0.states:state_of_charge'] assert_rel_error(self, soc0[-1], 1.0, 1e-6)
def __init__(self, body, sc, method, nb_seg, order, solver, ode_class, ode_kwargs, ph_name, snopt_opts=None, rec_file=None): if isinstance(order, int): order = tuple(order for _ in range(len(nb_seg))) if isinstance(method, str): method = tuple(method for _ in range(len(nb_seg))) NLP.__init__(self, body, sc, method, nb_seg, order, solver, snopt_opts=snopt_opts, rec_file=rec_file) # Transcription objects list self.transcription = [] for i in range(len(self.nb_seg)): if self.method[i] == 'gauss-lobatto': t = GaussLobatto(num_segments=self.nb_seg[i], order=self.order[i], compressed=True) elif self.method[i] == 'radau-ps': t = Radau(num_segments=self.nb_seg[i], order=self.order[i], compressed=True) elif self.method[i] == 'runge-kutta': t = RungeKutta(num_segments=self.nb_seg[i], order=self.order[i], compressed=True) else: raise ValueError('method must be either gauss-lobatto, radau-ps or runge-kutta') self.transcription.append(t) # Phase objects list self.phase = [] self.phase_name = [] for i in range(len(self.nb_seg)): ph = self.trajectory.add_phase(ph_name[i], Phase(ode_class=ode_class[i], ode_init_kwargs=ode_kwargs[i], transcription=self.transcription[i])) self.phase.append(ph) self.phase_name.append(''.join(['traj.', ph_name[i]]))
def test_control_rate2_path_constraint_radau(self): from openmdao.api import Problem, Group, ScipyOptimizeDriver, DirectSolver from openmdao.utils.assert_utils import assert_rel_error from dymos import Phase, Radau from dymos.examples.brachistochrone.brachistochrone_ode import BrachistochroneODE p = Problem(model=Group()) p.driver = ScipyOptimizeDriver() phase = Phase(ode_class=BrachistochroneODE, transcription=Radau(num_segments=10, compressed=False)) p.model.add_subsystem('phase0', phase) phase.set_time_options(initial_bounds=(0, 0), duration_bounds=(.5, 10)) phase.set_state_options('x', fix_initial=True, fix_final=True) phase.set_state_options('y', fix_initial=True, fix_final=True) phase.set_state_options('v', fix_initial=True) phase.add_control('theta', units='deg', lower=0.01, upper=179.9) phase.add_design_parameter('g', units='m/s**2', opt=False, val=9.80665) # Minimize time at the end of the phase phase.add_objective('time', loc='final', scaler=10) phase.add_path_constraint('theta_rate2', lower=-200, upper=200, units='rad/s**2') p.model.linear_solver = DirectSolver() p.model.options['assembled_jac_type'] = 'csc' p.setup() p['phase0.t_initial'] = 0.0 p['phase0.t_duration'] = 2.0 p['phase0.states:x'] = phase.interpolate(ys=[0, 10], nodes='state_input') p['phase0.states:y'] = phase.interpolate(ys=[10, 5], nodes='state_input') p['phase0.states:v'] = phase.interpolate(ys=[0, 9.9], nodes='state_input') p['phase0.controls:theta'] = phase.interpolate(ys=[5, 100.5], nodes='control_input') # Solve for the optimal trajectory p.run_driver() # Test the results assert_rel_error(self, p.get_val('phase0.timeseries.time')[-1], 1.8016, tolerance=1.0E-3)
def test_objective_design_parameter_radau(self): p = Problem(model=Group()) p.driver = ScipyOptimizeDriver() p.driver.options['dynamic_simul_derivs'] = True phase = Phase(ode_class=BrachistochroneODE, transcription=Radau(num_segments=20, order=3, compressed=True)) p.model.add_subsystem('phase0', phase) phase.set_time_options(fix_initial=True, duration_bounds=(4, 10)) phase.set_state_options('x', fix_initial=True, fix_final=True) phase.set_state_options('y', fix_initial=True, fix_final=True) phase.set_state_options('v', fix_initial=True, fix_final=False) phase.add_control('theta', continuity=True, rate_continuity=True, units='deg', lower=0.01, upper=179.9) phase.add_design_parameter('g', units='m/s**2', opt=True, val=9.80665) # Minimize time at the end of the phase phase.add_objective('g') p.model.options['assembled_jac_type'] = 'csc' p.model.linear_solver = DirectSolver() p.setup(check=True) p['phase0.t_initial'] = 0.0 p['phase0.t_duration'] = 2.0 p['phase0.states:x'] = phase.interpolate(ys=[0, 10], nodes='state_input') p['phase0.states:y'] = phase.interpolate(ys=[10, 5], nodes='state_input') p['phase0.states:v'] = phase.interpolate(ys=[0, 9.9], nodes='state_input') p['phase0.controls:theta'] = phase.interpolate(ys=[5, 100], nodes='control_input') p['phase0.design_parameters:g'] = 9.80665 p.run_driver() assert_rel_error(self, p['phase0.t_duration'], 10, tolerance=1.0E-3)
def test_solver_defects_reverse_propagation(self): prob = Problem() num_seg = 5 seg_ends, _ = lgl(num_seg + 1) traj = prob.model.add_subsystem('traj', Trajectory()) # First phase: normal operation. transcription = Radau(num_segments=5, order=5, segment_ends=seg_ends, compressed=True) phase0 = Phase(ode_class=BatteryODE, transcription=transcription) traj_p0 = traj.add_phase('phase0', phase0) traj_p0.set_time_options(fix_initial=True, fix_duration=True) traj_p0.set_state_options('state_of_charge', fix_initial=True, fix_final=False, solve_segments=True) # Second phase: normal operation. phase1 = Phase(ode_class=BatteryODE, transcription=transcription) traj_p1 = traj.add_phase('phase1', phase1) traj_p1.set_time_options(fix_initial=False, fix_duration=True) traj_p1.set_state_options('state_of_charge', fix_initial=False, fix_final=False, solve_segments=True) traj_p1.add_objective('time', loc='final') traj.link_phases(phases=['phase0', 'phase1'], vars=['state_of_charge', 'time'], connected=True) prob.setup() prob['traj.phase0.t_initial'] = 0 prob['traj.phase0.t_duration'] = -1.0 * 3600 prob['traj.phase0.states:state_of_charge'][:] = 0.23794217 prob['traj.phase1.t_initial'] = 0 prob['traj.phase1.t_duration'] = -1.0 * 3600 prob.set_solver_print(level=0) prob.run_model() soc1 = prob['traj.phase1.states:state_of_charge'] assert_rel_error(self, soc1[-1], 1.0, 1e-6)
def _make_problem(self, transcription, num_seg, transcription_order=3): p = Problem(model=Group()) p.driver = ScipyOptimizeDriver() # Compute sparsity/coloring when run_driver is called p.driver.options['dynamic_simul_derivs'] = True t = {'gauss-lobatto': GaussLobatto(num_segments=num_seg, order=transcription_order), 'radau-ps': Radau(num_segments=num_seg, order=transcription_order), 'runge-kutta': RungeKutta(num_segments=num_seg)} phase = Phase(ode_class=_BrachistochroneTestODE, transcription=t[transcription]) p.model.add_subsystem('phase0', phase) phase.set_time_options(initial_bounds=(1, 1), duration_bounds=(.5, 10)) phase.set_state_options('x', fix_initial=True) phase.set_state_options('y', fix_initial=True) phase.set_state_options('v', fix_initial=True) phase.add_control('theta', units='deg', rate_continuity=True, lower=0.01, upper=179.9) phase.add_design_parameter('g', units='m/s**2', opt=False, val=9.80665) phase.add_boundary_constraint('x', loc='final', equals=10) phase.add_boundary_constraint('y', loc='final', equals=5) # Minimize time at the end of the phase phase.add_objective('time', loc='final', scaler=10) p.model.linear_solver = DirectSolver() p.setup() p['phase0.t_initial'] = 1.0 p['phase0.t_duration'] = 3.0 p['phase0.states:x'] = phase.interpolate(ys=[0, 10], nodes='state_input') p['phase0.states:y'] = phase.interpolate(ys=[10, 5], nodes='state_input') p['phase0.states:v'] = phase.interpolate(ys=[0, 9.9], nodes='state_input') p['phase0.controls:theta'] = phase.interpolate(ys=[5, 100.5], nodes='control_input') return p
def test_radau(self): p = Problem(model=Group()) phase = Phase(ode_class=MyODE, ode_init_kwargs={'n_traj': n_traj}, transcription=Radau(num_segments=25, order=3, compressed=True)) p.model.add_subsystem('phase0', phase) phase.add_input_parameter('alpha', val=np.ones((n_traj, 2)), units='m') try: p.setup() except Exception as e: self.fail('Exception encountered in setup:\n' + str(e))
def test_timeseries_radau(self): p = Problem(model=Group()) p.driver = ScipyOptimizeDriver() p.driver.options['dynamic_simul_derivs'] = True phase = Phase(ode_class=BrachistochroneODE, transcription=Radau(num_segments=8, order=3, compressed=True)) p.model.add_subsystem('phase0', phase) phase.set_time_options(fix_initial=True, duration_bounds=(.5, 10)) phase.set_state_options('x', fix_initial=True, fix_final=True) phase.set_state_options('y', fix_initial=True, fix_final=True) phase.set_state_options('v', fix_initial=True, fix_final=False) phase.add_control('theta', continuity=True, rate_continuity=True, units='deg', lower=0.01, upper=179.9) phase.add_design_parameter('g', units='m/s**2', opt=False, val=9.80665) # Minimize time at the end of the phase phase.add_objective('time_phase', loc='final', scaler=10) p.model.options['assembled_jac_type'] = 'csc' p.model.linear_solver = DirectSolver() p.setup(check=True) p['phase0.t_initial'] = 0.0 p['phase0.t_duration'] = 2.0 p['phase0.states:x'] = phase.interpolate(ys=[0, 10], nodes='state_input') p['phase0.states:y'] = phase.interpolate(ys=[10, 5], nodes='state_input') p['phase0.states:v'] = phase.interpolate(ys=[0, 9.9], nodes='state_input') p['phase0.controls:theta'] = phase.interpolate(ys=[5, 100], nodes='control_input') p['phase0.design_parameters:g'] = 9.80665 p.run_driver() gd = phase.options['transcription'].grid_data state_input_idxs = gd.subset_node_indices['state_input'] control_input_idxs = gd.subset_node_indices['control_input'] assert_rel_error(self, p.get_val('phase0.time'), p.get_val('phase0.timeseries.time')[:, 0]) assert_rel_error(self, p.get_val('phase0.time_phase'), p.get_val('phase0.timeseries.time_phase')[:, 0]) for state in ('x', 'y', 'v'): assert_rel_error( self, p.get_val('phase0.states:{0}'.format(state)), p.get_val('phase0.timeseries.states:' '{0}'.format(state))[state_input_idxs]) for control in ('theta', ): assert_rel_error( self, p.get_val('phase0.controls:{0}'.format(control)), p.get_val('phase0.timeseries.controls:' '{0}'.format(control))[control_input_idxs]) for dp in ('g', ): for i in range(gd.subset_num_nodes['all']): assert_rel_error( self, p.get_val('phase0.design_parameters:{0}'.format(dp))[0, :], p.get_val('phase0.timeseries.design_parameters:' '{0}'.format(dp))[i])
def test_reentry_radau_dymos(self): p = Problem(model=Group()) p.driver = pyOptSparseDriver() p.driver.declare_coloring() traj = p.model.add_subsystem("traj", Trajectory()) phase0 = traj.add_phase( "phase0", Phase(ode_class=ShuttleODE, transcription=Radau(num_segments=50, order=3))) phase0.set_time_options(fix_initial=True, units="s", duration_ref=200) phase0.set_state_options("h", fix_initial=True, fix_final=True, units="ft", rate_source="hdot", targets=["h"], lower=0, ref0=75000, ref=300000, defect_ref=1000) phase0.set_state_options("gamma", fix_initial=True, fix_final=True, units="rad", rate_source="gammadot", targets=["gamma"], lower=-89. * np.pi / 180, upper=89. * np.pi / 180) phase0.set_state_options("phi", fix_initial=True, fix_final=False, units="rad", rate_source="phidot", lower=0, upper=89. * np.pi / 180) phase0.set_state_options("psi", fix_initial=True, fix_final=False, units="rad", rate_source="psidot", targets=["psi"], lower=0, upper=90. * np.pi / 180) phase0.set_state_options("theta", fix_initial=True, fix_final=False, units="rad", rate_source="thetadot", targets=["theta"], lower=-89. * np.pi / 180, upper=89. * np.pi / 180) phase0.set_state_options("v", fix_initial=True, fix_final=True, units="ft/s", rate_source="vdot", targets=["v"], lower=0, ref0=2500, ref=25000) phase0.add_control("alpha", units="rad", opt=True, lower=-np.pi / 2, upper=np.pi / 2, targets=["alpha"]) phase0.add_control("beta", units="rad", opt=True, lower=-89 * np.pi / 180, upper=1 * np.pi / 180, targets=["beta"]) phase0.add_path_constraint("q", lower=0, upper=70, units="Btu/ft**2/s", ref=70) phase0.add_objective("theta", loc="final", ref=-0.01) p.driver.options["optimizer"] = 'SNOPT' p.driver.opt_settings["iSumm"] = 6 p.setup(check=True) p.set_val("traj.phase0.states:h", phase0.interpolate(ys=[260000, 80000], nodes="state_input"), units="ft") p.set_val("traj.phase0.states:gamma", phase0.interpolate(ys=[-1 * np.pi / 180, -5 * np.pi / 180], nodes="state_input"), units="rad") p.set_val("traj.phase0.states:phi", phase0.interpolate(ys=[0, 75 * np.pi / 180], nodes="state_input"), units="rad") p.set_val("traj.phase0.states:psi", phase0.interpolate(ys=[90 * np.pi / 180, 10 * np.pi / 180], nodes="state_input"), units="rad") p.set_val("traj.phase0.states:theta", phase0.interpolate(ys=[0, 25 * np.pi / 180], nodes="state_input"), units="rad") p.set_val("traj.phase0.states:v", phase0.interpolate(ys=[25600, 2500], nodes="state_input"), units="ft/s") p.set_val("traj.phase0.t_initial", 0, units="s") p.set_val("traj.phase0.t_duration", 2000, units="s") p.set_val("traj.phase0.controls:alpha", phase0.interpolate( ys=[17.4 * np.pi / 180, 17.4 * np.pi / 180], nodes="control_input"), units="rad") p.set_val("traj.phase0.controls:beta", phase0.interpolate(ys=[-75 * np.pi / 180, 0 * np.pi / 180], nodes="control_input"), units="rad") p.run_driver() print(p.get_val("traj.phase0.timeseries.time")[-1]) print(p.get_val("traj.phase0.timeseries.states:theta")[-1]) assert_rel_error(self, p.get_val("traj.phase0.timeseries.time")[-1], 2181.90371131, tolerance=1e-3) assert_rel_error(self, p.get_val("traj.phase0.timeseries.states:theta")[-1], .53440626, tolerance=1e-3)
def test_solver_defects(self): prob = Problem() num_seg = 5 seg_ends, _ = lgl(num_seg + 1) traj = prob.model.add_subsystem('traj', Trajectory()) # First phase: normal operation. transcription = Radau(num_segments=5, order=5, segment_ends=seg_ends, compressed=True) phase0 = Phase(ode_class=BatteryODE, transcription=transcription) traj_p0 = traj.add_phase('phase0', phase0) traj_p0.set_time_options(fix_initial=True, fix_duration=True) traj_p0.set_state_options('state_of_charge', fix_initial=True, fix_final=False, solve_segments=True) # Second phase: normal operation. phase1 = Phase(ode_class=BatteryODE, transcription=transcription) traj_p1 = traj.add_phase('phase1', phase1) traj_p1.set_time_options(fix_initial=False, fix_duration=True) traj_p1.set_state_options('state_of_charge', fix_initial=False, fix_final=False, solve_segments=True) traj_p1.add_objective('time', loc='final') # Second phase, but with battery failure. phase1_bfail = Phase(ode_class=BatteryODE, ode_init_kwargs={'num_battery': 2}, transcription=transcription) traj_p1_bfail = traj.add_phase('phase1_bfail', phase1_bfail) traj_p1_bfail.set_time_options(fix_initial=False, fix_duration=True) traj_p1_bfail.set_state_options('state_of_charge', fix_initial=False, fix_final=False, solve_segments=True) # Second phase, but with motor failure. phase1_mfail = Phase(ode_class=BatteryODE, ode_init_kwargs={'num_motor': 2}, transcription=transcription) traj_p1_mfail = traj.add_phase('phase1_mfail', phase1_mfail) traj_p1_mfail.set_time_options(fix_initial=False, fix_duration=True) traj_p1_mfail.set_state_options('state_of_charge', fix_initial=False, fix_final=False, solve_segments=True) traj.link_phases(phases=['phase0', 'phase1'], vars=['state_of_charge', 'time'], connected=True) traj.link_phases(phases=['phase0', 'phase1_bfail'], vars=['state_of_charge', 'time'], connected=True) traj.link_phases(phases=['phase0', 'phase1_mfail'], vars=['state_of_charge', 'time'], connected=True) prob.setup() prob['traj.phase0.t_initial'] = 0 prob['traj.phase0.t_duration'] = 1.0 * 3600 prob['traj.phase1.t_initial'] = 1.0 * 3600 prob['traj.phase1.t_duration'] = 1.0 * 3600 prob['traj.phase1_bfail.t_initial'] = 1.0 * 3600 prob['traj.phase1_bfail.t_duration'] = 1.0 * 3600 prob['traj.phase1_mfail.t_initial'] = 1.0 * 3600 prob['traj.phase1_mfail.t_duration'] = 1.0 * 3600 prob['traj.phase0.states:state_of_charge'][:] = 1.0 prob.set_solver_print(level=0) prob.run_model() soc0 = prob['traj.phase0.states:state_of_charge'] soc1 = prob['traj.phase1.states:state_of_charge'] soc1b = prob['traj.phase1_bfail.states:state_of_charge'] soc1m = prob['traj.phase1_mfail.states:state_of_charge'] # Final value for State of Charge in each segment should be a good test. assert_rel_error(self, soc0[-1], 0.63464982, 1e-6) assert_rel_error(self, soc1[-1], 0.23794217, 1e-6) assert_rel_error(self, soc1b[-1], 0.0281523, 1e-6) assert_rel_error(self, soc1m[-1], 0.18625395, 1e-6)
def test_optimizer_segments_direct_connections(self): prob = Problem() if optimizer == 'SNOPT': opt = prob.driver = pyOptSparseDriver() opt.options['optimizer'] = optimizer opt.options['dynamic_simul_derivs'] = True opt.opt_settings['Major iterations limit'] = 1000 opt.opt_settings['Major feasibility tolerance'] = 1.0E-6 opt.opt_settings['Major optimality tolerance'] = 1.0E-6 opt.opt_settings["Linesearch tolerance"] = 0.10 opt.opt_settings['iSumm'] = 6 else: opt = prob.driver = ScipyOptimizeDriver() opt.options['dynamic_simul_derivs'] = True num_seg = 5 seg_ends, _ = lgl(num_seg + 1) traj = prob.model.add_subsystem('traj', Trajectory()) # First phase: normal operation. transcription = Radau(num_segments=5, order=5, segment_ends=seg_ends, compressed=True) phase0 = Phase(ode_class=BatteryODE, transcription=transcription) traj_p0 = traj.add_phase('phase0', phase0) traj_p0.set_time_options(fix_initial=True, fix_duration=True) traj_p0.set_state_options('state_of_charge', fix_initial=True, fix_final=False) # Second phase: normal operation. phase1 = Phase(ode_class=BatteryODE, transcription=transcription) traj_p1 = traj.add_phase('phase1', phase1) traj_p1.set_time_options(fix_initial=False, fix_duration=True) traj_p1.set_state_options('state_of_charge', fix_initial=False, fix_final=False) traj_p1.add_objective('time', loc='final') # Second phase, but with battery failure. phase1_bfail = Phase(ode_class=BatteryODE, ode_init_kwargs={'num_battery': 2}, transcription=transcription) traj_p1_bfail = traj.add_phase('phase1_bfail', phase1_bfail) traj_p1_bfail.set_time_options(fix_initial=False, fix_duration=True) traj_p1_bfail.set_state_options('state_of_charge', fix_initial=False, fix_final=False) # Second phase, but with motor failure. phase1_mfail = Phase(ode_class=BatteryODE, ode_init_kwargs={'num_motor': 2}, transcription=transcription) traj_p1_mfail = traj.add_phase('phase1_mfail', phase1_mfail) traj_p1_mfail.set_time_options(fix_initial=False, fix_duration=True) traj_p1_mfail.set_state_options('state_of_charge', fix_initial=False, fix_final=False) traj.link_phases(phases=['phase0', 'phase1'], vars=['state_of_charge', 'time'], connected=True) traj.link_phases(phases=['phase0', 'phase1_bfail'], vars=['state_of_charge', 'time'], connected=True) traj.link_phases(phases=['phase0', 'phase1_mfail'], vars=['state_of_charge', 'time'], connected=True) prob.model.options['assembled_jac_type'] = 'csc' prob.model.linear_solver = DirectSolver(assemble_jac=True) prob.setup() prob['traj.phase0.t_initial'] = 0 prob['traj.phase0.t_duration'] = 1.0 * 3600 prob['traj.phase1.t_initial'] = 1.0 * 3600 prob['traj.phase1.t_duration'] = 1.0 * 3600 prob['traj.phase1_bfail.t_initial'] = 1.0 * 3600 prob['traj.phase1_bfail.t_duration'] = 1.0 * 3600 prob['traj.phase1_mfail.t_initial'] = 1.0 * 3600 prob['traj.phase1_mfail.t_duration'] = 1.0 * 3600 prob.set_solver_print(level=0) prob.run_driver() soc0 = prob['traj.phase0.states:state_of_charge'] soc1 = prob['traj.phase1.states:state_of_charge'] soc1b = prob['traj.phase1_bfail.states:state_of_charge'] soc1m = prob['traj.phase1_mfail.states:state_of_charge'] # Final value for State of Chrage in each segment should be a good test. assert_rel_error(self, soc0[-1], 0.63464982, 1e-6) assert_rel_error(self, soc1[-1], 0.23794217, 1e-6) assert_rel_error(self, soc1b[-1], 0.0281523, 1e-6) assert_rel_error(self, soc1m[-1], 0.18625395, 1e-6)
import numpy as np import matplotlib.pyplot as plt from openmdao.api import Problem, Group, ScipyOptimizeDriver, SqliteRecorder, CaseReader, IndepVarComp from dymos import Trajectory, GaussLobatto, Phase, Radau from shuttle_ode import ShuttleODE prob = Problem(model=Group()) traj = prob.model.add_subsystem("traj", Trajectory()) phase0 = Phase(ode_class=ShuttleODE, transcription=Radau(num_segments=20, order=3)) traj.add_phase(name="phase0", phase=phase0) phase0.set_time_options(fix_initial=True, units="s", duration_ref=200)#, duration_ref=2000, duration_bounds=(50, 3000) phase0.set_state_options("h", fix_initial=True, fix_final=True, units="ft", rate_source="hdot", targets=["h"], lower=0)#, ref=260000, defect_ref=260000, ref0=80000 phase0.set_state_options("gamma", fix_initial=True, fix_final=True, units="rad", rate_source="gammadot", targets=["gamma"], lower=-89.*np.pi/180, upper=89.*np.pi/180) phase0.set_state_options("phi", fix_initial=True, fix_final=False, units="rad", rate_source="phidot", lower=0, upper=89.*np.pi/180) phase0.set_state_options("psi", fix_initial=True, fix_final=False, units="rad", rate_source="psidot", targets=["psi"], lower=0, upper=90.*np.pi/180) phase0.set_state_options("theta", fix_initial=True, fix_final=False, units="rad", rate_source="thetadot", targets=["theta"], lower=-89.*np.pi/180, upper=89.*np.pi/180) phase0.set_state_options("v", fix_initial=True, fix_final=True, units="ft/s", rate_source="vdot", targets=["v"], lower=0)#, ref=25600, defect_ref=25600, ref0=2500 phase0.add_control("alpha", units="rad", opt=True, lower=-np.pi/2, upper=np.pi/2, targets=["alpha"]) phase0.add_control("beta", units="rad", opt=True, lower=-89*np.pi/180, upper=1*np.pi/180, targets=["beta"]) phase0.add_path_constraint("q", lower=0, upper=70, units="Btu/ft**2/s", ref=70)# phase0.add_objective("theta", loc="final", ref=-1) prob.driver = ScipyOptimizeDriver() prob.driver.declare_coloring()
def test_brachistochrone_integrated_control_radau_ps(self): import numpy as np from openmdao.api import Problem, Group, ScipyOptimizeDriver, DirectSolver from openmdao.utils.assert_utils import assert_rel_error from dymos import Phase, Radau p = Problem(model=Group()) p.driver = ScipyOptimizeDriver() phase = Phase(ode_class=BrachistochroneODE, transcription=Radau(num_segments=10)) p.model.add_subsystem('phase0', phase) phase.set_time_options(initial_bounds=(0, 0), duration_bounds=(.5, 10)) phase.set_state_options('x', fix_initial=True, fix_final=True) phase.set_state_options('y', fix_initial=True, fix_final=True) phase.set_state_options('v', fix_initial=True) phase.set_state_options('theta', targets='theta', fix_initial=False) phase.add_control('theta_dot', units='deg/s', rate_continuity=True, lower=0, upper=60) phase.add_design_parameter('g', units='m/s**2', opt=False, val=9.80665) # Minimize time at the end of the phase phase.add_objective('time', loc='final', scaler=10) p.model.linear_solver = DirectSolver() p.model.options['assembled_jac_type'] = 'csc' p.setup() p['phase0.t_initial'] = 0.0 p['phase0.t_duration'] = 2.0 p['phase0.states:x'] = phase.interpolate(ys=[0, 10], nodes='state_input') p['phase0.states:y'] = phase.interpolate(ys=[10, 5], nodes='state_input') p['phase0.states:v'] = phase.interpolate(ys=[0, 9.9], nodes='state_input') p['phase0.states:theta'] = np.radians( phase.interpolate(ys=[0.05, 100.0], nodes='state_input')) p['phase0.controls:theta_dot'] = phase.interpolate( ys=[50, 50], nodes='control_input') # Solve for the optimal trajectory p.run_driver() # Test the results assert_rel_error(self, p.get_val('phase0.timeseries.time')[-1], 1.8016, tolerance=1.0E-3) sim_out = phase.simulate(times_per_seg=20) x_sol = p.get_val('phase0.timeseries.states:x') y_sol = p.get_val('phase0.timeseries.states:y') v_sol = p.get_val('phase0.timeseries.states:v') theta_sol = p.get_val('phase0.timeseries.states:theta') theta_dot_sol = p.get_val('phase0.timeseries.controls:theta_dot') time_sol = p.get_val('phase0.timeseries.time') x_sim = sim_out.get_val('phase0.timeseries.states:x') y_sim = sim_out.get_val('phase0.timeseries.states:y') v_sim = sim_out.get_val('phase0.timeseries.states:v') theta_sim = sim_out.get_val('phase0.timeseries.states:theta') theta_dot_sim = sim_out.get_val('phase0.timeseries.controls:theta_dot') time_sim = sim_out.get_val('phase0.timeseries.time') x_interp = interp1d(time_sim[:, 0], x_sim[:, 0]) y_interp = interp1d(time_sim[:, 0], y_sim[:, 0]) v_interp = interp1d(time_sim[:, 0], v_sim[:, 0]) theta_interp = interp1d(time_sim[:, 0], theta_sim[:, 0]) theta_dot_interp = interp1d(time_sim[:, 0], theta_dot_sim[:, 0]) assert_rel_error(self, x_interp(time_sol), x_sol, tolerance=1.0E-5) assert_rel_error(self, y_interp(time_sol), y_sol, tolerance=1.0E-5) assert_rel_error(self, v_interp(time_sol), v_sol, tolerance=1.0E-5) assert_rel_error(self, theta_interp(time_sol), theta_sol, tolerance=1.0E-5) assert_rel_error(self, theta_dot_interp(time_sol), theta_dot_sol, tolerance=1.0E-5)
def brachistochrone_min_time(transcription='gauss-lobatto', num_segments=8, transcription_order=3, compressed=True, sim_record='brach_min_time_sim.db', optimizer='SLSQP', dynamic_simul_derivs=True, force_alloc_complex=False, solve_segments=False, run_driver=True): p = Problem(model=Group()) if optimizer == 'SNOPT': p.driver = pyOptSparseDriver() p.driver.options['optimizer'] = optimizer p.driver.opt_settings['Major iterations limit'] = 100 p.driver.opt_settings['Major feasibility tolerance'] = 1.0E-6 p.driver.opt_settings['Major optimality tolerance'] = 1.0E-6 p.driver.opt_settings['iSumm'] = 6 else: p.driver = ScipyOptimizeDriver() p.driver.options['dynamic_simul_derivs'] = dynamic_simul_derivs if transcription == 'runge-kutta': transcription = RungeKutta(num_segments=num_segments, compressed=compressed) elif transcription == 'gauss-lobatto': transcription = GaussLobatto(num_segments=num_segments, order=transcription_order, compressed=compressed) elif transcription == 'radau-ps': transcription = Radau(num_segments=num_segments, order=transcription_order, compressed=compressed) phase = Phase(ode_class=BrachistochroneVectorStatesODE, transcription=transcription) p.model.add_subsystem('phase0', phase) phase.set_time_options(fix_initial=True, duration_bounds=(.5, 10)) fix_final = not solve_segments # can't fix final position if you're solving the segments phase.set_state_options('pos', fix_initial=True, fix_final=fix_final, solve_segments=solve_segments) phase.set_state_options('v', fix_initial=True, fix_final=False, solve_segments=solve_segments) phase.add_control('theta', continuity=True, rate_continuity=True, units='deg', lower=0.01, upper=179.9) phase.add_design_parameter('g', units='m/s**2', opt=False, val=9.80665) # Minimize time at the end of the phase phase.add_objective('time', loc='final', scaler=10) p.model.linear_solver = DirectSolver() p.setup(check=True, force_alloc_complex=force_alloc_complex) p['phase0.t_initial'] = 0.0 p['phase0.t_duration'] = 2.0 pos0 = [0, 10] posf = [10, 5] p['phase0.states:pos'] = phase.interpolate(ys=[pos0, posf], nodes='state_input') p['phase0.states:v'] = phase.interpolate(ys=[0, 9.9], nodes='state_input') p['phase0.controls:theta'] = phase.interpolate(ys=[5, 100], nodes='control_input') p['phase0.design_parameters:g'] = 9.80665 p.run_model() if run_driver: p.run_driver() # Plot results if SHOW_PLOTS: p.run_driver() exp_out = phase.simulate(record_file=sim_record) fig, ax = plt.subplots() fig.suptitle('Brachistochrone Solution') x_imp = p.get_val('phase0.timeseries.states:pos')[:, 0] y_imp = p.get_val('phase0.timeseries.states:pos')[:, 1] x_exp = exp_out.get_val('phase0.timeseries.states:pos')[:, 0] y_exp = exp_out.get_val('phase0.timeseries.states:pos')[:, 1] ax.plot(x_imp, y_imp, 'ro', label='implicit') ax.plot(x_exp, y_exp, 'b-', label='explicit') ax.set_xlabel('x (m)') ax.set_ylabel('y (m)') ax.grid(True) ax.legend(loc='upper right') fig, ax = plt.subplots() fig.suptitle('Brachistochrone Solution') x_imp = p.get_val('phase0.timeseries.time') y_imp = p.get_val('phase0.timeseries.control_rates:theta_rate2') x_exp = exp_out.get_val('phase0.timeseries.time') y_exp = exp_out.get_val('phase0.timeseries.control_rates:theta_rate2') ax.plot(x_imp, y_imp, 'ro', label='implicit') ax.plot(x_exp, y_exp, 'b-', label='explicit') ax.set_xlabel('time (s)') ax.set_ylabel('theta rate2 (rad/s**2)') ax.grid(True) ax.legend(loc='lower right') plt.show() return p
def test_basic(self): import matplotlib.pyplot as plt from openmdao.api import Problem, ScipyOptimizeDriver, DirectSolver from openmdao.utils.assert_utils import assert_rel_error from dymos import Trajectory, Phase, Radau from dymos.examples.battery_multibranch.battery_multibranch_ode import BatteryODE from dymos.utils.lgl import lgl prob = Problem() opt = prob.driver = ScipyOptimizeDriver() opt.options['dynamic_simul_derivs'] = True opt.options['optimizer'] = 'SLSQP' num_seg = 5 seg_ends, _ = lgl(num_seg + 1) traj = prob.model.add_subsystem('traj', Trajectory()) # First phase: normal operation. transcription = Radau(num_segments=num_seg, order=5, segment_ends=seg_ends, compressed=False) phase0 = Phase(ode_class=BatteryODE, transcription=transcription) traj_p0 = traj.add_phase('phase0', phase0) traj_p0.set_time_options(fix_initial=True, fix_duration=True) traj_p0.set_state_options('state_of_charge', fix_initial=True, fix_final=False) # Second phase: normal operation. phase1 = Phase(ode_class=BatteryODE, transcription=transcription) traj_p1 = traj.add_phase('phase1', phase1) traj_p1.set_time_options(fix_initial=False, fix_duration=True) traj_p1.set_state_options('state_of_charge', fix_initial=False, fix_final=False) traj_p1.add_objective('time', loc='final') # Second phase, but with battery failure. phase1_bfail = Phase(ode_class=BatteryODE, ode_init_kwargs={'num_battery': 2}, transcription=transcription) traj_p1_bfail = traj.add_phase('phase1_bfail', phase1_bfail) traj_p1_bfail.set_time_options(fix_initial=False, fix_duration=True) traj_p1_bfail.set_state_options('state_of_charge', fix_initial=False, fix_final=False) # Second phase, but with motor failure. phase1_mfail = Phase(ode_class=BatteryODE, ode_init_kwargs={'num_motor': 2}, transcription=transcription) traj_p1_mfail = traj.add_phase('phase1_mfail', phase1_mfail) traj_p1_mfail.set_time_options(fix_initial=False, fix_duration=True) traj_p1_mfail.set_state_options('state_of_charge', fix_initial=False, fix_final=False) traj.link_phases(phases=['phase0', 'phase1'], vars=['state_of_charge', 'time']) traj.link_phases(phases=['phase0', 'phase1_bfail'], vars=['state_of_charge', 'time']) traj.link_phases(phases=['phase0', 'phase1_mfail'], vars=['state_of_charge', 'time']) prob.model.options['assembled_jac_type'] = 'csc' prob.model.linear_solver = DirectSolver(assemble_jac=True) prob.setup() prob['traj.phase0.t_initial'] = 0 prob['traj.phase0.t_duration'] = 1.0 * 3600 prob['traj.phase1.t_initial'] = 1.0 * 3600 prob['traj.phase1.t_duration'] = 1.0 * 3600 prob['traj.phase1_bfail.t_initial'] = 1.0 * 3600 prob['traj.phase1_bfail.t_duration'] = 1.0 * 3600 prob['traj.phase1_mfail.t_initial'] = 1.0 * 3600 prob['traj.phase1_mfail.t_duration'] = 1.0 * 3600 prob.set_solver_print(level=0) prob.run_driver() soc0 = prob['traj.phase0.states:state_of_charge'] soc1 = prob['traj.phase1.states:state_of_charge'] soc1b = prob['traj.phase1_bfail.states:state_of_charge'] soc1m = prob['traj.phase1_mfail.states:state_of_charge'] # Final value for State of Chrage in each segment should be a good test. print('State of Charge after 1 hour') assert_rel_error(self, soc0[-1], 0.63464982, 1e-6) print('State of Charge after 2 hours') assert_rel_error(self, soc1[-1], 0.23794217, 1e-6) print('State of Charge after 2 hours, battery fails at 1 hour') assert_rel_error(self, soc1b[-1], 0.0281523, 1e-6) print('State of Charge after 2 hours, motor fails at 1 hour') assert_rel_error(self, soc1m[-1], 0.18625395, 1e-6) # Plot Results t0 = prob['traj.phases.phase0.time.time'] / 3600 t1 = prob['traj.phases.phase1.time.time'] / 3600 t1b = prob['traj.phases.phase1_bfail.time.time'] / 3600 t1m = prob['traj.phases.phase1_mfail.time.time'] / 3600 plt.subplot(2, 1, 1) plt.plot(t0, soc0, 'b') plt.plot(t1, soc1, 'b') plt.plot(t1b, soc1b, 'r') plt.plot(t1m, soc1m, 'c') plt.xlabel('Time (hour)') plt.ylabel('State of Charge (percent)') I_Li0 = prob['traj.phases.phase0.rhs_all.pwr_balance.I_Li'] I_Li1 = prob['traj.phases.phase1.rhs_all.pwr_balance.I_Li'] I_Li1b = prob['traj.phases.phase1_bfail.rhs_all.pwr_balance.I_Li'] I_Li1m = prob['traj.phases.phase1_mfail.rhs_all.pwr_balance.I_Li'] plt.subplot(2, 1, 2) plt.plot(t0, I_Li0, 'b') plt.plot(t1, I_Li1, 'b') plt.plot(t1b, I_Li1b, 'r') plt.plot(t1m, I_Li1m, 'c') plt.xlabel('Time (hour)') plt.ylabel('Line Current (A)') plt.legend([ 'Phase 1', 'Phase 2', 'Phase 2 Battery Fail', 'Phase 2 Motor Fail' ], loc=2) plt.show()
def test_dynamic_input_params(self): prob = Problem(model=Group()) traj = prob.model.add_subsystem('traj', Trajectory()) # First phase: normal operation. # NOTE: using RK4 integration here P_DEMAND = 2.0 phase0 = Phase(ode_class=BatteryODE, transcription=RungeKutta(num_segments=200)) phase0.set_time_options(fix_initial=True, fix_duration=True) phase0.set_state_options('state_of_charge', fix_initial=True, fix_final=False) phase0.add_timeseries_output('battery.V_oc', output_name='V_oc', units='V') phase0.add_timeseries_output('battery.V_pack', output_name='V_pack', units='V') phase0.add_timeseries_output('pwr_balance.I_Li', output_name='I_Li', units='A') phase0.add_input_parameter('P_demand', val=P_DEMAND, units='W') traj.add_phase('phase0', phase0) # Second phase: normal operation. transcription = Radau(num_segments=5, order=5, compressed=True) phase1 = Phase(ode_class=BatteryODE, transcription=transcription) phase1.set_time_options(fix_initial=False, fix_duration=True) phase1.set_state_options('state_of_charge', fix_initial=False, fix_final=False, solve_segments=True) phase1.add_timeseries_output('battery.V_oc', output_name='V_oc', units='V') phase1.add_timeseries_output('battery.V_pack', output_name='V_pack', units='V') phase1.add_timeseries_output('pwr_balance.I_Li', output_name='I_Li', units='A') phase1.add_input_parameter('P_demand', val=P_DEMAND, units='W') traj.add_phase('phase1', phase1) # Second phase, but with battery failure. phase1_bfail = Phase(ode_class=BatteryODE, ode_init_kwargs={'num_battery': 2}, transcription=transcription) phase1_bfail.set_time_options(fix_initial=False, fix_duration=True) phase1_bfail.set_state_options('state_of_charge', fix_initial=False, fix_final=False, solve_segments=True) phase1_bfail.add_timeseries_output('battery.V_oc', output_name='V_oc', units='V') phase1_bfail.add_timeseries_output('battery.V_pack', output_name='V_pack', units='V') phase1_bfail.add_timeseries_output('pwr_balance.I_Li', output_name='I_Li', units='A') phase1_bfail.add_input_parameter('P_demand', val=P_DEMAND, units='W') traj.add_phase('phase1_bfail', phase1_bfail) # Second phase, but with motor failure. phase1_mfail = Phase(ode_class=BatteryODE, ode_init_kwargs={'num_motor': 2}, transcription=transcription) phase1_mfail.set_time_options(fix_initial=False, fix_duration=True) phase1_mfail.set_state_options('state_of_charge', fix_initial=False, fix_final=False, solve_segments=True) phase1_mfail.add_timeseries_output('battery.V_oc', output_name='V_oc', units='V') phase1_mfail.add_timeseries_output('battery.V_pack', output_name='V_pack', units='V') phase1_mfail.add_timeseries_output('pwr_balance.I_Li', output_name='I_Li', units='A') phase1_mfail.add_input_parameter('P_demand', val=P_DEMAND, units='W') traj.add_phase('phase1_mfail', phase1_mfail) traj.link_phases(phases=['phase0', 'phase1'], vars=['state_of_charge', 'time'], connected=True) traj.link_phases(phases=['phase0', 'phase1_bfail'], vars=['state_of_charge', 'time'], connected=True) traj.link_phases(phases=['phase0', 'phase1_mfail'], vars=['state_of_charge', 'time'], connected=True) # prob.model.linear_solver = DirectSolver(assemble_jac=True) prob.setup() prob.final_setup() prob['traj.phases.phase0.time_extents.t_initial'] = 0 prob['traj.phases.phase0.time_extents.t_duration'] = 1.0*3600 # prob['traj.phases.phase1.time_extents.t_initial'] = 1.0*3600 prob['traj.phases.phase1.time_extents.t_duration'] = 1.0*3600 # prob['traj.phases.phase1_bfail.time_extents.t_initial'] = 1.0*3600 prob['traj.phases.phase1_bfail.time_extents.t_duration'] = 1.0*3600 # prob['traj.phases.phase1_mfail.time_extents.t_initial'] = 1.0*3600 prob['traj.phases.phase1_mfail.time_extents.t_duration'] = 1.0*3600 prob.set_solver_print(level=0) prob.run_model() plot = True if plot: import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt t0 = prob['traj.phase0.timeseries.time'] t1 = prob['traj.phase1.timeseries.time'] t1b = prob['traj.phase1_bfail.timeseries.time'] t1m = prob['traj.phase1_mfail.timeseries.time'] soc0 = prob['traj.phase0.timeseries.states:state_of_charge'] soc1 = prob['traj.phase1.timeseries.states:state_of_charge'] soc1b = prob['traj.phase1_bfail.timeseries.states:state_of_charge'] soc1m = prob['traj.phase1_mfail.timeseries.states:state_of_charge'] plt.subplot(2, 2, 1) plt.plot(t0, soc0, 'b') plt.plot(t1, soc1, 'b') plt.plot(t1b, soc1b, 'r') plt.plot(t1m, soc1m, 'c') plt.xlabel('Time (hour)') plt.ylabel('State of Charge (percent)') V_oc0 = prob['traj.phase0.timeseries.V_oc'] V_oc1 = prob['traj.phase1.timeseries.V_oc'] V_oc1b = prob['traj.phase1_bfail.timeseries.V_oc'] V_oc1m = prob['traj.phase1_mfail.timeseries.V_oc'] plt.subplot(2, 2, 2) plt.plot(t0, V_oc0, 'b') plt.plot(t1, V_oc1, 'b') plt.plot(t1b, V_oc1b, 'r') plt.plot(t1m, V_oc1m, 'c') plt.xlabel('Time (hour)') plt.ylabel('Open Circuit Voltage (V)') V_pack0 = prob['traj.phase0.timeseries.V_pack'] V_pack1 = prob['traj.phase1.timeseries.V_pack'] V_pack1b = prob['traj.phase1_bfail.timeseries.V_pack'] V_pack1m = prob['traj.phase1_mfail.timeseries.V_pack'] plt.subplot(2, 2, 3) plt.plot(t0, V_pack0, 'b') plt.plot(t1, V_pack1, 'b') plt.plot(t1b, V_pack1b, 'r') plt.plot(t1m, V_pack1m, 'c') plt.xlabel('Time (hour)') plt.ylabel('Terminal Voltage (V)') I_Li0 = prob['traj.phase0.timeseries.I_Li'] I_Li1 = prob['traj.phase1.timeseries.I_Li'] I_Li1b = prob['traj.phase1_bfail.timeseries.I_Li'] I_Li1m = prob['traj.phase1_mfail.timeseries.I_Li'] plt.subplot(2, 2, 4) plt.plot(t0, I_Li0, 'b') plt.plot(t1, I_Li1, 'b') plt.plot(t1b, I_Li1b, 'r') plt.plot(t1m, I_Li1m, 'c') plt.xlabel('Time (hour)') plt.ylabel('Line Current (A)') plt.show()
def test_brachistochrone_undecorated_ode_radau(self): import numpy as np import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt from openmdao.api import Problem, Group, ScipyOptimizeDriver, DirectSolver from openmdao.utils.assert_utils import assert_rel_error from dymos import Phase, Radau p = Problem(model=Group()) p.driver = ScipyOptimizeDriver() phase = Phase(ode_class=BrachistochroneODE, transcription=Radau(num_segments=10)) p.model.add_subsystem('phase0', phase) phase.set_time_options(initial_bounds=(0, 0), duration_bounds=(.5, 10), units='s') phase.set_state_options('x', fix_initial=True, fix_final=True, rate_source='xdot', units='m') phase.set_state_options('y', fix_initial=True, fix_final=True, rate_source='ydot', units='m') phase.set_state_options('v', fix_initial=True, rate_source='vdot', targets=['v'], units='m/s') phase.add_control('theta', units='deg', rate_continuity=False, lower=0.01, upper=179.9, targets=['theta']) phase.add_design_parameter('g', units='m/s**2', opt=False, val=9.80665, targets=['g']) # Minimize time at the end of the phase phase.add_objective('time', loc='final', scaler=10) p.model.linear_solver = DirectSolver() p.setup() p['phase0.t_initial'] = 0.0 p['phase0.t_duration'] = 2.0 p['phase0.states:x'] = phase.interpolate(ys=[0, 10], nodes='state_input') p['phase0.states:y'] = phase.interpolate(ys=[10, 5], nodes='state_input') p['phase0.states:v'] = phase.interpolate(ys=[0, 9.9], nodes='state_input') p['phase0.controls:theta'] = phase.interpolate(ys=[5, 100.5], nodes='control_input') # Solve for the optimal trajectory p.run_driver() # Test the results assert_rel_error(self, p.get_val('phase0.timeseries.time')[-1], 1.8016, tolerance=1.0E-3)
from __future__ import print_function, division, absolute_import import numpy as np import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt from openmdao.api import Problem, Group from dymos import Phase, Radau from dymos.examples.brachistochrone.brachistochrone_ode import BrachistochroneODE p = Problem(model=Group()) phase = Phase(ode_class=BrachistochroneODE, transcription=Radau(num_segments=4, order=[3, 5, 3, 5])) p.model.add_subsystem('phase0', phase) p.setup() p['phase0.t_initial'] = 1.0 p['phase0.t_duration'] = 9.0 p.run_model() grid_data = phase.options['transcription'].grid_data t_all = p.get_val('phase0.timeseries.time') t_disc = t_all[grid_data.subset_node_indices['state_disc'], 0] t_col = t_all[grid_data.subset_node_indices['col'], 0] def f(x): # pragma: no cover return np.sin(x) / x + 1
def test_reentry_mixed_controls(self): p = om.Problem(model=om.Group()) p.driver = om.pyOptSparseDriver() p.driver.declare_coloring(tol=1.0E-12) p.driver.options['optimizer'] = 'IPOPT' p.driver.opt_settings['alpha_for_y'] = 'safer-min-dual-infeas' p.driver.opt_settings['print_level'] = 5 p.driver.opt_settings['nlp_scaling_method'] = 'gradient-based' p.driver.opt_settings['mu_strategy'] = 'monotone' traj = p.model.add_subsystem('traj', Trajectory()) phase0 = traj.add_phase( 'phase0', Phase(ode_class=ShuttleODE, transcription=Radau(num_segments=30, order=3))) phase0.set_time_options(fix_initial=True, units='s', duration_ref=200) phase0.add_state('h', fix_initial=True, fix_final=True, units='ft', rate_source='hdot', targets=['h'], lower=0, ref0=75000, ref=300000, defect_ref=1000) phase0.add_state('gamma', fix_initial=True, fix_final=True, units='rad', rate_source='gammadot', targets=['gamma'], lower=-89. * np.pi / 180, upper=89. * np.pi / 180) phase0.add_state('phi', fix_initial=True, fix_final=False, units='rad', rate_source='phidot', lower=0, upper=89. * np.pi / 180) phase0.add_state('psi', fix_initial=True, fix_final=False, units='rad', rate_source='psidot', targets=['psi'], lower=0, upper=90. * np.pi / 180) phase0.add_state('theta', fix_initial=True, fix_final=False, units='rad', rate_source='thetadot', targets=['theta'], lower=-89. * np.pi / 180, upper=89. * np.pi / 180) phase0.add_state('v', fix_initial=True, fix_final=True, units='ft/s', rate_source='vdot', targets=['v'], lower=500, ref0=2500, ref=25000) phase0.add_control('alpha', units='rad', opt=True, lower=-np.pi / 2, upper=np.pi / 2) phase0.add_polynomial_control('beta', order=9, units='rad', opt=True, lower=-89 * np.pi / 180, upper=1 * np.pi / 180) phase0.add_objective('theta', loc='final', ref=-0.01) phase0.add_path_constraint('q', lower=0, upper=70, ref=70) p.setup(check=True, force_alloc_complex=True) p.set_val('traj.phase0.states:h', phase0.interp('h', [260000, 80000]), units='ft') p.set_val('traj.phase0.states:gamma', phase0.interp('gamma', [-1 * np.pi / 180, -5 * np.pi / 180]), units='rad') p.set_val('traj.phase0.states:phi', phase0.interp('phi', [0, 75 * np.pi / 180]), units='rad') p.set_val('traj.phase0.states:psi', phase0.interp('psi', [90 * np.pi / 180, 10 * np.pi / 180]), units='rad') p.set_val('traj.phase0.states:theta', phase0.interp('theta', [0, 25 * np.pi / 180]), units='rad') p.set_val('traj.phase0.states:v', phase0.interp('v', [25600, 2500]), units='ft/s') p.set_val('traj.phase0.t_initial', 0, units='s') p.set_val('traj.phase0.t_duration', 2000, units='s') p.set_val('traj.phase0.controls:alpha', phase0.interp('alpha', [17.4, 17.4]), units='deg') p.set_val('traj.phase0.polynomial_controls:beta', phase0.interp('beta', [-20, 0]), units='deg') run_problem(p, simulate=True) sol = om.CaseReader('dymos_solution.db').get_case('final') sim = om.CaseReader('dymos_simulation.db').get_case('final') from scipy.interpolate import interp1d t_sol = sol.get_val('traj.phase0.timeseries.time') beta_sol = sol.get_val( 'traj.phase0.timeseries.polynomial_controls:beta', units='deg') t_sim = sim.get_val('traj.phase0.timeseries.time') beta_sim = sim.get_val( 'traj.phase0.timeseries.polynomial_controls:beta', units='deg') sol_interp = interp1d(t_sol.ravel(), beta_sol.ravel()) sim_interp = interp1d(t_sim.ravel(), beta_sim.ravel()) t = np.linspace(0, t_sol.ravel()[-1], 1000) assert_near_equal(sim_interp(t), sol_interp(t), tolerance=0.01) assert_near_equal(p.get_val('traj.phase0.timeseries.time')[-1], expected_results['constrained']['time'], tolerance=1e-2) assert_near_equal(p.get_val('traj.phase0.timeseries.states:theta', units='deg')[-1], expected_results['constrained']['theta'], tolerance=1e-2)
def test_brachistochrone_vector_ode_path_constraints_radau_no_indices( self): p = Problem(model=Group()) p.driver = ScipyOptimizeDriver() p.driver.options['dynamic_simul_derivs'] = True phase = Phase(ode_class=BrachistochroneVectorStatesODE, transcription=Radau(num_segments=20, order=3)) p.model.add_subsystem('phase0', phase) phase.set_time_options(fix_initial=True, duration_bounds=(.5, 10)) phase.set_state_options('pos', fix_initial=True, fix_final=True) phase.set_state_options('v', fix_initial=True, fix_final=False) phase.add_control('theta', continuity=True, rate_continuity=True, units='deg', lower=0.01, upper=179.9) phase.add_design_parameter('g', units='m/s**2', opt=False, val=9.80665) phase.add_path_constraint('pos_dot', shape=(2, ), units='m/s', lower=-4, upper=12) phase.add_timeseries_output('pos_dot', shape=(2, ), units='m/s') # Minimize time at the end of the phase phase.add_objective('time', loc='final', scaler=10) p.model.linear_solver = DirectSolver() p.setup(check=True, force_alloc_complex=True) p['phase0.t_initial'] = 0.0 p['phase0.t_duration'] = 2.0 pos0 = [0, 10] posf = [10, 5] p['phase0.states:pos'] = phase.interpolate(ys=[pos0, posf], nodes='state_input') p['phase0.states:v'] = phase.interpolate(ys=[0, 9.9], nodes='state_input') p['phase0.controls:theta'] = phase.interpolate(ys=[5, 100], nodes='control_input') p['phase0.design_parameters:g'] = 9.80665 p.run_driver() assert_rel_error(self, np.min(p.get_val('phase0.timeseries.pos_dot')[:, -1]), -4, tolerance=1.0E-2) # Plot results if SHOW_PLOTS: exp_out = phase.simulate(times_per_seg=50) fig, ax = plt.subplots() fig.suptitle('Brachistochrone Solution') x_imp = p.get_val('phase0.timeseries.states:pos')[:, 0] y_imp = p.get_val('phase0.timeseries.states:pos')[:, 1] x_exp = exp_out.get_val('phase0.timeseries.states:pos')[:, 0] y_exp = exp_out.get_val('phase0.timeseries.states:pos')[:, 1] ax.plot(x_imp, y_imp, 'ro', label='implicit') ax.plot(x_exp, y_exp, 'b-', label='explicit') ax.set_xlabel('x (m)') ax.set_ylabel('y (m)') ax.grid(True) ax.legend(loc='upper right') fig, ax = plt.subplots() fig.suptitle('Brachistochrone Solution\nVelocity') t_imp = p.get_val('phase0.timeseries.time') t_exp = exp_out.get_val('phase0.timeseries.time') xdot_imp = p.get_val('phase0.timeseries.pos_dot')[:, 0] ydot_imp = p.get_val('phase0.timeseries.pos_dot')[:, 1] xdot_exp = exp_out.get_val('phase0.timeseries.pos_dot')[:, 0] ydot_exp = exp_out.get_val('phase0.timeseries.pos_dot')[:, 1] ax.plot(t_imp, xdot_imp, 'bo', label='implicit') ax.plot(t_exp, xdot_exp, 'b-', label='explicit') ax.plot(t_imp, ydot_imp, 'ro', label='implicit') ax.plot(t_exp, ydot_exp, 'r-', label='explicit') ax.set_xlabel('t (s)') ax.set_ylabel('v (m/s)') ax.grid(True) ax.legend(loc='upper right') fig, ax = plt.subplots() fig.suptitle('Brachistochrone Solution') x_imp = p.get_val('phase0.timeseries.time') y_imp = p.get_val('phase0.timeseries.control_rates:theta_rate2') x_exp = exp_out.get_val('phase0.timeseries.time') y_exp = exp_out.get_val( 'phase0.timeseries.control_rates:theta_rate2') ax.plot(x_imp, y_imp, 'ro', label='implicit') ax.plot(x_exp, y_exp, 'b-', label='explicit') ax.set_xlabel('time (s)') ax.set_ylabel('theta rate2 (rad/s**2)') ax.grid(True) ax.legend(loc='lower right') plt.show() return p
def brachistochrone_min_time(transcription='gauss-lobatto', num_segments=8, transcription_order=3, run_driver=True, compressed=True, optimizer='SLSQP'): p = Problem(model=Group()) # if optimizer == 'SNOPT': p.driver = pyOptSparseDriver() p.driver.options['optimizer'] = optimizer p.driver.options['dynamic_simul_derivs'] = True if transcription == 'gauss-lobatto': t = GaussLobatto(num_segments=num_segments, order=transcription_order, compressed=compressed) elif transcription == 'radau-ps': t = Radau(num_segments=num_segments, order=transcription_order, compressed=compressed) elif transcription == 'runge-kutta': t = RungeKutta(num_segments=num_segments, order=transcription_order, compressed=compressed) phase = Phase(ode_class=BrachistochroneODE, transcription=t) p.model.add_subsystem('phase0', phase) phase.set_time_options(fix_initial=True, duration_bounds=(.5, 10)) phase.set_state_options('x', fix_initial=True, fix_final=False, solve_segments=False) phase.set_state_options('y', fix_initial=True, fix_final=False, solve_segments=False) phase.set_state_options('v', fix_initial=True, fix_final=False, solve_segments=False) phase.add_control('theta', continuity=True, rate_continuity=True, units='deg', lower=0.01, upper=179.9) phase.add_input_parameter('g', units='m/s**2', val=9.80665) phase.add_boundary_constraint('x', loc='final', equals=10) phase.add_boundary_constraint('y', loc='final', equals=5) # Minimize time at the end of the phase phase.add_objective('time_phase', loc='final', scaler=10) p.model.linear_solver = DirectSolver() p.setup(check=True) p['phase0.t_initial'] = 0.0 p['phase0.t_duration'] = 2.0 p['phase0.states:x'] = phase.interpolate(ys=[0, 10], nodes='state_input') p['phase0.states:y'] = phase.interpolate(ys=[10, 5], nodes='state_input') p['phase0.states:v'] = phase.interpolate(ys=[0, 9.9], nodes='state_input') p['phase0.controls:theta'] = phase.interpolate(ys=[5, 100], nodes='control_input') p['phase0.input_parameters:g'] = 9.80665 p.run_model() if run_driver: p.run_driver() # Plot results if SHOW_PLOTS: exp_out = phase.simulate() fig, ax = plt.subplots() fig.suptitle('Brachistochrone Solution') x_imp = p.get_val('phase0.timeseries.states:x') y_imp = p.get_val('phase0.timeseries.states:y') x_exp = exp_out.get_val('phase0.timeseries.states:x') y_exp = exp_out.get_val('phase0.timeseries.states:y') ax.plot(x_imp, y_imp, 'ro', label='implicit') ax.plot(x_exp, y_exp, 'b-', label='explicit') ax.set_xlabel('x (m)') ax.set_ylabel('y (m)') ax.grid(True) ax.legend(loc='upper right') fig, ax = plt.subplots() fig.suptitle('Brachistochrone Solution') x_imp = p.get_val('phase0.timeseries.time_phase') y_imp = p.get_val('phase0.timeseries.controls:theta') x_exp = exp_out.get_val('phase0.timeseries.time_phase') y_exp = exp_out.get_val('phase0.timeseries.controls:theta') ax.plot(x_imp, y_imp, 'ro', label='implicit') ax.plot(x_exp, y_exp, 'b-', label='explicit') ax.set_xlabel('time (s)') ax.set_ylabel('theta (rad)') ax.grid(True) ax.legend(loc='lower right') plt.show() return p
def test_battery_power(self): """ for battery explicit integration testings """ _, local_opt = set_pyoptsparse_opt('SNOPT') if local_opt != 'SNOPT': raise unittest.SkipTest("pyoptsparse is not providing SNOPT") p = om.Problem() p.driver = om.pyOptSparseDriver() p.driver.options['optimizer'] = 'SNOPT' p.driver.opt_settings['Major iterations limit'] = 100 p.driver.opt_settings['Major optimality tolerance'] = 5.0E-3 p.driver.opt_settings['Major feasibility tolerance'] = 1e-6 p.driver.opt_settings['iSumm'] = 6 transcription = Radau(num_segments=15, order=3, compressed=True) phase0 = Phase(transcription=transcription, ode_class=BatteryGroup) phase0.set_time_options(fix_initial=True, duration_bounds=(30, 30)) p.model.add_subsystem(name='phase0', subsys=phase0) phase0.add_state('SOC', fix_initial=True, rate_source='dXdt:SOC', lower=0.0, upper=1.) phase0.add_state('U_Th', units='V', fix_initial=False, rate_source='dXdt:V_{thev}', lower=0.0, upper=5.0) # phase0.add_parameter('P_out', units='W', opt=False) # phase0.add_boundary_constraint('U_pack', units='V', loc='initial', equals=5100) phase0.add_objective('time', loc='final', ref=1) p.model.linear_solver = om.DirectSolver(assemble_jac=True) phase0.add_timeseries_output('Q_{batt}', output_name='Q_{batt}', units='W') # phase0.add_timeseries_output('U_pack', output_name='V', units='V') p.setup() # p.check_partials() T0 = 10 + 273 p['phase0.t_initial'] = 0.0 p['phase0.t_duration'] = 30 p['phase0.states:SOC'] = phase0.interpolate(ys=[1.0, 0.0], nodes='state_input') p['phase0.states:U_Th'] = phase0.interpolate(ys=[0.1, 0.1], nodes='state_input') # p['phase0.parameters:P_out'][:] = 72000. p.run_driver() fig, ax = plt.subplots(3, 1, sharex=True) fig.suptitle('Temperature Plots') t_opt = p.get_val('phase0.timeseries.time') SOC_opt = p.get_val('phase0.timeseries.states:SOC', units=None) Q_batt_opt = p.get_val('phase0.timeseries.Q_{batt}', units='kW') ax[1].plot(t_opt, Q_batt_opt * 128 * 40, 'r', label='$Q_{cell}$') ax[2].plot(t_opt, SOC_opt, 'r', label='$SOC$') #spot check final values # assert_rel_error(self, T_batt_opt[-1], 1.25934406, tolerance=1.0E-6) # ax[3].plot(t_opt, V_opt, 'r', label='$Voltage$') # axarr = fig.add_subplot(1, 2, 2) # axarr.plot(sim_out.get_values('time'),sim_out.get_values('electric.battery.I_Li'), 'b') # # # axarr.plot(p['phase0.state_interp.state_col:r'], # # # p['phase0.controls:h'], 'bo', ms=4) # axarr.set_ylabel('I_Li, amps') # axarr.set_xlabel('time, s') # axarr.axes.get_xaxis().set_visible(True) import matplotlib matplotlib.use( 'agg') # <--- comment out if you want to show this plot. plt.show()
def test_two_phase_cannonball_for_docs(self): from openmdao.api import Problem, Group, IndepVarComp, DirectSolver, SqliteRecorder, \ pyOptSparseDriver from openmdao.utils.assert_utils import assert_rel_error from dymos import Phase, Trajectory, Radau, GaussLobatto from dymos.examples.cannonball.cannonball_ode import CannonballODE from dymos.examples.cannonball.size_comp import CannonballSizeComp p = Problem(model=Group()) p.driver = pyOptSparseDriver() p.driver.options['optimizer'] = 'SLSQP' p.driver.options['dynamic_simul_derivs'] = True external_params = p.model.add_subsystem('external_params', IndepVarComp()) external_params.add_output('radius', val=0.10, units='m') external_params.add_output('dens', val=7.87, units='g/cm**3') external_params.add_design_var('radius', lower=0.01, upper=0.10, ref0=0.01, ref=0.10) p.model.add_subsystem('size_comp', CannonballSizeComp()) traj = p.model.add_subsystem('traj', Trajectory()) transcription = Radau(num_segments=5, order=3, compressed=True) ascent = Phase(ode_class=CannonballODE, transcription=transcription) ascent = traj.add_phase('ascent', ascent) # All initial states except flight path angle are fixed # Final flight path angle is fixed (we will set it to zero so that the phase ends at apogee) ascent.set_time_options(fix_initial=True, duration_bounds=(1, 100), duration_ref=100, units='s') ascent.set_state_options('r', fix_initial=True, fix_final=False) ascent.set_state_options('h', fix_initial=True, fix_final=False) ascent.set_state_options('gam', fix_initial=False, fix_final=True) ascent.set_state_options('v', fix_initial=False, fix_final=False) # Limit the muzzle energy ascent.add_boundary_constraint('kinetic_energy.ke', loc='initial', units='J', upper=400000, lower=0, ref=100000, shape=(1,)) # Second Phase (descent) transcription = GaussLobatto(num_segments=5, order=3, compressed=True) descent = Phase(ode_class=CannonballODE, transcription=transcription) traj.add_phase('descent', descent) # All initial states and time are free (they will be linked to the final states of ascent. # Final altitude is fixed (we will set it to zero so that the phase ends at ground impact) descent.set_time_options(initial_bounds=(.5, 100), duration_bounds=(.5, 100), duration_ref=100) descent.set_state_options('r', fix_initial=False, fix_final=False) descent.set_state_options('h', fix_initial=False, fix_final=True) descent.set_state_options('gam', fix_initial=False, fix_final=False) descent.set_state_options('v', fix_initial=False, fix_final=False) descent.add_objective('r', loc='final', scaler=-1.0) # Add internally-managed design parameters to the trajectory. traj.add_design_parameter('CD', val=0.5, units=None, opt=False) traj.add_design_parameter('CL', val=0.0, units=None, opt=False) traj.add_design_parameter('T', val=0.0, units='N', opt=False) traj.add_design_parameter('alpha', val=0.0, units='deg', opt=False) # Add externally-provided design parameters to the trajectory. traj.add_input_parameter('mass', target_params={'ascent': 'm', 'descent': 'm'}, val=1.0) traj.add_input_parameter('S', val=0.005) # Link Phases (link time and all state variables) traj.link_phases(phases=['ascent', 'descent'], vars=['*']) # Issue Connections p.model.connect('external_params.radius', 'size_comp.radius') p.model.connect('external_params.dens', 'size_comp.dens') p.model.connect('size_comp.mass', 'traj.input_parameters:mass') p.model.connect('size_comp.S', 'traj.input_parameters:S') # Finish Problem Setup p.model.linear_solver = DirectSolver() p.driver.add_recorder(SqliteRecorder('ex_two_phase_cannonball.db')) p.setup(check=True) # Set Initial Guesses p.set_val('external_params.radius', 0.05, units='m') p.set_val('external_params.dens', 7.87, units='g/cm**3') p.set_val('traj.design_parameters:CD', 0.5) p.set_val('traj.design_parameters:CL', 0.0) p.set_val('traj.design_parameters:T', 0.0) p.set_val('traj.ascent.t_initial', 0.0) p.set_val('traj.ascent.t_duration', 10.0) p.set_val('traj.ascent.states:r', ascent.interpolate(ys=[0, 100], nodes='state_input')) p.set_val('traj.ascent.states:h', ascent.interpolate(ys=[0, 100], nodes='state_input')) p.set_val('traj.ascent.states:v', ascent.interpolate(ys=[200, 150], nodes='state_input')) p.set_val('traj.ascent.states:gam', ascent.interpolate(ys=[25, 0], nodes='state_input'), units='deg') p.set_val('traj.descent.t_initial', 10.0) p.set_val('traj.descent.t_duration', 10.0) p.set_val('traj.descent.states:r', descent.interpolate(ys=[100, 200], nodes='state_input')) p.set_val('traj.descent.states:h', descent.interpolate(ys=[100, 0], nodes='state_input')) p.set_val('traj.descent.states:v', descent.interpolate(ys=[150, 200], nodes='state_input')) p.set_val('traj.descent.states:gam', descent.interpolate(ys=[0, -45], nodes='state_input'), units='deg') p.run_driver() assert_rel_error(self, p.get_val('traj.descent.states:r')[-1], 3183.25, tolerance=1.0E-2) exp_out = traj.simulate() print('optimal radius: {0:6.4f} m '.format(p.get_val('external_params.radius', units='m')[0])) print('cannonball mass: {0:6.4f} kg '.format(p.get_val('size_comp.mass', units='kg')[0])) print('launch angle: {0:6.4f} ' 'deg '.format(p.get_val('traj.ascent.timeseries.states:gam', units='deg')[0, 0])) print('maximum range: {0:6.4f} ' 'm '.format(p.get_val('traj.descent.timeseries.states:r')[-1, 0])) fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(10, 6)) time_imp = {'ascent': p.get_val('traj.ascent.timeseries.time'), 'descent': p.get_val('traj.descent.timeseries.time')} time_exp = {'ascent': exp_out.get_val('traj.ascent.timeseries.time'), 'descent': exp_out.get_val('traj.descent.timeseries.time')} r_imp = {'ascent': p.get_val('traj.ascent.timeseries.states:r'), 'descent': p.get_val('traj.descent.timeseries.states:r')} r_exp = {'ascent': exp_out.get_val('traj.ascent.timeseries.states:r'), 'descent': exp_out.get_val('traj.descent.timeseries.states:r')} h_imp = {'ascent': p.get_val('traj.ascent.timeseries.states:h'), 'descent': p.get_val('traj.descent.timeseries.states:h')} h_exp = {'ascent': exp_out.get_val('traj.ascent.timeseries.states:h'), 'descent': exp_out.get_val('traj.descent.timeseries.states:h')} axes.plot(r_imp['ascent'], h_imp['ascent'], 'bo') axes.plot(r_imp['descent'], h_imp['descent'], 'ro') axes.plot(r_exp['ascent'], h_exp['ascent'], 'b--') axes.plot(r_exp['descent'], h_exp['descent'], 'r--') axes.set_xlabel('range (m)') axes.set_ylabel('altitude (m)') fig, axes = plt.subplots(nrows=4, ncols=1, figsize=(10, 6)) states = ['r', 'h', 'v', 'gam'] for i, state in enumerate(states): x_imp = {'ascent': p.get_val('traj.ascent.timeseries.states:{0}'.format(state)), 'descent': p.get_val('traj.descent.timeseries.states:{0}'.format(state))} x_exp = {'ascent': exp_out.get_val('traj.ascent.timeseries.states:{0}'.format(state)), 'descent': exp_out.get_val('traj.descent.timeseries.states:{0}'.format(state))} axes[i].set_ylabel(state) axes[i].plot(time_imp['ascent'], x_imp['ascent'], 'bo') axes[i].plot(time_imp['descent'], x_imp['descent'], 'ro') axes[i].plot(time_exp['ascent'], x_exp['ascent'], 'b--') axes[i].plot(time_exp['descent'], x_exp['descent'], 'r--') params = ['CL', 'CD', 'T', 'alpha', 'm', 'S'] fig, axes = plt.subplots(nrows=6, ncols=1, figsize=(12, 6)) for i, param in enumerate(params): p_imp = { 'ascent': p.get_val('traj.ascent.timeseries.traj_parameters:{0}'.format(param)), 'descent': p.get_val('traj.descent.timeseries.traj_parameters:{0}'.format(param))} p_exp = {'ascent': exp_out.get_val('traj.ascent.timeseries.' 'traj_parameters:{0}'.format(param)), 'descent': exp_out.get_val('traj.descent.timeseries.' 'traj_parameters:{0}'.format(param))} axes[i].set_ylabel(param) axes[i].plot(time_imp['ascent'], p_imp['ascent'], 'bo') axes[i].plot(time_imp['descent'], p_imp['descent'], 'ro') axes[i].plot(time_exp['ascent'], p_exp['ascent'], 'b--') axes[i].plot(time_exp['descent'], p_exp['descent'], 'r--') plt.show()