This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
SMA5233 SMA5233 Particle Methods and Molecular DynamicsParticle Methods and 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..
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:
...21...
21 22
ttattvtrttrttrtrttr
Rewrite in discrete form (where n is at time t and n+1 at time t+t):
...21 3
12 tOt
mtvrr nF
nnn
Integration algorithm: implementation of the above equation
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 through 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 ordinary 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 vector of certain differential equations can be cast in this form if we allow x to be a vector of certain dimensions. dimensions.
– For example, a second-order differential equation can be rewritten as a set of two 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 solve the 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).above equation, keeping in mind that x could mean (x1, x2, ... , xd).
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.
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 two 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 second-points, once at the starting point, and once at the midpoint. This lead to the second-order Runge-Kutta or midpoint method: 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)
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.
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:
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:
trr
tttrtrttvv nn
n
1
21
)()(2 t
rrt
trttrttvv nnn
1
21
)()(2
22
12
1
nn vv
nv Instantaneous velocity at time t
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.
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
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.
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.
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:
Clearly, the algorithm is accurate to first order in h.
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.
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.
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:
• 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.
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.
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.
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
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;}
{ 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;
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