Skip to content

Commit 88a4e67

Browse files
committed
fix(axes): take the moment of inertia tensor after making the molecule whole
1 parent 6e68b74 commit 88a4e67

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

‎CodeEntropy/levels/axes.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,10 @@ def get_vanilla_axes(self, molecule):
819819
moment of inertia tensor.
820820
821821
The original description is preserved:
822-
- The molecule is made whole to ensure correct handling of PBC.
822+
823+
- The molecule is made whole to ensure correct handling of PBC. The tensor
824+
is taken after that, so it does not depend on where the molecule sits
825+
relative to the periodic boundary.
823826
- The moments are obtained by diagonalising the moment of inertia tensor.
824827
- Eigenvalues are returned sorted from largest to smallest magnitude.
825828
@@ -832,8 +835,8 @@ def get_vanilla_axes(self, molecule):
832835
- principal_axes: (3, 3) axes.
833836
- moment_of_inertia: (3,) moments sorted descending by absolute value.
834837
"""
835-
moment_of_inertia_tensor = molecule.moment_of_inertia(unwrap=True)
836838
make_whole(molecule.atoms)
839+
moment_of_inertia_tensor = molecule.moment_of_inertia()
837840
principal_axes = self.get_principal_axes(molecule)
838841

839842
eigenvalues, _ = np.linalg.eigh(moment_of_inertia_tensor)

‎tests/unit/CodeEntropy/levels/test_axes.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from unittest.mock import MagicMock
22

3+
import MDAnalysis as mda
34
import numpy as np
45
import pytest
56

@@ -392,6 +393,26 @@ def test_get_principal_axes_passes_wrap_to_moment_of_inertia():
392393
group.atoms.moment_of_inertia.assert_called_once_with(wrap=True)
393394

394395

396+
def test_get_vanilla_axes_moments_ignore_position_relative_to_box():
397+
"""Shifting a molecule by a box vector must not change its moments."""
398+
universe = mda.Universe.empty(
399+
n_atoms=3, n_residues=1, atom_resindex=[0, 0, 0], trajectory=True
400+
)
401+
universe.add_TopologyAttr("masses", [16.0, 1.0, 1.0])
402+
universe.add_TopologyAttr("bonds", [(0, 1), (0, 2)])
403+
universe.dimensions = [50.0, 50.0, 50.0, 90.0, 90.0, 90.0]
404+
positions = np.array([[0.0, 0.0, 0.0], [0.96, 0.0, 0.0], [-0.24, 0.93, 0.0]])
405+
ax = AxesCalculator()
406+
407+
universe.atoms.positions = positions + 25.0
408+
_, moments = ax.get_vanilla_axes(universe.atoms)
409+
universe.atoms.positions = positions + 25.0 + np.array([50.0, 0.0, 0.0])
410+
_, shifted_moments = ax.get_vanilla_axes(universe.atoms)
411+
412+
assert np.allclose(shifted_moments, moments, rtol=1e-3)
413+
assert moments.max() < 10.0
414+
415+
395416
def test_find_bonded_atoms_selects_heavy_and_hydrogen_groups():
396417
ax = AxesCalculator()
397418

0 commit comments

Comments
 (0)