Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of...

93
ChE/MSE 557 Lecture 4 Fall 2006 1 Computational Nanoscience of Soft Materials [email protected] Molecular Dynamics Method, continued Lecture 4 •Calculating observables •Discussion of error •Thermodynamic ensembles •Tricks of the trade •Parallel MD

Transcript of Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of...

Page 1: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 1Computational Nanoscience of Soft [email protected]

Molecular Dynamics Method,continued

Lecture 4

•Calculating observables•Discussion of error•Thermodynamic ensembles•Tricks of the trade•Parallel MD

Page 2: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 2Computational Nanoscience of Soft [email protected]

Calculating properties inMD simulations

Recommended reading:Leach Chapter 6-7

and/orFrenkel & Smit Chapter 4

Page 3: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 3Computational Nanoscience of Soft [email protected]

Measuring quantities in molecular simulations

•Suppose we wish to measure in an experiment the value ofa property, A, of a system such as pressure or density orheat capacity.

•The property A will depend on the positions and momentaof the N atoms or molecules that comprise the system. Theinstantaneous value of A is:

A(pN(t),rN(t)) = A(p1x,p1y,p1z, …, x1,y1,z1,…t).

•Over time, A fluctuates due to interactions between theatoms.

Page 4: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 4Computational Nanoscience of Soft [email protected]

Measuring quantities in molecular simulations

•In any experiment, the value that we measure is an averageof A over the time of the measurement (a time average).

•As the time, t, of the measurement increases to infinity,the “true” average value of A is attained:

!

A = lim"#$

1

"A p

N(t),r

N(t)( )dt

t= 0

"

%

Note that strictly speaking this is true only for ergodic systems in equilibrium.

Page 5: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 5Computational Nanoscience of Soft [email protected]

Measuring quantities in molecular simulations

•To calculate properties in a molecular simulation, weaverage a property A over successive configurations in thetrajectory generated by the equations of motion.

•The larger nsteps is, the more accurate the estimate of thetrue time-averaged value of A.

!

A = limnsteps "#

1

nsteps

A pN(t),r

N(t)( )

t= 0

nsteps

$

Page 6: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 6Computational Nanoscience of Soft [email protected]

Ensemble averages

• In statistical mechanics we learn thatexperiments and simulations may be performedin different ensembles.

• An ensemble is a collection of individual systemsthat can be studied en masse to yieldmacroscopic properties of the entire collection.

• Every equilibrium configuration we generate viaMD represents a unique microstate of theensemble.

Page 7: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 7Computational Nanoscience of Soft [email protected]

Ensemble averages

• There are many different thermodynamicensembles, each of which is characterized bythermodynamic variables that are fixed in thesimulation.– NVE - microcanonical– NVT - canonical– NPT - isobaric-isothermal– µVT - Gibbs

N = number of particles, V = volume, E = total energy, T =temperature, P = pressure, µ = chemical potential

Page 8: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 8Computational Nanoscience of Soft [email protected]

Measuring quantities in molecular simulations

•The ergodic hypothesis of statistical mechanics states thatthe time-averaged value A of a property A in an equilibriumsystem is equal to the ensemble-averaged value <A>.

•The quantity P is the probability density of the ensemble:the probability density of finding a configuration withmomenta pN and positions rN.

!

A = dpNdr

NA p

N,r

N( )P(pN ,rN )""

Page 9: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 9Computational Nanoscience of Soft [email protected]

Measuring quantities in molecular simulations

•In a simulation, the ensemble average of A is obtained from:

where the sum is performed over M independentconfigurations (or microstates).•The larger M is, the more accurate the estimate of the trueexpectation value of A.

•Note: often, angle brackets (<…>) are used to indicate atime average as well, invoking the ergodic hypothesis.

!

A = limM "#

1

MAmpN,r

N( )m=1

M

$

Page 10: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 10Computational Nanoscience of Soft [email protected]

Measuring quantities in molecular simulations

In equilibrium, if system is ergodic:

!

A = A

Combine to achievethe best estimate of a property A.

t = 0

1

2

3

evolve in timeA1

A2

A3

<A>

A

A

A

t → ∞

The ergodic hypothesisis used regularly in materials simulation.

<A>

Page 11: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 11Computational Nanoscience of Soft [email protected]

Estimating errors in equilibrium properties

• Two kinds of errors possible:– Statistical - There are always statistical errors, even when all

sources of systematic error have been eliminated.• Often reported as “standard deviation”• The error (sd) σ<A>in the average value of a property A,

obtained from M data values is:

• Thus the error is inversely proportional to the number ofindependent (uncorrelated) data values averaged over, soa longer simulation gives a more accurate estimate of thetrue average of A.

!

"A

=

A i( ) # A( )2

i=1

M

$

M

Applies toindependentsamples!

Page 12: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 12Computational Nanoscience of Soft [email protected]

Estimating errors in equilibrium properties

• Statistical errors:– Note that this expression applies to independent samples.

So, if time averaging, M is not just the number of steps in thesimulation, since there is a high degree of correlationbetween successive steps.

– To know the number of independent samples contained in Mdata values, we must know the correlation or relaxation time- the number of steps required for the system to lose itsmemory of previous configurations with respect to theproperty A.

– The correlation time can be estimated during a simulation.

Page 13: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 13Computational Nanoscience of Soft [email protected]

Estimating errors in equilibrium properties

