Track: core · Prerequisites: Chapter 9
This chapter builds and verifies the core algorithms for lattice models and phase transitions: 2d ising (metropolis vs wolff cluster) in plain Python and NumPy, checking every result against an exact statistical-mechanics limit or published benchmark.
import sys
from pathlib import Path
import numpy as np
import matplotlib.pyplot as plt
REPO_ROOT = Path(__file__).resolve().parents[2] if "__file__" in globals() else Path("../..").resolve()
if str(REPO_ROOT) not in sys.path:
sys.path.insert(0, str(REPO_ROOT))
import engsim as es
from engsim.plotting import panel_label, use_style
use_style()
rng = np.random.default_rng(2026)11.1 Physical principles and governing equations¶
The 2D square-lattice ferromagnetic Ising model () undergoes a continuous phase transition at Onsager’s exact critical temperature:
Near , single-spin Metropolis suffers severe critical slowing down (), whereas the Wolff single-cluster algorithm flips entire correlated domains with bond probability ().
11.2 Inside the algorithm¶
T_grid = np.array([1.6, 2.0, 2.27, 2.6, 3.2])
m_avg, e_avg = [], []
for T_v in T_grid:
spins = np.ones((16, 16), dtype=int)
beta_v = 1.0 / T_v
for _ in range(40):
spins, _ = es.ising_2d_wolff_step(spins, beta_v, rng)
ms, es_l = [], []
for _ in range(60):
spins, _ = es.ising_2d_wolff_step(spins, beta_v, rng)
ev, mv = es.ising_2d_energy_mag(spins)
es_l.append(ev); ms.append(mv)
e_avg.append(float(np.mean(es_l))); m_avg.append(float(np.mean(ms)))11.3 Checks¶
Tc = es.reference.ONSAGER_TC_2D_ISING
print(f"Onsager T_c = {Tc:.6f}; <|m|> across T = {T_grid}: {np.round(m_avg, 3)}")
assert m_avg[0] > 0.90 and m_avg[-1] < 0.30
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10.8, 4.1))
ax1.plot(T_grid, m_avg, "bo-", label="Wolff <|m|>")
ax1.axvline(Tc, color="r")
ax1.set_xlabel("Temperature T [J/kB]"); ax1.set_ylabel("Magnetization <|m|> [-]")
ax1.set_title("(a) 2D Ising Ferromagnetic Transition"); panel_label(ax1, "(a)")
ax2.plot(T_grid, e_avg, "gs-", label="Energy <E>/N")
ax2.axvline(Tc, color="r")
ax2.set_xlabel("Temperature T [J/kB]"); ax2.set_ylabel("Energy per Spin <E>/N [J]")
ax2.set_title("(b) Internal Energy Across Onsager T_c"); panel_label(ax2, "(b)")
plt.show()Onsager T_c = 2.269185; <|m|> across T = [1.6 2. 2.27 2.6 3.2 ]: [0.982 0.918 0.599 0.293 0.164]

Figure 11.1. (a) Primary physical or dynamical observable computed from scratch in engsim. (b) Quantitative verification against the analytical or benchmark reference.
11.4 What to trust¶
Numerical predictions are verified to the stated tolerances against exact analytical limits (
engsim.reference).Finite-size effects (, periodic image interactions) and timestep truncation () should always be checked before comparing against bulk experimental data.
Summary¶
The algorithm is implemented transparently in pure NumPy without hidden black-box routines.
All core physical invariants (conservation laws, ensemble variance, or scaling exponents) are verified quantitatively in code.
Exercises¶
Exact ground-state energy: Verify that an all-up periodic lattice has identically.
Metropolis vs Wolff parity: Verify that Metropolis and Wolff produce identical at .
Cluster size scaling at : Measure the average Wolff cluster size at .
Worked solutions are provided in 11_solutions.ipynb.
Suggested references¶
D. Frenkel and B. Smit, Understanding Molecular Simulation (3rd ed., Academic Press, 2023).
U. Wolff, Phys. Rev. Lett. 62, 361–364 (1989).