Skip to content

Dymos Command Line Interface

Dymos has several command line argumants that can make it easier to repeatedly run a script with different options.

You can see all the possible Dymos command line options by running dymos --help:

dymos --help
usage: dymos [-h] [-s] [-n] [-t SOLUTION] [-r] [-l REFINE_LIMIT] [-o SOLUTION_RECORD_FILE] [-i SIMULATION_RECORD_FILE] [-p] [-e PLOT_DIR] script

Dymos Command Line Tool

positional arguments:
  script                Python script that creates a Dymos problem to run

optional arguments:
  -h, --help            show this help message and exit
  -s, --simulate        If given, perform simulation after solving the problem.
  -n, --no_solve        If given, do not run the driver on the problem (simulate only)
  -t SOLUTION, --solution SOLUTION
                        A previous solution record file (or explicit simulation record file) from which the initial guess should be loaded. (default: None)
  -r, --reset_grid      If given, reset the grid to the specs given in the problem definition instead of the grid associated with the loaded solution.
  -l REFINE_LIMIT, --refine_limit REFINE_LIMIT
                        The number of passes through the grid refinement algorithm to use. (default: 0)
  -o SOLUTION_RECORD_FILE, --solution_record_file SOLUTION_RECORD_FILE
                        Set the name of the case recorder file for solution results. (default: dymos_solution.db)
  -i SIMULATION_RECORD_FILE, --simulation_record_file SIMULATION_RECORD_FILE
                        Set the name of the case recorder file for simulation results. (default: dymos_simulation.db)
  -p, --make_plots      If given, automatically generate plots of all timeseries outputs.
  -e PLOT_DIR, --plot_dir PLOT_DIR
                        Set the name of the directory to store the timeseries plots. (default: plots)
The only non-optional argument to a dymos command line invocation is the name of a script that creates an instance of a Dymos Problem. For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import matplotlib
import matplotlib.pyplot as plt

import openmdao.api as om

import dymos as dm
from dymos.examples.brachistochrone.brachistochrone_ode import BrachistochroneODE

SHOW_PLOTS = True


def brachistochrone_min_time(transcription='gauss-lobatto', num_segments=8, transcription_order=3,
                             compressed=True, optimizer='SLSQP'):
    p = om.Problem(model=om.Group())

    p.driver = om.pyOptSparseDriver()
    p.driver.options['optimizer'] = optimizer
    p.driver.declare_coloring()

    if transcription == 'gauss-lobatto':
        t = dm.GaussLobatto(num_segments=num_segments,
                            order=transcription_order,
                            compressed=compressed)
    elif transcription == 'radau-ps':
        t = dm.Radau(num_segments=num_segments,
                     order=transcription_order,
                     compressed=compressed)

    traj = dm.Trajectory()
    phase = dm.Phase(ode_class=BrachistochroneODE, transcription=t)
    traj.add_phase('phase0', phase)

    p.model.add_subsystem('traj0', traj)

    phase.set_time_options(fix_initial=True, duration_bounds=(.5, 10))

    phase.add_state('x', fix_initial=True, fix_final=False)
    phase.add_state('y', fix_initial=True, fix_final=False)
    phase.add_state('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_parameter('g', units='m/s**2', val=9.80665)

    phase.add_timeseries('timeseries2',
                         transcription=dm.Radau(num_segments=num_segments*5,
                                                order=transcription_order,
                                                compressed=compressed),
                         subset='control_input')

    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 = om.DirectSolver()
    p.setup(check=['unconnected_inputs'])

    p['traj0.phase0.t_initial'] = 0.0
    p['traj0.phase0.t_duration'] = 2.0

    p['traj0.phase0.states:x'] = phase.interpolate(ys=[0, 10], nodes='state_input')
    p['traj0.phase0.states:y'] = phase.interpolate(ys=[10, 5], nodes='state_input')
    p['traj0.phase0.states:v'] = phase.interpolate(ys=[0, 9.9], nodes='state_input')
    p['traj0.phase0.controls:theta'] = phase.interpolate(ys=[5, 100], nodes='control_input')
    p['traj0.phase0.parameters:g'] = 9.80665

    p.final_setup()
    return p

if __name__ == '__main__':
    p = brachistochrone_min_time(transcription='radau-ps', num_segments=8,
                                 transcription_order=3, compressed=True,
                                 optimizer='SLSQP')

The Dymos command line handler recognizes the Dymos Problem by the call to final_setup, so the script that creates a problem for command line execution should call that function last.

Solving for the optimal trajectory

The default behavior for calling a script with the dymos command line is to solve the optimal control problem, equivalent to calling the problem.run_driver function. For example:

dymos dymos/utils/test/brachistochrone_for_command_line.py

Dymos will run the optimizer to solve the problem created by the script and show the results.

Loading an existing trajectory as an initial guess

You will see a message before the run about a recorder being added. This is a database file that by default is called named dymos_solution.db but can be set using the --solution_record_file option. The file is automaticaly created in your current working directory. It will allow restarting work on the Dymos problem from where it left off. The -t or --solution command line option is used to tell dymos to restart from the specified recorded solution:

dymos -t dymos_solution.db dymos/utils/test/brachistochrone_for_command_line.py

If you run the two commands above, the second command will report that the solution was found in one iteration (because it started from a converged solution that was already found by the first command). This option is useful for combining with other command line options to continue simulating or refining a solution from a previous command line involcation.

The name of the automatially created recorder database is dymos_solution.db unless set by the optional argument --solution_record_file option. If you restart using a database with that same name, the database being read will be renamed to old_ followed by the name of the recorder database before the run to avoid overwriting the input.

Simulating a trajectory

There are two command line options related to simulating a trajectory (propaging the ODEs). The options are equivalent to calling problem.run_model in addition to or instead of problem.run_driver.

The -s or --simulate command line option runs the simulation after the optimal control problem is solved. For example:

dymos -s dymos/utils/test/brachistochrone_for_command_line.py

The -n or --no_solve command line option runs the simulation but skips running the driver to solve the optimal control problem. For example:

dymos -n dymos/utils/test/brachistochrone_for_command_line.py

Using grid refinement

There are two command line options related to grid refinement.

The -l or --refine_limit command line option is used to set the number of passes through the grid refinement algorithm. It defaults to zero, which does no grid refinement. Setting it to a positive integer will enable grid refinement. For example:

dymos -l 10 dymos/utils/test/brachistochrone_for_command_line.py

The -r or --reset_grid command line option (not currently implemented) resets the grid to the specifications given in the problem definition, ignoring any grid that would have been loaded from a restart database. It is called like this:

dymos -r -t dymos_solution.db dymos/utils/test/brachistochrone_for_command_line.py

Setting file paths to database recorder files

The -o or --solution_record_file optional argument can be used to set the name of database recorder file used for recording the solution results.

The -i or --simulation_record_file optional argumentcan be used to set the name of database recorder file used for recording the simulation results.

Plotting timeseries

The -p or --make_plots optional argument is used to indicate that plot files should be made of all timeseries variables. The files will by default be put into the plots directory. The directory can be changed by using the optional argument -e or --plot_dir. The plots will be saved and not displayed. The user has to manually open the plot files for them to be displayed.