• The correlation time can be estimated during a simulationas follows:– Decompose entire trajectory into nb blocks, each containing

tb successive steps.– Calculate <A>b, the average of A for each block.

– As tb increases, the block averages will become uncorrelated,and then the variance of the block averages will becomeinversely proportional to tb.

!

" 2A

b( ) =

1

nb

Ab# A

total( )

2

b=1

nb

$!

Ab

=1

tb

Ak

k=1

tb

"

Page 14: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 14Computational Nanoscience of Soft [email protected]

Estimating errors in equilibrium properties

• Thus, within a simulation of M total steps,M/tcorr = P (number of independent data values).

• The error in the simulation will decrease not like1/sqrt(M), but like 1/sqrt(P).

Page 15: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 15Computational Nanoscience of Soft [email protected]

Identifying & estimating errors in a simulation

• Two kinds of errors possible:– Systematic: shows up as a constant bias from proper

behavior (e.g. average property is displaced from its propervalue).

• Error in algorithm or force field

• Approximations in algorithm, since all finite differencemethods used in MD generate only an approximation tothe true integral of the equations of motion.

• Round-off errors due to limited precision of computer

Page 16: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 16Computational Nanoscience of Soft [email protected]

Identifying & estimating errors in a simulation

• To detect systematic error: calculate the distribution ofsome thermodynamic quantity -- should be Gaussian aboutthe average:

• The variance σ2 = <(A - <A>) 2 >. SD = sqrt(σ)

!

p(A) =1

2"#exp $ A $ A( )

2

2# 2[ ]

Page 17: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 17Computational Nanoscience of Soft [email protected]

Autocorrelation functions

• An autocorrelation function indicates the extent to whicha system retains a “memory” of itself at a previous time (or,conversely, how long it takes the system to “lose” itsmemory and “decorrelate” from itself.)

• The autocorrelation function for a particular property tellsyou how long you must simulate to generate a series ofconfigurations uncorrelated from the previous series withrespect to that property.

Page 18: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 18Computational Nanoscience of Soft [email protected]

Autocorrelation functions

!

Cvv(t) =

1

N

vi(t) " v

i(0)

vi(0) " v

i(0)

i=1

N

#

• One example is the velocity autocorrelation functionCvv(t), whose value indicates how closely the velocity of aparticle at time t is correlated with the velocity at time zero.

The normalizationinitializes the functionto unity. In general, ACfunctions start at oneand decay to zero.Average over

all atoms

Page 19: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 19Computational Nanoscience of Soft [email protected]

Autocorrelation functions

•In many instances, autocorrelation functions decayexponentially: C(t) = exp(t/τC)

•Then the relaxation time is typically the time it takes for thefunction to fall to 1/e of its initial value.

•It is possible to construct an autocorrelation function for anyparticle quantity (e.g. vi, ui,…) or any system quantity (e.g. U, E,m, …).

•Generally,

!

CAA(t) =

1

N

Ai(t) " A

i(0) # A

i(0)

2

Ai(0) " A

i(0) # A

i(0)

2

i=1

N

$

Page 20: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 20Computational Nanoscience of Soft [email protected]

Autocorrelation functions

•Generally, simulations are run for times much longer than therelaxation time of the property of interest.

•Then, many sets of data can be extracted from the simulationand averaged over to provide a better estimate of thecalculation.

•If P MD steps are required for relaxation to zero, and the totalsimulation time is Q steps, then Q - P different sets of valuescould be used to calculate a value for the correlation function.

Page 21: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 21Computational Nanoscience of Soft [email protected]

Autocorrelation functions

•The first set runs from step 1 to step n, the second from step 2to step n+1, and so on.•If we use M time origins (tj) then the VACF is:

•Quantities with small relaxation times can thus be determinedwith greater statistical precision since we can average over agreat number of independent data sets within a given simulation.•No quantity whose relaxation time exceeds the length of thesimulation can be determined accurately.

!

Cvv (t) =1

NMj=1

M

"vi(t j ) # vi(t j + t)

vi(t j ) # vi(t j )i=1

N

"Mmnt atlatest timenoisy!

Page 22: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 22Computational Nanoscience of Soft [email protected]

Fluctuations decrease as averaging increases

Velocity Autocorrelation Function

Page 23: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 23Computational Nanoscience of Soft [email protected]

Simulation Observables: Dynamics

!

D = T limt"#

dt 'Cvv(t')

0

t

$

The diffusion coefficient is related to the velocityautocorrelation function by a Green-Kubo relation:

!

D =1

6limt"#

d

dt

1

Nri(t) $ r

i(0)( )

2

i=1

N

%

The diffusion coefficient can also be obtained from theslope of the MSD:

Use normalizedform of Cvv(t’)!For unnormalized:include factor of 1/3in 3-d, 1/2 in 2-d, anddon’t include T.

msd

Page 24: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 24Computational Nanoscience of Soft [email protected]

Simulation Observables: Dynamics

Mean square displacement

DenseLJliquidaboveTg.

!

r2(t) =

1

Nri(t) " r

i(0)( )

2

i=1

N

#

Page 25: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 25Computational Nanoscience of Soft [email protected]

Accuracy increases as averaging increases

Page 26: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 26Computational Nanoscience of Soft [email protected]

Advanced MD Methods:Isothermal and Isobaric Simulations

Recommended reading:Leach Chapter 6-7

and/orFrenkel & Smit Chapters 4, 6, 15 and

