A comprehensive C++ simulation of the 2D Ising Model on a square lattice using the Metropolis Monte Carlo algorithm. This project was developed as part of a Computational Physics course in 2017 to study phase transitions, critical behavior, and correlation functions in ferromagnetic systems.
- Overview
- Physics Background
- Repository Structure
- How to Compile and Run
- Automatic Plotting with Gnuplot
- Output Files & Observables
- Visualizing Snapshots & Making Movies
- System Requirements
- References
The 2D Ising model is a classic model in statistical mechanics used to describe ferromagnetism and phase transitions. It exhibits a second-order phase transition at a critical temperature
This repository provides multiple C++ programs that simulate the 2D Ising model on a square lattice (typically
- Mean Magnetization (
$\langle M \rangle$ ) vs. Temperature ($T$ ) - Magnetic Susceptibility (
$\chi$ ) - Spin-Spin Correlation Functions ($G(r)$)
- Relaxation and equilibrium dynamics over Monte Carlo steps (time)
- Critical exponent fitting (
$\beta$ )
All simulations use a high-quality random number generator based on the ran2() algorithm from Numerical Recipes in C++.
The energy of the system of spins
where:
-
$J > 0$ is the ferromagnetic coupling constant. -
$\langle i, j \rangle$ denotes summation over nearest neighbor pairs. -
$h$ is the external magnetic field. - Spins are arranged on a 2D grid with periodic boundary conditions:
$s_{i+L, j} = s_{i, j}$ and$s_{i, j+L} = s_{i, j}$ .
- Select a spin
$s_i$ at random. - Calculate the energy change
$\Delta E$ if the spin is flipped:$\Delta E = 2 s_i (h + J \sum_{j \in \text{neighbors}} s_j)$ . - Accept the spin flip with probability:
$$P_{\text{accept}} = \min\left(1, e^{-\frac{\Delta E}{k_B T}}\right)$$ - A Monte Carlo Step (MCS) consists of repeating this trial flip
$L^2$ times (so that on average each spin is given one chance to flip).
| File | Description |
|---|---|
Findig_equlibrum.cpp |
Investigates the time (MCS) required to reach equilibrium at different temperatures ( |
Ising _part_B_sus_magPerT_Critical_point.cpp |
Measures susceptibility |
Ising_final_correlation_snapshot_sus_varianc_movie.cpp |
Full simulation: computes magnetizations, susceptibility, variance, and correlation functions at temperatures |
Correlation_in_Critical_Point.cpp |
Computes the spin-spin correlation function |
movie.gp |
Gnuplot script to batch-process snapshot data files and generate sequential PNG frames in a pic/ directory. |
video2.m |
MATLAB/Octave script to read snapshot.txt and generate an AVI video of the spin grid evolution. |
Report/latex-report-sample.tex |
Persian LaTeX source code for the academic course report. |
Report/latex-report-sample.pdf |
Compiled PDF report in Persian describing results, figures, and calculations. |
To compile and run any of the simulation files on Linux or macOS, use a C++ compiler (g++) with optimization flags:
# 1. Compile the equilibrium finder
g++ -o equlibrium.out Findig_equlibrum.cpp -O2 -Wall
./equlibrium.out
# 2. Compile the critical point susceptibilty finder
g++ -o partB.out "Ising _part_B_sus_magPerT_Critical_point.cpp" -O2 -Wall
./partB.out
# 3. Compile the full Ising simulation (generates movies, snapshots, correlations)
g++ -o ising_full.out Ising_final_correlation_snapshot_sus_varianc_movie.cpp -O2 -Wall
./ising_full.out
# 4. Compile the correlation at critical point simulation
g++ -o correlation.out Correlation_in_Critical_Point.cpp -O2 -Wall
./correlation.outThe C++ files are designed to produce output files that contain both gnuplot scripting headers and numerical data tables in the same file.
At the end of the simulation execution, the C++ code automatically executes shell commands using the system() function to call gnuplot directly:
system("gnuplot 'magnTemp.txt'");This will automatically generate visual PNG plots in your directory without requiring manual script editing!
Make sure gnuplot is installed on your system and available in your environment path:
- macOS:
brew install gnuplot - Ubuntu/Debian:
sudo apt-get install gnuplot
When the simulations run, they produce the following files:
-
magnTemp.txt/MagnPerTemp.png: Magnetization$M$ vs. Temperature$T$ . -
Susceptibility.txt/Susceptibility.png: Magnetic susceptibility$\chi$ vs.$T$ . -
VarMagnetismPerT.txt/varianceOFmagnetism.png: Magnetization variance vs.$T$ . -
magnTime*.txt/magnTime*.png: Magnetization vs. Monte Carlo Steps (MCS) showing equilibrium time at a given$T$ . -
Corelationdata*.txt/Corelationdata*.png: Spatial spin correlation$G(r) = \langle s_0 s_r \rangle$ vs. distance$r$ .
The full simulation (Ising_final_correlation_snapshot_sus_varianc_movie.cpp) outputs snapshots of the lattice configurations at regular intervals to files named snapshot[T]-[step].txt.
The script movie.gp can generate PNG images from the snapshot files:
- Create a
pic/directory:mkdir pic - Run the gnuplot script:
gnuplot movie.gp - Convert the generated sequential PNG files to a video/GIF using a utility like
ffmpegorconvert(ImageMagick):convert -delay 5 -loop 0 pic/*.png movie.gif
The MATLAB script video2.m reads snapshot.txt directly, reconstructs the imshow(), and compiles them into a video file named spinvideo.avi.
- Compiler: Any standard C++ compiler supporting C++11 (like
g++orclang++). - Plotting:
gnuplotversion 5.0+ (required to run the automated plot generation). - Video Generation:
MATLAB/OctaveorImageMagick/ffmpeg.
- Press, W. H., Teukolsky, S. A., Vetterling, W. T., & Flannery, B. P. (1992). Numerical Recipes in C: The Art of Scientific Computing. Section 7.1:
ran2Random Number Generator. - Onsager, L. (1944). "Crystal Statistics. I. A Two-Dimensional Model with an Order-Disorder Transition". Physical Review. 65 (3–4): 117–149.