Sunteți pe pagina 1din 61

Computational Statistical Physics

Lecture Notes

After a lecture by H.J. Herrmann


written by

Nicola Offeddu
Marcel Thielmann
Madis Ollikainen
Summer Semester 2014

Preface
This is the first part of a provvisory draft of the script. The rest of the script will follow
during the semester. Since the course covered a rather broad spectrum of topics, time
ofted did not allow to treat them deeply. This is also why, as you will notice while
reading, special attention has been paid in giving some references along the text. If
during your (fruitful) learning, you should notice any mistake, typoes, or in the case
you would like some extra information, explanation, reference or whatsoever about the
methods, the scientists or the sources, please notify us (nico.offeddu@gmail.com). We
would be really happy to receive feedback and to integrate/modify/expand the script.
You can be sure that your fellow (and onfollowing generations of) students will be more
than grateful for that.

Zurich, April 29, 2015

iii

Table of contents
Preface
1 Monte Carlo Methods
1.1 Classical Statistical Physics . . . . . . . . . . . . . . . . . .
1.1.1 Phase Space . . . . . . . . . . . . . . . . . . . . . . .
1.1.2 Ensembles . . . . . . . . . . . . . . . . . . . . . . . .
1.1.3 The Ising Model . . . . . . . . . . . . . . . . . . . .
1.2 Monte Carlo Algorithms . . . . . . . . . . . . . . . . . . . .
1.2.1 Markov Chains in Monte Carlo: M(RT)2 , Glauber,
and Creutz algorithms . . . . . . . . . . . . . . . . .
1.2.2 Boundary conditions: . . . . . . . . . . . . . . . . . .
1.2.3 Sampling Uncorrelated Configurations . . . . . . . .
1.3 Finite Size Methods . . . . . . . . . . . . . . . . . . . . . . .
1.3.1 Binder Cumulant . . . . . . . . . . . . . . . . . . . .
1.3.2 First Order Transition . . . . . . . . . . . . . . . . .
1.4 Cluster algorithms . . . . . . . . . . . . . . . . . . . . . . .
1.4.1 Potts Model . . . . . . . . . . . . . . . . . . . . . . .
1.4.2 O(n) Model . . . . . . . . . . . . . . . . . . . . . . .
1.4.3 Implementation of Cluster Algorithms . . . . . . . .
1.5 Histogram Methods . . . . . . . . . . . . . . . . . . . . . . .
1.5.1 Broad Histogram Method . . . . . . . . . . . . . . .
1.5.2 Flat Histogram Method . . . . . . . . . . . . . . . .
1.5.3 Umbrella sampling . . . . . . . . . . . . . . . . . . .
1.6 Renormalization Group . . . . . . . . . . . . . . . . . . . . .
1.6.1 Real Space Renormalization . . . . . . . . . . . . . .
1.6.2 Generalization . . . . . . . . . . . . . . . . . . . . . .
1.6.3 Monte Carlo Renormalization Group . . . . . . . . .

iii
1
1
1
2
3
9

. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
Kawasaki
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .

9
14
15
18
18
20
23
23
26
28
30
30
32
33
34
34
38
39

2 Parallelization
2.1 Multi Spin Coding . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2 Vectorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

43
43
45

3 Bibliography

49

A Codes

51

vi

Table of contents
A.1
A.2
A.3
A.4

vi

Multi Spin Coding .


Parallelization . . . .
MPI . . . . . . . . .
Linked Cells Method

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

51
52
54
55

1 Monte Carlo Methods


We begin this chapter with a recapitulation of part of the lecture held in the past
Semester, Introduction to Computational Physics (ICP). Basic knowledge of statistical
physics as well as a certain acquaintance with the content of ICP is assumed. For
more information about the mentioned lecture and detailed derivation of some of the
formulae contained in this chapter, see Herrmann (2012).
After a brief definition of some basic concept in statistical physics (phase space,
micro- and canonical ensembles), the Ising model is introduced with some important concepts and observables (order parameter, response functions, fluctuationdissipation theorem, correlation length and critical exponents). In the second
and third part, Monte Carlo and finite size methods are treated respectively. Those
who attended ICP will find that some parts are already familiar to them. The last
three sections of the chapter (cluster algorithms, histogram methods and renormalization) contain some optimization and generalization of MC and were not treated
in ICP.

1.1 Classical Statistical Physics


1.1.1 Phase Space
Let us consider a classical physical system with N particles numbered by an index
i, each of them provided with n degrees of freedom. We can refer to each degree of
(j)
freedom for each particle with pi . Hereby the subscript denotes the ith particle, and
the superscripted index the j th degreen of freedom. The system can beo then completely
described by its configuration X (pj1 , pj2 , pj3 , ..., pjN )|j {1, ..., n} . The set of all
possible configurations X is called the phase space.
The Hamiltonian H describes the energy of the system and together with the distribution of configurations it determines (if it is not explicitly time dependent) the
time evolution of the system. The Liouville Theorem,

(X, t) = {H, (X, t)} ,


t

(1.1)

describes the evolution of the distribution of the configurations. In thermal equilibrium,


the system reaches a steady state in which the distribution of the configurations is

Chapter 1: Monte Carlo Methods

constant:
t = 0. Mind that this does not mean that the system is not allowed to
jump from one configuration to another. As in usual statistical physics, the thermal
average of a quantity Q over its distribution is defined as
hQi =

1X
Q (X) (X),
X

(1.2)

where is the volume of the phase space such that 1 X (X) = 1. With this
definition, systems can be described by means of some averaged quantities, such as
temperature, energy and pressure.
P

