Perform Ackland-Jones Analysis (AJA) to identify crystal structures.
The Ackland-Jones Analysis is a structure identification method that classifies
local atomic environments based on coordination patterns and bond angles. It can
distinguish between FCC, BCC, HCP, and other common crystal structures.
Parameters:
data (pl.DataFrame) – Atomic data containing at least the columns ‘x’, ‘y’, ‘z’ for positions.
Calculate Angular Distribution Function (ADF) for bond angle analysis.
The angular distribution function quantifies the distribution of bond angles
between atomic triplets (i-j-k), where i is the central atom. This is useful
for characterizing local atomic structure and coordination environments.
Parameters:
data (pl.DataFrame) – Atomic data containing positions and element information.
Must have columns: ‘x’, ‘y’, ‘z’, ‘element’.
box (Box) – Simulation box object with lattice parameters and boundaries.
rc_dict (dict of str to list of float) – Dictionary mapping triplet patterns to cutoff radii.
Format: {‘A-B-C’: [rc_AB_min, rc_AB_max, rc_AC_min, rc_AC_max]}
where A, B, C are element symbols and A is the central atom.
Example: {‘O-H-H’: [0, 2.0, 0, 2.0]} for water molecules.
nbin (int) – Number of bins for angle discretization (0-180 degrees).
fig (Figure, optional) – Existing matplotlib figure. If None, a new figure is created.
ax (Axes, optional) – Existing matplotlib axes. If None, new axes are created.
Returns:
fig (Figure) – Matplotlib figure object.
ax (Axes) – Matplotlib axes object.
Notes
The plot shows bond angle distribution as a function of angle (0-180°)
for all triplet patterns specified in rc_dict. Each pattern is shown
as a separate line with markers.
Calculate atomic-level shear and volumetric strain tensors.
This class computes local strain at each atom by comparing neighbor distances
between a reference (undeformed) configuration and a current (deformed)
configuration. The method calculates the deformation gradient tensor F and
derives strain measures from it.
Parameters:
rc (float) – Cutoff radius for neighbor search in Angstroms.
affine (bool, default=False) – Whether to apply affine transformation to map current box to reference box.
If True, removes global box deformation to isolate local atomic strain.
max_neigh (int, optional) – Maximum number of neighbors to consider. If None, automatically determined.
where the sum is over all neighbors j within cutoff rc, and
\(\Delta r^{ref}\) and \(\Delta r^{cur}\) are neighbor
distance vectors in reference and current configurations.
The affine transformation option is useful when the simulation box itself
undergoes deformation (e.g., under stress). Setting affine=True removes this
global deformation to isolate local atomic-level strain.
References
Examples
Calculate atomic strain between two configurations:
Compute atomic strain for the current configuration.
This method calculates shear and volumetric strain for each atom by
comparing neighbor distances with the reference configuration. Results
are added as new columns ‘shear_strain’ and ‘volumetric_strain’ to
the current system’s data.
Parameters:
current (System) – Current (deformed) atomic system. Must have the same number of atoms
as the reference system.
Raises:
AssertionError – If current system has different number of atoms than reference.
Notes
The computed strain values are added directly to current.data:
shear_strain: Von Mises equivalent shear strain at each atom
volumetric_strain: Hydrostatic (volumetric) strain at each atom
Positive volumetric strain indicates expansion, negative indicates
compression. Shear strain is always non-negative and indicates local
distortion.
If affine=True was set during initialization, the current configuration
is first transformed to remove global box deformation before calculating
local atomic strain.
Calculate atomic temperature from velocity fluctuations, in the units of A/fs.
This class computes the local atomic temperature for each atom by analyzing
the velocity fluctuations of the atom and its neighbors. The temperature is
calculated from the kinetic energy distribution.
Parameters:
data (pl.DataFrame) – Atomic data containing at least ‘vx’, ‘vy’, ‘vz’ velocity columns and
either ‘amass’ (atomic mass) or ‘element’ columns.
verlet_list (np.ndarray) – Neighbor list array of shape (N, max_neigh).
distance_list (np.ndarray) – Distance list array of shape (N, max_neigh).
rc (float) – Cutoff radius for neighbor consideration.
factor (float, default=1.0) – Scaling factor for velocities (e.g., for unit conversion).
Computed atomic temperatures (K) after calling compute().
Type:
np.ndarray
Notes
The atomic temperature represents the local kinetic temperature based on
velocity fluctuations. It differs from the thermodynamic temperature and
is useful for identifying hot spots, shock fronts, or temperature gradients.
Analyze bond length and bond angle distributions in atomic systems.
This class computes histograms of bond lengths (distances between bonded
atoms) and bond angles (angles formed by atomic triplets i-j-k where i is
the central atom). These distributions are useful for characterizing local
structure and identifying coordination patterns.
Parameters:
data (pl.DataFrame) – Atomic data containing positions. Must have columns: ‘x’, ‘y’, ‘z’.
box (Box) – Simulation box object with lattice parameters and boundaries.
rc (float) – Cutoff radius for bond analysis in Angstroms. Pairs within this distance
are considered bonded.
nbin (int) – Number of histogram bins for both bond length (0 to rc) and bond angle
(0 to 180 degrees) distributions.
The Box class represents a parallelepiped simulation cell that can be either
orthogonal or triclinic (non-orthogonal). It supports periodic boundary conditions
and provides methods for coordinate transformations and box manipulations.
Parameters:
box (int, float, Iterable[float], np.ndarray, or Box) –
Defines the box vectors or size:
int/float: Creates a cubic box with given edge length
Iterable[float] of length 3: Creates an orthogonal box with given dimensions [Lx, Ly, Lz]
np.ndarray of shape (3,3): Full box matrix with vectors as rows
np.ndarray of shape (4,3): Box matrix with origin as 4th row
Box: Creates a deep copy of an existing Box object
boundary (Iterable[int] or np.ndarray, optional) –
Boundary condition flags for each dimension (x, y, z):
1: Periodic boundary condition
0: Fixed (non-periodic) boundary
Defaults to [1, 1, 1] (all periodic).
origin (Iterable[float] or np.ndarray, optional) – The origin position of the box in 3D space.
Defaults to [0.0, 0.0, 0.0].
The inverse of the box matrix for coordinate transformations.
Type:
np.ndarray
Examples
Creating different types of boxes:
importnumpyasnpfrommdapy.boximportBox# Cubic box with edge length 10cubic_box=Box(10)# Orthogonal box with different dimensionsortho_box=Box([10,20,30])# Triclinic box from full matrixmatrix=np.array([[10,0,0],[5,10,0],[0,0,10]])triclinic_box=Box(matrix)# Box with custom origin and boundary conditionscustom_box=Box([20,20,20],boundary=[1,1,0],# z-direction non-periodicorigin=[5,5,5])# Copy an existing boxbox_copy=Box(cubic_box)
Applying periodic boundary conditions:
# Create a boxbox=Box(10)# Apply PBC to a displacement vectorrij=np.array([12,-8,5])# Vector that may cross boundariesrij_pbc=box.pbc(rij)print(f"Original: {rij}")print(f"After PBC: {rij_pbc}")
Notes
The box matrix convention follows the standard where box vectors are rows:
Build crystal structure with optional Miller indices orientation.
This function creates a crystal structure with specified lattice type and orientation.
It supports standard crystallographic structures (FCC, BCC, HCP, diamond, graphene) and
allows custom orientations via Miller indices for cubic structures.
Parameters:
name (str) – Element symbol (e.g., ‘Cu’, ‘Al’, ‘Mg’, ‘C’).
structure (str) –
Crystal structure type. Supported values:
'fcc': Face-centered cubic
'bcc': Body-centered cubic
'diamond': Diamond cubic
'hcp': Hexagonal close-packed
'graphene': Graphene layer
a (float) –
Lattice constant in Angstroms.
For cubic structures (FCC/BCC/diamond): edge length of cubic unit cell
For HCP: basal plane lattice parameter
For graphene: C-C bond length
miller1 (tuple of int, optional) – First Miller index (h, k, l) defining the x-axis direction.
Only applicable for cubic structures. If None, uses standard orientation [1,0,0].
miller2 (tuple of int, optional) – Second Miller index (h, k, l) defining the y-axis direction.
Only applicable for cubic structures. If None, uses standard orientation [0,1,0].
miller3 (tuple of int, optional) –
Third Miller index (h, k, l) defining the z-axis direction.
Only applicable for cubic structures. If None, uses standard orientation [0,0,1].
Note
The three Miller indices must be mutually orthogonal and satisfy the
right-hand rule: miller1 × miller2 = miller3.
nx (int, default=1) – Number of repetitions along x-axis direction.
ny (int, default=1) – Number of repetitions along y-axis direction.
nz (int, default=1) – Number of repetitions along z-axis direction.
c_over_a (float, default=sqrt(8/3)) – Ratio of c to a for HCP structure. Default value gives ideal HCP packing.
Ignored for other structures.
Returns:
System – A System object containing the built crystal structure with atomic positions
and simulation box.
Raises:
ValueError –
If an unsupported structure type is specified
- If Miller indices are provided for non-cubic structures
- If Miller indices are not mutually orthogonal
- If Miller indices don’t satisfy the right-hand rule
Build a high-entropy alloy (HEA) crystal structure with an optional orientation
defined by Miller indices.
Parameters:
element_list (Tuple[str]) – List of element symbols, e.g., ('Cu','Al','Mg').
element_ratio (Tuple[float]) – Corresponding atomic ratios. The sum must equal 1.
structure (str) –
Crystal structure type. Supported values are:
'fcc' — Face-centered cubic
'bcc' — Body-centered cubic
'hcp' — Hexagonal close-packed
a (float) – Lattice constant in Ångströms.
- For cubic structures (FCC/BCC): edge length of the cubic unit cell.
- For HCP: basal plane lattice parameter.
miller1 (tuple of int, optional) –
Miller indices (h,k,l) defining the orientation of x-, y-, and z-axes.
Only applicable to cubic structures.
If any are None, the standard orthogonal orientation ([1,0,0], [0,1,0], [0,0,1]) is used.
Note
The three Miller indices must be orthogonal and follow the right-hand rule:
miller1×miller2=miller3.
miller2 (tuple of int, optional) –
Miller indices (h,k,l) defining the orientation of x-, y-, and z-axes.
Only applicable to cubic structures.
If any are None, the standard orthogonal orientation ([1,0,0], [0,1,0], [0,0,1]) is used.
Note
The three Miller indices must be orthogonal and follow the right-hand rule:
miller1×miller2=miller3.
miller3 (tuple of int, optional) –
Miller indices (h,k,l) defining the orientation of x-, y-, and z-axes.
Only applicable to cubic structures.
If any are None, the standard orthogonal orientation ([1,0,0], [0,1,0], [0,0,1]) is used.
Note
The three Miller indices must be orthogonal and follow the right-hand rule:
miller1×miller2=miller3.
nx (int, default=1) – Number of unit cell repetitions along x, y, and z directions.
ny (int, default=1) – Number of unit cell repetitions along x, y, and z directions.
nz (int, default=1) – Number of unit cell repetitions along x, y, and z directions.
c_over_a (float, default=sqrt(8/3)) – The c/a ratio for the HCP structure.
The default corresponds to the ideal HCP packing.
Ignored for other structures.
random_seed (int, optional) – Random seed for reproducible element assignment.
Returns:
System – A System object containing the generated crystal structure
with atomic positions and simulation box information.
Generate a FCC strcuture with a pair partial dislocations along y axis.
The crystalline orientation is: x->[1-10], y->[11-2], z->[111]. If element_list and
element_ratio are given, it will generate a HEA.
Similar to Construct a dislocation by superimposing two crystals in atomsk: https://atomsk.univ-lille.fr/tutorial_Al_edge.php
Parameters:
name (str) – Element symbol (e.g., ‘Cu’, ‘Al’, ‘Mg’, ‘C’).
a (float) – Lattice constant in Angstroms.
nx (int, default=1) – Number of repetitions along x-axis direction.
ny (int, default=1) – Number of repetitions along y-axis direction.
nz (int, default=1) – Number of repetitions along z-axis direction.
element_list (Tuple[str], optional) – List of element symbols, e.g., ('Cu','Al','Mg').
element_ratio (Tuple[float], optional) – Corresponding atomic ratios. The sum must equal 1.
random_seed (int, optional) – Random seed for reproducible element assignment.
Returns:
System – A System object containing the dislocations.
This module defines the abstract base class for implementing static
calculators in the mdapy project. All calculator implementations should inherit
from this class and implement the required abstract methods.
Abstract base class for static property calculators.
This class defines the interface that all calculators must implement.
Calculators are responsible for computing physical properties such as
energies, forces, stresses, and virials for a given atomic configuration.
Dictionary storing computed results. Keys typically include:
‘energies’: per-atom potential energies
‘forces’: per-atom forces
‘stress’: system stress tensor
‘virials’: per-atom virial tensors
‘energy’: system potential energy
Type:
dict
Notes
All subclasses must implement the five abstract methods defined here.
The data parameter should contain atomic positions and elements, while
the box parameter describes the simulation cell geometry.
np.ndarray – 1D array of shape (6,) containing stress tensor components in Voigt notation:
[σ_xx, σ_yy, σ_zz, σ_yz, σ_xz, σ_xy]
Notes
The stress tensor is symmetric, so only 6 independent components are returned.
Stress is typically computed from virial: σ = -(virial + virial^T) / (2 * volume)
np.ndarray – 2D array of shape (N, 9) containing virial tensor components for each atom.
Components ordered as: [v_xx, v_xy, v_xz, v_yx, v_yy, v_yz, v_zx, v_zy, v_zz]
where N is the number of atoms
Calculate the Centro-Symmetry Parameter (CSP) for detecting crystal defects.
The Centro-Symmetry Parameter is a structural metric that measures the local
symmetry of an atom’s environment. It is particularly useful for identifying
defects in centro-symmetric structures like FCC and BCC crystals.
Parameters:
data (pl.DataFrame) – Atomic data containing at least ‘x’, ‘y’, ‘z’ position columns.
Perform cluster analysis based on atomic connectivity within a cutoff distance.
This class identifies connected groups of atoms (“clusters”) by examining
neighbor relationships. Two atoms belong to the same cluster if their
interatomic distance is smaller than a given cutoff value.
The cutoff can be either:
a single scalar value (applied to all atom pairs), or
a dictionary defining different cutoffs between atom types,
e.g. {"1-1":1.2,"1-2":1.5,"2-2":1.8}.
Parameters:
rc (float, int, or dict of str to float) – The cutoff distance for bonding. If a dict is provided, the keys should
be in the form "type1-type2" (e.g. "1-2"), and type_list
must be given.
verlet_list (np.ndarray) – The neighbor list array, where each row contains neighbor indices for
each atom.
distance_list (np.ndarray) – The corresponding neighbor distances for each atom.
neighbor_number (np.ndarray) – The number of neighbors for each atom.
type_list (np.ndarray, optional) – Atom type array. Required if rc is a dict specifying per-type cutoffs.
verlet_list (Optional[np.ndarray], default=None) – Precomputed neighbor indices (optional). If not provided, neighbors
will be determined automatically.
neighbor_number (Optional[np.ndarray], default=None) – Number of neighbors for each atom (required if verlet_list is provided).
rc (Optional[float], default=None) – Cutoff radius for fixed-cutoff CNA. If None, adaptive cutoff is used.
Array of integer codes indicating the coordination type of each atom.
Type:
np.ndarray
Notes
Adaptive cutoff automatically finds neighbors based on the 14 nearest atoms.
Fixed cutoff requires the rc parameter and uses all neighbors within rc.
References
Faken D, Jónsson H. Systematic analysis of local atomic structure combined with 3D computer graphics[J]. Computational Materials Science, 1994, 2(2): 279-286.
Stukowski A. Structure identification methods for atomistic simulations of crystalline materials[J]. Modelling and Simulation in Materials Science and Engineering, 2012, 20(4): 045021.
Calculate the Common Neighbor Parameter (CNP) for structural analysis.
The Common Neighbor Parameter quantifies the local structural order by analyzing
the bonding topology between neighbors. It is effective for distinguishing between
crystalline and disordered regions.
Parameters:
data (pl.DataFrame) – Atomic data containing at least ‘x’, ‘y’, ‘z’ position columns.
Common Neighbor Parameters after calling compute(). Shape (N,).
Values closer to 0 indicate more ordered/crystalline structures,
while larger values indicate disordered regions.
Type:
np.ndarray
Notes
The CNP is calculated by analyzing the connectivity graph of nearest neighbors.
For each atom, it examines which of its neighbors are also neighbors of each other,
forming a local bonding topology signature.
Create polycrystalline structures using Voronoi tessellation method.
This class generates polycrystalline atomic structures by:
1. Creating Voronoi cells from seed positions
2. Filling each cell with rotated unit cell atoms
3. Optionally decorating grain boundaries with graphene layers
4. Removing overlapping atoms based on distance criteria
Parameters:
unitcell (System) – The unit cell structure to be replicated in each grain.
box (Union[int, float, Iterable[float], np.ndarray, Box]) – Simulation box dimensions. Can be a single value (cubic),
array of 3 values, or Box object.
seed_number (int) – Number of grains (Voronoi seeds) to generate.
seed_position (Optional[np.ndarray], default=None) – Positions of Voronoi seeds with shape (seed_number, 3).
If None, randomly generated within the box.
theta_list (Optional[np.ndarray], default=None) – Rotation angles (in degrees) for each grain with shape (seed_number, 3).
Represents rotations around x, y, z axes. If None, randomly generated.
randomseed (Optional[int], default=None) – Random seed for reproducibility. If None, randomly chosen.
metal_overlap_dis (Optional[float], default=None) – Minimum allowed distance between metal atoms (Angstrom).
Atoms closer than this will be removed.
add_graphene (bool, default=False) – Whether to add graphene layers at grain boundaries.
metal_gra_overlap_dis (float, default=3.0) – Minimum allowed distance between metal and carbon atoms (Angstrom).
face_threshold (float, default=0.0) – Minimum face area (Angstrom²) for graphene decoration.
Faces smaller than this will be skipped.
need_rotation (bool, default=True) – Whether to apply random rotations to grains.
This module provides fundamental atomic data used throughout MDAPY, including
chemical symbols, atomic numbers, van der Waals radii, and atomic masses (copy from ase.data).
All data are stored as immutable NumPy arrays or Python dictionaries for fast lookup
and consistent use across simulations and analysis.
The data originate from reliable references such as IUPAC atomic weight tables and
peer-reviewed compilations of van der Waals radii.
References
S. Alvarez, A cartography of the van der Waals territories, Dalton Trans., 2013, 42, 8617–8636.
DOI: 10.1039/C3DT50599E
J. Meija et al., Atomic weights of the elements 2013 (IUPAC Technical Report),
Pure and Applied Chemistry, 88(3), 265–291 (2016). DOI: 10.1515/pac-2015-0305
NumPy array of van der Waals radii (in Å).
Values are based on Alvarez (2013). Some elements have np.nan where data are unavailable.
The array is read-only.
NumPy array of standard atomic weights (in unified atomic mass units, u).
Values follow the IUPAC 2013 report (Meija et al., 2016).
For elements without stable isotopes, the mass of the most stable isotope is used.
The array is read-only.
// Define default names, colors, and radii for some predefined particle types.
//
// Van der Waals radii have been adopted from the VMD software, which adopted them from A. Bondi, J. Phys. Chem., 68, 441 - 452, 1964,
// except the value for H, which was taken from R.S. Rowland & R. Taylor, J. Phys. Chem., 100, 7384 - 7391, 1996.
// For radii that are not available in either of these publications use r = 2.0.
// The radii for ions (Na, K, Cl, Ca, Mg, and Cs) are based on the CHARMM27 Rmin/2 parameters for (SOD, POT, CLA, CAL, MG, CES).
//
// Colors and covalent radii of elements marked with ‘//’ have been adopted from OpenBabel.
Here we time 2 for radius.
Here is the code to extract the radius and color information:
res = [i.split(’, ‘)[:5] for i in context.split(‘ParticleType::PredefinedChemicalType’)[2:]]
ele_rgb, ele_radius = {}, {}
for i in res:
ele, r, g, b, size = i
#print(ele)
ele = ele.split(‘(‘)[-1].split(‘”’)[1]
if ‘/’ in r:
L, R = r.split(‘(‘)[1].split(‘/’)
r = float(L[:-1]) / float(R[:-1])
L, R = g.split(‘/’)
g = float(L[:-1]) / float(R[:-1])
L, R = b.split(‘/’)
b = float(L[:-1]) / float(R[:-2])
else:
r = float(r.split()[1].replace(‘f’, ‘’))
g = float(g.split()[0].replace(‘f’, ‘’))
b = float(b.split()[0][:-1].replace(‘f’, ‘’))
This module implements the Embedded Atom Method (EAM) for static simulations.
The EAM potential is widely used for metallic systems and consists of two main components:
Embedding energy: \(F_i(\rho_i)\) - energy to embed atom i into the electron density \(\rho_i\)
Pair interaction: \(\phi_{ij}(r_{ij})\) - pair potential between atoms i and j
This class implements the EAM potential for metallic systems, supporting both
single-element and multi-element alloy systems. It reads EAM potential files
in the LAMMPS alloy format and provides methods to calculate energies, forces,
stresses, and virials.
Parameters:
filename (str) – Path to the EAM potential file in LAMMPS alloy format.
NDArray[np.float64] – Per-atom virial tensors. Shape: (N, 9) where N is the number of atoms.
Components: [W_xx, W_xy, W_xz, W_yx, W_yy, W_yz, W_zx, W_zy, W_zz].
Units: eV.
Generate average EAM (A-atom) potential for alloy investigation.
This class creates an average EAM potential, which is useful in alloy investigation.
The A-atom potential has a similar formula to the original EAM potential:
Generator for EAM (Embedded Atom Method) potential files.
This class creates EAM potential files in the eam.alloy format for single
or multi-element systems. The implementation is based on the Zhou-Johnson-Wadley
parameterization.
Parameters:
elements_list (List[str]) – List of element symbols (e.g., [‘Cu’, ‘Ni’, ‘Al’]).
output_filename (str, optional) – Name of the output file. If None, generates name from elements.
nr (int, optional) – Number of points in distance grid (default: 2000).
nrho (int, optional) – Number of points in electron density grid (default: 2000).
ValueError – If any element is not in the supported elements list.
References
Zhou, X. W., Johnson, R. A., & Wadley, H. N. G. (2004).
Misfit-energy-increasing dislocations in vapor-deposited CoFe/NiFe multilayers.
Physical Review B, 69(14), 144113.
https://doi.org/10.1103/PhysRevB.69.144113
Examples
>>> gen=EAMGenerator(["Cu","Ni"])>>> # This automatically generates a 'CuNi.eam.alloy' file
A class to compute the elastic constants of a crystal structure
using finite strain and stress analysis based on molecular dynamics
or energy minimization simulations.
The workflow involves applying small deformations to the simulation box,
relaxing the atomic structure, and evaluating the resulting stress
to fit the elastic stiffness tensor components.
Parameters:
system (System) – The initial system to be used for deformation and stress calculation.
This modifier analyzes the coordination structure of particles to identify diamond
lattice structures, including both cubic and hexagonal diamond polymorphs. The
identification is performed using an extended Common Neighbor Analysis method that
considers both first and second nearest neighbor shells.
The pattern array assigns one of the following structure type identifiers to each particle:
0 Other
1 Cubic diamond
2 Cubic diamond (1st neighbor)
3 Cubic diamond (2nd neighbor)
4 Hexagonal diamond
5 Hexagonal diamond (1st neighbor)
6 Hexagonal diamond (2nd neighbor)
Parameters:
data (pl.DataFrame) – Particle properties data frame containing columns: ‘x’, ‘y’, ‘z’ for positions.
verlet_list (NDArray[np.int32], optional) – Pre-computed neighbor list for each particle with at least 4 neighbors.
If None, k-nearest neighbor search (k=4) is performed automatically.
Shape: (N, k) where N is the number of particles and k ≥ 4.
Structure type classification for each particle. Available after calling compute().
Shape: (N,) where N is the number of particles.
Type:
NDArray[np.int32]
References
[1] Maras E, Trushin O, Stukowski A, et al. Global transition path search for dislocation formation in Ge on Si (001)[J]. Computer Physics Communications, 2016, 205: 13-21.
Identify planar faults in FCC structures including stacking faults, twin boundaries, and ESF.
This class identifies intrinsic and extrinsic stacking faults as well as coherent twin
boundaries in FCC crystal structures. The algorithm is based on the method described in
the OVITO documentation [1]_, with an additional capability to identify Extrinsic
Stacking Faults (ESF).
Parameters:
structure_types (np.ndarray) – Array of structure type identifiers for each atom, shape (N,).
Typically from PTM analysis where 1=FCC, 2=HCP, etc.
ptm_indices (np.ndarray) – PTM nearest neighbor indices for each atom, shape (N, 12).
Contains the indices of the 12 nearest neighbors in FCC/HCP ordering.
cal_esf (bool, default=True) – Whether to identify Extrinsic Stacking Faults (ESF) in addition to
intrinsic stacking faults and twin boundaries.
Perform a nearest-neighbor search for atoms within a periodic or non-periodic box.
This class computes the indices and distances of the k nearest neighbors
for each atom, considering periodic boundary conditions (PBC) if applicable.
For small systems, where the number of atoms is less than k,
the simulation box is automatically replicated according to its
boundary conditions to ensure enough neighbors are available.
Parameters:
data (pl.DataFrame) – A Polars DataFrame containing atomic coordinates with columns
"x", "y", and "z".
box (Box) – The simulation box, defined as an instance of mdapy.box.Box.
k (int) – The number of nearest neighbors to search for.
Must be less than 25.
Compute the nearest neighbors for all atoms in the system.
If the number of atoms is smaller than k and periodic boundaries
are enabled, the system will be automatically replicated along the
periodic directions to ensure sufficient neighbors are found.
Returns:
None – Results are stored in the following attributes:
LAMMPS-based calculator that runs a single-point evaluation to obtain
per-atom energies, forces, virials and the global stress.
Parameters:
pair_parameter (str) – The LAMMPS pair style / pair coeff commands as a single string.
This string is passed directly to LAMMPS with commands_string.
element_list (List[str]) – List of element names supported by this potential. The index in this
list defines the corresponding LAMMPS atom type (1-based).
units (str, optional) – Units for LAMMPS (default "metal"). Currently the code asserts
that units == “metal”.
centroid_stress (bool, optional) – If True, uses compute centroid/stress/atom NULL in LAMMPS;
otherwise uses compute stress/atom NULL.
Run LAMMPS to calculate per-atom energies, forces and virials and
compute global stress.
This function validates inputs, constructs a triclinic LAMMPS box,
creates atoms, sets up computes, runs run 0, extracts LAMMPS
computed quantities, converts units, reorders virials, and stores
results in self.results.
Parameters:
data (polars.DataFrame) – Polars DataFrame with required columns: “x”, “y”, “z”, “element”.
The method relies on lammps Python bindings to exist and provide:
- lammps(cmdargs=…), commands_string, create_atoms,
- numpy.extract_atom(…), numpy.extract_compute(…),
- numpy.extract_atom(“f”), and .close().
Virial unit conversion: virial = virial / 1e4 / 160.21766208
(converts LAMMPS reported units to eV).
The final global stress is computed as:
stress = -(virial_tensor + virial_tensor.T) / (2 * box.volume)
and returned in Voigt order [xx, yy, zz, yz, xz, xy].
Calculate the Lindemann index for distinguishing melt processes.
This class computes the Lindemann index,
which is useful for distinguishing the melting process and determining the melting points
of nano-particles. The Lindemann index is defined as the root-mean-square bond-length
fluctuation with the following mathematical expression:
where \(N_p\) is the number of particles, \(r_{ij}\) is the distance between
atom \(i\) and \(j\), and the brackets \(\left\langle \right\rangle_t\)
represent a time average.
Parameters:
pos_list (np.ndarray) –
Array of particle positions with shape (\(N_f\), \(N_p\), 3), where
\(N_f\) is the number of frames and \(N_p\) is the number of particles.
Warning
The positions MUST be unwrapped coordinates! For systems with periodic
boundary conditions, you must provide unwrapped positions to correctly
calculate inter-particle distances across time. Wrapped coordinates will
produce incorrect results due to discontinuities when particles cross
periodic boundaries.
only_global (bool, optional) – If True, only compute the global Lindemann index (faster, parallel computation).
If False, compute both local and global indices (slower, serial computation).
Default is False.
Memory requirement: This calculation has high memory requirements. You can
estimate the required memory using: \(2 \times 8 \times N_p^2 / 1024^3\) GB.
Parallelization: If only the global Lindemann index is needed, the calculation
can be performed in parallel. The local Lindemann index runs serially due
to dependencies between different frames.
Algorithm: The Welford method
is used to update the variance and mean of \(r_{ij}\) for numerical stability.
Examples
>>> importmdapyasmp>>> importnumpyasnp
>>> # Generate a random walk trajectory with 200 frames and 1000 particles>>> pos_list=np.cumsum(... np.random.choice([-1.0,0.0,1.0],size=(200,1000,3)),axis=0... )
>>> # Calculate only global Lindemann index (faster)>>> LDMG=mp.LindemannParameter(pos_list,only_global=True)>>> LDMG.compute()>>> print(f"Global Lindemann index: {LDMG.lindemann_trj:.6f}")
>>> # Calculate both local and global Lindemann indices>>> LDML=mp.LindemannParameter(pos_list)>>> LDML.compute()>>> print(f"Global Lindemann index: {LDML.lindemann_trj:.6f}")>>> print(f"First 5 frame indices: {LDML.lindemann_frame[:5]}")
This method calls the C++ backend for efficient computation. If only_global
is True, only the global Lindemann index is computed; otherwise, both local
and global indices are calculated.
The computation results are stored in the following attributes:
lindemann_trj : Global Lindemann index
lindemann_frame : Lindemann index per frame (if only_global=False)
lindemann_atom : Local Lindemann index per atom (if only_global=False)
[1] Vania Calandrini, Eric Pellegrini, Paolo Calligari, Konrad Hinsen, Gerald R. Kneller.
“Nmoldyn – interfacing spectroscopic experiments, molecular dynamics simulations
and models for time correlation functions.” École thématique de la Société Française
de la Neutronique, 12:201–232, 2011.
This module implements the Fast Inertial Relaxation Engine (FIRE2) algorithm
for atomic structure optimization. It is a direct translation and adaptation of
the FIRE2 method from ASE (Atomic Simulation Environment).
References
J. Guénolé, W.G. Nöhring, A. Vaid, F. Houllé, Z. Xie, A. Prakash, E. Bitzek,
Assessment and optimization of the fast inertial relaxation engine (FIRE) for energy
minimization in atomistic simulations and its implementation in LAMMPS,
Computational Materials Science 175 (2020) 109584.
https://doi.org/10.1016/j.commatsci.2020.109584
S. Echeverri Restrepo, P. Andric,
ABC-FIRE: Accelerated Bias-Corrected Fast Inertial Relaxation Engine,
Computational Materials Science 218 (2023) 111978.
https://doi.org/10.1016/j.commatsci.2022.111978
When optimize_cell=True, this implementation behaves equivalently to
ase.optimize.FIRE2 combined with ase.constraints.UnitCellFilter,
allowing simultaneous optimization of both atomic positions and cell shape.
Implementation of the FIRE2 (and optional ABC-FIRE) energy minimization algorithm.
This optimizer relaxes atomic structures using a velocity-Verlet–like
integration with adaptive time-stepping, as proposed in the FIRE method.
It can optionally optimize both the atomic positions and simulation cell
(similar to ASE’s UnitCellFilter).
Parameters:
system (mdapy.system.System) – The system to be relaxed. It must provide get_force(), get_energy(),
and get_virials() methods, and an associated Box object.
dt (float, optional) – Initial time step for the integration (default: 0.1).
maxstep (float, optional) – Maximum allowed atomic displacement per step (default: 0.2 Å).
dtmax (float, optional) – Maximum time step allowed during optimization (default: 1.0).
dtmin (float, optional) – Minimum time step (default: 2e-3).
Nmin (int, optional) – Minimum number of consecutive “good” steps (dot(v, f) > 0) before
increasing the time step (default: 20).
finc (float, optional) – Factor by which the time step is increased after successful steps (default: 1.1).
fdec (float, optional) – Factor by which the time step is decreased after bad steps (default: 0.5).
astart (float, optional) – Initial value of the velocity-force mixing coefficient α (default: 0.25).
fa (float, optional) – Multiplicative decay factor for α after successful steps (default: 0.99).
use_abc (bool, optional) – Whether to use the ABC-FIRE algorithm (default: False).
optimize_cell (bool, optional) – Whether to optimize the simulation cell along with atomic positions
(default: False). Equivalent to wrapping ASE’s UnitCellFilter.
mask (array_like of shape (3,3) or (6,), optional) – Tensor mask controlling which components of strain are relaxed.
1 means free, 0 means fixed. Default is fully relaxed.
cell_factor (float, optional) – Scale factor applied to cell degrees of freedom. Equivalent to ASE’s
cell_factor. Default is the number of atoms.
hydrostatic_strain (bool, optional) – If True, only isotropic (hydrostatic) deformation is allowed (default: False).
constant_volume (bool, optional) – If True, constrain relaxation to approximately constant volume
(default: False).
scalar_pressure (float, optional) – External scalar pressure (in eV/ų) added to the enthalpy term (default: 0.0).
Notes
When optimize_cell=True, the deformation gradient is treated as
additional three “virtual atoms”, following the formalism from
Tadmor et al., Phys. Rev. B 59, 235 (1999).
This class modifies both atomic positions and the simulation box
to minimize the potential energy (or enthalpy if pressure is applied).
Compute the combined atomic and (optionally) cell forces.
Returns:
np.ndarray – Force array of shape (N, 3) when optimize_cell=False,
or (N+3, 3) when optimize_cell=True.
The last three rows correspond to the cell forces derived from the virial stress tensor.
This function adaptively updates velocity, time step, and the
mixing parameter α according to the FIRE2 algorithm.
If use_abc=True, it applies the ABC-FIRE bias correction
and per-direction displacement capping.
Construct the neighbor list for all atoms within a cutoff distance rc.
The Neighbor class builds the Verlet neighbor list and corresponding
distance list for each atom in the system.
Parameters:
rc (float) – Cutoff radius for neighbor searching.
box (Box) – Simulation box represented by a mdapy.box.Box instance. Supports
both orthogonal and triclinic boxes.
data (pl.DataFrame) – Atomic data containing at least the columns "x", "y", and "z",
representing atomic coordinates.
max_neigh (int, optional) – Maximum number of neighbors allowed for each atom. If not provided, the
neighbor list will be estimated as a largest safe value, and it will raise some performance overhead, expecially for memory.
NEP calculator for mdapy static calculation framework.
This class provides an interface to compute atomic properties using a
pre-trained NEP (Neuroevolution Potential) machine learning model. It
can calculate energies, forces, stresses, virials, descriptors, and
latent space representations.
Parameters:
filename (str) – Path to the NEP model file (typically ‘nep.txt’ or similar)
Calculate energies, forces, virials, and stress for the system.
This method performs the main NEP calculation and stores results
in the self.results dictionary. It’s automatically called by
other getter methods if results are not already cached.
Parameters:
data (pl.DataFrame) – DataFrame containing atomic positions and elements
np.ndarray – Array of shape (N, 9) with bec components for each atom
Ordered as: [bec_xx, bec_xy, bec_xz, bec_yx, bec_yy, bec_yz, bec_zx, bec_zy, bec_zz]
NEP calculator compatible with ASE (Atomic Simulation Environment).
This class wraps the NEP calculator to work seamlessly with ASE’s
calculator interface, allowing NEP models to be used in ASE workflows
for geometry optimization, molecular dynamics, and other simulations.
Parameters:
model_filename (str) – Path to the NEP model file
atoms (Atoms, optional) – ASE Atoms object to attach the calculator to
Simple, fast, and reliable:
- Uses multiprocessing (true parallelism, no GIL)
- Always uses maximum compression (level 9)
- Focuses on: speed for large files, correctness for all files
- Clean code, proper resource management
Compress a file to .gz format using multiprocessing.
This function provides parallel gzip compression for faster processing
of large files. It automatically uses all available CPU cores and
maintains full gzip format compatibility.
Args:
input_file (str): Path to input file to compress
output_file (str, optional): Path to output file.
If not specified, adds .gz to input filename.
Returns:
str: Path to the created compressed file
Raises:
FileNotFoundError: If input file doesn’t exist
ValueError: If input file already has .gz extension
Exception: If compression fails for any reason
Examples:
>>> # Compress with automatic output name>>> compress_file("data.txt")'data.txt.gz'
>>> # Compress with custom output name>>> compress_file("input.txt","output.gz")'output.gz'
This module provides utilities for creating publication-quality figures with Matplotlib.
It includes functions for configuring plot styles, creating figures with consistent
formatting, and saving figures with uniform margins.
The module is designed to produce figures suitable for scientific papers and presentations,
with sensible defaults for font sizes, line widths, tick marks, and color schemes.
Note: This module requires matplotlib to be installed. Install it with:
Polyhedral Template Matching (PTM) classifier for identifying local atomic structures.
This class implements the Polyhedral Template Matching algorithm to classify the local
structural environment of atoms in a simulation dataset. It matches the neighborhood of
each atom against predefined polyhedral templates for common crystal structures, providing
robustness against thermal fluctuations and elastic strains compared to methods like Common
Neighbor Analysis (CNA).
The supported structure types include:
FCC (Face-Centered Cubic)
HCP (Hexagonal Close-Packed)
BCC (Body-Centered Cubic)
ICO (Icosahedral)
SC (Simple Cubic)
DCUB (Cubic Diamond)
DHEX (Hexagonal Diamond)
Graphene
The algorithm computes a Root-Mean-Square Deviation (RMSD) for the best-matching template
and assigns a structure type if below the specified threshold. Additional outputs include
RMSD values, scale factors, orientations (as quaternions), and neighbor indices.
References
[1] Larsen P M, Schmidt S, Schiøtz J. Robust structural identification via polyhedral template matching[J]. Modelling and Simulation in Materials Science and Engineering, 2016, 24(5): 055007.
Parameters:
structure (str) – String specifying the structure types to consider, separated by hyphens (e.g., “fcc-hcp-bcc”).
Supported values: “fcc”, “hcp”, “bcc”, “ico”, “sc”, “dcub”, “dhex”, “graphene”.
Special values: “all” (all types), “default” (fcc, hcp, bcc).
data (pl.DataFrame) – DataFrame containing atomic positions with columns ‘x’, ‘y’, ‘z’. Optionally includes
‘type’ or ‘element’ for atomic types.
This function is used to sample the configurations using farthest point sampling method, based
on the descriptors. It is helpful to select the structures during active learning process.
Parameters:
n_sample (int) – Number of structures one wants to select.
descriptors (np.ndarray) – Two dimensional ndarray, it can be any descriptors.
start_idx (int) – For deterministic results, fix the first sampled point index.
Defaults to 0.
Calculate the radial distribution function (RDF) for atomic systems.
The radial distribution function g(r) describes the probability of finding
an atom at distance r from a reference atom, normalized by the probability
expected for a completely random distribution at the same density.
For multi-component systems, the total RDF is a weighted sum of partial RDFs:
where V is the system volume, \(N_{\alpha}\) and \(N_{\beta}\) are
the number of atoms of types α and β, and \(r_{ij}\) is the distance
between atoms i and j.
Parameters:
rc (float) – Cutoff distance for RDF calculation (in Angstroms).
nbin (int) – Number of bins for histogram.
box (Box) – System box object containing boundary information.
verlet_list (NDArray[np.int32]) – Shape (N_particles, max_neigh). Verlet neighbor list where
verlet_list[i, j] gives the index of the j-th neighbor of particle i.
Value of -1 indicates no neighbor at that position.
distance_list (NDArray[np.float64]) – Shape (N_particles, max_neigh). Distance list where distance_list[i, j]
gives the distance between particle i and its j-th neighbor.
neighbor_number (NDArray[np.int32]) – Shape (N_particles,). Number of neighbors for each particle.
type_list (NDArray[np.int32]) – Shape (N_particles,). Atom type indices for each particle.
This method calculates both the total RDF and partial RDFs for all
pair combinations of atom types. The results are stored in the
g_total, g, and r attributes.
Plot partial radial distribution functions for all atom type pairs.
Parameters:
elements_list (list of str, optional) – List of element symbols corresponding to each atom type,
e.g., [‘Al’, ‘Ni’]. If None, numeric labels are used.
Length must match the number of atom types (Ntype).
Spatial binning of atomic data in a simulation box.
This class divides a simulation box into spatial bins along specified
directions and computes aggregated statistics for atomic properties
within each bin.
Parameters:
data (pl.DataFrame) – DataFrame containing atomic data with at least ‘x’, ‘y’, ‘z’ columns.
Cubic Spline Interpolation with clamped boundary conditions, where the spline’s endpoint derivatives are set equal to the original derivatives
This class provides a convenient interface for cubic spline interpolation.
It supports evaluation of the spline function and its first derivative at
arbitrary points within the interpolation range.
Parameters:
x (array_like) – 1-D array of x-coordinates of data points. Must be strictly increasing
and contain at least 2 points. Can be list, tuple, or numpy array.
y (array_like) – 1-D array of y-coordinates of data points. Must have the same length as x.
Can be list, tuple, or numpy array.
Computes the interpolated value of the spline function at the specified
x-coordinate(s). Supports both scalar and array inputs.
Parameters:
x (float, int, list, tuple, or np.ndarray) – Point(s) at which to evaluate the spline. Must be within the range
[self.x[0], self.x[-1]]. Can be:
- A single number (int or float)
- A list or tuple of numbers
- A numpy array
Returns:
float or np.ndarray – The interpolated value(s) at x. Returns a float if input is scalar,
otherwise returns a numpy array of the same shape as input.
Raises:
AssertionError – If any value in x is outside the interpolation range [self.x[0], self.x[-1]].
Evaluate the first derivative of the spline at given point(s).
Computes the first derivative (slope) of the interpolated spline function
at the specified x-coordinate(s). Supports both scalar and array inputs.
Parameters:
x (float, int, list, tuple, or np.ndarray) – Point(s) at which to evaluate the derivative. Must be within the range
[self.x[0], self.x[-1]]. Can be:
- A single number (int or float)
- A list or tuple of numbers
- A numpy array
Returns:
float or np.ndarray – The derivative value(s) at x. Returns a float if input is scalar,
otherwise returns a numpy array of the same shape as input.
Raises:
AssertionError – If any value in x is outside the interpolation range [self.x[0], self.x[-1]].
Compute Steinhardt bond orientation order parameters.
The Steinhardt bond orientation order parameters provide a way to characterize
local structural order in molecular dynamics simulations. This class computes
the spherical harmonics-based order parameters \(q_l\) and \(w_l\) for
identifying crystal structures and distinguishing between solid and liquid phases.
Number of solid-like bonds for each particle, only computed if identify_liquid=True.
Type:
np.ndarray, optional
Notes
For a particle \(i\), we calculate the quantity \(q_{lm}\) by summing the spherical harmonics
between particle \(i\) and its neighbors \(j\) in a local region:
If the wl parameter is True, this class computes the quantity
\(w_l\), defined as a weighted average over the
\(q_{lm}(i)\) values using Wigner 3-j symbols (related to Clebsch-Gordan
coefficients).
The resulting combination is rotationally invariant:
\[\begin{split}w_l(i) = \sum \limits_{m_1 + m_2 + m_3 = 0} \begin{pmatrix}
l & l & l \\
m_1 & m_2 & m_3
\end{pmatrix}
q_{lm_1}(i) q_{lm_2}(i) q_{lm_3}(i)\end{split}\]
If wlhat parameter to True will
normalize the \(w_l\) order parameter as follows:
If average is True, the class computes a variant of this order
parameter that performs an average over the first and second shell combined
. To compute this parameter, we perform a second
averaging over the first neighbor shell of the particle to implicitly
include information about the second neighbor shell. This averaging is
performed by replacing the value \(q_{lm}(i)\) in the original
definition by \(\overline{q}_{lm}(i)\), the average value of
\(q_{lm}(k)\) over all the \(N_b\) neighbors \(k\)
of particle \(i\), including particle \(i\) itself:
If use_weight is True, the contributions of each neighbor are weighted.
Neighbor weights \(w_{ij}\) are defined for a
Vornoi face area obtained from Voronoi neighbor or one with user-provided weights, and
default to 1 if not otherwise provided. The formulas are modified as
follows, replacing \(q_{lm}(i)\) with the weighted value
\(q'_{lm}(i)\):
For solid-liquid classification (only performed when \(l=6\) is included),
particles are identified as solid if they have at least \(n_{\text{bond}}\)
“solid-like” bonds. A bond between particles \(i\) and \(j\) is considered
solid-like if their local bond orientations are correlated:
where the default threshold is 0.7 for normalized \(q_6\) values. When solid bond is larger than n_bond, the atom is treated as solid atom.
The default n_bond is 7, where 6-8 is generally good for FCC and BCC strutures.
Compute the Steinhardt bond orientation order parameters.
This method calculates the bond orientation order parameters and stores
the results as instance attributes. If identify_liquid is True, it also
performs solid-liquid classification.
Returns:
None – Results are stored in the following instance attributes:
qlm_r : Real part of spherical harmonics \(q_{lm}\)
qlm_i : Imaginary part of spherical harmonics \(q_{lm}\)
This module implements the calculation of local structural entropy
based on the method proposed by Piaggi and Parrinello
(J. Chem. Phys. 147, 114112 (2017)), which quantifies the degree
of local ordering in atomic configurations.
The local structural entropy measures how similar the local
radial distribution around each atom is to the average distribution
of its surroundings. It is a useful descriptor for identifying
crystalline and disordered regions, phase transitions, or defects.
The implementation supports both global and density-weighted
(normalized) entropy evaluation, and an optional neighbor-based
averaging of the entropy field.
Calculate the local structural entropy for each atom based on
its neighbor environment within a specified cutoff radius.
The local structural entropy quantifies the degree of
local disorder by comparing the local pair distribution
to a reference distribution. Lower entropy values correspond
to more ordered (crystalline) environments, while higher values
indicate disordered or amorphous regions.
Parameters:
box (Box) – Simulation box object defining the system boundaries.
verlet_list (np.ndarray) – Neighbor list of atomic indices. Each row corresponds to
one atom and contains indices of its neighbors.
distance_list (np.ndarray) – Distance list corresponding to the verlet_list. Each
element stores the pairwise distances between atoms.
neighbor_number (np.ndarray) – Number of valid neighbors for each atom.
rc (float) – Cutoff radius (in Å) used for calculating the local
pair distribution function.
sigma (float) – Gaussian smoothing width used in constructing the local
radial distribution function.
use_local_density (bool) – If True, the local RDF is normalized by the local atomic
density. If False, a global normalization is used.
average_rc (float, optional) – Cutoff radius for averaging entropy values over neighboring
atoms. If zero or not specified, no averaging is performed.
This method computes the entropy value for each atom based on
its local pair distribution function within rc. Optionally,
if average_rc > 0, the resulting entropy field is averaged
over neighbors within the given radius.
Returns:
None – Results are stored in the instance attributes:
entropy and, if applicable, entropy_ave.
Compute the static structure factor S(k) of an atomic system.
The static structure factor characterizes the spatial distribution of particles
in reciprocal space and is related to the Fourier transform of the pair
correlation function.
Two computational methods are available:
Direct method: Computes S(k) by directly evaluating the structure factor
in reciprocal space using discrete k-space bins:
Debye method: Uses the Debye scattering equation, which computes S(k)
for specific k values. Better for large systems or when specific k values
are needed.
where N is the number of particles, k is the wavenumber magnitude, and r_ij
is the distance between particles i and j.
Parameters:
data (pl.DataFrame) – Polars DataFrame containing atomic positions. Must have ‘x’, ‘y’, ‘z’ columns.
For partial structure factors, must also contain ‘element’ or ‘type’ column.
k_min (float) – Minimum wavenumber for S(k) calculation (must be >= 0).
k_max (float) – Maximum wavenumber for S(k) calculation (must be > k_min).
nbins (int) – Number of bins for discretizing the k-space (must be > 0).
- For ‘direct’ mode: creates nbins+1 bin edges, returns nbins values
- For ‘debye’ mode: creates nbins linearly spaced k values
cal_partial (bool, default=False) – If True, compute partial structure factors S_αβ(k) for each pair of
atom types α and β. Requires ‘element’ or ‘type’ column in data.
atomic_form_factors (bool, default=False) – If True, use atomic form factors f to weigh the atoms’ individual contributions to S(k). Atomic form factors are taken from TU Graz.
mode ({'direct', 'debye'}, default='direct') – Computational method:
- ‘direct’: Direct reciprocal space summation
- ‘debye’: Debye scattering equation
Dictionary of partial structure factors weighted by atomic_form (only if cal_partial=True and atomic_form_factors=True).
This is helpful to directly compare with experimental results.
Keys are formatted as ‘element1-element2’.
This method performs the actual calculation of S(k) using the specified
computational mode. Results are stored in the Sk attribute, and if
cal_partial=True, partial structure factors are stored in Sk_partial.
Raises:
AssertionError – If required columns (‘x’, ‘y’, ‘z’) are missing from data, or if
‘element’ or ‘type’ column is missing when cal_partial=True.
Notes
For partial structure factors in a binary system with species A and B,
three partial S(k) are computed: S_AA(k), S_AB(k), and S_BB(k).
The total structure factor is:
Plot partial structure factors S_αβ(k) for multi-component systems.
Creates a line plot showing all partial structure factors computed for
different atom type pairs. Each partial S(k) is plotted with a different
color and labeled accordingly.
Parameters:
fig (matplotlib.figure.Figure, optional) – Matplotlib figure object. If None, a new figure is created.
ax (matplotlib.axes.Axes, optional) – Matplotlib axes object. If None, new axes are created.
Returns:
fig (matplotlib.figure.Figure) – The matplotlib figure object containing the plot.
ax (matplotlib.axes.Axes) – The matplotlib axes object containing the plot.
Raises:
AttributeError – If compute() was not called with cal_partial=True.
This module provides the central System class that serves as the main interface
for atomic simulation data and analysis in mdapy. The System class integrates
all analysis modules and provides a unified API for structural analysis,
property calculations, and data manipulation.
Core class representing an atomic system with integrated analysis capabilities.
The System class is the central data structure in mdapy, providing a unified
interface for loading, manipulating, and analyzing atomic configurations. It
integrates various structural analysis methods, property calculators, and
visualization tools.
Parameters:
filename (str, optional) – Path to an atomic configuration file. Supports various formats including
POSCAR, LAMMPS data/dump, XYZ, mp etc. Format is auto-detected or can
be specified via the format parameter.
data (pl.DataFrame, optional) – Polars DataFrame containing atomic information. Must include columns
‘x’, ‘y’, ‘z’ for positions. Other common columns include ‘type’,
‘element’, ‘vx’, ‘vy’, ‘vz’, ‘fx’, ‘fy’, ‘fz’, etc.
pos (np.ndarray, optional) – Array of shape (N, 3) containing atomic positions. Used in conjunction
with box parameter.
box (int, float, Iterable[float], np.ndarray, or Box, optional) – Simulation box specification. Required when using data or pos parameters.
See Box for supported formats.
ase_atom (ase.Atoms, optional) – ASE Atoms object to initialize the system from.
ovito_atom (ovito.data.DataCollection, optional) – OVITO DataCollection object to initialize the system from.
format (str, optional) – File format specification when loading from filename.
If None, format is auto-detected.
global_info (dict, optional) – Dictionary containing global system properties such as total energy,
virial, stress tensor, timestep, etc. Defaults to empty dict.
Cutoff radius used for neighbor list construction.
Type:
float, optional
Notes
The System class uses lazy evaluation for many analyses - neighbor lists
and other expensive computations are only performed when needed. Results
are cached and reused when possible.
At initialization, exactly one of the following must be provided:
- filename
- data and box
- pos and box
- ase_atom
- ovito_atom
Examples
Creating a system from different sources:
frommdapy.systemimportSystemimportnumpyasnp# Load from file (auto-detect format)system=System("config.dump")# Load from file with explicit formatsystem=System("POSCAR",format="poscar")# Create from numpy arraypos=np.random.rand(100,3)*10system=System(pos=pos,box=10)# Create from polars DataFrameimportpolarsaspldata=pl.DataFrame({"x":np.random.rand(100),"y":np.random.rand(100),"z":np.random.rand(100),"type":np.ones(100,dtype=int),})system=System(data=data,box=[10,10,10])
# Get positionspos=system.data.select("x","y","z")# Add a new columnnew_data=system.data.with_columns(energy=np.random.rand(system.N))system.update_data(new_data)# Save to filesystem.write("output.dump")
dict – Dictionary containing global properties such as total energy,
virial tensor, stress tensor, timestep, temperature, etc.
Keys and values depend on the data source and attached calculator.
pl.DataFrame – Polars DataFrame containing per-atom properties. Always includes
columns ‘x’, ‘y’, ‘z’ for Cartesian coordinates. May include
additional columns such as ‘type’, ‘element’, ‘vx’, ‘vy’, ‘vz’
(velocities), ‘fx’, ‘fy’, ‘fz’ (forces), and various computed
properties from analysis methods.
element_list (list of str, tuple of str, or np.ndarray) – Ordered list of unique elements. Atoms are assigned type numbers
based on the index of their element in this list (starting from 1).
Raises:
AssertionError – If data does not contain ‘element’ column or if any element in
data is not present in the provided element list.
Examples
>>> system.set_element(["Cu","Cu","Al","Al"])>>> system.set_type_by_element(["Cu","Al"])>>> print(system.data.select("element","type"))┌─────────┬──────┐│ element │ type │├─────────┼──────┤│ Cu │ 1 ││ Cu │ 1 ││ Al │ 2 ││ Al │ 2 │└─────────┴──────┘
Notes
This method creates or updates the ‘type’ column in the data DataFrame.
Type numbers start from 1 and correspond to the order in the element list.
Set primary knock-on atom (PKA) for radiation damage simulation.
This method assigns initial kinetic energy and direction to a PKA atom,
commonly used to initialize cascade simulations in radiation damage studies.
We assume that the velocity in the units of A/fs, you need to use factor to convert your velocity to this units.
Parameters:
energy (float) – Kinetic energy to assign to the PKA (in eV).
direction (np.ndarray) – 1D array of length 3 specifying the velocity direction vector.
Will be normalized automatically.
index (int, optional) – Index of the atom to set as PKA. If None, selects by nearest center atom.
element (str, optional) – Element name to select PKA. If provided and index is None,
selects by nearest center atom with same element.
factor (float) – Convert your velocity to the units of A/fs.
Defaults to 1.0.
Notes
This method updates the velocity columns (‘vx’, ‘vy’, ‘vz’) in the
data DataFrame.
data (pl.DataFrame) – New DataFrame containing updated atomic information.
Should maintain the same structure (columns) as the original data.
reset_calcolator (bool, optional) – If True, clears cached results from the attached calculator.
Set to True when atomic positions or types/element change. Default is False.
reset_neighbor (bool, optional) – If True, clear all neighbor information.
Set to True when atomic positions change. Default is False.
Examples
>>> # Add a new property column>>> new_data=system.data.with_columns(... temperature=np.random.rand(system.N)*300... )>>> system.update_data(new_data)
box (int, float, Iterable[float], np.ndarray, or Box) – New box specification. See Box for accepted formats.
scale_pos (bool, optional) – If True, scales atomic positions affinely with the box change.
Useful for applying strain or changing lattice constants while
maintaining fractional coordinates. Default is False.
Raises:
AssertionError – If scale_pos=True but not all boundaries are periodic.
Examples
Change box without scaling positions:
>>> system.update_box([20,20,20])
Apply uniform strain by scaling box and positions:
>>> system.update_box([11,11,11],scale_pos=True)
Notes
When scale_pos=True, the transformation is:
new_pos = old_pos @ (old_box^-1 @ new_box)
This preserves fractional coordinates in the new box.
This method constructs a Verlet neighbor list within a cutoff radius,
which is required for many analysis methods. For small systems, the
box may be automatically replicated to ensure sufficient neighbors.
Parameters:
rc (float) – Cutoff radius for neighbor search (in simulation units).
max_neigh (int, optional) – Maximum number of neighbors per atom. If None, automatically
determined based on the actual neighbor number Increase this if you get warnings
about insufficient neighbor slots.
Calculate Voronoi neighbors and face properties for all atoms.
This method performs Voronoi tessellation to identify nearest neighbors
based on shared Voronoi cell faces. It can filter neighbors based on
face area thresholds to exclude insignificant contacts.
Parameters:
a_face_area_threshold (float, optional) – Absolute face area threshold.
Faces with area below this value are ignored as neighbors.
Default is -1.0 (no filtering).
r_face_area_threshold (float, optional) – Relative face area threshold (fraction of average face area).
Faces with relative area below this value are ignored.
Default is -1.0 (no filtering).
Calculate the local atomic temperature for each atom by analyzing the velocity fluctuations of the atom and its neighbors.
We assume velocity in units of Å/fs, you can use factor to convert your velocity.
This will save results to self.data[‘atomic_temp’] in the unit of K.
Parameters:
rc (float) – Cutoff radius for local averaging.
factor (float) – Scaling factor for velocities (e.g., for unit conversion).
Default is 1.0.
max_neigh (int, optional) – Maximum number of neighbors per atom.
Calculate Steinhardt bond orientation order parameters. One can also use it to identify the solid or liquid atom.
Parameters:
llist (np.ndarry or List[int]) – Degrees list, such as [4, 6, 8], should be positive, even integer.
use_voronoi (bool) – If True, compute using Voronoi neighbor. Default is False.
nnn (int) – If use_voronoi is False and this value is postive, compute using nnn nearest neighbors. Default is 0.
rc (float) – If use_voronoi is False and nnn is zero, make sure this parameter is postive, and use it to build neighbor. Default is -1.0.
average (bool) – If True, qlm will be averaged. Default is False.
use_weight (bool) – If True, the neighbor will be weighed by weight array, otherwise the weight of each neighbor is 1.0. Default is False.
weight (np.ndarray, optional) – If it is not None, it should be has the same shape with the self.verlet_list.
If it is None and the use_voronoi is True, then the voronoi_face_area will be used as weight. Default is None.
wl (bool) – If True, compute third-order invariant \(w_l\) parameters. Default is False.
wlhat (bool) – If True, compute normalized third-order invariant \(\hat{w}_l\) parameters. Default is False.
a_face_area_threshold (float) – Absolute face area threshold.
Faces with area below this value are ignored as neighbors.
Default is -1.0 (no filtering).
r_face_area_threshold (float) – Relative face area threshold (fraction of average face area).
Faces with relative area below this value are ignored.
Default is -1.0 (no filtering).
identify_liquid (bool) – Enable solid-liquid classification (requires \(l=6\) in llist).
threshold (float) – Threshold for solid-liquid identification (default: 0.7 for normalized \(q_6\)).
n_bond (int) – Minimum number of “solid-like” bonds for solid classification. Default to 7.
max_neigh (int, optional) – Maximum number of neighbors per atom.
Notes
See SteinhardtBondOrientation
for implementation details. The results will add to self.data[‘ql{l}’] for l in llist.
If set wl is True, also will add self.data[‘wl{l}’]. If set wlhat is True, also will add
self.data[‘wlh{l}’]. If set identify_liquid, it will add self.data[‘solidliquid’] and self.data[‘nbond’] results.
rc_dict (dict of str to list of float) – Dictionary mapping triplet patterns to cutoff radii.
Format: {‘A-B-C’: [rc_AB_min, rc_AB_max, rc_AC_min, rc_AC_max]}
where A, B, C are element symbols and A is the central atom.
Example: {‘O-H-H’: [0, 2.0, 0, 2.0]} for water molecules.
nbin (int) – Number of angular bins.
max_neigh (int, optional) – Maximum number of neighbors per atom.
Calculate Voronoi cell volumes and related properties.
This method computes the Voronoi tessellation and adds three columns
to the data DataFrame:
volume: Atomic volume from Voronoi cell
neighbor_number: Coordination number from Voronoi neighbors
cavity_radius: This is the distance from the atom to the farthest vertex of its Voronoi cell, representing the radius of the largest empty sphere (containing no other atoms) that touches the atom.
Compute the averaged property of each atom based on its neighbors within a given cutoff radius.
This function calculates the local average of a specified property (e.g., temperature, stress)
for each atom by averaging over its neighboring atoms within the cutoff average_rc.
The neighbor information is provided through the Verlet list and corresponding distances.
Parameters:
average_rc (float) – The cutoff radius used for averaging the property.
data (pl.DataFrame) – The input atomic data containing the property to be averaged.
property_name (str) – The name of the column in data to average.
verlet_list (np.ndarray) – A 2D array of neighbor indices. Each row corresponds to one atom and lists its neighbors.
distance_list (np.ndarray) – A 2D array of distances corresponding to the verlet_list entries.
neighbor_number (np.ndarray) – A 1D array indicating the actual number of neighbors for each atom.
include_self (bool, optional, default=True) – Whether to include the atom itself when computing the average.
output_name (str, optional) – The name of the output column to store the averaged property.
If not provided, defaults to f"{property_name}_ave".
Returns:
pl.DataFrame – A new DataFrame containing the original data plus an additional column
with the averaged property values.
Replicates atomic data along the x, y, and z directions.
This function creates a supercell by replicating the input atomic positions
and associated data nx times along x, ny times along y, and nz times
along z. The input DataFrame is expected to
contain at least ‘x’, ‘y’, and ‘z’ columns for atomic coordinates.
Args:
data: Atomic information, including positions in ‘x’, ‘y’, ‘z’ columns.
box: Simulation box information to be scaled.
nx: Number of replications along the x direction.
ny: Number of replications along the y direction.
nz: Number of replications along the z direction.
Returns:
A tuple containing the replicated DataFrame (with updated positions and
optionally renumbered ‘id’ column) and the scaled Box object.
Split a multi-frame XYZ file into individual frame files.
Parameters:
input_file (str) –
Path to the input XYZ file containing multiple frames.
The file should follow standard XYZ format:
Line 1: Number of atoms (integer)
Line 2: Comment line
Lines 3 to N+2: Atomic coordinates (element x y z …)
Repeat for each frame
output_dir (str, optional) – Directory where individual frame files will be saved.
The directory will be created if it doesn’t exist.
Default is “res”.
output_prefix (str, optional) – Prefix for output filenames. If None, uses the input filename
(without extension) as prefix.
Output files are named as: {output_prefix}.{frame:0Nd}.xyz
where N is the number of digits needed (e.g., 5 digits for 10000 frames).
Default is None.
in_memory (bool, optional) – If input file is too big to load into memory, set this parameter to False.
Default is True.
Returns:
None
Examples
>>> # Split trajectory.xyz into res/trajectory.00000.xyz, res/trajectory.00001.xyz, ...>>> split_xyz("trajectory.xyz")
>>> # For a file with 100000 frames, output will use 6 digits>>> split_xyz("large_traj.xyz")>>> # Output: res/large_traj.000000.xyz, ..., res/large_traj.099999.xyz
Raises:
RuntimeError – If the input file cannot be opened or contains no valid frames.
OSError – If the output directory cannot be created.
This class provides functionality to read, manipulate, and write XYZ format
trajectory files. It supports both classical XYZ format (element + coordinates)
and extended XYZ format (with periodic boundary conditions and additional properties).
Two reading modes are available:
- Serial mode: Read frames sequentially (default)
- Fast mode: Optimized batch reading assuming all frames have identical columns
Parameters:
filename (Optional[str]) – Path to XYZ trajectory file to load
systems (Optional[List[System]]) – List of System objects to initialize trajectory from memory
fast_mode (bool, default=False) – If True, use optimized reading assuming all frames have the same columns.
This mode is significantly faster but requires all frames to have identical
column structure.
Raises:
ValueError – If neither filename nor systems is provided
Examples
>>> # Load trajectory in serial mode>>> traj=XYZTrajectory("trajectory.xyz")
>>> # Load trajectory in fast mode>>> traj=XYZTrajectory("trajectory.xyz",fast_mode=True)
>>> # Create trajectory from existing System objects>>> traj=XYZTrajectory(systems=[system1,system2])
>>> # Access frames>>> print(len(traj))# Number of frames>>> frame=traj[0]# Get first frame>>> sub_traj=traj[0:10]# Slice trajectory
>>> # Iterate over frames>>> forframeintraj:... print(frame.N)
>>> # Save trajectory>>> traj.save("output.xyz")>>> traj.save("output.xyz",frames=[0,1,2])# Save specific frames
frames (Optional[Union[List[int], int]], default=None) – Frame indices to save. Can be:
- None: save all frames
- int: save single frame
- List[int]: save specified frames
>>> traj.save("output.xyz")# Save all frames>>> traj.save("output.xyz",0)# Save first frame only>>> traj.save("output.xyz",[0,5,10])# Save specific frames
system (System) – MDAPY System object containing atomic positions, box information
and optional per-atom properties such as element, type, radius, etc.
The type is mapped cyclically into nine predefined colors.
Colors are updated in system.data[“color”] and applied
to the k3d point object if already initialized.
This module provides efficient Voronoi tessellation analysis for atomic systems,
enabling calculation of Voronoi cells, neighbor identification, volumes, and
detailed geometric information. The implementation uses OpenMP parallelization
through the Voro++ library for high performance.
The module supports both orthogonal and triclinic simulation boxes with
periodic boundary conditions.
This class provides methods to compute Voronoi cells and their properties
for a system of atoms/particles. It supports both orthogonal and triclinic
simulation boxes with periodic boundary conditions.
Parameters:
box (Box) – The simulation box object containing box vectors, boundary conditions,
and origin information.
data (pl.DataFrame) – A Polars DataFrame containing atomic positions with columns ‘x’, ‘y’, ‘z’.
The implementation automatically handles small systems by replicating atoms
to ensure sufficient neighbors for accurate Voronoi tessellation. For
triclinic boxes, automatic rotation to LAMMPS-compatible format is performed
when necessary.
Calculate Voronoi neighbors and face properties for all atoms.
This method performs Voronoi tessellation to identify nearest neighbors
based on shared Voronoi cell faces. It can filter neighbors based on
face area thresholds to exclude insignificant contacts.
Parameters:
a_face_area_threshold (float, optional) – Absolute face area threshold.
Faces with area below this value are ignored as neighbors.
Default is -1.0 (no filtering).
r_face_area_threshold (float, optional) – Relative face area threshold (fraction of average face area).
Faces with relative area below this value are ignored.
Default is -1.0 (no filtering).
Returns:
verlet_list (np.ndarray) – 2D array of shape (N, max_neighbors) containing neighbor indices
for each atom. Unused entries are filled with -1.
distance_list (np.ndarray) – 2D array of shape (N, max_neighbors) containing distances to
each neighbor (in simulation units).
face_area (np.ndarray) – 2D array of shape (N, max_neighbors) containing the area of
the Voronoi face shared with each neighbor.
neighbor_number (np.ndarray) – 1D array of shape (N,) containing the coordination number
(number of Voronoi neighbors) for each atom.
Notes
If both thresholds are provided, the larger effective threshold is used.
The method automatically handles periodic boundary conditions.
For small systems (<50 atoms), automatic replication is performed
to ensure accurate neighbor identification.
OpenMP parallelization is used for performance with large systems.
Retrieve detailed Voronoi polygon information for all atoms.
This method provides comprehensive geometric information about each
Voronoi cell, including face vertices, positions, and areas. Due to
the detailed nature of the output, this method is computationally
intensive and memory-demanding, making it suitable primarily for
small systems.
Returns:
face_vertices_indices (List[List[List[int]]]) – For each atom, a list of faces, where each face is defined by
a list of vertex indices that form the face polygon.
face_vertices_positions (List[List[List[float]]]) – For each atom, a list of faces, where each face contains the
3D positions (x, y, z) of all vertices forming that face.
volume (List[float]) – The volume of each atom’s Voronoi cell (in cubic simulation units).
radius (List[float]) – The cavity radius for each atom - the distance from the atom to
the farthest vertex of its Voronoi cell (in simulation units).
face_areas (List[List[float]]) – For each atom, a list containing the area of each face of its
Voronoi cell (in square simulation units).
Raises:
AssertionError – If the box is triclinic (only orthogonal boxes are supported).
AssertionError – If the system contains less than 2 atoms.
Warning
This method is computationally expensive and requires significant memory,
especially for large systems. It should primarily be used for small systems.
For routine analysis of large systems, use get_neighbor() or
get_volume() instead.
Calculate Voronoi cell volumes, coordination numbers, and cavity radii.
This method efficiently computes key Voronoi cell properties without
storing detailed geometric information, making it suitable for large-scale
analysis of atomic systems.
Returns:
volume (np.ndarray) – 1D array of shape (N,) containing the volume of each atom’s
Voronoi cell (in cubic simulation units of length).
neighbor_number (np.ndarray) – 1D array of shape (N,) containing the coordination number for
each atom. This is the number of faces of the Voronoi cell,
which equals the number of nearest neighbors.
cavity_radius (np.ndarray) – 1D array of shape (N,) containing the cavity radius for each atom.
This is the distance from the atom to the farthest vertex of its
Voronoi cell, representing the radius of the largest empty sphere
(containing no other atoms) that touches the atom.
A lightweight container representing a single Voronoi cell and all its
geometric properties.
Parameters:
face_vertices (List[List[int]]) – A list of faces, where each face is represented by a list of vertex
indices referring to positions in the vertices array.
vertices (np.ndarray) – Array of shape (M, 3) containing the 3D coordinates of all polygon
vertices that form the Voronoi cell.
volume (float) – The volume of the Voronoi cell.
cavity_radius (float) – The distance from the particle to the farthest vertex of its Voronoi
cell, representing the largest empty-sphere radius.
face_areas (np.ndarray) – Array containing the area of each face of the Voronoi cell.
pos (np.ndarray) – The (x, y, z) coordinates of the particle associated with this Voronoi
cell.
Notes
This class stores only geometric results and contains no computational
methods. Instances of this class are typically created internally by the
Container class.
Calculate the Warren-Cowley Parameter (WCP) for chemical short-range order analysis.
The Warren-Cowley Parameter quantifies chemical ordering in multi-component systems
by measuring the deviation of local atomic arrangements from random mixing. It is
particularly useful for analyzing segregation, clustering, and ordering tendencies
in alloys.
Parameters:
verlet_list (np.ndarray) – Neighbor list array of shape (N, max_neigh).
neighbor_number (np.ndarray) – Number of neighbors for each atom, shape (N,).
data (pl.DataFrame) – Atomic data containing either ‘element’ or ‘type’ column.
fig (matplotlib.figure.Figure) – Figure object containing the plot.
ax (matplotlib.axes.Axes) – Axes object containing the plot.
Notes
The plot displays WCP values as a colored matrix with numerical annotations.
Rows represent central atom types, columns represent neighboring atom types.
This class performs point defect analysis using the Wigner-Seitz cell method,
similar to the implementation in OVITO. It identifies vacancies and interstitials
by comparing a reference configuration (ideal lattice sites) with a current
configuration (displaced atoms). Each atom in the current configuration is
assigned to the nearest reference site. Site occupancies are then computed to
detect defects: sites with occupancy 0 are vacancies, and sites with occupancy
>1 indicate interstitials.
Parameters:
ref (System) – Reference system defining the ideal lattice sites.
affine (bool, optional) – If True, applies an affine transformation to map current positions to the
reference cell before assignment (handles cell deformation). Default is False.