0% found this document useful (0 votes)
47 views9 pages

A Monte Carlo Implementation of The Ising Model in Python: Alexey Khorev

This document describes an implementation of the 2D Ising model using the Metropolis algorithm in Python. It discusses the theory behind the Ising model and ferromagnetism, outlines the simulation method, and presents results showing a visible phase transition near the calculated critical temperature on a 100x100 grid.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views9 pages

A Monte Carlo Implementation of The Ising Model in Python: Alexey Khorev

This document describes an implementation of the 2D Ising model using the Metropolis algorithm in Python. It discusses the theory behind the Ising model and ferromagnetism, outlines the simulation method, and presents results showing a visible phase transition near the calculated critical temperature on a 100x100 grid.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

A Monte Carlo Implementation of the Ising Model in

Python

Alexey Khorev
[email protected]

2017.08.29

Contents
1 Theory 1
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Calculated Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

2 Implementation 3

3 Results 3

4 Options for Further Development 7

5 Conclusion 7
Abstract
This article explores an implementation of the 2D Ising model using the Metropolis
algorithm in the Python programming language.
The goal of this work was to explore the scope of behaviours this model can demonstrate
through a simplistic implementation on a relatively low-end machine.
The Ising model itself is particularly interesting as it demonstrates relatively complex
behaviours of ferromagnetics e.g. the second-order phase transition in spite of its simplicity.
To study the specifics of this model several parameters were measured (namely the net
magnetization and energy, the specific heat and correlation function) and a visualization
of the grid was implemented.
The simulations demonstrated an easily observable phase transition near critical tem-
perature on a 100 × 100 Ising grid with the measured parameters behaving nearly as
predicted by the exact solution developed for this model.

1 Theory
1.1 Introduction
Ferromagnetics are a class of materials that can retain magnetization in absence of an external
magnetic field. Such materials exhibit long-range ordering on an atomic level resulting in for-
mation of domains - areas where unpaired electrons have identical spins. Each such domain has
a relatively strong magnetic field. In unmagnetized materials domains with opposite alignments
cancel each other’s fields out, but through exposure to an external magnetic field nonzero net
magnetization can be achieved.
At high temperatures thermal motion competes with the tendency for dipoles to align.
When the temperature is raised to a point called the Curie temperature, the system can no
longer sustain spontaneous magnetization and a second-order phase transition occurs.
The simplest model that describes ferromagnetism and this phase transition is the two
dimensional Ising model.

1.2 Model
The model itself is a square lattice, each cell containing a value of either "1" or "-1", representing
an electron’s spin. Each spin interacts only with its nearest neighbours. To eliminate the
effects of lattice boundaries, it is folded into torus [3] (the spins at opposite geometric edges
are considered neighbours). In absence of an external magnetic field interaction between two
dipoles "𝑖" and "𝑗" is given by the following formula:

𝐸𝑖𝑗 = −𝐽𝜎𝑖 𝜎𝑗 (1)

Where 𝐽 is the coupling constant, 𝜎𝑖 and 𝜎𝑗 are values of spins at sites 𝑖 and 𝑗. Therefore the
total energy 𝐸𝑘 of a spin 𝑘 is found as:
∑︁
𝐸𝑘 = −𝐽𝜎𝑘 𝜎𝑖 (2)
𝑖

Where 𝜎𝑖 is a neighbouring spin.


The partition function 𝑍 is given by [1]:
∑︁ −𝐸𝑖
𝑍= 𝑒 𝑘𝑇 (3)
𝑖

1
Where 𝑘 is the Boltzmann constant, 𝑇 is the temperature, 𝐸𝑖 is the total energy of the system
in the respective microstate.
The two-dimensional model with no external field was analytically solved by Lars Onsager,
showing that the model undergoes a phase transition at the critical temperature given by [5]:

2𝐽
𝑇𝑐 = √ (4)
𝑘 ln( 2 + 1)

The spontaneous magnetization that occurs at temperatures lower than 𝑇𝑐 was shown to be [1]:

2𝐽 1
𝑀 = [1 − sinh−4 ]8 (5)
𝑘𝑇
Where 𝑘 is the Boltzmann constant and 𝐽 is the coupling constant.

