Skip to content

Commit

Permalink
Restore constraints event handling (espressomd#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 (espressomd#2031), but never actually worked because the PR accidentally removed the event function call during merge conflict resolution
  • Loading branch information
kodiakhq[bot] authored and jngrad committed Dec 5, 2023
1 parent 2711650 commit 5efbf55
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
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.hpp"
#include "constraints.hpp"
#include "cuda_init.hpp"
#include "cuda_interface.hpp"
#include "cuda_utils.hpp"
Expand Down Expand Up @@ -289,6 +290,8 @@ void on_boxl_change(bool skip_method_adaption) {
LBBoundaries::lb_init_boundaries();
#endif
}

Constraints::constraints.on_boxl_change();
}

void on_cell_structure_change() {
Expand Down
8 changes: 7 additions & 1 deletion src/core/grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "grid.hpp"

#include "communication.hpp"
#include "errorhandling.hpp"
#include "event.hpp"
#include "particle_data.hpp"

Expand All @@ -38,6 +39,7 @@
#include <mpi.h>

#include <cmath>
#include <stdexcept>

BoxGeometry box_geo;
LocalBox<double> local_geo;
Expand Down Expand Up @@ -129,7 +131,11 @@ void rescale_boxl(int dir, double d_new) {

void mpi_set_box_length_local(const Utils::Vector3d &length) {
box_geo.set_length(length);
on_boxl_change();
try {
on_boxl_change();
} catch (std::exception const &err) {
runtimeErrorMsg() << err.what();
}
}

REGISTER_CALLBACK(mpi_set_box_length_local)
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 @@ -1068,6 +1071,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(Exception, "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 5efbf55

Please sign in to comment.