Ejemplo n.º 1
0
    def test_modify_problem(self):
        from dymos.examples.vanderpol.vanderpol_dymos import vanderpol
        from dymos.examples.vanderpol.vanderpol_dymos_plots import vanderpol_dymos_plots
        from dymos.run_problem import modify_problem, run_problem
        from scipy.interpolate import interp1d
        from numpy.testing import assert_almost_equal

        # Create the Dymos problem instance
        p = vanderpol(transcription='gauss-lobatto', num_segments=75)

        # Run the problem (simulate only)
        p.run_model()

        # simulate and record
        p.model.traj.simulate(record_file='vanderpol_simulation.sql')

        # create a new problem for restart to simulate a different command line execution
        q = vanderpol(transcription='gauss-lobatto', num_segments=75)

        # Call modify_problem with simulation restart database
        # modify_problem(q, restart='vanderpol_simulation.sql')

        # # Run the model
        run_problem(q, restart='vanderpol_simulation.sql')

        #  The solution should look like the explicit time history for the states and controls.
        DO_PLOTS = False
        if DO_PLOTS:
            vanderpol_dymos_plots(q)  # only for visual inspection and debug
        else:  # automate comparison
            s = q.model.traj.simulate()

            # get_val returns data for duplicate time points; remove them before interpolating
            tq = q.get_val('traj.phase0.timeseries.time')[:, 0]
            nodup = np.insert(tq[1:] != tq[:-1], 0, True)
            tq = tq[nodup]
            x1q = q.get_val('traj.phase0.timeseries.states:x1')[:, 0][nodup]
            x0q = q.get_val('traj.phase0.timeseries.states:x0')[:, 0][nodup]
            uq = q.get_val('traj.phase0.timeseries.controls:u')[:, 0][nodup]

            ts = s.get_val('traj.phase0.timeseries.time')[:, 0]
            nodup = np.insert(ts[1:] != ts[:-1], 0, True)
            ts = ts[nodup]
            x1s = s.get_val('traj.phase0.timeseries.states:x1')[:, 0][nodup]
            x0s = s.get_val('traj.phase0.timeseries.states:x0')[:, 0][nodup]
            us = s.get_val('traj.phase0.timeseries.controls:u')[:, 0][nodup]

            # create interpolation functions so that values can be looked up at matching time points
            fx1s = interp1d(ts, x1s, kind='cubic')
            fx0s = interp1d(ts, x0s, kind='cubic')
            fus = interp1d(ts, us, kind='cubic')

            assert_almost_equal(x1q, fx1s(tq), decimal=2)
            assert_almost_equal(x0q, fx0s(tq), decimal=2)
            assert_almost_equal(uq, fus(tq), decimal=5)
Ejemplo n.º 2
0
    def test_vanderpol_for_docs_optimize_refine(self):
        import dymos as dm
        from dymos.examples.plotting import plot_results
        from dymos.examples.vanderpol.vanderpol_dymos import vanderpol

        # Create the Dymos problem instance
        p = vanderpol(transcription='gauss-lobatto',
                      num_segments=15,
                      transcription_order=3,
                      compressed=True,
                      optimizer='SLSQP')

        # Enable grid refinement and find optimal control solution to stop oscillation
        p.model.traj.phases.phase0.set_refine_options(refine=True)
        dm.run_problem(p, refine=True, refine_iteration_limit=10)

        # check validity by using scipy.integrate.solve_ivp to integrate the solution
        exp_out = p.model.traj.simulate()

        # Display the results
        plot_results([
            ('traj.phase0.timeseries.time', 'traj.phase0.timeseries.states:x1',
             'time (s)', 'x1 (V)'),
            ('traj.phase0.timeseries.time', 'traj.phase0.timeseries.states:x0',
             'time (s)', 'x0 (V/s)'),
            ('traj.phase0.timeseries.states:x0',
             'traj.phase0.timeseries.states:x1', 'x0 vs x1', 'x0 vs x1'),
            ('traj.phase0.timeseries.time',
             'traj.phase0.timeseries.controls:u', 'time (s)', 'control u'),
        ],
                     title='Van Der Pol Optimization with Grid Refinement',
                     p_sol=p,
                     p_sim=exp_out)

        plt.show()
