-
Notifications
You must be signed in to change notification settings - Fork 823
Description
Digging into the optimization of AtomGroup.unwrap (from the discussion in #2982) I am trying to replicate the vectorization done in AtomGroup.center. I noticed that in there there's a sorting of compound indices that isn't being done if the user requests compound unwrapping. This breaks center calculation if for some reason a compound's atoms are not initially contiguous.
This is a corner case, but still possible. The fix is quite simple, and if it's ok with you I'd include it in the upcoming PR dedicated to the wrap/unwrap enhancement because that will also be dealing with similar code patterns, in the same source file (core/groups.py).
Code to reproduce the behavior
With a simple 5-water system:
import MDAnalysis as mda
from MDAnalysis.tests.datafiles import waterPSF, waterDCD
u = mda.Universe(waterPSF, waterDCD)
testgrp = u.atoms
testndx = np.arange(len(testgrp))
np.random.shuffle(testndx)
testgrp_unordered = testgrp[testndx]
testgrp_unordered.fragindices
>> array([3, 0, 1, 1, 2, 2, 2, 4, 3, 4, 1, 0, 0, 4, 3])The fragment COGs of the test group:
testgrp.center_of_geometry(compound='fragments')
>> array([[ 16.86431313, -1.89109834, 23.81008784],
[ 18.23819478, 0.2692619 , -24.17409579],
[ 18.62940788, -3.88507994, -24.22752635],
[ 20.89391009, -0.74630801, -24.41894786],
[ 21.33577983, -3.48827068, -23.58818181]])Same answer if atoms are unwrapped but consecutive (molecules are already whole in this test traj):
testgrp.center_of_geometry(compound='fragments', unwrap=True)
>> array([[ 16.86431313, -1.89109834, 23.81008784],
[ 18.23819478, 0.2692619 , -24.17409579],
[ 18.62940788, -3.88507994, -24.22752635],
[ 20.89391009, -0.74630801, -24.41894786],
[ 21.33577983, -3.48827068, -23.58818181]])Also ok if randomized but not unwrapped:
testgrp_unordered.center_of_geometry(compound='fragments')
>> array([[ 16.86431313, -1.89109834, 23.81008784],
[ 18.23819478, 0.2692619 , -24.17409579],
[ 18.62940788, -3.88507994, -24.22752635],
[ 20.89391009, -0.74630801, -24.41894786],
[ 21.33577983, -3.48827068, -23.58818181]])Oops!
testgrp_unordered.center_of_geometry(compound='fragments', unwrap=True)
>> array([[ 18.76917013, -0.9612124 , -8.50591214],
[ 18.56716092, -2.47748999, -23.95220439],
[ 20.31393941, -2.90605104, -23.84455808],
[ 18.98535792, -1.3438748 , -8.07818158],
[ 19.32597733, -2.05286684, -8.21780777]])Current version of MDAnalysis
2.0.0-dev0 (at commit 64a7c05), Python 3.6.9, Ubuntu 18.04
EDIT: output clarification