Minimal example for forward dynamics in python [SOLVED]

The 2018 Fall Virtual Workshop will bring together a group of international scholars and OpenSim experts to help each other advance their research using modeling and simulation. This forum is a place to accelerate OpenSim based projects while also buildin
POST REPLY
User avatar
Benjamin Michaud
Posts: 31
Joined: Mon May 03, 2010 6:35 am

Minimal example for forward dynamics in python [SOLVED]

Post by Benjamin Michaud » Tue Oct 23, 2018 12:45 pm

Hello,

I am struggling to get the the joint torques (or joint accelerations) from activated muscles. If someone can help me with a minimalist code, that would be appreciated!
So far I came up with this (based on StaticOptimizationTarget.cpp computeAcceleration function

Code: Select all

import numpy as np

import opensim as osim

subject = "mars"
pyosim_example_path = "/home/pariterre/Documents/Laboratoire/Programmation/pyosim/examples"
osim_model = "wu_scaled.osim"

Q = np.ones([19]) *2
QDot = np.ones([19])*10
muscle_excitation = np.zeros([29]) + 1.1


# Load the model
model_path = f"{pyosim_example_path}/results/{subject}/_models/{osim_model}"

# Prepare a model
model = osim.Model(model_path)
state = model.initSystem()

# Update kinematic states
for i in range(state.getQ().size()):
    state.getQ().set(i, Q[i])  # Q From inverse kinematics
    state.getQDot().set(i, QDot[i])  # QDot from inverse kinematics
model.realizeVelocity(state)

# Update muscle states
for i in range(model.getMuscles().getSize()):
    model.getMuscles().get(i).setActivation(state, muscle_excitation[i])  # Set to desired muscle excitation
model.equilibrateMuscles(state)


# DOESN'T WORK FROM HERE
# This seems to be the key, but is not interfaced in python
udot = model.getMatterSubsystem().getUDot(state)  # ERROR

# Another idea that seems promizing is:
udot = osim.Vector()
A_GB = osim.VectorOfSpatialVec()
udot = model.getMatterSubsystem().calcAcceleration(state, appliedMobilityForces, appliedBodyForces, udot, A_GB) # ERROR
# Because I don't know how to get appliedForces (even if I don't have external for now)
Thanks!
Last edited by Benjamin Michaud on Thu Oct 25, 2018 3:22 pm, edited 1 time in total.

User avatar
Christopher Dembia
Posts: 506
Joined: Fri Oct 12, 2012 4:09 pm

Re: Minimal example for forward dynamics in python

Post by Christopher Dembia » Tue Oct 23, 2018 2:36 pm

Have you seen the Python examples? https://github.com/opensim-org/opensim- ... n/examples

By joint torques and joint accelerations, do you mean net joint moments and generalized accelerations?

For generalized accelerations:

model.realizeAcceleration(state)
state.getUDot()

User avatar
Benjamin Michaud
Posts: 31
Joined: Mon May 03, 2010 6:35 am

Re: Minimal example for forward dynamics in python

Post by Benjamin Michaud » Tue Oct 23, 2018 2:52 pm

Hello!

Yep, we just figured out that it was updated in the state variable.

Just to make sure I fully understand this variable, this UDot is the generalized accelerations computed from muscle (meaning: it takes excitation/activation set with the 'setActivation' method from getMuscles().get(x), then converted into muscle forces [probably something like activation*max_force], which are afterward used to compute generalized joint torques and finally converted to generalized joint accelerations)?

If so, this is indeed what I need :)

User avatar
Christopher Dembia
Posts: 506
Joined: Fri Oct 12, 2012 4:09 pm

Re: Minimal example for forward dynamics in python

Post by Christopher Dembia » Tue Oct 23, 2018 3:46 pm

Yes. Note that at a given time point, activation directly affects muscle force but excitation does not. Excitation only affects muscle force through the forward integration of the dynamics.

POST REPLY