Ejemplo n.º 3
0
    def test_vanderpol_for_docs_simulation(self):
        from dymos.examples.plotting import plot_results
        from dymos.examples.vanderpol.vanderpol_dymos import vanderpol

        # Create the Dymos problem instance
        p = vanderpol(transcription='gauss-lobatto', num_segments=75)

        # Run the problem (simulate only)
        p.run_model()

        # check validity by using scipy.integrate.solve_ivp to integrate the solution
        exp_out = p.model.traj.simulate()

        # Display the results
        plot_results([
            ('traj.phase0.timeseries.time', 'traj.phase0.timeseries.states:x1',
             'time (s)', 'x1 (V)'),
            ('traj.phase0.timeseries.time', 'traj.phase0.timeseries.states:x0',
             'time (s)', 'x0 (V/s)'),
            ('traj.phase0.timeseries.states:x0',
             'traj.phase0.timeseries.states:x1', 'x0 vs x1', 'x0 vs x1'),
            ('traj.phase0.timeseries.time',
             'traj.phase0.timeseries.controls:u', 'time (s)', 'control u'),
        ],
                     title='Van Der Pol Simulation',
                     p_sol=p,
                     p_sim=exp_out)

        plt.show()
Ejemplo n.º 4
0
    def test_vanderpol_optimal_mpi(self):
        """to test with MPI:
           OPENMDAO_REQUIRE_MPI=1 mpirun -n 4 python
               dymos/examples/vanderpol/test/test_vanderpol.py TestVanderpolExampleMPI.test_vanderpol_optimal_mpi
           (using varying values for n should give the same answer)
        """
        p = vanderpol(transcription='gauss-lobatto',
                      num_segments=75,
                      delay=True,
                      use_pyoptsparse=True,
                      optimizer='IPOPT')
        p.run_driver()  # find optimal control solution to stop oscillation

        if SHOW_PLOTS:
            vanderpol_dymos_plots(p)

        print('Objective function minimized to',
              p.get_val('traj.phase0.states:J')[-1, ...])
        # check that ODE states (excluding J) and control are driven to near zero
        assert_almost_equal(
            p.get_val('traj.phase0.states:x0')[-1, ...], np.zeros(1))
        assert_almost_equal(
            p.get_val('traj.phase0.states:x1')[-1, ...], np.zeros(1))
        assert_almost_equal(p.get_val('traj.phase0.controls:u')[-1, ...],
                            np.zeros(1),
                            decimal=3)
Ejemplo n.º 5
0
    def test_vanderpol_simulate(self):
        # simulate only: with no control, the system oscillates
        p = vanderpol(transcription='gauss-lobatto', num_segments=75)
        p.run_model()

        if SHOW_PLOTS:
            vanderpol_dymos_plots(p)
Ejemplo n.º 6
0
    def test_vanderpol_optimal_slow(self):
        """to test with MPI:
           OPENMDAO_REQUIRE_MPI=1 mpirun -n 4 python
               dymos/examples/vanderpol/test/test_vanderpol.py TestVanderpolExample.test_vanderpol_optimal_slow
           (using varying values for n should give the same answer)
        """
        p = vanderpol(transcription='gauss-lobatto',
                      num_segments=75,
                      delay=True)
        dm.run_problem(p)  # find optimal control solution to stop oscillation

        if SHOW_PLOTS:
            vanderpol_dymos_plots(p)

        print('Objective function minimized to',
              p['traj.phases.phase0.final_conditions.states:J++'])
        # check that ODE states (excluding J) and control are driven to near zero
        assert_almost_equal(
            p['traj.phases.phase0.final_conditions.states:x0++'], np.zeros(1))
        assert_almost_equal(
            p['traj.phases.phase0.final_conditions.states:x1++'], np.zeros(1))
        assert_almost_equal(
            p['traj.phases.phase0.final_conditions.controls:u++'],
            np.zeros(1),
            decimal=3)
