Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/somd2/runner/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from contextlib import contextmanager as _contextmanager
from glob import glob as _glob
from math import isclose as _isclose
from pathlib import Path as _Path
from shutil import copyfile as _copyfile

Expand Down Expand Up @@ -107,6 +108,26 @@ def __init__(self, system, config):
_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

Expand Down Expand Up @@ -1067,6 +1088,17 @@ def __init__(self, system, config):
else:
self._checkpoint_sire_threads = sire_threads

@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]

@_contextmanager
def _checkpoint_threads(self):
"""
Expand Down
48 changes: 48 additions & 0 deletions tests/runner/test_repex.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pathlib import Path

import numpy as np
import platform
import pytest
import tempfile

Expand Down Expand Up @@ -1183,3 +1184,50 @@ def test_repex_perturbed_system_seeding(ethane_methanol, swap_end_states):
f"group {group} has {len(wrong)} replicas seeded from the "
f"wrong end state: {wrong}"
)


@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.

The runner is created on the CPU platform, so a system that passes the box
check is instead rejected later on, since GCMC needs a GPU.
"""
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,
platform="cpu",
)

match = (
"periodic box of 'perturbed_system'"
if raises
else "GCMC simulations require the CUDA or OpenCL platform"
)
with pytest.raises(ValueError, match=match):
RunnerBase(ethane_methanol, config)