Page 1 of 1

extract generaliseSpringForce from optimal solution

Posted: Thu Mar 12, 2020 11:53 am
by nicos1993
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

Re: extract generaliseSpringForce from optimal solution

Posted: Thu Mar 12, 2020 12:21 pm
by nbianco
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

Re: extract generaliseSpringForce from optimal solution

Posted: Thu Mar 12, 2020 1:30 pm
by nicos1993
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

Re: extract generaliseSpringForce from optimal solution

Posted: Thu Mar 12, 2020 2:57 pm
by chrisdembia
You should be able to use

Code: Select all

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

Re: extract generaliseSpringForce from optimal solution

Posted: Fri Mar 13, 2020 2:15 am
by nicos1993
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

Re: extract generaliseSpringForce from optimal solution

Posted: Thu Mar 19, 2020 12:09 pm
by dhruv
Thanks Nicos, that helped! :D