sma5233 particle methods and molecular dynamics lecture 4: integration methods a/p chen yu zong tel:...

42
SMA5233 SMA5233 Particle Methods and Molecular Particle Methods and Molecular Dynamics Dynamics Lecture 4: Integration Methods Lecture 4: Integration Methods A/P Chen Yu Zong A/P Chen Yu Zong Tel: 6516-6877 Tel: 6516-6877 Email: Email: [email protected] [email protected] http:// http:// bidd.nus.edu.sg bidd.nus.edu.sg Room 08-14, level 8, S16 Room 08-14, level 8, S16 National University of Singapore National University of Singapore

Upload: giles-lambert-briggs

Post on 28-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

SMA5233 SMA5233 Particle Methods and Molecular DynamicsParticle Methods and Molecular Dynamics

Lecture 4: Integration MethodsLecture 4: Integration Methods

A/P Chen Yu ZongA/P Chen Yu Zong

Tel: 6516-6877Tel: 6516-6877Email: Email: [email protected]@nus.edu.sg

http://http://bidd.nus.edu.sgbidd.nus.edu.sgRoom 08-14, level 8, S16 Room 08-14, level 8, S16

National University of SingaporeNational University of Singapore

Page 2: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

22

NEWTONIAN MOLECULAR DYNAMICS

Fi=force on particle i, m=mass, a=acceleration and U=potential energy

Some important properties of Newton's equation of motion are: Conservation of energy Conservation of linear momentum Conservation of angular momentum Time reversibility

iiiiii rmamUF..

Page 3: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

33

Computational algorithms:

Numerical procedure to integrate the differential equation

Finite-difference approach: molecular coordinates and velocities at time t=t+t, obtained from coordinates and velocities at time t (t is the time interval).

Taylor expansion:

...2

1...

2

1 22

ttattvtrttrttrtrttr

Rewrite in discrete form (where n is at time t and n+1 at time t+t):

...2

1 31

2 tOtm

tvrr nFnnn

Integration algorithm: implementation of the above equation

Page 4: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

44

Runge-Kutta method Runge-Kutta method

This is a very straightforward but less accurate method if restricted to a This is a very straightforward but less accurate method if restricted to a particular orderparticular order

Method Method

– Our task is to solve the differential equation: dx/dt = f(t, y), x(t0)= x0 Our task is to solve the differential equation: dx/dt = f(t, y), x(t0)= x0

– That is, given the function f(t,y), find the function x(t) such that it passes That is, given the function f(t,y), find the function x(t) such that it passes through the point (t0, x0), whose derivative is the given function. Any set of through the point (t0, x0), whose derivative is the given function. Any set of ordinary differential equations can be cast in this form if we allow x to be a ordinary differential equations can be cast in this form if we allow x to be a vector of certain dimensions. vector of certain dimensions.

– For example, a second-order differential equation can be rewritten as a set of For example, a second-order differential equation can be rewritten as a set of two first-order differential equations. Thus it is suffice to just discuss how to two first-order differential equations. Thus it is suffice to just discuss how to solve the above equation, keeping in mind that x could mean (x1, x2, ... , xd).solve the above equation, keeping in mind that x could mean (x1, x2, ... , xd).

Page 5: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

55

Runge-Kutta method Runge-Kutta method

Clearly, the most obvious scheme to solve the above equation is to Clearly, the most obvious scheme to solve the above equation is to replace the differential by finite differences: replace the differential by finite differences:

dt = h dt = h

dx = x(t+h) - x(t) dx = x(t+h) - x(t)

We then get the Euler method or first-order Runge-Kutta formula: We then get the Euler method or first-order Runge-Kutta formula:

x(t+h) = x(t) + h f(t, x(t)) + O(h2) x(t+h) = x(t) + h f(t, x(t)) + O(h2)

The term first order refers to the fact that the equation is accurate to first The term first order refers to the fact that the equation is accurate to first order in the small step size h, thus the (local) truncation error is of order order in the small step size h, thus the (local) truncation error is of order h2. The Euler method is not recommended for practical use, because it is h2. The Euler method is not recommended for practical use, because it is less accurate in comparison to other methods and it is not very stable. less accurate in comparison to other methods and it is not very stable.

Page 6: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

66

Runge-Kutta method Runge-Kutta method Improvement of accuracyImprovement of accuracy

