Skip to content

Commit

Permalink
fixed system.part.pairs() (#4628)
Browse files Browse the repository at this point in the history
Fixes #4622

Description of changes:
- the `System.part.pairs()` method now returns the correct particle pairs when particle ids aren't both contiguous and starting from 0
  • Loading branch information
kodiakhq[bot] authored Dec 12, 2022
2 parents 54a0c59 + d15ae58 commit 466d54b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/python/espressomd/particle_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from .utils import check_type_or_throw_except
from .code_features import assert_features, has_features
from .script_interface import script_interface_register, ScriptInterfaceHelper
import itertools


@script_interface_register
Expand Down Expand Up @@ -1176,10 +1177,9 @@ def pairs(self):
"""

ids = self.call_method("get_particle_ids")

for i in ids:
for j in ids[i + 1:]:
yield (self.by_id(i), self.by_id(j))
id_pairs = itertools.combinations(ids, 2)
for id_pair in id_pairs:
yield (self.by_id(id_pair[0]), self.by_id(id_pair[1]))

def select(self, *args, **kwargs):
"""
Expand Down
17 changes: 17 additions & 0 deletions testsuite/python/pairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,23 @@ def test_dd_partial_z(self):
self.run_and_check()
self.check_range_exception()

def test_non_consecutive(self):
self.system.part.clear()
self.system.part.add(id=100, pos=(0.1, 0.5, 0.5))
self.system.part.add(id=200, pos=(0.2, 0.5, 0.5))
self.system.part.add(id=1, pos=(0.3, 0.5, 0.5))
self.system.part.add(id=2, pos=(0.4, 0.5, 0.5))

self.system.integrator.run(0)

pairs = self.system.part.pairs()
pairs_ids = []
for pair in pairs:
pairs_ids.append(tuple(sorted([pair[0].id, pair[1].id])))
expected_pairs = [(1, 2), (1, 100), (2, 200),
(100, 200), (2, 100), (1, 200)]
self.assertSetEqual(set(pairs_ids), set(expected_pairs))


if __name__ == "__main__":
ut.main()

0 comments on commit 466d54b

Please sign in to comment.