Skip to content

Commit

Permalink
script_interface: Forbid particles with negative mass
Browse files Browse the repository at this point in the history
  • Loading branch information
jngrad committed Mar 1, 2023
1 parent e031659 commit 9cba959
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/script_interface/particle_data/ParticleHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ ParticleHandle::ParticleHandle() {
{"mass",
#ifdef MASS
[this](Variant const &value) {
if (get_value<double>(value) <= 0.) {
throw std::domain_error(error_msg("mass", "must be a float > 0"));
}
set_particle_property(&Particle::mass, value);
},
#else // MASS
Expand Down
18 changes: 18 additions & 0 deletions testsuite/python/particle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9cba959

Please sign in to comment.