Skip to content

Commit

Permalink
Replace atomic trajectory cython version with numpy implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Raunakk Banerjee committed Oct 3, 2024
1 parent 0c733b8 commit e35cf46
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
.vscode
*.pyc
mdanse_env/
.venv/
sandbox/

# Distribution / packaging
Expand Down
10 changes: 4 additions & 6 deletions MDANSE/Src/MDANSE/Trajectory/AtomicTrajectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@


def atomic_trajectory(config, cell, rcell, box_coordinates=False):
trajectory = np.empty((config.shape[0], 3), dtype=np.float64)
trajectory = np.einsum("ij,ikj->ik", config, rcell[: config.shape[0]])
sdxyz = trajectory[:-1, :] - trajectory[1:, :]
sdxyz -= np.round(sdxyz)
trajectory = np.einsum("ij,ikj->ik", config, rcell)
sdxyz = trajectory[1:, :] - trajectory[:-1, :]
sdxyz -= np.cumsum(np.round(sdxyz), axis=0)
trajectory[1:, :] = trajectory[:-1, :] + sdxyz

if not box_coordinates:
trajectory = np.einsum("ij,ikj->ik", trajectory, cell[: config.shape[0]])
trajectory = np.einsum("ij,ikj->ik", trajectory, cell)
return trajectory
14 changes: 12 additions & 2 deletions MDANSE/Src/MDANSE/Trajectory/MdanseTrajectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,18 @@ def read_atomic_trajectory(
coords = grp["coordinates"][first:last:step, index, :].astype(np.float64)

if self._unit_cells is not None:
direct_cells = np.array([uc.transposed_direct for uc in self._unit_cells])
inverse_cells = np.array([uc.transposed_inverse for uc in self._unit_cells])
direct_cells = np.array(
[
self._unit_cells[nf].transposed_direct
for nf in range(first, last, step)
]
)
inverse_cells = np.array(
[
self._unit_cells[nf].transposed_inverse
for nf in range(first, last, step)
]
)
atomic_traj = atomic_trajectory(
coords, direct_cells, inverse_cells, box_coordinates
)
Expand Down
26 changes: 26 additions & 0 deletions MDANSE/Tests/UnitTests/test_atomic_trajectory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import numpy as np
import pytest
from MDANSE.Trajectory.AtomicTrajectory import atomic_trajectory
# from MDANSE.Extensions import atomic_trajectory

cells = np.array([np.eye(3)]*5)

def test_constant():
coords = np.zeros((5, 3)) + 0.5
atomic_traj = atomic_trajectory(coords, cells, cells, False)
assert np.allclose(atomic_traj, 0.5)


def test_one_big_jump():
coords = np.zeros((5, 3)) + 3.2
coords[0, 0] = 0.1
atomic_traj = atomic_trajectory(coords, cells, cells, False)
result = np.zeros((5, 3)) + 3.2
result[0, 0] = 0.1
result[1:, 0] = 0.2
assert np.allclose(atomic_traj, result)





0 comments on commit e35cf46

Please sign in to comment.