1.3 Simulation
The Ising model is rather hard to evaluate numerically as the number of possible states for an
NxN grid is 2𝑁 . It is for this reason that the model is often simulated using the Monte Carlo
method.
The most often used Monte Carlo algorithm used for this model (and by far the simplest
one) is the Metropolis-Hastings algorithm, where each step a spin is chosen at random (so as
to satisfy ergodicity) and flipped (its value changed to the opposite) under these conditions [4]:

1. If flipping the chosen spin 𝑘 leads to a decrease in its energy 𝐸𝑘 given by formula 2, then
it is flipped.

2. If flipping the chosen spin 𝑘 does not lead to a decrease in its energy, then it is flipped
Δ𝐸𝑘
with the probability of 𝑒− 𝑘𝑇 where ∆𝐸𝑘 is the change in energy arising from flipping
the spin.

1.4 Calculated Values


To evaluate the model’s behaviour quantitatively the following values were measured:

∙ 𝑀 - the average magnetization of all the spins on the grid.

∙ 𝐸 - the average energy of all the spins of the grid.

∙ 𝑐𝑣 - the specific heat of the grid, given by [6]:


1
𝑐𝑣 = [⟨𝐸 2 ⟩ − ⟨𝐸⟩2 ] (6)
𝑇2

∙ 𝐺(𝑟) - the correlation function between two spins 𝜎𝑖 and 𝜎𝑗 , 𝑟 units apart from one another
given by [6]:
𝐺(𝑟) = ⟨𝜎𝑖 𝜎𝑗 ⟩ − ⟨𝜎𝑖 ⟩2 (7)

2
2 Implementation
The model was implemented in Python. An 𝑛 × 𝑛 numpy array was used as the Ising grid.
To simplify calculations, both the magnetic coupling constant 𝐽 and the Boltzmann constant
𝑘 were taken to be 1, so the "true" temperature can be found as:
𝑇𝐽
𝑇𝐾 = (8)
𝑘
The Metropolis algorithm was implemented according to the outline given in section 1.3.
while satisfying the periodic boundary conditions. Through trial-and-error it was found that,
for a 100 × 100 grid, 150 steps of 10000 Metropolis loops were enough to achieve a stable state,
yielding acceptable results in reasonable time.
In addition to calculating the values listed in section 1.4 , two visualizations using Matplotlib
[2] were implemented as well - one allowing to see the end state of the grid and another showing
the average values of spins on the grid throughout the simulation.
Three following methods were used for the simulation:

1. Start from a randomized grid for every value of temperature and record the final state.

2. Start from a randomized grid and reduce the temperature every 150 steps, recording the
end state at every temperature, without resetting the grid.

3. Start from a ground state (where every spin is the same) and raise the temperature every
150 steps, recording the end state at every temperature, without resetting the grid.

3 Results
The implemented model successfully demonstrates a visible phase transition at around the
calculated critical temperature using any of the methods listed in section 2.

Figure 1: Final configurations of grids achieved through method one.

3
Figures 1 and 2 demonstrate the phase transition achieved through the first method. Figure
2 clearly shows the process of the grid falling into a ground state from a configuration of several
large clusters. The states where the grid is composed of these large clusters are relatively
stable and require quite a lot of iterations to fall into a ground state, the 150 steps given in all
experiments sometimes proving insufficient. The third method listed in section 2 avoids these
metastable states as there is no way for such clusters to form at a low temperature in a grid
composed of identical spins.

Figure 2: Final configurations and heatmaps of spin averages of grids achieved through method
one. Note the progression of clusters vanishing in the bottom left image.

The results for energy of spins (presented in figure 3 below) clearly shows the predicted
phase transition between two distinct phases with the low-temperature state having the lowest
energy and the high-temperature state having the highest. All the methods outlined in section
2 produced nearly identical results, the only difference being the energy of the low-temperature
state. This value is consistently lower in experiments produced by the third method as there
are no metastable states that increase the mean energy through interaction between boundary
spins.

4
Figure 3: Plots of the average energy of spins as a function of temperature. Note the inflection
point at around the critical temperature.

Results for the magnetization value differed significantly between methods (see figure 5 be-
low). The first two methods produced results drastically deviating from the analytical solution,
but nevertheless demonstrated the spontaneous symmetry breaking of the phase transition.
This deviation is also caused by the presence of metastable states in configurations obtained
through methods 1 and 2. Such states can possess any average magnetization between −1 and
1 as shown by the second and third plots in figure 5. The reason for this issue, as stated in the
second paragraph of this section, is insufficient time for the system to fall into a truly stable
state. Once again, the third method experiences no such problem, almost perfectly matching
the analytical prediction.