Ejemplo n.º 7
0
    def test_vanderpol_for_docs_optimize_refine(self):
        import dymos as dm
        from dymos.examples.vanderpol.vanderpol_dymos import vanderpol
        from openmdao.utils.assert_utils import assert_near_equal

        # Create the Dymos problem instance
        p = vanderpol(transcription='gauss-lobatto',
                      num_segments=15,
                      transcription_order=3,
                      compressed=True,
                      optimizer='SLSQP')

        # Enable grid refinement and find optimal control solution to stop oscillation
        p.model.traj.phases.phase0.set_refine_options(refine=True)

        dm.run_problem(p,
                       refine_iteration_limit=10,
                       simulate=True,
                       make_plots=True)

        assert_near_equal(p.get_val('traj.phase0.states:x0')[-1, ...], 0.0)
        assert_near_equal(p.get_val('traj.phase0.states:x1')[-1, ...], 0.0)
        assert_near_equal(p.get_val('traj.phase0.states:J')[-1, ...],
                          5.2808,
                          tolerance=1.0E-3)
        assert_near_equal(p.get_val('traj.phase0.controls:u')[-1, ...],
                          0.0,
                          tolerance=1.0E-3)
Ejemplo n.º 8
0
    def test_modify_problem(self):
        from dymos.examples.vanderpol.vanderpol_dymos import vanderpol
        from dymos.run_problem import run_problem
        from scipy.interpolate import interp1d
        from numpy.testing import assert_almost_equal

        # Create the Dymos problem instance
        p = vanderpol(transcription='gauss-lobatto', num_segments=75)

        # Run the problem (simulate only)
        p.run_model()

        # simulate and record
        p.model.traj.simulate(record_file='vanderpol_simulation.sql')

        # create a new problem for restart to simulate a different command line execution
        q = vanderpol(transcription='gauss-lobatto', num_segments=75)

        # # Run the model
        run_problem(q, restart='vanderpol_simulation.sql')

        s = q.model.traj.simulate(rtol=1.0E-9, atol=1.0E-9)

        # get_val returns data for duplicate time points; remove them before interpolating
        tq = q.get_val('traj.phase0.timeseries.time')[:, 0]
        nodup = np.insert(tq[1:] != tq[:-1], 0, True)
        tq = tq[nodup]
        x1q = q.get_val('traj.phase0.timeseries.states:x1')[:, 0][nodup]
        x0q = q.get_val('traj.phase0.timeseries.states:x0')[:, 0][nodup]
        uq = q.get_val('traj.phase0.timeseries.controls:u')[:, 0][nodup]

        ts = s.get_val('traj.phase0.timeseries.time')[:, 0]
        nodup = np.insert(ts[1:] != ts[:-1], 0, True)
        ts = ts[nodup]
        x1s = s.get_val('traj.phase0.timeseries.states:x1')[:, 0][nodup]
        x0s = s.get_val('traj.phase0.timeseries.states:x0')[:, 0][nodup]
        us = s.get_val('traj.phase0.timeseries.controls:u')[:, 0][nodup]

        # create interpolation functions so that values can be looked up at matching time points
        fx1s = interp1d(ts, x1s, kind='cubic')
        fx0s = interp1d(ts, x0s, kind='cubic')
        fus = interp1d(ts, us, kind='cubic')

        assert_almost_equal(x1q, fx1s(tq), decimal=2)
        assert_almost_equal(x0q, fx0s(tq), decimal=2)
        assert_almost_equal(uq, fus(tq), decimal=5)
Ejemplo n.º 9
0
    def test_vanderpol_for_docs_simulation(self):
        import dymos as dm
        from dymos.examples.vanderpol.vanderpol_dymos import vanderpol

        # Create the Dymos problem instance
        p = vanderpol(transcription='gauss-lobatto', num_segments=75)

        dm.run_problem(p, run_driver=False, simulate=True, make_plots=True)
