Home / Learn / Science / Cosmology A to Z / Lesson 10.9
All lessons

Module 10: From smooth to lumpy · Lesson 10.9 · about 16 min

Universes in a computer

How cosmologists grow a universe in a computer: particles, gravity on a grid, softening and expanding boxes, from Millennium to IllustrisTNG, with a live model.

Key points, meanings and formulas on one page: choose "Save as PDF" in the print box.

Module 10 · about 16 minutes. Read first: 10.6 How galaxies form. Gravity’s equations are simple, but following billions of masses pulling on each other is impossible by hand. Since the 1980s, cosmologists have grown whole universes in computers, from the measured ripples to today’s web of galaxies. By the end you will be able to explain how a simulation works, what its particles and boxes mean, and what simulations can and cannot tell us.

Start a box of particles with the ripples the CMB shows, let it expand with the universe, and let every particle pull on every other. Out comes a cosmic web like the real one: the strongest test that cold dark matter and gravity can build the universe we see.

1. The N-body idea

Fill a box with N particles of dark matter. Work out the pull on each from all the others, nudge the speeds (a kick), move the particles a little (a drift), and repeat thousands of times. That is an N-body simulation. The pull on particle i is Newton’s law, added up over every other particle j, with one change:

𝐚i=∑j≠iGmj(𝐱j−𝐱i)(|𝐱j−𝐱i|2+ε2)3/2
ai
the acceleration of particle i
mj, xj
the mass and position of each other particle
ε
the softening length. Without it, two particles passing very close would pull infinitely hard and fling each other apart. Each particle stands for a smooth cloud, so gravity is softened at distances below ε (a few kiloparsecs or less)
Physical meaning
Newton's inverse-square law (Newton's gravity), summed over everything. Newton is enough: the particles move far slower than light, and the expansion is handled separately (section 2).

Each “particle” is not a real particle but a sample of the matter: the box’s total mass shared out equally. In big simulations each is a cloud of millions to billions of suns.

2. Boxes that expand

A simulation cannot hold the whole universe, so it follows a cube, typically tens to thousands of megaparsecs across, and wraps it around at the edges: a particle leaving through the right face comes back in through the left, so there are no edges and the cube is a fair sample of an endless universe. Positions are measured in comoving units that stretch with the universe (Lesson 5.1); the computer only tracks the extra motion caused by lumps, with the expansion rate taken from the Friedmann equation.

The run begins at a redshift of roughly 50 to 130, while the lumps are still small. The starting positions come from a random field with exactly the ripple strengths measured by the CMB and galaxy surveys, displaced with Zel’dovich’s straight-line rule (Lesson 10.4). Nothing about the web is put in by hand.

3. Doing the sum quickly

Adding up every pair takes about N²/2 sums per step. For ten billion particles that is far too slow.

Two tricks cut it to about N log N:

  • Tree codes (Josh Barnes and Piet Hut, 1986) group far-away particles into boxes and treat each box as one mass, like looking at a distant city as one light.
  • Particle-mesh codes spread the mass onto a grid and find gravity for the whole grid at once with a fast Fourier transform. Modern codes like GADGET combine both: mesh for long range, tree for short.
A real particle-mesh simulation running in your browser: 16,384 particles in a 2-D box that wraps at the edges and expands with the universe. Each step: spread the mass onto a 64 × 64 grid, find gravity with a Fourier transform (softened over about a cell), kick, drift. It starts almost smooth; lumps, threads and empty regions grow. "New universe" starts again from different random ripples. (2-D gravity is not quite 3-D gravity, but the method is the one the big simulations use.)

4. From dark matter to galaxies

Dark matter alone gives the scaffolding: halos, subhalos and the web, all from gravity and the starting ripples, with no knobs to turn. Galaxies need gas too. Hydrodynamic simulations add the gas, with its pressure, shocks and cooling. But stars, supernovae and black holes act on scales far smaller than one simulation cell, so they enter as approximate subgrid recipes, tuned to match a few observed facts such as how many galaxies there are of each mass. The best runs then reproduce many other things they were not tuned for, such as the mix of spirals and ellipticals.

  • Millennium Run (2005): Volker Springel and the Virgo Consortium followed about 10 billion particles (2160³) in a box 500 h⁻¹ Mpc (about 700 Mpc) across, then “painted” galaxies onto its halos with simple models.
  • Illustris (2014) and IllustrisTNG (2018): gas, stars and black holes together in boxes up to about 300 Mpc across.
  • EAGLE (2015): the Virgo Consortium’s galaxy simulation, tuned to the masses and sizes of galaxies today.
  • 1941 Erik Holmberg simulates two colliding galaxies with 74 light bulbs, measuring "gravity" with light meters.
  • 1985 Davis, Efstathiou, Frenk and White run cold dark matter universes with 32,768 particles: they look like the real galaxy maps.
  • 1986 Barnes and Hut's tree algorithm.
  • 2005 The Millennium Run: 10 billion particles.
  • 2014-18 Illustris, EAGLE and IllustrisTNG grow realistic galaxies with gas and feedback.

“Simulations just show whatever their makers put in.” For dark matter, the only inputs are gravity, the amounts of each ingredient and the starting ripples, all measured independently; the web, the halos and their NFW shape (Lesson 10.3) come out by themselves. Galaxy physics is different: its subgrid recipes are tuned, so a simulation’s galaxies are a test only of what was not tuned.

  • An N-body simulation follows particles that each stand for a cloud of matter, adding up gravity (softened at short range) and stepping forwards: kick, drift, repeat.
  • Boxes are comoving and wrap around; the starting positions follow the measured ripples.
  • Tree and particle-mesh methods cut the work from N² to about N log N.
  • Dark matter-only runs have no tunable knobs; galaxy simulations add gas and tuned subgrid recipes for stars and black holes.
  • Landmarks: Davis-Efstathiou-Frenk-White (1985), Millennium (2005, 10 billion particles), Illustris, EAGLE, IllustrisTNG.

Check yourself

1In a cosmological N-body simulation, what is one 'particle'?
2Why do simulations use 'softening'?
3Which part of a galaxy simulation is least certain?

Sources

Your ticks stay in this browser. Save them as a file on the course home.

Updated 2026-11-22. Numbers are rounded for teaching; measured values come from the sources listed in each lesson. Simulations run in your browser; nothing you type is sent anywhere.