Molecular Dynamics
In the statistical mechanics chapter, we introduced the Boltzmann distribution and showed how the partition function encodes all equilibrium thermodynamics. In the MCMC chapter, we saw that Hamiltonian Monte Carlo uses leapfrog integration to propose distant states with high acceptance probability — and noted that those trajectories look suspiciously like a physics simulation. In this chapter, we make that connection precise.
Molecular dynamics (MD) is a deterministic method for simulating the time evolution of a physical system. Rather than proposing random moves and accepting or rejecting them, MD directly integrates Newton's equations of motion. This produces a trajectory through phase space — the system evolves in time, and thermodynamic observables emerge as time averages along the trajectory.
We will see how the Hamiltonian formalism from the statistical mechanics chapter translates into equations of motion, how to integrate them numerically while preserving the structure of phase space, and how coupling the system to a heat bath (thermostatting) produces samples from the Boltzmann distribution.
Hamilton's equations of motion
Recall from the statistical mechanics chapter that the state of a classical system is described by positions
Hamilton's equations give the time evolution of the system:
The first equation says that velocity is momentum divided by mass. The second is Newton's second law: the force on each particle equals the negative gradient of the potential energy. Together, they define a flow on phase space — given an initial condition
Key property: Hamiltonian dynamics conserves the total energy
Numerical integration: the velocity-Verlet algorithm
In practice, Hamilton's equations cannot be solved analytically for interesting systems. We discretize time into steps of size
The velocity-Verlet algorithm:
Given positions
- Half-step velocity update:
- Full position update:
- Compute new forces:
- Complete velocity update:
This is the same leapfrog integrator we encountered in HMC, just written in a different form. It has a crucial property: it is symplectic, meaning it exactly preserves the phase-space volume
NVE molecular dynamics
When we integrate Hamilton's equations without any external coupling, the system evolves at approximately constant energy. This is the microcanonical (NVE) ensemble — fixed particle number
Click anywhere on the energy landscape below to launch an NVE trajectory. The particle starts at the clicked position with random initial velocities. Watch how it rolls downhill, accelerates through minima, and decelerates as it climbs toward higher-energy regions — exactly like a ball on a surface.
Energy conservation
The hallmark of a good NVE integrator is that it conserves total energy. The plot below shows the potential energy
Notice how kinetic and potential energy oscillate as the particle moves through minima and over barriers — energy is constantly exchanged between the two. But the total energy
Try increasing the time step
The microcanonical ensemble and the sampling problem
A long NVE trajectory samples the microcanonical ensemble — the system visits all states compatible with its total energy
that we introduced in the statistical mechanics chapter. In the canonical ensemble, the system can exchange energy with a heat bath, and the probability of visiting a configuration depends on its energy and the temperature. To sample the Boltzmann distribution with MD, we need to couple the system to a thermostat.
The Andersen thermostat
The simplest thermostat is the Andersen thermostat. To understand why it works, recall from the statistical mechanics chapter that the full canonical distribution on phase space factorizes:
The momentum part
where
The Andersen thermostat exploits this factorization. At random times, it interrupts the deterministic Hamiltonian dynamics and replaces the particle's velocity with a fresh draw from the Maxwell-Boltzmann distribution at temperature
The Andersen thermostat:
At each time step with probability
- Replace all velocity components with fresh samples from
where
Why does this produce the correct canonical distribution? The argument has two parts. First, Hamiltonian dynamics preserves the canonical distribution — if
The collision frequency
: No thermostatting — pure NVE dynamics. The trajectory conserves energy. : Velocities are resampled at every step. The trajectory becomes a random walk in velocity space — all dynamical correlations are destroyed.
In practice,
NVT molecular dynamics
Now let us see the thermostat in action. The visualization below runs NVT MD with the Andersen thermostat. Click on the landscape to start a trajectory — notice how the particle's behavior changes qualitatively compared to NVE. The occasional velocity resampling events allow the particle to gain or lose energy, enabling it to cross barriers and explore the full landscape at temperature
Energy fluctuations under the thermostat
Unlike NVE, the total energy now fluctuates — each thermostat collision injects or removes kinetic energy. The plot below shows the energy traces for the trajectory you just launched. Click on the landscape above to update.
Compare this to the NVE trace above: the total energy (green) now wanders freely, driven by the thermostat collisions. The potential energy distribution reflects the Boltzmann weight at the target temperature — at low
Try adjusting the collision rate
From trajectories to the Boltzmann distribution
The fundamental promise of NVT MD is the ergodic hypothesis: time averages along a sufficiently long trajectory equal ensemble averages over the Boltzmann distribution. In the language of the statistical mechanics chapter, this means
for any observable
Let us verify this directly. The plot below compares two things: the analytical Boltzmann distribution
The left panel shows the analytical Boltzmann distribution
NPT molecular dynamics: the barostat
So far we have sampled the canonical (NVT) ensemble at fixed volume. In many applications — particularly in materials science — we want to control pressure instead. This is the isothermal-isobaric (NPT) ensemble from the statistical mechanics chapter, where the volume
Making volume dynamical
In the NVT ensemble, we sample configurations
The
and the corresponding probability density is
To sample this distribution with MD, we need a barostat — a mechanism that couples the volume to a pressure bath, just as the thermostat couples velocities to a heat bath. The simplest approach, introduced by Andersen (1980), treats the volume as an additional degree of freedom with its own conjugate momentum
and Hamilton's equations now include an equation of motion for the volume:
where
The toy model revisited
For our toy model — two particles in a periodic box with interparticle distance
Since
NPT energy traces
The enthalpy
At low pressure, the system explores large volumes (large
Sampling the NPT Boltzmann distribution
Just as in the NVT case, we can verify that the thermostatted NPT trajectory produces the correct distribution. The panels below compare the analytical NPT Boltzmann density with the density of MD samples on the enthalpy surface.
Connection to Hamiltonian Monte Carlo
If you have read the MCMC chapter, you will have noticed a striking similarity between MD and HMC. This is no coincidence — HMC is MD, wrapped in a Metropolis accept/reject step.
The key differences are:
| Molecular dynamics | HMC | |
|---|---|---|
| Momenta | Evolve continuously | Resampled fresh at each proposal |
| Trajectory | One long continuous run | Short segments ( |
| Acceptance | None (deterministic) | Metropolis criterion on |
| Energy errors | Bounded (symplectic) | Corrected by accept/reject |
| Ensemble | NVE (without thermostat) | Canonical (by construction) |
In HMC, the accept/reject step corrects for the finite-
The common thread: Both MD and HMC exploit the geometry of the energy landscape through Hamiltonian dynamics. Rather than proposing random jumps (as in Metropolis-Hastings), they follow the natural flow of the system — accelerating downhill, decelerating uphill — to generate distant, low-energy configurations efficiently. This geometric awareness is what makes gradient-based sampling methods essential for high-dimensional systems.
In the context of nested sampling, the connection runs even deeper. The TE-HMC variant caps the kinetic energy to enforce a hard energy constraint