From f12716a29f06b11ea732462fd5e2792dd88ae195 Mon Sep 17 00:00:00 2001 From: Lester Hedges Date: Wed, 17 Jul 2024 15:23:14 +0100 Subject: [PATCH] Add test for creation of alchemical ions. --- tests/runner/test_alchemical_ions.py | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tests/runner/test_alchemical_ions.py diff --git a/tests/runner/test_alchemical_ions.py b/tests/runner/test_alchemical_ions.py new file mode 100644 index 0000000..c808406 --- /dev/null +++ b/tests/runner/test_alchemical_ions.py @@ -0,0 +1,31 @@ +import math + +from somd2.runner import Runner + + +def test_alchemical_ions(ethane_methanol): + """Ensure that alchemical ions are added correctly.""" + + # Clone the system. + mols = ethane_methanol.clone() + + # Helper function to return the charge difference between the end states. + def charge_difference(mols): + from sire import morph + + reference = morph.link_to_reference(mols) + perturbed = morph.link_to_perturbed(mols) + + return (perturbed.charge() - reference.charge()).value() + + # Add 10 Cl- ions. + new_mols = Runner._create_alchemical_ions(mols, 10) + + # Make sure the charge difference is correct. + assert math.isclose(charge_difference(new_mols), -10.0, rel_tol=1e-6) + + # Add 10 Na+ ions. + new_mols = Runner._create_alchemical_ions(mols, -10) + + # Make sure the charge difference is correct. + assert math.isclose(charge_difference(new_mols), 10.0, rel_tol=1e-6)