Logo

N-Body

Gravitational dynamics from first principles. from my Cosmology Final Project

Please Scroll

Two-Body Problem

Two masses under Newtonian gravity. The force on body \(i\):

\[ \mathbf{F}_{ij} = -\frac{Gm_i m_j}{\lvert\mathbf{r}_{ij}\rvert^3}\,\mathbf{r}_{ij} \]

Define the reduced mass \(\mu = m_1 m_2 / (m_1 + m_2)\) and relative coordinate \(\mathbf{r} = \mathbf{r}_1 - \mathbf{r}_2\). The problem reduces to an equivalent one-body Kepler problem:

\[ \mu\,\ddot{\mathbf{r}} = -\frac{Gm_1 m_2}{\lvert\mathbf{r}\rvert^3}\,\mathbf{r} \]

Closed-form solutions: conic sections. Bound orbits are ellipses. Energy and angular momentum are both conserved.

Three-Body Problem

Add a third comparable mass and the system becomes non-integrable. Poincaré proved in 1890 that no general closed-form solution exists.

Trajectories are sensitive to initial conditions—deterministic chaos. Small perturbations grow exponentially:

\[ \lvert\delta\mathbf{r}(t)\rvert \;\sim\; \lvert\delta\mathbf{r}(0)\rvert\, e^{\lambda t} \]

where \(\lambda > 0\) is the largest Lyapunov exponent. Apart from special periodic orbits (Lagrange, figure-eight), long-term prediction requires numerical integration.

N-Body: Direct Summation

Compute all pairwise gravitational accelerations. A softening length \(\epsilon\) regularizes close encounters:

\[ \ddot{\mathbf{r}}_i = G\sum_{j \neq i} \frac{m_j\,(\mathbf{r}_j - \mathbf{r}_i)}{(\lvert\mathbf{r}_j - \mathbf{r}_i\rvert^2 + \epsilon^2)^{3/2}} \]

For a body at radius \(r\), treat all enclosed mass \(M(r)\) as a point source at the center. Equilibrium requires the gravitational pull to supply the centripetal acceleration:

\[ \frac{GM(r)\,m}{r^2} = \frac{mv^2}{r} \]

Cancel \(m\) and solve for the circular velocity:

\[ v(r) = \sqrt{\frac{GM(r)}{r}} \]

Each body is initialized at this velocity. If \(v < v(r)\), gravity wins and the orbit decays inward. If \(v > v(r)\), centrifugal force wins and the body escapes. The disk you see is the balance point.

Integrated with leapfrog (Störmer–Verlet), a symplectic scheme. Cost per step: \(\mathcal{O}(N^2)\). Color encodes velocity magnitude.

Barnes–Hut

Hierarchical spatial decomposition via quadtree. Each internal node stores the total mass and center of mass of its subtree.

When computing the force on a body, walk the tree. If a cell subtends a small angle, treat it as a point mass. The multipole acceptance criterion:

\[ \frac{s}{d} < \theta \]

where \(s\) is the cell width, \(d\) the distance to the cell's center of mass, and \(\theta \approx 0.5\).

Cost per step: \(\mathcal{O}(N \log N)\). The quadtree cells are drawn behind the particles.

Galactic Collision

Two self-gravitating disks on a bound trajectory. Each galaxy is a central mass surrounded by an orbiting population, initialized with circular Keplerian velocities.

As the galaxies approach, tidal forces distort both disks. The near side of each galaxy feels a stronger pull than the far side, stretching material into tidal tails and bridges:

\[ \Delta\mathbf{a} \;\approx\; -\frac{2GM}{d^3}\,\delta\mathbf{r} \]

where \(d\) is the inter-galactic separation and \(\delta\mathbf{r}\) the offset from a galaxy's center. The \(1/d^3\) scaling is the tidal term from a Taylor expansion of the gravitational field.

Energy is redistributed through violent relaxation (Lynden-Bell, 1967): the time-varying potential scrambles individual orbits on a crossing timescale, driving the merged remnant toward a quasi-equilibrium.