The accuracy of the approximation can be improved by evaluating the function f at The accuracy of the approximation can be improved by evaluating the function f at two points, once at the starting point, and once at the midpoint. This lead to the two points, once at the starting point, and once at the midpoint. This lead to the second-order Runge-Kutta or midpoint method: second-order Runge-Kutta or midpoint method:

k1 = h f(t, x(t)) k1 = h f(t, x(t))

k2 = h f(t+h/2, x(t)+k1/2) k2 = h f(t+h/2, x(t)+k1/2)

x(t + h) = x(t) + k2 + O(h3) x(t + h) = x(t) + k2 + O(h3)

However, the most popular Runge-Kutta formula is the following fourth-order one: However, the most popular Runge-Kutta formula is the following fourth-order one:

k1 = h f(t, x(t)) k1 = h f(t, x(t))

k2 = h f(t+h/2, x(t)+k1/2) k2 = h f(t+h/2, x(t)+k1/2)

k3 = h f(t+h/2, x(t)+k2/2) k3 = h f(t+h/2, x(t)+k2/2)

k4 = h f(t+h, x(t)+k3) k4 = h f(t+h, x(t)+k3)

x(t + h) = x(t) + k1/6 + k2/3 + k3/3 + k4/6 + O(h5) x(t + h) = x(t) + k1/6 + k2/3 + k3/3 + k4/6 + O(h5)

Page 7: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

77

Runge-Kutta method Runge-Kutta method

The fourth-order Runge-Kutta method is suitable for many The fourth-order Runge-Kutta method is suitable for many applications for systems involving few degrees of freedom. applications for systems involving few degrees of freedom. But it was almost never used for molecular dynamics But it was almost never used for molecular dynamics involving many degrees of freedom. Reasons for this: involving many degrees of freedom. Reasons for this:

– Computationally demanding Computationally demanding – Four evaluations of the right-hand side of the equationsFour evaluations of the right-hand side of the equations– Lacks the time-reversal symmetry of the Newton Lacks the time-reversal symmetry of the Newton

equations. equations.

Page 8: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

88

VERLET ALGORITHMThis algorithm is particularly suited for molecular dynamics. It has been widely used in many areas from simulation of liquids and solids to biological molecules.

Method

Our problem is to solve the set of Newton's equations:  

m d2x / dt2 = F  

where x is the position vector of a particle, m is the mass of this particle, and F is the total force acting on this particle. We'll discuss the form of the forces later on. To get approximate difference formula for derivatives, let's look at the Taylor expansion of the function x(t) with h=dt:  

x(t + h) = x(t) + h x'(t) + h2/2 x''(t) + h3/6 x'''(t) + O(h4)  Replacing h by -h, we get:  

x(t - h) = x(t) - h x'(t) + h2/2 x''(t) - h3/6 x'''(t)  + O(h4)

Page 9: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

99

VERLET ALGORITHM

...2

1 31

2 tOtm

tvrr nFnnn

...

2

1 31

2 tOtm

tvrr nFnnn

Sum the forward and backward expansions:

...2 411

2 tOtm

rrr nFnnn

1. Use rn to calculate Fn

2. Use rn, rn-1 and Fn (step1) to calculate rn+1

Subtract the forward and backward expansions:

211

2tO

t

rrv nn

n

Propagate velocities

Page 10: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

1010

Advantages: 1. Integration does not require the velocities, only position

information is taken into account. 2. Only a single force evaluation per integration cycle. (Force

evaluation is the most computationally expensive part in the simulation).

3. This formulation, which is based on forward and backward expansions, is naturally reversible in time (a property of the equation of motion).

Disadvantages: 1. The velocities, which are required for energy evaluation are

calculated in an approximate manner only through the equation: vn = (rn+1 - rn-1)/2 t . (large errors)

2. Need to know rn+1 to calculate vn

VERLET ALGORITHM

Page 11: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

1111

Verlet Algorithm Flow DiagramVerlet Algorithm Flow Diagram

r

v

F

t-t t t+t

Given current position and position at end of previous time step

...2 411

2 tOtm

rrr nFnnn

Page 12: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

1212

Verlet Algorithm Flow DiagramVerlet Algorithm Flow Diagram

r

v

F

t-t t t+t

Compute the force at the current position

...2 411

2 tOtm

rrr nFnnn

Page 13: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

1313

Verlet Algorithm Flow DiagramVerlet Algorithm Flow Diagram

r

v

F

t-t t t+t

Compute new position from present and previous positions, and present force

