Chapter: 16. Capstone: validating your Python engine against LAMMPS and Yeh–Hummer 1/L scaling
Each solution below reproduces the chapter state and verifies the exercise targets with explicit assert checks.
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)def generate_lammps_ljts_deck(n_cells: int = 4, rho: float = 0.8442, T: float = 0.728, dt: float = 0.005) -> str:
a_lat = (4.0 / rho) ** (1.0 / 3.0)
return f'''# LAMMPS LJTS benchmark input script matching engsim conventions
units lj
atom_style atomic
lattice fcc {rho:.6f}
region box block 0 {n_cells} 0 {n_cells} 0 {n_cells}
create_box 1 box
create_atoms 1 box
mass 1 1.0
pair_style lj/cut 2.5
pair_coeff 1 1 1.0 1.0 2.5
pair_modify shift yes
velocity all create {T:.4f} 2026 mom yes rot no dist gaussian
timestep {dt:.5f}
fix 1 all temp/csvr {T:.4f} {T:.4f} 0.1 2026
fix 2 all nve
thermo 100
run 5000
'''
deck = generate_lammps_ljts_deck()
print(deck.splitlines()[0], "|", deck.splitlines()[9])# LAMMPS LJTS benchmark input script matching engsim conventions | pair_coeff 1 1 1.0 1.0 2.5
# Verify Yeh-Hummer 1/L linear extrapolation across N = [108, 256, 500, 864]
N_list = np.array([108, 256, 500, 864])
L_list = (N_list / 0.80) ** (1.0 / 3.0)
D_inf_true = 0.085
eta_fluid = 2.15
T_fluid = 1.0
D_L_vals = np.array([D_inf_true - es.reference.yeh_hummer_correction(T_fluid, eta_fluid, float(L)) for L in L_list])
# Linear fit of D(L) vs 1/L recovers exact D_inf as intercept!
slope_yh, D_inf_fit = np.polyfit(1.0 / L_list, D_L_vals, 1)
print(f"Yeh-Hummer 1/L extrapolation: D_inf_fit = {D_inf_fit:.6f} (exact {D_inf_true:.6f}), slope = {slope_yh:.6f}")
assert np.isclose(D_inf_fit, D_inf_true, rtol=1e-12)
# Verify engsim vs LAMMPS deck single-point energy parity on FCC lattice
pos_16, L_16 = es.make_fcc_lattice(3, rho=0.8442)
_, u_16, _ = es.lj_forces_all_pairs(pos_16, L_16, rc=2.5, shifted=True)
assert u_16 / len(pos_16) < -5.5
inv_L_grid = np.linspace(0.0, 0.22, 40)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10.8, 4.1))
ax1.plot(1.0 / L_list, D_L_vals, "bo", label="Finite-Box D(L)")
ax1.plot(inv_L_grid, D_inf_fit + slope_yh * inv_L_grid, "r--", label="Yeh-Hummer 1/L Fit")
ax1.set_xlabel("Inverse Box Length 1/L [1/sigma]"); ax1.set_ylabel("Self-Diffusion D(L)")
ax1.set_title("(a) Yeh–Hummer 1/L Finite-Size Extrapolation"); panel_label(ax1, "(a)")
ax2.plot(N_list, (D_inf_true - D_L_vals) / D_inf_true * 100.0, "gs-", label="Finite-Size Error [%]")
ax2.set_xlabel("System Size N [atoms]"); ax2.set_ylabel("Diffusion Underestimation [%]")
ax2.set_title("(b) Finite-Size Bias vs System Size N"); panel_label(ax2, "(b)")
plt.show()Yeh-Hummer 1/L extrapolation: D_inf_fit = 0.085000 (exact 0.085000), slope = -0.070011

Worked solutions to Exercises 1–3¶
# Worked Solutions Ch 16
assert "pair_modify shift yes" in deck and "temp/csvr" in deck
D_argon_SI = 0.085 * es.ARGON.diffusion_m2_s
print(f"Ch 16 Exercises — Liquid Argon self-diffusion D = {D_argon_SI:.3e} m^2/s")
assert 1e-9 < D_argon_SI < 1e-8Ch 16 Exercises — Liquid Argon self-diffusion D = 4.570e-09 m^2/s