extract generaliseSpringForce from optimal solution

OpenSim Moco is a software toolkit to solve optimal control problems with musculoskeletal models defined in OpenSim using the direct collocation method.
POST REPLY
User avatar
Nicos Haralabidis
Posts: 190
Joined: Tue Aug 16, 2016 1:46 am

extract generaliseSpringForce from optimal solution

Post by Nicos Haralabidis » Thu Mar 12, 2020 11:53 am

Hi all,

We have attached six generalisedSpringForces to a 6DOF joint and performed a tracking simulation using MocoTrack, optimising stiffness and viscosity parameters simultaneously.

Once an optimal solution has been found, how can we obtain the generalisedSpringForces?

I should also add that we have included them as components, using the addComponent method.

Cheers,

Nicos

User avatar
Nicholas Bianco
Posts: 1012
Joined: Thu Oct 04, 2012 8:09 pm

Re: extract generaliseSpringForce from optimal solution

Post by Nicholas Bianco » Thu Mar 12, 2020 12:21 pm

Hi Nicos,

It's great you were able to find a solution optimizing spring parameters! How long did your simulation take?

To get SpringGeneralizedForce values, you can use

Code: Select all

forceArray = springGeneralizedForce.getRecordValues(state)
which returns an OpenSim Array (of length 1 in this case) of your force values. You need the SimTK::State realized to the velocity stage to call this method. To get the SimTK::State's from your solution, use the method

Code: Select all

statesTraj = solution.exportToStatesTrajectory(modelOrMocoProblem)
which takes a model or the MocoProblem as an argument.

-Nick

User avatar
Nicos Haralabidis
Posts: 190
Joined: Tue Aug 16, 2016 1:46 am

Re: extract generaliseSpringForce from optimal solution

Post by Nicos Haralabidis » Thu Mar 12, 2020 1:30 pm

Good evening Nick,

Thanks for the quick reply!

I should also add we have 4 muscles, 2 seconds of data to track, 201 mesh intervals, trapezoidal transcription, with 12 threads... takes just over 6 minutes!

I think I follow what you mean, but I guess I need a for-loop which updates the model's states from statesTraj and then I extract the generalisedSpringForce for each spring? Something like this:

% Example translational spring
% X Translation
spring_xT=SpringGeneralizedForce('xT_B1toB2');
spring_xT.setName('xT_B1toB2_SGF');
spring_xT.setStiffness(k_tra);
spring_xT.setViscosity(b_tra);
spring_xT.setRestLength(Lo_tra);
myModel.addComponent(spring_xT);

% Code omitted for brevity

myState = model.initSystem();

% Realize velocity
model.realizeVelocity(myState);

% Create states trajectory
statesTraj = TrackingSolution.exportToStatesTrajectory(problem);

for i = 0:num_mesh_intervals-1

% Set state variables
model.setStateVariableValues(myState,statesTraj(i)); % Is this possible?
% Extract force
force = spring_xT.getRecordValues(myState);
% Need to figure out how to make a Matlab vector of these forces from OpenSim Array

end

Cheers,

Nicos

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

Re: extract generaliseSpringForce from optimal solution

Post by Christopher Dembia » Thu Mar 12, 2020 2:57 pm

You should be able to use

Code: Select all

myState = statesTraj.get(i)
No need for `model.setStateVariableValues`.

User avatar
Nicos Haralabidis
Posts: 190
Joined: Tue Aug 16, 2016 1:46 am

Re: extract generaliseSpringForce from optimal solution

Post by Nicos Haralabidis » Fri Mar 13, 2020 2:15 am

Thanks for that Chris, it worked nicely!

For completeness and anyone else interested in getting the forces:

myModel = Model('6DOF_2Bodies_4Muscles.osim');

% Example translational spring
% X Translation
spring_xT=SpringGeneralizedForce('xT_B1toB2');
spring_xT.setName('xT_B1toB2_SGF');
spring_xT.setStiffness(k_tra);
spring_xT.setViscosity(b_tra);
spring_xT.setRestLength(Lo_tra);
myModel.addComponent(spring_xT);

% Code omitted for brevity

TrackingSolution = moco.solver();

statesTraj = TrackingSolution.exportStatesTrajectory(problem);

myState = myModel.initSystem();

for i = 0:statesTraj.getSize()-1
myState = statesTraj.get(i);
myModel.realizeVelocity(myState);
f_xT = spring_xT.getRecordValues(myState);
f_xT_vector(i+1) = f_xT.get(0);
end

Thanks!

Nicos

User avatar
Dhruv Gupta
Posts: 30
Joined: Sat Aug 29, 2015 11:23 am

Re: extract generaliseSpringForce from optimal solution

Post by Dhruv Gupta » Thu Mar 19, 2020 12:09 pm

Thanks Nicos, that helped! :D

POST REPLY