Utility functions:
This is an overview of various miscellaneous utility functions, useful for different purposes when doing calculations with vitrum. These used to live together in a single vitrum.utility module, which has since been split into focused modules.
Geometry
Low-level distance/PDF helpers used internally by GlassAtoms — useful directly when you have raw distances rather than a full Atoms object.
from vitrum.geometry import pdf, find_min_after_peak
xval, gr = pdf(dist_list, volume, rrange=10, nbin=100)
cutoff = xval[find_min_after_peak(gr)] # first minimum after the first peak
find_min_after_peak(padf)
Find the index of the first local minimum after the first peak in a function. Useful for determining cutoffs from PDFs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
padf
|
ndarray
|
The probability density function or similar array. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
The index of the minimum. |
Source code in src/vitrum/geometry.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
get_dist(list, cell)
Calculate the pairwise distance matrix for atoms in a periodic simulation box.
Kept as a plain-numpy (non-numba) reference implementation for backward compatibility; get_dist_numba is the version used internally by GlassAtoms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list
|
ndarray
|
Atomic positions (N x 3). |
required |
cell
|
ndarray
|
Cell dimensions (3,). Assumes an orthorhombic cell. |
required |
Returns:
| Type | Description |
|---|---|
|
np.ndarray: Symmetric distance matrix (N x N) containing distances between all atom pairs. |
Source code in src/vitrum/geometry.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
get_dist_numba(pos, cell)
Calculate the distance matrix between atoms using Numba for performance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pos
|
ndarray
|
Array of atomic positions (N x 3). |
required |
cell
|
ndarray
|
Cell dimensions (3,). Assumes an orthorhombic cell (lx, ly, lz). |
required |
Returns:
| Type | Description |
|---|---|
|
np.ndarray: Symmetric distance matrix (N x N). |
Source code in src/vitrum/geometry.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
pdf(dist_list, volume, rrange=10, nbin=100)
Calculate the pair distribution function (PDF) of a list of distances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dist_list
|
ndarray
|
A 2D numpy array of distances. |
required |
volume
|
float
|
The volume of the system. |
required |
rrange
|
float
|
The range of the PDF. Defaults to 10. |
10
|
nbin
|
int
|
The number of bins. Defaults to 100. |
100
|
Returns:
| Name | Type | Description |
|---|---|---|
xval |
ndarray
|
The x values of the PDF. |
pdf |
ndarray
|
The PDF values. |
Source code in src/vitrum/geometry.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
Trajectory
See Quick start for a worked unwrap_trajectory example.
get_high_low_displacement_index(initial_state, current_state, target_atom, percentage=0.25)
Calculates the indices of the atoms with the high and low displacements between an initial and current state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial_state
|
Atoms
|
The initial state of the system. |
required |
current_state
|
Atoms
|
The current state of the system. |
required |
target_atom
|
str or int
|
The chemical symbol or atomic number of the target atom. |
required |
percentage
|
float
|
The percentage of the highest and lowest displacements to consider. Defaults to 0.25. |
0.25
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
A list of two elements, where the first element is the index of the atoms with the highest displacements and the second element is the index of the atoms with the lowest displacements. |
Source code in src/vitrum/trajectory.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
unwrap_trajectory(atoms_list)
Unwraps a list of Atoms objects to remove periodic boundary crossings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atoms_list
|
list of Atoms objects
|
The list of Atoms objects to unwrap. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
unwrapped_atoms_list |
list of Atoms objects
|
The unwrapped list of Atoms objects. |
Source code in src/vitrum/trajectory.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
Packing
See Quick start for a worked get_random_packed example.
apply_strain_to_structure(structure, deformations)
Apply strain(s) to input structure and return transformation(s) as list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
.Structure
|
Input structure to apply strain to |
required |
deformations
|
list[.Deformation]
|
A list of deformations to apply independently to the input structure, in anticipation of performing an EOS fit. Deformations should be of the form of a 3x3 matrix, e.g., [[1.2, 0., 0.], [0., 1.2, 0.], [0., 0., 1.2]] or ((1.2, 0., 0.), (0., 1.2, 0.), (0., 0., 1.2)) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
A list of .TransformedStructure objects corresponding to the list of input deformations. |
Source code in src/vitrum/packing.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
get_random_packed(composition, target_atoms=100, min_distance=None, radii_scaling=1.0, volume_scaling=1.0, vol_per_atom_source='mp', datatype='ase', db_kwargs=None, density=None, seed=None, side_ratios=[1, 1, 1], **kwargs)
Generate a random packed structure based on the given composition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
composition
|
(str, dict or Composition)
|
The composition of the structure. |
required |
density
|
float
|
The target density of the structure (in g/cm^3). If not provided, the volume per atom is estimated using the Materials Project API. |
None
|
target_atoms
|
int
|
The target number of atoms in the structure. Defaults to 100. |
100
|
radii_scaling
|
float
|
Scaling factor for the covalent radii of the atoms. Defaults to 1.0. |
1.0
|
volume_scaling
|
float
|
Scaling factor for the volume of the structure. Defaults to 1.0. |
1.0
|
vol_per_atom_source
|
float or str
|
The source for the volume per atom. Can be a float value or one of the following strings: "mp" (Materials Project), "icsd" (Inorganic Crystal Structure Database), "density" (use provided density), "covalent_radius" (estimate from covalent radii), or "convex_hull" (estimate from convex hull). Defaults to "mp". |
'mp'
|
datatype
|
str
|
The type of data to return. Can be "ase" for ASE format or "pymatgen" for pymatgen format. Defaults to "ase". |
'ase'
|
db_kwargs
|
dict
|
Additional keyword arguments for database access. Defaults to None. |
None
|
seed
|
int
|
The seed for random number generation. Defaults to 0. |
None
|
side_ratios
|
list
|
The side ratios for the lattice. Defaults to [1, 1, 1]. |
[1, 1, 1]
|
Returns:
| Name | Type | Description |
|---|---|---|
data |
Atoms or Structure
|
The generated random packed structure. |
Source code in src/vitrum/packing.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
Volume estimation
Used internally by packing.get_random_packed to estimate the cell volume for a target composition; can also be called directly.
from vitrum.volume_estimation import get_volume
# estimate volume purely from covalent radii, no external API required
volume = get_volume("SiO2", {"Si": 1, "O": 2}, vol_per_atom_source="covalent_radius")
get_average_volume_convex_hull(composition, MP_API_KEY=None)
Get the average volume per atom from the convex hull on Materials Project.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
composition
|
Composition
|
The composition to query. |
required |
MP_API_KEY
|
str
|
Materials Project API Key. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
Average volume per atom. |
Source code in src/vitrum/volume_estimation.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
get_volume(composition, structure, vol_per_atom_source='mp', db_kwargs=None, density=None, MP_API_KEY=None)
Get the volume of the cell based on the composition and various estimation methods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
composition
|
Union[Composition, str]
|
The composition of the material. |
required |
structure
|
dict
|
A dictionary mapping element symbols to their count in the structure. |
required |
vol_per_atom_source
|
Union[float, str]
|
Method to estimate volume per atom. Options: - "mp": Use Materials Project (requires API key). - "icsd": Use ICSD database (if available). - "density": Calculate from provided density. - "covalent_radius": Estimate from covalent radii. - "convex_hull": Estimate from convex hull on Materials Project. - float: Directly provide the volume per atom. Defaults to "mp". |
'mp'
|
db_kwargs
|
dict
|
Keyword arguments for database queries. Defaults to None. |
None
|
density
|
float
|
Density in g/cm^3. Required if vol_per_atom_source="density". Defaults to None. |
None
|
MP_API_KEY
|
str
|
Materials Project API key. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
The calculated total volume of the cell in Angstrom^3. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If estimating from density but estimates fail, or if an unknown source is provided. |
Source code in src/vitrum/volume_estimation.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
I/O helpers
See Quick start for worked correct_atom_types/get_LAMMPS_dump_timesteps examples.
correct_atom_types(atoms_list, atom_to_type_map)
Correct the atom types in a list of Atoms objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atoms_list
|
list of Atoms objects
|
The list of Atoms objects to correct. |
required |
atom_to_type_map
|
dict
|
A dictionary mapping atomic numbers to atom types. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
atoms_list |
list of Atoms objects
|
The corrected list of Atoms objects. |
Source code in src/vitrum/io_helpers.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
get_LAMMPS_dump_timesteps(filename)
Retrieves the timesteps from a LAMMPS dump file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The path to the LAMMPS dump file. |
required |
Returns:
| Type | Description |
|---|---|
|
List[int]: A list of timesteps extracted from the file. |
Source code in src/vitrum/io_helpers.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
Structure validation
Sanity checks for generated or simulated structures.
from vitrum.structure_validation import homogeneity_checker, dimer_checker
is_homogeneous = homogeneity_checker(atoms, grid_density=(3, 3, 3))
has_too_many_dimers = dimer_checker(atoms, bond_length=2.0, num_allowed=2)
dimer_checker(atoms, bond_length=2.0, num_allowed=2)
Check for the presence of dimers (e.g., O2, N2, F2, Cl2, Br2, I2) in the structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atoms
|
Atoms
|
The ASE Atoms object to check. |
required |
bond_length
|
float
|
The cutoff distance to define a bond. Defaults to 2.0. |
2.0
|
num_allowed
|
int
|
The maximum number of allowed dimers before returning True. Defaults to 2. |
2
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
True if the number of dimers exceeds |
Source code in src/vitrum/structure_validation.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
homogeneity_checker(atoms, grid_density, slide_steps=2, target_species='all', upper_bound=1.5, lower_bound=0.5, box_threshold=0.1, separated_species_threshold=0.5)
Check the homogeneity of the atomic structure by analyzing atom density in grid boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atoms
|
Atoms
|
The ASE Atoms object to analyze. |
required |
grid_density
|
tuple or list
|
Number of grid divisions in x, y, z directions (nx, ny, nz). |
required |
slide_steps
|
int
|
Number of steps to slide the grid for averaging. Defaults to 2. |
2
|
target_species
|
str or list
|
Species to check. "all" checks all present species. Defaults to "all". |
'all'
|
upper_bound
|
float
|
Multiplier for average density to consider a box over-dense. Defaults to 1.5. |
1.5
|
lower_bound
|
float
|
Multiplier for average density to consider a box under-dense. Defaults to 0.5. |
0.5
|
box_threshold
|
float
|
Fraction of boxes allowed to be out of bounds before flagging a species. Defaults to 0.1. |
0.1
|
separated_species_threshold
|
float
|
Fraction of species allowed to be phase separated before the structure is flagged. Defaults to 0.5. |
0.5
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
True if the structure is considered homogeneous, False otherwise. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no species are found in the atoms object. |
Source code in src/vitrum/structure_validation.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
Comparison
from vitrum.comparison import r_chi
simulated = {"x": sc.xval, "y": rdf}
experimental = {"x": exp_r, "y": exp_gr}
rchi, common_x, y_sim, y_exp = r_chi(simulated, experimental)
r_chi(function_1, function_2, x_min=0, x_max=np.inf, steps=100)
Calculate the Wright coefficient (https://doi.org/10.1016/0022-3093(93)90232-M) between two functions
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function_1
|
dict
|
Dictionary with keys 'x' and 'y' representing the first function, usually from simulations |
required |
function_2
|
dict
|
Dictionary with keys 'x' and 'y' representing the second function, usually from experimental meassurements. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
rchi |
float
|
Wright coefficient, a measure of similarity between the two functions. |
Source code in src/vitrum/comparison.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |