Skip to content

Commit 658e276

Browse files
committed
fix sorting bug in get_vanilla_axes moment of inertia
1 parent 1278269 commit 658e276

1 file changed

Lines changed: 25 additions & 12 deletions

File tree

CodeEntropy/axes.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -245,27 +245,40 @@ def find_bonded_atoms(self, atom_idx: int, system):
245245

246246
def get_vanilla_axes(self, molecule):
247247
"""
248-
From a selection of atoms, get the ordered principal axes (3,3) and
249-
the ordered moment of inertia axes (3,) for that selection of atoms
248+
Compute the principal axes and sorted moments of inertia for a molecule.
249+
250+
This method computes the translationally invariant principal axes and
251+
corresponding moments of inertia for a molecular selection using the
252+
default MDAnalysis routines. The molecule is first made whole to ensure
253+
correct handling of periodic boundary conditions.
254+
255+
The moments of inertia are obtained by diagonalising the moment of inertia
256+
tensor and are returned sorted from largest to smallest magnitude.
250257
251258
Args:
252-
molecule: mdanalysis instance of molecule
253-
molecule_scale: the length scale of molecule
259+
molecule (MDAnalysis.core.groups.AtomGroup):
260+
AtomGroup representing the molecule or bead for which the axes
261+
and moments of inertia are to be computed.
254262
255263
Returns:
256-
principal_axes: the principal axes, (3,3) array
257-
moment_of_inertia: the moment of inertia, (3,) array
264+
Tuple[np.ndarray, np.ndarray]:
265+
A tuple containing:
266+
267+
- principal_axes (np.ndarray):
268+
Array of shape ``(3, 3)`` whose rows correspond to the
269+
principal axes of the molecule.
270+
- moment_of_inertia (np.ndarray):
271+
Array of shape ``(3,)`` containing the moments of inertia
272+
sorted in descending order.
258273
"""
259-
# default moment of inertia
260274
moment_of_inertia = molecule.moment_of_inertia(unwrap=True)
261275
make_whole(molecule.atoms)
262276
principal_axes = molecule.principal_axes()
263-
# diagonalise moment of inertia tensor here
264-
# pylint: disable=unused-variable
277+
265278
eigenvalues, _eigenvectors = np.linalg.eig(moment_of_inertia)
266-
# sort eigenvalues of moi tensor by largest to smallest magnitude
267-
order = sorted(eigenvalues, reverse=True) # decending order
268-
# principal_axes = principal_axes[order] # PI already ordered correctly
279+
280+
# Sort eigenvalues from largest to smallest magnitude
281+
order = np.argsort(np.abs(eigenvalues))[::-1]
269282
moment_of_inertia = eigenvalues[order]
270283

271284
return principal_axes, moment_of_inertia

0 commit comments

Comments
 (0)