API  4.5.1
For C++ developers
OpenSim::MocoTrajectory Class Reference

The values of the variables in an optimal control problem. More...

+ Inheritance diagram for OpenSim::MocoTrajectory:

Public Member Functions

 MocoTrajectory ()=default
 
 MocoTrajectory (std::vector< std::string > state_names, std::vector< std::string > control_names, std::vector< std::string > multiplier_names, std::vector< std::string > parameter_names)
 Create a trajectory with no data. More...
 
 MocoTrajectory (std::vector< std::string > state_names, std::vector< std::string > control_names, std::vector< std::string > multiplier_names, std::vector< std::string > derivative_names, std::vector< std::string > parameter_names)
 Create a trajectory (including columns for derivatives) with no data. More...
 
 MocoTrajectory (std::vector< std::string > state_names, std::vector< std::string > control_names, std::vector< std::string > input_control_names, std::vector< std::string > multiplier_names, std::vector< std::string > derivative_names, std::vector< std::string > parameter_names)
 
 MocoTrajectory (const SimTK::Vector &time, std::vector< std::string > state_names, std::vector< std::string > control_names, std::vector< std::string > multiplier_names, std::vector< std::string > parameter_names, const SimTK::Matrix &statesTrajectory, const SimTK::Matrix &controlsTrajectory, const SimTK::Matrix &multipliersTrajectory, const SimTK::RowVector &parameters)
 
 MocoTrajectory (const SimTK::Vector &time, std::vector< std::string > state_names, std::vector< std::string > control_names, std::vector< std::string > multiplier_names, std::vector< std::string > derivative_names, std::vector< std::string > parameter_names, const SimTK::Matrix &statesTrajectory, const SimTK::Matrix &controlsTrajectory, const SimTK::Matrix &multipliersTrajectory, const SimTK::Matrix &derivativesTrajectory, const SimTK::RowVector &parameters)
 This constructor is for use with the implicit dynamics mode, and allows specifying a derivativesTrajectory. More...
 
 MocoTrajectory (const SimTK::Vector &time, std::vector< std::string > state_names, std::vector< std::string > control_names, std::vector< std::string > input_control_names, std::vector< std::string > multiplier_names, std::vector< std::string > derivative_names, std::vector< std::string > parameter_names, const SimTK::Matrix &statesTrajectory, const SimTK::Matrix &controlsTrajectory, const SimTK::Matrix &inputControlsTrajectory, const SimTK::Matrix &multipliersTrajectory, const SimTK::Matrix &derivativesTrajectory, const SimTK::RowVector &parameters)
 
 MocoTrajectory (const SimTK::Vector &time, const std::map< std::string, NamesAndData< SimTK::Matrix >> &continuousVars, const NamesAndData< SimTK::RowVector > &parameters={})
 
 MocoTrajectory (const std::string &filepath)
 Read a MocoTrajectory from an STO file (see STOFileAdapter). More...
 
virtual ~MocoTrajectory ()=default
 
virtual MocoTrajectoryclone () const
 Returns a dynamically-allocated copy of this trajectory. More...
 
bool empty () const
 
bool hasCoordinateStates () const
 
Change the length of the trajectory
void setNumTimes (int numTimes)
 Resize the time vector and the time dimension of the states, controls, multipliers, and derivatives trajectories, and set all times, states, controls, multipliers, and derivatives to NaN. More...
 
double resampleWithNumTimes (int numTimes)
 Uniformly resample (interpolate) the trajectory so that it retains the same initial and final times but now has the provided number of time points. More...
 
double resampleWithInterval (double desiredTimeInterval)
 Uniformly resample (interpolate) the trajectory to try to achieve the provided time interval between mesh points, while preserving the initial and final times. More...
 
double resampleWithFrequency (double desiredNumTimePointsPerSecond)
 Uniformly resample (interpolate) the trajectory to try to achieve the provided frequency of time points per second of the trajectory, while preserving the initial and final times. More...
 
void resample (SimTK::Vector newTime)
 Resample (interpolate) the data in this trajectory at the provided times. More...
 
Set the data
void setTime (const SimTK::Vector &time)
 Set the time vector. More...
 
void setState (const std::string &name, const SimTK::Vector &trajectory)
 Set the value of a single state variable across time. More...
 
void setControl (const std::string &name, const SimTK::Vector &trajectory)
 Set the value of a single control variable across time. More...
 
void setInputControl (const std::string &name, const SimTK::Vector &trajectory)
 Set the value of a single Input control variable across time. More...
 
void setMultiplier (const std::string &name, const SimTK::Vector &trajectory)
 Set the value of a single Lagrange multiplier variable across time. More...
 
void setDerivative (const std::string &name, const SimTK::Vector &trajectory)
 Set the value of a single state derivative variable across time. More...
 
void setParameter (const std::string &name, const SimTK::Real &value)
 Set the value of a single parameter variable. More...
 
void setTime (std::initializer_list< double > time)
 Set the time vector. More...
 
void setState (const std::string &name, std::initializer_list< double > trajectory)
 Set the value of a single state variable across time. More...
 
void setControl (const std::string &name, std::initializer_list< double > trajectory)
 Set the value of a single control variable across time. More...
 
void setInputControl (const std::string &name, std::initializer_list< double > trajectory)
 Set the value of a single Input control variable across time. More...
 
void setMultiplier (const std::string &name, std::initializer_list< double > trajectory)
 Set the value of a single Lagrange multiplier variable across time. More...
 
void setDerivative (const std::string &name, std::initializer_list< double > trajectory)
 Set the value of a single state derivative variable across time. More...
 
void setStatesTrajectory (const TimeSeriesTable &states, bool allowMissingColumns=false, bool allowExtraColumns=false)
 Set the states trajectory. More...
 
void insertStatesTrajectory (const TimeSeriesTable &subsetOfStates, bool overwrite=false)
 Add additional state columns. More...
 
void insertControlsTrajectory (const TimeSeriesTable &subsetOfControls, bool overwrite=false)
 Add additional control columns. More...
 
