From b468c5ffa3193f31ca5f1545a5d945da6056a893 Mon Sep 17 00:00:00 2001 From: Lester Hedges Date: Wed, 2 Dec 2026 14:30:22 +0000 Subject: [PATCH] Backport fix from PR #557. [ci skip] --- tests/convert/test_openmm.py | 19 +++++++++++++++++++ wrapper/Convert/SireOpenMM/_sommcontext.py | 3 ++- wrapper/Convert/__init__.py | 17 ++++++++++------- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/tests/convert/test_openmm.py b/tests/convert/test_openmm.py index 05ce770a6..98113b067 100644 --- a/tests/convert/test_openmm.py +++ b/tests/convert/test_openmm.py @@ -306,6 +306,25 @@ def test_openmm_options(ala_mols, openmm_platform): pass +@pytest.mark.skipif( + "openmm" not in sr.convert.supported_formats(), + reason="openmm support is not available", +) +def test_openmm_platform_defaults_unchanged(ala_mols): + import openmm + + mol = ala_mols[0] + + platform = openmm.Platform.getPlatformByName("CPU") + default = platform.getPropertyDefaultValue("Threads") + threads = "1" if default != "1" else "2" + + omm = sr.convert.to(mol, "openmm", map={"platform": "CPU", "threads": threads}) + + assert omm.getPlatform().getPropertyValue(omm, "Threads") == threads + assert platform.getPropertyDefaultValue("Threads") == default + + @pytest.mark.skipif( "openmm" not in sr.convert.supported_formats(), reason="openmm support is not available", diff --git a/wrapper/Convert/SireOpenMM/_sommcontext.py b/wrapper/Convert/SireOpenMM/_sommcontext.py index 54289232c..b9da9fca2 100644 --- a/wrapper/Convert/SireOpenMM/_sommcontext.py +++ b/wrapper/Convert/SireOpenMM/_sommcontext.py @@ -24,6 +24,7 @@ def __init__( platform=None, metadata=None, map=None, + properties=None, ): """ Construct from a passed OpenMM Context, the @@ -64,7 +65,7 @@ def __init__( # more than worth it. Note that this is why minimisations # start running more quickly than dynamics jobs. There # are no constraints in minimisations - super().__init__(system, integrator, platform) + super().__init__(system, integrator, platform, properties or {}) # place the coordinates and velocities into the context set_openmm_coordinates_and_velocities(self, metadata) diff --git a/wrapper/Convert/__init__.py b/wrapper/Convert/__init__.py index d34cedee0..b72f7e54f 100644 --- a/wrapper/Convert/__init__.py +++ b/wrapper/Convert/__init__.py @@ -448,9 +448,13 @@ def sire_to_openmm(mols, map): supported_properties = platform.getPropertyNames() + # Passed to the context, since the platform's defaults are shared between + # threads. + properties = {} + if "Precision" in supported_properties and map.specified("precision"): precision = map.get_string("precision") - platform.setPropertyDefaultValue("Precision", precision) + properties["Precision"] = precision if "Threads" in supported_properties and map.specified("threads"): try: @@ -458,7 +462,7 @@ def sire_to_openmm(mols, map): except Exception: threads = map["threads"].source() - platform.setPropertyDefaultValue("Threads", str(threads)) + properties["Threads"] = str(threads) if "DeviceIndex" in supported_properties and map.specified("device"): try: @@ -466,11 +470,11 @@ def sire_to_openmm(mols, map): except Exception: device_index = map["device"].source() - platform.setPropertyDefaultValue("DeviceIndex", str(device_index)) + properties["DeviceIndex"] = str(device_index) if map.specified("cpu_pme") and "UseCpuPme" in supported_properties: usecpu = int(map["cpu_pme"].value().as_boolean()) - platform.setPropertyDefaultValue("UseCpuPme", str(usecpu).lower()) + properties["UseCpuPme"] = str(usecpu).lower() if "OpenCLPlatformIndex" in supported_properties and map.specified( "opencl_platform_index" @@ -482,9 +486,7 @@ def sire_to_openmm(mols, map): except Exception: opencl_platform_index = map["opencl_platform_index"].source() - platform.setPropertyDefaultValue( - "OpenCLPlatformIndex", str(opencl_platform_index) - ) + properties["OpenCLPlatformIndex"] = str(opencl_platform_index) try: from ._sommcontext import SOMMContext @@ -493,6 +495,7 @@ def sire_to_openmm(mols, map): system=system, integrator=integrator, platform=platform, + properties=properties, metadata=openmm_metadata, map=map, )