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..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 @@ -153,6 +164,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() + 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 " + 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..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 @@ -2675,3 +2676,48 @@ def expected_times(num_hot, num_cold, r): [runner._filenames["topology0"], runner._filenames[i]["trajectory"]] ) assert mols.num_frames() == 6 + + +@pytest.mark.skipif( + platform.system() == "Darwin", reason="GCMC is not supported on macOS." +) +@pytest.mark.parametrize( + "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, dimensions, raises): + """ + Validate that a perturbed system with a different box is rejected for GCMC + 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() + perturbed.set_space(sr.vol.PeriodicBox(sr.maths.Vector(*dimensions))) + + 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()