Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds unwrap keyword to moment of inertia #2288

Merged
merged 15 commits into from
Jul 9, 2019
Prev Previous commit
Next Next commit
Review changes
  • Loading branch information
NinadBhat committed Jun 19, 2019
commit 6f21b3143c60199e95fc50bb7d95de878aeabf33
12 changes: 5 additions & 7 deletions package/MDAnalysis/core/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,11 +679,8 @@ def center(self, weights, pbc=None, compound='group', unwrap=False):
will be returned as an array of position vectors, i.e. a 2d array.
Note that, in any case, *only* the positions of :class:`Atoms<Atom>`
*belonging to the group* will be taken into account.
unwrap : bool or None, optional
If ``True`` and `compound` is ``'group'``, the atoms will be unwrapped
before calculations. If ``True`` and `compound` is
``'segments'`` or ``'residues'``, all molecules will be unwrapped before
calculation to keep the compounds intact.
unwrap : bool, optional
If ``True``, compounds will be unwrapped before computing their centers.

Returns
-------
Expand Down Expand Up @@ -739,6 +736,9 @@ def center(self, weights, pbc=None, compound='group', unwrap=False):

comp = compound.lower()
if comp == 'group':
if unwrap and pbc:
raise ValueError("'unwrap' and 'pbc' cannot be true at the same time "
"for compound='group")
if unwrap:
coords = atoms.unwrap(compound=comp, reference=None, inplace=False)
if pbc:
Expand Down Expand Up @@ -776,8 +776,6 @@ def center(self, weights, pbc=None, compound='group', unwrap=False):
" one of 'group', 'residues', 'segments', "
"'molecules', or 'fragments'.".format(compound))



# Sort positions and weights by compound index and promote to dtype if
# required:
sort_indices = np.argsort(compound_indices)
Expand Down
7 changes: 7 additions & 0 deletions testsuite/MDAnalysisTests/core/test_atomgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,13 @@ def test_center_unwrap(self, level, compound, is_triclinic):
ref_center = u.center(compound=compound)
assert_almost_equal(ref_center, center, decimal=4)

def test_center_unwrap_pbc_true(self):
u = UnWrapUniverse(is_triclinic=False)
# select group appropriate for compound:
group = u.atoms[39:47] # molecule 12
with pytest.raises(ValueError):
group.center(weights=None, compound="group", unwrap=True, pbc=True)


class TestSplit(object):

Expand Down
20 changes: 10 additions & 10 deletions testsuite/MDAnalysisTests/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,28 +579,28 @@ def center(self, compound):
center_pos = np.zeros((12, 3), dtype=np.float32)

for base in range(3):
loc_centre = relpos[base, :]
center_pos[pos,:] = loc_centre
loc_center = relpos[base, :]
center_pos[pos,:] = loc_center
pos+=1

for base in range(3, 15, 3):
loc_centre = np.mean(relpos[base:base + 3, :], axis=0)
center_pos[pos,:] = loc_centre
loc_center = np.mean(relpos[base:base + 3, :], axis=0)
center_pos[pos,:] = loc_center
pos+=1

if compound=="residues":
for base in range(15, 47, 4):
loc_centre = np.mean(relpos[base:base + 4, :], axis=0)
center_pos[pos,:] = loc_centre
loc_center = np.mean(relpos[base:base + 4, :], axis=0)
center_pos[pos,:] = loc_center
pos+=1
else:
for base in range(15, 23, 4):
loc_centre = np.mean(relpos[base:base + 4, :], axis=0)
center_pos[pos,:] = loc_centre
loc_center = np.mean(relpos[base:base + 4, :], axis=0)
center_pos[pos,:] = loc_center
pos+=1
for base in range(23, 47, 8):
loc_centre = np.mean(relpos[base:base + 8, :], axis=0)
center_pos[pos,:] = loc_centre
loc_center = np.mean(relpos[base:base + 8, :], axis=0)
center_pos[pos,:] = loc_center
pos+=1

if compound == "group":
Expand Down