1.1.2 Ensembles
The point of an experiment is to read out some observable quantities out of a system,
while controlling others. Generally it is not possible to set all the desired thermal
quantities to a given value. As an intuitive example, think of a classical gas the
dynamics of which is given by perfect elastic collisions. It is impossible to compress
the volume and keep the pressure and the temperature unchanged. It is clear that
depending on which quantities are being held constant, and which others are let free,
the system will behave differently. There are quantities which are called conjugate to
another, like the volume V and the pressure p. One can fix either V or p, not both.
Other examples are energy and temperature (E, T ), particle number and chemical
~ , H).
~
potential (N, ), magnetization and magnetic field (M
Depending on which of
these values are held constant, the system is called
Microcanonical ensemble: fix E,V, N
Canonical ensemble: fix T,V, N
Canonical pressure ensemble: fix T,p, N
Grandcanonical ensemble: fix T,V,
Microcanonical Ensemble:
In the microcanonical ensemble, the number of particles, the volume and the energy of
the system are fixed. This means that any configuration X that the system can assume
has the same energy E (X) = constant. Without proof, the probability of the system
to be in any configuration is also constant:
peq (X) =

1
(H (X) E)
Zmc

with Zmc being the partition function of the microcanonical ensemble:


Zmc =

X
X

(H (X) E) = Tr [ (H (X) E)] .

(1.3)

Classical Statistical Physics

Canonical Ensemble:
In an experiment, microcanonical ensembles must be created by completely isolating
the system from the outer world, not allowing it to exchange its energy with the outside.
This is rarely the case in an experiment since it is difficult to realize in practice. Much
more common is the situation in which the temperature T is fixed (for example, if the
experiment is done at room temperature). This is the case for the canonical ensemble.
One can derive that the probability for a system to be in a certain configuration X
(with energy E (X)) is given by the Boltzmann factor:
1
E (X)
peq (X) =
,
exp
ZT
kB T
"

(1.4)

with ZT = X exp E(X)


being the partition function of the canonical ensemble.
kB T
According to the prior definition, the thermal average of a quantity is then given by
P

hQi =

E(X)
1 X

Q (X) e kB T .
ZT X

For the grandcanonical ensemble we will need some extra information to describe
open systems by the means of a coupling to an external heat bath.

1.1.3 The Ising Model


Sigfried Lenz proposed a model to his PhD student, Ernst Ising, to describe systems
composed by magnetic dipole moments that are only allowed to assume two states,
from now on denoted by +1 and -1. His original goal was to describe phase transitions
in magnetic materials. When they found out1 that in the one-dimensional case the
model does not admit a phase transition, they thought that this model was of no
use. It had to wait until 1949 for Onsager to publish the solution for the spontaneous
magnetization in the two-dimensional case. A few years passed until this formula was
proved by Yang in 1952. Since then, the Ising model has been successfully applied to
a huge number of physical, as well as non-physical, problems (e.g. magnetic systems,
opinion models, binary mixtures, ...). To date, there is no general analytical solution for
the Ising model in 3D, if not for a few very special cases. This is the reason why this
mathematical model has been studied so intensively from theoretical and numerical
perspective, using tools of statistical physics some of which we will describe in this
chapter.

This was part of his PhD thesis, in 1924

Chapter 1: Monte Carlo Methods

Figure 1.1: Graphical representation of the


Ising model on a 2D square lattice.

Figure 1.2: Spontaneous magnetization in


the Ising model.

The simplest approach to the Ising model is imagining a squared lattice and assuming
that every variable i {1, 1} on the lattice only affects the nearest neighbors (see
Fig. 1.1). This restriction can be relaxed by letting the sites interact with the next
nearest neighbors or even farther sites. If we think of the variables as spins, their
interaction is given by the Hamiltonian
H (X) = J

N
X

i j H

i,j:nn

{z
A

i .

(1.5)

i=1

}|

{z
B

where A is the interaction between all the nearest neighbors (nn), and B the interaction
of each site with an external magnetic field H. Note that if two spins are parallel, then
the energy is lowered (J) and if they are anti-parallel the energy is increased (+J).
The first term in Eq. (1.5) tries to create order in the system (in the sense of the spins
being aligned in the same direction) by reducing the energy when neighboring variables
are in the same state. The second term tends to align the spins in the direction of the
external field H. While the energy is lower in an ordered state, the heat tends to destroy
the order by flipping single spins. Beyond a critical temperature Tc (i.e., for T > Tc ),
the system is dominated by randomness and there is no alignment of the spins anymore.
This transition, like any other phase transition (e.g. sol-gel transition) between ordered
and disordered states, can be characterized by an order parameter. In the case of the
Ising model, the order parameter is given by the spontaneous magnetization (see Fig.
1.2).
The order parameter in the Ising model
The magnetization is defined as
*

M (T ) =

N
1 X
i ,
N i=1

(1.6)

Classical Statistical Physics

that is, the thermal average of the mean value of the spins. This alone would not be a
good measure since taking the thermal average of a quantity means taking the mean
value over all the possible states. The problem with this procedure is that for any given
state there will always exist an opposite state in which all the spins are flipped. In the
average these two configurations cancel each other out. Thus, on average, every state
will always cancel out with its opposite companion and the averaged magnetization
will always be zero. A better quantity is the spontaneous magnetization:
*

MS (T ) lim

~
H0

N
1 X
i
N i=1

(1.7)

~ that aligns
Here the symmetry of the Ising model is broken by applying a small field H
the spins in one direction. Now it will not be the same if the spins are up or down, and
the thermal average of the spontaneous magnetization wont be zero anymore. Another
way of breaking the symmetry would be fixing the boundaries of the lattice in a certain
state. This is not practical if periodic boundaries are being used.
In the proximity of the critical temperature, the spontaneous magnetization decays
like a power law:
MS (T Tc ) ,
(1.8)
where is known analytically in 2D (1/8), and numerically in 3D ( 0.326) (See Fig.
1.2).
Response functions:
Response functions are second derivatives of the free energy2 Ef = 1 ln (Z):

M
(T )
|T Tc |

H T,H=0

(1.9)

E
CV (T )
|T Tc |

T H=0

(1.10)

The divergence of these functions at the critical temperature can be used to determine
the critical temperature itself. We will encounter these functions again later in the
context of the nth order transition (see Sec. 1.3.2).

The following established nomenclature is unfortunate: with , the thermodynamical quantity kB1T
is meant, not the exponent presented in the power law (1.8). See classical statistical physics
textbooks for more information.

Chapter 1: Monte Carlo Methods

Fluctuation-dissipation theorem for the magnetic susceptibility:


Taking equation (1.7), and plugging it into Eq. (1.9) yields






h
i

PN
P PN


exp
H
+
H

i
i
0

hM (T, H)i
X
i=1
i=1

=
(T ) =



N
H
H

X
X
H=0

exp H0 + H
i


i=1
X

|
{z
}


=Z (H)
T

with =

(T ) =

1
kB T

and H0 = J

i,j:nn i j .

2
h
N
exp H0
i=1 i

P

+ H

Using the product rule yields:


PN

i=1 i

ZT (H)
{z

|
D

=
=

}
2

M (T )
hD

H=0

P

i=1 i exp

PN

H0 + H

(ZT (H))2
|

{z

hM (T )i

M (T )2 hM (T )i2 0

i2

i=1 i



}

PN

(1.11)

and analogously one can show that


CV = 2

hD

E (T )2 hE (T )i2

(1.12)

You may have already noticed that these expressions are suspiciously similar to the
classical definition of the variance. These two last formulae are both very important in
Monte Carlo simulations, as we will learn further on. See Fig. 1.3, 1.4, and 1.1.3. In
the vicinity of Tc (see Fig. 1.3), the magnetic susceptibility decays like a power law:
(T ) |T Tc |
with =7/4 in 2D and 1.24 ind 3D. Near Tc (see Fig. 1.4), the specific heat can
also be described by a power law:
C (T ) |T Tc |
where the decay is logarithmic in 2D (= 0) and numerically known in 3D ( 0.11).

H=0

Classical Statistical Physics

Figure 1.3: Susceptibility in a finite system

Figure 1.4: Heat capacity of a binary mixture.


Retr.
Jan 2015 from
web.mit.edu/8.334/www/grades/projects/

Correlation length3
The correlation function describes to
which extent two sites (or extended regions) are related. It is defined by the
thermal average of the product of the values of sites at different positions:
C(R) h (0) (R)i .

(1.13)

If two regions are exactly in the same


physical state, the correlation function is
maximized. This function decays exponentially for large R:
R

C(R) M 2 + ae

where is called the correlation length.


Figure 1.5: Correlation in finite system
In the vicinity of Tc the correlation length
diverges as
|T Tc |
3

See Fig. 1.1.3 and the lecture notes Computational Physics of the previous course.

Chapter 1: Monte Carlo Methods

with = 1 in 2D and 0.63 in 3D. At Tc and for large R, we have that


C(R) R2d ,
where d is the dimensionality of the system and = 1/4 in 2D and 0.05 in 3D.
Critical Exponents:
All the quantities described in Figs. 1.3, 1.4 and 1.1.3 are characterized by some
exponents (, , , and ). An interesting fact is that these exponents are not
independent from each other. They are related by the scaling and the hyperscaling
relations 4 :
+ 2 + = 2
2 = d
(2 ) =
Due to these relations, the number of independent exponents reduces to two.

For more information about these the exponents, see Stanley (1999) and Stanley (1971)

(1.14)

Monte Carlo Algorithms

1.2 Monte Carlo Algorithms


Monte Carlo integration has been already extensively discussed in ICP (see (Herrmann,
2012)). In this section, we will briefly summarize how this method works and go deeper
into some details and properties we did not study in the past semester.
The basic idea behind Monte Carlo methods is that in order to calculate some thermodynamical quantity, it is enough to sample randomly in phase space instead of
averaging over all states. If the sampling is large enough, the computed quantity will
eventually converge toward the real thermodynamical quantity. The main steps in the
Monte Carlo integration are:
Choose randomly a new configuration in phase space (with a Markov chain).
Accept or reject the new configuration, depending on the strategy used (e.g.,
Glauber Dynamics).
Calculate the physical quantity and add it in the averaging loop.
Repeat the procedure.

1.2.1 Markov Chains in Monte Carlo: M(RT)2 , Glauber, Kawasaki


and Creutz algorithms
Often, choosing equally distributed configurations will be very inefficient since
most of the possible configurations are unlikely to be assumed by the system. As an
example take the kinetic energy of a gas.
The distribution of the average energy
will be a sharp peak, as in Fig. 1.6. There
are a number of methods to avoid unnecessary sampling of regions where the system is unlikely to be found (see importance sampling in Herrmann (2012) as an
example).

Figure 1.6: Example of energy distribution

A common way to choose the configurations to sample is introducing a virtual time


and explore the phase space through a Markov chain. Mind that the time has no
physical meaning, and it only represents the steps of a stochastic process.
In terms of a Markov chain, the transition probability from one state to another is
given by the probability of a new state to be proposed (T ) and the probability of this
state to be accepted and assumed (A). In other words, T (X Y ) simply gives us the

10

Chapter 1: Monte Carlo Methods

probability that a new configuration Y is proposed, starting from the configuration X.


This must fulfill three conditions:
1. Ergodicity: any configuration in the phase space must be reachable within a finite
number of steps
2. Normalization:

T (X Y ) = 1

3. Reversibility: T (X Y ) = T (Y X)
Once a configuration is proposed, one can accept the new configuration with probability
A(X Y ) or reject it with probability 1 A(X Y ). The transition probability is
then given by
W (X Y ) = T (X Y ) A(X Y ).
(1.15)
With the transition probability one can investigate the probability of finding the state
in a certain configuration (during the stochastic process, not in real time!) p (X, ).
The master equation describes how the distribution evolves in time.
X
dp (X, ) X
p(X)W (X Y )
p(Y )W (Y X)
=
d
Y
Y

(1.16)

For Markov chains, it is known that the system always reaches a stationary state (called
pst ) defined by the derivative in Eq. (1.16) being zero. The transition probability must
fulfill
1. Ergodicity: any configuration must be reachable: X, Y : W (X Y ) 0
2. Normalization:
3. Homogeneity:

W (X Y ) = 1

pst (Y )W (Y X) = pst (X)

Note that the condition of reversibility is not required anymore. This is one of the
effects of introducing A (X Y ). Just think of a two level system, in which one of the
two energy levels is higher (e.g. the electronic shells in an atom): At low energies it
would be nonsense to equally sample the excited and the ground state of the electrons.
On the contrary, at very high energies the sampling will have to reflect the higher
probability of being in an excited state, rather then in the ground state. In order for
the Markov chain algorithm to choose effectively which areas of the phase space to
explore, somehow W has to depend on the system. Imposing the distribution of the
stationary states pst as the equilibrium distribution of the physical system pst (a real
and measurable distribution) is called detailed balance:
dp (X, )
!
= 0 pst = peq
d

10

(1.17)

11

Monte Carlo Algorithms


)
It then follows from the stationary state condition ( dp(X,
= 0) that
d

peq (Y )W (Y X) =

peq (X)W (X Y ).

A sufficient (not necessary!) condition for this to be true is


peq (Y )W (Y X) = peq (X)W (X Y ).

(1.18)

As an example, in a canonical ensemble (at fixed Temperature T),


h the equilibrium
i
1
distribution is given by the Boltzmann factor: peq (X) = ZT exp E(X)
with the
kB T
h

partition function ZT = X exp E(X)


kB T . The Boltzmann equilibrium indeed fulfills
2
the detailed balance (see M(RT) algorithm).
P

M(RT)2 algorithm:
If equation (1.18) is fulfilled, we automatically found the way to fulfill detailed balance
by the chosen W and peq . The algorithm (also called Metropolis algorithm5 ) uses the
acceptance probability
peq (Y )
A (X Y ) = min 1,
.
peq (X)
"

In the case of the canonical ensemble with peq (X) =


probability becomes then
E(Y ) E(X)
A (X Y ) = min 1, exp
kB T
"

"

##

1
ZT

(1.19)
h

exp E(X)
the acceptance
kB T

E
= min 1, exp
kB T
"

"

##

(1.20)

which means that if the energy decreases, theh step isi always accepted, and if the energy
increases it is accepted with probability exp kE
. Plugging Eq. (1.19) with peq into
BT
Eq. (1.18) shows that detailed balance is fulfilled. For a more detailed discussion about
the Metropolis and alternatives algorithms (e.g. Glauber dynamics), see Herrmann
(2012). The algorithm has been then generalized in 1970 (Hastings, 1969). We can use
this rather general algorithm to explore the phase space of the Ising model, by flipping
the values on the lattice following the acceptance probability. Summarized, the steps
in the Metropolis algorithm would then be:
5

The rather curious name of this algorithm finds its reason in the names of the authors of the paper
in which it was proposed: Metropolis et al. (1953). RT is squared because besides Metropolis,
the other four authors of the paper formed two married couples and therefore carried the same
family names. The real contributions of some of the authors (in particular of Metropolis and of
A.H. Teller) is subject of controversy (Barth, 2003; Gubernatis, 2005). It has been even stated by
Roy Glauber and Emilio Segr that the original algorithm was invented by Enrico Fermi, which
described it to Metropolis while they were working together at Los Alamos and later reinvented
by Stan Ulam (Segr, 1970).

11

12

Chapter 1: Monte Carlo Methods


Choose (randomly) a site i
Calculate E = E(Y ) E(X) = 2Ji hi
h

If E 0, flip the spin, otherwise accept it with probability exp kE


BT

with hi = i,j:nn j and E = J i,j:nn i j . Mind that in the case of a squared


lattice there are a limited number of possibilities that can occur. Consider creating a
look-up table to spare computation time during the acceptance loop. For a 2D lattice,
hi {0, 2, 4}.
P

Heat bath method (Glauber dynamics):


The Metropolis algorithm is not the only possible Monte Carlo update: there are a
number of other algorithms that fulfill Eq. (1.18). One of these has been elaborated
by Glauber, with the acceptance probability given as
h

exp kE
BT

A (X Y )

1 + exp kE
BT

(1.21)

One can see that the expression in Eq. (1.21) fulfills Eq. (1.18):
1=1
h

1 + exp kE
BT

E
E
i = exp
h
exp +

E
kB T
kB T
1 + exp
"

"

kB T

1 + exp

+ kE
BT

i

exp kE
BT

1 + exp kE
BT

E
1 + exp +
kB T
"

#!

exp kE
BT
h

exp
T (XY ) symmetric

const. temperature

exp kE
BT

EX EY
E
exp +
= exp
kB T
kB T

i =

exp

"

1 + exp kE
BT
h

"
EX
E
kB T
h
i exp +
kB T
exp kEBYT
h

exp + kE
BT

EY
EX
h
i = exp
h
i
E
kB T 1 + exp
kB T 1 + exp + E
kB T
kB T


peq (Y )T (Y X)AGl. (Y X) = peq (X)T (X Y )AGl. (X Y ).

Mind that knowledge about the whole system configuration before the spin flip is not
needed here: only the local configuration around the site is relevant. With J = 1, the
probability to flip the spin i is:
Ai =

12

exp
1+

2i hi
k T
hB
i
i hi
exp 2
kB T

13

Monte Carlo Algorithms

with hi being the local field as usual hi = j=nn j . Using the abbreviation pi
exp[2hi ]
1+exp[2hi ] , one can express the probability to flip the spin as being
P

pflip =

1 pi

for i = 1
for i = +1

and

pno flip =

1 p
pi

for i = 1
for i = +1

(1.22)

This can be implemented as


i ( + 1) = i ( ) sign(Ai z),
with z being a random number, or as
i ( + 1) =

+1

with propability pi
1 with propability 1 pi

with

pi

exp [2hi ]
1 + exp [2hi ]

This method which does not depend on the spin at time t, is called heat bath MC.
Binary mixtures (Kawasaki dynamics):
In this method, the sum of the spins pointing up and the sum of the spins pointing
down (i.e., the magnetization) is held constant. Kawasaki dynamics can be used for
simulating binary mixtures of gases and other systems were the population numbers
are conserved. In the case of a two species mixture, the energy is larger for A-B bonds,
with A and B being the two species in the binary mixture (spin up and spin down
particles, two different gas molecules, etc.). What one can do is to switch two particles
with a certain probability and then add the configuration to the averaging loop.
Creutz algorithm:
Let us consider a situation in which energy is constant. The algorithm generally used in
this case is the Creutz algorithm. In this technique the condition of energy conservation
is relaxed a bit and energy is not exactly conserved anymore. The movement in phase
space is therefore not strictly constrained to a subspace of constant energy, but we have
a certain additional volume in which we can freely move. The condition is softened by
introducing a demon, which is a small reservoir of energy Ed that can store a certain
maximum energy Em :
Choose a site
Calculate E for the spin flip
Accept the change if Em Ed E 0

13

14

Chapter 1: Monte Carlo Methods

This method is completely deterministic (besides the fact that one can randomly choose
the sites to update) and therefore reversible. The drawback is that the temperature
of the system is not known, but it can be estimated with the Boltzmann distribution.
h
i
Taking a histogram of the energies Ed one observes a distribution P (Ed ) exp kEBdT .
The fit is not optimal, since one has very few values of Ed . The bigger Em , the faster
is the method, since the condition of constant energy is relaxed and the exploration of
phase space is less restricted to certain regions.
Q2R:
In the case of Em 0, the Creutz algorithm becomes a totalistic cellular automaton
called Q2R. The update rules on a square lattice are then given by
i,j ( + 1) = f (xij ) i,j ( )
with
xi,j = i1,j + i+1,j + i,j1 + i,j+1

and

f (x) =

1
0

if x = 2
if x =
6 2

In this case, the spins are flipped if and only if the change in energy is zero. This can
be implemented in a very efficient way using multi spin coding. The changer word
defined by f (x) can be expressed in a very elegant way using logical functions:
( + 1) = ( ) {[(1 2 ) (3 4 )] [(1 3 ) (2 4 )]}
This logical expression can be computed in roughly 12 cycles (8 logical operation and
4 fetches) which last typically around 10ns. At this computational speed and using
multi spin coding (see Sec. 2.1) one can roughly update 4 sites per nanosecond. The
method is extremely fast, deterministic and reversible. The problem is that it is not
ergodic, and that it strongly depends on the initial configuration. As an example, try
to imagine the development of a small lattice, in which only 2,1 and 1,2 are equal
to +1. In fact, this method is not used in statistical physics but it is useful for other
purposes e.g., neuroinformatics or cellular automata.

1.2.2 Boundary conditions:


When simulating a lattice, one of the finite size effects that one has to take into account
is that at the boundaries the sites do not have further neighbors. The values there
can be fixed (boundary condition) or periodic boundaries can be introduced. Mind
that this is not only a question of finite size effects, but it can also correspond to
some real physical situation. As an example, think of some zero potential boundary
condition while solving the electrostatic potential using finite difference methods6 . In
finite lattices following methods can be used:
6

See Herrmann (2012)

14

15

Monte Carlo Algorithms


Open boundaries, i.e. no neighbors at the edges of the system.
Fixed boundary conditions,
and periodic boundaries.

If our system is big enough7 one identies the two sides of the lattice as being neighbors. (i,L+1 i,1 , and L+1,j 1,j ). Identifying the last element of a row (or column) with the first element of the next row (or column) leads to so called helical boundary condition. i,L+1 i+1,1 , therefore one can use only one index k = i + j(L 1)
instead of keeping track of two.

1.2.3 Sampling Uncorrelated Configurations


Each time we accept a spin-flip in our sampling chain, a new configuration is generated.
The problem is that the new and the previous configurations are strongly correlated,
and the error analysis (e.g., the decreasing of the error like 1N ) is no longer valid.
We have to find a measure to know whether we already are in equilibrium or not and
to be sure that we are sampling uncorrelated configurations. The (Monte Carlo) time
evolution of a quantity is defined as
hA( )i =

eq.

p (X, ) A (X ( )) =

p (X, 0 ) A (X( ))

(1.23)

With the condition expressed in (1.16), we


know how the probability distribution p
evolves. If we assume that our configuration distribution is not at equilibrium
at = 0 , we can define the non-linear
correlation function:
nl
A ( ) =

hA( )i hA()i
hA(0 )i hA()i

(1.24)

This is not a correlation function in the


strict sense, but it can be a measure to
investigate the correlation of the configurations.

Figure 1.7: Non-linear correlation function


over Monte Carlo time.

The non-linear correlation time Anl describes the relaxation towards equilibrium:
Anl
7

Z
0

nl
A ( ) d

(1.25)

Big here is not purely arbitrary but it depends on what we want to simulate. A good measure of
big can be that the edges of the system are uncorrelated. It is clear that this method is useless in
small lattices.

15

16

Chapter 1: Monte Carlo Methods

Near Tc , it assumes the form of a power law (critical slowing down):


nl

Anl |T Tc |zA

(1.26)

nl
with zA
being the non-linear dynamical critical exponent. This means that at Tc , the
time needed to reach equilibrium diverges!
The linear correlation function of two values A, B

AB ( ) =
with

hA (0 ) B ( )i =

hA(0 )B( )i hAi hBi


hABi hAi hBi

(1.27)

p (X, 0 ) A (X (0 )) B (X ( ))

is now a proper correlation function. Note that it goes from 1 to 0 when goes to
infinity. If A = B, we call Eq. (1.27) the autocorrelation function. For the spin-spin
correlation in the Ising model we get:
( ) =

h(0 )( )i h(0 )i2


h 2 (0 )i h(0 )i2

The linear correlation time Anl describes the relaxation in equilibrium:


AB

Z
0

AB ( ) d

(1.28)

Figure 1.8: Spin-spin autocorrelation function over MC time in the Ising model.

16

Monte Carlo Algorithms

17

Near Tc , it assumes the form of a power law (critical slowing down):


AB |T Tc |zA

(1.29)

with zA being the linear dynamical critical exponent.


The dynamical exponents turn out to be
z = 2.16 (in 2D)
z = 2.09 (in 3D)
There is a conjectured relation between the critical exponents of the previous sections
and the critical dynamical exponents (for spin and for energy correlation) that is numerically well established but not yet analytically proven:
z znl =
zE

nl
zE

=1

(1.30)
(1.31)
(1.32)

Decorrelated configurations:
The behavior we studied until now is only
valid in the case of an infinite lattice. In
a finite system there is no divergence by
definition (See Herrmann (2012)). The
correlation length diverges at the critical
temperature Tc :
L = (T ) |T Tc |
AB |T Tc |zAB L

(1.33)
zAB

which means that with growing system


size, the number of samples to be discarded also increases.

Figure 1.9: Finite size effects for different


system sizes can be used to obtain the critical temperature by extrapolation. See Herrmann (2012)

This is a problem when sampling big systems since the computation becomes very
expensive. To be sure not to sample correlated configurations one should
reach equilibrium first (discard n0 = c nl (T ) configurations).
Sample every nth
e = c (T ) configurations.

17

18

Chapter 1: Monte Carlo Methods


At Tc use n0 = cL

z nl

and ne = cL

where c 3 is a "safety factor" to make be sure to discard enough samples. A trick for
reducing this effect is using cluster algorithms, which we will encounter later on.

18

19

Finite Size Methods

1.3 Finite Size Methods


In the Ising model spins tend to form clusters, which adds a spatial correlation to the
time correlation. Quantities already introduced in Herrmann (2012) and (1.13) (the
correlation and the correlation length) are measures for how the system is spatially
correlated at separated locations. With increasing distance the correlation decays exponentially until the cut-off due to the finite system size. At Tc the correlation length
diverges with an exponent of , as described in Eq. (1.33). Many quantities diverge
at Tc (e.g. the magnetic susceptibility or the heat capacity). The larger the system
size, the more pronounced is the divergence.
In finite systems there is no real divergence,
and the peak will be cut at some value. The
maximum of the cutoff in the susceptibility
grows proportionally to L , while the criti1
cal region shrinks as L . If we rescale the
values for different system sizes (i.e., the
axes) accordingly, we will get a data collapse (i.e., all the values fall on one single
curve). This can be used to find the critical
exponents. One can for example plot

(T, L) = L F (T Tc ) L , (1.34) Figure 1.10: Divergence of the magnetic


where F is called scaling function.
Figs. 1.16

susceptibility for different system sizes. Note

See that the divergence is more pronounced for


larger system sizes.

1.3.1 Binder Cumulant


Binder defined a quantity, which is meant to be independent of the system size L at
Tc :
D
E
M4
L
(1.35)
UL 1
3 hM 2 i2L
This can be easily seen:
D

M4

L
2
3 hM i2L

=

L FM 4 (T Tc ) L
2

i
h

i2 = FC (T Tc ) L
1

L FM 2 (T Tc ) L

19

20

Chapter 1: Monte Carlo Methods

If T = TC , then the scaling function FC , which is just the ratio of two other scaling
functions, is a constant which is independent of the lattice size. For T > TC , the
magnetization follows a Gaussian distribution according to
s

PL (M ) =

Ld
M 2 Ld
exp
,
L
L
"

with L 
= kB T 2L . Since the
fourth moment equals three times the second moment
D
E
D
E2 
4
2
squared M = 3 M
it follows that UL must be zero. Below the critical temL

perature (T < Tc ) there are two ground states in the ordered phase (one with positive
and one with negative magnetization).
1
PL (M ) =
2

Ld
(M MS )2 Ld
(M + MS )2 Ld
exp
+ exp
l
L
L
(

"

"

#)

(1.36)

E2

For this distribution, it holds that M 4 = M 2 and therefore UL = 23 . This means


L
that

for T < Tc

3
UL = const. for T = Tc
(1.37)

0
for T > Tc
This is the most efficient way to calculate the critical temperature, since the Binder
cumulant UL is a quantity which is very sensitive to the temperature. For infinite
systems it shows a jump at Tc .

Figure 1.11: Binder cumulant for 2D finite systems in the Ising model.

20

21

Finite Size Methods

The divergence of quantities such as the magnetization, is only approximated as a


simple power law. Far away from Tc they do not follow this ansatz anymore, and corrections have to be made. We can observe the subdominant effects of this by calculating
the Binder cumulants for small system sizes.

Figure 1.12: 4D finite systems for the Ising model. Note that the lines do not cross all at
the same point.

M (T ) = A0 (Tc T )0 + A1 (Tc T )1 + ...


(T ) = C0 (Tc T )0 + C1 (Tc T )1 + ...
with 1 > and 1 < . These corrections are very important for high quality data,
where the errors are small and the deviations become visible. The scaling functions
must also be generalized as

1
M (T, L) = L FM (T Tc ) L + Lx FM
(T Tc ) L + ...

with x = max


, 1 ,

1 .

1.3.2 First Order Transition


Until now, we only considered critical points for a second order transition, which is
characterized by a discontinuity in the second derivative of the free energy (e.g., at the
critical temperature). There are also other kinds of transitions, which are characterized
by their order by means of the nth derivative of the free energy not being continuous.
We call those transitions, for which the lowest non-continuous derivative is of order n,
nth order phase transitions. For T < Tc , the Ising model has a jump in the magnetization at H = 0 (from minus to plus, by varying the field H), which is proportional

21

22

Chapter 1: Monte Carlo Methods

the first derivative of the free energy. This leads to the susceptibility assuming a delta
peak at H = 0. This kind of phase transition is more common in nature than second
order transitions.

(a) Specific heat

(b) Susceptibility

Figure 1.13: First order transitions

Usual symptoms of a first order transition are magnetization


jumps in small systems (See Fig
1.14), or the hysteresis. In infinite lattices this is not possible
since the jump to a ground state
of opposite sign would take an infinite (Monte Carlo) time to happen.
Figure 1.14: First order transitions.

Binder derived that if the distribution of the magnetization is described by two Gaussians, the magnetization (in dependence on the field H) has the form of a tanh(Ld ).

22

23

Finite Size Methods

Consequently, susceptibility can be calculated by differentiating the magnetization with


respect to the field H:


d
M (H) = D
L H + ML tanh HML L

L (H) =

M
ML Ld
= D
+
.
L
H
cosh2 (HML Ld )

(1.38)
(1.39)

Similarly to the scaling for the second order transition, we can scale the maximum of
the susceptibility (L (H = 0) Ld ) and the width of the peak (L Ld ).

Figure 1.15: Susceptibility for different system sizes in the Ising model.

Figure 1.16: Data Collapse for scaled susceptibility for different system sizes in the
Ising model.

23

24

Chapter 1: Monte Carlo Methods

1.4 Cluster algorithms


We have seen that the larger our system is, the longer we have to wait to sample
our states. This happened because just flipping one or a few spins before sampling
ends up in a strong correlation between the states, and the assumption of statistically
independent samples is not given anymore. The aim of cluster algorithms is to reduce
this critical slowing down by flipping multiple spins. It is essential that the group of
spins to flip is chosen with a tiny acceptance probability. To do this, we will generalize
the Ising model and adapt it to our needs.

1.4.1 Potts Model


The Potts model is a generalization of the Ising model to a q-states model, and it
also exhibits in certain cases a first order phase transition at a critical temperature.
Applications of the Potts model range from sociology through biology to material
science, making this a very versatile model. The Potts model exhibits a first order
transition in 2D for any q larger than 4, and for any q larger than 2 for dimensions
larger than 2. We start by defining the Hamiltonian of the system as
H = J

X
i,j=nn

i ,j H1

i ,1

(1.40)

with i {1, ..., q}, and q being an integer 2. In this case, the state 1 is favored, by
lowering the energy whenever a site is in this particular state. The sites of the lattice
we are dealing with can assume more than just two states (up/down, as we had in the
Ising model). The interesting fact now is that there is a strong connection between the
Potts model and the well known bond percolation model. After Kasteleyn and Fortuin
(1969), which we will discuss now, the two models have related partition functions.

24

Cluster algorithms

25

Figure 1.17: Specific heat for two dimensional Potts model with q=10. On the right the
scaling for different sizes.

Any system is characterized by its partition function. From the latter one can derive
all other thermodynamic quantities. Therefore the complete knowledge of the partition
function corresponds to the complete knowledge of the system. If two systems have
exactly the same partition function, those two systems are therefore equivalent. In the
following, we will prove a relation between the bond percolation and the Potts model.
The Kasteleyn and Fortuin theorem:
Consider the Potts model not on a square lattice, but on an arbitrary graph of nodes
connected with bonds. On the nodes we have q possible states, and for every connection
we have an energy cost of 1 if two connected nodes are in a different state and of 0 if
they are in the same state.
E=J

with  =

0
1

if endpoints are in the same state


else

(1.41)

We also introduce two operations on the bonds: the contraction C and deletion D
(see Fig. 1.18). Bonds can thus be either removed by merging two sites (which implies
that they have the same state) or be simply removed.

25

26

Chapter 1: Monte Carlo Methods

Figure 1.18: Contraction and deletion of bonds.

The partition function is the sum over all the possible configurations weighted by
the Boltzmann factor:
Z=

(1.41)

eE(X) =

eJ

XY
X

(1.42)

eJ

We will now consider a particular bond 1 and rewrite the partition function as
X
X

eJ1

Y
6=1

eJ =

eJ + eJ

= ZC + e


eJ

X,i 6=j 6=1

X,i =j 6=1
J

(ZD ZC )


= 1 eJ ZC + eJ ZD
= pZC + (1 p) ZD
Here i and j are the states at the two ends of the bonds, ZC and ZD are the partition
functions of the graphs contracted and deleted at 1 respectively and p 1 eJ .
We have now divided the partition function in a sum of two other partition functions,
namely the partition function of the same system with a deleted bond and the partition
function of the same system with a contracted bond. We can repeat this operation on
another bond 2 , and we find that
Z = p2 ZC1 ,C2 + p(1 p)ZC1 ,D2 + (1 p)2 ZD1 ,D2 .

26

27

Cluster algorithms

We repeat this operation on every bond. At the end the graph is reduced to a set of
separated points corresponding to connected/contracted bonds: the clusters of sites
which are connected and in the same state. The partition function is then reduced to:
Z=

q # of clusters pc (1 p)d = q # of clusters

configuration of

E
bond percolation configurations

bond percolation

(1.43)
where c and d are the numbers of contracted and deleted bonds respectively. We have
now an equivalence between a purely geometrical model (percolation) and a thermal
(or magnetic) model with a Hamiltonian (Potts model). Interestingly, one could now
choose values for q which are not integers. This made no sense in the beginning since
q was introduced as the state of each site.
Coniglio-Klein clusters:
Mind that the equivalence between the bond percolation model and the magnetic model
is of statistical nature, since a particular bond configuration can have several spin configurations and a particular spin configuration can have several bond configurations.
The fact that the value of the spins is absent in Eq. (1.43) forms the base for cluster algorithms. The probability of a given cluster C to be in a certain state 0 is independent
of the state itself:
p(C, 0 ) = pcC (1 p)dC

q # of clusters pc (1 p)d

(1.44)

bond percolation
without cluster C

This means that flipping this particular cluster wont affect the partition function
(and therefore the energy) so that it is possible to accept the flip with probability one.
This can be seen by looking at the detailed balance for this system:
p(C, 1 )W [(C, 1 ) (C, 2 )] = p(C, 2 )W [(C, 2 ) (C, 1 )]

(1.45)

and remembering that p(C, 1 ) = p(C, 2 ). If hwe now apply


Glauber dynamics we
i
p(C,2 )
p(C,2 )
1
obtain W = p(C,1 )+p(C,2 ) = 2 and W = min 1, p(C,1 ) = 1 for Metropolis. This
makes the cluster algorithm much faster than single-spin flip algorithms and reduces
the problem of critical slowing down.

1.4.2 O(n) Model


There are a number of models that resemble the Ising model, but with slight modifications to adapt them to a particular context (e.g., the antiferromagnetic models,
Ising spin glass, ANNNI model, metamagnets, ...). One of the possible generalizations of the Ising model is the so called n-vector model. Unlike the Potts model, it
takes into account that the degrees of freedom of the sites do not have to be discrete.

27

28

Chapter 1: Monte Carlo Methods

This is a very important model for describing phenomena related to magnetism (e.g.,
ferromagnetism). The variables on the sites are n-dimensional vectors. The Hamiltonian resembles the one of the Potts model in the sense that it favors alignment of the
variables:
H=J

X
i,j:nn

~i S
~j + H1
S

~ 1 with S
~i = S 1 , S 2 , ..., S n
S
i
i
i
i

~
and S
i = 1

(1.46)

For n = 1 we retrieve the Ising model, for n = 2 we have the XY-model, for n = 3 the
Heisenberg model and finally, for n = we have the spherical model.
Note that the models for various values of n are not equivalent. In fact, there are huge
differences. As an example, the XY-model does not exhibit any phase transition from
an ordered to a disordered state. The proof of this statement can be found in Mermin
and Wagner (1966), and it is known as the Mermin-Wagner theorem. Ernst Ising
himself proved in his PhD thesis that the one dimensional case also does not exhibit
any phase transition. In three dimensions, however, there are phase transitions and the
behavior regarding magnetization, susceptibility and specific heat is very similar to the
behavior of the Ising model we studied earlier (critical temperature, critical exponents
etc.).
For Monte Carlo simulations with continuous degrees of freedom we have to adapt
our techniques because phase space is not a discrete set of configurations anymore. The
classical strategy is to make spin flips by modifying the spin locally by a fixed amount
~0 = S
~i + S.
~ The classical Metropolis algorithm can then be used in
(a rotation): S
i
the same fashion as in the discrete models. In order to use cluster methods one can
project a group of spins onto a plane, and then reflect the spins with respect to the
plane.
In order to obtain clusters one has to find a method to identify equal spins. The
probability to find equal spins in a continuous model vanishes and one can therefore
considers a certain range of values instead(if two spins are equal within a certain error,
then they belong to the same cluster).

28

Cluster algorithms

29

Figures: Broad Histogram method for the 3D XY model.

1.4.3 Implementation of Cluster Algorithms


Swendsen-Wang algorithm:
The Swendsen-Wang algorithm is a refined Monte Carlo technique. For a certain
configuration, we look through the bonds connecting the spins. Whenever two bonded
sites are in the same state, the two sites belong to the same cluster with probability

29

30

Chapter 1: Monte Carlo Methods

p 1 eJ . Once the clusters are determined, they can be flipped using any of the
dynamics mentioned above. The basic procedure is as follows:
Occupy the bonds with probability p 1 eJ if sites are in the same state.
Identify the clusters with the Hoshen-Kopelman algorithm.
Flip the clusters with probability 0.5 for Ising or choose always a new state for
q > 2.
Repeat the procedure.

Figure 1.19: Critical slowing down for the Swendsen-Wang algorithm.

Wolff algorithm:
In this variant only one cluster is flipped per step using the Leath algorithm:
Choose a site randomly.
If neighboring sites are in the same state, add them to the cluster with probability
p 1 eJ .
Repeat this for any site on the boundaries of the cluster, until all the bonds of
the cluster have been checked exactly once.
Choose any new state for the cluster.
Repeat the procedure.

30

31

Histogram Methods

1.5 Histogram Methods


Histogram methods go back to Salzburg et al. (1959). To reduce the computational
cost, one can compute a certain quantity at a given temperature and then extrapolate
the result to other temperatures. This is only possible if the temperature dependence
is known. In the case of canonical variables the dependence is given by the Boltzmann
factor. As a start, the partition function is reformulated as a sum over all possible
energies instead of over all possible configurations:
Q (T ) =

X
1 X
Q (E) pT (E) with ZT =
pT (E)
ZT E
E

(1.47)

where pT (E) g (E) e kB T with g (E) being the density of states (i.e. the number of
states with energy E). This takes into account the fact that more configurations can
have the same energy. The goal is to compute the quantity Q at another temperature
P
T : Q (T ) = Z1 E Q (E) pT (E). The density of states contains all the information
T
needed. Using the definition of g (E) yields
p

(E) = g (E) e

BT

=
|{z}

def. of g (E)

and with fT,T (E) exp kBET +


Q (T ) =

E
kB T

E
E
pT (E) exp
+

kB T
kB T


one finally obtains

Q (E) pT (E) fT,T (E)


.
E pT (E) fT,T (E)

(1.48)

We have now obtained an expression for the quantity Q at the temperature T without
having to measure any distribution at temperature T, since fT,T are known scalars.
The drawback of this method is that the values of Q (E) are sampled around the
maximum of pT (E), which converges to a delta distribution for large systems. This
means that if T and T are not very close, the statistics are very poor, and results will
be inaccurate or even wrong. One possible solution consists of interpolating data from
several temperatures (multi-canonical method) but this involves calculations for many
temperatures, which is also not efficient for large systems. Another solution to this
problem has been presented in de Oliveira et al. (1996) and will be also treated in this
chapter.

1.5.1 Broad Histogram Method


The aim of the broad histogram method (BHMC) is to directly calculate the density of
states. It obtains the function g(E) through a stochastic process. The density of states
increases very steeply (exponentially) with energy E, since the number of possible

31

32

Chapter 1: Monte Carlo Methods

configurations increases with higher energy. A plain random walk through phase space
would also have the problem that it would explore only the regions with high energies.
To explore all the regions equally (with respect to the energy) we can create a Markov
chain in which the number of processes that increase the energy by E (we shall call
this number Nup ) and the number of processes that decrease the energy (Ndown ) fulfill
an equivalent condition to detailed balance to reach a homogeneous steady state:
g (E + E) Ndown (E + E) = g (E) Nup (E) .

(1.49)

The motion (in phase space) towards higher energies can then be penalized with
Metropolis-like dynamics:
Choose a new configuration
If the new energy is lower, accept the move
If the new energy is higher then accept with probability

Ndown (E+E)
Nup (E)

However, we havent obtained the function g (E) yet. Therefore we will take the logarithm of Eq. (1.49) and divide by E
log [g (E + E)] log [g (E)] = log [Nup (E)] log [Ndown (E + E)]
Nup (E)
log [g (E)]
1

=
log
E
E
Ndown (E + E)
"

(1.50)

which we can numerically integrate to obtain g (E). Estimates of Nup and Ndown can
be obtained by checking if a change of state at each site would increase or decrease the
energy.

Figure 1.20: Example for the density of


states for the Ising model

32

Figure 1.21: Number of visits done in the


phase space against the energy.

Histogram Methods

33

While moving through phase space one


can not only accumulate the values of Nup
and Ndown , but also keep the values for
any quantity Q (E). Finally one can compute
Q (T ) =

k ET

Q (E) g (E) e

k ET
B

g (E) e

Knowing g (E) one can now calculate


quantities at any temperature.

Figure 1.22: The continuous line is the analytic solution of the specific heat for a 32x32
lattice. The crosses represents the result of
the BHMC and the circles are the result for
the usual histogram.

1.5.2 Flat Histogram Method


This is another method, proposed by Wang, to obtain a flat distribution of visits in
energy space. The basic algorithm works as follows:
Start with g (E) = 1 and set f E.
Make MC update with p(E) = 1/g(E).
If the attempt is successful at E: g(E) = f g(E).
Obtain a histogram of the energies H(E).

If H(E) is flat enough, then f f .


Stop when f 108 .
Note that choosing the probability as 1/g(E) in the second step implies that for
higher configuration density at energy E, the choosing probability decreases. Therefore
the method tends to go towards energies with fewer configurations. At this point the
function has to be corrected by updating the value of g and the energy E. Once a
histogram has been obtained one can increase the precision by decreasing f , e.g. by
dividing it by two. The flatness of the histogram can be measured as the ratio of the
minimum to the maximum value. For further details, see the Wang-Landau algorithm.

33

34

Chapter 1: Monte Carlo Methods

1.5.3 Umbrella sampling


This technique was developed and proposed in Torrie and Valleau (1977). The aim is
to overcome the problem of the lack in ergodicity for certain energy landscapes. As
an example, in the Ising model the system could have difficulties in jumping from a
positive to a negative magnetization (or vice versa) if the system is very large. The
basic idea is to multiply transition probabilities with a function that is large at the free
energy barrier and later remove this correction in the averaging step.
p (C) = P

E(C)
BT

w (C) e

E(C)
BT

C w (C) e

with

hAi =

hA/wiw
.
h1/wiw

(1.51)

Summarizing, some of the most common techniques related to the histogram methods
are
Wang-Landau method
Multiple histogram method
Multicanonical MC
Flat Histogram method
Umbrella sampling

34

Renormalization Group

35

1.6 Renormalization Group


In this section, we wont be able to go very deeply into the theory of the subject due to
its strong mathematical nature. We will therefore only skim through the main concepts
and present some numerical results, for more details see Niemeijer and van Leeuwen.
We will make use of symmetries of the physical system to improve our simulations.
Usually, the more information there is available about a system, the better certain
quantities can be evaluated. Close to the criticality, changes in the scale of the system can be used to better extrapolate the values to an infinite system. Close to the
critical point, one of the most important properties is the invariance under conformal
transformations. This also implies the invariance under changes of scale (dilation invariance). Mind that this is only true around Tc . To increase mathematical precision
one has to define exactly what it means to change scale. In the case of geometrical
objects, the meaning is somewhat intuitive. A good example is the density in fractals,
which does not change under scale transformations (remember the sand-box method
in Herrmann (2012)). However, we wikk also treat systems with different energy and
spin configurations as well as different Hamiltonians. One possibility is to look at the
free energy density, and its invariance. To renormalize a system means to change its
= L/l. This can be done either in position, or in momentum
scale by a factor l: L
(Fourier) space.

1.6.1 Real Space Renormalization


If the system is invariant under a certain transformation, in theory one is able to iterate
this transformation infinitely often without changing the quantities in the system8 . In
other words, the critical point is a fixed point for this transformation. In order to put
the concept of renormalization in a mathematical framework we will give two concrete
examples (renormalization of the free energy and decimation of the 1D Ising model).
After that, in sec. 1.6.2, we will generalize the concept and present the implementation
of renormalization with MC in sec 1.6.3.
Renormalization and free energy
We consider the free energy density of a system, and require it to stays invariant under
scale transformations. Since the free energy is an extensive quantity9 , we have to

[...]We can construct certain transformations on the Hamiltonian of the system for which the critical
point is a fixed point. Meaning that observables do not change, no matter how many times we apply
these transformations. (Wilson, 1975)
9
Extensive quantities change with the size of the system, like the volume or the total mass of a
gas. Intensive quantities are not dependent on the system size, e.g., the energy density or the
temperature.
8

35

36

Chapter 1: Monte Carlo Methods

renormalize the quantity itself to the system size. With F being the renormalized free
energy,


= ld F (, H) with  T Tc .
F , H
(1.52)
We then make use of the scaling law close to the critical point:
F (, H) = ld F (lyT , lyH H)
= ld F (lyT , lyH H)
F , H


= lyH H.
 = lyT , H

(1.53)

Since renormalization also affects the correlation length |T Tc | = || we


can relate the critical exponent to yT :
 =
 =


l

= l  = lyT 

1
.

The critical point is a fixed point of the transformation: at Tc ,  = 0, and whatever


the value of the scaling factor,  does not change.
yT =

Majority rule
An easy example that can be regarded as renormalization of a spin systems is the
majority rule. One considers local groups of spins i , and instead of considering
P
them
all separately taking a mean over that local group of spins:
i = sign
region i (see
Fig. 1.23). Attention must be paid on how the transformation is done. In a one
dimensional lattice with up/down spins for example it would be an error to apply this
transformation on an even number of spins, since the sign is then not well defined for
all the possible situations. The fact that one deals with system of a finite size is also
something that has to be taken into account: one can renormalize up to a certain scale,
before finite size effects are visible.

36

Renormalization Group

37

Figure 1.23: Majority rule for a 2D Ising model. The spins are grouped into larger regions
and averaged. If the majority of the spins is in a certain state, that region is considered as a
single spin in that state.

Decimation of 1D Ising Model


Another possible rule is decimation (see Fig. 1.24). In decimation one just eliminates
certain spins, generally in a regular pattern.

Figure 1.24: Decimation of spins: every second spin in every direction is ignored.

As already mentioned, we will consider a one dimensional Ising chain as a practical


example. The spins that only interact with their nearest neighbors:

37

38

Chapter 1: Monte Carlo Methods

To see explicitly what happens with the system, we calculate the partition function
Z. We will split the partition function into two terms: one for the even sites and one
for the odd sites. With K = KJB T and using
A=

eK(s2n s2n+1 +s2n+1 s2n+2 )

X
s2n+1 =1

= eK(s2n +s2n+2 ) + eK(s2n +s2n+2 )


= cosh [K (s2n + s2n+2 )]
we get

Z=

s2i =1 2i

X Y
X Y

eK(s2i s2i+1 +s2i+1 s2i+2 )

s2i+1 =1 2i+1

{2cosh [K (s2i + s2i+2 )]}

s2i =1 2i

X Y

z (K) eK s2i s2i+2

s2i =1 2i
N

= [z (K)] 2

X Y

eK s2i s2i+2

s2i =1 2i
0

There are only two possible states, and we can compute z (K) eK s2i s2i+2 = 2cosh [K (s2i + s2i+2 )]
explicitly:
K 0 s2i s2i+2

z (K) e

2cosh [2K]
2

for s2i = s2i+2


for s2i = s2i+2

(1.54)

By dividing and multiplying the two cases we get


0

e2K = cosh [2K] and z 2 (K) = 4cosh [2K]


1
K 0 = ln (cosh [2K]) .
2
We have now obtained a rule after which the coupling constant K (which contains the
temperature and the spin coupling constant J) changes under scaling. In summary,
we:
chose the scaling rule (decimation).
imposed the free energy to be constant
computed the consequences on the coupling constant J

38

39

Renormalization Group

Generally, when renormalizing a system, there are two steps that one has to undertake:
Decide which scale to change (e.g., the length of the system, the spins, etc.).
Evaluate the consequences for the system (e.g., for the Hamiltonian, the free
energy, etc.).

1.6.2 Generalization
In the above example it was possible to fulfill the condition of constant free energy with
just one coupling constant. Luckily, the transformation rule for the coupling constant
K was enough to fulfill all the conditions expressed by eq. (1.54). Generally this is not
the case, and more coupling constants (e.g., next nearest neighbors) are needed to close
the system of equations. We therefore have to construct a renormalized Hamiltonian
made up of renormalized coupling constants which generally contain many possible
interactions:
=
H

M
X

with O
=
K O

=1

X Y

i+k

(1.55)

i k

and with the renormalization rules


(K1 , ..., KM ) , with {1, ..., M } .
K
Note that using only M interaction terms instead of an infinite number is a truncation, and in fact a systematic error. The accuracy of this method will depend on the
number of iterations that we want to take into account. At Tc we have a fixed point:
(K1 , ..., K ) = K . A first ansatz to solve this problem is the linearization of the
K
M

K
transformation. We do this calculating the Jacobian T, = K
:

K =
K

T, |K K K

(1.56)

39

40

Chapter 1: Monte Carlo Methods

We can now construct a flow chart of the


coupling constant and obtain values for
for each vector K = (K1 , ..., KM ). At
K
Tc (where K = K ), we will have a fix
point. Fig. 1.25 shows a flow diagram of a
renormalization group transformation. It
can already be seen that the flow is stable in some directions (the ones in which
the system tends to the fixed point) and
unstable in others (the ones that lead the
system to go away from the fixed point).

Figure 1.25: Flow diagram of a renormalization transformation.

To analyze the behavior of the system close to criticality, we will consider the eigenvalues 1 , ..., M and the eigenvectors 1 , ..., M of the linearized transformation 1.56.
From linear algebra we know that = . Clearly, if > 1 we have an unstable
~ increases at every iteration of the transformation).
situation (the distance from K
The biggest eigenvalue will be the dominating one (which is the temperature exponent
in the Ising model). We can identify the scaling field  = lyT  with the eigenvector of
the transformation, and the scaling factor with eigenvalue T = lyT ,
=

1
ln [l]
=
.
yT
ln [T ]

This means that we can now calculate critical exponents using the scaling behavior of
the system, if the scaling factor l is known.

1.6.3 Monte Carlo Renormalization Group


The implementation of real space renormalization with Monte Carlo techniques was
first proposed by Ma (1976) and then reformulated by Swendsen (1979). Since we are
dealing with generalized Hamiltonians with a lot of interaction terms, we will calculate
the thermal average using the operators O .
hO i =

40

e
OP

K O

K O

F
K

(1.57)

41

Renormalization Group

where F = ln [Z] is the free energy. Using the fluctuation-dissipation theorem one can
also numerically calculate the response functions:
hO i
= hO O i hO i hO i
K
D
E

D
E D
E
O
O O
hO i
= O

,
,

Using the chain rule, one can calculate with equation (1.57) that

O
D

E
D
(n)
O
X K

T, (n+1)
, .

Thus we can obtain T, from the correlation functions by solving a set of M coupled
linear equations. We can iterate this method, in order to get systematically better
values, if we are at the point K = K .

Figure 1.26: Scheme of the MCRG strategy.

41

42

Chapter 1: Monte Carlo Methods

Figure 1.27: for a 4-state Potts model in 2D. Note the convergence.

Errors in MCRG
There are many error sources in this technique, that originate from the fact that we
are using a combination of several tricks to calculate our results, of which one should
be aware of:
Statistical errors
Truncation of the Hamiltonian to the Mth order.
Finite number of scaling iterations
Finite Size effects
No precise knowledge of K

42

2 Parallelization
In this short Chapter we will briefly discuss the main methods for making computations faster in a very general way. Parallelization can lead to an improvement of the
computation time of up to entire orders of magnitude! The easiest way to parallelize is
by farming: repeating the same calculations on many different computers/processors
with different data. This can be very easy to program in situations like Monte Carlo,
where one has to repeat the exact same sequence of operations as many times as possible. Of course, this is mostly not regarded as smart parallelization. Already with
some very basic knowledge of computer architecture there is the possibility of drastically improving the efficiency of the programs. Two ways to parallelize, which imply
a little deeper understanding of how a computer works, are presented in the chapter
(multi-spin coding and vectorization). The chapter is obviously not exhaustive,
and for the interested reader dedicated lectures are recommended.

2.1 Multi Spin Coding


Multi spin coding is a technique to reduce the computational time of a program. It is
very useful whenever dealing with single bit variables or integers limited to a certain
value. The idea is based on the fact that on a 64 bit system, not all the bits are used
for storing and/or for computations. In the case of the Ising model, we are dealing
with spins that can only assume two values {1}. This means that storing and
calculating our site values into 64 bit integers is not only a waste of memory, but also a
waste of computation time since most of the bits are not carrying any information. In
a cubic lattice, the local field can only assume values hi {0, 2, 4, 6}, for a total
of 7 possible values. 3 bits1 would then be enough to cover all possible local field values
of any site (i), while 61 bits remain unused. If we contemplate an integer variable E,
with bit values {0, 1},
Ej = (1 , ..., 64 )
it is possible to use every three digits for the local field of a site i
Ej = (1 , 2 , 3 , 4 , ..., 62 , 63 , 64 )
|

{z

(i+20)

{z

(1)

(2.1)

In binary code you can represent up to 8 different numbers with 3 digits

43

44

Chapter 2: Parallelization

And for the values of the spin on the sites we can use another integer N:
Nj = (1 , ..., 0, 0, 3i2 , ..., 0, 0, 1 )
|

{z

(i)

(2.2)

| {z }
(1)

where the value of 3i2 defines the ith spin. This way, only one bit remains unused,
instead of 63 (or 31, depending on the computer architecture). The price that one has
to pay is that we must be carefulabout how we organize the storage of the variables in
the words. This is because neighboring variables should not be stored in the same word.
As mentioned before, besides a purely memory storage question, the main advantage is
a much faster calculation. For this we will make use of bitwise logical functions XOR,
AND and OR (, and ). First, to extract the information about one site out of an
integer, we will define the mask: 7 (0, ..., 0, 1, 1, 1). This can be used to fetch the
value out of the last three digits of an integer Nj , as defined before.
7 Nj = (0, ..., 0, 1, 1, 1) bitwise (1 , ..., 62 , 63 , 64 ) = (0, ...0, 62 , 63 , 64 )

(2.3)

With the circular right shift operator (ror) or the circular left shift operator (lor) one
can act a clockwise (or counter-clockwise) permutation of the bits stored in an integer
variable
ror [(1 , 2 , 3 , 4 , 5 ), 2] = (4 , 5 , 1 , 2 , 3 )
(2.4)
Combining the two operations defined in equations (2.3) and (2.4) allows us to fetch
all the 21 sites or energy values stored in an integer. Similarly for the calculation of
the energy, only one cycle is needed to calculate the energy of 21 sites:
Ej = Nj Nright + Nj Nlef t +, ...+, Nj Ndown
For an example of implementation in C/C++, see A.1.

44

(2.5)

Vectorization

45

2.2 Vectorization
Multiple instruction - single data:
Vectorization means organizing the routines in such a way that many operations can be
done simultaneously. Vectorization only works in the innermost loops. As an example
1
2
3

i_max = 1 0 0 0 0 ;
f o r ( i =1; i<=i_max ; i ++){

A( i ) = B( i ) (C( i ) + D( i ) ) ;

5
6
7

In the loop, one has to do several operations: fetch the values of the vectors A,B,C,D,
multiply them, adding etc. ... The aim is to parallelize all these operations in a pipe.
This is usually already implemented in the compilers, so that the user does not have to
care about it. In more complicated routines this may have to be implemented directly.
The bad features for vectorization of a routine include
Short loops.
Conditional branchings like if-statements.
Indirect addressing.
If the loop is smaller than the assembly line then theres no need to bother programming in a vectorized way (i.e. if im ax in the upper example would be 4 or 5).
Another problem is that the instructions must be repetitive without interruptions or
exceptions like if-statements. There are ways to handle some cases in which a distinction is needed, but this is often a problem. An example would be replacing
1
2
3

i f (P( i )>z ) {
s = s ;
}

by
1

s = s s i g n ( zP( i ) ) ;

In order for vectorization to be maximally exploited, one has to make sure that the
inner loop is the largest. One can, for example, transform a n-dimensional system in
a one dimensional system using helical boundary conditions and appropriate indexing.
Other important elements of an efficient vectorized routine are

45

46

Chapter 2: Parallelization
Make update in inner loop, i.e. no loops inside this loop.
Use a vectorized random number generator.
Split up loops that cannot be vectorized.

SIMD vs MIMD:
There are many types of parallelization, but one can roughly divide them into two
groups according to the flexibility of the process and accessibility of your memory
access: Single Instruction - Multiple Data (SIMD) and Multiple Instruction - Multiple
Data. In the first case one applies the same set of instructions to all the data. In
the second one, different instructions are applied to different data, with the drawback
that the routines loose simplicity. There are shared memory computers which have the
advantage that they are really flexible in the organization of the data structure and
distributed memory computers which are much easier to build and program. Finally,
there is the possibility of combining a great number of processors, each of them with
very basic features or a smaller number of more powerful processors (coarse graining
vs fine graining). For each combination there exist different solutions with different
programming philosophies.
SIMD

MIMD
shared memory distributed memory
coarse grained
fine grained
MC parallelization:
Monte Carlo on regular lattices is well suited for parallelization because of a number
of reasons:
Local processes that do not involve the whole system (nearest neighbors).
Domain composition is possible (sub-lattices).
Use standard domain decomposition and distribute using in MPI.
Use logical mask to extract one sub-lattice.
Use periodic shift (CSHIFT) to get neighbors for periodic boundary conditions.
There are many options for dividing a system into domains. Depending on the
situations it can be more convenient to increase the domains or reducing the interfaces
between the domains in case that the processes need to work independently (e.g., in
the case of distributed memory). It is also possible to dynamically change the size of

46

Vectorization

47

the domains and the position of the interfaces. For an example of parallelization, see
Section A.2.

Figure 2.1: Domain decomposition of a square lattice.

Figure 2.2: Different interface setups for domain decomposition of a square lattice

Message Passing Instruction:


To parallelize a routine one has to use specific programming languages created specifically for parallelizing or embed special libraries in which the parallelization has been
implemented in such a way that it can be summoned in standard programming languages such as C++. An interesting example is CUDA (Compute Unified Device Architecture), a parallel computing platform and programming model created by NVIDIA

47

48

Chapter 2: Parallelization

and implemented by the graphics processing units (graphic cards) that they produce.
Graphic cards are capable of very fast computation of easy operations, and that can
be used in some cases to do scientific computing. Message Passing Instruction (MPI)
is a standardized and portable message-passing system. This means that instructions
can be passed by the user within programs written in languages such as Java or C++.
See Section A.3 for basic examples of MPI coding.
Amdahls law:
The bottleneck of parallelization is the communication between processors. Processors
are not isolated units that completely work on their own, and the more one slices the
system into domains, the more communication between processors is needed. This is
generally not efficient and usually slows down the parallelization.

Figure 2.3: Efficiency of Parallelization. On the right the fraction of parallelized time to
total time.

48

3 Bibliography
K. Barth. Oral History Transcript Dr. Marshall Rosenbluth. 2003. URL http:
//www.aip.org/history/ohilist/28636_1.html.
P.M.C. de Oliveira, T.J.P. Penna, and H.J. Herrmann. Broad histogram method.
Brazilian Journal of Physics, (26):677, 1996.
J.E. Gubernatis.
Marshall rosenbluth and the metropolis algorithm.
AIP,
2005. URL http://scitation.aip.org/content/aip/journal/pop/12/5/10.
1063/1.1887186.
W.K. Hastings. Monte carlo sampling methods using markov chains and their applications. BioMetrika, 1969. URL http://biomet.oxfordjournals.org/content/57/
1/97.
H.J. Herrmann. Introduction to Computational Physics. lecture notes, 2012.
S.K. Ma. Monte carlo renormalization group. Phys. Rev. Lett., 37:461, 1976.
N.D. Mermin and H. Wagner. Absence of ferromagnetism or antiferromagnetism in
one- or two-dimensional isotropic heisenberg models. Phys. Rev. Lett., (17):1133,
1966.
N. Metropolis, A. W. Rosenbluth, M.N. Rosenbluth, A.H. Teller, and E. Teller.
Equation of state calculations by fast computing machines. J. of Chem. Phys.,
1953. URL http://en.wikipedia.org/wiki/Equation_of_State_Calculations_
by_Fast_Computing_Machines.
T. Niemeijer and J.M.J. van Leeuwen. Renormalization: Theory ising-like spin systems. Phase transitions and critical phenomena, 6. URL http://en.wikipedia.
org/wiki/Phase_transitions_and_critical_phenomena.
Z.W. Salzburg, J.D. Jacobson, W. Fickett, and W.W. Wood. Excitation of non-radial
stellar oscillations by gravitational waves: a first model. Chem Phys, (30):65, 1959.
E. Segr. Enrico Fermi, Physicist. University of Chicago Press, 1970. ISBN 0-22674473-6.
H.E. Stanley. Introduction to Phase Transitions and Critical Phenomena. Clarendon,
Oxford, 1971.

49

50

Chapter 3: Bibliography

H.E. Stanley. Scaling, universality, and renormalization: Three pillars of modern


critical phenomena. 1999. URL http://link.springer.com/chapter/10.1007%
2F978-1-4612-1512-7_39.
R. Swendsen. Monte carlo renormalization group. Phys. Rev. Lett., 42:859, 1979.
G.M. Torrie and J.P. Valleau. Nonphysical sampling distributions in monte carlo
free-energy estimation: Umbrella sampling. Journal of Computational Physics,
23:187199, 1977. URL http://www.sciencedirect.com/science/article/pii/
0021999177901218.
Kenneth G. Wilson. The renormalization group: Critical phenomena and the kondo
problem. Rev. Mod. Phys., 47:773840, Oct 1975. doi: 10.1103/RevModPhys.47.773.
URL http://link.aps.org/doi/10.1103/RevModPhys.47.773.

50

Appendix A
Codes
In this Section, the codes have been taken by various open sources online. The routines
have been heavily shortened and altered for the sake of the explanations, mainly in
Chapter 2. It is obvious that the codes have not been tested and that we do not
guarantee for the compatibility of what are meant to be simple examples for this
scriptum.

A.1 Multi Spin Coding


1

#i n c l u d e <s t d i o . h>

2
3
4

// d e f i n e t h e " change " word


cw=0;

5
6
7
8

// go through t h e 21 s i t e s s t o r e d
// i n N with f i e l d v a l u e s E
f o r ( i =1; i <=21; i ++){

z=r a n f ( ) ;
// random number f o r t h e f l i p
i f ( z<P(E&7) ) { // f e t c h t h e v a l u e from t h e l o o k up t a b l e
cw=(cw | 1 ) ; // t h i s v a l u e has t o be f l i p p e d l a t e r
}

10
11
12
13
14
15
16
17
18
19

// c i r c u l a r r i g h t s h i f t
// t o g e t t h e next s i t e
cw=r o r ( cw , 3 ) ;
E=r o r (E , 3 ) ;

20
21
22

cw=r o r ( cw , 1 ) ; // i g n o r e t h e l a s t unused b i t
N=(N^cw ) ;
// f l i p t h e s p i n s i n N

chapters/codes/multi_spin.cpp

51

52

Chapter A: Codes

A.2 Parallelization
1
2
3
4
5

!
!
pi_hpf . hpf compute p i by i n t e g r . f ( x ) = 4 / ( 1 + x 2 ) from 0 t o 1
!
This i s an Adaptor 7 . 0 c o m p a t i b l e HPF program
!
PROGRAM pi_hpf

6
7
8
9
10
11

IMPLICIT NONE
DOUBLE PRECISION
PI25DT , pi , h , sum , x
REAL
t ( 2 ) , t _ s t a r t , t_end
INTEGER
n , i , numprocs
PARAMETER( PI25DT = 3 . 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3D0 )

12
13
14

! HPF$
! HPF$

PROCESSORS, DIMENSION(NUMBER_OF_PROCESSORS( ) ) : : a l l _ p r o c s
PROCESSORS : : one_proc

! ADP$
! ADP$

REPLICATED ONTO a l l _ p r o c s : : n
SINGLE : : t _ s t a r t , t_end , t , PI25DT , numprocs , p i

15
16
17
18
19
20
21
22

INTERFACE
EXTRINSIC(F77_SERIAL) FUNCTION ETIME( t )
REAL : : ETIME, t ( 2 )
END FUNCTION ETIME

23
24
25
26

EXTRINSIC(F77_SERIAL) SUBROUTINE PrintOut ( )


END SUBROUTINE PrintOut
END INTERFACE

27
28
29

numprocs = NUMBER_OF_PROCESSORS( )
WRITE( , ) "HPF i n i t i a l i z e d with " , numprocs , " p r o c e s s o r s "

30
31

DO

32
33
34
35
36

WRITE( , ) " Enter t h e number o f i n t e r v a l s ( 0 t o q u i t ) "


CALL PrintOut ( )
READ( , ) n
IF ( n<=0 ) EXIT

37
38

t _ s t a r t = ETIME( t )

39
40
41

h = 1 . 0 d0/n
sum = 0 . 0 d0

42
43
44
45
46
47

! HPF$
INDEPENDENT, NEW( i , x ) , REDUCTION( sum )
DO i = 1 , n
x = h (DBLE( i ) 0 . 5 d0 )
sum = sum + 4 . d0 / ( 1 . d0 + xx )
END DO

52

Parallelization

53

48
49

p i = h sum

50
51
52

WRITE( , ) " Approximation f o r p i i s " , p i


WRITE( , ) " The e r r o r i s " , abs ( p i PI25DT )

53
54
55
56

t_end = ETIME( t )
WRITE( , ) " Elapsed time i s " , t_endt _ s t a r t
CALL PrintOut ( )

57
58

END DO

59
60

END PROGRAM pi_hpf

chapters/codes/parallel_pi.f

53

54

Chapter A: Codes

A.3 MPI
1
2
3

/ C Example /
#i n c l u d e <s t d i o . h>
#i n c l u d e <mpi . h>

4
5
6
7
8
9
10

i n t main ( argc , argv )


int argc ;
c h a r argv [ ] ;
{
i n t rank , s i z e ;

11
12
13
14
15
16
17
18

MPI_Init (& argc , &argv ) ; / s t a r t s MPI


MPI_Comm_rank (MPI_COMM_WORLD, &rank ) ;
MPI_Comm_size (MPI_COMM_WORLD, &s i z e ) ;
p r i n t f ( " H e l l o world from p r o c e s s %d o f
MPI_Finalize ( ) ;
return 0;

/
/ g e t c u r r e n t p r o c e s s i d /
/ g e t number o f p r o c e s s e s /
%d\n " , rank , s i z e ) ;

chapters/codes/mpi.cpp
1
2
3
4

c F o r t r a n example
program h e l l o
i n c l u d e mpif . h
i n t e g e r rank , s i z e , i e r r o r , tag , s t a t u s (MPI_STATUS_SIZE)

5
6
7
8
9
10
11

c a l l MPI_INIT( i e r r o r )
c a l l MPI_COMM_SIZE(MPI_COMM_WORLD, s i z e , i e r r o r )
c a l l MPI_COMM_RANK(MPI_COMM_WORLD, rank , i e r r o r )
p r i n t , node , rank , : H e l l o world
c a l l MPI_FINALIZE( i e r r o r )
end

chapters/codes/mpi.f

54

Linked Cells Method

55

A.4 Linked Cells Method


1

// Program t o go through a l l p a r t i c l e s i n c e l l i =2

2
3

[...]

4
5
6
7

i n d e x _ p a r t i c l e = FIRST [ i ] ;
// t h e i n d e x o f t h e f i r s t
// p a r t i c l e i n t h e i t h c e l l

8
9
10

N=0;
// c o u n t e r

11
12
13
14
15

i f ( i n d e x _ p a r t i c l e !=0) {
N=1;
// t h e r e s a t l e a s t 1 p a r t i c l e

16

j = First [ i ] ;
w h i l e ( j !=0) {

17
18
19

p r i n t f ( " p a r t i c l e nr %d \n " , LIST [ j ] ) ;


j = LIST [ j ] ;
// go through t h e p a r t i c l e
// i n t h e i t h c e l l

20
21
22
23
24
25
26
27

N++;

28
29

p r i n t f ( " T o t a l nr o f p a r t i c l e s i n c e l l %d : %d \n " , i ,N) ;

30
31

[...]

chapters/codes/linked_cells.cpp

55

S-ar putea să vă placă și