Molecular dynamics calculations and the analysis of results



Similar documents
1 The basic equations of fluid dynamics

1. Fluids Mechanics and Fluid Properties. 1.1 Objectives of this section. 1.2 Fluids

State of Stress at Point

4 Microscopic dynamics

Basic Principles in Microfluidics

Differential Relations for Fluid Flow. Acceleration field of a fluid. The differential equation of mass conservation

Lecture 3 Fluid Dynamics and Balance Equa6ons for Reac6ng Flows

Fluid Mechanics: Static s Kinematics Dynamics Fluid

The Viscosity of Fluids

CBE 6333, R. Levicky 1 Differential Balance Equations

The derivation of the balance equations

Scalars, Vectors and Tensors

Basic Equations, Boundary Conditions and Dimensionless Parameters

CBE 6333, R. Levicky 1 Review of Fluid Mechanics Terminology

FLUID DYNAMICS. Intrinsic properties of fluids. Fluids behavior under various conditions

NUMERICAL ANALYSIS OF THE EFFECTS OF WIND ON BUILDING STRUCTURES

SELF DIFFUSION COEFFICIENT OF LENNARD-JONES FLUID USING TEMPERATURE DEPENDENT INTERACTION PARAMETERS AT DIFFERENT PRESSURES

Introduction To Materials Science FOR ENGINEERS, Ch. 5. Diffusion. MSE 201 Callister Chapter 5

Fluids and Solids: Fundamentals

Lecture 24 - Surface tension, viscous flow, thermodynamics

AP Physics 1 and 2 Lab Investigations

Heat Transfer From A Heated Vertical Plate

The Viscosity of Fluids

Introduction to COMSOL. The Navier-Stokes Equations

Ground Rules. PC1221 Fundamentals of Physics I. Kinematics. Position. Lectures 3 and 4 Motion in One Dimension. Dr Tay Seng Chuan

Chapter 4 One Dimensional Kinematics

Kinetic Theory of Gases. Chapter 33. Kinetic Theory of Gases

DIFFUSION IN SOLIDS. Materials often heat treated to improve properties. Atomic diffusion occurs during heat treatment

Physics 9e/Cutnell. correlated to the. College Board AP Physics 1 Course Objectives

Governing Equations of Fluid Dynamics

Mechanical Properties - Stresses & Strains

Figure 1.1 Vector A and Vector F

8.2 Elastic Strain Energy

Solving Simultaneous Equations and Matrices

SIZE OF A MOLECULE FROM A VISCOSITY MEASUREMENT

Chapter 22: Electric Flux and Gauss s Law

Diffusion and Fluid Flow

CLASSICAL CONCEPT REVIEW 8

Lecture 8 - Turbulence. Applied Computational Fluid Dynamics

High Speed Aerodynamics Prof. K. P. Sinhamahapatra Department of Aerospace Engineering Indian Institute of Technology, Kharagpur

Lecture L22-2D Rigid Body Dynamics: Work and Energy

Notes on Polymer Rheology Outline

XI / PHYSICS FLUIDS IN MOTION 11/PA

Unit 6 Plane Stress and Plane Strain

Indiana's Academic Standards 2010 ICP Indiana's Academic Standards 2016 ICP. map) that describe the relationship acceleration, velocity and distance.

2-1 Position, Displacement, and Distance

ENERGY CONSERVATION The First Law of Thermodynamics and the Work/Kinetic-Energy Theorem

Sound. References: L.D. Landau & E.M. Lifshitz: Fluid Mechanics, Chapter VIII F. Shu: The Physics of Astrophysics, Vol. 2, Gas Dynamics, Chapter 8

Mathematical Modeling and Engineering Problem Solving

Dimensional analysis is a method for reducing the number and complexity of experimental variables that affect a given physical phenomena.

3. Diffusion of an Instantaneous Point Source

Vacuum Technology. Kinetic Theory of Gas. Dr. Philip D. Rack

Lecture 4: Thermodynamics of Diffusion: Spinodals

CHEMICAL ENGINEERING AND CHEMICAL PROCESS TECHNOLOGY - Vol. I - Interphase Mass Transfer - A. Burghardt

Chapter 2. Derivation of the Equations of Open Channel Flow. 2.1 General Considerations

Physics 1A Lecture 10C

When the fluid velocity is zero, called the hydrostatic condition, the pressure variation is due only to the weight of the fluid.

