Module 9: Monte Carlo Part I: Fundamentals, and The Metropolis Algorithm
Module 9: Monte Carlo Part I: Fundamentals, and The Metropolis Algorithm
Arka Lahiri
Initial
Microstate
(t = 0)
Final
Microstate(t = tfin ) Trajectory
Predicting Macroscopic Properties: an MD simulation
An MD simulation tries to replicate the same trajectory of the
system by simulating the microstates at discrete time intervals
(shown by blue and green points). The time-steps
corresponding to the different microstates are mentioned
alongside the points. The MD simulation accesses 9 different
microstates, with one of them accessed twice (highlighted in
green).
(6∆t)
Initial Microstate
(7∆t) (5∆t)
(t = 0)
(2∆t), (8∆t)
(4∆t)
(∆t) (3∆t)
(9∆t)
Final Microstate
(t = tfin )
Predicting Macroscopic Properties: an MD simulation
The macroscopic pressure of the system can be given by:
R t=t fin
1
Pmacro = Pmicro (t) dt,
tfin t=0
which can also be equivalently written as,
1 P9
Pmacro = Pmicro (i∆t).
10 i=0
The integration can be replaced by a summation only because the
system is accessing a finite set of microstates during an MD
simulation.
(6∆t)
Initial Microstate
(7∆t) (5∆t)
(t = 0)
(2∆t), (8∆t)
(4∆t)
(∆t) (3∆t)
(9∆t)
Final Microstate
(t = tfin )
Predicting Macroscopic Properties: Monte-Carlo way
Monte-Carlo simulations compute Pmacro by artificially accessing the
same finite set of microstates as observed in an MD simulation and
averaging Pmicro over all those microstates. Although, we have labeled
the microstates in the following schematic in the same order as they
are accessed during an MD simulation, the trajectory of the system or
the order in which the microstates are accessed become completely
irrelevant during a Monte Carlo simulation. Instead, a Monte Carlo
simulation is just concerned with, (1). the list of microstates which the
system accessed, and, (2). how frequently they were accessed.
7
8 6
3
5
2 4 Microstate 3 is accessed twice in total,
1 9 firstly when going from microstate 2 to 4
and secondly while going from 8 to 9.
All the others microstates
are accessed just once.
Predicting Macroscopic Properties: Monte-Carlo way
Assuming for the time being that a Monte-Carlo simulation of the
same system would access the same set of microstates with the
same frequency as observed during an MD simulation, we can write,
P9
PMacro = α=1 ρα Pmicroα
(we have already seen this formula in
2
modules 7 and 8), where, ρ3 = and for all the other microstates
10
1
(α), ραα6=3 = . The value of 10 in the denominator of the definition of
10
ρ is the total number of microstates visited in this example case. As
we are averaging over the microstates in an Ensemble, Monte-Carlo
simulations are based on the concept of “Ensemble averages”.
The trajectory of the system is irrelevant
for Monte-Carlo simulations and hence
7 has been removed. Monte-Carlo deals
8 6 just with the microstates.
3 The same microstates can be accessed
5
2 4 in an order which is different
1 9 to that of Monte Carlo.
Basis of Monte Carlo: Ensemble average
We will assume that the atoms interact only with their first nearest
neighbours. Every atom has 4 nearest neighbours in this system.
There are three kinds of bonds in the system: green(1)-green(1),
green(1)-red(2), green(1)-blue(3). The relative bond energies are
assumed to be, φ12 < φ11 < φ13 . We have to remember that bond
energies are negative and a more negative bond energy implies a
stronger bond.
Thus, there is a much stronger bond between the solvent atom (type
1) and the red solute atom (type 2) compared to the bond between
two solvent atoms (type 1). The bond between a green atom (type 1)
and a blue atom (type 3) is the weakest. So, while a green atom
would like to stay next to a red atom, it hates to be next to a blue
atom.
The Metropolis Monte-Carlo algorithm: The system
3
2 4
1
In the next few slides, we will investigate how the energy of the
system changes due to a jump of either of the four nearest
neighbours into the vacancy.
The Metropolis Monte-Carlo Algorithm: Jump of atom
1
Let us consider the situation that atom 1 jumps into the vacancy and
the generates the following microstate (defined by the atomic
positions) henceforth indicated by “MS1” and let the corresponding
potential energy be U MS1 .
3 3
2 4 2 1 4
1
We can see that after the atom 1 has jumped into the vacancy, the
vacancy is now surrounded by 3 green atoms and 1 red atom. This is
different from the initial configuration as it was surrounded by 4 green
atoms. So, there must be a difference in potential energy between the
current and the previous microstates.
The Metropolis Monte-Carlo Algorithm: Jump of atom
1
Before proceeding further, it is important to compare the
potential energies of MS0 and MS1. In MS0, due to the
presence of the vacancy, 4 solvent(green)-solvent(green)
bonds are missing.
But in contrast, in MS1, only 3 green-green bonds are missing,
while, 1 solvent(green)-solute(red) bond is also missing.
So, the key difference between MS0 and MS1 is that 1 broken
green-green bond in MS0 is replaced by a broken green-red
bond in MS1.
As we have already discussed that dissolution of a green-red
bond would increase the potential energy of the system the
most, it is correct to write,
U MS1 > U MS0 , which indicates that MS1 is further away from
equilibrium (energy minimum) compared to MS0.
The Metropolis Monte-Carlo algorithm: Jump of atom
2
Now, let us consider the situation that instead of atom 1, atom 2
jumps into the vacancy and the generates the following
microstate henceforth indicated by “MS2” with its energy U MS2 .
3 3
2 4 2 4
1 1
We can see that the broken bonds around a vacancy after the
jump of the second atom is exactly the same as that found after
the jump of the 1st atom as in both cases the vacancy is
surrounded by three green atoms and one red atom. Let us
denote this microstate by “MS2” and note that its potential
energy U MS2 must be equal to U MS1 , i.e. UMS1 = UMS2 > U MS0 .
Example system: Jump of atom 3
Now, let us consider the situation that atom 3 jumps into the
vacancy and the generates the following microstate henceforth
indicated by “MS3”.
3
2 4 2 3 4
1 1
3 3
2 4 2 4
1 1
3
2 4
1
Not
Allowed Allowed
0 Pacc 1
= 0.4
Any randomly selected number can either be smaller than Pacc
or larger than it.
The Metropolis Monte-Carlo Algorithm: Step 3
The probability(PR ) that any randomly selected number(R)
would be smaller than Pacc can be given by the ratio of the
length of the number line till Pacc (denoted by L1 ) and that of
the entire length of the number line (denoted by L),
L1 Pacc
PR = = = Pacc .
L 1
So, the choice of a random number accurately replicates the
probability of success of the atomic jump.
L
L1
0 Pacc 1
= 0.4
We will discuss this step in more detail a bit later. We will also
refer to this step as the “Decision” step in future discussions.
The Metropolis Monte-Carlo Algorithm: Iteration
Steps 1, 2 and 3 define an iteration of the Monte-Carlo algorithm
which is called a “trial”. Let us summarize what happened in each of
those steps:
So, the end of the 3 steps (the 1st iteration), the system can either
take up a new microstate or remain at its initial microstate.
The above trial is specific to the case where the jump of an atom
increased the energy of the system. How would the algorithm tackle
an atomic jump which reduces the energy ?
The Metropolis algorithm: Favourable jump in Step 1
3 3
2 4 2 4
1 1
The Metropolis algorithm: Jump of atom 4
We can express the relative probabilities of the system being in
MS4 versus the initial microstate (MS0) as,
ρMS4 (U MS4 − U MS0 )
∆U
= exp − = exp − > 1,
ρMS0 kB T kB T
where the last inequality follows from the fact that,
U MS4 < U MS0 .
As a result, we can write,
ρMS4 > ρMS0 ,
which implies that the system should be found more frequently
in MS4 compared to MS0. Thus, any atomic jump which takes
the system from MS0 to MS4 has to be immediately allowed.
So, we accept the jump of atom 4 without any further
conditions.
We are now in a position to describe the full “Metropolis Monte
Carlo” algorithm.
The Metropolis algorithm: Steps
1. Given the current microstate (MS0) of the system, randomly select one
out of all the possible mircostates the system can evolve to. The
selected microstate will be called MS#. We will now examine whether
the move to MS# will be allowed or not.
2. Determine the potential energies (U) of the system at the microstates
MS0 and MS#. Based on the values for U MS0 and U MS# determine the
relative probabilities of the microstates MS0 and MS# denoted by
Pacc = ρMS# /ρMS0 .
3. If, Pacc ≥ 1, accept the move and update the configuration of the
system. If not, then randomly select another number (R). If R < Pacc ,
accept the move and update the configuration of the system. If
R > Pacc , then reject the move and keep the system in the current
microstate.
4. If the atomic jump was accepted, then update the current microstate to
MS#, i.e. MS0 ≡ MS#.
The above 4 steps constitute a “trial” of the Metropolis Monte Carlo algorithm,
where the system “tries to access” the other microstates available to the
system.
Repeat steps 1,2, and 3 for accessing the other available microstates and the
average the microscopic property over the microstates accessed during the
course of the simulation.
The Metropolis algorithm: Flowchart
Initial microstate MS0
Determine Pacc
Is No Choose randomly
Repeat Pacc ≥ 1
(R) between 0 and 1
?
Yes
Is
Yes No Reject
Update MS0 = MS# R < Pacc
?
The Metropolis Algorithm: Key observations
Suppose, the Monte-Carlo simulation using the Metropolis algorithm
is run for 10 trials. The system accesses the following sequence of
the microstates (refer to the schematic): {1, 2, 3, 3, 4, 5, 6, 7, 8, 9}.
It is apparent that the system does not move out of the microstate 3
immediately and it stays there for 1 more trial. This happens if the
randomly selected atomic jump was rejected when the system was in
microstate 3.
What does this imply ? Check next slide !
7
8 6
3
5
2 4
1 9
The Metropolis Algorithm: Key observations
∆U
As Pacc = exp − , associated with an energetically unfavourable jump
kB T
becomes much smaller than 1, the probability of that jump being accepted
becomes quite small. From the expression of Pacc , it is evident that this can
happen when ∆U > 0, which indicates that the microstate attempted to
reach through the jump of the atom has a higher potential energy
(unfavourable) than the current microstate.
In the context of the schematic presented in the previous slide, it can be
deduced that the atomic arrangement (the atoms which are neighbouring to
the vacancy) in microstate 3 is a low energy configuration and can also be a
local minimum. That would explain why the system finds it difficult to move
out of that microstate.
It is critical to evaluate rejected atomic jumps as system’s preference for
staying in the current microstate and hence the contribution of microstate 3 is
counted twice while evaluating the macroscopic potential energy Umacro
(check the list of the microstates in the previous slide).
Atomic jumps can be rejected more than once and consequently the same
microstate may persist across several trials. We should consider all such
repetitions while calculating the ensemble average.
The Metropolis Algorithm: Key observations
It can so happen that the sequence of microstates accessed by the
Monte Carlo simulation is: {1, 2, 3, 4, 5, 6, 7, 8, 3, 9}. The microstate 3
is again accessed twice but not consecutively. It again shows that the
system prefers to be in microstate 3 and hence accesses it
repeatedly. Conceptually, there is nothing different in this situation
from the case where the microstate 3 is accessed consecutively.
So, the general expression for an ensemble average of a quantity X
from a Monte-Carlo simulation, can be given as,
PM
X macro = α=1 ρα X α ,
where, M denotes the total number of different microstates accessed
in the Monte Carlo simulation, ρα is the probability of being in a
particular microstate (α).
Nα
The probability of being in a microstate α, ρα = , where N α
Ntrials
denotes the number of times a particular microstate (α) is attained
PM
during the Monte Carlo simulation and Ntrials = α=1 N α denotes the
total number of trials during the simulation.
The Metropolis algorithm: Key observations
7
According to the
8 6 expression presented
3 macro
PM
= α=1 ρα U α .
5 in the previous slide: U
2 4
1 9 Counting the number of times
each microstate was visited,
1 α=3 2
ρα,α6=3 = ,ρ = .
10 10
macro
The final expression for U is,
1 P9 α 2 3
Umacro = U + U
10 α=1,α6=3 10
The above expression immediately establishes Monte-Carlo
simulation as a discrete version of the Ensemble average.
Thus, the same Ensemble average can be computed without
performing multi-dimensional integrations but by carrying out a
Monte-Carlo simulation instead.
But a Monte Carlo simulation can effectively substitute a theoretical
calculation of the Ensemble average only when the values of ρα
obtained from such a simulation is the same as that obtained from the
theory of Statistical Mechanics.
The Metropolis algorithm: Dealing with Randomness
How does a Monte Carlo simulation ensure that the microstates are
accessed with the same probabilities as suggested by Statistical
Mechanics ?
The Metropolis algorithm directly employs the expression of ρα from
Statistical Mechanics when evaluating the relative probabilities of
microstates in Step 2 of the algorithm,
Uα
1
ρα = exp − ,
Q kB T
where α denotes a microstate. So, everything looks fine here!
But in Step 1, the algorithm involves a random selection of the
microstate which the system can evolve to. So, does the
randomess/indeterminacy in Step 1 influence the value of ρα that
emerges out of the algorithm ?
Also, in Step 3, when Pacc < 1, the atomic jump is allowed only when
a randomly chosen number (R) is lower than Pacc . Does the
randomness involved in the decision to allow the atomic jump or not
influence the probabilities (ρα ) with which the microstates are
accessed during the simulation ?
The Metropolis algorithm: Dealing with Randomness
0 1
In spite of being randomly chosen, it is possible to observe some
clustering of the random numbers at certain points on the number
line. Such clusters will disappear if we plot more randomly chosen
numbers on the number line and their distribution will appear more
and more uniform.
The Metropolis Monte-Carlo Algorithm: Understanding
Step 3
Assume that a sequence of microstates accessed by a Monte Carlo
simulation is {1, 2, 3, 2, 4, 5, 3, 3, 6}, where our interest is the transformation
from microstate 3 to 2. Let Pacc = 0.3 for this transformation and let us
attempt to understand it in the context of the number line.
We will first consider the 3 → 2 transformation (highlighted in blue in the first
paragraph). In order to decide whether to allow this transformation, a random
number (R1 ) was drawn and found to be equal to 0.23 and so the jump was
accepted. Both the random number R1 and Pacc are demonstrated in the
number line.
R1
0 Pacc 1
= 0.3
As is the reality during a Monte-Carlo simulation, the transition between the
same two microstates is attempted again in a later trial (between two
repetitions of microstates 3, highlighted in red), but this time the random
number drawn (R2 ) is 0.76 (shown below) and so the jump is denied.
R1 R2
0 Pacc = 0.3 1
The Metropolis Monte-Carlo Algorithm: Understanding
Step 3
So, out of the two attempts to move from microstate 3 to microstate 2, one is
successful and the other is a failure. Thus, after 2 attempts, the Monte Carlo
simulation demonstrates a probability of transformation from microstate 3 to 2
to be 0.5. But this in huge error from the theoretical value of Pacc = 0.3. It
must be appreciated that such a difference is a result of there being just two
trials which want to take the system from microstate 3 to microstate 2. If the
simulation is run longer then there would be more instances where the
system tries to move from microstate 3 to microstate 2 and in that case the
probability of transition from microstate 3 to 2 from the simulation will
replicate Pacc very closely. This can be clarified from the schematic below
which depicts all the random numbers generated whenever a transformation
from microstate 3 to microstate 2 was attempted during the course of a
simulation. It can be confirmed from the figure that about 30% of the random
numbers fall below 0.3. It must be noted that as Pacc is a ratio of ρ for two
different microstates, realizing an accurate value of Pacc during simulations is
similar to realizing the accurate values of ρα .
0 Pacc 1
= 0.3
The Metropolis Monte-Carlo Algorithm: Dealing with
Randomness
From the discussion in the earlier slides, it is clear that we have
to take the following precautions to ensure that the simulation is
an accurate representation of reality.
1. Ensure that all the possible microstates that the system
can transition to have equal probability of getting selected
at Step 1.
2. Run a longer simulation to ensure that transitions between
microstates accurately replicate the theoretical value of
Pacc . The length of the simulation is usually determined by
checking whether the values of several quantities have
saturated or not, as done for a Molecular dynamics
simulation (we will take up this point in the next part).
The above steps will ensure that the value of ρα emerging out
of a simulation is very close to that predicted by Statistical
Mechanics.
Key benefits of a Monte-Carlo simulation over a
theoretically calculated Ensemble average
The primary benefit of a Monte Carlo simulation over a
theoretical calculation of the Ensemble average is that a Monte
Carlo simulation doesn’t require the evaluation of
multi-dimensional integrals.
As a Monte Carlo simulation is concerned with a transition
between microstates, it just deals with the ratio of the
probabilities of being in those microstates. Consider the ratio of
probabilities of being in microstate α and β,
,
Uα
Uα
exp − Q exp −
ρα kB T kB T
= = ,
ρβ
,
Uβ
β
U exp −
exp − Q kB T
kB T