The Equations Behind NBody


###The physics

Here we review the equations governing the motion of the particles according to Newton’s laws of motion and gravitation. Don’t worry if your grounding in physics is a bit rusty or even nonexistent; all of the necessary formulas are included below. We’ll assume for now that the position (px, py) and velocity (vx, vy) of each particle is known. In order to model the dynamics of the system, we must know the net force exerted on each particle.

###The numerics

We use the leapfrog finite difference approximation scheme to numerically integrate the above equations: this is the basis for most astrophysical simulations of gravitational systems. In the leapfrog scheme, we discretize time, and update the time variable t in increments of the time quantum Δt. We maintain the position (px, py) and velocity (vx, vy) of each particle at each time step. The steps below illustrate how to evolve the positions and velocities of the particles.

  1. Calculate forces
    For each particle:
    Calculate the net force (Fx, Fy) at the current time t acting on that particle using Newton's law of gravitation and the principle of superposition.

  2. Update positions, velocities, and acceleration
    For each particle:
    1. Calculate its acceleration (ax, ay) at time t using the net force computed in Step 1 and Newton's second law of motion: ax = Fx / m, ay = Fy / m.
    2. Calculate its new velocity (vx, vy) at the next time step by using the acceleration computed in Step 2a and the velocity from the old time step. Assuming the acceleration remains constant in this interval, the new velocity is (vx + Δt * ax, vy + Δt * ay).
    3. Calculate its new position (px, py) at time t + Δt by using the velocity computed in Step 2b and its old position at time t. Assuming the velocity remains constant in this interval, the new position is (px + Δt * vx, py + Δt * vy).
  3. Display update
    For each particle
    Draw using the position computed in the previous step.

The simulation is more accurate when Δt is very small, but this comes at the price of more computation. The default Δt we use is 25,000, which achieves a reasonable balance.