-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
144 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# TinyFF is a minimalistic Force Field evaluator. | ||
# Copyright (C) 2024 Toon Verstraelen | ||
# | ||
# This file is part of TinyFF. | ||
# | ||
# TinyFF is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License | ||
# as published by the Free Software Foundation; either version 3 | ||
# of the License, or (at your option) any later version. | ||
# | ||
# TinyFF is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program; if not, see <http://www.gnu.org/licenses/> | ||
# -- | ||
"""Trajectory analysis functions.""" | ||
|
||
import numpy as np | ||
from numpy.typing import ArrayLike, NDArray | ||
from scipy.signal import correlate | ||
|
||
from .neighborlist import NBuild | ||
|
||
__all__ = ("compute_rdf", "compute_acf") | ||
|
||
|
||
def compute_rdf( | ||
traj_atpos: ArrayLike, cell_lengths: ArrayLike, spacing: float, nbuild: NBuild | ||
) -> tuple[NDArray[float], NDArray[float]]: | ||
"""Compute an RDF in post-processing, given a trajectory of atomic positions. | ||
Parameters | ||
---------- | ||
traj_atpos | ||
Configurational snaphots in a 3D array with shape `(nstep, natom, 3)`. | ||
cell_length | ||
The length of the edge of a cubic simulation cell. | ||
spacing | ||
The with of the bins of the histogram. | ||
nbuild | ||
The neighborlist build algorithm for the computation of pairwise distances. | ||
Returns | ||
------- | ||
bin_mids | ||
The midpoints of the histogram bins used to count the number of pairs. | ||
rdf | ||
The radial distribution function at each bin midpoint. | ||
""" | ||
bins = np.arange(int(np.floor(nbuild.rmax / spacing)) + 1) * spacing | ||
counts = 0 | ||
for atpos in traj_atpos: | ||
cell_lengths = nbuild.update(atpos, cell_lengths) | ||
counts += np.histogram(nbuild.nlist["dist"], bins)[0] | ||
sphere_vols = (4 * np.pi / 3) * bins**3 | ||
delta_vols = sphere_vols[1:] - sphere_vols[:-1] | ||
rho_pair = counts / (traj_atpos.shape[0] * delta_vols) | ||
natom = traj_atpos.shape[1] | ||
rho_pair0 = ((natom - 1) * natom) / (2 * np.prod(cell_lengths)) | ||
bin_mids = (bins[1:] + bins[:-1]) / 2 | ||
return bin_mids, rho_pair / rho_pair0 | ||
|
||
|
||
def compute_acf(traj_data): | ||
"""Compute the autocorrelation function of time-dependent data. | ||
Parameters | ||
---------- | ||
traj_data | ||
An array of which the first index corresponds to an equidistant time step. | ||
The autocorrelation function is averaged over all remaining indexes. | ||
Returns | ||
------- | ||
acf | ||
The autocorrelation function, as a function of time lag, | ||
at the same equidistant time steps of the input. | ||
""" | ||
traj_data = traj_data.reshape((traj_data.shape[0], -1)) | ||
acf = 0 | ||
for column in traj_data.T: | ||
acf += correlate(column, column, mode="full") | ||
acf = acf[traj_data.shape[0] - 1 :] | ||
acf /= traj_data.shape[1] | ||
acf /= np.arange(traj_data.shape[0], 0, -1) | ||
return acf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# TinyFF is a minimalistic Force Field evaluator. | ||
# Copyright (C) 2024 Toon Verstraelen | ||
# | ||
# This file is part of TinyFF. | ||
# | ||
# TinyFF is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License | ||
# as published by the Free Software Foundation; either version 3 | ||
# of the License, or (at your option) any later version. | ||
# | ||
# TinyFF is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program; if not, see <http://www.gnu.org/licenses/> | ||
# -- | ||
"""Unit tests for tinyff.analysis.""" | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
from tinyff.analysis import compute_acf, compute_rdf | ||
from tinyff.neighborlist import NBuildSimple | ||
|
||
|
||
def test_compute_rdf_simple(): | ||
traj_atpos = np.array([[[0.0, 0.0, 0.0], [0.0, 0.0, 3.0], [0.0, 4.0, 0.0]]]) | ||
r, g = compute_rdf(traj_atpos, cell_lengths=12.1, spacing=0.7, nbuild=NBuildSimple(rmax=6.0)) | ||
assert r == pytest.approx(np.arange(0.35, 5.5, 0.7)) | ||
mask = np.zeros(r.shape, dtype=bool) | ||
mask[abs(r - 3.0).argmin()] = True | ||
mask[abs(r - 4.0).argmin()] = True | ||
mask[abs(r - 5.0).argmin()] = True | ||
assert (g[mask] != 0).all() | ||
assert (g[~mask] == 0).all() | ||
nz = g[mask] | ||
assert (nz[1:] < nz[:-1]).all() | ||
assert (abs((g * r**2)[mask] - 67) < 0.5).all() | ||
|
||
|
||
def test_compute_vaf_simple(): | ||
rng = np.random.default_rng(42) | ||
traj_atvel = rng.uniform(-1, 1, (100, 10, 3)) | ||
acf = compute_acf(traj_atvel) | ||
assert acf.shape == (100,) |