utils
torch_admp.utils
Utility functions for torch-admp.
This module provides various utility functions used throughout the torch-admp package, including mathematical operations, unit conversions, and helper functions for optimization and calculations.
TorchConstants(units_dict: Optional[Dict] = None)
Bases: Module
Physical constants and unit conversions for torch-admp.
This class provides consistent physical constants and unit conversions compatible with ASE (Atomic Simulation Environment). It handles conversion between different unit systems for energy, length, and other physical quantities used in molecular simulations.
Notes
Electron volts (eV), Ångström (Ang), atomic mass unit and Kelvin are defined as 1.0.
Consistent with ASE: https://wiki.fysik.dtu.dk/ase/ase/units.html Example: units_dict = { "energy": "kJ/mol", "length": "nm", } Electron volts (eV), Ångström (Ang), the atomic mass unit and Kelvin are defined as 1.0.
Source code in torch_admp/utils.py
calc_grads(t_out: torch.Tensor, t_in: torch.Tensor)
Calculate gradients
| PARAMETER | DESCRIPTION |
|---|---|
t_out
|
Outputs of the differentiated function
TYPE:
|
t_in
|
Inputs w.r.t. which the gradient will be returned
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
grad
|
Gradients
TYPE:
|
Source code in torch_admp/utils.py
calc_pgrads(t_out: torch.Tensor, t_in: torch.Tensor, constraint_matrix: torch.Tensor, coeff_matrix: torch.Tensor)
Calculate projected gradients for constrained optimization
| PARAMETER | DESCRIPTION |
|---|---|
t_out
|
Output tensor
TYPE:
|
t_in
|
Input tensor
TYPE:
|
constraint_matrix
|
n_const * natoms, constraint matrix
TYPE:
|
coeff_matrix
|
natoms * n_const, Coefficient matrix for vector projection
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Projected gradients |
Source code in torch_admp/utils.py
pair_buffer_scales(pairs: torch.Tensor) -> torch.Tensor
Calculate buffer scales for atom pairs.
| PARAMETER | DESCRIPTION |
|---|---|
pairs
|
Tensor of atom pairs
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Buffer scales for each pair (1 if i < j, else 0) |
Source code in torch_admp/utils.py
regularize_pairs(pairs: torch.Tensor, buffer_scales: torch.Tensor) -> torch.Tensor
Regularize atom pairs based on buffer scales.
| PARAMETER | DESCRIPTION |
|---|---|
pairs
|
Tensor of atom pairs
TYPE:
|
buffer_scales
|
Buffer scales for each pair
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Regularized atom pairs |
Source code in torch_admp/utils.py
safe_inverse(x: torch.Tensor, threshold: float = 1e-08) -> torch.Tensor
Safe inverse for numerical stability
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Input tensor
TYPE:
|
threshold
|
Threshold for numerical stability
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
inv_x
|
Inverse of x if x.abs() > threshold, otherwise 0
TYPE:
|
Source code in torch_admp/utils.py
vector_projection(vector_in: torch.Tensor, constraint_matrix: torch.Tensor, constraint_vector: Optional[torch.Tensor] = None) -> torch.Tensor
Vector projection subject to linear constraints P(x) = x + A^T(A A^T)^{-1}(b - Ax)
| PARAMETER | DESCRIPTION |
|---|---|
vector_in
|
Input vector (n_atoms * 1).
TYPE:
|
constraint_matrix
|
Constraint matrix (n_const * natoms).
TYPE:
|
constraint_vector
|
Constraint vector (n_const * 1). All zeros when set as None (default).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
vector_out
|
n_atoms, projected vector
TYPE:
|
Source code in torch_admp/utils.py
vector_projection_coeff_matrix(constraint_matrix: torch.Tensor) -> torch.Tensor
Calculate coefficient matrix for vector projection based on constraint matrix
| PARAMETER | DESCRIPTION |
|---|---|
constraint_matrix
|
Constraint matrix (n_const * natoms).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
coeff_mat
|
Coefficient matrix (n_atoms * n_const).
TYPE:
|