AP2 Magnetism. (c) Explain why the magnetic field does no work on the particle as it moves in its circular path.

Fluid Mechanics Prof. S. K. Som Department of Mechanical Engineering Indian Institute of Technology, Kharagpur

4. Introduction to Heat & Mass Transfer

Diffusione e perfusione in risonanza magnetica. E. Pagani, M. Filippi

Ch 8 Potential energy and Conservation of Energy. Question: 2, 3, 8, 9 Problems: 3, 9, 15, 21, 24, 25, 31, 32, 35, 41, 43, 47, 49, 53, 55, 63

7. DYNAMIC LIGHT SCATTERING 7.1 First order temporal autocorrelation function.

Lecture L2 - Degrees of Freedom and Constraints, Rectilinear Motion

Viscoelasticity of Polymer Fluids.

S. Boyd EE102. Lecture 1 Signals. notation and meaning. common signals. size of a signal. qualitative properties of signals.

Chapter Outline. Diffusion - how do atoms move through solids?

11. Rotation Translational Motion: Rotational Motion:

Heat Transfer and Energy

Rheology of polymer systems/ Reologia dos sistemas poliméricos

Chapter 5: Diffusion. 5.1 Steady-State Diffusion

Potential Energy and Equilibrium in 1D

Kyu-Jung Kim Mechanical Engineering Department, California State Polytechnic University, Pomona, U.S.A.

CE 3500 Fluid Mechanics / Fall 2014 / City College of New York

11 Navier-Stokes equations and turbulence

Physics of the Atmosphere I

Heterogeneous Catalysis and Catalytic Processes Prof. K. K. Pant Department of Chemical Engineering Indian Institute of Technology, Delhi

Dynamic Process Modeling. Process Dynamics and Control

Chapter Outline. Mechanical Properties of Metals How do metals respond to external loads?

Mechanics lecture 7 Moment of a force, torque, equilibrium of a body

Normality Testing in Excel

Lecture 3: Models of Solutions

Orbits of the Lennard-Jones Potential

Physics Notes Class 11 CHAPTER 3 MOTION IN A STRAIGHT LINE

Chapter 8 Maxwell relations and measurable properties

CFD Application on Food Industry; Energy Saving on the Bread Oven

Universitätsstrasse 1, D Düsseldorf, Germany 3 Current address: Institut für Festkörperforschung,

Interactive simulation of an ash cloud of the volcano Grímsvötn

D Alembert s principle and applications

Appendix A: Science Practices for AP Physics 1 and 2

Defects Introduction. Bonding + Structure + Defects. Properties

Dimensional Analysis

AP1 Oscillations. 1. Which of the following statements about a spring-block oscillator in simple harmonic motion about its equilibrium point is false?

AP1 Electricity. 1. A student wearing shoes stands on a tile floor. The students shoes do not fall into the tile floor due to

Lecture 3. Turbulent fluxes and TKE budgets (Garratt, Ch 2)

Physics 111: Lecture 4: Chapter 4 - Forces and Newton s Laws of Motion. Physics is about forces and how the world around us reacts to these forces.

Incorporating Internal Gradient and Restricted Diffusion Effects in Nuclear Magnetic Resonance Log Interpretation

On a Flat Expanding Universe

Transcription:

Molecular dynamics calculations and the analysis of results Dr Billy D. Todd Centre for Molecular Simulation Swinburne University of Technology PO Box 218, Hawthorn Victoria 3122, Australia Email: btodd@swin.edu.au B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 1

Overview Now that we know how to set up, initialise and run an MD program we have to ask a very serious, but as yet neglected question: what do we actually want to do with it? Generally we will want to calculate some material properties of interest, such as, say, system energies, pressures, atomic structure, transport properties of fluids, etc. While all the previous material allows us to set up and simulate the systems we are interested in, they do not give us any clues as to how to calculate the properties we may actually be interested in. In this Module, we learn how to do just that. Indeed, it is this aspect of MD that makes it such a useful and powerful tool in science and engineering. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 2

Topic 1: Some quantities to calculate There are of course a number of different physical quantities which one may be interested in. For a liquid, they may be liquid structure factors, transport coefficients (eg, coefficients of diffusion, viscosity or thermal conductivity), the properties of liquid-vapour (solid) interfaces, etc. For solids, they may be crystal structure, adsorption of molecules on surface, melting behaviour, etc. We can not consider the numerous possible quantities we could calculate, but consider only a few of the most common and important in Topic 1. You can consult references [1-5] for details of other calculations. In addition to calculating relevant quantities, we will learn how to make sense of them in a statistically meaningful way, and how to output the results of a simulation so that they can be used by yourself and others, in Topic 2. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 3