Appendices D, E, and F

Page 27: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 27Computational Nanoscience of Soft [email protected]

MD Simulations in Different Ensembles

• Thus far, we’ve been discussing MD simulations of systemswith constant N, V and E. The NVE ensemble is themicrocanonical ensemble; all states are equally likely.• How do we know this? Because we’ve been solving F=mafor an isolated system, one for which we have not allowedthe exchange of energy or particles with the outside.

• MD simulations may be performed in different ensembles,i.e. with different variables held fixed.

•Microcanonical ensemble: NVE•Canonical (Isothermal) ensemble: NVT•Isothermal-isobaric ensemble: NPT•Grand canonical ensemble: µVT

Page 28: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 28Computational Nanoscience of Soft [email protected]

MD Simulation in the NVT Ensemble

•In the canonical ensemble, where N, V, and T are held fixed,the probability of observing a particular configuration is givenby the familiar Boltzmann probability:

•The partition function Q for the canonical ensemble is

!

"(pN ,rN ) =exp #E(pN ,rN ) /kBT( )

Q

!

QNVT =1

N!

1

h3N

dpNdr

Nexp "

E(pN,r

N)

kBT

#

$ %

&

' ( ))

Page 29: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 29Computational Nanoscience of Soft [email protected]

Molecular Dynamics Simulation: NVT

Velocity Rescaling• Recall KE = mv2/2 = 3NkBT/2 in an unconstrained system.• Simple way to alter T: rescale velocities every time step by

a factor λ(t):– If temperature at time t is T(t) and v is multiplied by λ, then the

temperature change is

!

"T =1

2

2

3

mi(#v

i)2

NkBi=1

N

$ %1

2

2

3

mivi

2

NkBi=1

N

$

"T & Tt arg et %T(t) = (#2 %1)T(t)

# = Tt arg et /T(t)

Page 30: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 30Computational Nanoscience of Soft [email protected]

Molecular Dynamics Simulation: NVT

Velocity Rescaling• This method does not generate configurations in the

canonical ensemble.

• Why? Because system is not coupled to an externalreservoir, or heat bath.

• Thus not a good method for controlling temperature,since it’s unknown how rescaling affects properties of thesystem.

• Instead, use a “proper” thermostat.

Page 31: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 31Computational Nanoscience of Soft [email protected]

Molecular Dynamics Simulation: NVT

Isothermal Ensemble: Using a Thermostat

• Most systems of practical interest are not isolated. - theyinteract with their surroundings (“heat bath”).

• Want physical way of coupling to heat bath.

• Using a weakly coupled thermostat can help eliminate driftin total energy due to algorithm inaccuracies and round-off error.

• Several types of thermostats; choose wisely depending onyour problem!

Page 32: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 32Computational Nanoscience of Soft [email protected]

Molecular Dynamics Simulation: NVT

Berendsen thermostat• Maintain temperature by coupling system to an external

heat bath that is fixed at the desired temperature, andwhich acts as a source of thermal energy, supplying orremoving heat from the system.

• Basic idea: Rescale velocities to achieve a mean kineticenergy consistent with the target (bath) temperature.

• Rescale velocity of every particle at each time step suchthat

• The quantity τ is a coupling factor whose magnitudedetermines how tightly the bath and system are coupled.

!

dT

dt=1

"Tbath

#T(t)( )

Page 33: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 33Computational Nanoscience of Soft [email protected]

Molecular Dynamics Simulation: NVT

Berendsen thermostat• This method gives an exponential decay of the system

towards the desired T.• The change in T between successive time steps is:

• Referring back to the simple velocity rescaling method,the scaling factor for the velocities is then

• If τ is large (λ near 1), coupling is weak; if τ is small (λlarge), coupling is strong. When τ = δt, algorithm isequivalent to simple velocity rescaling.

• Effectively, a proportional controller.

!

"T =#t

$Tbath

%T(t)( )

!

" = 1+#t

$

Tbath

T(t)%1

&

' (

)

* +

This is applied atevery time step.

Page 34: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 34Computational Nanoscience of Soft [email protected]

Molecular Dynamics Simulation: NVT

Berendsen thermostat• Typical: Choose τ = 0.4ps for δt = 1fs, so δt/τ = 0.0025.

• Advantage over simple rescaling: it permits fluctuationsabout the target temperature.

• If coupled weakly, system trajectory is nearly identical toan NVE trajectory, and ok for dynamics.

• Used primarily to prevent long time drift of E in NVE.

• For long time dynamical phenomena, even strongcouplings ok.

• BUT…does not reproduce canonical ensemble, andfluctuations do not correspond to any obvious ensemble.

Page 35: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 35Computational Nanoscience of Soft [email protected]

Molecular Dynamics Simulation: NVT

Two classes of methods for constraining T thatproduce fluctuations according to the canonicalensemble when properly implemented:

• Stochastic collisions method

• Extended system method

Page 36: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 36Computational Nanoscience of Soft [email protected]

Molecular Dynamics Simulation: NVT

Stochastic collision method: Andersen thermostat• Occasionally replace a randomly chosen particle’s velocity

with a velocity chosen randomly from a Maxwell-Boltzmann distribution at the desired T.

• This replacement represents stochastic collisions with theheat bath.

• Equivalent to the system being in contact with a heatbath that randomly emits thermal particles that collidewith the atoms in the system and change their velocity.!

p(vix ) = m

2"kBT

