windowns + anaconda + python3.9+opensim 4.5
I rewrote the official example example2DWalkingMetabolics.cpp into Python, and it runs successfully. Based on that, I modified the model: I removed the left lower leg muscles from the 2D_gait.osim model and added a SpringGeneralizedForce at the left ankle joint.
I want to optimize the stiffness of this spring during the tracking process, so I used addParameter(osim.MocoParameter) to add the spring’s stiffness as an optimization parameter to the problem. However, when running the code with this parameter included, I encountered the following error:
RuntimeError: std::exception in 'OpenSim::MocoSolution OpenSim::MocoStudy::solve() const':
casVector should be 1-dimensional, but has size 0 x 0.
Thrown at MocoCasOCProblem.h:108 in convertToSimTKVector().
Through debugging, I found that if I do not use addParameter() to add the stiffness parameter, the code runs without issues. So I suspect this error is related to how the optimization parameter is defined.
Could anyone advise how to properly include the spring stiffness as an optimizable parameter in the Moco problem? Thank you!
mycode:
stiffness_param = osim.MocoParameter(
'prosthetic_stiffness',
'prosthetic',
'stiffness',
osim.MocoBounds(40.0, 60.0)
)
problem.addParameter(stiffness_param)
Additionally, I am wondering whether this issue might be caused by how I load the model. In the official example exampleOptimizeMass.py, the model is directly passed to the problem using problem.setModel(model), and the parameter optimization works as expected. However, in my code, I use:
modelProcessor = osim.ModelProcessor("my_modified_model.osim")
track.setModel(modelProcessor)
Could this error be due to using ModelProcessor instead of directly passing a Model? If so, how should I correctly define and optimize model parameters like spring stiffness when using a ModelProcessor?
Also, I'm using MocoTrack, not MocoStudy directly. Could it be that MocoTrack does not handle MocoParameter in the same way as MocoStudy does? Is there a recommended way to optimize model parameters (e.g., spring stiffness) when using MocoTrack?
Any help or insight would be greatly appreciated!