In this topic, we consider the following properties and their calculation: Internal energy Pressure tensor Self-diffusion coefficient Zero-shear viscosity coefficient Pair distribution function The first three quantities pertain to all states of matter, whereas the last two are particularly useful and relevant for liquid state simulations. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 4

1.1 Energy The energy is the simplest and most straightforward quantity to calculate. Let us use the WCA potential [6], introduced in Module 9, to illustrate its calculation. φ WCA ( ) 12 6 σ σ 4ε + ε r < rc r = r r 0 r r c (10.1) From all pairs of atoms (i, j), one calculates their separations r ij, and from these, simply substitute them into Eqn (10.1). The trick is to calculate these pairs via the neighbour-list routine for computational efficiency. This is most readily accomplished in the force routine (see Module 9). B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 5

However, we have only got half the solution! The energy has contributions due to both potential and kinetic terms. The kinetic energy should be calculated after the momenta have been updated, i.e., after the force routine has been called. The kinetic energy can then be calculated, and then added to the potential energy. In Problem 10.1 you are asked to output both the potential and kinetic energy as a time series for the MD program you downloaded for Problem 9.2. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 6

1.2 Pressure tensor The standard definition of the pressure, given in most texts on mechanics, is that it is the force per unit area exerted upon a surface. But in which direction is the force? The following diagram makes this more clear. df z df y ds df x z y x Figure 10.1. Infinitesimal force, df, across an infinitesimal surface, ds. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 7

In Figure 10.1, an infinitesimal surface, ds, in the yz plane experiences an infinitesimal force across it, df. The pressure experienced by ds thus has three components: one directed normal to its surface (in this case, the x- direction), and another two directed in the y and z directions. However, there are another two surfaces to consider, those in the xy and xz planes. Thus, the pressure contains 9 components! It is not a vector, but is rather a 2 nd rank tensor, and may be defined by the expression: df = -ds.p (10.1) The negative sign, by convention, implies that the unit normal to the surface is directed opposite to the force exerted by the fluid on the surface. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 8

The pressure tensor may thus be written as: P P P P xx xy xz = Pyx Pyy Pyz P P P zx zy zz (10.2) Here, the indices of the components P αβ are such that α represents the direction of the normal to the surface (e.g., α = x implies that the surface is in the yz plane), and β is the component of force acting across that surface. In an MD simulation the calculation of the pressure tensor by calculating the forces across surfaces is usually not the most efficient method. For homogeneous systems this is definitely not the way to proceed! However, for inhomogeneous systems, it is in fact the best B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 9

way (see reference [7]). For homogeneous systems, one uses the socalled virial to calculate the configurational part of the pressure tensor, and then add that to the kinetic part. As the derivation of the virial is not trivial, we will not consider it further, except to refer to references [3, 8]. The full expression for the pressure tensor of a homogeneous system of particles (due to Irving and Kirkwood [8]) is given as: N N N 1 1 P( r, t) = mi i( t) i( t) + ij( t) ij ( t) i ( t) = V v v r F r r i= 1 2 (10.3) i= 1 j= 1 j i kinetic configurational where V is the volume, m i and v i are the mass and velocity of particle i respectively, and r ij and F ij are as defined in Module 9. Note that Eqn (10.3) is valid for atomic systems at equilibrium. Systems of molecules require some modifications to be made, as do non-equilibrium systems. Note also that the vector products are dyadic, so that the resultant expression is a tensor. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 10

The factor of ½ in the configurational component of the pressures tensor reflects the fact that the double sum ranges over all atoms twice. Thus, every pair interaction is calculated twice. This is actually an inefficient way of performing this calculation. A more efficient way, that can be coupled with the neighbour list formulation, is to just calculate interactions between pairs only once. Thus, Eqn (10.3) can be written as P 1 V N N N ri = r i= 1 i= 1 j> i ( r, t) = mivi( t) vi( t) + rij( t) Fij ( t) ( t ) (10.4) As with the calculation of the energy, the configurational component should be calculated in the force loop, whereas the kinetic component should be calculated once the velocities are updated, and then the two components added together. Problem 10.2 asks you to program Eqn(10.4) into the MD sample code provided to you. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 11

