OpenSim Moco  0.4.0
Functions
Model and trajectory utilities

These are utilities for visualizing model, editing a MocoTrajectory, and more. More...

Functions

OSIMMOCO_API void OpenSim::visualize (Model, Storage)
 Play back a motion (from the Storage) in the simbody-visuailzer. More...
 
OSIMMOCO_API void OpenSim::visualize (Model, TimeSeriesTable)
 This function is the same as visualize(Model, Storage), except that the states are provided in a TimeSeriesTable.
 
template<typename T >
TimeSeriesTable_< T > OpenSim::analyze (Model model, const MocoTrajectory &trajectory, std::vector< std::string > outputPaths)
 Calculate the requested outputs using the model in the problem and the states and controls in the MocoTrajectory. More...
 
OSIMMOCO_API void OpenSim::prescribeControlsToModel (const MocoTrajectory &trajectory, Model &model, std::string functionType="GCVSpline")
 Given a MocoTrajectory and the associated OpenSim model, return the model with a prescribed controller appended that will compute the control values from the MocoSolution. More...
 
OSIMMOCO_API MocoTrajectory OpenSim::simulateTrajectoryWithTimeStepping (const MocoTrajectory &trajectory, Model model, double integratorAccuracy=-1)
 Use the controls and initial state in the provided trajectory to simulate the model using an ODE time stepping integrator (OpenSim::Manager), and return the resulting states and controls. More...
 
OSIMMOCO_API std::vector< std::string > OpenSim::createStateVariableNamesInSystemOrder (const Model &model)
 The map provides the index of each state variable in SimTK::State::getY() from its each state variable path string. More...
 
OSIMMOCO_API std::vector< std::string > OpenSim::createStateVariableNamesInSystemOrder (const Model &model, std::unordered_map< int, int > &yIndexMap)
 Same as above, but you can obtain a map from the returned state variable names to the index in SimTK::State::getY() that accounts for empty slots in Y.
 
OSIMMOCO_API std::unordered_map< std::string, int > OpenSim::createSystemYIndexMap (const Model &model)
 The map provides the index of each state variable in SimTK::State::getY() from its state variable path string.
 
OSIMMOCO_API std::vector< std::string > OpenSim::createControlNamesFromModel (const Model &model, std::vector< int > &modelControlIndices)
 Create a vector of control names based on the actuators in the model for which appliesForce == True. More...
 
OSIMMOCO_API std::vector< std::string > OpenSim::createControlNamesFromModel (const Model &model)
 Same as above, but when there is no mapping to the modelControlIndices.
 
OSIMMOCO_API std::unordered_map< std::string, int > OpenSim::createSystemControlIndexMap (const Model &model)
 The map provides the index of each control variable in the SimTK::Vector return by OpenSim::Model::getControls() from its control name.
 
OSIMMOCO_API void OpenSim::checkOrderSystemControls (const Model &model)
 Throws an exception if the order of the controls in the model is not the same as the order of the actuators in the model.
 
OSIMMOCO_API void OpenSim::checkRedundantLabels (std::vector< std::string > labels)
 Throws an exception if the same label appears twice in the list of labels. More...
 
template<typename T = double>
std::vector< SimTK::ReferencePtr< const Output< T > > > OpenSim::getModelOutputReferencePtrs (const Component &component, const std::string &pattern, bool includeDescendents=false)
 Get a list of reference pointers to all outputs whose names (not paths) match a substring defined by a provided regex string pattern. More...
 
OSIMMOCO_API MocoTrajectory OpenSim::createPeriodicTrajectory (const MocoTrajectory &halfPeriodTrajectory, std::vector< std::string > addPatterns={".*pelvis_tx/value"}, std::vector< std::string > negatePatterns={ ".*pelvis_list(?!/value).*", ".*pelvis_rotation.*", ".*pelvis_tz(?!/value).*", ".*lumbar_bending(?!/value).*", ".*lumbar_rotation.*"}, std::vector< std::string > negateAndShiftPatterns={ ".*pelvis_list/value", ".*pelvis_tz/value", ".*lumbar_bending/value"}, std::vector< std::pair< std::string, std::string >> symmetryPatterns={{R"(_r(\_|$))", "_l$1"}, {R"(_l(\_|$))", "_r$1"}})
 Convert a trajectory covering half the period of a symmetric motion into a trajectory over the full period. More...
 
