Introduction to Molecular Dynamics: Concepts, Algorithms, and Formalism
1. The Philosophy: A Computational Microscope¶
1.1 Two Paradigms of Molecular Simulation¶
In previous sections, we used Statistical Mechanics to calculate properties based on the probability of states. We asked: “In an infinite ensemble of copies, what is the average energy?”
Molecular Dynamics (MD) takes a fundamentally different approach. It asks: “How does this specific system evolve over time?”
MD is a deterministic simulation of the classical Many-Body problem. By solving Newton’s laws of motion for every atom, we generate a Trajectory: a chronological movie of the microscopic world.
| Approach | Question Asked | Output | Time Information |
|---|---|---|---|
| Statistical Mechanics | What are equilibrium properties? | Ensemble averages (T, P, G, S) | No |
| Molecular Dynamics | How does the system evolve? | Trajectories, mechanisms | Yes |
Statistical Mechanics gives us equilibrium averages (Temperature, Free Energy, Entropy).
Molecular Dynamics gives us Time (Diffusion coefficients, Viscosity, Relaxation times) and Mechanisms (How does a protein fold? How does a crack propagate? What is the reaction pathway?).
1.2 What Can MD Tell Us?¶
MD simulations provide unique insights that cannot be obtained from experiments or static calculations:
Dynamical Properties:
Diffusion coefficients
Viscosity and thermal conductivity
Vibrational spectra
Reaction rates and transition states
Microscopic Mechanisms:
Protein folding pathways
Crystal nucleation
Crack propagation in materials
Drug binding mechanisms
Structural Fluctuations:
Conformational changes
Phase transitions
Defect formation and migration
1.3 Limitations and Approximations¶
MD simulations, while powerful, have important limitations:
Classical Mechanics: Electrons are not explicitly treated (except in ab initio MD)
Time Scale: Limited to microseconds (or milliseconds with special techniques)
Length Scale: Typically millions of atoms maximum
Force Field Accuracy: Results depend on the quality of the potential energy function
2. The Formalism: Equations of Motion¶
2.1 Phase Space Representation¶
Formally, the state of a system with atoms is defined by a single point in Phase Space () with dimensions:
Positions:
Momenta:
The trajectory of the system traces out a path in this high-dimensional space. Each point uniquely defines the microstate of the system.
Key Insight: In classical mechanics, if you know the exact positions and momenta at time , you can predict them at all future times (determinism).
2.2 The Hamiltonian Formulation¶
The evolution of this point is governed by the Hamiltonian (), which represents the total energy of the system.
Where:
is the kinetic energy
is the potential energy (depends only on positions)
2.3 Hamilton’s Equations of Motion¶
The motion is derived from Hamilton’s equations:
Velocity:
Force:
These are equivalent to Newton’s second law:
Conceptual Check: The Conservative Force
The second equation is critical. It tells us that Force is the negative gradient of Potential Energy.
This means atoms always “roll downhill” on the Potential Energy Surface, trying to minimize their potential energy. The steeper the slope, the larger the force.
2.4 Conservation Laws¶
Hamilton’s equations automatically conserve several important quantities:
Energy: (in the absence of external forces)
Momentum: (if no external forces)
Phase Space Volume: Liouville’s theorem states that phase space volume is incompressible
These conservation laws are critical for the stability and physical validity of MD simulations.
3. The Algorithm: Anatomy of an MD Code¶
3.1 The Basic Structure¶
How do we translate this formalism into code? Despite differences between software like LAMMPS, GROMACS, NAMD, or AMBER, they all share a common algorithmic “skeleton.”
The simulation proceeds in a loop that advances time in small, discrete steps (), typically 1-2 femtoseconds (fs).
3.2 Pseudo-code Representation¶
# ========================================
# Initialize the System
# ========================================
t = 0
Initialize Positions (x) from structure file or lattice
Initialize Velocities (v) from Maxwell-Boltzmann distribution
Initialize Forces (F) by calling force calculation
# ========================================
# The Main MD Loop
# ========================================
while (t < t_max):
# 1. Calculate Forces (The "Expensive" Step)
# This takes ~90% of computational time
forces = calculate_forces(x)
# 2. Integrate Equations of Motion (Verlet)
# Update positions and velocities
x_new, v_new = velocity_verlet(x, v, forces, dt)
# 3. Apply Constraints (if any)
# E.g., SHAKE/RATTLE for bond lengths
apply_constraints(x_new, v_new)
# 4. Apply Thermostat/Barostat (if needed)
# Control temperature and/or pressure
if (using_thermostat):
v_new = thermostat_update(v_new, T_target)
if (using_barostat):
x_new, box_size = barostat_update(x_new, P_target)
# 5. Sampling (Thermodynamics)
# Calculate and store properties
if (t % sample_freq == 0):
calculate_properties(x_new, v_new)
write_trajectory(x_new, v_new)
# 6. Advance Time
t = t + dt
x, v = x_new, v_new
end loop
# ========================================
# Finalize
# ========================================
calculate_averages()
write_final_configuration()3.3 Component 1: Initialization¶
We need to set the starting point in Phase Space.
Positions:
Often come from experimental structures (PDB files for biomolecules, CIF for crystals)
Can be generated on a lattice (FCC, BCC, etc.) for simple systems
Must avoid overlapping atoms (would create infinite forces)
Velocities:
Assigned randomly from a Maxwell-Boltzmann distribution at the desired temperature:
Each component () is independently sampled
Center-of-mass velocity is usually removed to prevent drift
Initial Force Calculation:
Forces must be calculated before the first integration step
This sets up the system for the Verlet algorithm
3.4 Timestep Selection¶
The choice of timestep is critical:
Too large: Energy will not be conserved, simulation becomes unstable
Too small: Simulation is unnecessarily slow
Rule of Thumb: should be ~1/10 of the fastest vibrational period in the system
| System Type | Typical Timestep |
|---|---|
| All-atom with hydrogens | 1 fs |
| All-atom, constrained H bonds | 2 fs |
| United atom (CH₃ groups) | 2-5 fs |
| Coarse-grained | 10-50 fs |
4. The Force Problem: Interatomic Potentials¶
4.1 The Central Challenge¶
The accuracy of any MD simulation depends entirely on the quality of the Potential Energy Surface, . Since solving the Schrödinger equation for every step is too expensive (quantum chemistry scales as to ), we use Empirical Force Fields.
4.2 The Car Engine Analogy¶
Imagine a car driving on a straight road. Newton’s laws tell us how it moves (kinematics), but to calculate its acceleration, we need to know the energy produced by the engine (dynamics).
Similarly, the Interatomic Potential is the driving engine of the simulation. The integrator (Verlet) is the transmission, but without accurate forces, the trajectory is meaningless.
4.3 The Lennard-Jones (12-6) Potential¶
For noble gases and simple fluids, the standard model is the Lennard-Jones potential:
Where:
= depth of the potential well (energy scale)
= distance at which (length scale)
(equilibrium distance)
Physical Interpretation:
Repulsion (): Models the Pauli Exclusion Principle. As electron clouds overlap, energy skyrockets. The power 12 is chosen for computational speed ().
Attraction (): Models London Dispersion Forces (induced dipole-dipole attraction). The dependence comes from quantum mechanics.
The Force:
4.4 Python: Visualizing the Physics¶
import numpy as np
import matplotlib.pyplot as plt
# LJ Parameters for Argon
epsilon = 1.0 # Normalized energy (119.8 K in real units)
sigma = 1.0 # Normalized distance (3.405 Å in real units)
r = np.linspace(0.9, 3.0, 200)
# Potential U(r)
U = 4 * epsilon * ((sigma/r)**12 - (sigma/r)**6)
# Force F(r) = -dU/dr
F = 24 * epsilon / sigma * (2*(sigma/r)**13 - (sigma/r)**7)
# Create figure with two subplots
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 10))
# Plot potential
ax1.plot(r, U, 'b-', linewidth=2, label='U(r)')
ax1.axhline(y=0, color='k', linestyle='--', alpha=0.3)
ax1.axhline(y=-epsilon, color='r', linestyle='--', alpha=0.5, label=r'$-\epsilon$')
ax1.axvline(x=2**(1/6)*sigma, color='g', linestyle='--', alpha=0.5, label=r'$r_{min}$')
ax1.set_ylim(-1.5, 2)
ax1.set_xlabel('Distance r/σ', fontsize=12)
ax1.set_ylabel('Energy U/ε', fontsize=12)
ax1.set_title("Lennard-Jones Potential", fontsize=14, fontweight='bold')
ax1.legend()
ax1.grid(alpha=0.3)
# Plot force
ax2.plot(r, F, 'r-', linewidth=2, label='F(r)')
ax2.axhline(y=0, color='k', linestyle='--', alpha=0.3)
ax2.axvline(x=2**(1/6)*sigma, color='g', linestyle='--', alpha=0.5, label=r'$r_{min}$')
ax2.set_xlabel('Distance r/σ', fontsize=12)
ax2.set_ylabel('Force F×σ/ε', fontsize=12)
ax2.set_title("Lennard-Jones Force", fontsize=14, fontweight='bold')
ax2.legend()
ax2.grid(alpha=0.3)
plt.tight_layout()
plt.savefig('LJ_potential_force.png', dpi=150)
plt.show()4.5 Cutoffs and Long-Range Corrections¶
Computing all pairwise interactions scales as . For atoms, this means 1012 evaluations per timestep!
Cutoff Radius ():
Only compute interactions within a sphere of radius (typically 10-12 Å)
For LJ:
Reduces scaling to with neighbor lists
Tail Correction: For LJ, we can analytically correct for neglected long-range interactions:
Electrostatics: Coulomb interactions are long-range () and cannot be simply cut off. We use:
Ewald Summation: Exact solution using reciprocal space
Particle Mesh Ewald (PME): Fast Fourier Transform version,
Reaction Field: Continuum approximation beyond cutoff
5. Integration: Moving the Atoms¶
5.1 Why Not Simple Integration?¶
We have the initial state and the forces . Why can’t we use simple calculus?
Euler Method:
Problems:
First-order accurate: error
Not time-reversible
Does NOT conserve energy → total energy drifts to infinity!
Not symplectic → phase space volume not conserved
5.2 Required Properties of MD Integrators¶
A good integrator must be:
Time-reversible: when velocities are reversed
Symplectic: Preserves phase space volume (Liouville’s theorem)
Energy-conserving: Total energy fluctuates but doesn’t drift
Efficient: Requires few force evaluations (forces are expensive!)
Stable: Works for reasonably large timesteps
5.3 The Velocity Verlet Algorithm¶
The industry standard is Velocity Verlet, which satisfies all the above requirements.
Algorithm:
Half-Kick: Update velocities by half a timestep using current forces.
Drift: Update positions using the half-step velocities.
Force Update: Calculate new forces at the new positions .
Half-Kick: Complete the velocity update.
Properties:
Second-order accurate: global error
Time-reversible
Symplectic
Requires only one force evaluation per timestep
5.4 Python Implementation¶
import numpy as np
def velocity_verlet(positions, velocities, forces, masses, dt, force_function):
"""
Velocity Verlet integrator
Parameters:
-----------
positions : ndarray, shape (N, 3)
Current positions
velocities : ndarray, shape (N, 3)
Current velocities
forces : ndarray, shape (N, 3)
Current forces
masses : ndarray, shape (N,)
Particle masses
dt : float
Timestep
force_function : callable
Function that computes forces from positions
Returns:
--------
positions_new : ndarray
Updated positions
velocities_new : ndarray
Updated velocities
forces_new : ndarray
Updated forces
"""
# Half-step velocity update
velocities_half = velocities + 0.5 * forces / masses[:, np.newaxis] * dt
# Full-step position update
positions_new = positions + velocities_half * dt
# Calculate new forces
forces_new = force_function(positions_new)
# Half-step velocity update to complete the step
velocities_new = velocities_half + 0.5 * forces_new / masses[:, np.newaxis] * dt
return positions_new, velocities_new, forces_new5.5 Alternative Integrators¶
Leapfrog Algorithm:
Positions and velocities are offset by half a timestep
Equivalent to Verlet but different formulation
Popular in astrophysics
Runge-Kutta Methods:
Higher-order accuracy (4th order common)
Multiple force evaluations per step
Not symplectic → not ideal for MD
Multiple Timestep (RESPA):
Fast forces (bonds) computed every
Slow forces (non-bonded) computed every
Can speed up simulations by 2-3×
6. Controlling the Environment: Thermostats & Barostats¶
6.1 The Ensemble Problem¶
Newton’s laws naturally conserve Total Energy (). A standard MD simulation therefore generates the Microcanonical (NVE) Ensemble:
= Number of particles (constant)
= Volume (constant)
= Energy (constant)
However, real experiments are usually done at:
Constant Temperature (NVT) - Canonical Ensemble
Constant Pressure (NPT) - Isothermal-Isobaric Ensemble
Why do we care?
Most lab experiments are at constant T and P
Free energy calculations require specific ensembles
Structural properties may depend on ensemble
6.2 The Concept of Coupling¶
Imagine your simulation box is a test tube sitting in a large water bath (the reservoir). Heat flows in and out to keep constant. Similarly, a piston can compress/expand the box to keep constant.
Extended System Method: The reservoir itself becomes a dynamical variable with its own equation of motion. The combined system (simulation + reservoir) is isolated and conserves energy, but the simulation part exchanges energy with the reservoir.
6.3 Thermostats (NVT)¶
Temperature is related to kinetic energy via the equipartition theorem:
To control , we must manipulate the velocities.
6.3.1 Velocity Rescaling (Berendsen Thermostat)¶
Method: At each step, multiply all velocities by a scaling factor:
Where:
= target temperature
= current temperature
= coupling time constant (typically 0.1-1.0 ps)
Implementation:
Pros:
Simple and efficient
Good for equilibration (reaches target T quickly)
Stable
Cons:
Does NOT produce the canonical ensemble
Suppresses temperature fluctuations
Not suitable for accurate free energy calculations
6.3.2 Nosé-Hoover Thermostat¶
Method: Introduce an additional degree of freedom (the “heat bath”) with its own mass and coordinate . The extended Hamiltonian is:
Where is the number of degrees of freedom.
Equations of Motion:
Physical Interpretation:
The term acts as a friction coefficient
When : friction increases, slowing atoms down
When : friction becomes negative (anti-friction), speeding atoms up
Pros:
Generates the correct canonical ensemble
Temperature fluctuations are physical
Suitable for free energy calculations
Cons:
Slightly more complex to implement
Can show oscillatory behavior if is poorly chosen
Requires careful equilibration
Choosing :
where is the desired coupling time (typically 0.1-1.0 ps).
6.3.3 Langevin Dynamics¶
Method: Add both friction and random force terms to mimic collisions with solvent:
Where:
= friction coefficient
= random force with
Pros:
Generates canonical ensemble
Good for implicit solvent simulations
Can use larger timesteps
Cons:
Destroys dynamical properties (diffusion is affected)
Not suitable for studying dynamics
6.3.4 Summary: Choosing a Thermostat¶
| Thermostat | Ensemble | Use Case |
|---|---|---|
| None (NVE) | Microcanonical | Testing energy conservation |
| Velocity Rescaling | Approximate NVT | Equilibration |
| Nosé-Hoover | Canonical (NVT) | Production runs, free energy |
| Langevin | Canonical (NVT) | Implicit solvent, large systems |
6.4 Barostats (NPT)¶
To control Pressure, we treat the Volume () of the simulation box as a dynamic variable.
Instantaneous Pressure: The virial theorem gives us:
The first term is the kinetic contribution, the second is the virial (force × distance).
6.4.1 Berendsen Barostat¶
Method: Scale the box size and all coordinates:
Where:
= target pressure
= coupling constant
= isothermal compressibility
Update:
Pros/Cons: Same as Berendsen thermostat - good for equilibration, but doesn’t produce true NPT ensemble.
6.4.2 Parrinello-Rahman Barostat¶
Method: The box itself becomes a dynamical matrix with its own equation of motion:
Where:
= “mass” of the box (barostat coupling parameter)
= internal pressure tensor
= target pressure (can be anisotropic)
Pros:
Generates correct NPT ensemble
Allows box shape changes (important for crystals under stress)
Correct pressure fluctuations
Cons:
More complex
Can be unstable if not carefully equilibrated
Requires proper choice of
6.5 Combined Thermostat + Barostat (NPT)¶
For NPT simulations, we typically use:
Nosé-Hoover chains + Parrinello-Rahman (most rigorous)
Berendsen thermostat + Berendsen barostat (equilibration)
Velocity rescaling + Parrinello-Rahman (good compromise)
Practical Workflow:
Energy minimization (eliminate bad contacts)
NVT equilibration with Berendsen (heat up system)
NPT equilibration with Berendsen (adjust density)
NPT production with Nosé-Hoover/Parrinello-Rahman (data collection)
7. Statistical Mechanics Connection: The Ergodic Hypothesis¶
9.1 The Fundamental Question¶
If we simulate only 1000 atoms for 100 nanoseconds, how can we predict macroscopic properties of a mole () of atoms?
The solution is the Ergodic Hypothesis. It states that:
Over sufficiently long time periods, a single trajectory will visit all accessible microstates with the correct probability.
Therefore, the Time Average (from our simulation trajectory) equals the Ensemble Average (measuring all possible states simultaneously).
9.2 Practical Implications¶
What this means:
We don’t need to simulate a mole of atoms
A long trajectory of a small system gives the same averages as many copies of the system
We can compute thermodynamic properties from a single MD run
Caveats:
The system must be ergodic (able to access all states)
Simulations must be long enough to sample all relevant states
Broken ergodicity occurs when barriers are too high (proteins can get stuck in local minima)
9.3 Statistical Inefficiency and Correlation¶
Not all data points are independent. If we save configurations every 1 fs, consecutive frames are highly correlated.
Autocorrelation Function:
Correlation Time (): The time for to decay to of its initial value.
Statistical Inefficiency:
Effective Number of Independent Samples:
Practical Rule: Save configurations every to ensure independence.
8. Periodic Boundary Conditions¶
8.1 The Surface Problem¶
If we simulate a box of 1000 atoms with free boundaries, ~30% of atoms are at the surface. Surface atoms behave differently than bulk atoms.
Solution: Periodic Boundary Conditions (PBC)
The simulation box is replicated infinitely in all directions. When an atom leaves one side of the box, its periodic image enters from the opposite side.
8.2 Implementation¶
Minimum Image Convention: Each atom interacts with the nearest image of every other atom (including its own periodic images if the box is small).
Wrapping Coordinates:
def apply_pbc(positions, box_length):
"""Wrap positions into the central box"""
return positions - box_length * np.floor(positions / box_length)Computing Distances:
def minimum_image_distance(r1, r2, box_length):
"""Compute minimum image distance between two atoms"""
dr = r1 - r2
dr = dr - box_length * np.round(dr / box_length)
return np.linalg.norm(dr)8.3 Consequences¶
Advantages:
Eliminates surface effects
Simulates bulk properties
Conserves momentum automatically
Disadvantages:
Imposes artificial periodicity (correlation length limited by box size)
Cannot study surfaces or interfaces directly
Box size must be at least to avoid self-interaction
9. Summary and Workflow¶
9.1 Step-by-Step Protocol¶
1. System Preparation
Obtain or build initial structure
Add solvent, ions if needed
Define simulation box with PBC
2. Energy Minimization
Remove steric clashes
Typically steepest descent or conjugate gradient
Run until forces < threshold (e.g., 10 kJ/mol/nm)
3. Equilibration Phase
NVT: Heat system to target temperature (0 → 300 K over 100 ps)
NPT: Equilibrate density (300 K, 1 bar, 500 ps)
Use strong coupling (Berendsen) for rapid equilibration
4. Production Phase
Switch to rigorous ensemble (Nosé-Hoover, Parrinello-Rahman)
Run for 10-100 ns (or longer)
Save trajectories every 10-100 ps
5. Analysis
Check energy conservation/stability
Calculate structural properties (RDF, RMSD)
Calculate dynamical properties (MSD, diffusion)
Compute thermodynamic averages
9.2 Common Pitfalls and Best Practices¶
❌ Common Mistakes:
Timestep too large → energy explosion
Insufficient equilibration → artifacts in results
Box too small → artificial correlations
Cutoff too short → missing interactions
Poor initial structure → system won’t equilibrate
✅ Best Practices:
Always plot energy vs. time to check stability
Compare results with different timesteps
Run multiple independent simulations
Compute error bars using block averaging
Validate with experimental data when available
9.3 The Big Picture¶
When you run an MD simulation, you are orchestrating these components:
| Component | Physical Meaning | Key Parameters |
|---|---|---|
| Force Field | Potential Energy Surface | (LJ); bond/angle constants |
| Integrator | Time Evolution (Verlet) | Timestep (typically 1-2 fs) |
| Thermostat | Temperature Control (NVT) | Target T, coupling time |
| Barostat | Pressure Control (NPT) | Target P, coupling time |
| Boundary | Periodic Conditions | Box size (> 2×cutoff) |
| Sampling | Data Collection | Output frequency |
9.4 Topics to be Covered Later¶
The following advanced topics will be covered in detail in subsequent lectures:
Other Force Fields: AMBER, CHARMM, OPLS force fields for proteins, DNA, and lipids
Computational Optimization: Neighbor lists, cell lists, and efficient force calculations
Trajectory Analysis: RDF, MSD, diffusion coefficients, and structural analysis
Enhanced Sampling: Umbrella sampling, metadynamics, replica exchange methods
Constraint Algorithms: SHAKE and RATTLE for rigid bonds
Software Implementation: Detailed LAMMPS, GROMACS, and NAMD tutorials
10. Conclusion and Further Reading¶
10.1 What We’ve Learned¶
Molecular Dynamics is a powerful bridge between microscopic laws and macroscopic observations. By solving Newton’s equations for individual atoms, we can:
Predict thermodynamic properties
Observe dynamical behavior and mechanisms
Understand atomic-level processes
Design new materials through computational screening
The key concepts covered in these notes are:
Deterministic evolution in phase space via Hamilton’s equations
Force fields that approximate the potential energy surface
Symplectic integrators (Velocity Verlet) that conserve energy and phase space
Thermostats/barostats that control temperature and pressure
Ergodicity that connects time averages to ensemble averages
Periodic boundary conditions that eliminate surface effects
These fundamental concepts form the foundation for all molecular dynamics simulations, from simple Lennard-Jones fluids to complex biomolecular systems.
10.2 Recommended Resources¶
Textbook:
Frenkel & Smit, “Understanding Molecular Simulation”
10.3 The Future of MD¶
Current Frontiers:
Machine Learning Force Fields: Neural networks trained on quantum data (orders of magnitude faster)
Exascale Computing: Simulating entire viruses, millisecond timescales
Enhanced Sampling: Better algorithms for rare events
Quantum MD: Including nuclear quantum effects (path integrals)
Multiscale Methods: Coupling atomistic MD with continuum mechanics
The field continues to evolve rapidly, driven by increasing computational power and algorithmic innovations. The fundamental principles, however, remain those we’ve covered here.
End of Comprehensive Notes