1.3 Self-diffusion coefficient Microscopic motion, such as how a molecule in a liquid diffuses, or how an adsorbed atom on a surface migrates from one site to another (or indeed through various layers) is difficult to measure experimentally. However, this motion is one of the most straightforward to observe within an MD simulation. All one need do is tag any particular atom or molecule and write out its coordinates as a time series. However, apart from visualisation, this information does not really give one much insight into the physics of how and why atoms or molecules move. A more quantitative measure is required, of which the self-diffusion coefficient is one of the most common and useful. There are two routes to calculating diffusion coefficients: via the meansquared displacement vector (originally derived by Einstein in his paper on Brownian dynamics [9]), or by what is known as a B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 12

Green-Kubo relation [3, 10, 11], in which the diffusion coefficient is related to the velocity autocorrelation function. Both expressions are formally equivalent. In this section we will only consider the Einstein method; a Green-Kubo calculation will be described in the following section on zero-shear viscosity. The Einstein expression for the self-diffusion coefficient for an isotropic fluid at equilibrium is given as 2tD= 1 r ( ) ( 0) 2 i t r i t = (10.5) 3 B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 13

The angle brackets denote a time average performed over all possible atoms (molecules) i. In actual fact, Eqn (10.5) is only strictly valid in the limit of long times, i.e. D= lim 1 r ( ) ( 0) 2 i t r i t = (10.6) t 6t Eqns (10.5) and (10.6) suggest that, after an initial transitional time, the self-diffusion coefficient should be proportional to the mean-square displacement (MSD). In fact, a plot of the MSD against t should yield a straight line, with the slope being 6D. In Problem 10.3 you are asked to write a function to calculate the MSD, insert it into the MD code, and then plot the MDS against time to extract the diffusion coefficient for an MD simulation. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 14

1.4 Zero-shear viscosity The other transport coefficients of great importance are the viscosity coefficients. The linear coefficients (shear, bulk and elongational) are relatively straightforward to simulate by MD (simulation of elongational flow, however, have certain technical difficulties, see refs [12-15]). These may be calculated most directly by non-equilibrium molecular dynamics (NEMD) techniques, and we will briefly touch on this in Module 12. However, the equilibrium, zero-shear viscosity may never-the-less be calculated by use of one of the Green-Kubo relations mentioned in section 1.3. Before we comment on the Green-Kubo relation, let us recap briefly what we mean by shear viscosity. Consider a fluid confined between parallel plates, as depicted in Figure 10.2. The top plate is dragged in the x-direction with force F, such that a uniform linear velocity profile is established, with v 0 being the velocity of the top plate. The plates are separated in the y-direction by L and have surface area S. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 15

y z v 0 x S F L Figure 10.2. Depiction of a fluid confined between parallel plates sheared in the x-direction. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 16

At steady-state, and under laminar flow conditions, the force per unit area exerted by the fluid in the x-direction on the upper plate is equal in magnitude, but opposite in direction, to the force used to drag the plate, F. Thus, the force per unit area exerted on the plate by the fluid in the x- direction is just F/S. However, Newton s law of viscosity states that this force per unit area is proportional to the velocity gradient, in this case v 0 /L, the constant of proportionality being termed the shear viscosity. Thus, F = η S v 0 L More generally, if the velocity gradient is defined as v x γ then Eqn. (10.7) may be written as σ xy = ηγ where σ xy = F/S is just the shear stress experienced by the plate (Note: σ xy = -P xy ). (10.7) (10.8) B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 17

The above discussion on shear viscosity is based purely on macroscopic Newtonian mechanics. However, in statistical mechanics it turns out that these mechanical transport coefficients are linked to statistical fluctuations in dissipative fluxes. The theory of these fluctuations may already have been covered in your statistical mechanics courses, and we will not go into it any further in this course. Detailed discussions may be found in references [3, 10]. Instead, we will simply quote the Green- Kubo expression for the zero-shear viscosity, defined as η 0 = = lim η γ 0 lim γ 0 σ xy γ (10.9) B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 18

and then show how to compute this quantity. The Green-Kubo expression for the zero-shear viscosity is η V = dt Pxy( t) Pxy ( 0) kt (10.10) B 0 The angle brackets represent a time average. The integrand is simply the time autocorrelation function of the shear stress at time t, with itself at time 0. The expression looks simple enough, however we note that the statistics can be vastly improved by observing that at equilibrium every value of P xy (t) may be used as a new origin at t = 0. To illustrate this, consider the diagram below. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 19

