You are on page 1of 43

Lecture 4

Molecular Monte Carlo Simulation

고려대학교
화공생명공학과
강정원
Contents

I. Theoretical Basis of Molecular Monte Carlo Method


II. Implementation of Molecular Monte Carlo Method in
NVT Ensemble
III. Implementation of Molecular Monte Carlo Method in
Other Ensembles
I. Theoretical Basis of Molecular Monte Carlo Method

I-1. Introduction

◼ What is Monte Carlo Method ?


 The term “Monte Carlo Method” is used to embrace a wide range
of problem solving techniques which uses random numbers and
statics of probability.
 Not surprisingly, the term was coined after the casino in
principality of Monte Carlo. Every game in a casino is a game of
change relying on random events such as ball falling into a
particular slots on a roulette wheel, being dealt useful cards from a
randomly shuffled deck, or dice that falling the right way.

 In principle, any methods that uses random number to examine


some problem is a Monte Carlo method.
I. Theoretical Basis of Molecular Monte Carlo Method

I-1. Introduction

◼ Example of MC in Science
 Classical Monte Carlo (CMC)
• Draw samples from a probability distribution to determine such thing
as energy minimum structure
 Quantum Monte Carlo (QMC)
• Random walks are used to determine such things as quantum-
mechanical energy
 Path-integral Monte Carlo (PMC)
• Quantum-statistical mechanical integral are evaluated to obtain
thermodynamic properties
 Simulation Monte Carlo (SMC)
• Algorithm that evolve configurations depending on acceptance rules.
I-1. Introduction

◼ A classical example – The calculation of p

 Shots are generated in OABC square


 Two independent random numbers are generated (0-1)
 These random numbers are x and y coordinates of the shot
 N: Number of shots fired
 H: Number of hits within the circle
I-1. Introduction

◼ C-language code
I-1. Introduction

◼ Another Example – Radioactive decay


 The kinetics of radioactive decay are first-order, which means that
decay constant (l) is independent of the amount of material (N).

 The decay of radio active material, although constant with time is


purely a random event. As such it can be simulated using random
numbers.
I-1. Introduction

◼ C-Language code example


I. Theoretical Basis of Molecular Monte Carlo Method

I-1. Introduction

◼ Overview of Molecular Monte Carlo Method


Generation of Random
Objective Configurations

Calculation of Macroscopic Use of Random Number


Properties from Microscopic Importance Sampling
Properties (intermolecular Markov Chain
forces…) Metropolis Algorithm

Averaging Method Approximations


Ensemble Averages
Periodic Boundary Condition
Minimum Image Convention
NVT Ensemble
Long Range Correction
NPT Ensemble
Neighborhood List
mPT Ensemble
I. Theoretical Basis of Molecular Monte Carlo Method

I-2. Averaging Method

◼ Statistical Mechanics : Theoretical Basis for derivation of


macroscopic behavior from microscopic properties
◼ Configuration : position and momenta (rN and pN)
◼ Configurational Variable : A(rN , pN)
◼ Ensemble average
 Weighted sum over all possible members of ensemble
 Using classical mechanics

 A =
1 1
3N
Q h N!  dp N
 dr N
A( p N
, r N
) exp 
− E ( r N
, p N

)

Q=
1
3N
h N!  
dp N
dr N
exp − E( p N
, r N
) 
I. Theoretical Basis of Molecular Monte Carlo Method

I-2. Averaging Method

◼ Separation of Energy
 Total energy is sum of kinetic and potential parts
E(r N , p N ) = K ( p N ) + U (r N )

 Kinetic parts can be treated separately from potential parts


