diff --git a/src/loch/_kernels.py b/src/loch/_kernels.py index 07fbcb4..eefad7e 100644 --- a/src/loch/_kernels.py +++ b/src/loch/_kernels.py @@ -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; diff --git a/src/loch/_sampler.py b/src/loch/_sampler.py index 39e833b..9c49197 100644 --- a/src/loch/_sampler.py +++ b/src/loch/_sampler.py @@ -1834,40 +1834,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) @@ -2174,6 +2141,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. diff --git a/tests/test_energy.py b/tests/test_energy.py index 4a5ea06..78bcdfe 100644 --- a/tests/test_energy.py +++ b/tests/test_energy.py @@ -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()