#

$ %

&

' (

1/2

exp(-1

2kBT m vix

2)

Page 37: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 37Computational Nanoscience of Soft [email protected]

Molecular Dynamics Simulation: NVT

Stochastic collision method: Andersen thermostat• Between each collision, the system is simulated at constant

energy. Thus, the overall effect is equivalent to a series ofmicrocanonical simulations, each performed at a slightly differentenergy.

• The distribution of energies of these “mini-microcanonical”simulations should be a Gaussian function.

• Anderson showed that, to achieve this, the mean rate ν at whicheach particle should suffer a stochastic collision is:

• a is a dimensionless constant. κ is thermal conductivity. n is thenumber density of the particles. If κ isn’t known, then can find νfrom the intermolecular collision frequency νc: ν = νc/N2/3.

!

" =2a#

3kBn1/ 3N2 / 3

Page 38: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 38Computational Nanoscience of Soft [email protected]

Molecular Dynamics Simulation: NVT

Stochastic collision method: Andersen thermostat• If the collision frequency ν is too low, then the system

will not sample from a canonical ensemble of energies.

• If ν is too large, then the temperature controllerdominates and the fluctuations in the kinetic energyaren’t physical.

• If ν is just right, the stochastic collisions turn the MDsimulation into a Markov process and generate acanonical ensemble. That is, states of energy E will begenerated according to the Boltzmann distribution.

• More than one particle’s velocity may be changed at atime.

Page 39: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 39Computational Nanoscience of Soft [email protected]

Molecular Dynamics Simulation: NVT

Stochastic collision method: Andersen thermostat

• Ok for statics and thermodynamics, since generates theright ensemble.

• But --- Anderson thermostat randomly decorrelatesvelocities, and messes up dynamics. Dynamics notphysical.

• Don’t use if you plan to measure dynamical properties.

Page 40: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 40Computational Nanoscience of Soft [email protected]

Molecular Dynamics Simulation: NVT

Extended system method: Nose’-Hoover thermostat• Introduced by Nose’ and developed by Hoover in 1985.

• Considers the thermostat to be part of the system (hence‘extended system’).

• Based on extended Lagrangian approach in which anadditional dynamical variable or degree of freedom s,representing the reservoir, imposes constraint of constant T.

reservoir

physical system

extendedsystem

Page 41: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 41Computational Nanoscience of Soft [email protected]

Molecular Dynamics Simulation: NVT

Extended system method: Nose’-Hoover thermostat• Reservoir has potential energy Ures and kinetic energy Kres.

• f = # of dof in physical system, and T is the desired temperature.Q has dimensions energy x (time)2, and is considered thefictitious “mass” of the additional degree of freedom.

• The magnitude of Q controls the coupling between the physicalsystem and the reservoir, and thus influences the temperaturefluctuations.

reservoir

physical systemUres = (f+1)kBT ln s

Kres = (Q/2)(ds/dt)2

!

"(t) = sps Q

Page 42: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 42Computational Nanoscience of Soft [email protected]

Molecular Dynamics Simulation: NVT

Extended system method: Nose’-Hoover thermostat• Q controls the energy flow between system and reservoir.• If Q is large, the energy flow is slow; the limit of infinite Q

corresponds to NVE MD since there is then no exchange of energybetween system and reservoir.

• If Q is small, then the energy oscillates unphysically, causingequilibration problems.

• Nose’ suggested Q = CfkBT, where C can be obtained by performingtrial simulations for a series of test systems and observing how wellthe system maintains that desired T.

• Upshot: equations of motion are modified and δt is multiplied by Q.

Read in Frenkel and Smit for details if interested.

Page 43: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 43Computational Nanoscience of Soft [email protected]

Molecular Dynamics Simulation: NVT

Extended system method: Nose’-Hoover thermostat

• Best and most used thermostat today for NVT simulations.

• Ensures that average total KE per particle is that of theequilibrium isothermal ensemble.

• Reproduces canonical ensemble in every respect (provided onlythe total energy of the extended system is conserved.)– If momentum also conserved, NH still works only if COM fixed.– Otherwise, must use the method of “NH chains”.

• More complicated than other methods, but can be used withvelocity Verlet and predictor-corrector methods.

• Dynamics NOT independent of Q, but better than Andersen.

Page 44: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 44Computational Nanoscience of Soft [email protected]

Molecular Dynamics Simulation: NPT

• Just as one might like to specify T in an MD simulation,one might also like to maintain the system at a fixedpressure P.

• Simulations in the isobaric-isothermal NPT ensemble arethe most similar to experimental conditions.

• Certain phenomena may be more easily achieved underconditions of constant pressure, like certain phasetransformations.

Page 45: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 45Computational Nanoscience of Soft [email protected]

Molecular Dynamics Simulation: NPT

• A system maintains constant P by changing its volume,and thus to perform an NPT simulation we must allow oursimulation cell to fluctuate in size.

• The amount of volume fluctuation is related to theisothermal compressibility

• An easily compressible material has a larger value of κ, solarger V fluctuations occur for a given change in P than ina more incompressible material.

!

"T

= #1

V

$V

$P

%

& '

(

) * T

=1

kBT

V # +V ,( )2

V2

Page 46: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 46Computational Nanoscience of Soft [email protected]

Molecular Dynamics Simulation: NPT

