N-Body Simulation

Numerical integration of gravitational systems with energy and time-step control.

Conceptual context

An N-body simulation follows several objects interacting through gravity. At each instant, the code knows rir_i, rjr_j, masses, and velocities; it computes FijF_ij, then aia_i, and finally updates positions and velocities. The challenge is not only obtaining plausible orbits but controlling how numerical integration alters EE. The decisive parameter is dtdt. A large step can invent precession, ejections, or collisions; a smaller step increases computational cost. The central question is how ϵE\epsilon _E decreases under temporal refinement and when the expected law stops applying.

🟢 Essential level

In a gravitational simulation, every body feels the attraction of all others. The computer repeats a cycle: it computes rijr_ij, obtains FijF_ij, sums FiF_i, determines aia_i, and advances the system through a small dtdt. A smaller interval resolves orbital curvature and close encounters more accurately. It does not repair incorrect masses, inconsistent initial conditions, or an untreated singularity. Quality is monitored through EE, which should remain nearly constant in an isolated system. ϵE\epsilon _E compares current energy with E0E_0. A regular trend under decreasing dtdt supports convergence. Growing drift, a plateau, or abrupt jumps indicate that the integrator, a close encounter, or the energy definition requires inspection.

Energy and orbital shape must be checked together because either diagnostic alone can hide a different numerical defect. A convincing orbit may accumulate phase error, and a dramatic ejection may disappear when the calculation is repeated at higher resolution. Runs must therefore be compared before accepting a physical conclusion, and the comparison should always report both the energy trend and the orbital shape side by side.

🔵 Formal level

Basic formulas

The gravitational interaction between two bodies follows:

Vector gravitational force

F⃗ij=Gmimjr⃗j−r⃗irij3\vec F_{ij}=G m_i m_j\frac{\vec r_j-\vec r_i}{r_{ij}^3}

The total force on body i gives aia_i through:

Acceleration of one body

a⃗i=∑jF⃗ijmi\vec a_i=\frac{\sum_j \vec F_{ij}}{m_i}

Leapfrog separates the update into staggered velocity and new position:

Leapfrog velocity update

v⃗n+1/2=v⃗n−1/2+a⃗nΔt\vec v^{n+1/2}=\vec v^{n-1/2}+\vec a^n\Delta t

Leapfrog position update

r⃗n+1=r⃗n+v⃗n+1/2Δt\vec r^{n+1}=\vec r^n+\vec v^{n+1/2}\Delta t

Global kinetic energy is:

Total kinetic energy

K=∑i12mivi2K=\sum_i \frac{1}{2}m_i v_i^2

Potential energy must sum unique pairs:

Total gravitational potential energy

U=−∑i<jGmimjrijU=-\sum_{i<j}\frac{G m_i m_j}{r_{ij}}

The mechanical total is:

Total mechanical energy

E=K+UE=K+U

Real student questions

Does energy conservation mean the orbit is exact? No. Phase error can accumulate while energy stays bounded.

Why does Leapfrog use vhalfv_half? Staggering improves geometric structure and limits secular drift.

What if E0E_0 is nearly zero? Relative error becomes unstable; use another normalization scale.

Why do close encounters break convergence? They introduce a much shorter time scale and rapidly changing force.

Is adaptive stepping always better? It resolves encounters efficiently, but may lose symplectic advantages if designed carelessly.

Final synthesis

An N-body simulation is trustworthy when dynamics, units, and integration are coherent. Reducing dtdt should decrease ϵE\epsilon _E predictably until another source dominates. Convergence turns a computed orbit into numerical evidence.

Formal development

This section is part of the complete theory. See the subscription options to access it.

🔴 Structural level

This section is part of the complete theory. See the subscription options to access it.

Deep physical interpretation

This section is part of the complete theory. See the subscription options to access it.

Order of magnitude

This section is part of the complete theory. See the subscription options to access it.

Personalized resolution method

This section is part of the complete theory. See the subscription options to access it.

Special cases and extended example

This section is part of the complete theory. See the subscription options to access it.

Cross-cutting connections and study paths

This section is part of the complete theory. See the subscription options to access it.