OpenMDAO / dymos

Open Source Optimization of Dynamic Multidisciplinary Systems
Apache License 2.0
212 stars 67 forks source link

input_initial breaks approximated total jacobeans #1077

Open andrewellis55 opened 5 months ago

andrewellis55 commented 5 months ago

Description

The check magnitudes are all 0's for the states with input_initial=True

Example

import openmdao.api as om
import dymos as dm
import matplotlib.pyplot as plt

from dymos.examples.oscillator.oscillator_ode import OscillatorODE

# Instantiate an OpenMDAO Problem instance.
prob = om.Problem()
idv: om.IndepVarComp = prob.model.add_subsystem('idv', om.IndepVarComp())
idv.add_output('obj')
idv.add_objective('obj')

# Instantiate a Dymos Trajectory and add it to the Problem model.
traj = dm.Trajectory()
prob.model.add_subsystem('traj', traj)

# Instantiate a Phase and add it to the Trajectory.
# Here the transcription is necessary but not particularly relevant.
phase = dm.Phase(ode_class=OscillatorODE, transcription=dm.Radau(num_segments=4))
traj.add_phase('phase0', phase)
phase.set_time_options(fix_initial=True, fix_duration=True)

# Tell Dymos the states to be propagated using the given ODE.
phase.add_state('v', rate_source='v_dot', targets=['v'], units='m/s', input_initial=True)
phase.add_state('x', rate_source='v', targets=['x'], units='m')

# The spring constant, damping coefficient, and mass are inputs to the system
# that are constant throughout the phase.
phase.add_parameter('k', units='N/m', targets=['k'])
phase.add_parameter('c', units='N*s/m', targets=['c'])
phase.add_parameter('m', units='kg', targets=['m'])

# Setup the OpenMDAO problem
prob.setup()

# Assign values to the times and states
prob.set_val('traj.phase0.t_initial', 0.0)
prob.set_val('traj.phase0.t_duration', 15.0)

prob.set_val('traj.phase0.states:x', 10.0)
prob.set_val('traj.phase0.states:v', 0.0)

prob.set_val('traj.phase0.parameters:k', 1.0)
prob.set_val('traj.phase0.parameters:c', 0.5)
prob.set_val('traj.phase0.parameters:m', 1.0)

# Perform a single execution of the model (executing the model is required before simulation).
prob.driver = om.ScipyOptimizeDriver()
prob.run_driver()

prob.check_totals(show_only_incorrect=True)

Output:

Optimization terminated successfully    (Exit mode 0)
            Current function value: 1.0
            Iterations: 1
            Function evaluations: 2
            Gradient evaluations: 1
Optimization Complete
-----------------------------------

** Only writing information about incorrect total derivatives **

-----------------
Total Derivatives
-----------------

+------------------------------------------------+------------------------+-------------+-------------+-------------+-------------+--------------------+
| of '<variable>'                                | wrt '<variable>'       |   calc mag. |  check mag. |  a(cal-chk) |  r(cal-chk) | error desc         |
+================================================+========================+=============+=============+=============+=============+====================+
| 'traj.phase0.collocation_constraint.defects:v' | 'traj.phase0.states:v' |  1.1909e+01 |  0.0000e+00 |  1.1909e+01 |  1.0000e+00 |  >ABS_TOL >REL_TOL |
+------------------------------------------------+------------------------+-------------+-------------+-------------+-------------+--------------------+
| 'traj.phase0.collocation_constraint.defects:v' | 'traj.phase0.states:x' |  6.4952e+00 |  0.0000e+00 |  6.4952e+00 |  1.0000e+00 |  >ABS_TOL >REL_TOL |
+------------------------------------------------+------------------------+-------------+-------------+-------------+-------------+--------------------+
| 'traj.phase0.collocation_constraint.defects:x' | 'traj.phase0.states:v' |  6.2187e+00 |  0.0000e+00 |  6.2187e+00 |  1.0000e+00 |  >ABS_TOL >REL_TOL |
+------------------------------------------------+------------------------+-------------+-------------+-------------+-------------+--------------------+
| 'traj.phase0.collocation_constraint.defects:x' | 'traj.phase0.states:x' |  1.2952e+01 |  0.0000e+00 |  1.2952e+01 |  1.0000e+00 |  >ABS_TOL >REL_TOL |
+------------------------------------------------+------------------------+-------------+-------------+-------------+-------------+--------------------+

Dymos Version

1.10.0

Relevant environment information

openmdao==3.33.0