• For an ideal gas, which is very compressible, κ = 1 atm-1.For a simulation box 2 nm on a side (volume 8 nm3) at300K, the rms change in V is roughly 18 nm3, which islarger than the initial size of the box.

• For water, which is relatively incompressible, κ = 44 x 10-6

atm-1, and the volume fluctuation is roughly 1/10 nm3,which corresponds to roughly 0.46 nm in one direction.

• These bounds give a rough idea of how we should pick thesize of our simulation cell when V constant - larger thanany fluctuation.

Page 47: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 47Computational Nanoscience of Soft [email protected]

Molecular Dynamics Simulation: NPT

Two types:• Andersen Barostat

– Allows box size to fluctuate but not change shape.– Appropriate for liquids, which do not support shear

stress.– Change volume by rescaling positions: r = r+αr

• Rahman-Parinello Barostat– Allows box size and shape to change.– Use only for solids.– Necessary for simulating phase transformations.

Read in Frenkel and Smit for details if interested.

Page 48: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 48Computational Nanoscience of Soft [email protected]

Simulation Observables:Thermodynamics

Page 49: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 49Computational Nanoscience of Soft [email protected]

time steps

Simulation Observables: Thermodynamics

• Thermodynamic quantities– Calculate T, ρ, p, E etc. depending on ensemble

NVE

!

T(t) =m

ivi

2(t)

kB(3N " N

c)

i=1

N

#

Nc = # ofconstraints(e.g. linearmomentum)3N - Nc = #of degreesof freedom

Page 50: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 50Computational Nanoscience of Soft [email protected]

Pressure

!

P =1

3Vmivi

2 "1

6Vfijrij

j=1

N

#i=1

N

#i=1

N

#

Simulation Observables: Thermodynamics

NVE

time steps

P is obtained from the virialthm of Clausius.

Virial = exp. valueof the sum of theproducts of the particle coords and the forces acting on them.

W=Σxifxi=-3NkBT

Page 51: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 51Computational Nanoscience of Soft [email protected]

The pressure may also be derived from the radialcorrelation function g(r):

Simulation Observables: Pressure

!

P =NkBT

V"2#N$

3kBTVr2r%u(r)

%rg(r)dr

0

&

'

This is less accurate than calculating the pressure directly, becauseof the errors inherent in calculating g(r). Calculating both and comparingis a typical way of checking simulation.

Page 52: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 52Computational Nanoscience of Soft [email protected]

NVE vs. NVT

Page 53: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 53Computational Nanoscience of Soft [email protected]

Thermodynamic Response Functions

• All thermodynamic response functions are derivatives ofone thermodynamic quantity with respect to another.

• They may be calculated in any ensemble, but theexpressions will in general be different since differentensembles constrain fluctuations in different variables.

• For first order derivatives of free energies, such as V, P,etc., the expression is independent of ensemble.

Simulation Observables: Thermodynamics

Page 54: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 54Computational Nanoscience of Soft [email protected]

Thermodynamic Response Functions: Heat Capacity

• NVE: Perform simulations at many different E for thesame N & V, and measure T. Calculate Cv from derivativeof E(T) (numerically or fitting polynomial and thendifferentiating).

• NVT: (1) Fix N and V, perform simulations at different Tand measure E. Then calculate Cv from derivative of E(T).

(2) Calculate Cv from fluctuations in energy.!

CV

="E

"T

#

$ %

&

' ( V

=1

kBT2

E ) *E+( )2

Simulation Observables: Thermodynamics

<E2> - <E>2 On the fly

At the end (less error?)

Page 55: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 55Computational Nanoscience of Soft [email protected]

Thermodynamic Response Functions: Adiabatic Compressibility

• NVE: (1) Fix N and E, perform simulations at different V andmeasure P. Calculate κS from derivative of V(P).

(2) Calculate κS from fluctuations in pressure.

• NVT: Calculate κS from fluctuations in pressure.!

"S

= #1

V

$V

$P

%

& '

(

) * S

+ P # ,P-( )2

Simulation Observables: Thermodynamics

Page 56: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 56Computational Nanoscience of Soft [email protected]

Thermodynamic Response Functions: Isothermal Compressibility

• NVT: Fix N and T, perform simulations at different V and measure P.Calculate κT from derivative of V(P).

• NPT: Fix N and T, perform simulations at different P and measure V.Calculate κT from derivative of V(P).

• NPT: Calculate κT from fluctuations in volume.• µVT: Calculate κT from fluctuations in N.

!

"T

= #1

V

$V

$P

%

& '

(

) * T

+ V # ,V -( )2

Simulation Observables: Thermodynamics

Page 57: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 57Computational Nanoscience of Soft [email protected]

The MD Method:Tricks of the Trade

Recommended reading:Leach Chapter 6-7

and/orFrenkel & Smit Chapters 4, 6, 15 and

Appendices D, E, and F

Page 58: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 58Computational Nanoscience of Soft [email protected]

MD Simulation: Tricks of the Trade

The Burden of Force Calculations

• Typically ~80% of effort is in force calculation, and the mosttime consuming part of that is for forces between non-bondedatoms.– Algorithm is O(N2) for pairwise (2-body) non-bonded forces; O(N) for

two, three, and four-body bonded forces; O(N3) for 3-body non-bonded forces, ….

• For short-range non-bonded forces, calculating forces betweenall pairs is not necessary, since fij ≈ 0 for pairs separated by largedistances.– Recall for LJ potential, the energy between two atoms separated by