t 0 t 1 t 2 t 3 t n.. t 0 t 1 t 2 t 3 t n.. Figure 10.3. Schematic representation of time correlation functions. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 20

In the above diagram, we represent the time series of some arbitrary calculated phase variable B(t), depicted as squares. Each value of B(t) is separated in time by t. In the specific case of shear viscosity, B(t) = P xy (t). The time series is listed alongside itself to illustrate the fact that we are forming an autocorrelation function in time, i.e., B(t m ) x B(t n ). The arrows represent the products, B(t 0 ) x B( t n ). Thus, the purple arrows represent all possible contributions to the term B(t 0 ) x B( t = 0), i.e. B(t 0 ) x B(t 0 ). The other arrows represent the following contributions: Blue: B(t 0 ) x B( t ) Green: B(t 0 ) x B(2 t ) Orange: B(t 0 ) x B(3 t ). Red: B(t 0 ) x B(n t ) B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 21

Note that there are n terms involving B(t 0 ) x B(t 0 ), thus the n contributions to this term are summed and then divided by n to give the correct average value. Similarly, there are n-1 terms involving B(t 0 ) x B( t ), and these terms, when summed are divided by n-1 for correct averaging. So on for all other terms, noting that there is only one term involving B(t 0 ) x B(n t ). The code on the following pages calculates these time correlation functions. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 22

// calculate brute-force time correlation function for Green-Kubo viscosity int i; correl->storepxy[ncorrel] = pt[1,2]; // store Pxy(t) for (i=1; i<=ncorrel; i++) { correl->pxy[i] += correl->storepxy[ncorrel-i]*correl->storepxy[ncorrel]; } In the above brute-force code, correl is a data structure that contains relevant data for the correlation functions. The variable storepxy is a time history of all P xy (t) values, and the variable Ncorrel is the number of correlation sums (i.e., the equivalent of the variable n in Figure 10.3). Note that pxy, storepxy and Ncorrel are all dynamically updated at each processing time step. Furthermore, pxy[i] must then be averaged over the correct number of terms (e.g, Ncorrel for i = 1, Ncorrel-1 for i = 2, etc.), and finally integrated over all time to give the viscosity coefficient. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 23

The main problem with this seemingly simple few lines of code is that Ncorrel grows in time. After Ncorrel reaches a few thousand, the time spent in this loop grows significantly, as do the memory requirements of pxy and storepxy. Thus, the simple brute-force algorithm actually becomes impractical, and is in fact often unfeasible. This is particularly so when there are double-loops involved, in which case the time increases with Ncorrel*Ncorrel; the shear stress correlation function is actually one of the least computationally intensive correlation functions to calculate. A better, faster, and less memory intensive method is to use a so-called shift register algorithm. In this case one makes an initial assumption about how big Ncorrel will be. Because time correlation functions will in general decay to zero as t, this allows us to predetermine the size of Ncorrel. Once <B(t 0 ) x B(n t )> 0 we no longer need to calculate any terms higher than n (Ncorrel). The shift-register algorithm is demonstrated as follows. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 24

// calculate time correlation functions for Green-Kubo relations by shift-register algorithm int i; // update the shift registers params->nshift += 1; for (i=1; i<ncorrel; i++) { correl->shift[i] = correl->shift[i+1]; } correl->shift[ncorrel] = pt[1][2]; // accumulate forward time correlation functions if(params->nshift > Ncorrel) { params->ncorfn += 1; for (i=1; i<=ncorrel; i++) { correl->pxy[i] += correl->shift[i]*correl->shift[1]; } } B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 25

We observe now that Ncorrel is fixed. In the first loop, the values of P xy (t) stored are being shifted down by one space in the array shift. Thus, the shift register is first built up by the time Nshift =Ncorrel. Once this happens, the actual correlation functions are calculated. The next time that the routine is called from the main program we lose all information stored in shift[1]. But as Ncorrel is now fixed, this information is no longer required in any case; it has already been stored in the variable pxy[1]. Thus, we no longer need a complete history of P xy (t), rather we only need to store values of P xy (t) within a range of Ncorrel. Note also that the number of correlation functions is stored in the variable Ncorfn, so each element of the array pxy[] must be divided by this amount. Once again the array is integrated to obtain the shear viscosity. In Problem 10.4 you are asked to code the shift-register algorithm into your MD program, and then write a small algorithm to (a) perform the average over the correlation function, and (b) numerically integrate the function to obtain the zero-shear viscosity. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 26

