Questing · 2026-07-22 · Statistical Mechanics · Zero Dependencies

SPIN

A 200×200 lattice of magnetic spins evolves under the Metropolis-Hastings rule. Drag temperature through Tc ≈ 2.27 and watch the ferromagnetic phase transition happen in front of you — the same physics that makes a fridge magnet lose its magnetism when you heat it up.

Open Spin →

What Is Spin?

Spin simulates the two-dimensional Ising model — the simplest lattice model of a ferromagnet and one of the most important exactly solvable systems in all of physics. A 200×200 grid of sites each holds a spin: either up (+1, amber) or down (−1, dark teal). Adjacent spins prefer to align — that is the ferromagnetic interaction. Temperature competes against this tendency, injecting thermal noise that randomly flips spins regardless of alignment.

The result is a competition between order and disorder. Below a critical temperature Tc, order wins: large domains of aligned spins form spontaneously, and the lattice acquires a net magnetization even though no external field is applied. Above Tc, disorder wins: spins flip independently and the net magnetization averages to zero. Right at Tc, neither wins — the system sits at a continuous phase transition with fluctuations at every length scale simultaneously.

The Ising Model

Wilhelm Lenz introduced the model in 1920 as a simplified picture of ferromagnetism, assigning it to his student Ernst Ising for his thesis. The Hamiltonian captures nearest-neighbor interactions and an optional external field h:

H = −J Σ_{<ij>} s_i s_j − h Σ_i s_i

  s_i ∈ {+1, −1}      — spin at site i
  J > 0               — ferromagnetic coupling (J=1 here)
  <ij>                — sum over nearest-neighbor pairs
  h = 0               — no external field in this simulation

Ising himself solved the 1D case in his 1924 thesis and found no phase transition — which led him to incorrectly conclude the model was physically uninteresting. The 2D case is richer. Lars Onsager solved it exactly in 1944 and found a genuine phase transition at:

T_c = 2J / (k_B · ln(1 + √2))   ≈ 2.2692   (J = k_B = 1)

This is one of the most celebrated exact results in theoretical physics.
Onsager derived it using transfer matrices — the calculation spans 33 pages.

The Three Phases

Drag the temperature slider slowly from cold to hot and watch three distinct regimes:

T < T_c  (ferromagnet)
  Domains of aligned spins dominate the grid.
  Most spins point the same way — amber or teal covers > 80% of the canvas.
  Magnetization |M| → 1 as T → 0.
  Domain walls are thin, smooth, low-energy boundaries.
  The symmetry between up and down is spontaneously broken:
  the system picks one direction without any external field.

T = T_c  (critical point)
  Neither phase wins. Clusters exist at every scale simultaneously —
  the domain pattern is fractal: it looks the same zoomed in or out.
  The correlation length ξ diverges: a flip anywhere can feel the
  effect of spins arbitrarily far away.
  Magnetization |M| fluctuates wildly — the live display shows this.
  Critical slowing down: the system takes forever to equilibrate.

T > T_c  (paramagnet)
  Thermal noise dominates. Spins flip independently.
  No persistent large-scale structure — the lattice looks like salt-and-pepper.
  Magnetization |M| → 0 as T → ∞.
  Short-range patches briefly form and dissolve,
  but no long-range order survives.

The Metropolis-Hastings Algorithm

The simulation uses the Metropolis-Hastings Monte Carlo method introduced by Metropolis, Rosenbluth, Teller, and Teller in 1953 — one of the ten most important algorithms of the 20th century. The rule is:

1. Pick a random spin i.
2. Compute the energy cost ΔE of flipping it:
     ΔE = 2 · s_i · (s_up + s_right + s_down + s_left)
   ΔE ∈ {−8, −4, 0, +4, +8}
3. If ΔE ≤ 0: flip — the flip is energetically favorable, always accept.
   If ΔE > 0:  flip with probability exp(−ΔE / T).
4. Repeat N² times = one "sweep" ≈ one Monte Carlo step per site.

Only ΔE = 4 and ΔE = 8 require a random draw.
These Boltzmann factors are precomputed when T changes:
  exp(−4/T) and exp(−8/T)

The magic of the Metropolis rule is the acceptance criterion: by accepting bad moves with probability proportional to the Boltzmann factor, the algorithm samples the correct thermal equilibrium distribution at temperature T — the Gibbs measure — without ever explicitly computing the partition function (an astronomically large sum). This is why it works.

