A high-performance C++/Python project for simulating the classical 2D XY model and studying the Berezinskii–Kosterlitz–Thouless (BKT) phase transition.
Note: The report is given in Final report.pdf.
- Wolff cluster algorithm — eliminates critical slowing down
- C++ acceleration via pybind11 —
float32SoA layout, AVX2,-O3 -ffast-math - Multi-process parallel data generation — one
(T, L)per task - Automatic observable recording — magnetization, energy, helicity modulus
- Analysis & plotting — finite-size scaling of thermodynamic quantities
.
├── cpp/ # C++ core (pybind11 module)
│ ├── xy.hpp # XY model header
│ ├── xy.cpp # Wolff cluster + observable computation
│ ├── bind.cpp # Python bindings
│ └── CMakeLists.txt # Build config (AVX2, LTO, OpenMP)
├── generate_data.py # Parallel MC data generator
├── data/ # Raw simulation output (pkl)
│ └── thermal_L{L}/T{T}.pkl # One file per (L, T)
├── drawer/
│ ├── analysis.py # Shared loader & derived-quantity computation
│ ├── plot_thermal.py # M, χ, E, C_v vs T
│ ├── plot_helicity.py # Helicity analysis: raw + interpolation + BKT line + T_KT
│ ├── plot_eta_chi.py # η(T) from χ finite-size scaling
| └── animation_spin.py # gives vortex animation
└── run_all.sh # One-shot: generate → plot → save
# 1. Build the C++ module
cd cpp/build && cmake .. && cmake --build .
# 2. Test run (small params, ~1 minute)
bash run_all.sh --test
# 3. Full run (~10^5 samples per temperature)
bash run_all.shFigures are saved to drawer/fig/.
Each data/thermal_L{L}/T{T:.4f}.pkl contains:
{
"L": 16, "T": 0.1,
"m": np.array(Ntest * 1024), # magnetization magnitude raw sequence
"e": np.array(Ntest * 1024), # energy per site raw sequence
"h": np.array(Ntest * 1024), # per-site sin(Δθ) average raw sequence
"params": {"Ntest": 100, "spacing": 10, "hot_runs": 5, "flush_length": 1024}
}All averaging, error estimation, and derived quantities (χ, C_v, Binder ratio,
helicity modulus) are computed on-the-fly by analysis.py — the pkl files
store raw measurements only.
| Quantity | Formula |
|---|---|
| Magnetization | |
| Susceptibility | |
| Energy | |
| Heat capacity | |
| Binder ratio | |
| Helicity modulus |
- CMake ≥ 3.14, C++17 compiler
- Python 3.12, pybind11
- numpy, scipy, matplotlib (in
.venv, managed by uv)