2.5σ is just 1% of its value at the minimum (21/6σ). This is becausethe 1/r6 dispersion term falls off quickly.

Page 59: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 59Computational Nanoscience of Soft [email protected]

MD Simulation: Tricks of the Trade

• To decrease effort in non-bonded force calculation:– Introduce cut-off

• Eliminate unnecessary calculations b/t non-interacting particles. (e.g. 2.5σ where small).

• Double the cutoff --> 8 times the amount of work since #particles increases like r3 if density constant

Page 60: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 60Computational Nanoscience of Soft [email protected]

MD Simulation: Tricks of the Trade

• To decrease effort in non-bonded force calculation:– Use minimum image convention.

• Each atom sees just one image of every other atom, andthe force is calculated with the closest atom or image.

• When a cutoff is used, forces between all pairs fartherapart than the cutoff distance are set to zero, taking intoaccount the closest image.

• When using periodic boundary conditions (PBC), thecutoff should not be so large that a particle interacts withits own image, or with the same particle twice.

– With PBC in a cubic box, cut-off is limited to half the boxlength at most. For asymmetric boxes, cut-off must be lessthan half the length of the shortest side.

Page 61: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 61Computational Nanoscience of Soft [email protected]

MD Simulation: Tricks of the Trade

• Cut-offs

– Cut-offs for short-range forces, if done correctly,should not affect the results significantly.

– Cut-offs for long-range forces, like electrostatic forces,are tricky and must be done carefully. They mayaffect the results somewhat.

– If both types of forces exist, must use two cut-offs.

Page 62: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 62Computational Nanoscience of Soft [email protected]

Typical cut-off values

• LJ = 2.5σ (standard)• Explicit and united atom = 8-12 angstroms• Bead-spring= 2.5σ or 21/6σ

• Coulomb forces = 12-20 angstroms• Solid state (metals) = several neighbor shells

Always test the sensitivity of your results to choice ofcutoff!

Page 63: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 63Computational Nanoscience of Soft [email protected]

MD Simulation: Tricks of the Trade

• Cut-offs– Cut-offs can introduce discontinuities in the forces

and/or energies.• E.g. Lennard Jones:

r = σ

Energy goes continuously to zero.Force = - du/dr is discontinuous.

Energy is discontinuous.Force is discontinuous.

Page 64: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 64Computational Nanoscience of Soft [email protected]

MD Simulation: Tricks of the Trade

• Cut-offs– Cut-offs can introduce discontinuities in the forces

and/or energies.• Shift the energy and force so both are continuous at the

cutoff.

E = 0

!

u'(r) = u(r) " uc" (r " r

c)du(r)

dr

#

$ %

&

' ( r= r

c

u'(r) = 0 r > rc

r < rc

Force discontinuitycan cause instabilities

Page 65: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 65Computational Nanoscience of Soft [email protected]

MD Simulation: Tricks of the Trade

• Cut-offs– Shifting the potential causes it to deviate from the

‘true’ potential, so any calculated properties will differ.• Cut-offs always lead to some loss of the potential energy.• The missing energy can be easily calculated if it is

assumed that g(r) is one at r > rc.– For N particles, the correction is, in general:

– For LJ, the correction is:

!

Ecorrection

= 2"#N r2u(r)dr

rc

$

%

!

Ecorrection

= 8"#N$%12

9rc

9&% 6

3rc

3

'

( )

*

+ ,

Page 66: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 66Computational Nanoscience of Soft [email protected]

MD Simulation: Tricks of the Trade

• Cut-offs– Corrections not always easy to calculate. Thus when

using an atomistically accurate force field, shiftedpotentials not often used.

– Alternative way to eliminate energy and forcediscontinuities at the cutoff: use a switching function.

– Cut-offs for long range forces:• Ewald summation method• Reaction field method• Cell multipole method

Page 67: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 67Computational Nanoscience of Soft [email protected]

MD Simulation: Tricks of the Trade

• Cutoffs– By itself, the use of a cutoff may not dramatically

reduce the time taken to compute the number of non-bonded interactions, since we must still spend timecalculating the distances between every pair of atomsto decide which pairs are close enough to calculate theforce between them.

– Calculating all those distances takes almost as muchtime as calculating the forces.

Page 68: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 68Computational Nanoscience of Soft [email protected]

MD Simulation: Tricks of the Trade

Q: How can we efficiently find all the interactingpairs?

• Simple solution: For each atom, test against allothers - requires O(N2) algorithm

• Sophisticated solution: Keep lists of each atoms’neighbors (atoms within cutoff range) and workfrom lists

Page 69: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 69Computational Nanoscience of Soft [email protected]

MD Simulation: Tricks of the Trade

• Neighbor lists– In many instances, atoms don’t move much over 10-20

MD steps.

– If we knew which atoms were within each other’scutoff distance, then we wouldn’t have to calculate allthe other distances.

– Trick: non-bonded neighbor lists.• Two types:

– Verlet neighbor list– Linked cell list - used when system size >> cutoff rc

of force fields

Page 70: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 70Computational Nanoscience of Soft [email protected]

MD Simulation: Tricks of the Trade

Verlet Neighbor Lists

rv

rc

Particle iinteracts withall particleswithin cutoffradius rc.

Verlet listcontains all*particles withina sphere ofradius rv > rc.

*1/2 for MD

Rebuild listwhen anyparticle movesmore thanrv - rc.

