HomeArticlesQuantum Physics

2D Spin Lattice: Growing a Magnet From a Coin Flip

A grid of spins, a neighbour-coupling energy, and the Metropolis Monte Carlo rule that lets a computer sample thermal equilibrium one flip at a time.

mysimulator teamUpdated June 2026≈ 8 min read▶ Open the simulation

A magnet is millions of coin flips agreeing with each other

A ferromagnet like iron is, microscopically, a lattice of atomic magnetic moments — spins — each of which would rather point the same direction as its neighbours, because that lowers the local exchange energy. At high temperature, thermal jostling randomises the spins faster than the coupling can align them, and the material shows no net magnetism. Cool it below a critical temperature and the coupling wins: neighbouring spins start to agree, domains of aligned spins grow, and the whole sample can develop a net magnetic moment with no external field at all. This is spontaneous magnetization, and it is one of the cleanest examples in physics of macroscopic order emerging from purely local interactions.

The Hamiltonian

The energy of a lattice of spins with nearest-neighbour ferromagnetic coupling is the Heisenberg (or, for discrete up/down spins, Ising) exchange Hamiltonian:

H = -J · Σ⟨i,j⟩ Sᵢ · Sⱼ

J     = exchange coupling constant (J > 0 favours parallel, "ferromagnetic" alignment)
⟨i,j⟩ = sum over each pair of NEAREST NEIGHBOURS on the lattice, counted once
Sᵢ    = spin at site i — a unit vector for Heisenberg spins, ±1 for Ising spins

This Hamiltonian is deliberately simple: only neighbours interact, and the coupling strength is the same everywhere. That simplicity is the point — it is the minimal model that reproduces a genuine ferromagnetic phase transition, and it is exactly solvable in some cases (the 2D Ising model, famously, by Lars Onsager in 1944), which makes it the benchmark every simulation and approximation is checked against.

live demo · domains forming and dissolving on a 2D spin grid● LIVE

The Metropolis algorithm

Exactly enumerating every spin configuration to find thermal averages is impossible for any lattice bigger than a handful of sites — the state space grows exponentially. The Metropolis algorithm (1953) instead builds a random walk through configuration space whose long-run visiting frequency matches the Boltzmann distribution, without ever computing a partition function:

repeat for each Monte Carlo step:
  pick a random site i
  propose flipping (or reorienting) spin Sᵢ
  compute ΔE = E_new - E_old   (only 4 neighbours matter — cheap!)
  if ΔE <= 0:
    accept the flip                         // always take improvements
  else:
    accept with probability exp(-ΔE / kT)   // sometimes take worse states
  else: reject, keep Sᵢ unchanged

The crucial detail is that flips are local: since only the four (or six, in 3D) nearest-neighbour bonds change when one spin flips, ΔE is a handful of additions regardless of lattice size, so one full "sweep" over every site is cheap. Run enough sweeps and the ensemble of visited configurations satisfies detailed balance — the accept/reject rule is constructed so that, at equilibrium, transitions between any two states occur at rates consistent with the Boltzmann distribution at temperature T. That is what justifies treating time-averages over the simulation as thermal averages.

Watching the transition

Track the order parameter — the mean magnetization per spin, m = (1/N)·Σ Sᵢ — while slowly lowering the temperature. Well above the critical temperature Tc, m fluctuates near zero: spins are essentially uncorrelated beyond a very short range. Well below Tc, m settles near ±1: almost every spin agrees. Near Tc itself the lattice shows domains of every size simultaneously, fluctuates wildly, and the correlation length (how far a spin's influence extends) diverges — the hallmark of a continuous (second-order) phase transition, and the reason critical phenomena near Tc obey universal power-law scaling shared by wildly different physical systems.

Practical simulation notes

Near Tc, single-spin-flip Metropolis suffers from critical slowing down: large domains take a very long time to flip one spin at a time, so equilibration and decorrelation both get slow exactly where you most want good statistics. Cluster algorithms such as Wolff and Swendsen-Wang fix this by flipping entire correlated clusters of spins in one move, cutting the effective relaxation time dramatically near the critical point. For an interactive canvas demo running in real time, plain single-spin Metropolis is usually enough — the visual formation and dissolution of domains is exactly what it is designed to show, even without cluster-level speedups.

Frequently asked questions

Why does the Metropolis algorithm sometimes accept a worse spin flip?

Because a simulation that only ever lowers energy gets trapped in the nearest local minimum and never samples the true thermal distribution. Occasionally accepting an energy-raising flip, with probability exp(-ΔE/kT), is exactly what makes the long-run distribution of visited states match the Boltzmann distribution at temperature T — this is the detailed-balance condition that makes Metropolis Monte Carlo statistically correct, not just a heuristic optimizer.

What actually happens at the critical temperature?

Below the critical temperature, thermal fluctuations are too weak to overcome the coupling between neighbouring spins, and the lattice settles into a state with net magnetization — most spins pointing the same way. Above it, thermal agitation wins and the average magnetization drops to zero. Right at the critical point, spin domains of every size coexist simultaneously, correlation length diverges, and quantities like magnetization and susceptibility follow universal power laws.

Isn't a true 2D Heisenberg ferromagnet supposed to have no order at any finite temperature?

Yes, that's the Mermin-Wagner theorem: continuous-symmetry spins (Heisenberg or XY models) in one or two dimensions cannot sustain true long-range order at any T > 0, because long-wavelength spin-wave fluctuations always destroy it. The sharp ferromagnetic transition visible in this simulation, and in most classroom demonstrations of this kind, is the behaviour of the discrete-symmetry 2D Ising model, which does have a genuine finite-temperature transition (solved exactly by Onsager in 1944).

Try it live

Everything above runs in your browser — open 2D Spin Lattice and change the parameters while it is running. Nothing is installed, nothing is uploaded, the whole model lives in one tab.

▶ Open 2D Spin Lattice simulation

What did you find?

Add reproduction steps (optional)