diff --git a/src/somd2/runner/_repex.py b/src/somd2/runner/_repex.py index 60e9b63..c37145c 100644 --- a/src/somd2/runner/_repex.py +++ b/src/somd2/runner/_repex.py @@ -1992,6 +1992,13 @@ def _reset_gcmc_sampler(gcmc_sampler, dynamics): dynamics: sire.mol.Dynamics The dynamics object associated with the GCMC sampler. """ + # Ensure the water count is up to date before resetting. If the last + # move was a bulk sampling move, _is_bulk is True and num_waters() + # needs the stored context to recompute _N. Calling it here, while + # the context is still available, clears _is_bulk so that reset() + # can safely null the context. + gcmc_sampler.num_waters() + # Reset the GCMC sampler. This resets the sampling statistics and # clears the associated OpenMM forces. gcmc_sampler.reset() diff --git a/src/somd2/runner/_runner.py b/src/somd2/runner/_runner.py index 7479c59..683e6b9 100644 --- a/src/somd2/runner/_runner.py +++ b/src/somd2/runner/_runner.py @@ -639,6 +639,12 @@ def generate_lam_vals(lambda_base, increment=0.001): # Reset the GCMC sampler. This resets the sampling statistics and clears # the associated OpenMM forces. if gcmc_sampler is not None: + # Ensure the water count is up to date before resetting. If the + # last move was a bulk sampling move, _is_bulk is True and + # num_waters() needs the stored context to recompute _N. Calling + # it here, while the context is still available, clears _is_bulk + # so that reset() can safely null the context. + gcmc_sampler.num_waters() gcmc_sampler.reset() # Bind the GCMC sampler to the dynamics object.