CE 530 Molecular Simulation
Download
Report
Transcript CE 530 Molecular Simulation
1
CE 530 Molecular Simulation
Lecture 11
Molecular Dynamics Simulation
David A. Kofke
Department of Chemical Engineering
SUNY Buffalo
[email protected]
2
Review and Preview
MD of hard disks
• intuitive
• collision detection and impulsive dynamics
Monte Carlo
• convenient sampling of ensembles
• no dynamics
• biasing possible to improve performance
Molecular dynamics
•
•
•
•
•
equations of motion
integration schemes
evaluation of dynamical properties
extensions to other ensembles
focus on atomic systems for now
3
Classical Equations of Motion
Several formulations are in use
• Newtonian
• Lagrangian
• Hamiltonian
Advantages of non-Newtonian formulations
•
•
•
•
more general, no need for “fictitious” forces
better suited for multiparticle systems
better handling of constraints
can be formulated from more basic postulates
Assume conservative forces
F U
Gradient of a scalar potential energy
4
Newtonian Formulation
Cartesian spatial coordinates ri = (xi,yi,zi) are primary variables
• for N atoms, system of N 2nd-order differential equations
d 2ri
m 2 mri Fi
dt
Sample application: 2D motion in central force field
yf
y
mx F eˆ x f (r )rˆ eˆ x xf
x2 y 2
my F eˆ y f (r )rˆ eˆ y
x2
2
• Polar coordinates are more natural
and convenient
mr 2
constant angular momentum
mr f (r )
2
mr 3
r
fictitious (centrifugal) force
F f (r )rˆ
5
Generalized Coordinates
Any convenient coordinates for description of particular system
• use qi as symbol for general coordinate
• examples
diatomic {q1,…,q6} = {xcom, ycom, zcom, r12, , f}
2-D motion in central field {q1, q2} = {r, }
r
f
Kinetic energy
• general quadratic form
K M 0 (q) M j (q)q j 12 M jk (q)q j qk
• examples
usually vanish
rotating diatomic
2-D central motion
K 12 m r 2 r 2 2
K 12 m q12 q22 q32 18 m r 2 r 2 2 (r sin )2f 2
6
Lagrangian Formulation
Independent of coordinate system
Define the Lagrangian
• L(q, q) K (q, q) U (q)
Equations of motion
d L L
0
dt q j q j
j 1
N
• N second-order differential equations
Central-force example
L 12 m r 2 r 2 2 U (r )
d L L
mr mr 2 f (r )
dt r r
Fr rU f (r )
d L L
dt
d
mr 2 0
dt
7
Hamiltonian Formulation 1. Motivation
Appropriate for application to statistical mechanics and quantum
mechanics
Newtonian and Lagrangian viewpoints take the qi as the
fundamental variables
• N-variable configuration space
• qi appears only as a convenient shorthand for dq/dt
• working formulas are 2nd-order differential equations
Hamiltonian formulation seeks to work with 1st-order
differential equations
• 2N variables
• treat the coordinate and its time derivative as independent variables
• appropriate quantum-mechanically
8
Hamiltonian Formulation 2. Preparation
Mathematically, Lagrangian treats q and q as distinct
• L( q j , q j , t )
• identify the generalized momentum as
pj
L
q j
• e.g. if L K U 1 mq 2 U (q); p L / q mq
2
dp j
• Lagrangian equations of motion dt
L
q j
We would like a formulation in which p is an independent
variable
• pi is the derivative of the Lagrangian with respect to qi, and we’re
looking to replace qi with pi
• we need …?
9
Hamiltonian Formulation 3. Defintion
…a Legendre transform!
Define the Hamiltonian, H
H (q, p) L(q, q) p j q j
K
K (q, q) U (q)
qj
q j
a j q 2j U (q) (2a j q j )q j
a j q 2j U (q)
K U
H equals the total energy (kinetic plus potential)
10
Hamiltonian Formulation 4. Dynamics
Hamilton’s equations of motion
• From Lagrangian equations, written in terms of momentum
Differential change in L
dp
L
p
dt
q
L
L
dq dq
q
q
pdq pdq
dL
Legendre transform
H ( L pq )
dH ( pdq qdp )
dH pdq qdp
p
H
p
H
p
q
L
q
Lagrange’s equation
of motion
Definition of momentum
q
Conservation of energy
Hamilton’s equations of motion
dH
dq
dp
p q pq qp 0
dt
dt
dt
11
Hamiltonian Formulation 5. Example
Particle motion in central force field
r
H K U
pr2
p2
U (r )
2m 2mr 2
dr pr
d
p
H
(1)
(2)
q
dt m
dt mr 2
p
2
H (3) dpr p f (r ) (4) dp 0
p
dt mr 3
dt
q
Fr rU f (r )
Lagrange’s equations
mr mr 2 f (r )
d
mr 2 0
dt
Equations no simpler, but theoretical basis is better
12
Phase Space (again)
Return to the complete picture of phase space
• full specification of microstate of the system is given by the values of
all positions and all momenta of all atoms
G = (pN,rN)
• view positions and momenta as completely independent coordinates
connection between them comes only through equation of motion
Motion through phase space
• helpful to think of dynamics as “simple” movement through the highdimensional phase space
facilitate connection to quantum mechanics
basis for theoretical treatments of dynamics
understanding of integrators
G
13
Integration Algorithms
Equations of motion in cartesian coordinates
dr j
dt
dp j
dt
pj
r (rx , ry )
m
p ( px , p y )
Fj
2-dimensional space (for example)
N
F j Fij
pairwise additive forces
i 1
i j
Desirable features of an integrator
•
•
•
•
•
•
F
minimal need to compute forces (a very expensive calculation)
good stability for large time steps
good accuracy
conserves energy and momentum
time-reversible
More on these later
area-preserving (symplectic)
14
Verlet Algorithm
1. Equations
Very simple, very good, very popular algorithm
Consider expansion of coordinate forward and backward in time
1 r (t ) t 3 O ( t 4 )
r (t t ) r (t ) m1 p(t ) t 21m F(t ) t 2 3!
1 r (t ) t 3 O ( t 4 )
r (t t ) r (t ) m1 p(t ) t 21m F(t ) t 2 3!
Add these together
r (t t ) r (t t ) 2r (t ) m1 F(t ) t 2 O( t 4 )
Rearrange
r (t t ) 2r (t ) r (t t ) m1 F(t ) t 2 O( t 4 )
• update without ever consulting velocities!
15
Verlet Algorithm 2. Flow diagram
One MD Cycle
Configuration r(t)
Previous configuration r(t-t)
One force
evaluation per
time step
Entire Simulation
Initialization
Compute forces F(t)
on all atoms using r(t)
Reset block sums
New configuration
1 move per cycle
Advance all positions according to
r(t+t) = 2r(t)-r(t-t)+F(t)/m t2
Add to block sum
cycles per block
Compute block average
blocks per simulation
Add to block sum
No
End of
block?
Compute final results
Yes
Block
averages
16
Verlet Algorithm 2. Flow Diagram
t-t
r
v
t
t+t
Given current position and
position at end of previous
time step
F
Schematic from Allen & Tildesley, Computer Simulation of Liquids
17
Verlet Algorithm 2. Flow Diagram
t-t
r
v
t
t+t
Compute the force at the
current position
F
Schematic from Allen & Tildesley, Computer Simulation of Liquids
18
Verlet Algorithm 2. Flow Diagram
t-t
r
v
t
t+t
Compute new position from
present and previous positions,
and present force
F
Schematic from Allen & Tildesley, Computer Simulation of Liquids
19
Verlet Algorithm 2. Flow Diagram
t-2t
r
v
t-t
t
t+t
Advance to next time step,
repeat
F
Schematic from Allen & Tildesley, Computer Simulation of Liquids
20
Verlet Algorithm 3. Java Code
User's Perspective on the Molecular Simulation API
Simulation
Space
Controller
IntegratorVerlet
Meter
Phase
Species
Boundary
Configuration
Potential
Display
Device
Verlet Algorithm
3. Relevant Methods in Java Code
21
public class IntegratorVerlet extends Integrator
//Performs one timestep increment in the Verlet algorithm
public void doStep(double tStep) {
atomIterator.reset();
while(atomIterator.hasNext()) {
//zero forces on all atoms
((Agent)atomIterator.next().ia).force.E(0.0); //integratorVerlet.Agent keeps a force Vector
}
pairIterator.allPairs(forceSum); //sum forces on all pairs
double t2 = tStep*tStep;
atomIterator.reset();
while(atomIterator.hasNext()) { //loop over all atoms, moving according to Verlet
Atom a = atomIterator.next();
Agent agent = (Agent)a.ia;
Space.Vector r = a.position(); //current position of the atom
temp.E(r);
//save it
r.TE(2.0);
//2*r
r.ME(agent.rLast);
//2*r-rLast
agent.force.TE(a.rm()*t2);
// f/m dt^2
r.PE(agent.force);
//2*r - rLast + f/m dt^2
agent.rLast.E(temp);
//rLast gets present r
}
return;
}
Verlet Algorithm
3. Relevant Methods in Java Code
22
public class IntegratorVerlet extends Integrator
//(anonymous) class for incrementing the sum of the forces on each atom
forceSum = new AtomPair.Action() {
private Space.Vector f = simulation().space.makeVector();
public void action(AtomPair pair) {
PotentialSoft potential = (PotentialSoft)simulation().getPotential(pair) //identify pot’l
f.E(potential.force(pair));
//compute force of atom1 on atom2
((Agent)pair.atom1().ia).force.PE(f); //increment atom1 force
((Agent)pair.atom2().ia).force.ME(f); //increment atom2 force
}
};
//Agent class for IntegratorVerlet; stores useful quantities in each Atom
public final static class Agent implements Integrator.Agent {
public Atom atom;
public Space.Vector force; //used to accumulate the force on the atom
public Space.Vector rLast; //holds the position of the atom at the last step
public Agent(Atom a) {
//constructor
atom = a;
force = atom.parentMolecule().parentPhase().parentSimulation.space.makeVector();
rLast = atom.parentMolecule().parentPhase().parentSimulation.space.makeVector();
}
}
23
Forces 1. Formalism
Force is the gradient of the potential
F21 u (r12 )
u (r12 )
u (r12 )
e
ey
Force on 1,
x
x1
y1
due to 2
du (r12 ) r12
r12
e
ey
x
dr12 x1
y1
f (r )
12 x12e x y12e y
r12
F21 F12
x12
y12
2
1
r12
1/ 2
r12 ( x2 x1 ) 2 ( y2 y1 ) 2
4
Energy
Force
2
0
-2
1.0
1.5
2.0
Separation, r/s
2.5
24
Forces 2. LJ Model
Force is the gradient of the potential
F21
f (r12 )
x12e x y12e y
r12
y12
2
e.g., Lennard-Jones model
s 12 s 6
u ( r ) 4
r
r
du
f (r )
dr
13
7
48 s
1s
s r
2 r
x12
1
r12
1/ 2
r12 ( x2 x1 ) 2 ( y2 y1 ) 2
4
Energy
Force
2
0
14
8
48 s
1 s
F21 2 x12e x y12e y
2 r12
s r12
-2
1.0
1.5
2.0
Separation, r/s
2.5
25
Forces 3. Java Code
User's Perspective on the Molecular Simulation API
Simulation
Space
Controller
IntegratorVerlet
Meter
Phase
Species
Boundary
Configuration
Potential
Display
Device
26
Forces
3. Relevant Methods from Java Code
public class PotentialLJ implements PotentialSoft
//Space.Vector used to compute and return a force
private Space.Vector force = Simulation.space.makeVector();
public Space.Vector force(AtomPair pair) {
double r2 = pair.r2();
//squared distance
if(r2 > cutoffDiameterSquared) {force.E(0.0);}
else {
double s2 = sigmaSquared/r2; // (sigma/r)^2
double s6 = s2*s2*s2;
// (sigma/r)^6
force.E(pair.dr());
// f = (x12 ex
force.TE(-48*s2*s6*(s6-0.5)/sigmaSquared);
// f *= -48*(sigma/r)^8 *
}
return force;
}
between pair of atoms
//outside cutoff; no interaction
+ y12 ey)
(vector)
[(sigma/r)^6 - 1/2] / sigma^2
27
Verlet Algorithm. 4. Loose Ends
Initialization
• how to get position at “previous time step” when starting out?
• simple approximation
r(t0 t ) r(t0 ) v(t0 ) t
Obtaining the velocities
• not evaluated during normal course of algorithm
• needed to compute some properties, e.g.
temperature
diffusion constant
• finite difference
v(t )
1
r(t t ) r(t t ) O( t 2 )
2 t
28
Verlet Algorithm 5. Performance Issues
Time reversible
• forward time step
r (t t ) 2r (t ) r (t t ) m1 F(t ) t 2
• replace t with t
r (t ( t )) 2r (t ) r (t ( t )) m1 F(t )( t ) 2
r (t t ) 2r (t ) r (t t ) m1 F (t ) t 2
• same algorithm, with same positions and forces, moves system
backward in time
Numerical imprecision of adding large/small numbers
O(t1)
O(t1)
r (t t ) r (t ) r (t ) r (t t ) m1 F(t ) t 2
O(t0)
O(t0)
O(t2)
29
Initial Velocities
(from Lecture 3)
Random direction
• randomize each component independently
• randomize direction by choosing point on spherical surface
Magnitude consistent with desired temperature. Choices:
•
•
•
•
Maxwell-Boltzmann: prob(vx ) exp 12 mvx2 / kT
2
Uniform over (-1/2,+1/2), then scale so that N1 vi, x kT / m
Constant at vx kT / m
Same for y, z components
Be sure to shift so center-of-mass momentum is zero
Px N1 pi, x
pi, x pi, x Px
30
Leapfrog Algorithm
Eliminates addition of small numbers O(t2) to differences in
large ones O(t0)
Algorithm
r (t t ) r (t ) v(t 12 t ) t
v(t 12 t ) v(t 12 t ) m1 F(t ) t
31
Leapfrog Algorithm
Eliminates addition of small numbers O(t2) to differences in
large ones O(t0)
Algorithm
r (t t ) r (t ) v(t 12 t ) t
v(t 12 t ) v(t 12 t ) m1 F(t ) t
Mathematically equivalent to Verlet algorithm
r (t t ) r (t ) v(t 12 t ) m1 F(t ) t t
32
Leapfrog Algorithm
Eliminates addition of small numbers O(t2) to differences in
large ones O(t0)
Algorithm
r (t t ) r (t ) v(t 12 t ) t
v(t 12 t ) v(t 12 t ) m1 F(t ) t
Mathematically equivalent to Verlet algorithm
r (t t ) r (t ) v(t 12 t ) m1 F(t ) t t
r(t) as evaluated from
r(t ) r(t t ) v(t 12 t ) t
previous time step
33
Leapfrog Algorithm
Eliminates addition of small numbers O(t2) to differences in
large ones O(t0)
Algorithm
r (t t ) r (t ) v(t 12 t ) t
v(t 12 t ) v(t 12 t ) m1 F(t ) t
Mathematically equivalent to Verlet algorithm
r (t t ) r (t ) v(t 12 t ) m1 F(t ) t t
r(t) as evaluated from
r(t ) r(t t ) v(t 12 t ) t
previous time step
r(t t ) r(t ) r(t ) r(t t ) m1 F(t ) t 2
34
Leapfrog Algorithm
Eliminates addition of small numbers O(t2) to differences in
large ones O(t0)
Algorithm
r (t t ) r (t ) v(t 12 t ) t
v(t 12 t ) v(t 12 t ) m1 F(t ) t
Mathematically equivalent to Verlet algorithm
r (t t ) r (t ) v(t 12 t ) m1 F(t ) t t
r(t) as evaluated from
r(t ) r(t t ) v(t 12 t ) t
previous time step
r(t t ) r(t ) r(t ) r(t t ) m1 F(t ) t 2
r (t t ) 2r (t ) r (t t ) m1 F(t ) t 2
original algorithm
35
Leapfrog Algorithm 2. Flow Diagram
t-t
r
v
t
t+t
Given current position, and
velocity at last half-step
F
Schematic from Allen & Tildesley, Computer Simulation of Liquids
36
Leapfrog Algorithm 2. Flow Diagram
t-t
r
t
t+t
Compute current force
v
F
Schematic from Allen & Tildesley, Computer Simulation of Liquids
37
Leapfrog Algorithm 2. Flow Diagram
t-t
r
v
t
t+t
Compute velocity at
next half-step
F
Schematic from Allen & Tildesley, Computer Simulation of Liquids
38
Leapfrog Algorithm 2. Flow Diagram
t-t
r
t
t+t
Compute next position
v
F
Schematic from Allen & Tildesley, Computer Simulation of Liquids
39
Leapfrog Algorithm 2. Flow Diagram
t-2t
r
v
t-t
t
t+t
Advance to next time step,
repeat
F
Schematic from Allen & Tildesley, Computer Simulation of Liquids
40
Leapfrog Algorithm. 3. Loose Ends
Initialization
• how to get velocity at “previous time step” when starting out?
• simple approximation
v(t0 t ) v(t0 ) m1 F(t0 ) 12 t
Obtaining the velocities
• interpolate
1
v (t ) v (t 12 t ) v (t 12 t )
2
41
Velocity Verlet Algorithm
Roundoff advantage of leapfrog, but better treatment of
velocities
Algorithm
r (t t ) r (t ) v(t ) t 21m F(t ) t 2
v(t t ) v(t ) 21m F(t ) F(t t ) t
Implemented in stages
•
•
•
•
•
evaluate current force
compute r at new time
add current-force term to velocity (gives v at half-time step)
compute new force
add new-force term to velocity
Also mathematically equivalent to Verlet algorithm
(in giving values of r)
42
Velocity Verlet Algorithm
2. Flow Diagram
t-t
r
v
t
t+t
Given current position,
velocity, and force
F
Schematic from Allen & Tildesley, Computer Simulation of Liquids
43
Velocity Verlet Algorithm
2. Flow Diagram
t-t
r
t
t+t
Compute new position
v
F
Schematic from Allen & Tildesley, Computer Simulation of Liquids
44
Velocity Verlet Algorithm
2. Flow Diagram
t-t
r
t
t+t
Compute velocity at half step
v
F
Schematic from Allen & Tildesley, Computer Simulation of Liquids
45
Velocity Verlet Algorithm
2. Flow Diagram
t-t
r
t
t+t
Compute force at new position
v
F
Schematic from Allen & Tildesley, Computer Simulation of Liquids
46
Velocity Verlet Algorithm
2. Flow Diagram
t-t
r
t
t+t
Compute velocity at full step
v
F
Schematic from Allen & Tildesley, Computer Simulation of Liquids
47
Velocity Verlet Algorithm
2. Flow Diagram
t-2t
r
v
t-t
t
t+t
Advance to next time step,
repeat
F
Schematic from Allen & Tildesley, Computer Simulation of Liquids
48
Other Algorithms
Predictor-Corrector
• not time reversible
• easier to apply in some instances
constraints
rigid rotations
Beeman
• better treatment of velocities
Velocity-corrected Verlet
49
Summary
Several formulations of mechancs
• Hamiltonian preferred
independence of choice of coordinates
emphasis on phase space
Integration algorithms
• Calculation of forces
• Simple Verlet algorithsm
Verlet
Leapfrog
Velocity Verlet
Next up: Calculation of dynamical properties