Skip to content

Commit

Permalink
Fixes RMSD force management
Browse files Browse the repository at this point in the history
  • Loading branch information
craabreu committed Dec 6, 2023
1 parent c2e153b commit 96d24b8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 34 deletions.
17 changes: 0 additions & 17 deletions devtools/linters/pylintrc

This file was deleted.

17 changes: 6 additions & 11 deletions docs/readme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ UFEDMM is an OpenMM extension for applying the Unified Free Energy Dynamics meth

* Free software: MIT license

============
Installation
============

UFEDMM is available as a conda package installable from the
[craabreu](https://anaconda.org/craabreu) channel.
UFEDMM is available as a conda package installable from the `craabreu <https://anaconda.org/craabreu>`_ conda channel.
To install it, either run:

.. code-block:: bash
Expand All @@ -29,15 +29,10 @@ To use UFEDMM in your own Python script or Jupyter notebook, import it as follow
import ufedmm
Documentation
=============

https://craabreu.github.io/ufedmm/

============
Contributing
============

Feedback, bug reports, and feature requests are welcome. They should be
submitted to https://github.com/craabreu/ufedmm/issues.

Pull requests are greatly appreciated!
Feedback, bug reports, and feature requests are welcome. They should be submitted to
UFEDMM's `issue tracker <https://github.com/craabreu/ufedmm/issues>`_. Pull requests are
alo greatly appreciated!
24 changes: 24 additions & 0 deletions ufedmm/tests/test_ufedmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
# Import package, test suite, and other packages as needed
import io
import sys
from copy import deepcopy

import openmm
import pytest
from openmm import unit

import ufedmm
import numpy as np


def ufed_model(
Expand Down Expand Up @@ -171,3 +173,25 @@ def test_well_tempered_metadynamics():
simulation.step(100)
energy = simulation.context.getState(getEnergy=True).getPotentialEnergy()
assert energy / energy.unit == pytest.approx(153.17, abs=0.1)


def test_rmsd_forces():
model, ufed = ufed_model(constraints=None)
rmsd = openmm.RMSDForce( # Level 1
model.positions, np.arange(model.system.getNumParticles())
)
cvforce = openmm.CustomCVForce("rmsd + cvforce")
cvforce.addCollectiveVariable("rmsd", deepcopy(rmsd)) # Level 2
inner_cvforce = openmm.CustomCVForce("rmsd")
inner_cvforce.addCollectiveVariable("rmsd", deepcopy(rmsd)) # Level 3
cvforce.addCollectiveVariable("cvforce", inner_cvforce)
model.system.addForce(rmsd)
model.system.addForce(cvforce)
integrator = ufedmm.MiddleMassiveNHCIntegrator(
300 * unit.kelvin, 10 * unit.femtoseconds, 1 * unit.femtoseconds
)
platform = openmm.Platform.getPlatformByName("Reference")
simulation = ufed.simulation(model.topology, model.system, integrator, platform)
simulation.context.setPositions(model.positions)
simulation.context.setVelocitiesToTemperature(300 * unit.kelvin, 1234)
simulation.step(1)
17 changes: 11 additions & 6 deletions ufedmm/ufedmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,27 @@ def _standardized(quantity):
def _update_RMSD_forces(system):
N = system.getNumParticles()

def update_RMSDForce(force):
def _update_RMSDForce(force):
positions = force.getReferencePositions()._value
if len(positions) >= N:
positions = positions[:N]
else:
positions += [openmm.Vec3(0, 0, 0)] * (N - len(positions))
force.setReferencePositions(positions)

def _update_RMSD_forces_in_cvforce(cvforce):
for index in range(cvforce.getNumCollectiveVariables()):
force = cvforce.getCollectiveVariable(index)
if isinstance(force, openmm.RMSDForce):
_update_RMSDForce(force)
elif isinstance(force, openmm.CustomCVForce):
_update_RMSD_forces_in_cvforce(force)

for force in system.getForces():
if isinstance(force, openmm.RMSDForce):
update_RMSDForce(force)
_update_RMSDForce(force)
elif isinstance(force, openmm.CustomCVForce):
for index in range(force.getNumCollectiveVariables()):
cv = force.getCollectiveVariable(index)
if isinstance(cv, openmm.RMSDForce):
update_RMSDForce(cv)
_update_RMSD_forces_in_cvforce(force)


class CollectiveVariable(object):
Expand Down

0 comments on commit 96d24b8

Please sign in to comment.