Constructinglist is O(N2),but, using list,forcecalculationsare O(N).

Verlet, Phys. Rev 159, 98 (1967)

Page 71: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 71Computational Nanoscience of Soft [email protected]

MD Simulation: Tricks of the Trade

Divide boxinto cells ofedge L ≥ rc

Each particleinteractswith onlyparticles inthe same orneighboringcells.

Re-bin only whena particle movesmore than L - rc.

Since listconstruction isO(N), & Ncellsneeded forforce calc isindependent ofN (always 27),algorithm isO(N)!

Cell or Linked Lists

rcL

Hockney, et al, J. Comp.Phys. 14, 148 (1974).

Page 72: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 72Computational Nanoscience of Soft [email protected]

MD Simulation: Tricks of the Trade

Verlet Neighbor Lists vs. Linked Cell Lists

• Linked cell wins when lists must be re-constructed (O(N) vs.0(N2)).

• Verlet list is ~6 times faster when calculating forces.

• Solution: Use both!– Use Verlet for force calculation, but build Verlet lists from linked cell

lists.– Resulting algorithm is O(N) in both CPU and memory

• For more discussion, see Frenkel & Smit appendix.

Page 73: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 73Computational Nanoscience of Soft [email protected]

More tricks

• Exploit Newton’s 3rd law– Pair-wise forces: compute each pairwise force only

once, store equal/opposite force on both atoms

– Molecular forces: compute 3-body and 4-body forcesonly once, store forces on all atoms

Page 74: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 74Computational Nanoscience of Soft [email protected]

MD Simulations: What’s do-able?

• On single processor: several µsec/atom/time step for non-bonded, short range interaction is good; usually, 7-30 µsec/atom/timestep depending on machine and model.

• “Big systems” on a single processor: 20,000 particles for10-100 million time steps.– 1 fs/time step -> 1-100 ns– Dynamics over 6-8 decades

• Parallel computing: Save time and/or study largersystems by distributing the job over several processorssimultaneously.

Page 75: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 75Computational Nanoscience of Soft [email protected]

Timescale issues in MD

• Limited timescale is most serious drawback ofMD– Timestep size limited by fastest atomic oscillations

• C-H bond = 10 fs --> < 1 fs timestep• Debye frequency = 1013 --> 2 fs timestep

– Interesting problems often on longer timescales• Protein folding (µs to seconds)• Polymer entanglement ( > msec)• Glass relaxation (seconds to decades)

– Even smaller timesteps required in quantum MD

Page 76: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 76Computational Nanoscience of Soft [email protected]

# particles - # timesteps metric

• Atom-steps = size of simulation

• >1012 atom-steps is typical for production simulation– E.g. 105 atoms for 107 timesteps– 2 months on a single 1.7 GHz Pentium (LJ)– Few hours if job split over hundreds of processors

• 1 cubic micron (1010 atoms) for 1 or 2 ns (106 steps)– MD gets 10% of peak theoretical speed

• 3 years on a teraflop/s machine!

Page 77: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 77Computational Nanoscience of Soft [email protected]

How to do better?

• Accelerate time-stepping on a single processor– Fast integration schemes– Multiple time-step algorithms like rRespa– Constraints to freeze out fast, unimportant motions

• Use more processors to handle more atoms– Parallel computing for MD

Page 78: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 78Computational Nanoscience of Soft [email protected]

MD simulations on parallel computers

General strategies

Page 79: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 79Computational Nanoscience of Soft [email protected]

Classes of parallel machines

• SMP ($$$)– Shared memory across multiple processors– SGI Origin, HP, Sun

• Distributed memory MPP ($$)– Cray T3E– DOE ASCI machines

• Linux cluster (Beowulf) ($)– Intel Pentium and Xeon clusters– Apple G5 clusters– AMD Athlon and Opteron clusters– 100 Mbit or Gbit ethernet ($) or Force 10 ($$)

or Myrinet ($$$) or Infiniband ($$$+) network– Also: hybrids (>2 processors/node)

Page 80: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 80Computational Nanoscience of Soft [email protected]

MD Simulations on Parallel Architectures

• MD is inherently parallel– Forces can be computed simultaneously– Particle positions and velocities can be updated

simultaneously– Long range forces - tricks exist.

• Implementation– MPI (message passing interface)

• “assembly language” of parallel computing• Portable• Performs well generally on all classes of machines• A must on clusters

Page 81: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 81Computational Nanoscience of Soft [email protected]

Goals for parallel MD algorithms

• Scalability– Short-range MD scales as N– Optimal scaling is N/P

• Good for short-range forces– 80% of cpu effort

• Fast for small systems, not just large– Nano, polymer, bios systems require long timescales– 1M steps of 1K atoms more useful than 1K steps of 1M atoms

• Efficient at finding neighbors– Liquid state, inhomogeneous systems– Neighbors change rapidly

Issues– communication costs (worst for clusters, even with Myrinet)– Worry about non-bonded force computations

Page 82: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 82Computational Nanoscience of Soft [email protected]

MD Simulations on Parallel Architectures

Two classes of algorithms for all MD codes:• Replicated data methods (split up atoms)

– Force decomposition– Atom decomposition

Good for modest numbers of processors, fastcommunication, inhomogeneous systems of modest size.

• Geometric methods (split up simulation box)– Spatial decomposition

Method of choice for large systems, homogeneous systems,large numbers of processors, and machines with poorcommunication.Easy to implement; can use linked cell lists.

