Simulation software for multi-body pendulums using Runge-Kutta 4, and with vizualization of the pendulum:
and the energy:
My notes/derivation of the governing system of equations for this system can be found here.
pip install multibodypendulum
A quick usecase to simulate a 5 pendulum system could look like this
import multibodypendulum as mbp
n = 5
dt = 0.001
g = 9.82
model = mbp.MultiBodyPendulum(n, dt,g=g)
theta0 = 0.5*math.pi*torch.ones(n)
dtheta0 = 0.0*torch.ones(n)
nsteps = 100
times, thetas, dthetas = model.simulate(nsteps,theta0,dtheta0)
model.plot_energy()
model.animate_pendulum()
The code has some limitations:
- The simulation assumes pendulums of mass 1 kg, and length between pendulums of 1 m. These assumptions significantly speeds up the simulation and was all I needed for my usecase.
- The simulation uses pytorch, but could easily be ported to numpy, which would make it easier to use for a lot of people. I haven't done this since I primarily use pytorch so it doesn't matter to me, but if others need it, it could be a nice contribution project.