Step by step integration

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Andrew Stolin
Posts: 12
Joined: Thu Mar 05, 2015 9:41 am

Step by step integration

Post by Andrew Stolin » Sun Jun 03, 2018 9:40 am

Hi,

I am trying to perform a simulation step by step in python. The results differ between a 0.2s integration and two 0.1s integrations.

Would you be able to tell me if there is something wrong in these few lines of code?

I am using the build_simple_arm_model.py" example.

The controller

Code: Select all

brain = osim.PrescribedController()
brain.addActuator(biceps)
func = osim.Constant(1.0)
brain.prescribeControlForActuator("biceps", func)
1 step (0.2)

Code: Select all

manager = osim.Manager(arm)
state.setTime(0)
manager.initialize(state)
state = manager.integrate(0.2)
The check

Code: Select all

print(elbow.getCoordinate().getValue(state))
print(biceps.getActivation(arm.initSystem()))
print(biceps.getExcitation(arm.initSystem()))
gives me :
1.5007997946757727
1.0
0.9999787865083376

2 steps (0.1 then 0.1)

Code: Select all

manager = osim.Manager(arm)
state.setTime(0)
manager.initialize(state)
state = manager.integrate(0.1)
state.setTime(0)
state = manager.integrate(0.1) # I believe that the manager.integrate is always based on the current state
The check
gives me :
1.5007492993654519
1.0
0.999978786034814

Any idea?

Tags:

User avatar
Thomas Uchida
Posts: 1777
Joined: Wed May 16, 2012 11:40 am

Re: Step by step integration

Post by Thomas Uchida » Wed Jun 06, 2018 11:24 pm

The results differ between a 0.2s integration and two 0.1s integrations.
This behavior is expected. The Manager uses a SimTK::RungeKuttaMersonIntegrator, which is a variable-time-step integrator. The differences you're seeing are about 5e-5 and 5e-10, which are likely below the integrator's default tolerance and would be explained by the integrator choosing different time step sizes depending on whether you're forcing it to stop exactly half-way through.

User avatar
Andrew Stolin
Posts: 12
Joined: Thu Mar 05, 2015 9:41 am

Re: Step by step integration

Post by Andrew Stolin » Thu Jun 07, 2018 3:02 am

Thank you very much for this information, I did not think of the integrator's default tolerance.

Just to confirm, does it mean that when I re-integrate, all the controls have been preserved by default (i.e. I am integrating from the last state of the last integration, and not from the beginning) ?

Code: Select all

manager = osim.Manager(arm)
manager.initialize(state)
arm.initializeState()
state.setTime(0)
state = manager.integrate(3.0)

# second integration
arm.initializeState()
state.setTime(0)
state = manager.integrate(3.0)
As a side note, I am not sure why we have to setTime to 0 every time in order to do more than two successive integrations.

User avatar
Thomas Uchida
Posts: 1777
Joined: Wed May 16, 2012 11:40 am

Re: Step by step integration

Post by Thomas Uchida » Thu Jun 07, 2018 3:54 am

does it mean that when I re-integrate, all the controls have been preserved by default
Not sure, but it seems like it would be relatively easy to test.
I am not sure why we have to setTime to 0 every time in order to do more than two successive integrations.
Does Manager::resetTimeAndDTArrays() not work? You might also want to check TestManager.cpp for examples (https://github.com/opensim-org/opensim- ... anager.cpp).

User avatar
Hide Kimpara
Posts: 135
Joined: Mon Sep 19, 2016 5:12 am

Re: Step by step integration

Post by Hide Kimpara » Fri Jun 08, 2018 12:13 pm

Hello Andrew,
manager.initialize(state)
state = manager.integrate(0.1)
state.setTime(0)
state = manager.integrate(0.1) # I believe that the manager.integrate is always based on the current state
As you thinking, manager.integrate starts from current state time. Therefore, this mentioned code looks like twice integration of 0 >> 0.1 [s].
If you are willing to 2 step integrations such as 0.0 >> 0.1 [s], and 0.1 >> 0.2 [s], you may do integrations as following:

Code: Select all

state.setTime(0)
manager.initialize(state)
state = manager.integrate(0.1)
state = manager.integrate(0.2)
if you are willing to see the time change in opensim's state variable, you can use "state.getTime()" just after integration.

I hope this would be help for you.

Hide

User avatar
Andrew Stolin
Posts: 12
Joined: Thu Mar 05, 2015 9:41 am

Re: Step by step integration

Post by Andrew Stolin » Sun Jul 01, 2018 3:29 am

Thank you very much, Thomas and Hide - everything seems to be working now

POST REPLY