K ( pN N ) =  pi2 / 22mi
i  pi / 2mi ) dr exp− U ( r )
1
Q=  − 
N N
dp exp(
h3 N N! i

1 1
= 3N
 N!  dr N
exp − U( r N
) 
ZN
1 1
= 3N
 ZN
I. Theoretical Basis of Molecular Monte Carlo Method

I-2. Averaging Method

◼ Ensemble Average of a property



N N N
−  N

 
1 dr A( r , p ) exp U ( r )
A =  −  ) =
N N N N

 
dr A( r , p ) exp U ( r
 − 
N N
Z N N! dr exp U ( r )

 Monte Carlo Simulation calculates excess thermodynamic


properties that result in deviation from ideal gas behavior

◼ Metropolis Monte Carlo Method


A =

exp − U (r N )  =  = A
N N N N N
N
A( r ) dr N ( r ) A( r ) dr trials ,N ( r N )
Z N (r )
probability of a given configuration rN
I. Theoretical Basis of Molecular Monte Carlo Method

I-3. General MMMC Scheme

Start

Generate initial configuration Random Number Generator (2.1)

Calculate Energy (2.2) PBC and MIC (2.3)

Loop
Ncycles Trial Move Importance Sampling
Acceptance Criteria (2.5) (Metropolis Algorithm)

Calculate Summation of Properties

Average Properties

End
II. Implementation of MMC in NVT Ensemble

II-1. Random Number Generation

◼ There is nothing like “Random number generator “


 “Pseudo Random Number Generator “
 Most of the pseudo random number generator repeats “sequence”
 It is important to know how long is the sequence

◼ Most FORTRAN, C compiler supplies random number


generator based on Linear Congruental Method
 The relationship will repeat when n greater than 32767

I n +1 = (aI n + c) mod(m) Ex) Digital FORTRAN


a, c  0, m  l0 , a, c RANDOM_NUMBER subroutine
Period = 10**18
II. Implementation of MMC in NVT Ensemble

II-1. Random Number Generation

◼ RANDU Algorithm
 1960’s , IBM

I n+1 = (65539 I n ) mod(231 )


 This generator was found to have serious problem : “The
Marsaglia Effect”

◼ Improving the behavior of random number generator


 Two initial seed can extend the period grater than m

I n = (a  I n−1 + b  I n−2 ) mod(m)


II. Implementation of MMC in NVT Ensemble

II-1. Random Number Generation

◼ Using Random number generators


 Check the period
 Serial Test : (x,y) or (x,y,z)
 Be careful about dummy argument
• Use different dummy argument for two different set of random
numbers
• Compiler’s optimizer is trying to remove multiple calls to random
number generator
DO 1 I = 1,100
X = RAND(IDUM) + RAND(IDUM) X = RAND(IDUM)
1 CONTINUE

You have to change


dummy argument
X = 2.0 * RAND(IDUM) each calls
Not evaluating every steps
Evaluated only once
II. Implementation of MMC in NVT Ensemble

II-1. Random Number Generation

◼ Initializing Configuration (Example : 2D Space)


3
bx = rLx/float(nx)
2
by = rLy/float(ny)
do 60 ip= 1, np 1

x(ip) = ( 0.5*(1.-float(nx)) +

Y
0

float(mod( ip-1, nx )) )*bx -1

y(ip) = ( 0.5*(1.-float(ny)) + -2

float((ip-1)/nx) )*by -3
-3 -2 -1 0 1 2 3
60 continue X

bx = rLx/float(nx) 3

by = rLy/float(ny) 2

do 60 ip= 1, np 1

x(ip) = ( 0.5*(1.-float(nx)) +
Y

float(mod( ip-1, nx ))+0.1*r2() )*bx -1

y(ip) = ( 0.5*(1.-float(ny)) + -2

float((ip-1)/nx) +0.1*r2())*by -3

60 continue -3 -2 -1 0

X
1 2 3
II. Implementation of MMC in NVT Ensemble

II-2. Calculating Potential Energy

◼ Potential Energy of N-interacting particles

U =  u1 (ri ) +  u2 (ri , rj ) +   u (r , r , r ) + ...


3 i j k
i i j i i j i k  j i

Three-body interaction
Two-body interaction

Effect of external field


II. Implementation of MMC in NVT Ensemble

II-2. Calculating Potential Energy

◼ Typically, effect of external field is zero


◼ Two body interaction is the most important term in the
calculations
 Truncated after second term
◼ For some cases, three body interactions may be important
◼ Including three body interactions imposes a very large
increase in computation time t  N m
◼ Short range and long range interactions
 Short range : Dispersion and Repulsion
 Long range : Ionic interaction
• Special methods are required for long range interactions due to
limited size of simulation box
II. Implementation of MMC in NVT Ensemble

II-2. Calculating Potential Energy

◼ Naïve calculation Summations are chosen to avoid duplicated evaluation and


“self” interaction
N −1 N
U =   u2 (rij )
i =1 j =i +1
  12   6 
