Skip to content

Commit

Permalink
pairs by type (#4022)
Browse files Browse the repository at this point in the history
Description of changes:
- add a ``types`` argument to ``system.cell_system.get_pairs()`` to look only for pairs of particles with the given types
- Interface change: removed the ``_`` from the end of ``get_pairs_()``
  • Loading branch information
kodiakhq[bot] authored Dec 21, 2020
2 parents 7765ce0 + 333cde7 commit 8ee78e2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
11 changes: 9 additions & 2 deletions src/python/espressomd/cellsystem.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,16 @@ cdef class CellSystem:
This function needs to be separated from ``self.get_pairs()`` because ``cdef``
cannot be inside an ``else`` block
"""
if hasattr(types, "__getitem__"):
if len(types) == 1:
check_type_or_throw_except(
types[0], 1, int, 'types must be a list of int')
else:
check_type_or_throw_except(
types, len(types), int, 'types must be a list of int')
else:
raise ValueError('types must be iterable')

check_type_or_throw_except(
types, len(types), int, 'types must be a list of int')
cdef vector[int] types_c
for type in types:
types_c.push_back(type)
Expand Down
12 changes: 9 additions & 3 deletions testsuite/python/pairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ def check_pairs(self):
epairs_by_type = self.expected_pairs_with_types(self.s.periodicity)
self.assertSetEqual(set(pairs_by_type), set(epairs_by_type))

def check_exception(self):
def test_input_exceptions(self):
with self.assertRaises(ValueError):
self.s.cell_system.get_pairs(0.1, types=3)
# check no exception for list of length 1
self.s.cell_system.get_pairs(0.1, types=[3])

def check_range_exception(self):
with self.assertRaises(Exception):
self.s.cell_system.get_pairs(3.)

Expand All @@ -103,13 +109,13 @@ def test_dd(self):
self.s.cell_system.set_domain_decomposition()
self.s.periodicity = [1, 1, 1]
self.run_and_check()
self.check_exception()
self.check_range_exception()

def test_dd_partial_z(self):
self.s.cell_system.set_domain_decomposition()
self.s.periodicity = [1, 1, 0]
self.run_and_check()
self.check_exception()
self.check_range_exception()


if __name__ == "__main__":
Expand Down

0 comments on commit 8ee78e2

Please sign in to comment.