This is a genuine gravitational N-body simulation of galaxy formation, not an already-formed disk. It starts from several small, cold, scattered proto-galactic clumps of stars — the way real galaxies are thought to build up through hierarchical clustering — and lets nothing but real gravity pull them together, merge them, and settle the result into a bound, roughly virialized structure.
a_i = G·Σ_j m_j(r_j - r_i) / (|r_j - r_i|² + ε²)^1.5 — the softened
Newtonian acceleration on body i from every other body j, summed
via the Barnes-Hut tree. Kinetic energy T = Σ ½m_i v_i² and potential
energy U = -G·Σ_{i<j} m_i m_j / |r_i - r_j| are recomputed directly
from the live positions and velocities to drive the virial-ratio readout.
Real galaxies are thought to have assembled the same way: small dark-matter-dominated clumps merged repeatedly over billions of years — "hierarchical clustering" — with each merger heating and mixing the stars until the whole structure relaxes toward the same virial balance this simulation converges to in seconds rather than gigayears.
A genuine N-body simulation of galaxy formation: several small, cold, scattered proto-galactic clumps of stars collapse and merge under real Barnes-Hut gravity, integrated with a symplectic leapfrog scheme, while a dark matter halo strengthens the binding — watch the virial ratio settle toward equilibrium as the merger plays out.
Hierarchical galaxy formation: scattered clumps fall together under mutual gravity, merge, and violently relax into a single bound structure. Real kinetic and potential energy are tracked live to compute the virial ratio 2T/|U|, which starts well below 1 during collapse and settles near 1 once the system virializes.
Drag to pan, scroll to zoom. Set the number of clumps, total bodies and initial spread (need Restart), and adjust the dark matter halo strength, gravity G and softening live. Raise the halo slider and watch the clumps bind together faster.
Real galaxies are thought to have assembled the same way — small dark-matter-dominated clumps merging repeatedly over billions of years. This simulation compresses that hierarchical clustering into seconds using the same Newtonian gravity and the same virial theorem astronomers use to weigh real galaxy clusters.
Unlike a simulation that starts from an already-formed rotating disk, this one starts from the formation problem itself: several small, cold clumps of stars scattered across a wide region, with no organised rotation and barely any internal motion. A Barnes-Hut quadtree sums real Newtonian gravity between every star each frame in O(N log N) time, integrated with a symplectic leapfrog scheme, while an optional dark matter halo adds a smooth background pull toward the system's own centre of mass. Nothing about the eventual shape is scripted — the clumps fall together, collide, and merge purely because gravity pulls them that way.
Hierarchical galaxy formation from first principles: cold clumps under-bound relative to the whole system collapse, merge violently, and relax toward a bound quasi-equilibrium, tracked live through the virial ratio 2T/|U| computed from real kinetic and potential energy — not asserted, measured.
Set clump count, total bodies and initial spread (apply on Restart), and tune dark matter halo strength, gravity G and softening ε live. Watch the virial ratio: it dips well below 1 while clumps are still falling and merging, then climbs back toward 1 as the system settles. Drag to pan, scroll to zoom.
The virial theorem says a bound, relaxed system satisfies 2T + U = 0, so 2T/|U| should hover near 1. Watching that ratio swing during the merger and then settle is direct evidence the collapse is being driven by the actual gravity computed each frame, not a preset animation.
Simulations of an existing disk (such as this site's 2D Galaxy) start stars already on near-circular orbits derived from a target rotation curve. This one starts from scattered, cold, unbound clumps and lets pure self-gravity build the bound structure from scratch — the formation process itself, not the end state.
It uses a symplectic leapfrog (kick-drift-kick) scheme rather than plain Euler integration. Leapfrog conserves energy much better over long runs, which matters here because the simulation needs to be trusted to show a genuine virial ratio rather than one drifting from numerical error.
It adds a smooth, non-clumpy background mass centred on the system's live centre of mass, pulling every clump inward and strengthening the binding between them. Raising it visibly speeds up how quickly the clumps fall together and merge — the same invisible-mass effect astronomers use to explain why real galaxies and clusters stay bound.
The virial ratio 2T/|U| compares twice the kinetic energy to the magnitude of the potential energy. A bound, relaxed system settles near 1. Here it starts well below 1 (the clumps are cold and still falling), spikes during the violent merger, and gradually relaxes back toward 1 as the system reaches quasi-equilibrium — computed live from real positions and velocities, not scripted.