...2 411

2 tOtm

rrr nFnnn

Page 14: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

1414

Verlet Algorithm Flow DiagramVerlet Algorithm Flow Diagram

r

v

F

t-2t t-t t t+t

Advance to next time step, repeat

...2 411

2 tOtm

rrr nFnnn

Page 15: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

1515

LEAP FROG INTEGRATOR

tvrrnnn

211 t

mvv nF

nn

2

12

1

Evaluate velocities at the midpoint of the position evaluations andVice versa.

Where v n+1/2 is the velocity at t+(1/2)t

1. Use rn to calculate Fn

2. Use Fn and vn-1/2 to calculate v n+1/2

3. Use rn and vn+1/2 to calculate r n+1

Define:

t

rr

t

ttrtrttvv nn

n

1

21

)()(

2 t

rr

t

trttrttvv nn

n

1

21

)()(

2

22

12

1

nn vv

nv Instantaneous velocity at time t

Page 16: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

1616

Advantages: 1. Improved evaluation of velocities. 2. Direct evaluation of velocities gives a useful handle for controling the temperature in the simulation. 3. Reduces the numerical error problem of the Verlet algorithm. Here O(dt1) terms are added to O(dt0) terms.

Disadvantages: 1. The velocities at time t are still approximate. 2. Computationally a little more expensive than Verlet.

Leap Frog

Page 17: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

1717

Leapfrog Algorithm Flow DiagramLeapfrog Algorithm Flow Diagram

r

v

F

t-t t t+t

Given current position, and velocity at last half-step

tvrrnnn

211 t

mvv nF

nn

2

12

1

Page 18: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

1818

Leapfrog Algorithm Flow DiagramLeapfrog Algorithm Flow Diagram

r

v

F

t-t t t+t

Compute current force

tvrrnnn

211 t

mvv nF

nn

2

12

1

Page 19: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

1919

Leapfrog Algorithm Flow DiagramLeapfrog Algorithm Flow Diagram

r

v

F

t-t t t+t

Compute velocity at next half-step

tvrrnnn

211 t

mvv nF

nn

2

12

1

Page 20: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

2020

Leapfrog Algorithm Flow DiagramLeapfrog Algorithm Flow Diagram

r

v

F

t-t t t+t

Compute next position

tvrrnnn

211 t

mvv nF

nn

2

12

1

Page 21: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

2121

Leapfrog Algorithm Flow DiagramLeapfrog Algorithm Flow Diagram

r

v

F

t-2t t-t t t+t

Advance to next time step,repeat

tvrrnnn

211 t

mvv nF

nn

2

12

1

Page 22: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

2222

VELOCITY VERLET INTEGRATOR

Stores positions, velocities and accelerations all at the same time t

2

2

11 t

mtvrr nF

nnn

21

2

11 t

mmvv nn FF

nn

Page 23: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

2323

Velocity Verlet Algorithm Velocity Verlet Algorithm Flow DiagramFlow Diagram

r

v

F

t-t t t+t

Given current position, velocity, and force

2

2

11 t

mtvrr nF

nnn

Page 24: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

2424

Velocity Verlet Algorithm Velocity Verlet Algorithm Flow DiagramFlow Diagram

r

v

F

t-t t t+t

Compute new position

2

2

11 t

mtvrr nF

nnn

Page 25: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

2525

Velocity Verlet Algorithm Velocity Verlet Algorithm Flow DiagramFlow Diagram

r

v

F

t-t t t+t

Compute velocity at half step

Page 26: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

2626

Velocity Verlet Algorithm Velocity Verlet Algorithm Flow Diagram Flow Diagram

r

v

F

t-t t t+t

Compute force at new position

Page 27: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

2727

Velocity Verlet Algorithm Velocity Verlet Algorithm Flow Diagram Flow Diagram

r

v

F

t-t t t+t

Compute velocity at full step

Page 28: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

2828

Velocity Verlet Algorithm Velocity Verlet Algorithm Flow Diagram Flow Diagram

r

v

F

t-2t t-t t t+t

Advance to next time step, repeat

Page 29: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

2929

Symplectic algorithms Symplectic algorithms

What is symplectic algorithm?   In the Verlet algorithm, our basic dynamic variables are the coordinates of the particles. And we have a somewhat inconvenient situation that the initial conditions are specified as the initial positions and initial velocities, while the difference scheme involves position only. It is possible to reformulate the Newtonian mechanics so that both positions and momenta are the basic dynamic variables.

This formulation is called Hamiltonian dynamics (which is equivalent to Newtonian dynamics). A Hamiltonian system is completely described by the Hamiltonian function (kinetic energy plus potential energy):  

H(p,q) = K + V

Page 30: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

3030

Symplectic algorithms Symplectic algorithms

A Hamiltonian system is completely described by the Hamiltonian function (kinetic energy plus potential energy):    

H(p,q) = K + V  

together with Hamilton's equations of motion:    

dpi/dt = - dH/dqi,    dqi/dt = dH/dpi  

where H is considered to be a function of the generalized coordinates qi and generalized momentum pi. The derivative on H is a partial derivative. The index i labels the degree of freedom, not the particle.   Algorithms for solving this set of  Hamilton's equations of motion are symplectic algorithms.

Page 31: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

3131

Symplectic algorithms Symplectic algorithms

Systems for which a symplectic algorithm can be derived:   The symplectic algorithm can be derived if the Hamiltonian takes a simple form:  

H = K + V = sumi pi2/2mi + V(q1, q2, ...)  

This is certainly the case if we use Cartesian coordinates and the interactions between particles depend only on the positions of the particles. With this particular form of Hamiltonian, Hamilton's equations can be written as:  

dpi/dt = - dV/dqi = Fi dqi/dt = dK/dpi = pi/mi

This set of equations reduce to Newton's equations if one eliminates the variable pi.

Page 32: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

3232

Symplectic algorithms Symplectic algorithms Algorithm A   We use an Euler type approximation for the momentum derivatives, and then a similar Euler algorithm for the position derivatives. However, for the positions, we use immediately the new result for the momenta. Thus, it is not exactly an Euler algorithm. For brevity, we'll use a vector notation:  

p = ( p1, p2, ... ) q = ( q1, q2, ... ) F = ( F1, F2, ... )  

and assume that all particles have the same mass m. With these notations and assumption, we have:  

p(t + h) = p(t) + hF(q(t)) q(t + h) = q(t) + h/m p(t + h)  

Clearly, the algorithm is accurate to first order in h.

Page 33: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

3333

Symplectic algorithms Symplectic algorithms Algorithm B     We do exactly the same thing as in algorithm A, except that we reverse the order of calculation. The new position is found first.    

q(t + h) = q(t) + h/m p(t) p(t + h) = p(t) + hF(q(t + h))  

If you compare algorithms A and B, you will find that it is not exactly the same. It is very interesting to note that both algorithm A and B are in fact mathematically equivalent to the Verlet algorithm if you eliminate the momenta from the equations. However, they have better roundoff error control over the plain Verlet algorithm.  

Page 34: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

3434

Symplectic algorithms Symplectic algorithms Algorithm C (second-order symplectic algorithm or velocity form of the Verlet algorithm):   This is the most widely used algorithm that we are really interested. This algorithm is derived by dividing the step size h into two, for the first half step size h/2, we use the algorithm A, for the second half of the step, we use the algorithm B. In total, we have full step size h. That is, use the formula for algorithm A but set h/2. We get equations relating the quantities at time t to time t + h/2. Then we use the equations for algorithms B with step size again h/2, but the time is for t+h/2 to t + h. We get four equations:  

p(t + h/2) = p(t) + h/2 F(q(t)) q(t + h/2) = q(t) + h/2m p(t + h/2) q(t + h) = q(t + h/2) + h/2m p(t + h/2) p(t + h) = p(t + h/2) + h/2 F(q(t + h))

Page 35: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

3535

Symplectic algorithms Symplectic algorithms • Now we eliminate the intermediate quantities evaluated at time t + h/2,

we get the final result for the symplectic algorithm:  

q(t + h) = q(t) + h/m p(t) + h2/2m F(q(t)) p(t + h) = p(t) + h/2 [ F(q(t)) + F(q(t + h)) ]

  • Note that the coordinates q have to be evaluated first since the new

values at time t+h are used to evaluate the forces. • The algorithm C is also second-order in step size h. From the derivation

it is not obvious that it is necessarily superior than Verlet algorithm. However, the symplectic algorithm has several advantages:  

It is time-reversible The system can be started naturally, with initial position

q0 and initial momentum p0 = mv0

  The symplectic algorithms A, B, and C have one important property

that share with the original Hamiltonian system---they preserve Poincare invariants.

Page 36: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

3636

Symplectic algorithms Symplectic algorithms This last property of Hamiltonian system is usually discussed in advanced

mechanics or dynamical system courses. It is about the phase space (p,q) flow property. A solution with initial condition (p0,q0) of the Hamiltonian system p=p(t,p0,q0), q = q(t,p0, q0) can be viewed as a map in the phase space from the point (p0,q0) to the point (p,q), where the time parameter t is taken as some constant. The map is also called a canonical transformation, or symplectic transformation.

One of the series of Poincare invariants is the phase space volume. Certain region of points of volume V0 can be mapped to a set of new points forming volume V. Invariance means V=V0. This is called Liouville theorem.

Not only is the volume in phase space invariant under the mapping of the dynamics, but also there are other lower dimensional objects (hypersurfaces) which are invariant. The symplectic algorithms preserve this invariant property exactly.

Page 37: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

3737

Example MD Integration CodeExample MD Integration CodeVerlet.C:

A C++ program that implements the Verlet algorithm to solve the Harmonic Oscillator problem. To run this program, it must first be compiled.

To compile this program, name it as a file called "Verlet.C". On most machines, the following command will work to compile this program: "g++ Verlet.C -o Verlet -lm".

This command should produce an executable file called "Verlet". This file can be run from the command line: "./Verlet 0.01". This will run the Verlet program with a time-step of 0.01 and output the results to standard output.

To plot the results, the output should first be placed into a file. This can be done on Unix-based machines with the following command: "./Verlet 0.01 > Verlet.out". This will place the output into a file "Verlet.out", which can be plotted.

Page 38: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

3838

Example MD Integration CodeExample MD Integration Code#include <iostream>#include <fstream>#include <iomanip>#include <cmath>using namespace std;int main(int argc, char* argv[]){ double alpha = 1.0; // force constant double f; // force double m = 1.0; // mass double t = 0.0; // current time double x = 4.0; // current pos double xold; double xnew; double v = 0.0; // current velocity double dt = 0.1; // timestep

Page 39: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

3939

Example MD Integration CodeExample MD Integration Code double tot_time = 10000.0; // total time unsigned long nstep = (int)(tot_time/dt); //number of integration steps dt = strtod(argv[1],0); nstep = (int)(tot_time/dt); cout << t << " " << x << endl; xold = x - v * dt; for (unsigned long step=0ul; step<nstep; step++) { t = t + dt; f = -alpha * x; // force at n xnew = 2.0*x - xold + f/m * dt * dt; // position at n+1 xold = x; x = xnew; // we could save mem here by reusing xold to // the xnew value cout << t << " " << x << endl; } return 0;}

Page 40: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

4040

Example MD Integration CodeExample MD Integration Code #ifndef __FORCE_h_#define __FORCE_h_#include "VEC3D.h"#include "MDUTIL.h"using namespace std;void compute_force(const VEC3D& L,

const vector<VEC3D>& x, vector<VEC3D>& f, float& pe);

