Algorithmadvanced
Nose-Hoover chains
Couples the Nose-Hoover friction to further thermostats in a chain, curing the lack of ergodicity of one thermostat on small or stiff systems.
- Time
- O(S (N^2 + M)) worst
- Space
- O(N + M) extra
- Where
- M = the number of links in the chain; N = the number of particles; S = the number of time steps
- Model
- Real RAM
- Properties
- deterministiciterativesymmetricnot symplectic
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Glenn J. Martyna, Michael L. Klein, Mark E. Tuckerman, 1992
Nose-Hoover chains are a deterministic thermostat for molecular dynamics that extends the Nose-Hoover thermostat. The friction that controls the particles is itself controlled by a second friction , which is controlled by a third, and so on, in a chain of links. The extended system conserves an energy, and the canonical ensemble is a stationary density of its flow (a theorem). The extra links make the motion chaotic in cases where one thermostat is not, so in practice the chain samples the canonical distribution of small and stiff systems such as one harmonic oscillator. This is a measurement, not a proof of ergodicity.
In plain words
One Nose-Hoover thermostat is a pedal that brakes or speeds up all atoms together. The pedal itself swings in a regular way, and for simple systems the atoms and the pedal fall into a fixed rhythm that never explores all the states.
The chain adds a second pedal that acts on the first one, a third that acts on the second, and so on. Each pedal tries to keep the one before it at the right temperature. The extra pedals break the fixed rhythm, so the motion becomes irregular and fills the space of states. A total energy that includes all pedals stays fixed, so the program can still be checked.
The problem
The input is as for the Nose-Hoover thermostat: positions , velocities , masses , forces , a time step , a number of steps , the target and the number of velocity components that link 1 scales. In addition it takes the masses of the links and start values of and (often 0). The output is the trajectory of the equations of Martyna, Klein and Tuckerman (1992):
with the thermostat forces
and the extended energy after each step,
which the exact flow conserves. Link has one degree of freedom, , to control, which is the reason has and not . In the implementation ; with the total momentum fixed, use . The masses of Martyna, Klein and Tuckerman are and for , with a thermostat period that is a choice.
The algorithm
NOSE-HOOVER-CHAINS(x, v, m, FORCES, h, S, kT, Q_1..Q_M, N_f, xi, eta)- 1
(U, f) ← FORCES(x) - 2
for step ← 1 to S do - 3
CHAIN-HALF(v, xi, eta) - 4
v_i ← v_i + (h / 2) f_i / m_i for each particle i ▷ half kick - 5
x_i ← x_i + h v_i for each particle i ▷ drift - 6
(U, f) ← FORCES(x) - 7
v_i ← v_i + (h / 2) f_i / m_i for each particle i ▷ half kick - 8
CHAIN-HALF(v, xi, eta) - 9
record H' - 10
return (x, v, xi, eta, the recorded H') LINK-QUARTER(j) ▷ exact flows of link j for a time h / 4- 11
if j = M then xi_M ← xi_M + (h / 4) G_M and return - 12
s ← exp(-xi_{j+1} h / 8) - 13
xi_j ← s xi_j - 14
xi_j ← xi_j + (h / 4) G_j - 15
xi_j ← s xi_j CHAIN-HALF(v, xi, eta) ▷ exact flows of the chain for a time h / 2- 16
for j ← M down to 1 do LINK-QUARTER(j) - 17
v_i ← exp(-xi_1 h / 2) v_i for each particle i, and update K - 18
eta_j ← eta_j + (h / 2) xi_j for j ← 1 to M - 19
for j ← 1 to M do LINK-QUARTER(j)
Each line is the exact flow of a part of the equations with the other variables fixed. does not depend on , so line L14 is exact, and the friction term alone is the exponential decay of lines L13 and L15. The order of the sweep, from the end of the chain to the particles and back, makes CHAIN-HALF a palindrome, and with the Verlet step in the middle the whole step is time-reversible and of order 2. This is the splitting of Martyna, Tuckerman, Tobias and Klein (1996), with one stage per half step. With , LINK-QUARTER is one quarter kick, and the step is the Nose-Hoover step.
Example
One step by hand. One particle on a spring with in reduced units, , , , , , all and , so . In the first CHAIN-HALF, , so link 2 gets and (L11). Link 1 is scaled by , which changes nothing since , kicked by to (L14), and scaled again to (L15). The rest of the step goes as on the Nose-Hoover page, and after it
| variable | |||||||
|---|---|---|---|---|---|---|---|
| value | 0.995 | 0.4999797 |
The extended energy is conserved to second order. Two oscillators with , masses 1 and 2, started at , , with , and links with the masses of Martyna, Klein and Tuckerman for , run to :
| 0.1 | 0.05 | 0.025 | |
|---|---|---|---|
| spread of | 0.02196 | 0.005495 | 0.001375 |
The spread falls by a factor of 4 when is halved. A run of 300 steps forward and 300 back with and all flipped recovers the start to .
One oscillator becomes canonical. One oscillator with and , from , , with and a sample every 10 steps:
| thermostat | run length | ||
|---|---|---|---|
| one Nose-Hoover, | 0.793 | 1.538 | |
| chain, , all | 500 | 0.993 | 2.890 |
| chain, , all | 0.980 | 2.913 |
The canonical values are 1 and 3. These are measurements: the chain gives the canonical moments within the sampling error, and one thermostat does not.
Correctness
The proof in proofs/correctness.md shows:
- The extended energy is conserved: in the coupling terms cancel in pairs, and the terms cancel the rest.
- The canonical density is stationary. The density solves the stationary Liouville equation, since the flow has divergence . Its marginal in is canonical, and each is normal with variance .
- The step is a palindrome of exact flows, so it is time-reversible and of order 2.
- Length 1 gives the Nose-Hoover step.
So the method samples the canonical ensemble if the extended flow is ergodic. This is still a hypothesis. The chain does not prove ergodicity; it makes it much more likely in practice, as the measurements show. The dynamics of the particles are disturbed as little as with one thermostat, since only acts on them.
Proof: Nose-Hoover chains keep the extended energy and the canonical density, and the splitting is reversiblefull
The equations of Martyna, Klein and Tuckerman (1992) for a chain of thermostats with masses are
with the thermostat forces
is the number of velocity components that link 1 scales.
Claim 1 (the extended energy). The exact flow of (1) keeps
Proof. The particle terms give , as for one thermostat. For the chain,
The term for cancels the term of the last sum. What is left is . The terms give . The total is 0.
Claim 2 (the canonical density is stationary). The density on is stationary for (1).
Proof. We show for the vector field of (1). The divergence comes from ( times) and for ; does not depend on . So . Next,
by the computation in Claim 1. The sum is 0.
As for one thermostat, the marginal of in is canonical, and time averages are canonical averages only if the flow is ergodic. The chain does not prove ergodicity. It adds variables that make the dynamics chaotic in cases where one thermostat is not, such as one harmonic oscillator (a measurement on the page).
Claim 3 (the splitting). One step is , where and are the exact kick and drift and is a palindrome of exact flows of parts of the chain field:
read from right to left. Here is the exact flow for a time of , with all fixed. For , , with the exact flow for a time of and the exact flow for a time of , all other variables fixed; and .
Proof. multiplies by : lines L12, L13 and L15. depends on or and not on , so adds to : line L14, and line L11 for . Line L16 applies in this order, line L17 scales the velocities by and line L18 adds to each , which is , and line L19 applies . The kinetic energy is updated after the scaling, so the second uses the new . Lines L4 to L7 are the Verlet part.
Claim 4 (time reversal). With , each exact flow above satisfies , and the step is a palindrome of such flows, so .
Proof. Under , and are unchanged (they depend on and on ), so changes the sign of its increment; multiplies by , and with this is for ; the same holds for , and . A palindrome of maps with this property is reversed by , as in the proof for one thermostat.
A symmetric composition of the flows of a split field has order 2, so is kept up to over a fixed time.
Claim 5 (length 1). For the step is the Nose-Hoover step with .
Proof. With there are no maps, and : a quarter kick of by , the scaling of by with , and a quarter kick with the new . These are lines L11 to L14 of the Nose-Hoover page, and reduces to .
Complexity
Each step does one force evaluation, work for the particles and work for the chain, with four exponentials per link. With the all-pairs forces of the molecular dynamics page, steps cost time. The extra space is . In practice is a small number such as 3 to 5, which is a choice, and the chain costs nothing next to the forces.
Properties
- Ensemble. Canonical (NVT) for the marginal, if the extended dynamics is ergodic, up to the time step error.
- Deterministic and time-reversible. The step is a palindrome of exact flows.
- A conserved quantity to monitor. drifts only by integration error.
- Not symplectic. The flow does not keep phase-space volume; it keeps the measure times the volume.
- Robust where one thermostat fails. On one harmonic oscillator and on harmonic solids, measured moments agree with the canonical values.
Variants and improvements
- Multiple time steps in the chain. Martyna, Tuckerman, Tobias and Klein (1996) split the chain part further, into several stages with Suzuki-Yoshida weights, for stiff chains. This page uses one stage per half step. The same idea for the forces is RESPA.
- Massive chains. One chain for each degree of freedom, as in path integral simulations, makes the thermostat act locally.
- Barostats. The constant pressure methods of Martyna, Tobias and Klein put chains on the particles and on the volume or cell variables. The Parrinello-Rahman barostat needs such a thermostat to sample NPT.
- Stochastic thermostats. Langevin dynamics and stochastic velocity rescaling reach ergodicity with noise, and there is a proof for them. A Langevin thermostat on the last link of a chain is a hybrid that some codes use.
When to use it
Use Nose-Hoover chains when a deterministic, time-reversible thermostat with a conserved quantity is wanted and the system is small, stiff or close to harmonic: crystals at low temperature, small molecules, path integral beads. For large disordered systems, one thermostat often works as well. When a proof of ergodicity matters more than a deterministic trajectory, use a stochastic thermostat.
Pitfalls
- Chain masses that are too small. The links then move fast, and the time step must resolve them. Check the spread of .
- Reporting the wrong energy. Only with all terms is conserved.
- A wrong . The first link targets , and must count the constraints and a fixed centre of mass.
- Assuming ergodicity. The chain is a remedy that works well in tests, not a theorem. Check the distribution, not only the mean temperature, for example with on a harmonic test.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/nose-hoover-chains/. The authors are listed in prov/KYF8ST.jsonl.
"""Molecular dynamics with a Nose-Hoover chain thermostat.
The equations of Martyna, Klein and Tuckerman (1992) for N particles in d
dimensions, with a chain of M thermostats of masses Q_1, ..., Q_M, are
dx_i/dt = v_i,
dv_i/dt = f_i / m_i - xi_1 v_i,
dxi_1/dt = G_1 - xi_1 xi_2, G_1 = (2 K - N_f kT) / Q_1,
dxi_j/dt = G_j - xi_j xi_(j+1), G_j = (Q_(j-1) xi_(j-1)^2 - kT) / Q_j,
dxi_M/dt = G_M,
deta_j/dt = xi_j.
The extended energy
H' = K + U + sum_j Q_j xi_j^2 / 2 + N_f kT eta_1 + kT (eta_2 + ... + eta_M)
is conserved by the exact flow. With M = 1 the chain is the Nose-Hoover
thermostat.
One step of length h is the symmetric splitting of Martyna, Tuckerman,
Tobias and Klein (1996): a half step of the chain, a velocity Verlet step of
the particles, and a second half step of the chain. In the half step of the
chain, each xi_j gets a quarter step kick by G_j between two scalings by
exp(-xi_(j+1) h / 8), from the end of the chain to its start; then the
velocities are scaled by exp(-xi_1 h / 2) and all eta_j move by xi_j h / 2;
then the kicks are repeated from the start of the chain to its end. Each
piece is the exact flow of a part of the equations, so the step is
time-reversible. One Suzuki-Yoshida stage is used (no multiple time steps
inside the chain).
Cost: one force evaluation and Theta(N d + M) extra work per step.
"""
import math
from typing import Callable
Vec = list[float]
ForceField = Callable[[list[Vec]], tuple[float, list[Vec]]]
def harmonic(k: float) -> ForceField:
"""Independent springs U = k |x_i|^2 / 2 for each particle."""
def field(pos: list[Vec]) -> tuple[float, list[Vec]]:
u = sum(0.5 * k * c * c for p in pos for c in p)
return u, [[-k * c for c in p] for p in pos]
return field
def kinetic(v: list[Vec], m: list[float]) -> float:
"""The kinetic energy K = sum_i m_i |v_i|^2 / 2."""
return sum(0.5 * mi * sum(c * c for c in vi) for vi, mi in zip(v, m))
def chain_masses(nf: int, kT: float, tau: float, length: int) -> list[float]:
"""The masses of Martyna et al. (1992): Q_1 = N_f kT tau^2, Q_j = kT tau^2."""
return [nf * kT * tau * tau] + [kT * tau * tau] * (length - 1)
def _force(j, xi, q, k, kT, nf):
"""G_j: the thermostat force on link j (0-based)."""
if j == 0:
return (2.0 * k - nf * kT) / q[0]
return (q[j - 1] * xi[j - 1] ** 2 - kT) / q[j]
def _kick(j, xi, q, k, kT, nf, h):
"""A quarter step of link j: scale, kick by G_j, scale."""
mm = len(xi)
if j == mm - 1:
xi[j] += 0.25 * h * _force(j, xi, q, k, kT, nf) # @line L11
return
s = math.exp(-0.125 * h * xi[j + 1]) # @line L12
xi[j] *= s # @line L13
xi[j] += 0.25 * h * _force(j, xi, q, k, kT, nf) # @line L14
xi[j] *= s # @line L15
def _chain_half(v, m, xi, eta, q, h, kT, nf):
"""The exact flows of the chain part for a time h / 2."""
mm = len(xi)
k = kinetic(v, m)
for j in range(mm - 1, -1, -1): # @line L16
_kick(j, xi, q, k, kT, nf, h)
s = math.exp(-0.5 * h * xi[0]) # @line L17
for vi in v:
for a in range(len(vi)):
vi[a] *= s
k *= s * s
for j in range(mm):
eta[j] += 0.5 * h * xi[j] # @line L18
for j in range(mm): # @line L19
_kick(j, xi, q, k, kT, nf, h)
def extended_energy(x, v, m, u, xi, eta, q, kT, nf) -> float:
"""H' = K + U + sum Q_j xi_j^2 / 2 + N_f kT eta_1 + kT sum_(j >= 2) eta_j."""
e = kinetic(v, m) + u + sum(0.5 * qj * z * z for qj, z in zip(q, xi))
return e + nf * kT * eta[0] + kT * sum(eta[1:])
def nose_hoover_chains(
x: list[Vec], v: list[Vec], m: list[float], forces: ForceField, h: float,
steps: int, kT: float, q: list[float], nf: int,
xi: list[float] | None = None, eta: list[float] | None = None,
) -> tuple[list[Vec], list[Vec], list[float], list[float], list[float]]:
"""Run steps of Nose-Hoover chain dynamics with masses q (the chain length is len(q)).
Returns the final positions, velocities, xi and eta, and the extended
energy H' after each step. The inputs are not changed.
"""
if h <= 0 or not q or min(q) <= 0 or kT <= 0 or nf <= 0:
raise ValueError("need h > 0, positive masses q, kT > 0 and nf > 0")
mm = len(q)
xi = list(xi) if xi is not None else [0.0] * mm
eta = list(eta) if eta is not None else [0.0] * mm
x = [list(p) for p in x]
v = [list(p) for p in v]
n = len(x)
u, f = forces(x) # @line L1
energies = []
for _ in range(steps): # @line L2
_chain_half(v, m, xi, eta, q, h, kT, nf) # @line L3
for i in range(n):
for a in range(len(x[i])):
v[i][a] += 0.5 * h * f[i][a] / m[i] # @line L4
x[i][a] += h * v[i][a] # @line L5
u, f = forces(x) # @line L6
for i in range(n):
for a in range(len(x[i])):
v[i][a] += 0.5 * h * f[i][a] / m[i] # @line L7
_chain_half(v, m, xi, eta, q, h, kT, nf) # @line L8
energies.append(extended_energy(x, v, m, u, xi, eta, q, kT, nf)) # @line L9
return x, v, xi, eta, energies # @line L10
test_nose_hoover_chains.py 66 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/nose-hoover-chains/. The authors are listed in prov/KYF8ST.jsonl.
import pytest
from nose_hoover_chains import chain_masses, harmonic, kinetic, nose_hoover_chains
def sample_oscillator(q, steps_per_sample, samples, h=0.05):
"""One oscillator, m = k = kT = 1, from x = 1, v = 0; returns x samples."""
x, v, xi, eta = [[1.0]], [[0.0]], None, None
out = []
for _ in range(samples):
x, v, xi, eta, _ = nose_hoover_chains(x, v, [1.0], harmonic(1.0), h, steps_per_sample, 1.0, q, 1, xi, eta)
out.append(x[0][0])
return out
def test_single_oscillator_becomes_canonical_with_a_chain():
# Canonical values: <x^2> = 1 and <x^4> / <x^2>^2 = 3. A single
# Nose-Hoover thermostat gives about 0.79 and 1.54 on this system.
xs = sample_oscillator([1.0] * 4, 10, 1000)
x2 = sum(x * x for x in xs) / len(xs)
x4 = sum(x ** 4 for x in xs) / len(xs)
assert x2 == pytest.approx(1.0, abs=0.15)
assert x4 / (x2 * x2) == pytest.approx(3.0, abs=0.4)
def test_extended_energy_is_conserved_with_second_order_error():
q = chain_masses(2, 1.0, 1.0, 3)
spreads = []
for h in (0.1, 0.05, 0.025):
n = round(20 / h)
_, _, _, _, es = nose_hoover_chains([[1.0], [0.3]], [[0.0], [0.7]], [1.0, 2.0], harmonic(1.0), h, n, 1.0, q, 2)
spreads.append(max(es) - min(es))
assert spreads[0] / spreads[1] == pytest.approx(4.0, rel=0.2)
assert spreads[1] / spreads[2] == pytest.approx(4.0, rel=0.2)
def test_time_reversible():
q = [0.5, 1.0, 2.0]
m = [1.0, 2.0]
x0, v0 = [[1.0], [0.3]], [[0.0], [0.7]]
x, v, xi, eta, _ = nose_hoover_chains(x0, v0, m, harmonic(1.0), 0.05, 300, 1.0, q, 2)
back = [[-c for c in p] for p in v]
x, v, xi, eta, _ = nose_hoover_chains(x, back, m, harmonic(1.0), 0.05, 300, 1.0, q, 2, [-z for z in xi], eta)
assert x == [pytest.approx(p, abs=1e-9) for p in x0]
assert [[-c for c in p] for p in v] == [pytest.approx(p, abs=1e-9) for p in v0]
assert xi == pytest.approx([0.0, 0.0, 0.0], abs=1e-9)
def test_chain_of_length_one_is_nose_hoover():
# With one link the update is the Nose-Hoover update: the extended
# energy K + U + Q xi^2 / 2 + N_f kT eta is conserved.
x, v, xi, eta, es = nose_hoover_chains([[1.0]], [[0.0]], [1.0], harmonic(1.0), 0.02, 500, 1.0, [1.0], 1)
assert max(abs(e - 0.5) for e in es) < 1e-3
e = kinetic(v, [1.0]) + 0.5 * x[0][0] ** 2 + 0.5 * xi[0] ** 2 + eta[0]
assert e == pytest.approx(es[-1], abs=1e-12)
def test_chain_masses_follow_martyna_klein_tuckerman():
assert chain_masses(30, 2.0, 0.5, 3) == pytest.approx([15.0, 0.5, 0.5])
def test_bad_arguments_raise():
with pytest.raises(ValueError):
nose_hoover_chains([[1.0]], [[0.0]], [1.0], harmonic(1.0), 0.05, 1, 1.0, [1.0, 0.0], 1)
History
Hoover (1985) showed that one Nose-Hoover thermostat does not give the canonical distribution of one harmonic oscillator. Martyna, Klein and Tuckerman (1992) proposed to thermostat the thermostat, proved that the chain keeps the canonical density, and showed in numerical tests that it samples the harmonic oscillator correctly. Martyna, Tuckerman, Tobias and Klein (1996) gave the reversible integrators built from exact flows, including those for the chains and for constant pressure. The method is a standard option in many molecular dynamics codes.
References
- Glenn J. Martyna, Michael L. Klein and Mark Tuckerman, Nose-Hoover chains, The canonical ensemble via continuous dynamics, Journal of Chemical Physics 97(4), 1992, 2635-2643, the equations of the chain, the conserved energy, the proof of the stationary canonical density and the harmonic oscillator test
- Glenn J. Martyna, Mark E. Tuckerman, Douglas J. Tobias and Michael L. Klein, Explicit reversible integrators for extended systems dynamics, Molecular Physics 87(5), 1996, 1117-1157, the factorization of the chain part into exact flows, which gives the reversible step of this page
- Mark E. Tuckerman, Statistical Mechanics, Theory and Molecular Simulation, Oxford University Press, 2010, Chapter 4, the canonical ensemble (Nose-Hoover chains and their integration)
- Ben Leimkuhler and Charles Matthews, Molecular Dynamics, With Deterministic and Stochastic Numerical Methods, Springer, 2015, Chapter 8, extended variable methods
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag KYF8ST Edit this page History Report an error Markdown