Utilities
This module contains some utility functions which are employed extensively throughout the code.
snow.descriptors.utils
distance_matrix(coords)
Computes distance between atoms and saves them in a matrix of distances
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coords
|
ndarray
|
array of the 3d coordinates of the N atoms |
required |
Returns:
| Name | Type | Description |
|---|---|---|
distance_matrix |
ndarray
|
distance matrix |
Source code in snow/descriptors/utils.py
adjacency_matrix(coords, cutoff)
Computes the adjacency matrix Ad_ij, where the entry ij is 1 if distance r_ij<=cutoff, else is 0
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coords
|
ndarray
|
coordinates of the system |
required |
cutoff
|
ndarray
|
cutoff to define neighbouring atoms |
required |
Returns:
| Name | Type | Description |
|---|---|---|
adj_matrix |
ndarray
|
adjacency matrix |
Source code in snow/descriptors/utils.py
sparse_adjacency_matrix(coords, cutoff)
Create a sparse adjacency matrix where entries are 1 if the distance between points is less than or equal to the cutoff.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coords
|
ndarray
|
Array with the XYZ coordinates of the atoms, shape (n_atoms, 3). |
required |
cut_off
|
float
|
Cutoff distance for finding neighbors in angstrom. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
adjacency_matrix |
ndarray
|
A sparse adjacency matrix with ones for adjacent points. |
Source code in snow/descriptors/utils.py
hetero_distance_matrix(coords, elements)
Computes the distance matrix for only atoms with different chemical species
Only takes into account hetero pairs, e.g. if atom "i" is a gold atom and atom "j" a platinum atom then \(M_{ij} = d_{ij}\), if they are both gold atoms then \(M_{ij} = 0\))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coords
|
ndarray
|
positions of the atoms in the system |
required |
elements
|
ndarray
|
array of chemical species of the atoms in the system |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dist_mat |
ndarray
|
hetero-pairs distance matrix |
Source code in snow/descriptors/utils.py
distance_matrix_pbc(positions, cell)
Compute pairwise distance matrix under periodic boundary conditions.
CKDTree only accepts parallelopipedal cells. This method is slower but works with any cell provided as a (3,3) array (3 axes defining the cell).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
positions
|
(N,3) array-like
|
Cartesian coordinates. |
required |
cell
|
(3,3) array-like
|
Simulation cell matrix (rows = lattice vectors). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dmat |
(N,N) ndarray
|
Pairwise distance matrix using minimum image convention. |
Source code in snow/descriptors/utils.py
nn_pbc(coords, box, cut_off)
Computes the nearest neighbour list with a slower but pbc-compatible algorithm
Nearest neighbour calculation with KDTree can only be done with rectangular boxes. This helper function allows to get nearest neighbours in PBC with any kind of box, given the box as ((a1, a2, a3), (b1, b2, b3), (c1, c2, c3))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coords
|
ndarray
|
XYZ coordinates of atoms, shape (n_atoms, 3). |
required |
box
|
ndarray
|
Simulation cell matrix with shape (3, 3), where rows are lattice vectors. |
required |
cut_off
|
float
|
Cutoff distance for finding neighbors (in Ã…). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
neigh |
list of lists
|
The i-th sublist contains the indices of neighboring atoms for the i-th atom. |
Source code in snow/descriptors/utils.py
nearest_neighbours(coords, cut_off=None, pbc=False, box=None)
Computes nearest neighbors for each atom, considering periodic boundary conditions (PBC) if requested.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coords
|
ndarray
|
XYZ coordinates of atoms, shape (n_atoms, 3). |
required |
cut_off
|
float
|
Cutoff distance for finding neighbors (in Ã…). If None, an adaptive cutoff is used. |
None
|
pbc
|
bool
|
Whether to apply periodic boundary conditions (default: False). |
False
|
box
|
ndarray
|
Simulation box size in the form (3,) for orthorhombic boxes or (3,2) for lower and upper bounds or (3,3) if you pass box vectors (slower). |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
neigh |
list of lists
|
The i-th sublist contains the indices of neighboring atoms for the i-th atom. |
Source code in snow/descriptors/utils.py
pairs_from_neighbor_list(neigh_list)
Convert neighbor list to unique pair list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
neigh_list
|
list of lists
|
For each atom i, a list of its neighbors |
required |
Returns:
| Name | Type | Description |
|---|---|---|
piars |
list of tuples
|
Unique pairs |
Source code in snow/descriptors/utils.py
pair_list(coords, cut_off=None, pbc=False, box=None)
Generates a list of all pairs of atoms within a certain adaptive cutoff distance of each other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coords
|
ndarray
|
Array with the XYZ coordinates of the atoms, shape (n_atoms, 3). |
required |
cut_off
|
float
|
Cutoff distance for finding pairs in angstroms. If None, an adaptive cutoff is used per atom. |
None
|
pbc
|
bool
|
Whether to apply periodic boundary conditions. |
False
|
box
|
ndarray
|
Simulation box size (either [Lx, Ly, Lz] or [[xmin, xmax], [ymin, ymax], [zmin, zmax]] or 3 cell vectors (shape (3,3) - slower)). Has to provided if pbc=True |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
pairs |
list
|
List of tuples with indexes of pairs of atoms that are within the cutoff distance. |
Source code in snow/descriptors/utils.py
kl_div(func1, func2)
Calculate the Kullback-Leibler divergence between two functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func1
|
array
|
values taken by the first function for a given set of inputs |
required |
func2
|
array
|
values taken by the second function for the same set of inputs |
required |
Returns:
| Name | Type | Description |
|---|---|---|
kldiv |
float)
|
KL divergence between function 1 and function 2 |
Source code in snow/descriptors/utils.py
apply_pbc(coords, box_size)
Apply periodic boundary conditions to atom coordinates.
Maps coordinates to be within the simulation box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coords
|
ndarray
|
Atom coordinates array, shape (n_atoms, 3). |
required |
box_size
|
ndarray
|
Box size array (3,) or (3,2) depending on box format. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
coords |
ndarray
|
Coordinates after applying PBC. |
Source code in snow/descriptors/utils.py
bounding_box(points)
Calculate an axis-aligned bounding box from a set of points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
ndarray
|
coordinates of the points in the system |
required |
Returns:
| Name | Type | Description |
|---|---|---|
box |
ndarray
|
computed axis-aligned bounding box. |
Source code in snow/descriptors/utils.py
second_neighbours(coords, cutoff, pbc=False, box=None)
Generates a list of lists of atomic indices for each atom corresponding to atoms that are neighbours of first neighbours excluding those which are already first neighbours.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coords
|
ndarray
|
Array with the XYZ coordinates of the atoms, shape (n_atoms, 3). |
required |
cutoff
|
float
|
Cutoff distance for finding pairs in angstroms. If None, an adaptive cutoff is used per atom. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
snn_list |
list[list]
|
List of lists containing indeces of second neighbours for each atom |
Source code in snow/descriptors/utils.py
get_coords_by_element(el, coords, chosen_element)
Returns the coordinates (and the chemical element array, for consistency) filtered for a selected chemical specie. It can deal with both single frame and 'movie'-style coords objects in list or np.ndarray format
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
el
|
chemical elements of the atoms corresponding to the positions in coords |
required | |
cooords
|
positions of the atoms in the system |
required | |
chosen_element
|
element you want to select in your system to get the coordinates of those atoms. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tuple
|
|
|
el |
list or list[list]
|
list of elements of selected atoms. The return type depends on whether the input is a single frame or a movie (list of frames) |
coords |
ndarray or list[ndarray]
|
cooridnates of selected atoms. The return type depends on whether the input is a single frame or a movie (list of frames) |
Source code in snow/descriptors/utils.py
pbc_distance(p1, p2, box)
Computes the minimum image distance between two points under periodic boundary conditions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p1
|
ndarray
|
Position of the first point. |
required |
p2
|
ndarray
|
Position of the second point. |
required |
box
|
ndarray
|
Cell vectors as a (3,3) array (rows are vectors a, b, c) or (3,) array (xmax, ymax, zmax). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
distance |
float
|
Minimum image distance between p1 and p2. |
Source code in snow/descriptors/utils.py
progress_bar(current, total, length=50)
Prints a nice progess bar
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current
|
float
|
current step |
required |
total
|
float
|
total number of steps |
required |
length
|
int
|
how many elements your progress bar should be made of. |
50
|