diff --git a/CHANGELOG.md b/CHANGELOG.md index bcdfc58..c0592a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ Changelog * Give a trial water that overlaps a non-ghost atom in an alchemical system the overlap energy, rather than inf or NaN [#57](https://github.com/OpenBioSim/loch/pull/57). * Remove support for the osmotic ensemble, which isn't valid for GCMC of solvent water: the number of waters is barely fixed, so it drifts. A barostat is rejected again [#63](https://github.com/OpenBioSim/loch/pull/63). * Added support for Python 3.13 and 3.14, and dropped support for Python 3.10 and 3.11 [#65](https://github.com/OpenBioSim/loch/pull/65). +* Fix `num_waters()` without a context after a region move, which raised an `AttributeError` before any bulk move, and returned the count from before any accepted insertions or deletions [#67](https://github.com/OpenBioSim/loch/pull/67). [2026.2.0](https://github.com/openbiosim/loch/compare/2026.1.0...2026.2.0) - Sep 2026 -------------------------------------------------------------------------------------- diff --git a/src/loch/_sampler.py b/src/loch/_sampler.py index cba746f..f2b4056 100644 --- a/src/loch/_sampler.py +++ b/src/loch/_sampler.py @@ -786,6 +786,9 @@ def __init__( # the count for the volume that move() samples. None when unknown. self._N_region = None + # The context stored by a bulk move, which num_waters() falls back to. + self._openmm_context = None + import sys # Create a logger that writes to stderr and the log file. @@ -2077,9 +2080,9 @@ def move(self, context: _openmm.Context) -> list[int]: if self._is_pme: initial_energy = final_energy - # Return immediately if we're in test mode. + # Stop after the first accepted move if we're in test mode. if self._is_test: - return moves + break # If no moves were accepted at the PME level, then update the # number of attempts by the batch size. else: @@ -2095,6 +2098,10 @@ def move(self, context: _openmm.Context) -> list[int]: if self._reference is not None and self._is_bulk: self._openmm_context = context + # Accepted moves update self._N, so refresh the region count to match. + if not self._is_bulk: + self._N_region = self._N + return moves def _compute_energy(self, candidates_gpu: _Any, is_deletion_gpu: _Any) -> None: diff --git a/tests/test_num_waters.py b/tests/test_num_waters.py index 641d821..ff6e93b 100644 --- a/tests/test_num_waters.py +++ b/tests/test_num_waters.py @@ -1,3 +1,5 @@ +import os + import pytest from loch import GCMCSampler @@ -73,3 +75,40 @@ def test_num_waters_refuses_a_whole_box_count(): with pytest.raises(RuntimeError, match="OpenMM context is not set"): sampler.num_waters() + + +@pytest.mark.skipif( + "CUDA_VISIBLE_DEVICES" not in os.environ, + reason="Requires CUDA enabled GPU.", +) +def test_num_waters_after_a_region_move(bpti): + """ + A sampler with a region must report the stored count after a move without + a context, before any bulk move has stored one. + """ + mols, reference = bpti + + sampler = GCMCSampler( + mols, + reference=reference, + cutoff_type="rf", + cutoff="10 A", + bulk_sampling_probability=0.0, + ghost_file=None, + log_file=None, + test=True, + platform="cuda", + seed=42, + ) + + d = sampler.system().dynamics( + cutoff_type="rf", + cutoff="10 A", + pressure=None, + constraint="h_bonds", + platform="cuda", + ) + + sampler.move(d.context()) + + assert sampler.num_waters() == sampler.num_waters(d.context())