U = 4   −   
 r   r  

Pseudo Code FORTRAN Code

DO 10 I = 1, N
Loop i = 1, N-1 DO 20 J = I+1, N
Loop j = i+1,N DX = X(I)-X(J)
Evaluate rij DY = Y(I)-Y(J)
Evaulate Uij RIJ2 = DX*DX + DY*DY
Accumulate Energy RIJ6 = RIJ2*RIJ2*RIJ2
End j Loop RIJ12 = RIJ6*RIJ6
UTOT = UTOT + 1/RIJ12 – 1/RIJ6
End j Loop 20 CONTINUE
10 CONTINUE
II. Implementation of MMC in NVT Ensemble

II-3. Periodic Boundary Condition and


Minimum Image Convention

◼ Problems
 Simulations are performed typically with a few hundred molecules
arranged in a cubic lattice → computational limitation
• Large fraction of the molecules can be expected in a surface rather
than in the bulk
 Simulation require summation over almost infinite interactions
 PBC (Periodic Boundary Condition) and MIC (Minimum Image
Convention) are used to avoid this problems
II. Implementation of MMC in NVT Ensemble

II-3. Periodic Boundary Condition and


Minimum Image Convention

◼ Periodic Boundary Condition


 Infinite replica of the simulation box
 Molecules on any lattice have mirror
image counter parts in all the other
boxes
 Changes in one box are matched
exactly in the other box → surface
effects are eliminated
II. Implementation of MMC in NVT Ensemble

II-3. Periodic Boundary Condition and


Minimum Image Convention

◼ Minimum Image Convention

 Summation over infinite array of


periodic image → impossible
 For a given molecule, we position
the molecule at the center of a
box with dimension identical to
simulation box
 Assume that the central
molecules only interacts with all
molecules whose center fall
Nearest images of colored sphere
within this region
II. Implementation of MMC in NVT Ensemble

II-3. Periodic Boundary Condition and


Minimum Image Convention

◼ Implementing PBC
 Decision based : IF statement
 Function based : rounding , truncation, modulus

Decision Function

BOXL2 = BOXL/2.0
IF(RX(I).GT.BOXL2) RX(I)=RX(I)-BOXL RX(I) = RX(I) – BOXL * AINT(RX(I)/BOXL)
IF(RX(I).LT.-BOXL2) RX(I) = RX(I) + BOXL
II. Implementation of MMC in NVT Ensemble

II-3. Periodic Boundary Condition and


Minimum Image Convention

◼ Implementing PBC and MIC


Pseudo CODE FORTRAN CODE

Loop i = 1, N -1 do 10 ip= 1, Np-1


Loop j = I + 1, N xip = x(ip)
Evaluate rij yip = y(ip)
Convert rij to its periodic image (rij’) do 20 jp= ip+1, Np
xx = xip - x(jp)
if (rij’ < cutOffDistance) dx = dble( xx - rLx*anint( xx/rLx ) )
Evaluate U(rij) yy = yip - y(jp)
Accumulate Energy dy = dble( yy - rLy*anint( yy/rLy ) )
End if rij2 = dx*dx + dy*dy
End j Loop if ( rij2 .lt. dcut2 ) then
End i Loop rij6 = 1.d0/(rij2*rij2*rij2)
rij12 = rij6*rij6
ham = ham + rij12 - rij6
pre = pre + 2.d0*rij12 - rij6
endif
20 continue
10 continue
II. Implementation of MMC in NVT Ensemble

II-3. Periodic Boundary Condition and


Minimum Image Convention

◼ Features due to PBC and MIC


 Accumulated energies are calculated for the periodic separation distance
 Only molecules within cut-off distance contributed to calculated energy
 Caution : Cut-off distance should be smaller than the size of simulation box →
if not, violation to MIC
 Calculated potential = truncated potential
◼ Long range correction

X full = X c + X lrc

Elrc = 2pN  r 2u (r )dr
rc

 For NVT ensemble No. of particle and density (V) are const.
• LRC can be added after simulation
 For other ensembles, LRC must be added during the simulation
II. Implementation of MMC in NVT Ensemble

II-4. Neighborhood List

◼ In 1967, Verlet proposed a new algorithm to reduce


