Structures and energetics
First-principles molecular dynamics
Run first-principles molecular dynamics at constant temperature (NVT) with a Langevin thermostat, using the ASE MD modules.
AOFDFTCalculator is an ASE calculator, so it works with the ASE molecular dynamics modules (Langevin, VelocityVerlet, ...) as it is. Here crystalline Si (8 atoms) is kept at 300 K with a Langevin thermostat for 200 fs. Because the electronic structure is solved by an SCF calculation at every step, the time evolution of the electron density and atomic charges can be followed as well.
"""
Advance/OF-DFT example: first-principles molecular dynamics with ASE.
Crystalline Si is kept at 300 K with a Langevin thermostat (NVT). The calculator
keeps the converged density matrix and uses it as the initial guess of the next
step, so the SCF of every MD step after the first is short.
Copyright (C) 2026 AdvanceSoft Corporation, All rights reserved.
"""
import matplotlib.pyplot as plt
import numpy as np
from ase import units
from ase.build import bulk
from ase.constraints import FixCom
from ase.io.trajectory import Trajectory
from ase.md.langevin import Langevin
from ase.md.velocitydistribution import MaxwellBoltzmannDistribution, Stationary
from aofdft.calculator import AOFDFTCalculator
VPS_DIR = "path/to/DFT_DATA19_AS/VPS" # edit these two paths for your environment
PAO_DIR = "path/to/DFT_DATA19_AS/PAO"
T_TARGET = 300 # target temperature (K)
DT = 2.0 # time step (fs)
N_STEPS = 100 # number of MD steps (200 fs)
FRICTION = 0.02 / units.fs # friction coefficient of the thermostat
rng = np.random.default_rng(0) # fixed random numbers, to make the run reproducible
atoms = bulk("Si", crystalstructure="diamond", a=5.43, cubic=True) # 8 atoms
atoms.set_constraint(FixCom()) # keeps the centre of mass at rest during the MD
atoms.calc = AOFDFTCalculator(
console = False,
logging_file = "si_md.log",
kin_type = "AS26",
kin_model = "Crystal26",
vps_dir = VPS_DIR,
pao_dir = PAO_DIR,
)
# initial velocities at the target temperature, without centre-of-mass motion
MaxwellBoltzmannDistribution(atoms, temperature_K=T_TARGET, rng=rng)
Stationary(atoms)
# NVT molecular dynamics with a Langevin thermostat
dyn = Langevin(atoms, timestep=DT * units.fs, temperature_K=T_TARGET, friction=FRICTION,
fixcm=False, rng=rng)
traj = Trajectory("si_md.traj", "w", atoms)
dyn.attach(traj.write, interval=1)
times, temperatures = [], []
def report():
e_pot = atoms.get_potential_energy() / len(atoms)
times.append(dyn.nsteps * DT)
temperatures.append(atoms.get_temperature())
print(f"step {dyn.nsteps:3d} t = {times[-1]:5.1f} fs E_pot = {e_pot:.5f} eV/atom "
f"T = {temperatures[-1]:6.1f} K", flush=True)
dyn.attach(report, interval=1)
dyn.run(N_STEPS)
traj.close()
n_last = N_STEPS // 2
print(f"Mean temperature of the last {n_last} steps: {np.mean(temperatures[-n_last:]):.0f} K")
# temperature as a function of time
fig, ax = plt.subplots(figsize=(6.0, 3.6))
ax.plot(times, temperatures)
ax.axhline(T_TARGET, color="gray", linestyle="--", linewidth=0.8)
ax.set_xlabel("Time (fs)")
ax.set_ylabel("Temperature (K)")
fig.tight_layout()
fig.savefig("si_md_temperature.png", dpi=150)
print("Saved si_md_temperature.png")
Example output
Results with v1.0.0 (Crystal26).
step 0 t = 0.0 fs E_pot = -115.26326 eV/atom T = 246.9 K
step 1 t = 2.0 fs E_pot = -115.26285 eV/atom T = 233.3 K
step 2 t = 4.0 fs E_pot = -115.26130 eV/atom T = 291.4 K
...
step 98 t = 196.0 fs E_pot = -115.23784 eV/atom T = 401.5 K
step 99 t = 198.0 fs E_pot = -115.23993 eV/atom T = 371.9 K
step 100 t = 200.0 fs E_pot = -115.24096 eV/atom T = 367.8 K
Mean temperature of the last 50 steps: 306 K
Saved si_md_temperature.png

- In the first 20 fs or so the temperature falls to 120 K. When the run starts from the perfect crystal, kinetic energy turns into potential energy as the atoms start to move. The thermostat then supplies energy, and the temperature reaches 300 K at 34 fs.
- After that the temperature fluctuates between about 160 and 450 K. The fluctuations are large because the system has only 8 atoms, but the mean over the last 50 steps is 306 K, in agreement with the temperature of the thermostat.
Notes
- The temperature is set with
temperature_KofLangevin.frictionis the strength of the thermostat;0.02 / units.fscorresponds to a relaxation time of 50 fs. - The initial velocities are given by
MaxwellBoltzmannDistribution, andStationaryremoves the motion of the centre of mass. Since the velocities are random, the initial temperature of a small system differs from the requested value (247 K in this example). FixComis a constraint that fixes the centre of mass of the system. The Langevin thermostat applies random forces to the atoms, so without the constraint the centre of mass slowly moves.Langevinalso has the argumentfixcmfor this purpose, but it is deprecated since ASE 3.28, sofixcm=Falseis combined withFixCom.- The seed of the random numbers is fixed with
np.random.default_rng(0), so the initial velocities and the random forces of the thermostat are the same in every run. - From the second step on, the density matrix converged in the previous step is used automatically as the initial guess of the SCF. In this run the SCF took 11 iterations in the first step and 5 - 6 iterations in the later steps.
- The time step is 2 fs. The appropriate value depends on the elements and the temperature. For how to choose the time step, and for other thermostats, see Molecular dynamics of liquid Al and the radial distribution function.
- For NVE molecular dynamics without a thermostat, use
ase.md.verlet.VelocityVerlet. With the default settings the total energy changes slightly even in NVE, because of the preconditioning that stabilizes the SCF. The settings for runs in which the conservation of the total energy matters are also explained in the Notes of that article. - The trajectory is saved in the ASE
Trajectoryformat; view it withase gui si_md.traj. - The cost grows linearly with the number of atoms, which brings first-principles MD within reach for system sizes that are difficult for Kohn-Sham DFT. A GPU is recommended for large systems.