Skip to content

Classical Metropolis simulation of the 2D Ising model with Numba & Cython.

Notifications You must be signed in to change notification settings

ruihao-li/2d-ising-mcmc

Repository files navigation

2d-ising-mcmc

Simulation of the Ising model on a square lattice with the Metropolis algorithm, which is a Markov chain Monte Carlo method for sampling the equilibrium distribution of a system. In addition to pure Python implementation (ising.py), the code is also implemented with Numba (ising_numba.py) and in Cython (ising_cython.pyx). The Cython code is parallelized with OpenMP.

Usage

The simulation can be simply run with python driver.py. One can change the parameter values therein.

Should any modifications be made to ising_cython.pyx, one would need to to run python setup.py build_ext --inplace in a CIL to re-compile it to C. Then the function run_ising_cython(...) can be imported in any Python script (see e.g., driver.py).

Results

Here's a plot showing the evolution of energy and magnetization expectations with temperature for different system sizes (L = 10, 20, 40, 80).

temp_evolution

Another plot showing the performance/speed gain using Numba and Cython, normalized to the pure Python implementation, for different system sizes (L = 2, 4, 6, 8, 10, 12):

speedup

Here is a GIF showing the evolution of the spin configuration (80 x 80) as temperature increases:

ising_spins_gif

About

Classical Metropolis simulation of the 2D Ising model with Numba & Cython.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published