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
extract generaliseSpringForce from optimal solution
- Nicos Haralabidis
- Posts: 205
- Joined: Tue Aug 16, 2016 1:46 am
- Nicholas Bianco
- Posts: 1078
- Joined: Thu Oct 04, 2012 8:09 pm
Re: extract generaliseSpringForce from optimal solution
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 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 which takes a model or the MocoProblem as an argument.
-Nick
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)
Code: Select all
statesTraj = solution.exportToStatesTrajectory(modelOrMocoProblem)
-Nick
- Nicos Haralabidis
- Posts: 205
- Joined: Tue Aug 16, 2016 1:46 am
Re: extract generaliseSpringForce from optimal solution
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
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
- Christopher Dembia
- Posts: 506
- Joined: Fri Oct 12, 2012 4:09 pm
Re: extract generaliseSpringForce from optimal solution
You should be able to use
No need for `model.setStateVariableValues`.
Code: Select all
myState = statesTraj.get(i)
- Nicos Haralabidis
- Posts: 205
- Joined: Tue Aug 16, 2016 1:46 am
Re: extract generaliseSpringForce from optimal solution
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
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
- Dhruv Gupta
- Posts: 30
- Joined: Sat Aug 29, 2015 11:23 am
Re: extract generaliseSpringForce from optimal solution
Thanks Nicos, that helped! 
