matcalc._md module
Calculator for MD properties.
- class MDCalc(calculator: Calculator, *, ensemble: Literal['nve', 'nvt', 'nvt_nose_hoover', 'nvt_berendsen', 'nvt_langevin', 'nvt_andersen', 'nvt_bussi', 'npt', 'npt_nose_hoover', 'npt_berendsen', 'npt_inhomogeneous', 'npt_mtk', 'npt_isotropic_mtk'] = 'nvt', temperature: float = 300, timestep: float = 1.0, steps: int = 100, pressure: float = 6.324209121801212e-07, taut: float | None = None, taup: float | None = None, friction: float = 0.10180505671156725, andersen_prob: float = 0.01, ttime: float = 25.0, pfactor: float | None = 33.874542329339036, external_stress: float | np.ndarray | None = None, compressibility_au: float | None = None, tchain: int = 3, pchain: int = 3, tloop: int = 1, ploop: int = 1, trajfile: Any = None, logfile: str | None = None, loginterval: int = 1, append_trajectory: bool = False, mask: tuple | np.ndarray | None = None, relax_structure: bool = True, fmax: float = 0.1, optimizer: str = 'FIRE', frames: int | None = None, equilibration_frames: int = 0, relax_calc_kwargs: dict | None = None, set_com_stationary: bool = False, set_zero_rotation: bool = False, seed: int | None = None)[source]
Bases:
PropCalcPerforms molecular dynamics (MD) simulations on the input structure to calculate the final energy and obtain the final structure. Optionally, the structure is relaxed prior to the MD simulation. The simulation setup is flexible, supporting various ensembles (e.g., “nvt”, “nve”, “npt”, etc.) and user-defined parameters such as temperature, time step, and number of steps.
Initializes an MDCalc instance with the specified simulation parameters and relaxation settings.
- Parameters:
calculator (Calculator) – The calculator used for energy, force, and stress evaluations. Default to the provided calculator.
ensemble (str) – Ensemble for MD simulation. Options include “nve”, “nvt_langevin”, “nvt_andersen”, “nvt_bussi”, “npt”, “npt_berendsen”, “npt_nose_hoover”, “npt_mtk”, “npt_isotropic_mtk”. Default to “nvt”.
temperature (float) – Simulation temperature in Kelvin. Default to 300.
timestep (float) – Time step in femtoseconds. Default to 1.0.
steps (int) – Number of MD simulation steps. Default to 100.
pressure (float) – External pressure for NPT simulations (in eV/ų). Default to 1.01325 * units.bar.
taut (float | None) – Time constant for temperature coupling, in ASE internal time units (multiply a value in fs by
ase.units.fs). If None, defaults to 100 * timestep * units.fs. Used by nvt/nvt_nose_hoover (tdamp), nvt_berendsen, nvt_bussi, npt_berendsen, npt_inhomogeneous and npt_mtk/npt_isotropic_mtk (tdamp).taup (float | None) – Time constant for pressure coupling, in ASE internal time units. If None, defaults to 1000 * timestep * units.fs. Used by npt_berendsen, npt_inhomogeneous and npt_mtk/npt_isotropic_mtk (pdamp).
friction (float) – Friction coefficient for nvt_langevin, in ASE internal inverse-time units (multiply a value in 1/fs by
1 / ase.units.fs), passed toase.md.Langevinunchanged. Default to 0.01 / units.fs, i.e. 0.01 fs^-1 (1 / friction = 100 fs, matching the defaulttaut). The default before matcalc v0.8.0 was 1.0e-3, i.e. ~1e-4 fs^-1 (1 / friction ~ 10 ps), which barely thermostats a run of a few thousand steps; passfriction=1.0e-3to reproduce it.andersen_prob (float) – Per-atom, per-step collision probability for nvt_andersen (dimensionless; the mean time between collisions of an atom is timestep / andersen_prob). Default to 1.0e-2.
ttime (float) – Characteristic time scale of the npt/npt_nose_hoover thermostat, in fs (converted to ASE units internally). Default to 25.0. Note that, unlike
taut,taup,frictionandpfactor, this one is in fs rather than ASE units.pfactor (float | None) – Barostat constant of the npt/npt_nose_hoover (Melchionna) dynamics, in ASE internal units (eV/ų x ASE-time²), passed to
ase.md.npt.NPTunchanged. For a barostat time constant ptime and bulk modulus B, setpfactor = (ptime * units.fs) ** 2 * B * units.GPa(ptime in fs, B in GPa). Default to (75 fs)² x 100 GPa ~ 33.9, ASE’s recommended ptime with a typical solid bulk modulus. None disables the barostat: the cell is then fixed and the run is Melchionna Nose-Hoover NVT thermostatted withttime, nottaut; for NVT, prefer ensemble=”nvt”/”nvt_nose_hoover” (Nose-Hoover chain withtaut). Before matcalc v0.8.0 the value was multiplied byunits.fsand defaulted to 75.0**2, i.e. 75**2 * units.fs ~ 553 in ASE units, 16x the documented recipe (an effective ptime of ~300 fs at B = 100 GPa); passpfactor=75.0**2 * units.fsto reproduce it.external_stress (float | np.ndarray | None) – External stress for the “npt”/”npt_nose_hoover” ensembles (a scalar is a pressure). If not provided, defaults to
pressure.compressibility_au (float | None) – Material compressibility in ų/eV. Default to None.
tchain (int) – The number of thermostat variables in the Nose-Hoover thermostat. Default to 3. Only used by IsotropicMTKNPT and MTKNPT.
pchain (int) – The number of barostat variables in the Nose-Hoover barostat. Default to 3. Only used by IsotropicMTKNPT and MTKNPT.
tloop (int) – The number of sub-steps in thermostat integration. Default to 1. Only used by IsotropicMTKNPT and MTKNPT.
ploop (int) – T The number of sub-steps in barostat integration. Default to 1. Only used by IsotropicMTKNPT and MTKNPT.
trajfile (Any) – Trajectory object or file for storing simulation data. Default to None.
logfile (str | None) – Filename for simulation logs. Default to None.
loginterval (int) – Interval (in steps) for logging simulation data. Default to 1.
append_trajectory (bool) – Whether to append to an existing trajectory file. Default to False.
mask (tuple | np.ndarray | None) – Constraint mask for cell deformations in npt/npt_nose_hoover, passed to
ase.md.npt.NPT. A symmetric 3x3 array marks which strain components may change; a 3-tuple (i, j, k) marks which Cartesian axes may change length (shear between free axes stays allowed). If None, defaults to np.eye(3): only the three normal strains fluctuate and all shear strains are frozen, so the cell keeps its angles (in the upper-triangular frame NPT uses). Passmask=(1, 1, 1)(ornp.ones((3, 3))) for a fully flexible cell.relax_structure (bool) – Whether to relax the input structure before MD simulation. Default to True.
fmax (float) – Maximum force tolerance for structure relaxation (in eV/Å). Default to 0.1.
optimizer (str) – Optimizer used for structure relaxation. Default to “FIRE”.
frames (int) – Number of trailing MD frames to average energies over. Default to None, which means all frames (i.e. frames = steps).
equilibration_frames (int) – Skip this many leading frames before the trailing
frameswindow. Use this to discard non-equilibrated state at the start of an MD run. Default 0.relax_calc_kwargs (dict | None) – Additional keyword arguments for the relaxation calculation. Default to None.
set_com_stationary (bool) – Whether to set the center-of-mass momentum to zero after setting up the Maxwell-Boltzmann distribution. Default to False.
set_zero_rotation (bool) – Whether to set the total angular momentum to zero after setting up the Maxwell-Boltzmann distribution. Default to False.
seed – Seed for the initial velocities. None uses NumPy’s global random number generator.
- _initialize_md(atoms: Atoms) Any[source]
Initializes the MD simulation object based on the provided ASE atoms object and simulation parameters.
- Parameters:
atoms (Atoms) – ASE atoms object representing the structure.
- Returns:
An MD simulation object (e.g., an instance of NVTBerendsen, VelocityVerlet, etc.)
- _run_ase(atoms: Atoms) tuple[list[Atoms], Atoms, int][source]
Run the MD with ASE.
- Returns:
The frames of the averaging window, the final frame and the number of frames of this run (frames are recorded every
logintervalsteps).
- _run_lammps(atoms: Atoms) tuple[list[Atoms], Atoms, int][source]
Run the MD with the LAMMPS backend; returns the same as
_run_ase.
- _upper_triangular_cell(atoms: Atoms) None[source]
Transforms the cell of the given atoms object to an upper triangular form if it is not already, as required by the Nose-Hoover NPT implementation.
- Parameters:
atoms (Atoms) – ASE atoms object.
- _window(n_frames: int) list[int][source]
Indices of the averaging window among the
n_framesframes of a run.Drops
equilibration_framesfrom the start of the run, then takes the trailingframesframes.
- calc(structure: Structure | Atoms | dict[str, Any]) dict[str, Any][source]
Performs the MD simulation calculation for the input structure. Prior to generating initial velocities, this method calls the superclass’s calc method for preprocessing and optionally relaxes the structure. It then initializes the atomic velocities using the Maxwell-Boltzmann distribution, runs the MD simulation, and returns the final energy along with the final atomic configuration.
- Parameters:
structure (Structure | Atoms | dict[str, Any]) – Input structure as a Structure instance or a dictionary.
- Returns:
- A dictionary with
final_structure,trajectory(list of ASE Atoms), potential_energy(eV),kinetic_energy(eV),total_energy(eV) averaged over the lastframesframes,_unitsmapping each numeric output to its unit string, plus any relaxation fields merged in.
- A dictionary with
- Return type:
dict