Source code for ase.md.nvtberendsen

"""Berendsen NVT dynamics class."""

import numpy as np

from ase import Atoms
from ase.md.md import MolecularDynamics


[docs] class NVTBerendsen(MolecularDynamics): """Berendsen (constant N, V, T) molecular dynamics. Berendsen *et al.*, J. Chem. Phys. 81, 3684–3690 (1984). https://doi.org/10.1063/1.448118 """ def __init__( self, atoms: Atoms, timestep: float, temperature: float | None = None, taut: float | None = None, fixcm: bool = True, *, temperature_K: float | None = None, **kwargs, ): """ Parameters ---------- atoms: Atoms object The list of atoms. timestep: float The time step in ASE time units. temperature: float The desired temperature, in Kelvin. temperature_K: float Alias for *temperature* taut: float Time constant for Berendsen temperature coupling in ASE time units. fixcm: bool (optional) If True, the position and momentum of the center of mass is kept unperturbed. Default: True. **kwargs : dict, optional Additional arguments passed to :class:~ase.md.md.MolecularDynamics base class. Physical background and limitations ----------------------------------- In Berendsen NVT simulations the velocities are scaled to achieve the desired temperature. This method does not result in proper NVT sampling but it usually is sufficiently good in practice (with large `taut`). For discussion see the gromacs manual at https://www.gromacs.org. Example -------- Room temperature simulation (300 K, 0.1 fs time step): >>> from ase import units >>> from ase.build import bulk >>> from ase.calculators.emt import EMT >>> from ase.md import thermalize_momenta >>> from ase.md.nvtberendsen import NVTBerendsen >>> atoms = bulk('Cu', cubic=True) >>> atoms.calc = EMT() >>> thermalize_momenta(atoms, temperature_K=300) >>> dyn = NVTBerendsen(atoms, 0.1 * units.fs, temperature_K=300, ... taut=0.5 * 1000 * units.fs) >>> dyn.run(10) True """ MolecularDynamics.__init__(self, atoms, timestep, **kwargs) if taut is None: raise TypeError("Missing 'taut' argument.") self.taut = taut self.temperature = self._process_temperature( temperature, temperature_K, 'K' ) self.fix_com = fixcm # will the center of mass be held fixed? def set_taut(self, taut): self.taut = taut def get_taut(self): return self.taut def set_temperature(self, temperature=None, *, temperature_K=None): self.temperature = self._process_temperature( temperature, temperature_K, 'K' ) def get_temperature(self): return self.temperature def set_timestep(self, timestep): self.dt = timestep def get_timestep(self): return self.dt def scale_velocities(self): """Do the NVT Berendsen velocity scaling""" tautscl = self.dt / self.taut old_temperature = self.atoms.get_temperature() scl_temperature = np.sqrt( 1.0 + (self.temperature / old_temperature - 1.0) * tautscl ) # Limit the velocity scaling to reasonable values scl_temperature = min(scl_temperature, 1.1) scl_temperature = max(scl_temperature, 0.9) p = self.atoms.get_momenta() p = scl_temperature * p self.atoms.set_momenta(p) def step(self, forces=None): """Move one timestep forward using Berenden NVT molecular dynamics.""" self.scale_velocities() # one step velocity verlet atoms = self.atoms if forces is None: forces = atoms.get_forces(md=True) p = self.atoms.get_momenta() p += 0.5 * self.dt * forces if self.fix_com: # calculate the center of mass # momentum and subtract it psum = p.sum(axis=0) / float(len(p)) p = p - psum self.atoms.set_positions( self.atoms.get_positions() + self.dt * p / self.atoms.get_masses()[:, np.newaxis] ) # We need to store the momenta on the atoms before calculating # the forces, as in a parallel Asap calculation atoms may # migrate during force calculations, and the momenta need to # migrate along with the atoms. For the same reason, we # cannot use self.masses in the line above. self.atoms.set_momenta(p) forces = self.atoms.get_forces(md=True) atoms.set_momenta(self.atoms.get_momenta() + 0.5 * self.dt * forces) return forces