1.5 Pair distribution function The pair distribution function, g(r), gives the probability of locating pairs of atoms separated by a distance r, relative to that for a completely random distribution at the same density (i.e., the ideal gas). It is defined as V N N N ( ) = δ 2 ( ij ) g r r r (10.11) i= 1 j i In a computer simulation, the delta function is replaced by a function that is finite (say, given the value 1) over a small range of separations, and a histogram is accumulated over time of all pair separations that fall within this range. A typical plot of g(r) vs. r is given in Figure 10.4 below. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 27

3.5 3.0 2.5 g(r) 2.0 1.5 1.0 0.50 0.0 0 1 2 3 4 5 6 7 8 r Figure 10.4. g(r*) vs r* for a 2-dimensional WCA fluid at the state point (T*, ρ ) = (0.722, 0.844). B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 28

Note that the various peaks in the distribution denote nearest neighbour shells, and that in the limit as r, g(r) 1. g(r) is effectively a measure of fluid structure, but is particularly important because all thermodynamic quantities may be expressed as some function it. We do not have time to delve into its usefulness at this stage, but refer you instead to references [1, 10] for further reading. Of particular relevance for this lecture course is how we go about calculating g(r). An algorithm to do this is given on the following page. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 29

// equilibrium g(r) calculation int i, j, bin; double prefact, rx, ry, rz, rsq, rij, rupper, rlower, nideal, hist[numbin], gofr[numbin]; prefact = 4*PI*params->Rho/3; for(i = 1; i <= NUMBIN; i++) { hist[i] = 0; // initialise histogram; NUMBIN is size of array } // calculate g(r) first step for(i = 1; i <= params->np-1; i++) { for (j = i+1; j <= params->np; j++) { // calculate minimum image distances rx = phase->x0[i] - phase->x0[j]; ry = phase->y0[i] - phase->y0[j]; rz = phase->z0[i] - phase->z0[j]; B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 30

// apply pbcs pbcs(&params->cube, &rx, &ry, &rz); } } rsq = rx*rx + ry*ry + rz*rz; rij = sqrt(rsq); bin = int(rij/params->delr) + 1; if(bin <= NUMBIN) hist[bin] += 2; // add 2 to account for all pairs // calculate normalised g(r) for (i = 1; i <= NUMBIN; i++) { rlower = (i-1)*params->delr; rupper = rlower + params->delr; nideal = prefact*(pow(rupper,3) - pow(rlower,3)); gofr[i] = float(hist[i])/(params->np*nideal); } B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 31

In the above we note that the histogram is incremented by 2 (not 1!) whenever the bin number max bin number to account for both pairs (i, j) and (j, i) being counted simultaneously. delr is the bin width, so we are counting pairs within a range r to r + δr. Note that the number of atoms in this interval for an ideal gas is 4πρ ( ) 3 3 Nideal = r+ δ r r 3 (10.12) Finally, note that we average over all atoms. We also need to average over the run time (sampling time), but this is not coded in the above routine. It is a simple matter of dividing gofr[i] by the total sampling time. In problem 10.5 you are asked to place the above code in your MD program within a separate function, calculate the time average and then plot the resulting g(r) distribution. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 32

Topic 2: Averaging and output of results 2.1 Time averaging To date we have concentrated on (a) setting up our MD program, and (b) calculating certain material properties of interest to us. We now spend a little effort in discussing how to obtain statistically meaningful results. Many of the algorithms thus far described only showed how to calculate certain physical properties, instantaneously, i.e., at time t. However, statistically meaningful results are only obtained by time averaging over sufficiently long periods of time (see Module 6 for discussion). As a demonstration of this, consider some phase variable, B(t), defined as N ( ) = ( ) δ ( ) B t B t t t i= 1 i i (10.13) Over some arbitrary sampling interval, τ, the time average <B(t)> is computed as τ 1 = τ ( ) dtb( t) B t 0 (10.14) B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 33

Topic 2: Averaging and output of results (cont.) Substituting (10.13) into (10.14) gives τ N 1 B t dt Bi t t t τ ( ) = ( ) δ ( ) 0 i= 1 0 i= 1 N 1 = dtbi t t t τ = 1 τ N ( ) δ ( ) Bi ( ti) (10.15) i= 1 τ i i Thus, clearly, all quantities we calculate must be time averaged, within the actual simulation, to give physically meaningful results for output. It is good practice to calculate totals of quantities as they are being computed (i.e., sum of all B i (t i )), and then have a separate averaging routine that takes the time average of the totals. This process, as well as use of data files for storage, as the objective of problem 10.6. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 34