OSIMMOCO_API TimeSeriesTable OpenSim::createExternalLoadsTableForGait (Model model, const StatesTrajectory &trajectory, const std::vector< std::string > &forcePathsRightFoot, const std::vector< std::string > &forcePathsLeftFoot)
 Obtain the ground reaction forces, centers of pressure, and torques resulting from Force elements (e.g., SmoothSphereHalfSpaceForce), using a model and states trajectory. More...
 
OSIMMOCO_API TimeSeriesTable OpenSim::createExternalLoadsTableForGait (Model model, const MocoTrajectory &trajectory, const std::vector< std::string > &forcePathsRightFoot, const std::vector< std::string > &forcePathsLeftFoot)
 Same as above, but with a MocoTrajectory instead of a StatesTrajectory.
 

Detailed Description

These are utilities for visualizing model, editing a MocoTrajectory, and more.

To use these utilities in MATLAB, prepend the function names with "opensimMoco."; for example, opensimMoco.visualize() or org.opensim.modeling.opensimMoco.visualize(). In Python, use opensim.visualize().

Function Documentation

◆ analyze()

template<typename T >
TimeSeriesTable_<T> OpenSim::analyze ( Model  model,
const MocoTrajectory trajectory,
std::vector< std::string >  outputPaths 
)

Calculate the requested outputs using the model in the problem and the states and controls in the MocoTrajectory.

The output paths can be regular expressions. For example, ".*activation" gives the activation of all muscles. Constraints are not enforced but prescribed motion (e.g., PositionMotion) is. The output paths must correspond to outputs that match the type provided in the template argument, otherwise they are not included in the report.

Note
Parameters and Lagrange multipliers in the MocoTrajectory are not applied to the model.

◆ checkRedundantLabels()

OSIMMOCO_API void OpenSim::checkRedundantLabels ( std::vector< std::string >  labels)

Throws an exception if the same label appears twice in the list of labels.

The argument copies the provided labels since we need to sort them to check for redundancies.

◆ createControlNamesFromModel()

OSIMMOCO_API std::vector<std::string> OpenSim::createControlNamesFromModel ( const Model &  model,
std::vector< int > &  modelControlIndices 
)

Create a vector of control names based on the actuators in the model for which appliesForce == True.

For actuators with one control (e.g. ScalarActuator) the control name is simply the actuator name. For actuators with multiple controls, each control name is the actuator name appended by the control index (e.g. "/actuator_0"); modelControlIndices has length equal to the number of controls associated with actuators that apply a force (appliesForce == True). Its elements are the indices of the controls in the Model::updControls() that are associated with actuators that apply a force.

◆ createExternalLoadsTableForGait()

OSIMMOCO_API TimeSeriesTable OpenSim::createExternalLoadsTableForGait ( Model  model,
const StatesTrajectory &  trajectory,
const std::vector< std::string > &  forcePathsRightFoot,
const std::vector< std::string > &  forcePathsLeftFoot 
)

Obtain the ground reaction forces, centers of pressure, and torques resulting from Force elements (e.g., SmoothSphereHalfSpaceForce), using a model and states trajectory.

Forces and torques are expressed in the ground frame with respect to the ground origin. Hence, the centers of pressure are at the origin. Paths to Force elements should be provided separately for elements of the right and left feet. The output is a table formatted for use with OpenSim tools; the labels of the columns distinguish between right ("<>_r") and left ("<>_l") forces, centers of pressure, and torques. The forces and torques used are taken from the first six outputs of getRecordValues(); this order is of use for, for example, the SmoothSphereHalfSpaceForce contact model but might have a different meaning for different contact models.

Examples:
example2DWalking.cpp.

◆ createPeriodicTrajectory()

OSIMMOCO_API MocoTrajectory OpenSim::createPeriodicTrajectory ( const MocoTrajectory halfPeriodTrajectory,
std::vector< std::string >  addPatterns = {".*pelvis_tx/value"},
std::vector< std::string >  negatePatterns = { ".*pelvis_list(?!/value).*", ".*pelvis_rotation.*", ".*pelvis_tz(?!/value).*", ".*lumbar_bending(?!/value).*", ".*lumbar_rotation.*"},
std::vector< std::string >  negateAndShiftPatterns = { ".*pelvis_list/value", ".*pelvis_tz/value", ".*lumbar_bending/value"},
std::vector< std::pair< std::string, std::string >>  symmetryPatterns = {{R"(_r(\_|$))", "_l$1"}, {R"(_l(\_|$))", "_r$1"}} 
)

