-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #475 from N720720/467-optimize-lindemann-index-cal…
…culation-using-distance-vector 467 optimize lindemann index calculation using distance vector
- Loading branch information
Showing
8 changed files
with
140 additions
and
205 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 |
---|---|---|
@@ -1,72 +1,30 @@ | ||
from typing import List | ||
|
||
import numba as nb | ||
import numpy as np | ||
import numpy.typing as npt | ||
|
||
|
||
@nb.njit(fastmath=True, error_model="numpy") # type: ignore # , cache=True) #(parallel=True) | ||
def calculate(frames: npt.NDArray[np.float32]) -> npt.NDArray[np.float32]: | ||
"""calculate the progression of the lindemann index over the frames. | ||
Args: | ||
frames: numpy array of shape(frames,atoms) | ||
Returns: | ||
npt.NDArray[np.float32]: Returns 1D array with the progression of the lindeman index per frame of shape(frames) | ||
""" | ||
|
||
first = True | ||
dt = frames.dtype | ||
natoms = len(frames[0]) | ||
nframes = len(frames) | ||
len_frames = len(frames) | ||
array_mean = np.zeros((natoms, natoms), dtype=dt) | ||
array_var = np.zeros((natoms, natoms), dtype=dt) | ||
iframe = dt.type(1) | ||
lindex_array = np.zeros((len_frames), dtype=dt) | ||
for q, coords in enumerate(frames): | ||
n, p = coords.shape | ||
array_distance = np.zeros((n, n), dtype=dt) | ||
for i in range(n): | ||
for j in range(i + 1, n): | ||
d = 0.0 | ||
for k in range(p): | ||
d += (coords[i, k] - coords[j, k]) ** dt.type(2) | ||
array_distance[i, j] = np.sqrt(d) | ||
array_distance += array_distance.T | ||
|
||
################################################################################# | ||
# update mean and var arrays based on Welford algorithm suggested by Donald Knuth | ||
################################################################################# | ||
for i in range(natoms): | ||
for j in range(i + 1, natoms): | ||
xn = array_distance[i, j] | ||
mean = array_mean[i, j] | ||
var = array_var[i, j] | ||
delta = xn - mean | ||
# update mean | ||
array_mean[i, j] = mean + delta / iframe | ||
# update variance | ||
array_var[i, j] = var + delta * (xn - array_mean[i, j]) | ||
iframe += 1 # type: ignore[assignment] | ||
if iframe > nframes + 1: | ||
break | ||
|
||
for i in range(natoms): | ||
for j in range(i + 1, natoms): | ||
array_mean[j, i] = array_mean[i, j] | ||
array_var[j, i] = array_var[i, j] | ||
|
||
if first: | ||
lindemann_indices = 0 | ||
first = False | ||
else: | ||
np.fill_diagonal(array_mean, 1) | ||
lindemann_indices = np.zeros((natoms), dtype=dt) # type: ignore[assignment] | ||
lindemann_indices = np.divide(np.sqrt(np.divide(array_var, iframe - 1)), array_mean) # type: ignore[assignment] | ||
lindemann_indices = np.mean( | ||
np.asarray([np.mean(lin[lin != 0]) for lin in lindemann_indices]) # type: ignore[attr-defined] | ||
) | ||
|
||
lindex_array[q] = lindemann_indices | ||
return lindex_array | ||
@nb.njit(fastmath=True, parallel=False) | ||
def calculate(positions): | ||
num_frames, num_atoms, _ = positions.shape | ||
num_distances = num_atoms * (num_atoms - 1) // 2 | ||
|
||
mean_distances = np.zeros(num_distances, dtype=np.float32) | ||
m2_distances = np.zeros(num_distances, dtype=np.float32) | ||
linde_per_frame = np.zeros(num_frames, dtype=np.float32) | ||
for frame in range(num_frames): | ||
index = 0 | ||
frame_count = frame + 1 | ||
for i in range(num_atoms): | ||
for j in range(i + 1, num_atoms): | ||
dist = 0.0 | ||
for k in range(3): | ||
dist += (positions[frame, i, k] - positions[frame, j, k]) ** 2 | ||
dist = np.sqrt(dist) | ||
delta = dist - mean_distances[index] | ||
mean_distances[index] += delta / frame_count | ||
delta2 = dist - mean_distances[index] | ||
m2_distances[index] += delta * delta2 | ||
|
||
index += 1 | ||
linde_per_frame[frame] = np.mean(np.sqrt(m2_distances / frame_count) / mean_distances) | ||
|
||
return linde_per_frame |
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 |
---|---|---|
@@ -1,64 +1,29 @@ | ||
from typing import Any | ||
|
||
import numba as nb | ||
import numpy as np | ||
import numpy.typing as npt | ||
|
||
# No typing for jit functions https://github.com/numba/numba/issues/7424 | ||
|
||
|
||
@nb.njit(fastmath=True, error_model="numpy") # type: ignore | ||
def lindemann_per_atom(frames: npt.NDArray[np.float32]) -> Any: | ||
"""Calculate the lindeman index | ||
Args: | ||
frames: numpy array of shape(frames,atoms) | ||
Returns: | ||
float32: returns the lindeman index | ||
""" | ||
|
||
dt = frames.dtype | ||
natoms = len(frames[0]) | ||
nframes = len(frames) | ||
array_mean = np.zeros((natoms, natoms), dtype=dt) | ||
array_var = np.zeros((natoms, natoms), dtype=dt) | ||
iframe = dt.type(1) | ||
for coords in frames: | ||
|
||
# here we do someting similar to scipy's spatial.distance.pdist scipy.spatial.distance.pdist | ||
n, p = coords.shape | ||
array_distance = np.zeros((n, n), dtype=dt) | ||
for i in range(n): | ||
for j in range(i + 1, n): | ||
d = dt.type(0.0) | ||
for k in range(p): | ||
d += (coords[i, k] - coords[j, k]) ** dt.type(2) | ||
array_distance[i, j] = np.sqrt(d) | ||
array_distance += array_distance.T | ||
|
||
################################################################################# | ||
# update mean and var arrays based on Welford algorithm suggested by Donald Knuth | ||
################################################################################# | ||
for i in range(natoms): | ||
for j in range(i + 1, natoms): | ||
xn = array_distance[i, j] | ||
mean = array_mean[i, j] | ||
var = array_var[i, j] | ||
delta = xn - mean | ||
array_mean[i, j] = mean + delta / iframe | ||
array_var[i, j] = var + delta * (xn - array_mean[i, j]) | ||
iframe += 1.0 # type: ignore[assignment] | ||
if iframe > nframes: | ||
break | ||
|
||
for i in range(natoms): | ||
for j in range(i + 1, natoms): | ||
array_mean[j, i] = array_mean[i, j] | ||
array_var[j, i] = array_var[i, j] | ||
@nb.njit(fastmath=True) | ||
def calculate(positions): | ||
num_frames, num_atoms, _ = positions.shape | ||
num_distances = num_atoms * (num_atoms - 1) // 2 | ||
|
||
lindemann_indices = np.divide(np.sqrt(np.divide(array_var, nframes)), array_mean) | ||
return lindemann_indices | ||
mean_distances = np.zeros(num_distances, dtype=np.float32) | ||
m2_distances = np.zeros(num_distances, dtype=np.float32) | ||
|
||
for frame in range(num_frames): | ||
index = 0 | ||
frame_count = frame + 1 | ||
for i in range(num_atoms): | ||
for j in range(i + 1, num_atoms): | ||
dist = 0.0 | ||
for k in range(3): | ||
dist += (positions[frame, i, k] - positions[frame, j, k]) ** 2 | ||
dist = np.sqrt(dist) | ||
delta = dist - mean_distances[index] | ||
mean_distances[index] += delta / frame_count | ||
delta2 = dist - mean_distances[index] | ||
m2_distances[index] += delta * delta2 | ||
|
||
def calculate(frames: npt.NDArray[np.float64]) -> float: | ||
index += 1 | ||
|
||
return np.mean(np.nanmean(lindemann_per_atom(frames), axis=1)) # type: ignore[no-any-return, no-untyped-call] | ||
return np.mean(np.sqrt(m2_distances / num_frames) / mean_distances) |
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
Oops, something went wrong.