Skip to content

Commit

Permalink
Fix Issue #5008 (#5012)
Browse files Browse the repository at this point in the history
Fixes #5008 

Description of changes:
* Prevent users from adding angle/dihedral bonds with duplicate partners
  • Loading branch information
kodiakhq[bot] authored Nov 7, 2024
2 parents 8ca8f10 + f4fb3c7 commit 64edfdb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/python/espressomd/particle_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:])
Expand Down
7 changes: 4 additions & 3 deletions testsuite/python/mpiio.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ def get_random_mock_particles():
v=np.random.rand(3),
bonds=[])
# Up to 20 bonds; otherwise this test will take ages
for _ in range(random.randint(0, 20)):
while len(p.bonds) < 20:
btype = random.randint(0, nbonds - 1)
# Don't create loops, i.e. exclude "i" itself
p1 = randint_different_from(0, npart, i)
p2 = randint_different_from(0, npart, i)
# Don't add the same bond twice
if (btype, p1, p2) not in p.bonds:

# Don't add the same bond twice and don't use the same particle twice
if p1 != p2 and (btype, p1, p2) not in p.bonds:
p.bonds.append((btype, p1, p2))
parts.append(p)
return parts
Expand Down
17 changes: 17 additions & 0 deletions testsuite/python/particle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand All @@ -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()
Expand Down

0 comments on commit 64edfdb

Please sign in to comment.