Visualizations
457 / 500

457 · Simulation

Ising Magnet

A magnet at its critical point, where domains of spins turn fractal.

Each small square is a spin, up (paper) or down (ink), on a lattice of about 90,000, and neighbors that agree lower the energy. Below the critical temperature Tc = 2.269 the magnet orders, above it noise wins, and right at Tc domains appear inside domains at every scale. Ordinary spin flips crawl there, so the engine is the Wolff cluster algorithm: grow a cluster of aligned spins with bond probability 1 - exp(-2/T) and flip it whole, an exact Markov chain whose clusters are themselves the critical fractals, plus a checkerboard heat bath for local shimmer. Spins that disagree with the magnetization averaged over their neighborhood are colored, paper islands amber and ink lakes teal, so the nesting shows at a glance, and the inset tracks the measured magnetization against Onsager's exact solution.

Try it. Drag the temperature slider (or use the arrow keys; C snaps to Tc, 1 and 3 jump to cold and hot). Drag on the lattice to paint a magnetic field that pulls spins up, and right-drag or Shift-drag for the opposite pole. Space reheats to random spins, and R also clears the painted field. Leave it alone and it tours the phase diagram.

  • Wolff cluster algorithm
  • Checkerboard heat bath
  • Onsager's exact solution
  • Two-scale coloring by coarse-grained magnetization

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build an interactive 2D Ising model with JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Make a canvas that fills the window and stays sharp on high-DPI screens. Create a lattice of about 300 x 200 spins in an Int8Array, each +1 or -1 at random, with periodic (wrap-around) edges.
- Each frame, run one Metropolis sweep: pick random spins, compute the energy change of flipping one (2 times the spin times the sum of its four neighbors), and flip it if that change is negative or with probability exp(-change / T).
- Draw the lattice into an ImageData one pixel per spin, up spins light and down spins dark, put it on a small offscreen canvas, and drawImage it scaled up with image smoothing off.
- Add a temperature slider from 1.5 to 3.5 and mark the critical temperature, Tc = 2 / ln(1 + sqrt(2)), about 2.269. Watch it order when cold, boil when hot, and grow domains of every size near Tc.

Once that works, make it beautiful and fast:
- Near Tc, single flips get very slow. Add the Wolff cluster algorithm: pick a random seed spin, grow a cluster with a stack by adding each aligned neighbor with probability 1 - exp(-2 / T), then flip the whole cluster at once. Run a few clusters per frame and watch the fractal domains appear in seconds.
- Use warm paper and ink colors instead of pure white and black, and add a soft vignette.
- Plot the measured magnetization |m| against Onsager's exact curve, m = (1 - sinh(2 / T)^-4)^(1/8) below Tc and 0 above, in a small inset.
- Let the mouse paint a local magnetic field that adds to each spin's energy, so you can draw with domains.

Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as measuring the correlation length, a 3D Ising slice viewer, or the q-state Potts model with more colors.
PreviousJoukowski AirfoilExact flow past a wing, made by conformally mapping flow around a circle. NextImage TracerPixel art and a painted landscape traced into crisp, infinitely zoomable curves.

Related visualizations

  • Magnetic PendulumFractals A pendulum over three magnets, and the fractal map of where every release point ends up.
  • Iron FilingsPhysics Magnets on paper dusted with iron filings, drawn by line integral convolution.
  • Bloch SpherePhysics A qubit on the Bloch sphere: gates, Rabi flips, decoherence and a spin echo.
  • DendritesSimulation Supercooled melt freezes into branching dendrites, with latent heat glowing between arms.
  • Critical PercolationSimulation Open hexagons merge into a giant cluster the instant p crosses one half.
  • Melting CrystalSimulation Nearly 2,000 atoms freeze into crystal grains, melt and boil from one pairwise force.
  • Magnetic BottlePhysics Particles gyrate, bounce and drift in Earth's dipole, filling the radiation belts.
  • Protein FoldingAlgorithms An HP lattice protein folds a hydrophobic core under replica-exchange Monte Carlo.
  • Faraday WavesFluids A shaken dish of liquid locks into stripes, squares, hexagons and a 12-fold quasicrystal.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs