From 914b4bce64117220242ada37df34be479a38cd2b Mon Sep 17 00:00:00 2001 From: Yashas Gandhi Date: Thu, 7 Nov 2024 11:52:21 +0100 Subject: [PATCH] Fix 5008 (#1) * Check for duplicate bond partner IDs * Check for duplicate bond partner IDs --------- Co-authored-by: Yashas Tejaskumar Gandhi --- src/python/espressomd/particle_data.py | 3 +++ testsuite/python/particle.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/python/espressomd/particle_data.py b/src/python/espressomd/particle_data.py index 8b016f3b16..3a17c63e9f 100644 --- a/src/python/espressomd/particle_data.py +++ b/src/python/espressomd/particle_data.py @@ -617,6 +617,9 @@ def add_verified_bond(self, bond): if self.id in bond[1:]: raise Exception( f"Bond partners {bond[1:]} include the particle {self.id} itself") + if len(set(bond[1:])) is not len(bond[1:]): + raise Exception( + f"Cannot add duplicate bond partners {bond[1:]} to particle {self.id}") self.call_method("add_bond", bond_id=bond[0]._bond_id, part_id=bond[1:]) diff --git a/testsuite/python/particle.py b/testsuite/python/particle.py index 76d9710e69..a0db9a35de 100644 --- a/testsuite/python/particle.py +++ b/testsuite/python/particle.py @@ -440,6 +440,7 @@ def test_bonds(self): p1 = self.system.part.by_id(self.pid) p2 = self.system.part.add(pos=p1.pos) + p3 = self.system.part.add(pos=p1.pos) inactive_bond = espressomd.interactions.FeneBond(k=1, d_r_max=2) p2.add_bond([self.f1, p1]) with self.assertRaisesRegex(RuntimeError, "already exists on particle"): @@ -460,6 +461,22 @@ def test_bonds(self): with self.assertRaisesRegex(ValueError, "Bond partners have to be of type integer or ParticleHandle"): p2.delete_bond((self.f1, 'p1')) + active_pair_bond = espressomd.interactions.FeneBond(k=1, d_r_max=2) + self.system.bonded_inter.add(active_pair_bond) + with self.assertRaisesRegex(Exception, r"Bond partners \(17,\) include the particle 17 itself"): + p1.add_bond((active_pair_bond, p1)) + + active_angle_bond = espressomd.interactions.AngleCosine(bend=1, phi0=1) + self.system.bonded_inter.add(active_angle_bond) + with self.assertRaisesRegex(Exception, r"Cannot add duplicate bond partners \(17, 17\) to particle 18"): + p2.add_bond((active_angle_bond, p1, p1)) + + active_dihedral_bond = espressomd.interactions.Dihedral( + bend=1, mult=1, phase=1) + self.system.bonded_inter.add(active_dihedral_bond) + with self.assertRaisesRegex(Exception, r"Cannot add duplicate bond partners \(17, 17, 19\) to particle 18"): + p2.add_bond((active_dihedral_bond, p1, p1, p3)) + def test_zz_remove_all(self): for p in self.system.part.all(): p.remove()