Skip to content

Commit

Permalink
Add OpenMM <7.6 backwards compatability (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBoothroyd authored Nov 13, 2021
1 parent ad38fa8 commit c045892
Show file tree
Hide file tree
Showing 22 changed files with 248 additions and 64 deletions.
6 changes: 5 additions & 1 deletion openff/evaluator/datasets/curation/components/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ class ConvertExcessDensityData(CurationComponent):
def _molecular_weight(cls, smiles):

from openff.toolkit.topology import Molecule
from openmm import unit as openmm_unit

try:
from openmm import unit as openmm_unit
except ImportError:
from simtk.openmm import unit as openmm_unit

molecule = Molecule.from_smiles(smiles, allow_undefined_stereo=True)

Expand Down
6 changes: 5 additions & 1 deletion openff/evaluator/datasets/curation/components/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,11 @@ def _apply(
) -> pandas.DataFrame:

from openff.toolkit.topology import Molecule
from openmm import unit as openmm_unit

try:
from openmm import unit as openmm_unit
except ImportError:
from simtk.openmm import unit as openmm_unit

def filter_function(data_row):

Expand Down
6 changes: 5 additions & 1 deletion openff/evaluator/datasets/taproom/taproom.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ def _build_substance(
The built substance.
"""
from openff.toolkit.topology import Molecule
from openmm import unit as openmm_unit

try:
from openmm import unit as openmm_unit
except ImportError:
from simtk.openmm import unit as openmm_unit

substance = Substance()

Expand Down
6 changes: 5 additions & 1 deletion openff/evaluator/datasets/thermoml/thermoml.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,11 @@ def _smiles_to_molecular_weight(smiles):
"""

from openff.toolkit.topology import Molecule
from openmm import unit as openmm_unit

try:
from openmm import unit as openmm_unit
except ImportError:
from simtk.openmm import unit as openmm_unit

try:

Expand Down
17 changes: 14 additions & 3 deletions openff/evaluator/forcefield/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
if TYPE_CHECKING:

from openff.toolkit.topology import Topology
from openmm import System

try:
from openmm import System
except ImportError:
from simtk.openmm import System


class ParameterizedSystem(TypedBaseModel):
Expand All @@ -31,7 +35,11 @@ def topology_path(self) -> str:
def topology(self) -> "Topology":

from openff.toolkit.topology import Molecule, Topology
from openmm import app

try:
from openmm import app
except ImportError:
from simtk.openmm import app

pdb_file = app.PDBFile(self._topology_path)

Expand All @@ -51,7 +59,10 @@ def system_path(self) -> str:

@property
def system(self) -> "System":
import openmm
try:
import openmm
except ImportError:
from simtk import openmm

with open(self._system_path) as file:
system = openmm.XmlSerializer.deserialize(file.read())
Expand Down
14 changes: 11 additions & 3 deletions openff/evaluator/protocols/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
)

if typing.TYPE_CHECKING:
import openmm

try:
import openmm
except ImportError:
from simtk import openmm


E0 = 8.854187817e-12 * unit.farad / unit.meter # Taken from QCElemental
Expand Down Expand Up @@ -578,8 +582,12 @@ def _extract_charges(
The charge on each atom in the system if any are present, otherwise
none.
"""
import openmm
from openmm import unit as openmm_unit
try:
import openmm
from openmm import unit as openmm_unit
except ImportError:
from simtk import openmm
from simtk.openmm import unit as openmm_unit

forces = [
system.getForce(force_index)
Expand Down
12 changes: 10 additions & 2 deletions openff/evaluator/protocols/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
from os import path

import numpy as np
from openmm import app

try:
from openmm import app
except ImportError:
from simtk.openmm import app

from openff.evaluator import unit
from openff.evaluator.attributes import UNDEFINED
Expand Down Expand Up @@ -446,7 +450,11 @@ def _execute(self, directory, available_resources):

import mdtraj
from openeye import oechem, oedocking
from openmm import unit as openmm_unit

try:
from openmm import unit as openmm_unit
except ImportError:
from simtk.openmm import unit as openmm_unit

if (
len(self.ligand_substance.components) != 1
Expand Down
32 changes: 26 additions & 6 deletions openff/evaluator/protocols/forcefield.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@
from enum import Enum

import numpy as np
import openmm

try:
import openmm
except ImportError:
from simtk import openmm
import requests
from openmm import app

try:
from openmm import app
except ImportError:
from simtk.openmm import app

from openff.evaluator.attributes import UNDEFINED
from openff.evaluator.forcefield import (
Expand Down Expand Up @@ -547,7 +555,10 @@ def _built_template(molecule, force_field_source):
openmm.app.ForceField
The force field template.
"""
from openmm import unit as openmm_unit
try:
from openmm import unit as openmm_unit
except ImportError:
from simtk.openmm import unit as openmm_unit

initial_request_url = force_field_source.request_url
empty_stream = io.BytesIO(b"\r\n")
Expand Down Expand Up @@ -643,7 +654,10 @@ def _parameterize_molecule(self, molecule, force_field_source, cutoff):
openmm.System
The parameterized system.
"""
from openmm import unit as openmm_unit
try:
from openmm import unit as openmm_unit
except ImportError:
from simtk.openmm import unit as openmm_unit

template = self._built_template(molecule, force_field_source)

Expand Down Expand Up @@ -682,7 +696,10 @@ def _apply_opls_mixing_rules(system):
system: openmm.System
The system object to apply the OPLS mixing rules to.
"""
from openmm import unit as openmm_unit
try:
from openmm import unit as openmm_unit
except ImportError:
from simtk.openmm import unit as openmm_unit

forces = [system.getForce(index) for index in range(system.getNumForces())]
forces = [force for force in forces if isinstance(force, openmm.NonbondedForce)]
Expand Down Expand Up @@ -825,7 +842,10 @@ def _run_tleap(molecule, force_field_source, directory):
str
The file path to the `rst7` file.
"""
from openmm import unit as openmm_unit
try:
from openmm import unit as openmm_unit
except ImportError:
from simtk.openmm import unit as openmm_unit

# Change into the working directory.
with temporarily_change_directory(directory):
Expand Down
5 changes: 4 additions & 1 deletion openff/evaluator/protocols/gradients.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ class ZeroGradients(Protocol, abc.ABC):

def _execute(self, directory, available_resources):

from openmm import unit as openmm_unit
try:
from openmm import unit as openmm_unit
except ImportError:
from simtk.openmm import unit as openmm_unit

force_field_source = ForceFieldSource.from_json(self.force_field_path)

Expand Down
65 changes: 51 additions & 14 deletions openff/evaluator/protocols/openmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@

if TYPE_CHECKING:

import openmm
try:
import openmm
except ImportError:
from simtk import openmm

from mdtraj import Trajectory
from openff.toolkit.topology import Topology
from openff.toolkit.typing.engines.smirnoff import ForceField
Expand Down Expand Up @@ -84,8 +88,12 @@ def _evaluate_energies(
-------
The array containing the evaluated potentials.
"""
import openmm
from openmm import unit as openmm_unit
try:
import openmm
from openmm import unit as openmm_unit
except ImportError:
from simtk import openmm
from simtk.openmm import unit as openmm_unit

integrator = openmm.VerletIntegrator(0.1 * openmm_unit.femtoseconds)

Expand Down Expand Up @@ -178,7 +186,10 @@ def _compute_gradients(
The amount to perturb for the force field parameter by.
"""

import openmm
try:
import openmm
except ImportError:
from simtk import openmm

gradients = defaultdict(list)
observables.clear_gradients()
Expand Down Expand Up @@ -334,9 +345,14 @@ class OpenMMEnergyMinimisation(BaseEnergyMinimisation):

def _execute(self, directory, available_resources):

import openmm
from openmm import app
from openmm import unit as openmm_unit
try:
import openmm
from openmm import app
from openmm import unit as openmm_unit
except ImportError:
from simtk import openmm
from simtk.openmm import app
from simtk.openmm import unit as openmm_unit

platform = setup_platform_with_resources(available_resources)

Expand Down Expand Up @@ -445,7 +461,10 @@ def __init__(self, file, append=False):

def report(self, simulation, state):

from openmm import app
try:
from openmm import app
except ImportError:
from simtk.openmm import app

if self._dcd is None:

Expand Down Expand Up @@ -486,7 +505,11 @@ def __init__(self, protocol_id):
def _execute(self, directory, available_resources):

import mdtraj
from openmm import app

try:
from openmm import app
except ImportError:
from simtk.openmm import app

# We handle most things in OMM units here.
temperature = self.thermodynamic_state.temperature
Expand Down Expand Up @@ -596,9 +619,14 @@ def _setup_simulation_objects(self, temperature, pressure, available_resources):
the simulation.
"""

import openmm
import openmmtools
from openmm import app

try:
import openmm
from openmm import app
except ImportError:
from simtk import openmm
from simtk.openmm import app

# Create a platform with the correct resources.
if not self.allow_gpu_platforms:
Expand Down Expand Up @@ -678,7 +706,10 @@ def _write_checkpoint_file(self, current_step_number, context):
context: openmm.Context
The current OpenMM context.
"""
import openmm
try:
import openmm
except ImportError:
from simtk import openmm

# Write the current state to disk
state = context.getState(
Expand Down Expand Up @@ -831,7 +862,10 @@ def _resume_from_checkpoint(self, context):
int
The current step number.
"""
import openmm
try:
import openmm
except ImportError:
from simtk import openmm

current_step_number = 0

Expand Down Expand Up @@ -953,7 +987,10 @@ def _simulate(self, context, integrator):
integrator: openmm.Integrator
The integrator to evolve the simulation with.
"""
from openmm import app
try:
from openmm import app
except ImportError:
from simtk.openmm import app

# Define how many steps should be taken.
total_number_of_steps = (
Expand Down
Loading

0 comments on commit c045892

Please sign in to comment.