Topic 2: Averaging and output of results (cont.) 2.2 Equilibration Before actually storing your data for averaging, you need to be sure that the system is equilibrated, i.e., that any phase variable B(t) is genuinely time-independent. Note that while B(t) may fluctuate about some mean value, its time average should remain constant. It is thus useful to have a running time series of some quantity and check that it genuinely is fluctuating about its mean value. Such useful quantities might be the configurational energy or pressure. Note that (for constant energy simulations) the energy is a constant of the motion, even if the system is not yet at equilibrium; however the configurational energy (i.e., the potential energy) will change from its initial value to its equilibrium value. Once you are sure that your system is at equilibrium, then you can start to accumulate your data. This is best served by having switches in your program that allow you to control whether or not you wish the output of your simulation to be stored for averaging purposes, and is an additional objective of problem 10.6. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 35

Topic 2: Averaging and output of results (cont.) 2.3 Estimating errors There are a number of ways in which this may be done. First, one may run the simulation N different times (each time starting at a different configuration), then calculate the mean and standard deviations (σ) of all quantities you are interested in over these N independent runs. The error in the mean is then given by standard error σ = (10.16) N An alternative (and equivalent) method would be to divide your (long!) simulation into n blocks of τ timesteps and take averages over these blocks. One then calculates the mean of the block averages, their standard deviation, and their standard error by Eqn (10.16). These are the two simplest and most straightforward ways to estimate errors. There are more sophisticated techniques, which you may find an excellent description of in Chapter 6 of reference [1]. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 36

Topic 2: Averaging and output of results (cont.) 2.4 Is your simulation running correctly? Just because your program is running does not mean that it is running correctly! There are few exceptions to the rule that the bug-free program does not exist! No matter how careful you are, there will probably always be bugs in your code. Your task is to ensure that whatever bugs may exist in your code, they are not significant in terms of the validity of your results and are genuinely minor. While debugging a code is essentially a trial-and-error process, there here are a few helpful tips to check that your program is running correctly: Are total energy and momentum conserved with all external and constraint forces switched off? If not, then be sure there is a bug! Check that your equations of motion are being integrated correctly (perhaps your integration step-size is too large?), and make sure that you are calculating your forces correctly. Check that your neighbour list is not missing interactions. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 37

Topic 2: Averaging and output of results (cont.) Program crashes when starting from an initial lattice. Check the magnitude of the forces. If they are too large, then your lattice configurations may be incorrect, and you may be placing atoms too close together. Repulsive forces are thus too large and the integrator is unable to correctly solve the equations of motion. If that is not the problem, check that your forces are being calculated correctly, and check the neighbour list routine. Temperature is not held constant with the thermostat switched on. Check equations of motion and your thermostat constraint calculation! Program runs without crashing, and instantaneous configurational energy is constant, yet averaged quantities seem to grow in time. Probably due to incorrect averaging. Check that you are averaging over the correct number of samples, and totals are accumulated over that number of samples. Everything seems to work well, but how do I know the results are correct? Compare some sample runs with known (correct!) data from the available scientific literature. If you are able to reproduce existing data, then your code is probably okay, and you are ready to do some innovative science! B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 38

Problems Problem 1 In your MD program, code exists to calculate both the potential and kinetic energies, as well as the total energy. Write the energies to file as a time series, after every Ntime timesteps (Ntime is input from your input file). Run the program in constant energy mode (i.e., switch the thermostat off) and note what happens to the kinetic, potential and total energies. How do you know when the system has reached thermodynamic equilibrium? Problem 2 Program Eqn (10.4) into your MD code. As with Problem 1, output a time series of all the pressure tensor components after every Ntime timesteps, but now run at constant temperature, at the state point (T, ρ) = (0.722, 0.8442). What are the only non-zero components? Note that the pressures is defined as 1 p = Tr P 3 ( ) From this definition, estimate the equilibrium pressure of your fluid. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 39

