- Introduced by
- Isaac Newton
- Year
- 1687
- Type
- Computational simulation algorithm
- Family
- Computational Physics
What does it do?
N-body simulation is a numerical method for modelling the motion of a set of particles that attract one another gravitationally.
Newton's law of universal gravitation and the laws of motion govern the system, but these equations have exact solutions only for special cases such as two bodies, so the motion is integrated step by step on a computer. The method is used for planetary orbits, star cluster dynamics, and cosmological structure formation.
The intuition behind the method
An N-body system is a set of bodies that each pull on every other body through gravity. Because the forces depend on all current positions at once, the motion cannot in general be written as a closed-form expression once there are three or more bodies. The simulation instead divides time into small steps. At each step it computes every body's acceleration from the positions of all the others, then updates velocities and positions. Repeating this over many steps produces trajectories that can be examined for orbital periods, close encounters, resonances, and signs of chaotic or long-term stable behaviour.
History
Newton's Principia (1687) stated the law of universal gravitation that N-body simulations implement. Poincaré's 1892 work on celestial mechanics, cited here, is associated with recognising the complexity of the three-body problem. Aarseth's 1985 chapter, cited here, covers direct methods for N-body simulations, the numerical direct-summation approach that developed after Newton's work.
When to use it
Choose N-body simulation when the system consists of several gravitating bodies whose masses and initial positions and velocities are known, and the question concerns their dynamics. Typical cases are planetary systems, stellar clusters, galaxy formation, and spacecraft trajectory design. It is appropriate when the system is too complex for closed-form solutions but small enough for direct integration to be computationally feasible. Avoid it for integrations spanning Gyr timescales without approximations, and when relativistic effects dominate the dynamics.
Limitations and considerations
- Direct summation over all pairs requires on the order of N² force evaluations per step, so it becomes impractical for very large N unless approximations such as tree or multipole methods are used.
- Numerical errors accumulate over long integrations, and in chaotic systems small differences grow, so individual long-term trajectories are not reliable predictions beyond the Lyapunov time, and stability conclusions need statistical care. Test sensitivity to initial conditions by rerunning with slightly perturbed initial states, and base conclusions about stability on ensembles or on the spread across runs rather than on a single trajectory.
- Close encounters and collisions need special handling: softening smooths the force but does not model physical collisions, and very close approaches require small timesteps or merging rules.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Implements Newton's law of gravitation directly, so each term in the force calculation can be traced to a physical quantity.
- The number of bodies and their initial configurations can be varied without changing the basic algorithm.
- Trajectories can be examined for orbital chaos and resonances, which analytical approaches often cannot resolve for three or more bodies.
- Bodies can be advanced with different timesteps when close encounters or tight orbits need finer resolution.
Applications
- Modelling orbital dynamics of planets around host stars, including the perturbations used in exoplanet studies
- Simulating the early Solar System to understand planetary formation
- Modeling star cluster dynamics and mass segregation
- Computing large-scale cosmological simulations of dark matter structure
How it works
How it works
Collect initial conditions: mass, position, and velocity for each body.
Normalize units (for example, G = 1 with masses and lengths scaled to the system), move to the centre-of-mass frame and remove any net momentum, and check for overlapping bodies or near-zero separations. Such configurations produce very large accelerations and call for a smaller timestep or a softening length.
For each body i, sum the gravitational acceleration contributions from every other body j, using the inverse-square law in the formula shown. In that formula r_ij is the distance between bodies i and j, and r̂_ij is the unit vector pointing from body i toward body j, so each term accelerates body i toward body j. Because every pair contributes, the cost of this direct sum grows rapidly with the number of bodies. The expression shown is the unsoftened law; if a softening length ε is used, it is added to the distance term as described in the FAQ.
Formula
\mathbf{a}_i = \sum_{j \neq i} \frac{G m_j}{r_{ij}^2} \hat{r}_{ij}Advance velocities and positions over each time step Δt with a numerical integrator. The displayed equation is the first-order Euler-type velocity update from the acceleration. On its own, this scheme does not keep energy error from growing systematically over long integrations, so for orbital work a symplectic scheme is usually preferred, such as kick–drift–kick leapfrog (half-step velocity kicks around a full position drift) or Verlet. Higher-order Runge–Kutta schemes are more accurate over short intervals but do not preserve energy structurally. The choice of scheme affects how energy and orbits drift over time.
Formula
\mathbf{v}_i(t+\Delta t) = \mathbf{v}_i(t) + \mathbf{a}_i \Delta tFrom the trajectories, derive orbital elements, stability indicators, and long-term behaviour. As a consistency test, track the relative energy error ΔE/E and the drift in total linear momentum and angular momentum over the run. For an isolated system these quantities should stay close to their initial values. Runs whose drift exceeds a tolerance set for the study should be repeated with a smaller timestep or a better integrator before the results are interpreted.
Common pitfalls
- Choosing a timestep that is too large for the closest approaches causes energy drift and orbits that do not reflect the underlying physics.
- Neglecting relativistic corrections when bodies move at high speeds or lie deep within a massive gravitational potential, as near compact objects.
- Skipping checks of total energy and momentum conservation, which are the simplest diagnostics of integration error.
Frequently asked questions
What integration method should I use?
Verlet or leapfrog schemes are widely used because they are symplectic and keep energy error from growing systematically, which matters for orbits. High-order Runge–Kutta schemes can be more accurate over short intervals but do not preserve energy structurally, so energy drift can accumulate over long runs. For systems whose bodies have very different orbital times, individual or hierarchical timesteps can reduce cost.
How do I handle close approaches or collisions?
Add a softening length ε to the distance term in the force law, so that the force stays finite as separations approach zero. Softening smooths the singularity but does not model physical collisions; if realistic collisions are needed, add collision detection and explicit merging or outcome rules.
Why does my simulation show energy loss over time?
Common causes are a timestep that is too large for the closest encounters, a non-symplectic integrator, or rounding errors in low-precision arithmetic. Reduce the timestep, switch to a symplectic scheme, or use double-precision arithmetic.
Related methods
Sources
Poincaré, H. (1892). Les méthodes nouvelles de la mécanique céleste. Gauthier-Villars.
Open publication referenceNewton, I. (1687). Philosophiæ Naturalis Principia Mathematica. Royal Society.
Open publication referenceAarseth, S. J. (1985). Direct methods for N-body simulations. In Multiple Time Scales (pp. 377-418). Springer.
Open DOI reference