This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

← Posts

demoAIStatistical mechanics

Metropolis sampling finds Onsager's magnetization

A 16 by 16 grid of spins, each flipped at random by the Metropolis rule, settles at to an average magnetization of . The exact infinite-lattice answer, from Onsager and Yang, is . The critical temperature is where that formula reaches zero.

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

The test's tolerance is three standard errors, measured from the run itself by batch means: 20 batches of 60 sweeps each. A separate Metropolis code (posts/refs/metropolis-ising-against-onsager.py, 64 chains of 20000 sweeps) supports that choice three ways. It finds the correlation time of to be 1.3 sweeps, so the batches are independent. It predicts a standard error of 0.00131 for a run of this length, and the run measures 0.00130. And it finds that the 16 by 16 lattice's average sits below the infinite lattice's, a hundred times smaller than the statistical error, so the finite size does not need an allowance of its own at this temperature.

The model

The Ising model is a grid of little magnets, each pointing up () or down (). It shows how a material with only short-range forces ends up magnetized on its own, and why only below a certain temperature. Each spin interacts with its four neighbors, and the energy of an arrangement is

summed over neighboring pairs, so two aligned neighbors lower the energy by and two opposed ones raise it by . The program works in units where and Boltzmann's constant , so temperature is measured in units of .

At temperature , an arrangement with energy occurs with probability proportional to . The quantity of interest is the magnetization , the average spin. Summing over all arrangements of a 16 by 16 grid is impossible, so the program samples arrangements instead.

The Metropolis rule

Visit each spin in turn and propose flipping it. The flip changes the energy by , which is one of . Accept the flip if ; otherwise accept it with probability . Repeating this visits arrangements with exactly the Boltzmann probabilities in the long run. One pass over all 256 spins is a sweep. The edges wrap around (periodic boundaries), so every spin has four neighbors.

Onsager's answer

Lars Onsager solved the two-dimensional Ising model exactly in 1944, and C. N. Yang derived its magnetization in 1952. Below the critical temperature ,

and above it . The formula reaches zero where , which gives . Before running: what does the exponent say about the curve near ? Close to , behaves like , so it drops to zero almost vertically.

Printed

TC: 2.26919
sinh_at_tc: 1

Tests 1 of 1 pass

  • the critical temperature is 2 / ln(1 + sqrt 2) = 2.269185314213022, where sinh(2/T) = 1 (passes)

The measurement

The grid starts with every spin up, runs 200 sweeps to forget that start, then measures after each of 1200 more sweeps. The absolute value is there because a finite grid can flip as a whole from mostly up to mostly down; at that is rare, but makes the answer independent of it.

The 1200 values are not independent: one sweep changes only a few spins, so consecutive values are close. The standard error comes from batch means: average each block of 60 sweeps, and treat the 20 block averages as independent samples. That holds when a block is much longer than the time the grid takes to forget its state, which here is about one sweep.

Printed

m_mc: 0.956686
sigma: 0.00130175
m_exact: 0.956857
m_gap: 0.000170819
gap_in_sigma: 0.131223

Tests 3 of 3 pass

  • at T = 1.8 the sampled |m| is Onsager's 0.956857 within three standard errors (passes)
  • the run and Onsager's value differ by 0.00017, about an eighth of one standard error (passes)
  • the standard error is under 0.002, near the reference code's 0.00131 for 1200 sweeps (passes)

The run gives with a standard error of (m_mc, sigma), and Onsager's value is (m_exact): they differ by , about an eighth of one standard error.

The whole curve

The next cell repeats the measurement at ten temperatures, with shorter runs (100 sweeps to settle, 200 to measure), and draws them over Onsager's curve. Before looking: the formula says above . Will the 16 by 16 grid show zero there?

Printed

m_T: [0.979375, 0.955586, 0.915586, 0.873008, 0.800312, 0.692422, 0.367266, 0.267656, 0.220078, 0.146563]
Magnetization of the 2D Ising model00.20.40.60.811.522.53Tc=2.269temperature T (units of J/kB)magnetization |m|
Onsager and Yang, infinite latticeMetropolis, 16 x 16 gridone run's spread, from 256 reference runs
The 16 x 16 grid below, near and above T_c
The grid at T = 1.8, below T_c
row
column
16 × 16 grid
The grid at T = 2.3, just above T_c
row
column
16 × 16 grid
The grid at T = 3.2, well above T_c
row
column
16 × 16 grid

Tests 1 of 1 pass

  • at every temperature the short run is within three spreads of the reference code's average (passes)

Below the points sit on the curve. Above it they do not reach zero: at the grid still has (m_T), and the reference code, running the same short protocol 256 times, averages there. Each point is within three of the reference's run-to-run spreads of its average; the spread is largest near , at and , where the grid wanders slowly between ordered and disordered states. The reason is the grid's size. Above the spins form patches of aligned spins that are uncorrelated with each other, and the net spin of 256 spins in a few dozen independent patches is a random sum, whose absolute value is of order one over the square root of the number of patches, not zero. On an infinite lattice that goes to zero; at it does not. The same effect rounds off the sharp drop at .

The three grids show the change: at a few flipped spins in a sea of one color; at more of them, in small clusters, though this run ended in one of its ordered stretches (the grid wanders between ordered and disordered states there, and another seed can show patches of every size); and at patches of both colors with no preferred direction.

A larger grid at the critical temperature

The last cell runs a 32 by 32 grid at itself, from a random start, and draws it every two sweeps. A cell is 1 where the spin is up and 0 where it is down. At the patches have no typical size: small ones flicker and large ones drift and merge, and wanders instead of settling.

A 32 x 32 grid at T_c, every two sweeps
row
column
40/40
32 × 32 grid · 40 frames
|m| of the 32 x 32 grid at T_c
|m|
00.10.20.320406080sweep|m|

Tests 1 of 1 pass

  • every cell of the critical grid is +1 or -1, and |m| is at most 1 (passes)

Checked when the site was built: 6 of 6 tests passed, in 4.83 s. Download 2026-09-25-metropolis-ising-against-onsager.viv