Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified docs/source/_static/info_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions src/dynsight/_internal/lens/lens.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def compute_lens(
universe.trajectory[t1]
pos_env1 = ag_env.positions.astype(np.float64)
pos_cent1 = ag_cent.positions.astype(np.float64)
if respect_pbc and universe.trajectory.ts.dimensions is not None:
if universe.trajectory.ts.dimensions is not None:
box = universe.trajectory.ts.dimensions[:3]
else:
coords = universe.atoms.positions
Expand All @@ -284,7 +284,7 @@ def compute_lens(
universe.trajectory[t2]
pos_env2 = ag_env.positions.astype(np.float64)
pos_cent2 = ag_cent.positions.astype(np.float64)
if respect_pbc and universe.trajectory.ts.dimensions is not None:
if universe.trajectory.ts.dimensions is not None:
box = universe.trajectory.ts.dimensions[:3]
else:
coords = universe.atoms.positions
Expand Down Expand Up @@ -362,7 +362,7 @@ def _compute_frame_neighbors(frame_idx: int) -> list[AtomGroup]:
pos_env = ag_env.positions.astype(np.float64)
pos_cent = ag_centers.positions.astype(np.float64)

if respect_pbc and universe.trajectory.ts.dimensions is not None:
if universe.trajectory.ts.dimensions is not None:
box = universe.trajectory.ts.dimensions[:3]
else:
coords = universe.atoms.positions
Expand Down
7 changes: 6 additions & 1 deletion src/dynsight/_internal/trajectory/trajectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
from dynsight.trajectory import Insight

UNIVAR_DIM = 2
logging.getLogger("MDAnalysis").setLevel(logging.ERROR)
# Silence MDAnalysis INFO messages by default
logging.getLogger("MDAnalysis").setLevel(logging.WARNING)

# Prevent log propagation unless user configures logging
logging.getLogger("MDAnalysis").addHandler(logging.NullHandler())
logging.getLogger(__name__).addHandler(logging.NullHandler())


@dataclass(frozen=True)
Expand Down
53 changes: 53 additions & 0 deletions tests/lens/test_lens.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,35 @@
import pytest

from dynsight.trajectory import Trj
from dynsight.utilities import save_xyz_from_ndarray

from .case_data import LENSCaseData

# ---------------- Fixtures ----------------


@pytest.fixture
def trj_2d(tmp_path: Path) -> Trj:
"""Return a Trj for a bidimensional system."""
rng = np.random.default_rng(42)
coords = rng.random((100, 100, 3))
coords[..., 2] = 0.0 # system is 2D
traj_path = tmp_path / "random_2d.xyz"
save_xyz_from_ndarray(traj_path, coords)

trj = Trj.init_from_xyz(traj_path, dt=1.0)
for ts in trj.universe.trajectory: # Add box with thickness along z
coords = trj.universe.atoms.positions
mins = coords.min(axis=0)
maxs = coords.max(axis=0)
lengths = maxs - mins # Lx, Ly, Lz
lengths[2] = 0.5
ts.dimensions = np.concatenate([lengths, np.array([90, 90, 90])])
return trj


# ---------------- Tests ----------------


def test_lens(case_data: LENSCaseData) -> None:
original_dir = Path(__file__).resolve().parent
Expand All @@ -35,3 +61,30 @@ def test_lens(case_data: LENSCaseData) -> None:
)
exp_lens = np.load(expected_lens)
assert np.allclose(exp_lens, test_lens.dataset, atol=1e-6)


def test_lens_2d(trj_2d: Trj) -> None:
"""Test LENS and number of neighbors on a 2D system."""
original_dir = Path(__file__).resolve().parent

test_lens_pbc = trj_2d.get_lens(r_cut=0.1, respect_pbc=True)
_ = trj_2d.get_coord_number(r_cut=0.1, respect_pbc=True)
pbc_path = original_dir / "../systems/lens_2d_pbc.npy"
if not pbc_path.exists():
np.save(pbc_path, test_lens_pbc.dataset)
pytest.fail(
"LENS test files were not present. They have been created."
)
exp_lens = np.load(pbc_path)
assert np.allclose(exp_lens, test_lens_pbc.dataset)

test_lens_fbc = trj_2d.get_lens(r_cut=0.1, respect_pbc=False)
_ = trj_2d.get_coord_number(r_cut=0.1, respect_pbc=False)
fbc_path = original_dir / "../systems/lens_2d_fbc.npy"
if not fbc_path.exists():
np.save(fbc_path, test_lens_fbc.dataset)
pytest.fail(
"LENS test files were not present. They have been created."
)
exp_lens = np.load(fbc_path)
assert np.allclose(exp_lens, test_lens_fbc.dataset)
Binary file added tests/systems/lens_2d_fbc.npy
Binary file not shown.
Binary file added tests/systems/lens_2d_pbc.npy
Binary file not shown.