Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 39 · Non-Equilibrium and Monte Carlo Methods

Monte Carlo Simulation of the 2D Ising Model

Simulate the 2D Ising model with the Metropolis algorithm in NumPy, watch domains form, measure magnetization, energy, specific heat and susceptibility, and see critical slowing down.

Now we apply the Metropolis algorithm to the 2D Ising model on an L × L square lattice with periodic boundaries. You will see ordered and disordered phases emerge, measure thermodynamic quantities as functions of temperature, and compare them with Onsager's exact results from the previous chapter section. All code uses J = 1 and k = 1, so temperatures are in units of J/k and the critical temperature is T_c = 2/ln(1 + √2) ≈ 2.269.

The update rule

Flipping one spin s_i → -s_i changes the energy by

Text
ΔE = 2 J s_i Σ_{neighbors j} s_j

because only the four bonds touching s_i change, each from -J s_i s_j to +J s_i s_j. For a square lattice, ΔE can only take the values -8, -4, 0, 4, 8 (in units of J). A flip is accepted with probability min(1, e^(-ΔE/T)).

A one-spin-at-a-time Python loop is slow. A standard trick is checkerboard updating: color the sites like a chess board. Black sites have only white neighbors, so all black sites can be updated simultaneously (they do not influence one another), then all white sites. This satisfies detailed balance for each sub-step and is easily vectorized with NumPy (we need L even for the periodic lattice to be consistently two-colored).

A sweep here means one update of every spin (two half-sweeps, black then white). Averages are computed after discarding equilibration sweeps.

Watching domains form

The code below runs a 64 × 64 lattice at three temperatures from a random start, and draws snapshots. Below T_c large domains form and coarsen; at T_c there are domains on every scale; above, only small fluctuations remain.

Python Ising snapshots below, at and above the critical 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.

At T = 1.5 the lattice is nearly uniform (the exact infinite-lattice m is about 0.986). From a random start, a finite lattice sometimes freezes into stripes of up and down domains; if a snapshot shows stripes, that is a metastable state, not a failure of the code. At T = 4.0 the picture is salt-and-pepper.

Measuring thermodynamics

The thermodynamic quantities that diagnose the transition come from fluctuations (with E the total energy, M the total magnetization, N = L², k = 1):

Text
specific heat per spin:  C = ( ⟨E²⟩ - ⟨E⟩² ) / (N T²)
susceptibility per spin: χ = ( ⟨M²⟩ - ⟨|M|⟩² ) / (N T)

Both peak near T_c and the peaks grow with L. We use |M| because a finite lattice flips its overall sign over time. The code scans temperature for L = 16, starting each temperature from the previous configuration to save equilibration time.

Python Temperature scan: magnetization, energy, specific heat, susceptibility

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 magnetization falls from about 1 at low T to near 0 at high T, with the steepest change near T_c. The energy per spin goes from about -2 (all aligned) toward 0, and both C and χ have a peak close to T_c = 2.269. On a small lattice the peaks are broad and shifted slightly above T_c; try L = 32 to see them sharpen.

Cooling gradually from a hot state (as the code does) avoids many trapped stripe configurations. Quenching from a hot random state straight to a low temperature may leave domain walls that take a very long time to disappear.

Critical slowing down

Right at T_c, correlated domains span the whole lattice, and single-spin flips take longer and longer to decorrelate. The autocorrelation time grows like ξ^z with z ≈ 2.17 for the 2D Ising model with Metropolis dynamics. This is critical slowing down: simulations near T_c need many more sweeps. Cluster algorithms (Swendsen-Wang and Wolff) flip whole correlated clusters and cut the exponent z to a much smaller value, which is why they are standard for critical-region studies.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. In the 2D square-lattice Ising model, which values of ΔE (in units of J) occur when flipping a single spin?

    ΔE = 2 s Σ s_j and the neighbor sum is -4, -2, 0, 2 or 4, so ΔE ∈ {-8, -4, 0, 4, 8}.
  2. 2. Why does checkerboard updating work?

    A black site's four neighbors are all white, so flipping several black sites at once changes no other black site's ΔE.
  3. 3. Why do we measure ⟨|M|⟩ rather than ⟨M⟩ in a finite simulation?

    By symmetry the exact finite-system ⟨M⟩ is zero; |M| captures the order.
  4. 4. What is critical slowing down?

    Large correlated domains take many local flips to rearrange; cluster algorithms address this.

Summary

  • ΔE = 2 J s_i Σ s_j; accept with min(1, e^(-ΔE/T)).
  • Checkerboard updates make Metropolis fast in NumPy.
  • Snapshots show domains below T_c, scale-free structure at T_c and noise above.
  • C and χ peak near T_c ≈ 2.269 J/k, and the peaks grow with L; near T_c local updates suffer critical slowing down.

Next: Random Walks and Diffusion