← All projects
Independent research · September 2024

N-Body Simulations and AI Optimized Orbits: Part I

Dadmehr Ghasemfar

The ultimate goal of this two-part project is to calculate optimal mission profiles for interplanetary journeys. In part one, we develop a robust and accurate N-body simulation. It is written for parallel processing on the GPU, running at 30+ FPS with up to 400 bodies. In addition, the simulation supports a complete GUI package to visualize the system dynamics. RK-4 was overall the most effective integrator when accounting for runtime and accuracy.

Snapshots of a randomized test with N=50 particles.
Snapshots of a randomized test with N=50 particles.

Background

For this project, we use the Newtonian model of gravitation and take several simplifying conditions: we assume that all objects are point masses, we neglect all tidal effects, and we disregard the force of solar radiation. We also neglect the effects of non-spherical objects on orbits.

To solve the system of second order differential equations, we must know the initial positions and velocities of all the bodies at some initial time. The values for all the planets in the solar system (in addition to our moon) are collected from the JPL DE-430 ephemerides package, using the Python Astropy package. To run the solver and GUI on the GPU, we use the Taichi package in Python.

Results

For simplicity, we use the Euclidean error in the position of Earth to validate the simulation. We expect this to increase with the simulated frames, and generally it does. However, because of the cyclical nature of orbits, the error often seems to drop as the simulated and real positions happen to line up. To get around this, we track the average and maximum error over a period of time.

Position error vs time for five planets
Error over five years for five planets with the forward Euler integrator and a time step of one day. There is greater error in objects within close proximity (Venus, Earth, and Mars) than with distant and isolated objects.

Both the Runge-Kutta methods and the forward Euler method worked exactly as expected. The other two methods were unstable.

Global error vs time step for each integrator
Average and maximum global errors for each integrator, for time steps from fifteen minutes to six months.
Runtime vs time step for each integrator
Program runtime vs time step for a one year simulation window.

Working Integrators

Forward Euler is the simplest integrator. At each time frame, we evaluate the net gravitational force on all the particles and use it to determine the acceleration and then the velocity and position. Its global error is O(dt), and the biggest issue with it as a long-term integrator is that it is non-symplectic, meaning that it does not conserve energy over time.

RK4 is a widely used integrator with improved accuracy over the Euler integrator. At each time step, it calculates four intermediate slopes to estimate the slope at the next time step, with a global error of O(dt⁴). While it's not symplectic, its balance of accuracy and computational efficiency makes it popular for many applications. RK5 is similar, but with another point incorporated into the approximation.

Failed Integrators

The backward Euler method is an implicit integrator, solved here with a fixed-point iteration. The implementation was ultimately unstable. First, a few of the objects would get flung out of the solar system. After that, the planar orbits start to precess out of the plane of the solar system.

The third order Adams-Bashforth method is an explicit linear multistep method. Although its implementation was also unsuccessful, it demonstrated greater stability than backward Euler. At first, everything appears as expected. After some time the system collapses: some bodies slowly lose energy and spiral into the Sun, while others get flung into outer space.

Failed solar system tests
The failed solar system tests for the backward Euler and Adams-Bashforth integrators.