Skip to content

Commit

Permalink
Restore constraints event handling (#4778)
Browse files Browse the repository at this point in the history
Description of changes:
- Resizing the box now throws a runtime error if there are constraints present, since constraint preconditions might no longer be fulfilled (e.g. a wall constraint might end up outside the box boundaries when the box shrinks)
- This feature was originally introduced in ESPResSo 4.0.0 (#2031), but never actually worked because the PR accidentally removed the event function call during merge conflict resolution
  • Loading branch information
kodiakhq[bot] authored Aug 25, 2023
2 parents 8c88ccf + fc1c08f commit f8e0a66
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/constraints/Constraints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ template <class ParticleRange, class Constraint> class Constraints {
}

void on_boxl_change() const {
if (not this->empty()) {
if (not m_constraints.empty()) {
throw std::runtime_error("The box size can not be changed because there "
"are active constraints.");
}
Expand Down
3 changes: 3 additions & 0 deletions src/core/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "collision.hpp"
#include "communication.hpp"
#include "config/config.hpp"
#include "constraints.hpp"
#include "cuda/init.hpp"
#include "cuda/utils.hpp"
#include "electrostatics/coulomb.hpp"
Expand Down Expand Up @@ -241,6 +242,8 @@ void on_boxl_change(bool skip_method_adaption) {
system.dipoles.on_boxl_change();
#endif
}

Constraints::constraints.on_boxl_change();
}

void on_cell_structure_change() {
Expand Down
14 changes: 14 additions & 0 deletions testsuite/python/constraint_shape_based.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class ShapeBasedConstraintTest(ut.TestCase):
box_l = 30.
system = espressomd.System(box_l=3 * [box_l])

def setUp(self):
self.system.box_l = 3 * [self.box_l]

def tearDown(self):
self.system.part.clear()
self.system.constraints.clear()
Expand Down Expand Up @@ -1067,6 +1070,17 @@ def test_torus(self):
system.non_bonded_inter[0, 1].lennard_jones.set_params(
epsilon=0.0, sigma=0.0, cutoff=0.0, shift=0)

def test_exceptions(self):
system = self.system
wall = espressomd.shapes.Wall(normal=[0., 1., 0.], dist=0.)
constraint = espressomd.constraints.ShapeBasedConstraint(
shape=wall, particle_type=1)
system.constraints.add(constraint)
with self.assertRaisesRegex(RuntimeError, "there are active constraints"):
system.box_l = 0.5 * system.box_l
system.constraints.remove(constraint)
system.box_l = 0.75 * system.box_l


if __name__ == "__main__":
ut.main()
1 change: 1 addition & 0 deletions testsuite/python/integrator_steepest_descent.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def setUp(self):

def tearDown(self):
self.system.part.clear()
self.system.constraints.clear()
self.system.integrator.set_vv()

def test_relaxation_integrator(self):
Expand Down

0 comments on commit f8e0a66

Please sign in to comment.