Plimpton, J. Comp. Phys. 117, 1 (1995)Plimpton & Hendrickson, J. Comp. Chem. 17, 326 (1996)www.cs.sandia.gov/~sjplimp/codes.html

Page 83: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 83Computational Nanoscience of Soft [email protected]

MD Simulations on Parallel Computers

• With, e.g., 10,000 atoms per processor, cluster machines with64 dual-processor nodes can handle 1,280,000 atoms.– Largest MD simulation: 1 billion atoms on 1000 alpha processors.

(Vashishta, et al LSU Center for Concurrent Computing)

• Timescales still limited by speed of slowest processor, andcommunication speeds between processors. Thus withfemtosecond or 10 femtosecond timesteps, microsecondsimulations are doable, but just.

Page 84: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 84Computational Nanoscience of Soft [email protected]

Parallel algorithms for MD

• All three methods balance computation optimallyas N/P

• Differ in key issues for parallel scalability– Communication costs– Load balance

• Focus on pair-wise computation– Other tasks can be done with any of three methods

• Molecular forces• Ensembles• Thermodynamics• Diagnostics• …

Page 85: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 85Computational Nanoscience of Soft [email protected]

Atom decomposition

• Each processor assigned N/P atoms for durationof simulation

• Each processor stores copy of entire position (x)and force (f) vectors (“replicated data”)

• Each processor computes all forces on its N/Patoms

• Processor owns subset of rows of matrix offorces (NxN, sparse)

Page 86: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 86Computational Nanoscience of Soft [email protected]

Atom decomposition

• At every timestep, each processor– Updates v and x for its N/P atoms N/P– Communicates to acquire all N coords N– Computes forces on its N/P atoms N/P– Communicates to sum forces on its N

N/P atoms

• Newton’s third law and neighbor lists can beexploited

Page 87: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 87Computational Nanoscience of Soft [email protected]

Atom decomposition

• Bottom line:– Local computation (scales as N/P)– Global communication (scales as N)

• Advantages:– Simplest to implement, small change to existing code– Easy to compute molecular forces– Automatically load-balanced– Many legacy MD codes parallelized this way

• Disadvantages– Communication will dominate as P increases, particularly on a

cluster– Memory scales as N, independent of P - usually not a problem

Page 88: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 88Computational Nanoscience of Soft [email protected]

Force decomposition

• Similar to atom decomposition, but divide forcematrix up amongst processors

• Communication and memory scale better by afactor of sqrt(P)

Page 89: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 89Computational Nanoscience of Soft [email protected]

Spatial decomposition algorithm

• Physical domain split into boxes, one per processor• Each processor computes forces on atoms in its box using

info from nearby processors• Atoms “carry along” molecular topology as they move to

new processors• Communication accomplished via nearest-neighbor 6-way

stencil• Advantages

– Communication scalessub-linearly as (N/P)2/3

– Memory is optimal N/P

Page 90: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 90Computational Nanoscience of Soft [email protected]

Spatial decomposition algorithm

• At every timestep, each processor– Updates v and x for its N/P atoms N/P– Communicates to acquire nearby coordinates (N/P)2/3

– Computes forces on its N/P atoms N/P• Pairwise and molecular

– Communicates to sum nearby forces on its atoms (N/P)2/3

• Newton’s third law and neighbor lists can be exploited• Bottom line:

– Local computation (scales as N/P)– Local communication (scales as N/P or better)

• Disadvantage:– Harder to program efficiently– Major surgery on existing code– Load-imbalance will cause problems

Page 91: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 91Computational Nanoscience of Soft [email protected]

Which algorithm is best?

• Best = fastest, most scalable

• Depends on– Machine (balance factor = communication/computation

ratio)– Size of problem (N)– Number of processors (P)– How matter is distributed

• Should test code on your machine, and on yourproblem, to be sure.

Page 92: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 92Computational Nanoscience of Soft [email protected]

Which algorithm is best?

• Replicated data best for– Modest P– Fast network (gather/scatter)– Non-uniform matter distribution

• Irregular nano-geometry• Single biomolecules in water

• Spatial decomposition best for– Large P and large N– Uniform matter distribution

• Beowulf clusters bias more towards spatial decompositionsince P larger, but communication may be slow.

Page 93: Molecular Dynamics Method, continuederjank/Lectures/557/557-Lecture4...Computational Nanoscience of Soft Materials ChE/MSE 557 Lecture 4 Fall 2006 24 sglotzer@umich.edu Simulation

ChE/MSE 557 Lecture 4 Fall 2006 93Computational Nanoscience of Soft [email protected]

Parallel MD codes

• CHARMM - www.scripps.edu/brooks– proteins and DNA, replicated data (atom decomposition)

• AMBER - amber.scripps.edu– proteins and DNA, replicated data (atom decomposition)

• NAMD - www.ks.uiuc.edu/Research/namd– proteins and DNA, spatial decomposition

• NWCHEM - www.emsl.pnnl.gov/docs/nwchem/nwchem.html– Biological molecules, spatial decomposition

• DL_Poly - www.dl.ac.uk/TCS/Software/DL_POLY– LJ, metals, polymers, biomolecules, replicated data and spatial

decomposition versions• LAMMPS - www.cs.sandia.gov/~sjplimp/lammps.html

– LJ, metals, polymers, biomolecules, spatial decomposition

All free. Some with licenses.