Spontaneous Symmetry Breaking

The Ising model has a perfect symmetry: flipping every spin simultaneously (s_i → −s_i for all i) leaves the Hamiltonian unchanged. At high T, the thermal average respects this symmetry — the net magnetization is zero. Below Tc, the system breaks this symmetry spontaneously: it settles into a mostly-up or mostly-down state even though both are equally valid. The system chooses, without being pushed.

This is the canonical example of spontaneous symmetry breaking — the same phenomenon that gives mass to elementary particles (Higgs mechanism), explains superconductivity (U(1) symmetry breaks → Cooper pairs condense), and underlies almost every phase transition in nature. The Ising model is the simplest place where you can see it happen.

Critical Exponents and Universality

Near the critical point, all physical quantities scale as power laws in the reduced temperature t = (T − Tc) / Tc. The exponents are exact for the 2D Ising model:

Magnetization:       M ~ |t|^β        β = 1/8  = 0.125  (Onsager)
Susceptibility:      χ ~ |t|^{−γ}      γ = 7/4  = 1.75   (Onsager)
Correlation length:  ξ ~ |t|^{−ν}      ν = 1             (Onsager)
Specific heat:       C ~ |t|^{−α}      α = 0 (log)       (Onsager)

These are EXACT — not approximate. The 2D Ising model is one of
the only interacting statistical systems solved without approximation.

These exponents are universal: they do not depend on the microscopic details of the material — only on the dimensionality and the symmetry of the order parameter. A real ferromagnet near its Curie temperature, a binary alloy near its order-disorder transition, and the 2D Ising model all have the same β = 1/8. This universality is the deepest result in the theory of phase transitions.

Where This Appears in Nature

Iron, nickel, cobalt  — real ferromagnets. Their Curie temperatures are
                        ~1043 K (Fe), ~627 K (Ni), ~1388 K (Co).
                        Above T_c, a permanent magnet loses its magnetism instantly.
                        Below it, magnetic domains form exactly as in the simulation.

Binary alloys         — Cu-Zn (brass), Fe-Al, Fe-Co all undergo
                        order-disorder transitions in the Ising universality class.

Liquid-gas transition — near the critical point of any fluid, density
                        fluctuations have the same exponents as the Ising model.
                        Both are in the same universality class.

Protein folding       — simplified lattice models of protein folding use
                        Ising-like energy landscapes.

Neural networks       — the Hopfield network (associative memory)
                        is a generalized Ising model; the Boltzmann machine
                        is its stochastic extension.

Quantum field theory  — the 2D Ising model at T_c is exactly equivalent to
                        a free Majorana fermion field theory. The critical point
                        is a conformal field theory (c = 1/2 Virasoro algebra).

Implementation

The simulation runs on a 200×200 periodic (toroidal) lattice using a pre-built neighbor lookup table that stores the four neighbor indices for each of the 40,000 sites, eliminating modular arithmetic from the inner loop. Three sweeps run per animation frame, giving 120,000 Metropolis flip attempts at 60fps — around 7.2 million attempts per second.

Grid:       Int8Array(200×200)     — +1 or −1 per site
Neighbors:  4× Int32Array(200×200) — precomputed up/down/left/right indices
Render:     ImageData(200×200)     — updated each frame, put once per frame
Display:    200×200 → 480×480 CSS  — image-rendering: pixelated for sharp scaling

Per frame:
  • 3 sweeps × 40,000 attempts = 120,000 Metropolis steps
  • Per step: 1 random site, 4 array reads, 1 integer multiply,
    1 conditional, 0–1 random draws (precomputed Boltzmann)
  • Render: 40,000 pixel writes + 1 putImageData
  • HUD: every 8 frames (|M| = |totalM| / 40,000)

Precomputed Boltzmann factors (updated on T change):
  exp4 = exp(−4 / T)   — acceptance probability for ΔE = 4
  exp8 = exp(−8 / T)   — acceptance probability for ΔE = 8
  (ΔE = 0 always accepted; ΔE < 0 always accepted)

Running magnetization totalM is tracked incrementally:
  on flip: totalM += −2 · s_old
  cost: 1 addition per accepted flip (no full-grid sum per frame)

Built 2026-07-22 · Pure JavaScript · Canvas 2D · Zero backend · Live demo → · More questing →