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.
Open in the workbenchDownload .vivOutputs below are from the site build’s run; Run all runs it again in your browser.
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
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
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
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.
Tests 1 of 1 pass
- every cell of the critical grid is +1 or -1, and |m| is at most 1 (passes)