Skip to content

Commit 65606fa

Browse files
authored
Merge pull request #18 from OpenBioSim/feature_lrc
2 parents 474a24c + fcd8e96 commit 65606fa

6 files changed

Lines changed: 137 additions & 35 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Changelog
66

77
* Add support for getting and restoring sampling statistics.
88
* Handle XED force field virtual sites.
9+
* Add support for long-range Lennard-Jones dispersion correction.
10+
* Add support for Beutler soft-core Lennard-Jones form.
911

1012
[2025.2.0](https://github.com/openbiosim/loch/compare/2025.1.0...2025.2.0) - Feb 2026
1113
-------------------------------------------------------------------------------------

‎src/loch/_kernels.py‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -395,10 +395,10 @@
395395
float rf_kappa,
396396
float rf_correction,
397397
int softcore_form,
398-
float sc_coulomb_power,
399398
float sc_shift_coulomb,
400399
float sc_shift_delta,
401-
int sc_taylor_power)
400+
int sc_taylor_power,
401+
float sc_beutler_alpha)
402402
{
403403
// Work out the atom index.
404404
const int idx_atom = GET_GLOBAL_ID(0);
@@ -561,6 +561,7 @@
561561
562562
// Compute the LJ interaction using the chosen soft-core form.
563563
float sig6;
564+
float lj_prefactor = 1.0f;
564565
if (softcore_form == 1)
565566
{
566567
// Taylor soft-core LJ:
@@ -570,6 +571,14 @@
570571
: powf(a, (float)sc_taylor_power);
571572
sig6 = s6_val / (alpha_m * s6_val + r6);
572573
}
574+
else if (softcore_form == 2)
575+
{
576+
// Beutler soft-core LJ:
577+
// sig6 = sigma^6 / (sc_beutler_alpha * sigma^6 * alpha + r^6)
578+
// V_LJ = (1 - alpha) * 4 * epsilon * sig6 * (sig6 - 1)
579+
sig6 = s6_val / (sc_beutler_alpha * s6_val * a + r6);
580+
lj_prefactor = 1.0f - a;
581+
}
573582
else
574583
{
575584
// Zacharias soft-core LJ:
@@ -579,22 +588,11 @@
579588
const float denom = (s * delta_lj) + r2_sc;
580589
sig6 = s6_val / (denom * denom * denom);
581590
}
582-
energy_lj[idx] += 4.0f * e * sig6 * (sig6 - 1.0f);
583-
584-
// Compute the Coulomb power expression.
585-
float cpe;
586-
if (sc_coulomb_power == 0.0f)
587-
{
588-
cpe = 1.0f;
589-
}
590-
else
591-
{
592-
cpe = powf((1.0f - a), sc_coulomb_power);
593-
}
591+
energy_lj[idx] += lj_prefactor * 4.0f * e * sig6 * (sig6 - 1.0f);
594592
595593
// Compute the Coulomb interaction.
596594
energy_coul[idx] += (q0 * q1) *
597-
((cpe / sqrtf((sc_shift_coulomb * sc_shift_coulomb * a)
595+
((1.0f / sqrtf((sc_shift_coulomb * sc_shift_coulomb * a)
598596
+ r2_sc)) + (rf_kappa * r2) - rf_correction);
599597
600598
}

‎src/loch/_sampler.py‎

Lines changed: 95 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ def __init__(
7979
lambda_value: float = 0.0,
8080
rest2_scale: float = 1.0,
8181
rest2_selection: _Optional[str] = None,
82-
coulomb_power: float = 0.0,
8382
shift_coulomb: str = "1 A",
8483
shift_delta: str = "1.5 A",
8584
softcore_form: str = "zacharias",
8685
taylor_power: int = 1,
86+
beutler_alpha: float = 0.5,
8787
swap_end_states: bool = False,
8888
restart: bool = False,
8989
overwrite: bool = False,
@@ -212,15 +212,20 @@ def __init__(
212212
213213
softcore_form : str
214214
The soft-core potential form to use for alchemical interactions.
215-
This can be either 'zacharias' or 'taylor'. The default is
216-
'zacharias'.
215+
Valid options are 'zacharias' (default), 'taylor', and 'beutler'.
216+
The Beutler form is recommended for ABFE calculations.
217217
218218
taylor_power : int
219219
The power to use for the alpha term in the Taylor soft-core LJ
220220
expression, i.e. sig6 = sigma^6 / (alpha^m * sigma^6 + r^6).
221221
Must be between 0 and 4. The default is 1. Only used when
222222
softcore_form is 'taylor'.
223223
224+
beutler_alpha : float
225+
The dimensionless scale factor for the r^6 shift in the Beutler
226+
soft-core form. Must be >= 0. The default is 0.5. Only used when
227+
softcore_form is 'beutler'.
228+
224229
swap_end_states: bool
225230
Whether to swap the end states of the alchemical systems.
226231
@@ -287,6 +292,11 @@ def __init__(
287292
else:
288293
self._is_pme = False
289294

295+
# LRC state: initialised lazily on first move() call.
296+
self._has_gcmc_lrc = False
297+
self._lrc_w_solute = 0.0
298+
self._lrc_ww_half = 0.0
299+
290300
try:
291301
self._radius = self._validate_sire_unit("radius", radius, _sr.u("A"))
292302
except Exception as e:
@@ -489,12 +499,6 @@ def __init__(
489499
)
490500
self._rest2_selection = rest2_selection
491501

492-
try:
493-
coulomb_power = float(coulomb_power)
494-
except Exception:
495-
raise ValueError("'coulomb_power' must be of type 'float'")
496-
self._coulomb_power = float(coulomb_power)
497-
498502
try:
499503
self._shift_coulomb = self._validate_sire_unit(
500504
"shift_coulomb", shift_coulomb, _sr.u("A")
@@ -529,10 +533,21 @@ def __init__(
529533
raise ValueError("'taylor_power' must be between 0 and 4")
530534
self._taylor_power = taylor_power
531535

536+
try:
537+
beutler_alpha = float(beutler_alpha)
538+
except Exception:
539+
raise ValueError("'beutler_alpha' must be of type 'float'")
540+
if beutler_alpha < 0.0:
541+
raise ValueError("'beutler_alpha' must be >= 0")
542+
self._beutler_alpha = beutler_alpha
543+
532544
if not isinstance(swap_end_states, bool):
533545
raise ValueError("'swap_end_states' must be of type 'bool'")
534546
self._swap_end_states = swap_end_states
535547

548+
if swap_end_states and self._lambda_schedule is not None:
549+
self._lambda_schedule = self._lambda_schedule.reverse()
550+
536551
# Check for waters and validate the template.
537552
try:
538553
self._water_template = system["water and not property is_perturbable"][0]
@@ -775,7 +790,6 @@ def __str__(self) -> str:
775790
f"restart={self._restart}, "
776791
f"rest2_scale={self._rest2_scale}, "
777792
f"rest2_selection={self._rest2_selection}, "
778-
f"coulomb_power={self._coulomb_power}, "
779793
f"shift_coulomb={self._shift_coulomb}, "
780794
f"shift_delta={self._shift_delta}, "
781795
f"overwrite={self._overwrite}, "
@@ -1273,6 +1287,10 @@ def move(self, context: _openmm.Context) -> list[int]:
12731287
# We only need to get the positions and initial energy for the first
12741288
# batch. These will be updated dynamically as moves are accepted.
12751289
if num_batches == 1:
1290+
# Detect GCMC LRC parameters from context on first call.
1291+
if not self._has_gcmc_lrc:
1292+
self._init_gcmc_lrc(context)
1293+
12761294
# Get the OpenMM state.
12771295
state = context.getState(getPositions=True, getEnergy=self._is_pme)
12781296

@@ -1286,6 +1304,11 @@ def move(self, context: _openmm.Context) -> list[int]:
12861304
else:
12871305
initial_energy = None
12881306

1307+
# Cache the box volume (NVT, so constant throughout).
1308+
if self._has_gcmc_lrc:
1309+
box = state.getPeriodicBoxVectors(asNumpy=True)
1310+
v_nm3 = _np.linalg.det(box / _openmm.unit.nanometer)
1311+
12891312
# Sample within the GCMC sphere.
12901313
if self._reference is not None and not self._is_bulk:
12911314
target = self._get_target_position(positions_angstrom).astype(
@@ -1442,10 +1465,10 @@ def move(self, context: _openmm.Context) -> list[int]:
14421465
self._rf_kappa,
14431466
self._rf_correction,
14441467
self._sc_softcore_form,
1445-
self._sc_coulomb_power,
14461468
self._sc_shift_coulomb,
14471469
self._sc_shift_delta,
14481470
self._sc_taylor_power,
1471+
self._sc_beutler_alpha,
14491472
block=(self._num_threads, 1, 1),
14501473
grid=(self._atom_blocks, self._batch_size, 1),
14511474
)
@@ -1521,6 +1544,10 @@ def move(self, context: _openmm.Context) -> list[int]:
15211544

15221545
# Insertion move.
15231546
if is_deletion[idx] == 0:
1547+
# Capture n_w before the insertion for LRC delta calculation.
1548+
if self._has_gcmc_lrc:
1549+
n_w_before_insert = context.getParameter("n_w")
1550+
15241551
# Accept the move.
15251552
self._accept_insertion(
15261553
idx, idx_water, positions_openmm, positions_angstrom, context
@@ -1548,6 +1575,19 @@ def move(self, context: _openmm.Context) -> list[int]:
15481575
getEnergy=True
15491576
).getPotentialEnergy()
15501577

1578+
# Add the analytic LRC delta so the PME correction sees only
1579+
# the RF→PME electrostatic difference.
1580+
if self._has_gcmc_lrc:
1581+
dLRC = (
1582+
(
1583+
self._lrc_w_solute
1584+
+ 2.0 * n_w_before_insert * self._lrc_ww_half
1585+
)
1586+
/ v_nm3
1587+
* _openmm.unit.kilojoules_per_mole
1588+
)
1589+
dE_RF += dLRC
1590+
15511591
# Compute the PME acceptance correction.
15521592
acc_prob = _np.exp(
15531593
min(
@@ -1608,6 +1648,10 @@ def move(self, context: _openmm.Context) -> list[int]:
16081648

16091649
# Deletion move.
16101650
else:
1651+
# Capture n_w before the deletion for LRC delta calculation.
1652+
if self._has_gcmc_lrc:
1653+
n_w_before_delete = context.getParameter("n_w")
1654+
16111655
# Accept the move.
16121656
self._accept_deletion(candidates[idx], context)
16131657

@@ -1633,6 +1677,20 @@ def move(self, context: _openmm.Context) -> list[int]:
16331677
getEnergy=True
16341678
).getPotentialEnergy()
16351679

1680+
# Add the analytic LRC delta.
1681+
if self._has_gcmc_lrc:
1682+
dLRC = (
1683+
-(
1684+
self._lrc_w_solute
1685+
+ 2.0
1686+
* (n_w_before_delete - 1.0)
1687+
* self._lrc_ww_half
1688+
)
1689+
/ v_nm3
1690+
* _openmm.unit.kilojoules_per_mole
1691+
)
1692+
dE_RF += dLRC
1693+
16361694
# Compute the PME acceptance correction.
16371695
acc_prob = _np.exp(
16381696
min(
@@ -2106,6 +2164,9 @@ def _initialise_gpu_memory(self):
21062164
if self._softcore_form == _SoftcoreForm.TAYLOR:
21072165
_map["use_taylor_softening"] = True
21082166
_map["taylor_power"] = self._taylor_power
2167+
elif self._softcore_form == _SoftcoreForm.BEUTLER:
2168+
_map["use_beutler_softening"] = True
2169+
_map["beutler_alpha"] = self._beutler_alpha
21092170

21102171
# Create a dynamics object.
21112172
d = mols.dynamics(
@@ -2124,7 +2185,7 @@ def _initialise_gpu_memory(self):
21242185
map=_map,
21252186
)
21262187

2127-
# Flags for the required force.
2188+
# Flag for the required force.
21282189
has_gng = False
21292190

21302191
# Find the required forces.
@@ -2268,10 +2329,10 @@ def _initialise_gpu_memory(self):
22682329

22692330
# Store soft-core parameters as scalars.
22702331
self._sc_softcore_form = _np.int32(int(self._softcore_form))
2271-
self._sc_coulomb_power = _np.float32(self._coulomb_power)
22722332
self._sc_shift_coulomb = _np.float32(self._shift_coulomb.value())
22732333
self._sc_shift_delta = _np.float32(self._shift_delta.value())
22742334
self._sc_taylor_power = _np.int32(self._taylor_power)
2335+
self._sc_beutler_alpha = _np.float32(self._beutler_alpha)
22752336

22762337
# Store immutable per-atom buffers on GPU.
22772338
self._gpu_sigma = sigmas
@@ -2321,6 +2382,15 @@ def _initialise_gpu_memory(self):
23212382
(1, self._num_waters), _np.int32
23222383
)
23232384

2385+
def _init_gcmc_lrc(self, context):
2386+
"""Detect and cache GCMC LRC parameters from the OpenMM context."""
2387+
try:
2388+
self._lrc_w_solute = context.getParameter("lrc_w_solute")
2389+
self._lrc_ww_half = context.getParameter("lrc_ww_half")
2390+
self._has_gcmc_lrc = True
2391+
except Exception:
2392+
self._has_gcmc_lrc = False
2393+
23242394
def _accept_insertion(
23252395
self, idx, idx_water, positions_openmm, positions_angstrom, context
23262396
):
@@ -2417,6 +2487,10 @@ def _accept_insertion(
24172487
# Update the number of waters in the sampling volume.
24182488
self._N += 1
24192489

2490+
# Update the GCMC LRC water count in the context.
2491+
if self._has_gcmc_lrc:
2492+
context.setParameter("n_w", context.getParameter("n_w") + 1.0)
2493+
24202494
def _accept_deletion(self, idx, context):
24212495
"""
24222496
Accept a deletion move.
@@ -2487,6 +2561,10 @@ def _accept_deletion(self, idx, context):
24872561
# Update the number of waters in the sampling volume.
24882562
self._N -= 1
24892563

2564+
# Update the GCMC LRC water count in the context.
2565+
if self._has_gcmc_lrc:
2566+
context.setParameter("n_w", context.getParameter("n_w") - 1.0)
2567+
24902568
def _reject_deletion(self, idx, context):
24912569
"""
24922570
Reject a deletion move.
@@ -2560,6 +2638,10 @@ def _reject_deletion(self, idx, context):
25602638
# Update the number of waters in the sampling volume.
25612639
self._N += 1
25622640

2641+
# Update the GCMC LRC water count in the context.
2642+
if self._has_gcmc_lrc:
2643+
context.setParameter("n_w", context.getParameter("n_w") + 1.0)
2644+
25632645
def _set_water_state(self, context, indices=None, states=None, force=False):
25642646
"""
25652647
Update the state for a list of waters. This can be used by external

‎src/loch/_softcore.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ class SoftcoreForm(IntEnum):
2929

3030
ZACHARIAS = 0
3131
TAYLOR = 1
32+
BEUTLER = 2

0 commit comments

Comments
 (0)