Skip to content

Commit

Permalink
Adds option to scale attraction strength of contrasting group (#66)
Browse files Browse the repository at this point in the history
* Included option to scale attraction strength of contrasting group

* Bug fix

* Added unit test involving contrast scaling
  • Loading branch information
Charlles Abreu authored Mar 15, 2024
1 parent 11dcbbf commit 13ae38b
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions cvpack/attraction_strength.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ class AttractionStrength(openmm.CustomNonbondedForce, BaseCollectiveVariable):
Optionally, one can provide a third atom group :math:`{\bf g}_3` to contrast
the attraction strength between :math:`{\bf g}_1` and :math:`{\bf g}_2` with
that between :math:`{\bf g}_1` and :math:`{\bf g}_3`. In this case, the
collective variable becomes
that between :math:`{\bf g}_1` and :math:`{\bf g}_3`. One can also provide
a scaling factor :math:`\alpha` to balance the contributions of the two
interactions. In this case, the collective variable becomes
.. math::
S_{\rm attr}({\bf r}) = S_{1,2}({\bf r}) - S_{1,3}({\bf r})
S_{\rm attr}({\bf r}) = S_{1,2}({\bf r}) - \alpha S_{1,3}({\bf r})
.. note::
Expand Down Expand Up @@ -152,6 +153,17 @@ class AttractionStrength(openmm.CustomNonbondedForce, BaseCollectiveVariable):
2849.2 dimensionless
>>> print(cv1.getValue(context, 4) - cv2.getValue(context, 4))
2849.2 dimensionless
>>> cv4 = cvpack.AttractionStrength(
... guest, host, forces["NonbondedForce"], water, contrastScaling=0.5
... )
>>> _ = cv4.setUnusedForceGroup(0, model.system)
>>> _ = model.system.addForce(cv4)
>>> context.reinitialize(preserveState=True)
>>> print(cv4.getValue(context, 4))
3880.8 dimensionless
>>> print(1 * cv1.getValue(context, 4) - 0.5 * cv2.getValue(context, 4))
3880.8...
"""

yaml_tag = "!cvpack.AttractionStrength"
Expand All @@ -166,6 +178,7 @@ def __init__( # pylint: disable=too-many-arguments
reference: t.Union[mmunit.ScalarQuantity, openmm.Context] = mmunit.Quantity(
1.0, mmunit.kilojoule_per_mole
),
contrastScaling: float = 1.0,
) -> None:
group1 = list(group1)
group2 = list(group2)
Expand Down Expand Up @@ -194,10 +207,13 @@ def __init__( # pylint: disable=too-many-arguments
for parameter in ("charge", "sigma", "epsilon") + ("sign",) * contrasting:
self.addPerParticleParameter(parameter)
for atom in range(nonbondedForce.getNumParticles()):
parameters = nonbondedForce.getParticleParameters(atom)
charge, sigma, epsilon = nonbondedForce.getParticleParameters(atom)
if contrasting:
parameters += (-1 if atom in contrastGroup else 1,)
self.addParticle(parameters)
sign = -1 if atom in contrastGroup else 1
scale = contrastScaling if atom in contrastGroup else 1
self.addParticle([charge * scale, sigma, epsilon * scale**2, sign])
else:
self.addParticle([charge, sigma, epsilon])
for exception in range(nonbondedForce.getNumExceptions()):
i, j, *_ = nonbondedForce.getExceptionParameters(exception)
self.addExclusion(i, j)
Expand All @@ -218,4 +234,5 @@ def __init__( # pylint: disable=too-many-arguments
nonbondedForce,
contrastGroup,
reference,
contrastScaling,
)

0 comments on commit 13ae38b

Please sign in to comment.