Convert a trajectory covering half the period of a symmetric motion into a trajectory over the full period.

This is useful for simulations of half a gait cycle. This converts time, states, controls, and derivatives; all other quantities from the input trajectory are ignored. If a column in the trajectory does not match addPatterns, negatePatterns, negateAndShiftPatterns, or symmetryPatterns, then the second half of the period contains the same data as the first half.

Parameters
halfPeriodTrajectoryThe input trajectory covering half a period.
addPatternsIf a column label matches an addPattern, then the second half of the period for that column is (first_half_trajectory + half_period_value - initial_value).
negatePatternsIf a column label matches a negatePattern, then the second half of the period for that column is (-first_half_trajectory). This is usually relevant for only 3D models.
negateAndShiftPatternsIf a column label matches a negateAndShiftPattern, then the second half of the period for that column is (-first_half_trajectory + 2 * half_period_value). This is usually relevant for only 3D models.
symmetryPatternsThis argument is a list of pairs, where the first element of the pair is a pattern to match, and the second is a substitution to convert the column label into the opposite column label of the symmetric pair. If a column label matches a symmetryPattern, then its first half-period is copied into the second half of the period for the column identified by the substitution.

The default values for the patterns are intended to handle the column labels for typical 2D or 3D OpenSim gait models. The default values for negatePatterns, negateAndShiftPatterns, and symmetryPatterns warrant an explanation. The string pattern before the regex "(?!/value)" is followed by anything except "/value" since it is contained in the negative lookahead "(?!...)". R"()" is a string literal that permits us to not escape backslash characters. The regex "_r(\/|_|$)" matches "_r" followed by either a forward slash (which is escaped), an underscore, OR the end of the string ($). Since the forward slash and end of the string are within parentheses, whatever matches this is captured and is available in the substitution (the second element of the pair) as $1. The default symmetry patterns cause the following replacements:

  • "/jointset/hip_r/hip_flexion_r/value" becomes "/jointset/hip_l/hip_flexion_l/value"
  • "/forceset/soleus_r" becomes "/forceset/soleus_l"
Examples:
example2DWalking.cpp.

◆ createStateVariableNamesInSystemOrder()

OSIMMOCO_API std::vector<std::string> OpenSim::createStateVariableNamesInSystemOrder ( const Model &  model)

The map provides the index of each state variable in SimTK::State::getY() from its each state variable path string.

Empty slots in Y (e.g., for quaternions) are ignored.

◆ getModelOutputReferencePtrs()

template<typename T = double>
std::vector<SimTK::ReferencePtr<const Output<T> > > OpenSim::getModelOutputReferencePtrs ( const Component &  component,
const std::string &  pattern,
bool  includeDescendents = false 
)

Get a list of reference pointers to all outputs whose names (not paths) match a substring defined by a provided regex string pattern.

The regex string pattern could be the full name of the output. Only Outputs that match the template argument type will be returned (double is the default type). Set the argument 'includeDescendents' to true to include outputs from all descendents from the provided component.

◆ prescribeControlsToModel()

OSIMMOCO_API void OpenSim::prescribeControlsToModel ( const MocoTrajectory trajectory,
Model &  model,
std::string  functionType = "GCVSpline" 
)

Given a MocoTrajectory and the associated OpenSim model, return the model with a prescribed controller appended that will compute the control values from the MocoSolution.

This can be useful when computing state-dependent model quantities that require realization to the Dynamics stage or later. The function used to fit the controls can either be GCVSpline or PiecewiseLinearFunction.

◆ simulateTrajectoryWithTimeStepping()

OSIMMOCO_API MocoTrajectory OpenSim::simulateTrajectoryWithTimeStepping ( const MocoTrajectory trajectory,
Model  model,
double  integratorAccuracy = -1 
)

Use the controls and initial state in the provided trajectory to simulate the model using an ODE time stepping integrator (OpenSim::Manager), and return the resulting states and controls.

We return a MocoTrajectory (rather than a StatesTrajectory) to facilitate comparing optimal control solutions with time stepping. Use integratorAccuracy to override the default setting.

◆ visualize()

OSIMMOCO_API void OpenSim::visualize ( Model  ,
Storage   
)

Play back a motion (from the Storage) in the simbody-visuailzer.

The Storage should contain all generalized coordinates. The visualizer window allows the user to control playback speed. This function blocks until the user exits the simbody-visualizer window.

Examples:
exampleMocoTrack.m, exampleSquatToStand.m, and exampleSquatToStand_answers.m.