#endif

Page 41: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

4141

Example MD Integration CodeExample MD Integration Code #include "FORCE.h"void compute_force(const VEC3D& L,

const vector<VEC3D>& x, vector<VEC3D>& f, float& pe)

{ static VEC3D dx, dy; static int i, j; static float invDistSq, invDistSix, invDistTwelve; static VEC3D fpart; static int Np; // set number of particles Np Np = x.size(); // zero all the energies/forces pe = 0.0;

Page 42: SMA5233 Particle Methods and Molecular Dynamics Lecture 4: Integration Methods A/P Chen Yu Zong Tel: 6516-6877 Email: phacyz@nus.edu.sg

4242

Example MD Integration CodeExample MD Integration Codefor (i=0;i<Np;i++) f[i]=VEC3D(0.0); // Loop over all pairs of particles for (i=0;i<Np;i++) for (j=i+1;j<Np;j++) { dx = x[i] - x[j]; // vector separation dy = closest_image(dx,L); // modulo box length: nearest

image convention invDistSq = 1.0/mag2(dy); // 1/dy^2 invDistSix = invDistSq*invDistSq*invDistSq; // 1/dy^6 invDistTwelve = invDistSix*invDistSix; // 1/dy^12 pe += invDistTwelve - 2.0*invDistSix; // add contribution to potential

energy fpart = 12.0 * (invDistTwelve-invDistSix) * invDistSq * dy; f[i] += fpart; f[j] -= fpart; }};