Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 38 ยท Projects and Practice

Project: The Ising Model of Magnetism

Simulate the 2-D Ising model with the Metropolis algorithm, watch magnetism appear below a critical temperature and compare with Onsager's exact result.

Why does a piece of iron become a magnet when cooled, and stop being one when heated? The Ising model is the simplest model that captures this. It is also the birthplace of Monte Carlo methods in statistical physics: it put the Metropolis algorithm that you learned in the MCMC chapter to work on a real physical system.

In this project you will simulate it on a grid of tiny "spins" and watch a phase transition appear in your own data.

The model

Place a spin s on every cell of an L ร— L grid. Each spin is either +1 (up) or โˆ’1 (down). Neighboring spins prefer to agree. The energy of a configuration is

Text
E = โˆ’ J ยท ฮฃ_{โŸจi,jโŸฉ} s_i ยท s_j

where the sum runs over pairs of nearest neighbors (up, down, left, right) and J > 0 is the coupling. We use units with J = 1 and Boltzmann constant k = 1, and periodic boundaries (the grid wraps around like a torus), which avoids edge effects.

At temperature T, the probability of a configuration is proportional to the Boltzmann factor exp(โˆ’E/T): low-energy states are favored, but at high T thermal noise lets anything happen. We cannot sum over 2^(Lยฒ) configurations, but we can sample from this distribution with Metropolis:

  1. Pick a random spin.
  2. Compute the energy change ฮ”E if it were flipped. For spin s with neighbor sum nb, flipping gives ฮ”E = 2ยทsยทnb.
  3. If ฮ”E โ‰ค 0, accept the flip. Otherwise accept with probability exp(โˆ’ฮ”E/T).

Because only the ratio of Boltzmann factors enters, the intractable normalizing constant (the partition function) is never needed.

What to expect

At low temperature the spins align (all up or all down): the system is magnetized. At high temperature they point randomly and the average magnetization per spin is near zero. In between there is a sharp change at the critical temperature, which Lars Onsager computed exactly for the infinite 2-D square lattice:

Text
T_c = 2 / ln(1 + โˆš2) โ‰ˆ 2.269

Below T_c the exact spontaneous magnetization per spin is

Text
M(T) = ( 1 โˆ’ sinh(2/T)^(โˆ’4) )^(1/8)

We measure the average of the absolute value |m|, where m is the mean of all spins. (On a finite grid the magnetization can flip sign, so we use |m|.)

The simulation

A sweep is Lยฒ attempted flips, so on average each spin gets one chance. We use a 20ร—20 grid. At each temperature we equilibrate for 300 sweeps, then measure over 400 sweeps.

Python Magnetization against temperature

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The pattern should be clear: |m| near 1 at low T, close to the exact M(T) for T < T_c, then dropping to a small value at high T. On a finite grid it never becomes exactly zero (a random grid of 400 spins has |m| โ‰ˆ 0.04 just by chance), and the transition is rounded near T_c, which is why T = 2.3 shows a value between the extremes.

A picture of the transition

Spin patterns show what the numbers hide. Below T_c large domains of aligned spins; above, salt-and-pepper noise; near T_c clusters of every size.

Python Spin configurations at three temperatures

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Hot, cold or critical, the same ten lines of Metropolis produce all three regimes. At low temperature you may see a few large domains rather than a uniform grid, since a finite run can get stuck in stripes or islands that take a long time to disappear: a lesson about MCMC mixing from the diagnostics lesson.

Going further

  • Larger grids (L = 40, 80): the transition sharpens, and the measured magnetization near T_c gets closer to the infinite-lattice curve.
  • Specific heat and susceptibility: variances of energy and magnetization peak near T_c.
  • Critical slowing down: near T_c single-spin updates mix slowly; cluster algorithms (Swendsen-Wang, Wolff) were invented to fix this.
  • Other models: Potts, XY and Heisenberg models use the same framework with different spin values and energies.

The Ising model is a model, not iron. Real magnets differ in many details, but near a phase transition very different systems share the same large-scale behavior, one reason the model is so valuable.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Flipping spin s with neighbor sum nb changes the energy by:

    Energy is -J s * nb for the bonds of that spin; flipping reverses s, so the change is 2 s nb (with J = 1).
  2. 2. In the Metropolis step, a flip that raises the energy by dE > 0 is accepted with probability:

    That is the ratio of Boltzmann factors, so detailed balance holds for the Boltzmann distribution.
  3. 3. The 2-D Ising critical temperature (J = k = 1) is about:

    T_c = 2 / ln(1 + sqrt 2) = 2.269.
  4. 4. Why do we average |m| rather than m on a finite grid?

    Both all-up and all-down are equally likely, so the signed average would hover near zero.

Summary

  • The Ising model puts ยฑ1 spins on a lattice with energy โˆ’Jฮฃ s_i s_j and Boltzmann probability exp(โˆ’E/T).
  • Metropolis flips a spin with ฮ”E = 2ยทsยทnb, accepting with min(1, exp(โˆ’ฮ”E/T)), never needing the partition function.
  • Magnetization appears below T_c = 2/ln(1 + โˆš2) โ‰ˆ 2.269, following Onsager's exact M(T).
  • Finite grids round the transition; domains and slow mixing near T_c show why diagnostics matter.

Next: Practice: Variance Reduction