From 985454ea04e450d86bdb60b60875c6fc9f263fd6 Mon Sep 17 00:00:00 2001 From: Rudolf Weeber Date: Mon, 15 Jun 2026 12:25:18 +0200 Subject: [PATCH] magnetostatics: require 'prefactor' for DipolarP3M (bug-sweep #55) DipolarP3M.required_keys() returned only {"accuracy"}, so omitting the prefactor silently passed the Python required-keys check. default_params() then injected prefactor=0.0, the base float type check accepted it, and the value reached the core, which threw the misleading ValueError "Parameter 'prefactor' must be > 0" instead of the clean, documented RuntimeError "Parameter 'prefactor' is missing". Add 'prefactor' to DipolarP3M.required_keys() so the missing-key contract is enforced at the Python layer before the placeholder default is applied. This mirrors the established convention of electrostatics P3M (required_keys() -> {"prefactor", "accuracy"}) and the sibling DipolarDirectSum (required_keys() -> {"prefactor"}). Extend test_exceptions_p3m in testsuite/python/dipolar_interface.py to assert that constructing DipolarP3M without a prefactor raises the clean RuntimeError "Parameter 'prefactor' is missing". Co-Authored-By: Claude Opus 4.8 --- src/python/espressomd/magnetostatics.py | 2 +- testsuite/python/dipolar_interface.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/python/espressomd/magnetostatics.py b/src/python/espressomd/magnetostatics.py index bfe30d2e3c..a43e19a84d 100644 --- a/src/python/espressomd/magnetostatics.py +++ b/src/python/espressomd/magnetostatics.py @@ -160,7 +160,7 @@ def validate_params(self, params): raise TypeError("Parameter 'tune' has to be a boolean") def required_keys(self): - return {"accuracy"} + return {"prefactor", "accuracy"} def default_params(self): return {"cao": -1, diff --git a/testsuite/python/dipolar_interface.py b/testsuite/python/dipolar_interface.py index 22cc14e905..636fd6c95b 100644 --- a/testsuite/python/dipolar_interface.py +++ b/testsuite/python/dipolar_interface.py @@ -128,6 +128,10 @@ def test_exceptions_p3m(self): MDLC = espressomd.magnetostatics.DLC dp3m_params = dict(prefactor=1., epsilon=0.1, accuracy=1e-6, mesh=[49, 49, 49], cao=7, r_cut=4.5, alpha=0.9) + with self.assertRaisesRegex(RuntimeError, "Parameter 'prefactor' is missing"): + espressomd.magnetostatics.DipolarP3M( + **{key: value for key, value in dp3m_params.items() + if key != 'prefactor'}) with self.assertRaisesRegex(ValueError, "Parameter 'prefactor' must be > 0"): espressomd.magnetostatics.DipolarP3M( **{**dp3m_params, 'prefactor': -2.})