From 9cba9590eccaf5fc70ac712e164a7793b0674a9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-No=C3=ABl=20Grad?= Date: Wed, 1 Mar 2023 21:15:44 +0100 Subject: [PATCH] script_interface: Forbid particles with negative mass --- .../particle_data/ParticleHandle.cpp | 3 +++ testsuite/python/particle.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/script_interface/particle_data/ParticleHandle.cpp b/src/script_interface/particle_data/ParticleHandle.cpp index 4de2ff356a0..02e8af3badd 100644 --- a/src/script_interface/particle_data/ParticleHandle.cpp +++ b/src/script_interface/particle_data/ParticleHandle.cpp @@ -194,6 +194,9 @@ ParticleHandle::ParticleHandle() { {"mass", #ifdef MASS [this](Variant const &value) { + if (get_value(value) <= 0.) { + throw std::domain_error(error_msg("mass", "must be a float > 0")); + } set_particle_property(&Particle::mass, value); }, #else // MASS diff --git a/testsuite/python/particle.py b/testsuite/python/particle.py index b547f647823..54daee4baf8 100644 --- a/testsuite/python/particle.py +++ b/testsuite/python/particle.py @@ -376,6 +376,24 @@ def test_invalid_particle_attributes(self): p.swimming = {"v_swim": 0.3, "f_swim": 0.6} with self.assertRaisesRegex(ValueError, err_msg.format("swimming.mode", "has to be either 'pusher', 'puller' or 'N/A'")): p.swimming = {"v_swim": 0.3, "mode": "invalid"} + if espressomd.has_features("MASS"): + for mass in [0., -1., -2.]: + with self.assertRaisesRegex(ValueError, err_msg.format("mass", "must be a float > 0")): + p.mass = mass + + def test_missing_features(self): + def check(feature, prop, throwing_values, valid_value=None): + if not espressomd.has_features(feature): + if valid_value is not None: + # this should not throw + setattr(self.partcl, prop, valid_value) + for throwing_value in throwing_values: + with self.assertRaisesRegex(RuntimeError, f"Feature {feature} not compiled in"): + setattr(self.partcl, prop, throwing_value) + + check("MASS", "mass", [1.1, 0., -1.], 1.) + check("ELECTROSTATICS", "q", [1., -1.], 0.) + check("VIRTUAL_SITES", "virtual", [True], False) def test_parallel_property_setters(self): system = self.system