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\):
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:
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:
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:
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:
Cancel \(m\) and solve for the circular velocity:
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:
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:
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.