This program simulates a simplified 2D Ising model with the Monte Carlo method. It uses NumPy to load a 2D spin lattice and Matplotlib to visualize the result. The program is also able to use PyCUDA for (optional) GPU acceleration.
NOTE: For small enough lattice dimensions and number of steps, computing on the GPU may seem slower due to host->device and device->host memory transfer.
In every step of the simulation, one spin is selected at random. Its energy is calculated with the given formula:
Where:
-
i, j are the indexes of the spin's row and column, respectively
-
s is the value of the given spin (either 1 or -1)
-
E is the energy of the spin's square
If the calculated energy is greater than 0, the spin will switch. Otherwise, it will switch with the probability given by the following formula:
Where T is the systems temperature.
First, clone the repository.
git clone https://github.com/Griger5/Monte_Carlo_Ising_Model.git
Then create and activate the virtual environment for the program.
python -m venv .venv
PowerShell:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
.venv\Scripts\Activate.ps1
cmd:
.venv\Scripts\activate.bat
bash/zsh:
source .venv/bin/activate
csh/tcsh:
source .venv/bin/activate.csh
After that is done, install all the required libraries using:
pip install -r requirements.txt
This program supports user command line arguments. When none are present, it will run with the default parameters listed below. If a GPU and CUDA drivers are detected, the simulation will run on the GPU. Otherwise it simply runs on the CPU.
- Number of rows: 200
- Number of columns: 200
- Number of steps in the simulation: 10^6 (10^8 with GPU present)
- Temperature: 2.0
- Spin values: Initialized randomly
When in the virtual environment, you can run the program with:
python ising_model.py [--args n] [--flags]
Loads the lattice from a file. The file should contain a flattened lattice (as in: '1 -1 1 -1 1 1 -1 1...'). If rows and columns are not specified by the user, the program will attempt to turn the loaded lattice into a square.
Specify the number of rows for the spin lattice.
Specify the number of columns for the spin lattice.
Specify the number of Monte Carlo steps in the simulation.
Specify the temperature for the simulation.
When present, produces an animation of the simulation instead of a "before and after" image. GPU acceleration is not supported for creating an animation.
When present, forces the program to compute on CPU.
python ising_model.py --rows 150 --cols 150 --temp 1.75 --anim