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 @@ -10,6 +10,7 @@ Changelog
* Keep an existing ghost residue file on restart rather than clearing it, so that it stays aligned with the trajectory frames written before the restart [#50](https://github.com/OpenBioSim/loch/pull/50).
* Flag virtual sites on perturbable molecules as ghosts when their parent atom is a ghost, so that they use soft-core rather than full Coulomb interactions with the waters [#53](https://github.com/OpenBioSim/loch/pull/53).
* Add an `opencl_platform_index` option so that the OpenCL device is chosen from the same platform as the OpenMM context [#55](https://github.com/OpenBioSim/loch/pull/55).
* 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).

[2026.2.0](https://github.com/openbiosim/loch/compare/2026.1.0...2026.2.0) - Sep 2026
--------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/loch/_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@
if (r2 < cutoff2)
{
// Don't divide by zero.
if (!is_fep && r2 < 1e-6)
if (!is_ghost_atom && r2 < 1e-6)
{
energy_coul[idx] = 1e6;
energy_lj[idx] = 1e6;
Expand Down
83 changes: 49 additions & 34 deletions src/loch/_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1848,40 +1848,7 @@ def move(self, context: _openmm.Context) -> list[int]:
)

# Perform the energy calculation.
self._kernels["energy"](
_np.int32(self._num_points),
_np.int32(self._batch_size),
_np.int32(self._num_atoms),
self._water_positions,
self._energy_coul,
self._energy_lj,
candidates_gpu,
is_deletion_gpu,
_np.int32(self._is_fep),
self._gpu_position,
self._gpu_charge,
self._gpu_sigma,
self._gpu_epsilon,
self._gpu_alpha,
self._gpu_is_ghost_water,
self._gpu_is_ghost_fep,
self._gpu_sigma_water,
self._gpu_epsilon_water,
self._gpu_charge_water,
self._gpu_water_idx,
self._gpu_cell_matrix_inverse,
self._gpu_M,
self._rf_cutoff,
self._rf_kappa,
self._rf_correction,
self._sc_softcore_form,
self._sc_shift_coulomb,
self._sc_shift_delta,
self._sc_taylor_power,
self._sc_beutler_alpha,
block=(self._num_threads, 1, 1),
grid=(self._atom_blocks, self._batch_size, 1),
)
self._compute_energy(candidates_gpu, is_deletion_gpu)

# Transfer pre-computed acceptance randoms to GPU.
randoms_acceptance = self._backend.to_gpu(batch_randoms.acceptance)
Expand Down Expand Up @@ -2188,6 +2155,54 @@ def move(self, context: _openmm.Context) -> list[int]:

return moves

def _compute_energy(self, candidates_gpu: _Any, is_deletion_gpu: _Any) -> None:
"""
Compute the interaction energy of each trial water with the system.

Parameters
----------

candidates_gpu: GPU array
The deletion candidate for each trial.

is_deletion_gpu: GPU array
Whether each trial is a deletion (1) or an insertion (0).
"""
self._kernels["energy"](
_np.int32(self._num_points),
_np.int32(self._batch_size),
_np.int32(self._num_atoms),
self._water_positions,
self._energy_coul,
self._energy_lj,
candidates_gpu,
is_deletion_gpu,
_np.int32(self._is_fep),
self._gpu_position,
self._gpu_charge,
self._gpu_sigma,
self._gpu_epsilon,
self._gpu_alpha,
self._gpu_is_ghost_water,
self._gpu_is_ghost_fep,
self._gpu_sigma_water,
self._gpu_epsilon_water,
self._gpu_charge_water,
self._gpu_water_idx,
self._gpu_cell_matrix_inverse,
self._gpu_M,
self._rf_cutoff,
self._rf_kappa,
self._rf_correction,
self._sc_softcore_form,
self._sc_shift_coulomb,
self._sc_shift_delta,
self._sc_taylor_power,
self._sc_beutler_alpha,
block=(self._num_threads, 1, 1),
grid=(self._atom_blocks, self._batch_size, 1),
)

def bind_dynamics(self, dynamics: _Any) -> None:
"""
Bind the GCMC sampler to a Sire Dynamics object.
Expand Down
73 changes: 73 additions & 0 deletions tests/test_energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,3 +788,76 @@ def potential_energy():
)

assert math.isclose(energy_difference, sampler_energy, abs_tol=1e-2)


@pytest.mark.skipif(
"CUDA_VISIBLE_DEVICES" not in os.environ,
reason="Requires CUDA enabled GPU.",
)
@pytest.mark.parametrize("platform", ["cuda", "opencl"])
def test_overlap_in_alchemical_system(sd12, platform):
"""
Test that a trial water on top of a non-ghost atom in an alchemical system
gets the overlap energy, rather than inf or NaN.
"""

mols, reference = sd12

schedule = sr.cas.LambdaSchedule.standard_morph()

sampler = GCMCSampler(
mols,
cutoff_type="rf",
cutoff="10 A",
reference=reference,
lambda_schedule=schedule,
lambda_value=0.5,
log_level="debug",
ghost_file=None,
log_file=None,
test=True,
platform=platform,
)

d = sampler.system().dynamics(
cutoff_type="rf",
cutoff="10 A",
temperature="298 K",
pressure=None,
constraint="h_bonds",
timestep="2 fs",
schedule=schedule,
lambda_value=0.5,
platform=platform,
)

# A move uploads the atom positions to the GPU.
sampler.move(d.context())

backend = sampler._backend
positions = backend.from_gpu(sampler._gpu_position).reshape(-1, 3)
is_ghost_fep = backend.from_gpu(sampler._gpu_is_ghost_fep).flatten()
is_ghost_water = backend.from_gpu(sampler._gpu_is_ghost_water).flatten()
epsilon = backend.from_gpu(sampler._gpu_epsilon).flatten()

# A non-ghost atom with LJ, so the hard LJ path would diverge.
atom = np.where((is_ghost_fep == 0) & (is_ghost_water == 0) & (epsilon > 0))[0][0]

# Put the oxygen of every trial water on the atom.
water = np.zeros((sampler._num_points, 3), dtype=np.float32)
water[:] = positions[atom]
water[1:, 0] += 1.0
water_positions = np.tile(water.flatten(), sampler._batch_size)
sampler._water_positions = backend.to_gpu(water_positions.astype(np.float32))

zeros = backend.to_gpu(np.zeros(sampler._batch_size, dtype=np.int32))
sampler._compute_energy(zeros, zeros)

num_atoms = sampler._num_atoms
energy_coul = backend.from_gpu(sampler._energy_coul).reshape(-1, num_atoms)
energy_lj = backend.from_gpu(sampler._energy_lj).reshape(-1, num_atoms)

assert energy_coul[0, atom] == pytest.approx(1e6)
assert energy_lj[0, atom] == pytest.approx(1e6)
assert np.isfinite(energy_coul[0]).all()
assert np.isfinite(energy_lj[0]).all()
Loading