Skip to content

Commit

Permalink
Merge pull request #27 from craabreu/manage-inner-rmsd-forces
Browse files Browse the repository at this point in the history
Fixes RMSD force management
  • Loading branch information
craabreu authored Dec 6, 2023
2 parents c2e153b + 490d973 commit 826cd85
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 41 deletions.
10 changes: 3 additions & 7 deletions .github/workflows/Linter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,12 @@ jobs:
- name: Run flake8
shell: bash -l {0}
run: flake8 --max-line-length=100 --ignore=E203,W503 ufedmm/
run: flake8 ufedmm/

- name: Run black
shell: bash -l {0}
run: black --line-length 100 --diff --color ufedmm/
run: black --diff --color ufedmm/

- name: Run isort
shell: bash -l {0}
run: isort --line-length=100 --check-only ufedmm/

# - name: Run pylint
# shell: bash -l {0}
# run: pylint --rcfile=devtools/linters/pylintrc ufedmm/
run: isort --check-only ufedmm/
1 change: 1 addition & 0 deletions devtools/conda-envs/lint_env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies:

# Linting
- flake8
- flake8-pyproject
- black
- isort
# - pylint
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,7 +5,9 @@
# Import package, test suite, and other packages as needed
import io
import sys
from copy import deepcopy

import numpy as np
import openmm
import pytest
from openmm import unit
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 826cd85

Please sign in to comment.