computation time
◼ Instead of searching for neighboring molecules, the neighbors
of the molecules are stored and used for the calculation
◼ Variable d is used to encompass slightly outside the cut-off
distance
◼ Update of the list
 update of the list / 10-20 steps
 Largest displacement exceed d value
II. Implementation of MMC in NVT Ensemble

II-5. Metropolis Sampling Algorithm

◼ Average of a property

A =

exp − U (r N ) 
A(r N )dr N =  N (r N ) A(r N )dr N = A
N trials ,N ( r N )
Z N (r )

◼ There are a lot of choice to make Markov process that


follows a given PDF
“Microscopic reversibiltiy”
Sufficient, but not necessary condition
ρπ = ρ  pm mn = n
m mp mn = np nm
pn
mn =1

This condition is not sufficient :


more conditions are required to setup transition matrix
II. Implementation of MMC in NVT Ensemble

II-5. Metropolis Sampling Algorithm

◼ In 1953, Metropolis showed a transition probability matrix


exists than ensures that the PDF is obeyed
p mn =  mn ρn  ρm , m  n
 n 
p mn =  mn   ρn  ρm , m  n
 m 
p mm = 1 − p mn
nm

◼ Other choice can also satisfies condition of microscopic


reversibility (Barker , 1965)
 n 
p mn =  mn   mn
 n + m 
p mm = 1 − p mn
nm
II. Implementation of MMC in NVT Ensemble

II-5. Metropolis Sampling Algorithm

◼ Metropolis Recipe
 with probability pmn a trial state j for the move
 if n > m accept n as new state
 Otherwise, accept n as the new state with probability n > m
• Generate a random number R on (0,1) and accept if R < n /m
 If not accepting n as new state, take the present state as the next
one in Markov chain (pmn ≠ 0)

◼ What is the value of  ?


 mn = 1/N R if accepting
 mn = 0 if not accepting
N R : Number of accepted trials
II. Implementation of MMC in NVT Ensemble

II-6. Implementation
Start

Generate initial configuration

Generate trial displacement rTrial = PBC(r + (2*rand()-1)*dMax)


Loop Ncycle
Calculate the change in energy
DE = E trial - Eatom

Loop Natoms Apply Metropolis Algorithm r=rnew


if DE <0 or exp(-DE) >= rand() then accept move E = E+DE
else reject move nAccept = nAccept

Update periodically maximum displacement, dMax if (acceptRatio > 0.5)


dMax = 1.05*dMax
else
dMax = 0.95*dMax
Calculate Properties
Calculate Error Estimates

End
II. Implementation of MMC in NVT Ensemble

II-6. Implementation

Monte Carlo Move


Entire Simulation
New configuration
Initialization

Reset block sums Select type of trial move


each type of move has fixed
“cycle” or probability of being selected
“sweep” New configuration
moves per cycle “block”
Move each atom
Add to block sum 100’s or 1000’s
once (on average) Perform selected trial
cycles per block of cycles
move
Compute block average Independent
blocks per simulation
“measurement”

Compute final results


Decide to accept trial
configuration, or keep
original
II. Implementation of MMC in NVT Ensemble

II-7. Averages and Error Estimates

◼ Equilibration period
 The averages are evaluated after “equilibration period”
 The equilibration period must be tested : cycle vs. properties
• Ex) 20 000 run :
– 1 to 10 000 : equilibration period
– 10 001 to 20 000 : averages are accumulated

◼ Error estimation
 Error estimation based on different simulation runs (with different
initial configurations)
 Error estimation dividing total simulation runs into several blocks
→common method
II. Implementation of MMC in NVT Ensemble

II-7. Averages and Error Estimates

◼ Average and Error Estimates


1
A run
=  Ai
N − N equil − 1 i  N equil
1 Nb
A b =  Ai
l i =1

( A )
nb
1
= − A
2
b run
Nb b =1
III. Implementation of MMC in other Ensembles

III-1. Introduction

◼ MMC in different ensembles


 A very large number of systems for convenient calculation of time
average macroscopic properties
 Common macroscopic attributes
• (N, V, E) : Microcanonical ensemble
• (N, V, T) : Canonical ensemble No change in N , Closed system

• (N, P, T) : NPT ensemble


• (m, V, T) : Grand canonical ensemble Change in N , Open system
 Microcanonical ensemble cannot be used in MMC because
constant-kinetic energy constraint cannot be assumed
 In thermodynamic limit all ensembles are equivalent and it is also