Problems (cont.) Problem 3 Write a function to calculate the mean-squared displacement (MSD) for your MD system and insert it into the MD program. Output the MDS as a function of time every Ntime timesteps, and extract the self-diffusion coefficient from the slope of the plot of MDS vs. time. What is the shape of the curve at small times? Use the same state point as in Problem 2, and use this state point in all subsequent problems. Problem 4 (a) Using the shift-register algorithm supplied to you in section 1.4, calculate the correlation function <P xy (0)P xy (t)>. Make sure the algorithm is placed in a separate function. Perform the correct time averaging of <P xy (0)P xy (t)> and plot it out as a function of time. Vary Ncorrel to ensure that the correlation function decays to zero after Ncorrel timesteps. Make sure your run length is quite long to ensure you have reasonably good statistics, at least several hundred thousand timesteps. However, ignore the first 50000 timesteps until the system equilibrates. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 40

Problems (cont.) Problem 4 (cont.) (b) Numerically integrate <P xy (0)P xy (t)> using a simple trapezoidal scheme and extract the zero-shear viscosity. Problem 5 Place the code supplied in section 1.5 to calculate g(r) into your MD program in a separate function. Make sure you perform correct time averaging and then plot out g(r) vs r at the end of a 100000 timestep run. Make sure you discard the data before the system equilibrates (eg, discard the first 50000 timesteps). How does your g(r) compare to that in Figure 10.4? Why is there a difference at all, considering we are at the same state point? B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 41

Problems (cont.) Problem 6 You now have quite a few quantities that are calculated in your MD program. The last remaining task is to form totals of your quantities, and then time averages. The structure Totals contains the array total that does this for some quantities. The function accumtotals accumulates totals of quantities, whereas the function averagetotals forms averages and writes these to file. In the total and averaging functions, include all the new variables you have calculated, i.e., components of the pressures tensor, MSD and correlation function time series data, and g(r) as a function of r. Note that averagetotals currently only writes to a single file output.dat. Modify it so that it writes to other files to store the time series data for your MSD (diffusion) and correlation function data, as well as your g(r) data. Introduce new data structures as you require. Always make sure your MD program remains highly modularised. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 42

Problems (cont.) Observe that the switch Type has three options: start at Type = 1 (starting from lattice), then run with Type = 2 to equilibrate. Finally, for production runs, set Type to 3. Note that Type 3 runs read in the data from the previous run and continue averaging, whereas Type 2 reads from a previous run but re-zeros the totals so that one accumulates totals from scratch. The program already uses these switches, in that they are set up to initialise the total array as well as write data to binary files and reads them in if Type is either 2 or 3. You just need to ensure that your averaging is compatible with the different Type runs and that all new storage variables have been output as binaries so that they may be read in for Type 2 or 3 runs. B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 43

Reading Material and References 1. M.P. Allen and D.J. Tildesley, Computer Simulation of Liquids (Oxford University Press, New York, 1987). 2. D. Frenkel and B. Smit, Understanding Molecular Simulation: From Algorithms to Applications (Academic Press, San Diego, 1996). 3. D.J. Evans and G.P. Morriss, Statistical Mechanics of Nonequilibrium Liquids (Academic Press, London, 1990). 4. R.J. Sadus, Molecular Simulation of Fluids: Theory, Algorithms and Object-Orientation (Elsevier, Amsterdam, 1999). 5. D.C. Rapaport, The Art of Molecular Dynamics Simulation (Cambridge University Press, Cambridge, 1995). 6. J.D. Weeks, D. Chandler and H.C. Andersen, J. Chem. Phys. 54, 5237 (1971). 7. B.D. Todd, D.J. Evans and P.J. Daivis, Phys. Rev. E. 52(2), 1627 (1995). 8. J.H. Irving and J.G. Kirkwood, J. Chem. Phys. 18, 817 (1950). B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 44

Reading Material and References (cont.) 9. A. Einstein, Ann. Phys. 17, 549 (1905). 10. D. A. McQuarrie, Statistical Mechanics (Harper and Row, New York, 1976). 11. J.-P. Hansen and I. R. MacDonald, Theory of Simple Liquids (Academic Press, London, 1986) 12. B.D. Todd and P.J. Daivis, J. Chem. Phys. 107, 1617 (1997). 13. B.D. Todd and P.J. Daivis, Phys. Rev. Lett. 81, 1118 (1998). 14. B.D. Todd and P.J. Daivis, Comput. Phys. Commun. 117, 191 (1999). 15. B.D. Todd and P.J. Daivis, J. Chem. Phys. 112, 40 (2000). B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology 45