Figure 4: Several configurations showing the possible shapes of the metastable states.

5
Figure 5: Plots of the average magnetization of spins as a function of temperature. The orange
lines denote the value obtained through equation 5.

For calculating the specific heat only the second two methods were used. Both produced
similar results with a peak located at critical temperature (see figure 6). The second method
had an artifact where the specific heat peaked at zero temperature. Once again, the presence of
metastable states is the cause, as the difference ⟨𝐸 2 ⟩ − ⟨𝐸⟩2 is quite large due to a considerable
number of spins with differing values, which causes, in tandem with 𝑇12 growing quickly as 𝑇
drops, a sharp rise in specific heat.

1.0 1.0 T=1


Transition from T=0.1 T=2.1
Transition from T=5 0.8 T=2.3
0.8 T=4
0.6 T=5
0.6 0.4
G
Cv

0.2
0.4
0.0
0.2 0.2

0.0 0 10 20 30 40 50
0 1 2 3 4 5 r
T
Figure 7: Plot of the value of the cor-
Figure 6: Specific heat of spins as a
relation function between two spins at
function of temperature.
different temperatures.

6
The results for the correlation function were the least conclusive, even though only the third
method was used. As shown by figure 7 above, at low temperatures 𝐺(𝑟) is zero regardless of
the distance - spins are uncorrelated. As 𝑇 approaches the critical value, spins at increasing
distances become more correlated up to 𝑇𝑐 . At even higher temperatures, correlation decays
exponentially, then fluctuates randomly around zero.
It is worth noting that an attempt was made to implement a more complex Monte Carlo
method for the Ising model, namely the Wolff algorithm, which builds a whole cluster of spins
based on the temperature and flips them all each loop. Even though the algorithm worked
correctly, it failed to produce any relevant results due to its slowness (a consequence of its
complexity) - reaching a stable state took too much time even on smaller grids.

4 Options for Further Development


The implemented model has potential for further development, as there remain quite a few
relatively easily implemented features and modifications, that would allow for more in-depth
exploration. These potential features are listed below.

∙ Find the critical exponents for the Ising model through finite size scaling.

∙ Add an external magnetic field and plot a hysteresis curve.

∙ Calculate correlation lengths for a set of temperature values, using better data for the
correlation function.

∙ Further optimize the implemented Wolff algorithm and run the model for a longer time
on a more powerful machine.

∙ Use a small (e.g. 2 × 2) grid to get analytical solutions for all calculated values and
compare simulation results to theory.

5 Conclusion
The implemented Ising model, using the Metropolis-Hastings algorithm, demonstrated the ex-
pected behaviours, namely the phase transition as well as the breaking of symmetry and spon-
taneous magnetization below the critical temperature, which matched the analytical solution.
The results for energy and specific heat met the estimations through all three used methods,
while the result for spontaneous magnetization only satisfied the analytical solution through
one method. It was concluded that this deviation was caused by insufficient computing power
allocated to the task - the model did not have enough time to reach a truly stable state. The
result for the correlation function was largely inconclusive, only roughly demonstrating the
expected exponential decay of correlation between spins with distance.

References
[1] Ishita Agarwal. Numerical analysis of 2-d ising model. https://www.hiskp.uni-bonn.de/
uploads/media/ising_II.pdf, 2007.

[2] J. D. Hunter. Matplotlib: A 2d graphics environment. Computing In Science & Engineering,


9(3):90–95, 2007.

7
[3] J. Kotze. Introduction to Monte Carlo methods for an Ising Model of a Ferromagnet. ArXiv
e-prints, March 2008.

[4] N. Metropolis, A. W. Rosenbluth, M. N. Rosenbluth, A. H. Teller, and E. Teller. Equation


of State Calculations by Fast Computing Machines. The Journal of Chemical Physics,
21:1087–1092, June 1953.

[5] L. Onsager. Crystal Statistics. I. A Two-Dimensional Model with an Order-Disorder Tran-


sition. Physical Review, 65:117–149, February 1944.

[6] Lilian Witthauer and Manuel Dieterle. The phase transition of the 2d-ising model. http://
quantumtheory.physik.unibas.ch/people/bruder/Semesterprojekte2007/p1/, 2007.

You might also like