possible to transform between ensembles. The choice of ensemble
is completely a matter of convenience (which property ?)
III. Implementation of MMC in other Ensembles

III-1. Introduction

◼ Commonly encountered ensemble


Name All states of: Probability distribution Schematic
Microcanonical given EVN p i = 1
(EVN)
Canonical all energies p ( Ei ) = Q1 e −  Ei
(TVN)
Isothermal-isobaric all energies and p ( Ei ,Vi ) = D1 e −  ( Ei + PVi )
(TPN) volumes
Grand-canonical all energies and p ( Ei , Ni ) = 1 e −  ( Ei + m Ni )
(TVm) molecule numbers
III. Implementation of MMC in other Ensembles

III-1. Introduction

◼ Partition functions and bridge equation

Ensemble Thermodynamic Partition Function Bridge Equation


Potential
Microcanonical Entropy, S  = 1 S / k = ln ( E ,V , N )

Canonical Helmholtz, A Q =  e−  Ei −  A = ln Q(T ,V , N )

Isothermal-isobaric Gibbs, G D =  e−  ( Ei + PVi ) −  G = ln D(T , P, N )

Grand-canonical Hill, L = –PV  =  e−  ( Ei + m Ni )  PV = ln (T ,V , m )


III. Implementation of MMC in other Ensembles

III-2. NVT Ensemble

◼ Ensemble average : Boltzmann distribution as weighting factor


exp − U (r N )   A(i) exp(E (i))
A = A( r N
) dr N
= i
N
Z N (r )  exp(E (i))
i

◼ Weighted average
W (i) = exp(− E (i))
1
A = 
N i
A(i)

◼ For Other ensembles ? → Tricky technique used : “Pseudo Boltzmann Factor”


W (i) = exp(− Y )

for NVT ensemble , Y = E (i )


III. Implementation of MMC in other Ensembles

III-3. NPT Ensemble

◼ Thermodynamic properties




exp(− pV )dV  A(r N ,V ) exp − U (r N ) dr N 
A = 0 V


0

 
exp(− pV )dV  exp − U (r N ) dr N
V

◼ V can change
 Particles are confined in fluctuating length L
 Scaled coordinate :
αi = ri / L

 integration over total volume → integration over unit cube 




exp(− pV )dV  A((L ) N ,V ) exp − U ((L ) N ,V ) d N 
A = 0 


0

exp(− pV )dV  exp − U ((L ) N ,V ) d N


III. Implementation of MMC in other Ensembles

III-3. NPT Ensemble

◼ Pesude Boltzmann Factor

Y = pV + E((L ) N , L) − NkT ln V
◼ Simple modification of NVT ensemble →Use DY instead of DU
◼ Volume fluctuation
 NVT ensemble
• Move 1 molecule at a time
• Calculate energies of remaining N-1 molecules
 NPT ensemble
• Change in V affects the coordinates of all atoms
• N*N calculations are required
• Effective strategy : “Scaling Method”
E = E (12) + E (6)
12 6
     
E = 4   ij  −4   ij 
 L   L  L 
12
L 
6
 ij   ij  Etrial = E (12) trial  + E (6) trial 
 L   L 
III. Implementation of MMC in other Ensembles

III-3. NPT Ensemble

◼ “Scaling method” only applicable to relatively simple


model potential → Scalable potential
 if not, N*(N-1) calculations are required for each volume
fluctuation
◼ Trial moves : Displacement and volume fluctuation
 Displacement on each atoms
 Volume change
◼ When V change is attempted, long range correction must
be re-evaluated
III. Implementation of MMC in other Ensembles

III-3. mVT Ensemble

◼ Property average
−3 N
 


n −0 N !
exp(Nm )  A(r N ) exp − U (r N ) dr N
V
A =
Z mVT

mideal = log( N / V ) + 3log 

 

 exp( N m *
− ln N ! )  A
V
( r N
) exp − U ( r N
) dr N

A = n −0
Z mVT

m * = m ex + kT ln N
III. Implementation of MMC in other Ensembles

III-3. mVT Ensemble

◼ The Pseudo Boltzmann Factor

Y = −Nm * + kT ln N!+E(r N )

◼ Attempted Trial Moves


 Particle displacement
 Particle insertion
 Particle deletion

You might also like