Ejemplo n.º 10
0
    def test_vanderpol_optimal(self):
        p = vanderpol(transcription='gauss-lobatto', num_segments=75)
        dm.run_problem(p)  # find optimal control solution to stop oscillation

        print('Objective function minimized to', p.get_val('traj.phase0.states:J')[-1, ...])
        # check that ODE states (excluding J) and control are driven to near zero
        assert_almost_equal(p.get_val('traj.phase0.states:x0')[-1, ...], np.zeros(1))
        assert_almost_equal(p.get_val('traj.phase0.states:x1')[-1, ...], np.zeros(1))
        assert_almost_equal(p.get_val('traj.phase0.controls:u')[-1, ...], np.zeros(1), decimal=3)
Ejemplo n.º 11
0
    def test_vanderpol_optimal_grid_refinement(self):
        # enabling grid refinement gives a faster and better solution with fewer segments
        p = vanderpol(transcription='gauss-lobatto', num_segments=15)

        p.model.traj.phases.phase0.set_refine_options(refine=True)
        dm.run_problem(p, refine_iteration_limit=10)  # enable grid refinement and find optimal solution

        print('Objective function minimized to', p.get_val('traj.phase0.timeseries.states:J')[-1, ...])
        # check that ODE states (excluding J) and control are driven to near zero
        assert_almost_equal(p.get_val('traj.phase0.timeseries.states:x0')[-1, ...], np.zeros(1))
        assert_almost_equal(p.get_val('traj.phase0.timeseries.states:x1')[-1, ...], np.zeros(1))
        assert_almost_equal(p.get_val('traj.phase0.timeseries.controls:u')[-1, ...], np.zeros(1), decimal=4)
Ejemplo n.º 12
0
    def test_vanderpol_simulate_true(self):
        # simulate true
        p = vanderpol(transcription='radau-ps',
                      num_segments=30,
                      transcription_order=3,
                      compressed=True,
                      optimizer='SLSQP',
                      delay=0.005,
                      distrib=True,
                      use_pyoptsparse=True)

        dm.run_problem(p, run_driver=True, simulate=True)
Ejemplo n.º 13
0
    def test_vanderpol_for_docs_optimize(self):
        import dymos as dm
        from dymos.examples.vanderpol.vanderpol_dymos import vanderpol

        # Create the Dymos problem instance
        p = vanderpol(transcription='gauss-lobatto',
                      num_segments=75,
                      transcription_order=3,
                      compressed=True,
                      optimizer='SLSQP')

        dm.run_problem(p, simulate=True, make_plots=True)
Ejemplo n.º 14
0
    def test_vanderpol_optimal(self):
        p = vanderpol(transcription='gauss-lobatto', num_segments=75)
        dm.run_problem(p)  # find optimal control solution to stop oscillation

        if SHOW_PLOTS:
            vanderpol_dymos_plots(p)

        print('Objective function minimized to',
              p['traj.phases.phase0.final_conditions.states:J++'])
        # check that ODE states (excluding J) and control are driven to near zero
        assert_almost_equal(
            p['traj.phases.phase0.final_conditions.states:x0++'], np.zeros(1))
        assert_almost_equal(
            p['traj.phases.phase0.final_conditions.states:x1++'], np.zeros(1))
        assert_almost_equal(
            p['traj.phases.phase0.final_conditions.controls:u++'],
            np.zeros(1),
            decimal=3)
Ejemplo n.º 15
0
    def test_vanderpol_optimal_grid_refinement(self):
        # enabling grid refinement gives a faster and better solution with fewer segments
        p = vanderpol(transcription='gauss-lobatto', num_segments=15)

        p.model.traj.phases.phase0.set_refine_options(refine=True)
        dm.run_problem(p, refine=True, refine_iteration_limit=10
                       )  # enable grid refinement and find optimal solution

        if SHOW_PLOTS:
            vanderpol_dymos_plots(p)

        print('Objective function minimized to',
              p['traj.phases.phase0.final_conditions.states:J++'])
        # check that ODE states (excluding J) and control are driven to near zero
        assert_almost_equal(
            p['traj.phases.phase0.final_conditions.states:x0++'], np.zeros(1))
        assert_almost_equal(
            p['traj.phases.phase0.final_conditions.states:x1++'], np.zeros(1))
        assert_almost_equal(
            p['traj.phases.phase0.final_conditions.controls:u++'],
            np.zeros(1),
            decimal=4)