You are viewing the documentation for OpenSim 2.4. Are you looking for the latest OpenSim 4.0 Documentation?

Performing a Simulation Part Two

The steps covered in part two are:

Initialize the OpenSim Model System and Get the State

An OpenSim model is backed by a SimTK::System (see Chapter 5), which actually performs the computations. As such, the model itself is a stateless object with the state being stored externally in an instance of SimTK::State. To begin simulating the block falling, we initialize the SimTK::System associated with the OpenSim model and create an instance of the system state. After the call to initSystem(), no changes should be made to the structure of the model. For example, adding forces or constraints would require the re-creation of the system and a fresh call to initSystem() since these objects may have a state of their own that needs to be incorporated into the system's state.

// Initialize the system
SimTK::State& si = osimModel.initSystem();

Define Initial Position and Velocity States of the Block

Next, we define the initial position and velocity of the block. For the free joint, the position coordinates and their velocities are ordered 0 (x-rotation), 1 (y-rotation), 2 (z-rotation), 3 (x-translation), 4 (y-translation), and 5 (z-translation).

// Define non-zero (defaults are 0) states for the free joint
CoordinateSet& modelCoordinateSet = osimModel.updCoordinateSet();
 
// set x-translation value
modelCoordinateSet[3].setValue(si, blockSideLength);
 
// set x-speed value
modelCoordinateSet[3].setSpeedValue(si, 0.1)
 
// set y-translation value
modelCoordinateSet[4].setValue(si, blockSideLength/2+0.01); 

Create the Integrator and Manager for the Simulation

We create the integrator and manager for the simulation in order to perform the numerical integration of the system equations of motion during the forward dynamics simulation. An OpenSim Manager object collects together all the resources need to perform a simulation, including the Model, the numerical methods to be employed, the current State, storage for the trajectory, and runtime options for controlling the simulation.

 

// Create the integrator and manager for the simulation.
SimTK::RungeKuttaMersonIntegrator integrator(osimModel.getMultibodySystem());
integrator.setAccuracy(1.0e-4);
Manager manager(osimModel, integrator);

Integrate the System Equations of Motion

We integrate the system equations of motion from the initial time to the final time of the simulation. Depending on your computer speed, this numerical integration could take from a few to several seconds.

// Define the initial and final simulation times
double initialTime = 0.0;
double finalTime = 4.0; 
// Integrate from initial time to final time
manager.setInitialTime(initialTime);
manager.setFinalTime(finalTime);
std::cout<<"\n\nIntegrating from "<<initialTime<<" to " <<finalTime<<std::endl;
manager.integrate(si);

Save the Simulation Results

After we have performed the integration for the forward dynamics simulation, we save the resulting motion in order to visualize the simulation we have created. Note that OpenSim uses radians internally but degrees are required in a .mot file, so we have to convert to degrees before writing out the .mot file for visualization.

// Save the simulation results
Storage statesDegrees(manager.getStateStorage());
statesDegrees.print("tugOfWar_states.sto");
osimModel.updSimbodyEngine().convertRadiansToDegrees(statesDegrees);
statesDegrees.setWriteSIMMHeader(true);
statesDegrees.print("tugOfWar_states_degrees.mot");

 

Exercise 4: After we compile and run the current main program, we can load the model and the motion in the OpenSim GUI and visualize the simulation. (To load the motion, go to File → Load Motion. Select the motion file tugOfWar_states_degrees.mot that we just wrote out.)

Figure: Block is falling in the presence of gravity.

Except for the colors, you should see the above in the GUI. Using the motion slider and video controls, visualize the motion. You should see the block falling under gravity.

OpenSim is supported by the Mobilize Center , an NIH Biomedical Technology Resource Center (grant P41 EB027060); the Restore Center , an NIH-funded Medical Rehabilitation Research Resource Network Center (grant P2C HD101913); and the Wu Tsai Human Performance Alliance through the Joe and Clara Tsai Foundation. See the People page for a list of the many people who have contributed to the OpenSim project over the years. ©2010-2024 OpenSim. All rights reserved.