void generateSpeedsFromValues ()
 Compute coordinate speeds based on coordinate position values and append to the trajectory. More...
 
void generateAccelerationsFromValues ()
 Compute coordinate accelerations based on coordinate position values and append to the trajectory. More...
 
void generateAccelerationsFromSpeeds ()
 Compute coordinate accelerations based on coordinate speeds and append to the trajectory. More...
 
void generateControlsFromModelControllers (Model model, bool overwrite=false)
 Compute model controls based on any Controllers in the model and append to the trajectory. More...
 
void trimToIndices (int newStartIndex, int newFinalIndex)
 Trim the trajectory to include the rows starting at newStartIndex and and ending at newFinalIndex. More...
 
Accessors
int getNumTimes () const
 
const SimTK::Vector & getTime () const
 
double getInitialTime () const
 The first time in the time vector. More...
 
double getFinalTime () const
 The last time in the time vector. More...
 
int getNumStates () const
 
int getNumControls () const
 
int getNumInputControls () const
 
int getNumMultipliers () const
 
int getNumDerivatives () const
 
int getNumValues () const
 
int getNumSpeeds () const
 
int getNumMultibodyStates () const
 
int getNumAuxiliaryStates () const
 
int getNumAccelerations () const
 
int getNumDerivativesWithoutAccelerations () const
 
int getNumParameters () const
 
const std::vector< std::string > & getStateNames () const
 
const std::vector< std::string > & getControlNames () const
 
const std::vector< std::string > & getInputControlNames () const
 
const std::vector< std::string > & getMultiplierNames () const
 
const std::vector< std::string > & getDerivativeNames () const
 
std::vector< std::string > getValueNames () const
 
std::vector< std::string > getSpeedNames () const
 
std::vector< std::string > getMultibodyStateNames () const
 
std::vector< std::string > getAuxiliaryStateNames () const
 
std::vector< std::string > getAccelerationNames () const
 
std::vector< std::string > getDerivativeNamesWithoutAccelerations () const
 
const std::vector< std::string > & getParameterNames () const
 
SimTK::VectorView_< double > getState (const std::string &name) const
 
SimTK::VectorView_< double > getControl (const std::string &name) const
 
SimTK::VectorView_< double > getInputControl (const std::string &name) const
 
SimTK::VectorView_< double > getMultiplier (const std::string &name) const
 
SimTK::VectorView_< double > getDerivative (const std::string &name) const
 
const SimTK::Real & getParameter (const std::string &name) const
 
const SimTK::Matrix & getStatesTrajectory () const
 
const SimTK::Matrix & getControlsTrajectory () const
 
const SimTK::Matrix & getInputControlsTrajectory () const
 
const SimTK::Matrix & getMultipliersTrajectory () const
 
const SimTK::Matrix & getDerivativesTrajectory () const
 
SimTK::Matrix getValuesTrajectory () const
 
SimTK::Matrix getSpeedsTrajectory () const
 
SimTK::Matrix getMultibodyStatesTrajectory () const
 
SimTK::Matrix getAuxiliaryStatesTrajectory () const
 
SimTK::Matrix getAccelerationsTrajectory () const
 
SimTK::Matrix getDerivativesWithoutAccelerationsTrajectory () const
 
const SimTK::RowVector & getParameters () const
 
Comparisons
bool isCompatible (const MocoProblemRep &, bool requireAccelerations=false, bool throwOnError=false) const
 Do the state, control, multiplier, derivative, and parameter names in this trajectory match those in the problem? This may not catch all possible incompatibilities. More...
 
bool isNumericallyEqual (const MocoTrajectory &other, double tol=SimTK::NTraits< SimTK::Real >::getDefaultTolerance()) const
 Check if this trajectory is numerically equal to another trajectory. More...
 
double compareContinuousVariablesRMS (const MocoTrajectory &other, std::map< std::string, std::vector< std::string >> columnsToUse={}) const
 Compute the root-mean-square error between the continuous variables of this trajectory and another. More...
 
double compareContinuousVariablesRMSPattern (const MocoTrajectory &other, std::string columnType, std::string pattern) const
 This is an alternative interface for compareContinuousVariablesRMS() that uses regular expression patterns to select columns. More...
 
double compareParametersRMS (const MocoTrajectory &other, std::vector< std::string > parameterNames={}) const
 Compute the root-mean-square error between the parameters in this trajectory and another. More...
 
Convert to other formats
void write (const std::string &filepath) const
 Save the trajectory to a STO file. Use the ."sto" file extension. More...
 
TimeSeriesTable exportToStatesTable () const
 This table can be saved as a Storage file that can be used in the OpenSim GUI to visualize a motion, or as input to OpenSim's conventional tools (e.g., AnalyzeTool). More...
 
TimeSeriesTable exportToControlsTable () const
 Export the controls trajectory to a TimeSeriesTable. More...
 
TimeSeriesTable exportToInputControlsTable () const
 Export the Input controls trajectory to a TimeSeriesTable. More...
 
TimeSeriesTable exportToMultipliersTable () const
 Export the multipliers trajectory to a TimeSeriesTable. More...
 
TimeSeriesTable exportToDerivativesTable () const
 Export the derivatives trajectory to a TimeSeriesTable. More...
 
TimeSeriesTable exportToValuesTable () const
 Export the coordinate values from the states trajectory to a TimeSeriesTable. More...
 
TimeSeriesTable exportToSpeedsTable () const
 Export the coordinate speeds from the states trajectory to a TimeSeriesTable. More...
 
TimeSeriesTable exportToAccelerationsTable () const
 Export the coordinate accelerations from the derivatives trajectory to a TimeSeriesTable. More...
 
TimeSeriesTable exportToDerivativesWithoutAccelerationsTable () const
 Export the derivatives trajectory without coordinate accelerations to a TimeSeriesTable. More...
 
