Skip to content

MRHemmati/2D-Ising

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

2D Ising Model Simulation

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.


Table of Contents


Overview

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 $T_c$ (known as the Onsager transition temperature).

This repository provides multiple C++ programs that simulate the 2D Ising model on a square lattice (typically $256 \times 256$) with periodic boundary conditions. The programs calculate thermodynamic observables such as:

  • 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++.


Physics Background

Hamiltonian

The energy of the system of spins $s_i = \pm 1$ on a square lattice is given by the Hamiltonian:

$$H = -J \sum_{\langle i, j \rangle} s_i s_j - h \sum_i s_i$$

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}$.

Metropolis Algorithm

  1. Select a spin $s_i$ at random.
  2. 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)$.
  3. Accept the spin flip with probability: $$P_{\text{accept}} = \min\left(1, e^{-\frac{\Delta E}{k_B T}}\right)$$
  4. 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).

Repository Structure

File Description
Findig_equlibrum.cpp Investigates the time (MCS) required to reach equilibrium at different temperatures ($T = 1.5, 2.0, 2.25, 4.0$).
Ising _part_B_sus_magPerT_Critical_point.cpp Measures susceptibility $\chi$, magnetization, and variance across $T \in [2.0, 3.0]$ with step $0.01$ to identify $T_c$ and fit critical exponents.
Ising_final_correlation_snapshot_sus_varianc_movie.cpp Full simulation: computes magnetizations, susceptibility, variance, and correlation functions at temperatures $T = 1.5, 2.0, 2.25, 2.5, 3.0$ over a temperature range $[0.10, 5.10]$. Generates lattice spin configuration snapshots.
Correlation_in_Critical_Point.cpp Computes the spin-spin correlation function $G(r)$ specifically at the critical temperature $T = 2.48$.
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.

How to Compile and Run

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.out

Automatic Plotting with Gnuplot

The 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!

Prerequisites for Plotting:

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

Output Files & Observables

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$.

Visualizing Snapshots & Making Movies

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.

Method A: Gnuplot (movie.gp)

The script movie.gp can generate PNG images from the snapshot files:

  1. Create a pic/ directory: mkdir pic
  2. Run the gnuplot script: gnuplot movie.gp
  3. Convert the generated sequential PNG files to a video/GIF using a utility like ffmpeg or convert (ImageMagick):
    convert -delay 5 -loop 0 pic/*.png movie.gif

Method B: MATLAB/Octave (video2.m)

The MATLAB script video2.m reads snapshot.txt directly, reconstructs the $L \times L$ grid frames using imshow(), and compiles them into a video file named spinvideo.avi.


System Requirements

  • Compiler: Any standard C++ compiler supporting C++11 (like g++ or clang++).
  • Plotting: gnuplot version 5.0+ (required to run the automated plot generation).
  • Video Generation: MATLAB / Octave or ImageMagick / ffmpeg.

References

  1. 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: ran2 Random Number Generator.
  2. Onsager, L. (1944). "Crystal Statistics. I. A Two-Dimensional Model with an Order-Disorder Transition". Physical Review. 65 (3–4): 117–149.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages