From 3ae3bc039496093a46ac7c8affd92c1d1c0acab8 Mon Sep 17 00:00:00 2001 From: Lester Hedges Date: Wed, 4 Nov 2026 15:11:29 +0000 Subject: [PATCH 1/2] Check that the perturbed system box matches the reference for NVT GCMC. --- CHANGELOG.md | 1 + src/somd2/runner/_base.py | 20 ++++++++++++++++++++ tests/runner/test_repex.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f087f1e8..42727b50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ Changelog * Allow `equilibration_time` to be a list of times, one for each lambda window, e.g. `--equilibration-time 0ps 100ps 100ps 0ps` on the command line. Windows with a time of zero are not equilibrated. A single time is still used for every window [#265](https://github.com/OpenBioSim/somd2/pull/265). * Pass `opencl_platform_index` to the GCMC sampler, so that GCMC runs on the same OpenCL device as the dynamics when there are multiple OpenCL platforms [#266](https://github.com/OpenBioSim/somd2/pull/266). * Fixed energy trajectory times for ABFE bound legs without a Boresch restraint being offset by up to the `restraint_search_time`, since production started from the time of the restraint search frame rather than the input system [#268](https://github.com/OpenBioSim/somd2/pull/268). +* Raise an error when a `perturbed_system` with a different periodic box from the reference system is used for GCMC in the NVT ensemble, since every replica must share the same box. GCMC in the osmotic ensemble, with a pressure set, is unaffected [#271](https://github.com/OpenBioSim/somd2/pull/271). [2026.2.0](https://github.com/openbiosim/somd2/compare/2026.1.0...2026.2.0) - Sep 2026 -------------------------------------------------------------------------------------- diff --git a/src/somd2/runner/_base.py b/src/somd2/runner/_base.py index a3deeacd..f7355662 100644 --- a/src/somd2/runner/_base.py +++ b/src/somd2/runner/_base.py @@ -153,6 +153,26 @@ def _check_inputs(self): _logger.error(msg) raise ValueError(msg) + # GCMC in the NVT ensemble needs every replica to share the same box. + if self._config.gcmc and self._config.pressure is None: + space = self._system.space() + pert_space = self._config.perturbed_system.space() + volume = space.volume().value() + pert_volume = pert_space.volume().value() + if ( + space.what() != pert_space.what() + or abs(pert_volume - volume) > 1e-5 * volume + ): + msg = ( + f"The periodic box of 'perturbed_system' ({pert_space}) does not " + f"match that of the 'system' ({space}). GCMC in the NVT ensemble " + "needs both end states to share the same box. Rescale the perturbed " + "system to the reference box, or set a pressure to run in the " + "osmotic ensemble." + ) + _logger.error(msg) + raise ValueError(msg) + # Log the versions of somd2 and its OpenBioSim dependencies. from somd2 import get_versions as _get_versions diff --git a/tests/runner/test_repex.py b/tests/runner/test_repex.py index 0fc7227d..6b63bc7b 100644 --- a/tests/runner/test_repex.py +++ b/tests/runner/test_repex.py @@ -2675,3 +2675,36 @@ def expected_times(num_hot, num_cold, r): [runner._filenames["topology0"], runner._filenames[i]["trajectory"]] ) assert mols.num_frames() == 6 + + +@pytest.mark.parametrize( + "pressure, scale, raises", + [("none", 1.01, True), ("none", 1.0, False), ("1 atm", 1.01, False)], +) +def test_perturbed_system_box_gcmc(ethane_methanol, pressure, scale, raises): + """ + Validate that a perturbed system with a different box is rejected for GCMC + in the NVT ensemble, but allowed in the osmotic ensemble. + """ + perturbed = ethane_methanol.clone() + space = ethane_methanol.space() + perturbed.set_space(space.set_volume(scale * space.volume())) + + with tempfile.TemporaryDirectory() as tmpdir: + config = Config( + output_directory=tmpdir, + replica_exchange=True, + perturbed_system=perturbed, + gcmc=True, + pressure=pressure, + ) + + runner = RunnerBase.__new__(RunnerBase) + runner._system = ethane_methanol + runner._config = config + + if raises: + with pytest.raises(ValueError, match="periodic box of 'perturbed_system'"): + runner._check_inputs() + else: + runner._check_inputs() From 639f83a33580fda2e87aec80ac20360f9203ecb6 Mon Sep 17 00:00:00 2001 From: Lester Hedges Date: Wed, 4 Nov 2026 15:15:06 +0000 Subject: [PATCH 2/2] Compare box vectors and skip the GCMC box test on macOS. --- src/somd2/runner/_base.py | 21 ++++++++++++++++----- tests/runner/test_repex.py | 25 +++++++++++++++++++------ 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/somd2/runner/_base.py b/src/somd2/runner/_base.py index f7355662..66a3cfbc 100644 --- a/src/somd2/runner/_base.py +++ b/src/somd2/runner/_base.py @@ -135,6 +135,17 @@ def _set_config(self, config): # blocks only exist to update the bar. _sr.base.ProgressBar.set_silent() + @staticmethod + def _box_vectors(space): + """ + Return the box vector components of a periodic space, in Angstrom. + """ + if hasattr(space, "vector0"): + vectors = [space.vector0(), space.vector1(), space.vector2()] + else: + vectors = [space.dimensions()] + return [x.value() for vector in vectors for x in vector] + def _check_inputs(self): """ Check the perturbed system and the frame frequency, log the versions of @@ -157,11 +168,11 @@ def _check_inputs(self): if self._config.gcmc and self._config.pressure is None: space = self._system.space() pert_space = self._config.perturbed_system.space() - volume = space.volume().value() - pert_volume = pert_space.volume().value() - if ( - space.what() != pert_space.what() - or abs(pert_volume - volume) > 1e-5 * volume + vectors = self._box_vectors(space) + pert_vectors = self._box_vectors(pert_space) + if space.what() != pert_space.what() or not all( + _isclose(a, b, rel_tol=1e-5, abs_tol=1e-5) + for a, b in zip(vectors, pert_vectors) ): msg = ( f"The periodic box of 'perturbed_system' ({pert_space}) does not " diff --git a/tests/runner/test_repex.py b/tests/runner/test_repex.py index 6b63bc7b..5d712a46 100644 --- a/tests/runner/test_repex.py +++ b/tests/runner/test_repex.py @@ -1,6 +1,7 @@ from pathlib import Path import numpy as np +import platform import pytest import tempfile @@ -2677,18 +2678,30 @@ def expected_times(num_hot, num_cold, r): assert mols.num_frames() == 6 +@pytest.mark.skipif( + platform.system() == "Darwin", reason="GCMC is not supported on macOS." +) @pytest.mark.parametrize( - "pressure, scale, raises", - [("none", 1.01, True), ("none", 1.0, False), ("1 atm", 1.01, False)], + "pressure, dimensions, raises", + [ + ("none", (50, 50, 50), False), + ("none", (50.5, 50.5, 50.5), True), + ("none", (40, 50, 62.5), True), + ("1 atm", (50.5, 50.5, 50.5), False), + ], ) -def test_perturbed_system_box_gcmc(ethane_methanol, pressure, scale, raises): +def test_perturbed_system_box_gcmc(ethane_methanol, pressure, dimensions, raises): """ Validate that a perturbed system with a different box is rejected for GCMC - in the NVT ensemble, but allowed in the osmotic ensemble. + in the NVT ensemble, including a box of the same volume but a different + shape, but allowed in the osmotic ensemble. """ + import sire as sr + + assert [x.value() for x in ethane_methanol.space().dimensions()] == [50, 50, 50] + perturbed = ethane_methanol.clone() - space = ethane_methanol.space() - perturbed.set_space(space.set_volume(scale * space.volume())) + perturbed.set_space(sr.vol.PeriodicBox(sr.maths.Vector(*dimensions))) with tempfile.TemporaryDirectory() as tmpdir: config = Config(