Ising Proj
Ising Proj
1 Introduction
The subject of this project will be the two dimensional Ising model. This model will be used to describe the
interaction of spins (either spin up or spin down) on a two-dimensional lattice e.g. in a ferromagnet. At low
temperatures, the system is ordered (almost all spins are aligned), but if we raise the temperature above a critical
temperature Tc , the temperature fluctuations destroy the order and there is no preferred spin alignment any more
(compare Fig. 1). The task will be to study this phase transition numerically using Monte Carlo simulations. Many
of the techniques you will use are theoretically rather complex in their theory. Therefore they will only be discussed
on a level such that you will be able to implement and use them. The interested reader is therefore encouraged to
inform oneself in detail about these concepts during or after the project.
a) b)
Figure 1: Schematic representation of the spins in the two dimensional Ising model. The arrows represent spin up
or down. a): The system is below Tc and ordered b): The system is above Tc and unordered.
2 Theory
2.1 2D Ising Model
We will assume a square lattice Λ of ’volume’ L ˆ L “ V and denote the spins at positions i, j as si,j with si,j “ ˘1.
The Hamiltonian H of the system is
J ÿ
HpSq “ ´ si,j psi`1,j ` si´1,j ` si,j`1 ` si,j´1 q , (1)
2 i,j P Λ
where J is the interaction strength of spins, the 1{2 prevents double counting, S “ tsi,j u is a configuration of spins
and we assume periodic boundary conditions in both directions: si`L,j “ si,j , si,j`L “ si,j .
(2)
ÿ
Zpβq “ exp r´βHpSqs ,
S
where β “ 1{pkB T q with T the temperature, kB Boltzmann’s constant and the sum runs over all possible spin
configurations S of the system. Later, it will be useful to express everything in dimensionless quantities. Therefore
1
ˆ
let us already define β̂ :“ βJ and Ĥ :“ Ĥ{J . Then the the Boltzmann weight is expr´β̂ Ĥs. It is easy to see, that
both the dimensionless and the original weight have the same value. We will use these dimensionless quantities
when we talk about the implementation of the code.
The number of possible states S is Ntotal “ 2L . This renders a direct calculation of Eq. (3) already for quite small
2
volumes impossible. Therefore, in the following section we will briefly introduce a statistical method to approximate
sums like Eq. (3).
where xi are uniformly distributed randomly chosen samples in A. In practice, as an approximation of the integral,
we can choose N to be large but finite. This method can also be applied to sums as in Eq. (3). For our problem,
however, we would need too many samples for a good approximation of the sum, if we would create uniformly
distributed random samples (a sample of our domain of integration in the context of the ising model is just one
possible spin configuration S).
Although there is a large number of possible states of our system, the Boltzmann weight exp r´βHpSqs of the
majority of spin configurations S is very small and therefore their contribution to the partition function is close to
zero. Thus it is a good approximation to use mainly those spin configurations with a significant contribution for the
calculation of the partition function. The technique to generate these important configurations is called importance
sampling.
We will use the Metropolis-Hastings algorithm to generate such a set of configurations. Given a start configuration
the next configuration is generated by doing the following steps:
1. Flip one spin of your lattice: si,j Ñ s1i,j “ ´si,j 1 .
Thus obtaining a new set of spins S “ ts1,1 , s1,2 , . . . , si,j , . . . , sL,L u Ñ S 1 “ ts1,1 , s1,2 , . . . , ´si,j , . . . , sL,L u.
2. Calculate the resulting change in energy δH “ H pS 1 q ´ H pSq.
3. Accept S 1 as the new configuration with the probability
if δH ă 0
#
1
(5)
` ˘
W si,j Ñ s1i,j “ .
e´β δH
otherwise
2
steps, where Nskip is chosen such that the obtained configuration is essentially independent of the previous one.
where On “ OpSn q is the observable measured on the n-th generated configuration Sn and N is number of generated
configurations.
2.3 Observables
Moments
Given an observable On we can define moments mk of this observable as
N
1 ÿ k
mk “ O , (7)
N n“0 n
It can be shown that the central moments can also be calculated directly from the moments as
µ1 “ 0 , µ2 “ m2 ´ m21 ,
µ3 “ m3 ´ 3m2 m1 ` 2m31 , µ4 “ m4 ´ 4m3 m1 ´ 3m22 ` 12m2 m21 ´ 6m41 ` 3µ22 . (9)
Order Parameter
An order parameter is an observable O, that is a measure of the phase of a system. It normally ranges between
zero in one phase and non-zero in the other. Moments of this order parameter characterize the phase transition
and dynamics of the system:
• Mean m1 : We denote this also with xOy. This quantity reflects in which phase the system is. When crossing
β̂c the mean will jump immediately from zero to constant non zero for a first order phase transition in the
infinite volume.
• Susceptibility χ “ µ2 V : This quantity will peak (diverge in an infinite volume) at the phase transition.
• Skewness B3 : This quantity gives information about the asymmetry of the probability distribution of our
observable.
3
The magnetization
1 ÿ
Mn “ si,j (12)
L2 i,j P Λ
3 Tasks
The project consists of a mandatory part and optional tasks.
3. Plot B3 of M as a function of β̂. Would you try to locate β̂c with this quantity?
If you finish early with this task and are motivated two optional tasks are proposed
1. Simulate at different volumes and approach the infinite volume limit to understand the order of the phase
transition.
2. Visualize the Monte Carlo history of the spin configurations with an animation.
4 Implementation
This section is meant to help you to write your code and give you hints about its structure. A kind of flowchart of
the code can be seen in Fig. 5, where it is shown how the different parts of your program are connected and work
together. Unless you have good reasons not to do so, follow the suggested implementation!
identifier within this text and Fig. 5. For your program the files should have meaningful names by which you and your program can
distinguish files e.g. outputfiles of runs with different parameters.
4
a file, where the different lines correspond to the value assigned to their corresponding variable. Therefore, it is
important that the order of the parameters is defined and fixed. Possible parameters given via this file are shown
in Tab. 1. The meaning of some of them will only become clear after reading the following sections.
Alternatively you could use a two column file, where the first column specifies the parameter and the second the
given value. Your program then has to parse the entry of the first column to assign the value of the second column
to the right variable inside your program. Your file config_file could then look like this:
N 10000
NSkip 100
L 128
...
4.2 generate.c
The code in this file is responsible for
• initializing the lattice,
• do the following for every β̂ specified by config_file:
– generating configurations accordingly to the Metropolis-Hastings algorithm,
– measuring the magnetization per configuration Mn accordingly to Eq. (12),
– writing all calculated Mn to output_file
– optional: writing the generated spin configurations to spinConfig_file
To guide you a bit further, we will now discuss the vital aspects of this code.
5
The generate function
In the provided example of the main function there is a function called generate that handles all tasks for the
generation of the configurations. It could be structured like
void generate (char const * fileName )
{
/*
- declare variables
- parse config_file
- call init function
*/
/*
- set initial spin configuration
*/
/*
- save results in file
*/
}
}
Again, the meaning or necessity of some structures will only become clear when reading the following section.
Initialization
Once you’ve parsed your configuration file, you know crucial values like the size of the lattice or the number of
configurations to generate. This will affect the allocation of memory for different arrays among other things. To
handle such initialization tasks create a function
void init (...);
Lattice setup
You will need an array latticeSpin to store the spin values si,j . Instead of a two dimensional array, a one
dimensional array is usually used, which is indexed with a so-called superindex n. This is done by a function
n : Z ˆ Z Ñ N, npi, jq :“ j ¨ L ` i, whose signature could read
unsigned int getSuperIndex ( const int i, const int j);
where i and j are the x and y coordinates of the site. This is a suitable location to implement the periodic boundary
conditions.
Since the interaction takes place only between nearest neighbours, you will often have to retrieve the neighbour-
ing spin values, for which you would have to calculate the superindeces of these neighbours. However, since
these neighbour indices are constant during your simulation, it is recommended to create a two dimensional array
6
neighbourArray of dimensions L2 ˆ 4 during initialization that contains the superindices of all nearest neighbours
to a grid point. Filling neighbourArray with the correct indeces should be part of init.
Random numbers
In the implementation of some functions you will need to generate uniformly distributed random numbers. Therefore
implement a function
double drawRandomNumber (void);
which returns a random number x P r0, 1s drawn from a uniform distribution. Regarding our problem the basic C
generator is sufficient.
Implement a function
void setInitialConfig (int* latticeSpin , int mode);
to handle the task of setting the initial configuration.
Metropolis-Hastings algorithm
The next task is to implement the Metropolis-Hastings algorithm. Implement a function that performs the
Metropolis-Hastings algorithm , whose signature could read
void updateLattice (int* latticeSpin , const int ** neighbourArray , const double beta);
where one call of this function should perform L2 iterations of the Metropolis-Hastings algorithm. To do this, we
look at the computational requirement of every step of the algorithm:
1. Step - Flipping of a spin: By now you will have already wondered how to choose the spins to flip. There
are three answers that might come to mind
• Sequential update: Just go through the whole lattice in order Ñ Not recommended as the generated
configurations will be highly correlated. But it is encouraged to explore this option in combination with
the visualization task.
• Random update with doubling: Choose a spin of your lattice randomly Ñ Recommended option as it
has the right balance between cost and auto-correlation.
• Random update without doubling: Choose the order of the spins randomly, but ensure that updateLattice
updates every site exactly once Ñ Not recommended as it is very expensive and is not needed.
2. Step - Calculating δ Ĥ: The easiest way to do it, is to calculate Ĥ pS 1 q and Ĥ pSq and take the difference.
However, this calculates more than you need to. Since the interaction of the spins are only nearest neighbours,
the energy will only change locally. Therefore, you will have to simplify δ Ĥ analytically.
3. Step - The acceptance step: Use drawRandomNumber to implement the acceptance step.
Calculate Mn
Calculate the magnetization Mn every NSkip configurations and store them in an array.
7
Output files
As proposed in Fig. 5 this part of your program should be able to output a file output_file where every line
corresponds to the magnetization Mn of a generated configuration in your Monte Carlo simulation. To keep it
simple, generate one file per value of β̂. Generate unique names for you files from the run parameters. It should
look like
...
0.912109
0.908203
0.913574
0.898926
0.917480
0.912109
...
If you intend to do the optional task of the visualization, your code should also be able to output files of the spin
configuration spinConfig_file. Generate matrix blocks with the spin values and separate them by a double blank
line. Their content should look like
...
-1 -1 +1 -1 -1 -1
-1 -1 +1 +1 +1 -1
-1 +1 +1 -1 -1 -1
+1 +1 -1 -1 -1 +1
-1 +1 +1 +1 +1 -1
-1 +1 -1 +1 +1 +1
-1 -1 +1 -1 -1 +1
...
for a 6 ˆ 6 lattice. Make the output of such files optional and only output them when needed, as their size can
quickly increase for larger lattices and statistics (having a few GB file is easily possible).
4.3 measure.c
The code in this file is responsible for
• reading in the previously generated output_file corresponding to the β̂ specified by config_file
• calculate |xM y| and χ, B3 , B4 of M as defined in Sec. 2.3 for all configurations after NTherm .
• write them to a central file results_file, where the first column is β̂ and the next columns are the corre-
sponding quantities calculated in the previous step.
The content of results_file should look like
...
0.014000 0.029142 0.001359 -0.085496 3.066083
0.015000 0.024791 0.001016 0.091971 3.347088
0.016000 0.026640 0.001137 0.124383 2.998146
0.017000 0.026147 0.001075 0.034916 2.857480
...
Thermalization
When doing Monte Carlo simulations the system needs time to thermalize. This means that the system is not yet in
equilibrium (with respect to the Monte Carlo simulation) and the generated configurations are not representative.
This is best seen when looking at the Monte Carlo history from output_file. The Monte Carlo history of Mn
of a simulation for β̂ ă β̂c initialized with a cold start is shown in Fig. 2. At the beginning the value starts at
8
1 (due to the cold start) and then slowly evolves towards zero until it only fluctuates around this value, where it
seems to be in equilibrium. The amount of configurations it takes until the simulation is in equilibrium is called
thermalization time. One should only start to calculate expectation values of observables with configurations after
this thermalization time. Keep in mind that this thermalization time depends for one on your system’s size, but
also on the value β̂. Therefore is strongly recommended to have a quick look into some of the Monte Carlo histories
before calculating observables to have a rough estimate of NTherm . When in doubt it is better to overestimate
NTherm .
0.8
0.6
Mn
0.4
0.2
-0.2
0 50 100 150 200 250 300 350 400 450 500
Configurations n
Figure 2: Monte Carlo history of Mn for L “ 256, NSkip “ 10 and cold start.
A frame of your finished animation should then look like Fig. 3. Of course you are free to generate the animation
in any other way e.g. as a video file.
9
Configuration 234
250 1
datafile index (i-1) matrix
200
150
Spin
y
100
50
0 -1
Figure 3: Frame of the animation visualizing the Monte Carlo history of the spin configurations.
0.4407 you can compare the precision of your simulations to. In Fig. 4 |xM y| and χ from a run with the suggested
parameters are plotted against β̂. Why is a better idea to plot |xM y| instead of xM y?
1 500
0.9 450
0.8 400
0.7 350
0.6 300
|<M>|
0.5 250
χ
0.4 200
0.3 150
0.2 100
0.1 50
0 0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
βeff βeff
10
to a fix value. To investigate this behaviour determine β̂c for L P t4, 8, 16, 32, . . . , 256u (or up to any volume that
your computer is able to simulate in a reasonable time4 ). Do the small volumes have any relevant information? Do
you think that you reached the infinite volume limit? Are you able to determine the order of the phase transition?
Does B3 improve? Keep in mind that the thermalization time and auto-correlation will drastically increase with
the volume. So an increase in NTherm and NSkip will be necessary.
6 Closing remarks
Errors
So far, we did not speak about errors of observables nor instructed you to calculate these. However, of course, any
numerical result is not free from errors and one has to be very careful. The standard deviation alone is not suitable,
since the auto-correlation has to be taken into account in the estimation of the errors. Understanding and doing
this can be a very complex topic and therefore it is left out of the project.
Further literature
If you want to learn more in detail about Monte Carlo simulation we recommend the following books Markov Chain
Monte Carlo Simulations and Their Statistical Analysis by Bernd A. Berg and Monte Carlo Methods in Statistical
Physics by M. E. J. Newman and G. T. Barkema. An important application of Monte Carlo simulation in high-
energy physics is lattice field theory in general and lattice QCD. Quantum Chromodynamics on the Lattice by C.
Gattringer and C. B. Lang is an excellent introduction to this topic for readers familiar with the path integral
formalism of Quantum field theory
11
OPTIONAL
generate.c spinConfig_
• Do the following for every b in the interval
outputConfig == 1 file
1. generate N configurations according via Metropolis-Hastings algorithm
2. calculates mean magnetization on every NSkip-th configuration • spin values are written as a matrix
• matrices per configuration seperated
by empty two lines
mode == --generate
measure.c
config_file • reads in outputfiles named according to parameters
• calculates Mean, Susceptibility, Skewness, Kurtosis