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

fixed system.part.pairs() #4628

Merged
merged 2 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()