Recently, I designed an n-body engine and tested it with a binary star system. After a minute of running, the orbit began noticeably expanding:

enter image description here

Why is it happening, and is this natural? The source code is based on https://github.com/lemmingapex/N-bodyGravitySimulation.

  • $\begingroup$ Did you use an Euler integration scheme? If so, you probably want to use a Leapfrog. The latter will conserve energy and angular momentum on average, the former doesn't. Welcome to the wondrous world of numerical artifacts... $\endgroup$ May 5 at 16:55
  • $\begingroup$ That definitely looks like energy conservation failure due to a non-symplectic integrator. If you try a smaller time step, and less eccentricity, it will improve things, but eventually stuff will start spiralling. I also recommend Leapfrog, especially the variation by Yoshida (briefly described on the Wikipedia page). You could also use Verlet, which is also a symplectic integrator, like Leapfrog. That readme of that GitHub repo says it has Verlet. $\endgroup$
    – PM 2Ring
    May 5 at 17:13
  • $\begingroup$ @AtmosphericPrisonEscape I am using Runge-Kutta 4 as my integrator. $\endgroup$ May 5 at 17:54
  • $\begingroup$ Take into account what we said. RK4 can still easily fail with large timesteps. Don't start with a binary, simplify even more, do a extreme mass-ratio binary (i.e. testmass and star). Then check that a circular and an elliptic orbit are conserved. Once you have established that, you can go and complicate things again. Also see how things change when using the verlet integrator. If things look odd with verlet, then the program you are using is inherently flawed. $\endgroup$ May 5 at 21:30
  • $\begingroup$ @AtmosphericPrisonEscape Turns out, objects in highly eccentric orbits tend to have massive changes after periapsis (both increase and decrease). I think the timestep is too high, which is causing the issues. Any workaround? $\endgroup$ May 5 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.