4 ms·
The naive integrator you're describing, just taking the differential equations and substituting dt by a small, non-zero time step, is known as the Euler integra
by rhaps0dy 6y ago
The naive integrator you're describing, just taking the differential equations and substituting dt by a small, non-zero time step, is known as the Euler integrator [1]. As you say, this scheme does not keep energy invariant. In fact, energy may grow unbounded if you use it.
The simplest integrator that "preserves" energy is the Symplectic Euler [2] integrator, which reads:
v <- v + F/m * dt
x <- x + v * dt
The key is that you use the updated velocity to update the position in the same time step. In this scheme, energy isn't exactly preserved, it varies a little bit from one time step to the next. However, you can guarantee that the energy fluctuations will remain bounded, if your time step is below a (fairly large) critical value.
Reading the code of this blog post, the first version of the Step (at the beginning of the blogpost) uses the Symplectic Euler scheme, and the second version (towards the end) uses the naive Euler scheme. Bullet, a popular physics engine, uses the Symplectic Euler method [3].
A popular, more sophisticated, scheme is the Runge-Kutta integrator, which comes with variants that preserve the energy and ones that don't. The MuJoCo simulator, used in robotics research, uses the Runge-Kutta method [4]
[1] https://en.wikipedia.org/wiki/Euler_method https://en.wikipedia.org/wiki/Euler_method
[2] https://en.wikipedia.org/wiki/Semi-implicit_Euler_method https://en.wikipedia.org/wiki/Semi-implicit_Euler_method
[3] https://github.com/bulletphysics/bullet3/issues/2513 https://github.com/bulletphysics/bullet3/issues/2513
[4] http://www.mujoco.org/book/computation.html http://www.mujoco.org/book/computation.html
- IainWinter 6y agoOh you're correct, I was trying to make the code more readable for the blog post and didn't even think of that! Thanks for the heads up I would of never caught that, and good info about different integrators, not going to pretend that I knew all of it. It makes sense though because the velocity used for the position would be a frame behind. I was looking into the Runge-Kutta method, but it looked too complex for what I was doing.
- sgillen 6y agoI think all of OpenAI’s gym environments use Mujoco with rk4, but the engine supports both rk4 and semi implicit euler, and the default is actually Euler. Seems like that is the default for almost all robot focused sims too, see some benchmarks here: https://leggedrobotics.github.io/SimBenchmark/ https://leggedrobotics.github.io/SimBenchmark/