Simulating the Ising Model: Algorithms, Code, and Results
Introduction
The Ising model is a foundational statistical-physics model for ferromagnetism and phase transitions. It describes a lattice of spins si ∈ {+1, −1} with nearest-neighbor interactions. The Hamiltonian for the nearest-neighbor Ising model (no external field) is
where J>0 favors alignment. Simulating the Ising model numerically lets you study thermodynamic observables (energy, magnetization, specific heat, susceptibility) and critical behavior.
Goals and observables
- Compute equilibrium averages: energy ⟨E⟩, magnetization ⟨M⟩.
- Estimate fluctuations: specific heat CV = (⟨E^2⟩−⟨E⟩^2)/(k_B T^2), susceptibility χ = (⟨M^2⟩−⟨M⟩^2)/(k_B T).
- Observe phase transition (2D square lattice critical temperature Tc ≈ 2.269 J/kB).
- Measure autocorrelation times and finite-size effects.
Algorithms overview
- Metropolis-Hastings (single-spin flips)
- Proposes flipping a spin; accepts with probability min(1, e^{−ΔE/(k_B T)}).
- Simple and broadly useful; critical slowing down near Tc (autocorrelation ∝ L^z with z≈2).
- Glauber dynamics
- Similar to Metropolis; uses different acceptance formula with detailed balance; comparable performance.
- Wolff cluster algorithm
- Builds and flips clusters of aligned spins using bond probability p = 1−e^{−2J/(k_B T)}.
- Greatly reduces critical slowing down (dynamic exponent z small), ideal near Tc.
- Swendsen–Wang
- Builds multiple Fortuin–Kasteleyn clusters each update; similar advantages to Wolff.
- Exact methods (2D).
- For 2D Ising without field, there are analytic solutions (Onsager) and transfer-matrix techniques for small widths—not a general simulator but useful for validation.
Implementation details (practical choices)
- Lattice: 2D square lattice with periodic boundary conditions (PBC) is standard.
- Initialization: random (hot) for equilibration or all-up (cold). Use sufficient equilibration steps (e.g., 5×10^3–10^5 sweeps depending on L and T).
- Sweep definition: attempting N = L^2 single-spin updates (Metropolis/Glauber) or N_cluster ~ O(1) cluster flips for Wolff measured differently.
- RNG: use high-quality pseudorandom generator (Mersenne Twister or equivalent).
- Measurements: sample every τ steps where τ > autocorrelation time to get nearly independent samples; compute running averages and error estimates (blocking or bootstrap).
- Units: set J = 1 and kB = 1 to report temperature in units of J/kB.
Reference Python code (Metropolis and Wolff)
python
# metropolis_vs_wolff.py import numpy as np import random from collections import deque def neighbors(L): # return neighbor index deltas for 2D square lattice return [(-1,0),(1,0),(0,-1),(0,1)] def energy(lattice, J=1.0): L = lattice.shape[0] E = 0 for i in range(L): for j in range(L): s = lattice[i,j] E -= J s (lattice[(i+1)%L,j] + lattice[i,(j+1)%L]) return E def magnetization(lattice): return lattice.sum() def metropolis_step(lattice, T, J=1.0): L = lattice.shape[0] for _ in range(LL): i = np.random.randint(0,L); j = np.random.randint(0,L) s = lattice[i,j] nb = lattice[(i+1)%L,j] + lattice[(i-1)%L,j] + lattice[i,(j+1)%L] + lattice[i,(j-1)%L] dE = 2 J s nb if dE <= 0 or np.random.rand() < np.exp(-dE/T): lattice[i,j] = -s def wolff_step(lattice, T, J=1.0): L = lattice.shape[0] p_add = 1 - np.exp(-2J/T) i0 = np.random.randint(0,L); j0 = np.random.randint(0,L) cluster_spin = lattice[i0,j0] stack = [(i0,j0)] cluster = set([(i0,j0)]) while stack: i,j = stack.pop() for di,dj in [(-1,0),(1,0),(0,-1),(0,1)]: ni, nj = (i+di) % L, (j+dj) % L if (ni,nj) not in cluster and lattice[ni,nj] == cluster_spin: if np.random.rand() < p_add: cluster.add((ni,nj)) stack.append((ni,nj)) for (i,j) in cluster: lattice[i,j] = -lattice[i,j] return len(cluster) # Example run def run_simulation(L=32, T=2.269, steps=20000, equil=5000, algo=‘metropolis’): lattice = np.random.choice([1,-1], size=(L,L)) E_hist = []; M_hist = [] for t in range(steps): if algo==‘metropolis’: metropolis_step(lattice, T) else: wolff_step(lattice, T) if t >= equil: E_hist.append(energy(lattice)) M_hist.append(abs(magnetization(lattice))) E_avg = np.mean(E_hist); M_avg = np.mean(M_hist) Cv = (np.var(E_hist)) / (TT) / (LL) Chi = (np.var(M_hist)) / T / (LL) return {‘E’:E_avg/(LL), ’M’:M_avg/(LL), ‘Cv’:Cv, ‘Chi’:Chi} if name == ‘main’: res = run_simulation(L=32, T=2.269, steps=20000, equil=5000, algo=‘wolff’) print(res)
Suggested experiments and results to produce
- Temperature sweep: simulate a range T ∈ [1.0, 3.5] with fine spacing near Tc and plot ⟨|M|⟩, Cv, χ versus T for several L (e.g., L=16,32,64).
- Finite-size scaling: locate pseudo-critical temperatures Tc(L) from peaks of Cv or χ and extrapolate Tc(L -> ∞).
- Autocorrelation: measure integrated autocorrelation times τint for energy and magnetization for Metropolis vs Wolff near Tc.
- Performance: compare wall-clock time per independent sample for Metropolis and Wolff; show Wolff produces decorrelated samples far faster near criticality.
Typical expected findings
- 2D Ising shows a second-order phase transition at Tc ≈ 2.269 (J/kB = 1 units).
- Metropolis exhibits critical slowing down (large τint near Tc); cluster algorithms (Wolff/Swendsen–Wang) mitigate this effectively.
- Specific heat shows a peak that sharpens with L; magnetization drops rapidly around Tc.
- Finite-size scaling collapses with known critical exponents (β=⁄8, ν=1).
Tips for robust results
- Use many independent runs and compute error bars (bootstrap or jackknife).
- Ensure equilibration is long enough; verify by starting from cold and hot initial states and checking convergence.
- Use improved estimators where available (e.g., cluster algorithms give better variance properties).
- Store random seeds for reproducibility.
Extensions
- Add external magnetic field h: H = −J Σ⟨i,j⟩ s_i s_j − h Σ_i s_i to study hysteresis.
- Simulate 3D lattice (no exact solution; larger critical exponents and higher Tc).
- Study diluted Ising models, Potts models, or quantum Ising variants (requires different algorithms).
Conclusion
Combining Metropolis (or Glauber) for general-purpose sampling and Wolff/Swendsen–Wang for critical-region efficiency gives a practical and powerful simulation toolkit. Systematic temperature sweeps, finite-size scaling, and careful statistical analysis yield accurate characterization of phase behavior and critical exponents.
Leave a Reply