This code numerically solves the n-body problem using the leapfrog algorithm, a time-reversible symplectic integrator.
Clone and make
.
./orbit file
, where file
contains the number of "planets", their masses and their initial positions and velocities.
Number of planets
Mass of planet
x y v_x v_y
See examples/test2.dat
and examples/test3.dat
for examples (with stable orbits for n = 2, n = 3 respectively). It prints all output to stdout
in the format
x_0 y_0 x_1 y_1 ... x_n y_n
for each timestep, where (xi, yi) is the coordinate of the ith planet in the inertial reference frame of the 0th planet (under a coordinate transformation sending the 0th planet to the origin).
The energy and angular momentum of the system are also calculated with energy()
, angularmomentum()
(commented in main()
, ll 153, 154).