Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Includes wrapper for converting OpenMM Force objects into CVPack CVs #67

Merged
merged 7 commits into from
Mar 15, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Implemented generic collective variable
craabreu committed Mar 8, 2024
commit 91b29cc3c770f03f17fea05edef0e165c0310e58
2 changes: 2 additions & 0 deletions cvpack/__init__.py
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
from .centroid_function import CentroidFunction # noqa: F401
from .composite_rmsd import CompositeRMSD # noqa: F401
from .distance import Distance # noqa: F401
from .generic import Generic # noqa: F401
from .helix_angle_content import HelixAngleContent # noqa: F401
from .helix_hbond_content import HelixHBondContent # noqa: F401
from .helix_rmsd_content import HelixRMSDContent # noqa: F401
@@ -31,6 +32,7 @@
CentroidFunction,
CompositeRMSD,
Distance,
Generic,
HelixAngleContent,
HelixHBondContent,
HelixRMSDContent,
77 changes: 77 additions & 0 deletions cvpack/generic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""
.. class:: Generic
:platform: Linux, MacOS, Windows
:synopsis: Generic collective variable

.. classauthor:: Charlles Abreu <[email protected]>

"""

import typing as t

import openmm

from cvpack import unit as mmunit

from .cvpack import BaseCollectiveVariable


class Generic(BaseCollectiveVariable):
r"""
A generic collective variable.

Parameters
----------
atom1
The index of the first atom
atom2
The index of the second atom
atom3
The index of the third atom
pbc
Whether to use periodic boundary conditions

Example:
>>> import cvpack
>>> import numpy as np
>>> import openmm
>>> from openmm import unit
>>> from openmmtools import testsystems
>>> model = testsystems.AlanineDipeptideVacuum()
>>> cv1 = cvpack.Angle(0, 1, 2)
>>> model.system.addForce(cv1)
5
>>> angle = openmm.CustomAngleForce("theta")
>>> angle.addAngle(0, 1, 2)
0
>>> cv2 = cvpack.Generic(angle, unit.radian, period=2*np.pi)
>>> model.system.addForce(cv2)
6
>>> integrator = openmm.VerletIntegrator(0)
>>> platform = openmm.Platform.getPlatformByName('Reference')
>>> context = openmm.Context(model.system, integrator, platform)
>>> context.setPositions(model.positions)
>>> print(cv1.getValue(context))
1.911... rad
>>> print(cv2.getValue(context))
1.911... rad
>>> assert isinstance(cv2, openmm.CustomAngleForce)
"""

yaml_tag = "!cvpack.Generic"

def __init__( # pylint: disable=too-many-arguments, super-init-not-called
self,
openmmForce: t.Union[openmm.Force, str],
unit: mmunit.Unit,
period: t.Optional[mmunit.ScalarQuantity] = None,
) -> None:
if isinstance(openmmForce, openmm.Force):
openmmForce = openmm.XmlSerializer.serialize(openmmForce)
unit = mmunit.SerializableUnit(unit)
force_copy = openmm.XmlSerializer.deserialize(openmmForce)
self.__dict__.update(force_copy.__dict__)
self.__class__.__bases__ += (force_copy.__class__,)
self._registerCV(unit, openmmForce, unit, period)
if period is not None:
self._registerPeriod(period)