StatesTrajectory exportToStatesTrajectory (const MocoProblem &) const
 Controls are not carried over to the StatesTrajectory. More...
 
StatesTrajectory exportToStatesTrajectory (const Model &) const
 This is similar to the above function but requires only a model, not a MocoProblem. More...
 
Modify the data
void randomizeReplace (const SimTK::Random &randGen=SimTK::Random::Uniform(-0.1, 0.1))
 Randomize all data except time using the provided random number generator. More...
 
void randomizeAdd (const SimTK::Random &randGen=SimTK::Random::Uniform(-0.1, 0.1))
 Randomize all data except time using the provided random number generator. More...
 

Public Types

template<typename T >
using NamesAndData = std::pair< std::vector< std::string >, T >
 This constructor allows you to control which data you provide for the trajectory. More...
 

Static Public Member Functions

Convert from other formats
static MocoTrajectory createFromStatesControlsTables (const MocoProblemRep &, const TimeSeriesTable &statesTrajectory, const TimeSeriesTable &controlsTrajectory)
 (Experimental) Create a trajectory from a states trajectory and controls trajectory (i.e, from Manager::getStatesTable() and Model::getControlsTable()). More...
 

Protected Member Functions

void setSealed (bool sealed)
 
bool isSealed () const
 
void ensureUnsealed () const
 

Detailed Description

The values of the variables in an optimal control problem.

This can be used for specifying an initial guess, or holding the solution returned by a solver.

A MocoTrajectory can be written to and read from an STO (".sto") file. The file format is comprised of a file header followed by a row of column names and the stored data. The file header contains the number of states, controls, Input controls, Lagrange multipliers (for kinematic constraints), derivatives (non-zero if the dynamics mode is implicit), slacks (for special solver implementations), and parameters (order does not matter). Order does matter for the column names and corresponding data columns. The columns must follow this order: time, states, controls, Input controls, multipliers, derivatives, slacks, parameters.

Note
Slack columns may contain real number or NaN values, depending on their use. For example, values for velocity correction variables used in problems with model kinematic constraints are defined only at the midpoint of a Hermite- Simpson mesh interval. The non-midpoint variables are returned as NaN in the slack variable data structure.
For parameter columns, the value of the parameter is stored in the first row of the column, while the rest of the rows are filled with NaNs.

Sample file:

num_controls=<number-of-control-variables>
num_derivatives=<number-of-derivative-variables>
num_input_controls=<number-of-input-control-variables>
num_multipliers=<number-of-multiplier-variables>
num_parameters=<number-of-parameter-variables>
num_slacks=<number-of-slack-variables>
num_states=<number-of-state-variables>
time,<state-0-name>,...,<control-0-name>,...,<input-control-0-name>,..., \
        <multiplier-0-name>,...,<derivative-0-name>,...,<slack-0-name>,..., \
        <parameter-0-name>,...
<#>,<#>,...,<#>,...,<#>,...,<#>,...,<#-or-NaN>,...,<#>  ,...
<#>,<#>,...,<#>,...,<#>,...,<#>,...,<#-or-NaN>,...,<NaN>,...
 : , : ,..., : ,..., : ,..., : ,...,    :     ,...,  :  ,...
<#>,<#>,...,<#>,...,<#>,...,<#>,...,<#-or-NaN>,...,<NaN>,...

Column labels starting with "lambda" are Lagrange multipliers, and columns starting with "gamma" are slack variables (probably velocity corrections at certain collocation points).

