A package for computing distances accounting for periodic boundary conditions
Project description
pbc_distance_calculator
This Python package computes pairwise distances in a simulation box accounting for periodic boundary conditions.
The only inputs are the positions of each particle and the simulation supercell matrix.
To install:
pip install pbc_distance_calculator
Example usage:
from numpy.typing import NDArray
from pbc_distance_calculator import get_pairwise_distances
# array of shape (N, 3) where N is the number of particles
positions: NDArray = ...
# array of shape (3, 3)
cell_matrix: NDArray = ...
# array of shape (N, N)
# element (i, j) is minimum image distance between i and j
pairwise_distances: NDArray = get_pairwise_distances(positions, cell_matrix)
The above script performs the calculation in a vectorized form, computing every pairwise distance at once. To do it serially instead:
from numpy.typing import NDArray
from pbc_distance_calculator import get_pairwise_distance
# arrays of shape (1, 3) or (3, 1)
first_position: NDArray = ...
second_position: NDArray = ...
# array of shape (3, 3)
cell_matrix: NDArray = ...
# minimum image distance
pairwise_distance: float = get_pairwise_distance(
first_position - second_position,
cell_matrix
)
In both functions, you can also specify different engines to compute the distances. This is especially advantageous for large systems, where you can specify jax.numpy
or torch
as an engine. For example:
import torch
from pbc_distance_calculator import get_pairwise_distances
...
torch.set_default_device("cuda")
pairwise_distances = get_pairwise_distances(
positions,
cell_matrix,
engine=torch
)
which will calculate the pairwise distances using the CUDA-backend of PyTorch. Note that the only engine installed by default is numpy
, so make sure to separately install jax
or torch
if you want to use these modules.
Note that the cell matrix, is, in general:
$$ \begin{pmatrix} \mathbf{a} & \mathbf{b} & \mathbf{c} \end{pmatrix} $$
where $\mathbf{a}$, $\mathbf{b}$, and $\mathbf{c}$ are the lattice vectors of the supercell. Note that this definition works for any set of lattice parameters! So, no matter how weird your crystal, this package should work. If there are any problems, feel free to open an issue 🙂.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pbc_distance_calculator-1.3.2.tar.gz
.
File metadata
- Download URL: pbc_distance_calculator-1.3.2.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 184139cf4725a378a19f97931a8108a86e4485d02835593ddd53d741512af438 |
|
MD5 | c738d72b023988f899bfcda1f891732d |
|
BLAKE2b-256 | 69844e95a04723802b242d6733402c6b195d6aafbeb8c43728ef44bc5a161f9b |
File details
Details for the file pbc_distance_calculator-1.3.2-py3-none-any.whl
.
File metadata
- Download URL: pbc_distance_calculator-1.3.2-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a4b939492b1a1021aae52925119c3d527331e50d42071bea63265a38042e0afb |
|
MD5 | e42be4f9677dfeff9001f71ac3e6d0ba |
|
BLAKE2b-256 | 2f6d206c5ab8ac6c12757d7863d932e50ca20bbd1eb1ca07836ed4777f5bf6df |