Skip to content

Commit

Permalink
Added fix for Issue #532
Browse files Browse the repository at this point in the history
Prevents addition of AtomGroups from different Universes
  • Loading branch information
richardjgowers committed Nov 13, 2015
1 parent 51b109e commit b984aa9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions package/MDAnalysis/core/AtomGroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,10 @@ def __add__(self, other):
if not isinstance(other, (Atom, AtomGroup)):
raise TypeError('Can only add Atoms or AtomGroups (not "{0}")'
' to Atom'.format(other.__class__.__name__))
# Dodge property to avoid NoDataErrors
# Will then allow addition of NoneUniverse to NoneUniverse..
if not self._universe is other._universe:
raise ValueError("Can only add objects from the same Universe")
if isinstance(other, Atom):
return AtomGroup([self, other])
else:
Expand Down Expand Up @@ -1074,6 +1078,8 @@ def __add__(self, other):
if not isinstance(other, (Atom, AtomGroup)):
raise TypeError('Can only concatenate Atom or AtomGroup (not "{0}") to'
' AtomGroup'.format(other.__class__.__name__))
if (self and other) and (not self.universe is other.universe):
raise ValueError("Can only add objects from the same Universe")
if isinstance(other, AtomGroup):
return AtomGroup(self._atoms + other._atoms)
else:
Expand Down
9 changes: 9 additions & 0 deletions testsuite/MDAnalysisTests/test_atomgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2191,12 +2191,21 @@ def add(x, y):

def test_add_mixed_universes(self):
# Issue #532
# Checks that adding objects from different universes
# doesn't proceed quietly.
u1 = MDAnalysis.Universe(two_water_gro)
u2 = MDAnalysis.Universe(two_water_gro)

A = [u1.atoms[:2], u1.atoms[3]]
B = [u2.atoms[:3], u2.atoms[0]]

# Checks Atom to Atom, Atom to AG, AG to Atom and AG to AG
for x, y in itertools.product(A, B):
yield self._check_badadd, x, y

def test_adding_empty_ags(self):
# Check that empty AtomGroups don't trip up on the Universe check
u = MDAnalysis.Universe(two_water_gro)

assert_(len(AtomGroup([]) + u.atoms[:3]) == 3)
assert_(len(u.atoms[:3] + AtomGroup([])) == 3)

0 comments on commit b984aa9

Please sign in to comment.