Matlab and Python
Many of the functions in this class have variants ending with "Mat" that provide convenient access to the data directly in Matlab or Python (NumPy). In Python, the constructors can also accept NumPy matrices in addition to arguments of type SimTK::Matrix.
trajectory.getStateMat("<state-name>")
trajectory.getStatesTrajectoryMat()
Implicit dynamics model
If the solver uses an implicit dynamics mode, then there are "control" variables ("adjunct" variables in tropter's terminology) for the generalized accelerations. These are stored in the trajectory as derivative variables.
Sealed trajectories
If the trajectory is obtained as the failed solution to a problem, the trajectory will be sealed (MocoTrajectory::isSealed()), which means that you cannot do anything with the trajectory (read, edit, or write) until you call MocoTrajectory::unseal(). The sealing forces you to acknowledge that the solver failed.
Convenience accessors
The accessors getValuesTrajectory() and getSpeedsTrajectory() (and related methods (e.g., getValueNames()) are available to obtain only the coordinate values or coordinate speeds, which are part of the states trajectory. Note that these accessors create fresh data structures from the existing member variables, so repeated calls should be avoided. Similarly, the accessors getAccelerationsTrajectory() and getDerivativesWithoutAccelerationsTrajectory() are available to access subcomponents of the derivatives trajectory.
Examples:
exampleMocoCustomEffortGoal.cpp, exampleMocoInverse.cpp, and exampleSlidingMassAdvanced.cpp.

Member Typedef Documentation

◆ NamesAndData

template<typename T >
using OpenSim::MocoTrajectory::NamesAndData = std::pair<std::vector<std::string>, T>

This constructor allows you to control which data you provide for the trajectory.

The possible keys for continuousVars are "states", "controls", "multipliers", and "derivatives". The names and data are grouped together, and you can leave out any of the keys.

Constructor & Destructor Documentation

◆ MocoTrajectory() [1/9]

OpenSim::MocoTrajectory::MocoTrajectory ( )
default

◆ MocoTrajectory() [2/9]

OpenSim::MocoTrajectory::MocoTrajectory ( std::vector< std::string >  state_names,
std::vector< std::string >  control_names,
std::vector< std::string >  multiplier_names,
std::vector< std::string >  parameter_names 
)

Create a trajectory with no data.

To add data, use setNumTimes(), setTime(), and the other setters.

◆ MocoTrajectory() [3/9]

OpenSim::MocoTrajectory::MocoTrajectory ( std::vector< std::string >  state_names,
std::vector< std::string >  control_names,
std::vector< std::string >  multiplier_names,
std::vector< std::string >  derivative_names,
std::vector< std::string >  parameter_names 
)

Create a trajectory (including columns for derivatives) with no data.

To add data, use setNumTimes(), setTime(), and the other setters.

◆ MocoTrajectory() [4/9]

OpenSim::MocoTrajectory::MocoTrajectory ( std::vector< std::string >  state_names,
std::vector< std::string >  control_names,
std::vector< std::string >  input_control_names,
std::vector< std::string >  multiplier_names,
std::vector< std::string >  derivative_names,
std::vector< std::string >  parameter_names 
)

◆ MocoTrajectory() [5/9]

OpenSim::MocoTrajectory::MocoTrajectory ( const SimTK::Vector &  time,
std::vector< std::string >  state_names,
std::vector< std::string >  control_names,
std::vector< std::string >  multiplier_names,
std::vector< std::string >  parameter_names,
const SimTK::Matrix &  statesTrajectory,
const SimTK::Matrix &  controlsTrajectory,
const SimTK::Matrix &  multipliersTrajectory,
const SimTK::RowVector &  parameters 
)

◆ MocoTrajectory() [6/9]

OpenSim::MocoTrajectory::MocoTrajectory ( const SimTK::Vector &  time,
std::vector< std::string >  state_names,
std::vector< std::string >  control_names,
std::vector< std::string >  multiplier_names,
std::vector< std::string >  derivative_names,
std::vector< std::string >  parameter_names,
const SimTK::Matrix &  statesTrajectory,
const SimTK::Matrix &  controlsTrajectory,
const SimTK::Matrix &  multipliersTrajectory,
const SimTK::Matrix &  derivativesTrajectory,
const SimTK::RowVector &  parameters 
)

This constructor is for use with the implicit dynamics mode, and allows specifying a derivativesTrajectory.

◆ MocoTrajectory() [7/9]

OpenSim::MocoTrajectory::MocoTrajectory ( const SimTK::Vector &  time,
std::vector< std::string >  state_names,
std::vector< std::string >  control_names,
std::vector< std::string >  input_control_names,
std::vector< std::string >  multiplier_names,
std::vector< std::string >  derivative_names,
std::vector< std::string >  parameter_names,
const SimTK::Matrix &  statesTrajectory,
const SimTK::Matrix &  controlsTrajectory,
const SimTK::Matrix &  inputControlsTrajectory,
const SimTK::Matrix &  multipliersTrajectory,
const SimTK::Matrix &  derivativesTrajectory,
const SimTK::RowVector &  parameters 
)

◆ MocoTrajectory() [8/9]

OpenSim::MocoTrajectory::MocoTrajectory ( const SimTK::Vector &  time,
const std::map< std::string, NamesAndData< SimTK::Matrix >> &  continuousVars,
const NamesAndData< SimTK::RowVector > &  parameters = {} 
)

◆ MocoTrajectory() [9/9]

OpenSim::MocoTrajectory::MocoTrajectory ( const std::string &  filepath)
explicit

Read a MocoTrajectory from an STO file (see STOFileAdapter).

See output of write() for the correct format.

◆ ~MocoTrajectory()

virtual OpenSim::MocoTrajectory::~MocoTrajectory ( )
virtualdefault

Member Function Documentation

◆ clone()

virtual MocoTrajectory* OpenSim::MocoTrajectory::clone ( ) const
inlinevirtual

Returns a dynamically-allocated copy of this trajectory.

You must manage the memory for return value.

Note
This works even if the trajectory is sealed.

Reimplemented in OpenSim::MocoSolution.

◆ compareContinuousVariablesRMS()

double OpenSim::MocoTrajectory::compareContinuousVariablesRMS ( const MocoTrajectory other,
std::map< std::string, std::vector< std::string >>  columnsToUse = {} 
) const

Compute the root-mean-square error between the continuous variables of this trajectory and another.

The RMS is computed by numerically integrating the sum of squared error across states, controls, Input controls, Lagrange multipliers, and derivatives and dividing by the number of columns and the time duration. The calculation can be expressed as follows:

\[ \epsilon_{\textrm{RMS}} = \sqrt{\frac{1}{N(t_f - t_i)} \int_{t_i}^{t_f} \left( \sum_{ i \in \textrm{states} } \epsilon_i(t)^2 + \sum_{ i \in \textrm{controls} } \epsilon_i(t)^2 + \sum_{ i \in \textrm{mult} } \epsilon_i(t)^2 + \sum_{ i \in \textrm{deriv} } \epsilon_i(t)^2 \right) dt }, \]

where \( N \) is the number of columns, \( t_i \) is the minimum of the two initial times, \( t_f \) is the maximum of the two final times, and \( \epsilon \) indicates an error.

When the two trajectories do not cover the same time range, we assume values of 0 for the trajectory with "missing" time (we do NOT assume that the error is 0 over the non-overlapping time range).

First, the trajectories are splined and sampled. The number of sampling points is taken to be the number of times in the trajectory with the greater number of times. Numerical integration is performed on the sampled points using the trapezoidal rule.

By default, all states, controls, and multipliers are compared, and it is expected that both trajectories have the same states, controls, and multipliers. Alternatively, you can specify the specific states, controls, Input controls, multipliers, and derivatives to compare as keys for columnsToUse. Values are an empty vector to compare all columns for that key, {"none"} (single-entry vector with value "none") to compare none of the columns for that key, or a vector of column labels to compare for that key. Leaving out a key means no columns for that key are compared. Both trajectories must have at least 6 time nodes. If the number of columns to compare is 0, this returns 0.

◆ compareContinuousVariablesRMSPattern()

double OpenSim::MocoTrajectory::compareContinuousVariablesRMSPattern ( const MocoTrajectory other,
std::string  columnType,
std::string  pattern 
) const

This is an alternative interface for compareContinuousVariablesRMS() that uses regular expression patterns to select columns.

The parameter columnType is "states", "controls", "multipliers", or "derivatives". All columns for the provided column type whose entire name matches the provided regular expression are included in the root-mean-square.

◆ compareParametersRMS()

double OpenSim::MocoTrajectory::compareParametersRMS ( const MocoTrajectory other,
std::vector< std::string >  parameterNames = {} 
) const

Compute the root-mean-square error between the parameters in this trajectory and another.

The RMS is computed by dividing the sum of the squared errors between corresponding parameters and then dividing by the number of parameters compared. By default, all parameters are compared, and it is expected that both trajectories have the same parameters. Alternatively, you can specify the specific parameters to compare.

◆ createFromStatesControlsTables()

static MocoTrajectory OpenSim::MocoTrajectory::createFromStatesControlsTables ( const MocoProblemRep ,
const TimeSeriesTable statesTrajectory,
const TimeSeriesTable controlsTrajectory 
)
static

(Experimental) Create a trajectory from a states trajectory and controls trajectory (i.e, from Manager::getStatesTable() and Model::getControlsTable()).

The time columns from the two tables must match exactly. The times in the trajectory will be those from the tables. This does not (yet) handle parameters.

◆ empty()

bool OpenSim::MocoTrajectory::empty ( ) const
inline

◆ ensureUnsealed()

void OpenSim::MocoTrajectory::ensureUnsealed ( ) const
protected
Exceptions
MocoTrajectoryIsSealedif the trajectory is sealed.

◆ exportToAccelerationsTable()

TimeSeriesTable OpenSim::MocoTrajectory::exportToAccelerationsTable ( ) const

Export the coordinate accelerations from the derivatives trajectory to a TimeSeriesTable.

◆ exportToControlsTable()

TimeSeriesTable OpenSim::MocoTrajectory::exportToControlsTable ( ) const

Export the controls trajectory to a TimeSeriesTable.

Examples:
exampleHangingMuscle.cpp.

Referenced by OpenSim::analyzeMocoTrajectory().

◆ exportToDerivativesTable()

TimeSeriesTable OpenSim::MocoTrajectory::exportToDerivativesTable ( ) const

Export the derivatives trajectory to a TimeSeriesTable.

◆ exportToDerivativesWithoutAccelerationsTable()

TimeSeriesTable OpenSim::MocoTrajectory::exportToDerivativesWithoutAccelerationsTable ( ) const

Export the derivatives trajectory without coordinate accelerations to a TimeSeriesTable.

Referenced by OpenSim::analyzeMocoTrajectory().

◆ exportToInputControlsTable()

TimeSeriesTable OpenSim::MocoTrajectory::exportToInputControlsTable ( ) const

Export the Input controls trajectory to a TimeSeriesTable.

◆ exportToMultipliersTable()

TimeSeriesTable OpenSim::MocoTrajectory::exportToMultipliersTable ( ) const

Export the multipliers trajectory to a TimeSeriesTable.

◆ exportToSpeedsTable()

TimeSeriesTable OpenSim::MocoTrajectory::exportToSpeedsTable ( ) const

Export the coordinate speeds from the states trajectory to a TimeSeriesTable.

◆ exportToStatesTable()

TimeSeriesTable OpenSim::MocoTrajectory::exportToStatesTable ( ) const

This table can be saved as a Storage file that can be used in the OpenSim GUI to visualize a motion, or as input to OpenSim's conventional tools (e.g., AnalyzeTool).

Controls are not carried over to the states storage.

Examples:
exampleHangingMuscle.cpp.

Referenced by OpenSim::analyzeMocoTrajectory().

◆ exportToStatesTrajectory() [1/2]

StatesTrajectory OpenSim::MocoTrajectory::exportToStatesTrajectory ( const MocoProblem ) const

Controls are not carried over to the StatesTrajectory.

The MocoProblem is necessary because we need the underlying Model to order the state variables correctly.

◆ exportToStatesTrajectory() [2/2]

StatesTrajectory OpenSim::MocoTrajectory::exportToStatesTrajectory ( const Model ) const

This is similar to the above function but requires only a model, not a MocoProblem.

◆ exportToValuesTable()

TimeSeriesTable OpenSim::MocoTrajectory::exportToValuesTable ( ) const

Export the coordinate values from the states trajectory to a TimeSeriesTable.

◆ generateAccelerationsFromSpeeds()

void OpenSim::MocoTrajectory::generateAccelerationsFromSpeeds ( )

Compute coordinate accelerations based on coordinate speeds and append to the trajectory.

Coordinate speeds must exist in the original trajectory.

Note
Overrides any existing acceleration values in the trajectory.

◆ generateAccelerationsFromValues()

void OpenSim::MocoTrajectory::generateAccelerationsFromValues ( )

Compute coordinate accelerations based on coordinate position values and append to the trajectory.

Coordinate values must exist in the original trajectory.

Note
Overrides any existing acceleration values in the trajectory.

◆ generateControlsFromModelControllers()

void OpenSim::MocoTrajectory::generateControlsFromModelControllers ( Model  model,
bool  overwrite = false 
)

Compute model controls based on any Controllers in the model and append to the trajectory.

The model must contain InputControllers with Inputs that match the Input control names in the trajectory, if they exist.

Note
Use overwrite to replace existing control values in the trajectory, if desired.
Examples:
exampleMocoInverse.cpp.

◆ generateSpeedsFromValues()

void OpenSim::MocoTrajectory::generateSpeedsFromValues ( )

Compute coordinate speeds based on coordinate position values and append to the trajectory.

Coordinate values must exist in the original trajectory.

Note
Overrides any existing speed values in the trajectory.

◆ getAccelerationNames()

std::vector<std::string> OpenSim::MocoTrajectory::getAccelerationNames ( ) const
inline

◆ getAccelerationsTrajectory()

SimTK::Matrix OpenSim::MocoTrajectory::getAccelerationsTrajectory ( ) const
inline

◆ getAuxiliaryStateNames()

std::vector<std::string> OpenSim::MocoTrajectory::getAuxiliaryStateNames ( ) const
inline

◆ getAuxiliaryStatesTrajectory()

SimTK::Matrix OpenSim::MocoTrajectory::getAuxiliaryStatesTrajectory ( ) const
inline

◆ getControl()

SimTK::VectorView_<double> OpenSim::MocoTrajectory::getControl ( const std::string &  name) const

◆ getControlNames()

const std::vector<std::string>& OpenSim::MocoTrajectory::getControlNames ( ) const
inline

◆ getControlsTrajectory()

const SimTK::Matrix& OpenSim::MocoTrajectory::getControlsTrajectory ( ) const
inline

◆ getDerivative()

SimTK::VectorView_<double> OpenSim::MocoTrajectory::getDerivative ( const std::string &  name) const

◆ getDerivativeNames()

const std::vector<std::string>& OpenSim::MocoTrajectory::getDerivativeNames ( ) const
inline

◆ getDerivativeNamesWithoutAccelerations()

std::vector<std::string> OpenSim::MocoTrajectory::getDerivativeNamesWithoutAccelerations ( ) const
inline

◆ getDerivativesTrajectory()

const SimTK::Matrix& OpenSim::MocoTrajectory::getDerivativesTrajectory ( ) const
inline

◆ getDerivativesWithoutAccelerationsTrajectory()

SimTK::Matrix OpenSim::MocoTrajectory::getDerivativesWithoutAccelerationsTrajectory ( ) const
inline

◆ getFinalTime()

double OpenSim::MocoTrajectory::getFinalTime ( ) const

The last time in the time vector.

Exceptions
ExceptionIf numTimes is 0.

◆ getInitialTime()

double OpenSim::MocoTrajectory::getInitialTime ( ) const

The first time in the time vector.

Exceptions
ExceptionIf numTimes is 0.

◆ getInputControl()

SimTK::VectorView_<double> OpenSim::MocoTrajectory::getInputControl ( const std::string &  name) const

◆ getInputControlNames()

const std::vector<std::string>& OpenSim::MocoTrajectory::getInputControlNames ( ) const
inline

◆ getInputControlsTrajectory()

const SimTK::Matrix& OpenSim::MocoTrajectory::getInputControlsTrajectory ( ) const
inline

◆ getMultibodyStateNames()

std::vector<std::string> OpenSim::MocoTrajectory::getMultibodyStateNames ( ) const
inline

◆ getMultibodyStatesTrajectory()

SimTK::Matrix OpenSim::MocoTrajectory::getMultibodyStatesTrajectory ( ) const
inline

◆ getMultiplier()

SimTK::VectorView_<double> OpenSim::MocoTrajectory::getMultiplier ( const std::string &  name) const

◆ getMultiplierNames()

const std::vector<std::string>& OpenSim::MocoTrajectory::getMultiplierNames ( ) const
inline

◆ getMultipliersTrajectory()

const SimTK::Matrix& OpenSim::MocoTrajectory::getMultipliersTrajectory ( ) const
inline

◆ getNumAccelerations()

int OpenSim::MocoTrajectory::getNumAccelerations ( ) const
inline

◆ getNumAuxiliaryStates()

int OpenSim::MocoTrajectory::getNumAuxiliaryStates ( ) const
inline

◆ getNumControls()

int OpenSim::MocoTrajectory::getNumControls ( ) const
inline

◆ getNumDerivatives()

int OpenSim::MocoTrajectory::getNumDerivatives ( ) const
inline

◆ getNumDerivativesWithoutAccelerations()

int OpenSim::MocoTrajectory::getNumDerivativesWithoutAccelerations ( ) const
inline

◆ getNumInputControls()

int OpenSim::MocoTrajectory::getNumInputControls ( ) const
inline

◆ getNumMultibodyStates()

int OpenSim::MocoTrajectory::getNumMultibodyStates ( ) const
inline

◆ getNumMultipliers()

int OpenSim::MocoTrajectory::getNumMultipliers ( ) const
inline

◆ getNumParameters()

int OpenSim::MocoTrajectory::getNumParameters ( ) const
inline

◆ getNumSpeeds()

int OpenSim::MocoTrajectory::getNumSpeeds ( ) const
inline

◆ getNumStates()

int OpenSim::MocoTrajectory::getNumStates ( ) const
inline

◆ getNumTimes()

int OpenSim::MocoTrajectory::getNumTimes ( ) const
inline

◆ getNumValues()

int OpenSim::MocoTrajectory::getNumValues ( ) const
inline

◆ getParameter()

const SimTK::Real& OpenSim::MocoTrajectory::getParameter ( const std::string &  name) const

◆ getParameterNames()

const std::vector<std::string>& OpenSim::MocoTrajectory::getParameterNames ( ) const
inline

◆ getParameters()

const SimTK::RowVector& OpenSim::MocoTrajectory::getParameters ( ) const
inline

◆ getSpeedNames()

std::vector<std::string> OpenSim::MocoTrajectory::getSpeedNames ( ) const
inline

◆ getSpeedsTrajectory()

SimTK::Matrix OpenSim::MocoTrajectory::getSpeedsTrajectory ( ) const
inline

◆ getState()

SimTK::VectorView_<double> OpenSim::MocoTrajectory::getState ( const std::string &  name) const

◆ getStateNames()

const std::vector<std::string>& OpenSim::MocoTrajectory::getStateNames ( ) const
inline

◆ getStatesTrajectory()

const SimTK::Matrix& OpenSim::MocoTrajectory::getStatesTrajectory ( ) const
inline

◆ getTime()

const SimTK::Vector& OpenSim::MocoTrajectory::getTime ( ) const
inline

◆ getValueNames()

std::vector<std::string> OpenSim::MocoTrajectory::getValueNames ( ) const
inline

◆ getValuesTrajectory()

SimTK::Matrix OpenSim::MocoTrajectory::getValuesTrajectory ( ) const
inline

◆ hasCoordinateStates()

bool OpenSim::MocoTrajectory::hasCoordinateStates ( ) const
inline

◆ insertControlsTrajectory()

void OpenSim::MocoTrajectory::insertControlsTrajectory ( const TimeSeriesTable subsetOfControls,
bool  overwrite = false 
)

Add additional control columns.

The provided data are interpolated using GCV splines to match the times in this trajectory. By default, we do not overwrite data for controls that already exist in the trajectory; you can change this behavior with overwrite.

◆ insertStatesTrajectory()

void OpenSim::MocoTrajectory::insertStatesTrajectory ( const TimeSeriesTable subsetOfStates,
bool  overwrite = false 
)

Add additional state columns.

The provided data are interpolated using GCV splines to match the times in this trajectory. By default, we do not overwrite data for states that already exist in the trajectory; you can change this behavior with overwrite.

Examples:
exampleMocoInverse.cpp.

◆ isCompatible()

bool OpenSim::MocoTrajectory::isCompatible ( const MocoProblemRep ,
bool  requireAccelerations = false,
bool  throwOnError = false 
) const

Do the state, control, multiplier, derivative, and parameter names in this trajectory match those in the problem? This may not catch all possible incompatibilities.

If the trajectory should have generalized accelerations (for implicit multibody dynamics mode), set requireAccelerations to true. To throw an exception with a detailed message if the problem is not compatible, pass throwOnError as true. To get the detailed message without an exception, set the Logger level to Debug or a more verbose setting (e.g., Logger::setLevel(Logger::Level::Debug) in C++, and Logger.setLevel(Logger.Level_Debug) in MATLAB/Python).

◆ isNumericallyEqual()

bool OpenSim::MocoTrajectory::isNumericallyEqual ( const MocoTrajectory other,
double  tol = SimTK::NTraits< SimTK::Real >::getDefaultTolerance() 
) const

Check if this trajectory is numerically equal to another trajectory.

This uses SimTK::Test::numericallyEqual() internally. Accordingly, the tolerance is both a relative and absolute tolerance (depending on the magnitude of quantities being compared).

◆ isSealed()

bool OpenSim::MocoTrajectory::isSealed ( ) const
inlineprotected

◆ randomizeAdd()

void OpenSim::MocoTrajectory::randomizeAdd ( const SimTK::Random &  randGen = SimTK::Random::Uniform(-0.1, 0.1))
inline

Randomize all data except time using the provided random number generator.

The random numbers are added to the existing data. Use this to perturb an existing solution, probably for a MocoSolver guess. The default random number generator samples uniformly within [-0.1, 0.1].

◆ randomizeReplace()

void OpenSim::MocoTrajectory::randomizeReplace ( const SimTK::Random &  randGen = SimTK::Random::Uniform(-0.1, 0.1))
inline

Randomize all data except time using the provided random number generator.

All data is replaced with the random numbers. Use this to create a completely (pseudo-)random trajectory, probably for a MocoSolver guess. The default random number generator samples uniformly within [-0.1, 0.1].

◆ resample()

void OpenSim::MocoTrajectory::resample ( SimTK::Vector  newTime)

Resample (interpolate) the data in this trajectory at the provided times.

If all times have the same value (e.g., 0.0), then the value of each variable for all time is its previous value at the initial time.

Exceptions
Exceptionif new times are not within existing initial and final times, if the new times are decreasing, or if getNumTimes() < 2.

◆ resampleWithFrequency()

double OpenSim::MocoTrajectory::resampleWithFrequency ( double  desiredNumTimePointsPerSecond)

Uniformly resample (interpolate) the trajectory to try to achieve the provided frequency of time points per second of the trajectory, while preserving the initial and final times.

The resulting frequency may be higher than what you request (in order to preserve initial and final times), and is returned by this function. Resampling is done by creating a 5-th degree GCV spline of the states and controls and evaluating the spline at the new time points. The degree is reduced as necessary if getNumTimes() < 6, and resampling is not possible if getNumTimes() < 2.

◆ resampleWithInterval()

double OpenSim::MocoTrajectory::resampleWithInterval ( double  desiredTimeInterval)

Uniformly resample (interpolate) the trajectory to try to achieve the provided time interval between mesh points, while preserving the initial and final times.

The resulting time interval may be shorter than what you request (in order to preserve initial and final times), and is returned by this function. Resampling is done by creating a 5-th degree GCV spline of the states and controls and evaluating the spline at the new time points. The degree is reduced as necessary if getNumTimes() < 6, and resampling is not possible if getNumTimes() < 2.

◆ resampleWithNumTimes()

double OpenSim::MocoTrajectory::resampleWithNumTimes ( int  numTimes)

Uniformly resample (interpolate) the trajectory so that it retains the same initial and final times but now has the provided number of time points.

Resampling is done by creating a 5-th degree GCV spline of the states and controls and evaluating the spline at the numTimes time points. The degree is reduced as necessary if getNumTimes() < 6, and resampling is not possible if getNumTimes() < 2.

Returns
the resulting time interval between time points.
Examples:
exampleMocoCustomEffortGoal.cpp, and exampleSlidingMassAdvanced.cpp.

◆ setControl() [1/2]

void OpenSim::MocoTrajectory::setControl ( const std::string &  name,
const SimTK::Vector &  trajectory 
)

Set the value of a single control variable across time.

The provided vector must have length getNumTimes().

Note
Using setControl(name, {5, 10}) uses the initializer list overload below; it does not construct a 5-element vector with the value 10.

◆ setControl() [2/2]

void OpenSim::MocoTrajectory::setControl ( const std::string &  name,
std::initializer_list< double >  trajectory 
)
inline

Set the value of a single control variable across time.

The provided vector must have length getNumTimes(). This variant supports use of an initializer list:

trajectory.setControl("/forceset/soleus", {0, 0.5, 1.0});

◆ setDerivative() [1/2]

void OpenSim::MocoTrajectory::setDerivative ( const std::string &  name,
const SimTK::Vector &  trajectory 
)

Set the value of a single state derivative variable across time.

The provided vector must have length getNumTimes().

Note
Using setDerivative(name, {5, 10}) uses the initializer list overload below; it does not construct a 5-element vector with the value 10.

◆ setDerivative() [2/2]

void OpenSim::MocoTrajectory::setDerivative ( const std::string &  name,
std::initializer_list< double >  trajectory 
)
inline

Set the value of a single state derivative variable across time.

The provided vector must have length getNumTimes(). This variant supports use of an initializer list:

trajectory.setDerivative("/jointset/joint/coord/accel", {0, 0.5, 1.0});

◆ setInputControl() [1/2]

void OpenSim::MocoTrajectory::setInputControl ( const std::string &  name,
const SimTK::Vector &  trajectory 
)

Set the value of a single Input control variable across time.

The provided vector must have length getNumTimes().

Note
Using setInputControl(name, {5, 10}) uses the initializer list overload below; it does not construct a 5-element vector with the value 10.

◆ setInputControl() [2/2]

void OpenSim::MocoTrajectory::setInputControl ( const std::string &  name,
std::initializer_list< double >  trajectory 
)
inline

Set the value of a single Input control variable across time.

The provided vector must have length getNumTimes(). This variant supports use of an initializer list:

trajectory.setInputControl("/my_controller/input", {0, 0.5, 1.0});

◆ setMultiplier() [1/2]

void OpenSim::MocoTrajectory::setMultiplier ( const std::string &  name,
const SimTK::Vector &  trajectory 
)

Set the value of a single Lagrange multiplier variable across time.

The provided vector must have length getNumTimes().

Note
Using setMultiplier(name, {5, 10}) uses the initializer list overload below; it does not construct a 5-element vector with the value 10.

◆ setMultiplier() [2/2]

void OpenSim::MocoTrajectory::setMultiplier ( const std::string &  name,
std::initializer_list< double >  trajectory 
)
inline

Set the value of a single Lagrange multiplier variable across time.

The provided vector must have length getNumTimes(). This variant supports use of an initializer list:

trajectory.setMultiplier("lambda_cid0_p0", {0, 0.5, 1.0});

◆ setNumTimes()

void OpenSim::MocoTrajectory::setNumTimes ( int  numTimes)
inline

Resize the time vector and the time dimension of the states, controls, multipliers, and derivatives trajectories, and set all times, states, controls, multipliers, and derivatives to NaN.

Note
Parameters are NOT set to NaN.

◆ setParameter()

void OpenSim::MocoTrajectory::setParameter ( const std::string &  name,
const SimTK::Real &  value 
)

Set the value of a single parameter variable.

This value is invariant across time.

◆ setSealed()

void OpenSim::MocoTrajectory::setSealed ( bool  sealed)
inlineprotected

◆ setState() [1/2]

void OpenSim::MocoTrajectory::setState ( const std::string &  name,
const SimTK::Vector &  trajectory 
)

Set the value of a single state variable across time.

The provided vector must have length getNumTimes().

Note
Using setState(name, {5, 10}) uses the initializer list overload below; it does not construct a 5-element vector with the value 10.
Examples:
exampleMocoCustomEffortGoal.cpp, and exampleSlidingMassAdvanced.cpp.

◆ setState() [2/2]

void OpenSim::MocoTrajectory::setState ( const std::string &  name,
std::initializer_list< double >  trajectory 
)
inline

Set the value of a single state variable across time.

The provided vector must have length getNumTimes(). This variant supports use of an initializer list:

trajectory.setState("/jointset/knee/flexion/value", {0, 0.5, 1.0});

◆ setStatesTrajectory()

void OpenSim::MocoTrajectory::setStatesTrajectory ( const TimeSeriesTable states,
bool  allowMissingColumns = false,
bool  allowExtraColumns = false 
)

Set the states trajectory.

The provided data is interpolated at the times contained within this trajectory. The controls trajectory is not altered. If the table only contains a subset of the states in the trajectory (and allowMissingColumns is true), the unspecified states preserve their pre-existing values.

This function might be helpful if you generate a guess using a forward simulation; you can access the forward simulation's states trajectory using Manager::getStateStorage() or Manager::getStatesTable().

Parameters
statesThe column labels of the table should match the state names (see getStateNames()). By default, the table must provide all state variables. Any data outside the time range of this guess's times are ignored.
allowMissingColumnsIf false, an exception is thrown if there are states in the trajectory that are not in the table.
allowExtraColumnsIf false, an exception is thrown if there are states in the table that are not in the trajectory.
See also
createFromStatesControlsTables.

◆ setTime() [1/2]

void OpenSim::MocoTrajectory::setTime ( const SimTK::Vector &  time)

Set the time vector.

The provided vector must have the same number of elements as the pre-existing time vector; use setNumTimes() or the "resample..." functions to change the number of times.

Note
Using setTime({5, 10}) uses the initializer list overload below; it does not construct a 5-element vector with the value 10.
Examples:
exampleMocoCustomEffortGoal.cpp, and exampleSlidingMassAdvanced.cpp.

◆ setTime() [2/2]

void OpenSim::MocoTrajectory::setTime ( std::initializer_list< double >  time)
inline

Set the time vector.

The provided vector must have the same number of elements as the pre-existing time vector; use setNumTimes() or the "resample..." functions to change the number of times. This variant supports use of an initializer list. Example:

trajectory.setTime({0, 0.5, 1.0});

◆ trimToIndices()

void OpenSim::MocoTrajectory::trimToIndices ( int  newStartIndex,
int  newFinalIndex 
)

Trim the trajectory to include the rows starting at newStartIndex and and ending at newFinalIndex.

◆ write()

void OpenSim::MocoTrajectory::write ( const std::string &  filepath) const

Save the trajectory to a STO file. Use the ."sto" file extension.

Examples:
exampleMarkerTracking.cpp, exampleMocoInverse.cpp, exampleMocoTrack.cpp, and exampleTracking.cpp.

The documentation for this class was generated from the following file: