This project implements a Monte Carlo simulation of the 2D Ising model using the Metropolis algorithm. The Ising model is a mathematical model used to study the behavior of ferromagnetic materials and phase transitions.
The 2D Ising model consists of a lattice of spins that can take values of +1 or -1. Each spin interacts with its nearest neighbors, and the system evolves according to the Metropolis algorithm. The simulation computes various physical quantities such as energy, magnetization, and specific heat as a function of temperature.
The Hamiltonian
Here, +1
or -1
.
- Implements the Metropolis algorithm for efficient Monte Carlo sampling
- Utilizes Numba JIT compilation for accelerated performance
- Computes energy, magnetization, and specific heat of the system
- Allows customization of lattice size, number of Monte Carlo sweeps, temperature range, and external magnetic field strength
- Provides visualizations of the computed physical quantities
- Python 3.x
- NumPy
- Matplotlib
- Numba
- tqdm
-
Clone the repository:
git clone https://github.com/MohamedElashri/IsingModel.git
-
Install the required dependencies:
pip install numpy matplotlib numba tqdm
-
Run the simulation script:
python src/IsingModel.py
-
Adjust the simulation parameters in the script as desired:
L
: Lattice size (width)n
: Number of Monte Carlo sweepsTemperature
: Temperature range (includes critical temperature)B
: Strength of the external magnetic fieldmu
: Magnetic moment of each spin
An interactive Jupyter Notebook version of the simulation is also available. You can run the notebook on Google Colab by clicking on the following badge:
The simulation generates plots of the physical quantities (energy, magnetization, and specific heat) as a function of temperature. The plots provide insights into the phase transition and critical behavior of the 2D Ising model.
Significant optimization efforts have been made to improve the performance of the simulation. The use of Numba's JIT compiler has greatly reduced the execution time, allowing for larger lattice sizes and more Monte Carlo sweeps. The code has been optimized to minimize nested loops and leverage vectorized operations.
To install Numba on a Mac M1 machine, follow these steps:
-
Ensure you are using the Python 3 version from Homebrew and not the one that comes with the OS. Add the following line to your
.bashrc
or.zshrc
file:export PATH="/usr/local/opt/python/libexec/bin:$PATH"
-
Install the required packages:
python3 -m pip install conda python3 -m pip install cytoolz python3 -m conda config --add channels conda-forge python3 -m conda install -c numba numba
This project is licensed under the MIT License.
- The Ising model implementation is based on the work of Wilhelm Lenz and Ernst Ising.
- The Metropolis algorithm is a widely used Monte Carlo method for simulating physical systems.