Matlab version of example2DWalking
- Karthick Ganesan
- Posts: 119
- Joined: Thu Oct 10, 2013 12:11 am
Re: Matlab version of example2DWalking
Hi Aaron,
Yes. Implicit mode was faster (almost twice) than explicit mode.
Regards,
Karthick.
Yes. Implicit mode was faster (almost twice) than explicit mode.
Regards,
Karthick.
- Brian Umberger
- Posts: 48
- Joined: Tue Aug 28, 2007 2:03 pm
Re: Matlab version of example2DWalking
Hi All,
With regards to Aaron's last question, the best source for understanding the implicit formulation and its numerical advantages over the typical explicit formulation for the EOM is this paper by Ton van den Bogert:
https://www.ncbi.nlm.nih.gov/pubmed/22102983
and for the muscle model equations specifically this paper by Friedl De Groote:
https://www.ncbi.nlm.nih.gov/pubmed/27001399
There was also some discussion about this issue on the main OpenSim forum several years ago:
viewtopicPhpbb.php?f=91&t=3932&p=15988&start=0&view=
Hope this help.
Brian
With regards to Aaron's last question, the best source for understanding the implicit formulation and its numerical advantages over the typical explicit formulation for the EOM is this paper by Ton van den Bogert:
https://www.ncbi.nlm.nih.gov/pubmed/22102983
and for the muscle model equations specifically this paper by Friedl De Groote:
https://www.ncbi.nlm.nih.gov/pubmed/27001399
There was also some discussion about this issue on the main OpenSim forum several years ago:
viewtopicPhpbb.php?f=91&t=3932&p=15988&start=0&view=
Hope this help.
Brian
Re: Matlab version of example2DWalking
Thanks for those resources Brian - extremely useful.
It would be good if Chris/Nick could confirm exactly what the implicit formulation is in Moco (i.e. does it follow the process in those two papers mentioned)? I'm also wondering what/whether there are any negatives to the implicit formulation? The referred papers seem to suggest that convergence is better and quicker, while still producing good solutions - so is there a reason for using the explicit mode?
It's also good to look back at an old thread and see Chris talking about embedding direct collocation in OpenSim 8 years ago! Glad he followed through!
Aaron
It would be good if Chris/Nick could confirm exactly what the implicit formulation is in Moco (i.e. does it follow the process in those two papers mentioned)? I'm also wondering what/whether there are any negatives to the implicit formulation? The referred papers seem to suggest that convergence is better and quicker, while still producing good solutions - so is there a reason for using the explicit mode?
It's also good to look back at an old thread and see Chris talking about embedding direct collocation in OpenSim 8 years ago! Glad he followed through!
Aaron
- Nicholas Bianco
- Posts: 1041
- Joined: Thu Oct 04, 2012 8:09 pm
Re: Matlab version of example2DWalking
Hi Aaron,
We use the tendon-force-state implicit tendon compliance dynamics formulation in Moco pretty much as described in Freidl's paper. We've also tried the explicit dynamics formulation for tendon dynamics, but without much success so far.
One major difference in our implicit multibody dynamics formulation from Ton's paper is that we introduce continuous variables for coordinate accelerations as controls to the problem, while he uses finite differences to compute state derivatives (see the second argument of the implicit dynamics function in Equation 18 of that paper). Our formulation is similar to recent work by BJ Fregly and Antoine Falisse, and it works pretty well. However, it is often necessary to minimize these added acceleration variables (usually with a low cost weight, so your main cost terms dominate) in order see the computational benefit from the implicit formulation. Therefore, additional consideration is required for this extra "regularization" cost term, and there is also some argument to whether or not minimizing accelerations like this is physically valid. But even if you don't introduce additional variables, most problems still benefit from some sort of regularization on the all the state and control continuous variable; for example, Ton computes finite differences for all continuous variables (I think omitting dividing by the time step) and minimizes these differences with a small cost function weight.
We don't have any definitive examples in Moco showing that implicit dynamics is better than explicit dynamics yet, but we generally have better luck using implicit dynamics. Anecdotally, I've sometimes had better luck using explicit dynamics when doing coordinate tracking problems, but it's not consistently better. If you wish to use implicit dynamics in Moco, these MocoSolver commands will be helpful:
The setting 'set_multibody_dynamics_mode('implicit')' enables the implicit multibody dynamics formulation and introduces the additional acceleration variables. The following commands allow you to enable minimization of accelerations, set the weight, and set the bounds on the acceleration variables. There are commands for minimizing additional variables introduced for implicit tendon dynamics ('set_minimize_implicit_auxiliary_derivatives()' and 'set_implicit_auxiliary_derivatives_weight()'). Note that there is no command to enable implicit dynamics for muscles, since that is enabled via a property of DeGrooteFregly2016Muscle (see 'tendon_compliance_dynamics_mode()'). This means that you can use explicit multibody dynamics and implicit muscle dynamics and vice versa; they are not tied together. For implicit tendon compliance dynamics, the added control variable is the derivative of normalized tendon force since the state variable is normalized tendon force.
Finally, if you want all the nitty-gritty details of both implicit and explicit formulations in Moco check out the Theory Guide page in the docs: https://opensim-org.github.io/opensim-moco-site/docs/.
-Nick
We use the tendon-force-state implicit tendon compliance dynamics formulation in Moco pretty much as described in Freidl's paper. We've also tried the explicit dynamics formulation for tendon dynamics, but without much success so far.
One major difference in our implicit multibody dynamics formulation from Ton's paper is that we introduce continuous variables for coordinate accelerations as controls to the problem, while he uses finite differences to compute state derivatives (see the second argument of the implicit dynamics function in Equation 18 of that paper). Our formulation is similar to recent work by BJ Fregly and Antoine Falisse, and it works pretty well. However, it is often necessary to minimize these added acceleration variables (usually with a low cost weight, so your main cost terms dominate) in order see the computational benefit from the implicit formulation. Therefore, additional consideration is required for this extra "regularization" cost term, and there is also some argument to whether or not minimizing accelerations like this is physically valid. But even if you don't introduce additional variables, most problems still benefit from some sort of regularization on the all the state and control continuous variable; for example, Ton computes finite differences for all continuous variables (I think omitting dividing by the time step) and minimizes these differences with a small cost function weight.
We don't have any definitive examples in Moco showing that implicit dynamics is better than explicit dynamics yet, but we generally have better luck using implicit dynamics. Anecdotally, I've sometimes had better luck using explicit dynamics when doing coordinate tracking problems, but it's not consistently better. If you wish to use implicit dynamics in Moco, these MocoSolver commands will be helpful:
Code: Select all
solver = study.initCasADiSolver()
solver.set_multibody_dynamics_mode('implicit')
solver.set_minimize_implicit_multibody_accelerations(true)
solver.set_implicit_multibody_accelerations_weight(0.001)
solver.set_implicit_multibody_acceleration_bounds(MocoBounds(-200, 200))
solver.set_minimize_implicit_auxiliary_derivatives(true)
solver.set_implicit_auxiliary_derivatives_weight(0.01)
Finally, if you want all the nitty-gritty details of both implicit and explicit formulations in Moco check out the Theory Guide page in the docs: https://opensim-org.github.io/opensim-moco-site/docs/.
-Nick
- Ross Miller
- Posts: 375
- Joined: Tue Sep 22, 2009 2:02 pm
Re: Matlab version of example2DWalking
Hi all,
I recently started playing around with Moco (nothing "research-grade" yet) and was directed to this thread by Brian as I attempt to include tendon compliance in the walking example. Some remarks/questions:
- Moco is super-impressive! Very fast and easy to use. I will never go back to custom DC codes.
- "Predictive" simulations of gait (no tracking term in cost function) always seem to take a lot more iterations to converge in IPOPT than "tracking" simulations and are much less predictable in how long they take. This is the case in my own codes too (not just Moco) and for everyone else I know who uses collocation methods. I don't know why this is, it doesn't make sense to me and I've never found a solution that makes any practical difference.
-Something I've found that often decreases the convergence time greatly with DC is doing mesh refinement, e.g. get a solution on a coarse grid and use it as the initial guess for a less-coarse grid, repeat this process iteratively until on the desired grid. Is this possible in Moco? I guess it would be easy to do manually.
- Including compliant tendons in Moco does not seem to need any periodicity constraints on the muscle contractile state variable (fiber length, or perhaps force), is this correct and expected?
- I would caution very strongly against ignoring tendon compliance for any problems considering muscle energetics, even if it doesn't make a big difference in muscle force.
Ross
I recently started playing around with Moco (nothing "research-grade" yet) and was directed to this thread by Brian as I attempt to include tendon compliance in the walking example. Some remarks/questions:
- Moco is super-impressive! Very fast and easy to use. I will never go back to custom DC codes.
- "Predictive" simulations of gait (no tracking term in cost function) always seem to take a lot more iterations to converge in IPOPT than "tracking" simulations and are much less predictable in how long they take. This is the case in my own codes too (not just Moco) and for everyone else I know who uses collocation methods. I don't know why this is, it doesn't make sense to me and I've never found a solution that makes any practical difference.
-Something I've found that often decreases the convergence time greatly with DC is doing mesh refinement, e.g. get a solution on a coarse grid and use it as the initial guess for a less-coarse grid, repeat this process iteratively until on the desired grid. Is this possible in Moco? I guess it would be easy to do manually.
- Including compliant tendons in Moco does not seem to need any periodicity constraints on the muscle contractile state variable (fiber length, or perhaps force), is this correct and expected?
- I would caution very strongly against ignoring tendon compliance for any problems considering muscle energetics, even if it doesn't make a big difference in muscle force.
Ross
- Ross Miller
- Posts: 375
- Joined: Tue Sep 22, 2009 2:02 pm
Re: Matlab version of example2DWalking
Sorry, I think I'm mistaken on the muscle force periodicity remark (the forces were not periodic in the solution from the code earlier in this thread).
Ross
Ross
Re: Matlab version of example2DWalking
Hi Ross,
A small answer to one of your queries - I used a mesh refinement approach recently (on an upper limb, not a gait problem) and the approach is pretty simple in Moco. It is somewhat manual, but you can reuse the problem/study that you create in the first place, and then simply loop through consecutive iterations changing two things in the solver: 1) the mesh interval; and 2) the initial guess (setting this to the previous solution).
After setting up the problem/study, the iterative process for the mesh refinement is really only a few lines of code.
Aaron
A small answer to one of your queries - I used a mesh refinement approach recently (on an upper limb, not a gait problem) and the approach is pretty simple in Moco. It is somewhat manual, but you can reuse the problem/study that you create in the first place, and then simply loop through consecutive iterations changing two things in the solver: 1) the mesh interval; and 2) the initial guess (setting this to the previous solution).
After setting up the problem/study, the iterative process for the mesh refinement is really only a few lines of code.
Aaron
- Brian Umberger
- Posts: 48
- Joined: Tue Aug 28, 2007 2:03 pm
Re: Matlab version of example2DWalking
Hi Ross,
You can make the tendon forces periodic in the Matlab example with something like this
Or embed this within one of the existing periodicity loops.
Best,
Brian
You can make the tendon forces periodic in the Matlab example with something like this
Code: Select all
for i = 1:model.getNumStateVariables()
currentStateName = string(model.getStateVariableNames().getitem(i-1));
if endsWith(currentStateName,'/normalized_tendon_force')
if contains(currentStateName,'_r')
symmetryGoal.addStatePair(MocoPeriodicityGoalPair(currentStateName, ...
regexprep(currentStateName,'_r','_l')));
end
if contains(currentStateName,'_l')
symmetryGoal.addStatePair(MocoPeriodicityGoalPair(currentStateName, ...
regexprep(currentStateName,'_l','_r')));
end
end
end
Best,
Brian
- Ross Miller
- Posts: 375
- Joined: Tue Sep 22, 2009 2:02 pm
Re: Matlab version of example2DWalking
Thanks all
- Ross Miller
- Posts: 375
- Joined: Tue Sep 22, 2009 2:02 pm
Re: Matlab version of example2DWalking
One more question (actually two questions but they are related), for now!
(1) All the muscles in this model have a polynomial length function specified in the .osim file's XML. Can OpenSim/Moco generate these polynomial coefficients for muscles that do not yet have these functions defined, e.g. if you manually add a muscle to this model and want to specify its polynomial length approximation without knowing it ahead of time?
(2) When I take an existing .osim model with Millard2012Equilibrium muscles and replace them with DeGrooteFregly2016 muscles:
the muscles do not appear to have been replaced in the new .osim file's XML code, they are still Millard muscles. Relatedly, when I load then print the model that has polynomial length paths specified, in the printed model's XML code it looks like the polynomial length approximations have been removed (all <length_approximation> tags are gone). Am I doing this process incorrectly?
Ross
(1) All the muscles in this model have a polynomial length function specified in the .osim file's XML. Can OpenSim/Moco generate these polynomial coefficients for muscles that do not yet have these functions defined, e.g. if you manually add a muscle to this model and want to specify its polynomial length approximation without knowing it ahead of time?
(2) When I take an existing .osim model with Millard2012Equilibrium muscles and replace them with DeGrooteFregly2016 muscles:
Code: Select all
import org.opensim.modeling.*
modelProcessor = ModelProcessor(<old .osim filename here>);
modelProcessor.append(ModOpReplaceMusclesWithDeGrooteFregly2016());
model = modelProcessor.process();
model.print(<new .osim filename here>);
Ross