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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------------------------------------------------------------------------------
Expand Down
11 changes: 9 additions & 2 deletions src/loch/_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
39 changes: 39 additions & 0 deletions tests/test_num_waters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

import pytest

from loch import GCMCSampler
Expand Down Expand Up @@ -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())
Loading