Retrieve contact forces and Lagrange multipliers

OpenSim Moco is a software toolkit to solve optimal control problems with musculoskeletal models defined in OpenSim using the direct collocation method.
POST REPLY
User avatar
Bach Quoc Hoa
Posts: 24
Joined: Thu Jul 13, 2017 7:03 am

Retrieve contact forces and Lagrange multipliers

Post by Bach Quoc Hoa » Tue Feb 04, 2020 5:55 am

Hello,
How can I retrieve contact forces and Lagrange multipliers after the problem has been solved?
Thank you in advance!

User avatar
Christopher Dembia
Posts: 506
Joined: Fri Oct 12, 2012 4:09 pm

Re: Retrieve contact forces and Lagrange multipliers

Post by Christopher Dembia » Tue Feb 04, 2020 9:22 am

See this snippet from example2DWalking: https://github.com/opensim-org/opensim- ... #L288-L299

Lagrange multipliers are available in the MocoSolution, via getMultiplier() and related functions.

User avatar
Bach Quoc Hoa
Posts: 24
Joined: Thu Jul 13, 2017 7:03 am

Re: Retrieve contact forces and Lagrange multipliers

Post by Bach Quoc Hoa » Thu Feb 06, 2020 1:33 am

With the snippet of code from example2DWalking, it doesn't work even the system was already realized at Dynamics stage.
terminate called after throwing an instance of 'SimTK::Exception::StageTooLow'
what(): SimTK Exception thrown at GeneralContactSubsystem.cpp:131:
Expected stage to be at least Dynamics in GeneralContactSubsystemImpl::getContacts() but current stage was Velocity
Another question : If I run a forward dynamics simulation with OpenSim::Manager and use the forces previously obtained as inputs, will the contact forces and the multipliers be the same as those in the previous optimal control simulation?

User avatar
Christopher Dembia
Posts: 506
Joined: Fri Oct 12, 2012 4:09 pm

Re: Retrieve contact forces and Lagrange multipliers

Post by Christopher Dembia » Thu Feb 06, 2020 11:37 am

With the snippet of code from example2DWalking, it doesn't work even the system was already realized at Dynamics stage.
Without seieng your code, I'm not sure why you get this error. The example works as-is, so perhaps you are doing something different from the example?
If I run a forward dynamics simulation with OpenSim::Manager and use the forces previously obtained as inputs, will the contact forces and the multipliers be the same as those in the previous optimal control simulation?
For certain simple systems, yes. In general, no. Numerical errors due to differences between the integration schemes will cause the nonlinear chaotic system to diverge. We touch on this in the Moco bioRxiv preprint.

In general, you need to stabilize the control using a feedback controller such as LQR.

User avatar
Bach Quoc Hoa
Posts: 24
Joined: Thu Jul 13, 2017 7:03 am

Re: Retrieve contact forces and Lagrange multipliers

Post by Bach Quoc Hoa » Fri Feb 07, 2020 5:40 am

I found the issue!
It was

Code: Select all

State& _state = _model.initSystem(); 
while it should be

Code: Select all

_model.initSystem(); 
However, it only works with SmoothSphereHalfSpaceForce. The error remains with ElasticFoundationForce.

But why does that make the system not realized?
Last edited by Bach Quoc Hoa on Tue Feb 11, 2020 1:29 pm, edited 1 time in total.

User avatar
Bach Quoc Hoa
Posts: 24
Joined: Thu Jul 13, 2017 7:03 am

Re: Retrieve contact forces and Lagrange multipliers

Post by Bach Quoc Hoa » Fri Feb 07, 2020 10:40 am

I have encountered this problem while using Manager and a model containing SmoothSphereHalfSpaceForce (attached file).
Encountered unrecognized Object typename SmoothSphereHalfSpaceForce while reading property components. There is no registered Object of this type; ignoring.
There is just no contact and the model falls right through the ground. How can I register this type of contact force?

Solution
It seems mandatory to use at least one feature of Moco library, such as declare a void MocoProblem or call visualize(model, storage), so that the MocoComponents can be taken into account.

POST REPLY