Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent insertion of particles with negative mass #4679

Merged
merged 1 commit into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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