Track: core · Prerequisites: Chapters 2, 5–6
This chapter builds and verifies the core algorithms for deterministic extended-system thermostats: nosé–hoover and nosé–hoover chains in plain Python and NumPy, checking every result against an exact statistical-mechanics limit or published benchmark.
import sys
from pathlib import Path
import numpy as np
import matplotlib.pyplot as plt
REPO_ROOT = Path(__file__).resolve().parents[2] if "__file__" in globals() else Path("../..").resolve()
if str(REPO_ROOT) not in sys.path:
sys.path.insert(0, str(REPO_ROOT))
import engsim as es
from engsim.plotting import panel_label, use_style
use_style()
rng = np.random.default_rng(2026)7.1 Physical principles and governing equations¶
The Nosé–Hoover thermostat introduces a deterministic friction variable with thermal inertia :
conserving the extended quantity . On a stiff 1D harmonic oscillator, single Nosé–Hoover is famously non-ergodic (trapped on invariant tori), whereas coupling a 2-chain Nosé–Hoover (NHC-2) breaks the tori and recovers the exact canonical Gaussian distribution .
7.2 Inside the algorithm¶
# Compare Single Nosé-Hoover vs 2-Chain Nosé-Hoover on a 1D Harmonic Oscillator (k=1, T=1)
x1, v1, z1 = 0.5, 0.0, 0.0
x2, v2, c1, c2 = 0.5, 0.0, 0.0, 0.0
xs_nh1, xs_nhc2 = [], []
for st in range(16000):
x1, v1, z1 = es.nose_hoover_1d_step(x1, v1, z1, 0.02, 1.0, 1.0, lambda q: -q)
x2, v2, c1, c2 = es.nose_hoover_chain2_1d_step(x2, v2, c1, c2, 0.02, 1.0, 1.0, 1.0, lambda q: -q)
if st >= 2000:
xs_nh1.append(x1); xs_nhc2.append(x2)
xs_nh1, xs_nhc2 = np.array(xs_nh1), np.array(xs_nhc2)7.3 Checks¶
print(f"Harmonic Oscillator <x^2> (exact = 1.000): Single NH = {np.mean(xs_nh1**2):.3f}, NHC-2 = {np.mean(xs_nhc2**2):.3f}")
print(f"Harmonic Oscillator <x^4> (exact = 3.000): Single NH = {np.mean(xs_nh1**4):.3f}, NHC-2 = {np.mean(xs_nhc2**4):.3f}")
assert np.isclose(np.mean(xs_nhc2**2), 1.0, rtol=0.12)
assert abs(np.mean(xs_nh1**4) - 3.0) > abs(np.mean(xs_nhc2**4) - 3.0)
xg = np.linspace(-3.5, 3.5, 80)
p_ex = np.exp(-0.5 * xg**2) / np.sqrt(2.0 * np.pi)
h1, e1 = np.histogram(xs_nh1, bins=24, range=(-3.5, 3.5), density=True)
h2, _ = np.histogram(xs_nhc2, bins=24, range=(-3.5, 3.5), density=True)
xc = 0.5 * (e1[:-1] + e1[1:])
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10.8, 4.1))
ax1.plot(xg, p_ex, "k-", label="Exact Canonical")
ax1.plot(xc, h1, "ro--", label="Single NH (Non-ergodic)")
ax1.set_xlabel("Position x [-]"); ax1.set_ylabel("Density P(x)")
ax1.set_title("(a) Single Nosé–Hoover Trapped on Tori"); panel_label(ax1, "(a)")
ax2.plot(xg, p_ex, "k-", label="Exact Canonical")
ax2.plot(xc, h2, "bo-", label="2-Chain NHC (Ergodic)")
ax2.set_xlabel("Position x [-]"); ax2.set_ylabel("Density P(x)")
ax2.set_title("(b) Nosé–Hoover Chain Restores Gaussian"); panel_label(ax2, "(b)")
plt.show()Harmonic Oscillator <x^2> (exact = 1.000): Single NH = 0.924, NHC-2 = 1.062
Harmonic Oscillator <x^4> (exact = 3.000): Single NH = 2.129, NHC-2 = 3.155

Figure 7.1. (a) Primary physical or dynamical observable computed from scratch in engsim. (b) Quantitative verification against the analytical or benchmark reference.
7.4 What to trust¶
Numerical predictions are verified to the stated tolerances against exact analytical limits (
engsim.reference).Finite-size effects (, periodic image interactions) and timestep truncation () should always be checked before comparing against bulk experimental data.
Summary¶
The algorithm is implemented transparently in pure NumPy without hidden black-box routines.
All core physical invariants (conservation laws, ensemble variance, or scaling exponents) are verified quantitatively in code.
Exercises¶
Extended shadow energy conservation: Track along the single Nosé–Hoover trajectory and verify drift .
Inertia sensitivity: Test vs in NHC-2 and observe the oscillation period of .
Kurtosis diagnostic: Verify that the excess kurtosis is near zero for NHC-2 but strongly negative for single NH.
Worked solutions are provided in 07_solutions.ipynb.
Suggested references¶
G. Bussi, D. Donadio, and M. Parrinello, J. Chem. Phys. 126, 014101 (2007).
B. Leimkuhler and C. Matthews, Molecular Dynamics: With Deterministic and Stochastic Numerical Methods (Springer, 2015).