A small package that provides a simple Ising model simulation on a squared lattice with the Metropolis algorithm.
Project description
EasyIsing
This is a Python module that performs the simulation of the Ising model on a square lattice using the Metropolis algorithm. Initially, this tool was made to be used in courses on data analysis or statistical mechanics so that students could generate their own data without having to build a simulator from scratch.
Nevertheless, it contains reasonably clean code with minimal optimizations that can be copied and modified, making it easier for undergraduate students to start their projects.
It is important to note that this module describes the Ising system in terms of a reduced set of variables, which is a common practice. In other words, $\frac{E}{J}({\sigma}) = -\sum_{(i,j)}\sigma_i \sigma_j - \frac{H}{J} \sum_i \sigma_i$ , where the reduced energy is denoted as $\frac{E}{J}$, the reduced magnetic field as $\frac{H}{J}$, and the reduced temperature $t$ is the inverse of $\beta$, where $\beta = \frac{J}{T k_B}$ and $k_B$ is the Boltzmann constant.
The square lattice is a network of sites, each of which has four immutable connections/interactions with other sites, known as nearest neighbors, indicated by $(i,j)$ under the summation symbol. This is translated into an array of size $L$ by $L$, with periodic boundary conditions, where the elements are integers with values of $\sigma=1$ or $\sigma=-1$, representing a spin-up or a spin-down, respectively.
There is some freedom in the Metropolis algorithm, allowing for the choice of how to change spins in any way that satisfies the detailed balance condition. Typically, one site is chosen randomly because there are no particular reasons to favor one site over another. All sites are equivalent and, due to periodic boundary conditions, are indistinguishable even spatially.
The checkerboard decomposition parallelization approach consists of separating the lattice into two sub-lattices of the same size, like a chessboard. Since the interactions are restricted to first neighbors, there is no interaction between sites of the same sub-lattice. One can update all sites of a sub-lattice at once, which is a common approach to parallelizing Ising-like models.
Both random sequential and checkerboard parallelization should have the same equilibrium properties since both satisfy the detailed balance condition.
This library contains four classes, each with a different implementation:
Class | Name | Description |
---|---|---|
EasyIsing.Ising | Python | Vanilla, traditional random sequential site selection. |
EasyIsing.IsingC | C | Same as above, but in C. The source code will be compiled and saved in the working directory and called through numpy.ctypeslib. |
EasyIsing.IsingNumpy | NumPy | Checkerboard decomposition model parallelization scheme using NumPy array functions. |
EasyIsing.IsingCupy | CuPy | Same as above, but in CuPy to run on the GPU. Note that this class will only be loaded if CuPy is detected. |
With different performances:
It is assumed that the reader/user is being instruted about Ising model and Metropolis Algorithm. So, no futher information will be provided.
Instalation
pip install EasyIsing
Usage
When imported, there are four classes of objects that can be used. The user just have to create an instance with the desired arguments. For example:
import EasyIsing
L = 10
age = 1000
sample = 1000
T = 2.1
H = 0
seed = 84598945
mac = EasyIsing.IsingNumpy(L, seed)
mac.update(age,T,H)
R = mac.sampling(sample,T,H)
print(R)
EasyIsing.Ising.sampling return a dict with the data accumulated.
{'temperature': 2.1,
'field': 0,
'age': 2000,
'sampleSize': 1000,
'length': 10,
'seed': 84598945,
'energy1': -167.976,
'magnet1': -88.684,
'energy2': 28579.168,
'magnet2': 7945.392,
'name': 'Numpy'}
There is a few examples in directory examples:
The user can override 'EasyIsing.Ising.sampling' to accumulate other data, or create custom sampling routine. For example, to include the spatial correlation function or 'energy4' and 'magnet4' to calculate Binder's coefficient.
Suggestions
Here are some suggestions for fun things students can do and analyze using the Ising model simulation:
- Observe the system "walking" to equilibrium with different initial states.
- Analyze the size of error bars for energy, magnetization, etc. as a function of sample size and lattice size.
- Explore specific heat and magnetic susceptibility using the Fluctuation-Dissipation Theorem and derivatives.
- Calculate the entropy of the system.
- Compare Binder's coefficient with histogram results.
- Analyze critical coefficients.
- Study cluster statistics.
- Analyze the radial distribution function and the Virial expansion.
- Explore magnetic hysteresis using the Magnetic Hysteresis Loop example provided in the EasyIsing module.
Conclutions
Conclusively, C is an excellent choice for single-threaded performance. I expect similar performance for FORTRAN, Rust, and C++. For larger systems, delegating the work to a GPU is by far the better option. Systems of size $L=128$ are big enough for most projects. As we can see, for my test machine, for size $L=128$, with the exception of pure Python, the performance of all examples is similar.
Several improvements can be made to increase performance, but they will cost development time and increase the chance of mistakes.
Machine specifications: Intel® Core™ i7-8700K CPU @ 3.70GHz, GeForce GTX 1080 Ti, Ubuntu 22.04.1 LTS, Python 3.10.4, gcc version 11.2.0 (Ubuntu 11.2.0-19ubuntu1)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file easyising-0.1.0.tar.gz
.
File metadata
- Download URL: easyising-0.1.0.tar.gz
- Upload date:
- Size: 981.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.23.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3dd4173b2c7a31d094df1b2f6265cf6ceeb62774f1e8273ad79da8e02a696754 |
|
MD5 | 25a83176791cd8d2fa6b2d3fc5255e57 |
|
BLAKE2b-256 | e210d9384843d3463e9c4554d3b5e9efdcab94c641536dd8cfba719f2dd7ab9c |
File details
Details for the file easyising-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: easyising-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.23.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c52b3cb638f9b40a330f03c8b8f24965dcee2cbe0bc05093f770a815e2b59706 |
|
MD5 | 1d4e64659202fec6d2a4887cfb5b841a |
|
BLAKE2b-256 | 5e17cbbbb20ac3ad16abd9a2b77ba486f68ffbfe21db69bba7cb085b9c89a64f |