Ising model simulation using the Swendsen-Wang algorithm.
The RunSW
script demonstrates basic usage of the core SwendsenWangIsing
function.
The RunSWParallel
script gives an example of how the MATLAB Parallel Computing Toolbox can be used to accelerate simulation of multiple temperatures.
This code was tested in MATLAB R2017b.
As the graph
and conncomp
functions were first introduced in R2015b, that is likely the oldest version of MATLAB in which this code will run.
The simulation code itself - SwendsenWangIsing
- depends only on the MATLAB standard library. The RunSW
and RunSWParallel
scripts depend on the utilities in the utils
folder, and RunSWParallel
additionally uses parfor
from the MATLAB Parallel Computing Toolbox.
SwendsenWangIsing
takes five input arguments:
N
: The total dimensionality of the system.T
: The temperature of the system.J
: TheN
byN
interaction matrix defining the quadratic energy form. Currently, only non-negative interactions, which make it energetically favorable for spins to align, are supported. This implementation is designed for the case in whichJ
is sparse, that is, that each node interacts only with a small number of other nodes.nIter
: The number of iterations for which the simulation is run.displayIter
: An optional argument indicating the interval at which to print updates to the terminal. IfdisplayIter
is not specified, it defaults to zero, and no updates are displayed.
Once the simulation is complete, SwendsenWangIsing
returns up to five output arguments:
E
: AnnIter
by 1 vector of the energy at each iteration.M
: AnnIter
by 1 vector of the magnetization at each iteration.x
: AnN
by 1 vector of the final spin state.PRNGState
: The initial state of the MATLAB pseudo-random number generator, including the seed.X
: AnnIter
byN
matrix containing the spin states at every iteration. If only four output arguments are requested, only the current spin state is stored, increasing the speed and memory efficiency of the simulation.
The RunSW
and RunSWParallel
scripts demonstrate the usage of SwendsenWangIsing
to simulate the Ising model on a square lattice, including examples of how to construct the inputs and plot the outputs.
-
Swendsen, Robert H., and Jian-Sheng Wang. "Nonuniversal critical dynamics in Monte Carlo simulations." Physical Review Letters 58, no. 2 (1987): 86. (Link)
-
Wang, Jian-Sheng, and Robert H. Swendsen. "Cluster Monte Carlo algorithms." Physica A: Statistical Mechanics and its Applications 167, no. 3 (1990): 565-579. (Link)
-
Gilbert, John R., Cleve Moler, and Robert Schreiber. "Sparse matrices in MATLAB: Design and implementation." SIAM Journal on Matrix Analysis and Applications 13, no. 1 (1992): 333-356. (Link)
This project is licensed under the MIT License.