Page 1 of 1

Error when Adding a MocoParameter in Python

Posted: Tue Mar 14, 2023 12:34 pm
by gtuer51
Hello,

I am running a Moco state tracking problem through the Python API and am getting an error when trying to add a MocoParameter to the problem. I started with a basic test of an example from the class documentation to make sure I have the setup right, and I am already having issues.

Code Snippet:

Code: Select all

# Inside custom class for managing the MocoStudy
# Any variables that aren't defined here are defined earlier in the code
study = osim.MocoStudy()
# model processing
...
# Problem definition
problem = study.updProblem()
problem.setModelAsCopy(self.model)
problem.setTimeBounds(self.initialTime, self.finalTime)
# Test MocoParameter with example for Matlab from the documentation
param = osim.MocoParameter('soleus_r_fiber_length', '/forceset/soleus_r', 'optimal_fiber_length', osim.MocoBounds(0.04, 0.06))
problem.addParameter(param)
# Some additional setup, then study.solve()
Without the two lines involving the "param" variable, everything runs as expected. After adding those lines, the program seems to get hung up after printing details about the Jacobian and Lagrangian Hessian values:
MocoParameterHangup.PNG
MocoParameterHangup.PNG (31.13 KiB) Viewed 212 times
Am I using/implementing the MocoParameter correctly? Is there something I should be doing in addition/instead of what I am currently doing to get OpenSim to optimize a variable like this? Please let me know if you have any suggestions for me to improve/correct my setup.

For reference, I am using the Subject 14, no load, free speed, trial 2 data from this dataset: https://simtk.org/projects/assistloadwalk#. The parameter ranges were set to their extrema in the input kinematics +/- 5% of the range in the input data to allow some variance within the simulation.
Adjustments I made to the model:
1. In an attempt to get better foot-ground contact measurements from Moco, I unlocked the mtp and subtalar joints, repositioned toe-based markers to have the corresponding toe body as their parent, and reran IK with the new model.
2. I added contact spheres and forces according to the configuration provided here: https://simtk.org/projects/umocod. I scaled the positions of the spheres based on the corresponding scale factor of the parent body. I scaled the radii of the contact spheres based on the cube root of the product of the parent body's scale factors.
3. I used the ModOpReplaceMusclesWithDeGrooteFregly2016() model operator method to prepare the model for use with Moco.

I've attached the Moco setup file (comments removed to reduce size enough to attach) to provide additional information on the problem I am attempting to run.

Re: Error when Adding a MocoParameter in Python

Posted: Tue Mar 14, 2023 2:52 pm
by nbianco
Hi Garrett,

If you're using MocoCasADiSolver, then you you want to add the following line during the solver initialization:

Code: Select all

solver.set_parameters_require_initsystem(false);
This tells MocoCasADiSolver that it does not need to call initSystem() on the model internal to the problem on every iteration, which tends to slow down the problem massively. In other words, your problem isn't "hanging" it's just running very slow because of the internal initSystem() calls.

For many parameters in classes in OpenSim (e.g., optimal fiber length in a Muscle), it's fine to ignore initSystem(). But if you want to modify a parameter that changes the underlying system in Simbody (e.g., the mass of a body), then an initSystem() call is required.

Best,
Nick

Re: Error when Adding a MocoParameter in Python

Posted: Wed Mar 15, 2023 12:05 pm
by gtuer51
Excellent, that fixed the issue. Thank you, Nick!