diff --git a/docs/source/_static/info_plot.png b/docs/source/_static/info_plot.png index 5a8c2a4c..b426acf7 100644 Binary files a/docs/source/_static/info_plot.png and b/docs/source/_static/info_plot.png differ diff --git a/src/dynsight/_internal/lens/lens.py b/src/dynsight/_internal/lens/lens.py index 30c7a8bb..d3b8d26a 100644 --- a/src/dynsight/_internal/lens/lens.py +++ b/src/dynsight/_internal/lens/lens.py @@ -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 @@ -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 @@ -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 diff --git a/src/dynsight/_internal/trajectory/trajectory.py b/src/dynsight/_internal/trajectory/trajectory.py index 1df2da3c..ac121860 100644 --- a/src/dynsight/_internal/trajectory/trajectory.py +++ b/src/dynsight/_internal/trajectory/trajectory.py @@ -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) diff --git a/tests/lens/test_lens.py b/tests/lens/test_lens.py index dc524fb9..9d209fc7 100644 --- a/tests/lens/test_lens.py +++ b/tests/lens/test_lens.py @@ -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 @@ -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) diff --git a/tests/systems/lens_2d_fbc.npy b/tests/systems/lens_2d_fbc.npy new file mode 100644 index 00000000..726cc665 Binary files /dev/null and b/tests/systems/lens_2d_fbc.npy differ diff --git a/tests/systems/lens_2d_pbc.npy b/tests/systems/lens_2d_pbc.npy new file mode 100644 index 00000000..2ec09428 Binary files /dev/null and b/tests/systems/lens_2d_pbc.npy differ