Commit b4488289 authored by Jason Swails's avatar Jason Swails
Browse files

Add alternate Simulation constructor taking XML files.

parent bf141989
......@@ -36,6 +36,10 @@ import simtk.openmm as mm
import simtk.unit as unit
import sys
from datetime import datetime, timedelta
try:
string_types = (unicode, str)
except NameError:
string_types = (str,)
class Simulation(object):
"""Simulation provides a simplified API for running simulations with OpenMM and reporting results.
......@@ -52,29 +56,39 @@ class Simulation(object):
simulation.reporters.append(PDBReporter('output.pdb', 1000))
"""
def __init__(self, topology, system, integrator, platform=None, platformProperties=None):
def __init__(self, topology, system, integrator, platform=None, platformProperties=None, state=None):
"""Create a Simulation.
Parameters
----------
topology : Topology
A Topology describing the the system to simulate
system : System
The OpenMM System object to simulate
integrator : Integrator
The OpenMM Integrator to use for simulating the System
system : System or XML file name
The OpenMM System object to simulate (or the name of an XML file
with a serialized System)
integrator : Integrator or XML file name
The OpenMM Integrator to use for simulating the System (or the name
of an XML file with a serialized System)
platform : Platform=None
If not None, the OpenMM Platform to use
platformProperties : map=None
If not None, a set of platform-specific properties to pass to the
Context's constructor
state : XML file name
The name of an XML file containing a serialized State
"""
## The Topology describing the system being simulated
self.topology = topology
## The System being simulated
self.system = system
if isinstance(system, string_types):
with open(system, 'r') as f:
self.system = mm.XmlSerializer.deserialize(f.read())
else:
self.system = system
## The Integrator used to advance the simulation
self.integrator = integrator
if isinstance(integrator, string_types):
with open(integrator, 'r') as f:
self.integrator = mm.XmlSerializer.deserialize(f.read())
else:
self.integrator = integrator
## The index of the current time step
self.currentStep = 0
## A list of reporters to invoke during the simulation
......@@ -86,6 +100,34 @@ class Simulation(object):
self.context = mm.Context(system, integrator, platform)
else:
self.context = mm.Context(system, integrator, platform, platformProperties)
if state is not None:
with open(state, 'r') as f:
self.context.setState(mm.XmlSerializer.deserialize(f.read()))
## Determines whether or not we are using PBC. If no Topology is provided, take it from the System
if topology is None:
self._usesPBC = system.usesPeriodicBoundaryConditions()
else:
self._usesPBC = topology.getUnitCellDimensions() is not None
@classmethod
def fromXmlFiles(cls, system, integrator, state=None, platform=None, platformProperties=None):
""" Initialize a Simulation object from a set of XML files
Parameters
----------
system : str (or mm.System)
XML file name containing a serialized OpenMM System object
integrator : str (or mm.Integrator)
XML file name containing a serialized OpenMM Integrator subclass
state : str, optional
XML file name containing a serialized OpenMM State
platform : Platform=None
If not None, the OpenMM Platform to use
platformProperties : map=None
If not None, a set of platform-specific properties to pass to the
Context's constructor
"""
return cls(None, system, integrator, platform, platformProperties, state=state)
def minimizeEnergy(self, tolerance=10*unit.kilojoule/unit.mole, maxIterations=0):
"""Perform a local energy minimization on the system.
......@@ -186,7 +228,8 @@ class Simulation(object):
getForces = True
if next[4]:
getEnergy = True
state = self.context.getState(getPositions=getPositions, getVelocities=getVelocities, getForces=getForces, getEnergy=getEnergy, getParameters=True, enforcePeriodicBox=(self.topology.getUnitCellDimensions() is not None))
state = self.context.getState(getPositions=getPositions, getVelocities=getVelocities, getForces=getForces,
getEnergy=getEnergy, getParameters=True, enforcePeriodicBox=self._usesPBC)
for reporter, next in zip(self.reporters, nextReport):
if next[0] == nextSteps:
reporter.report(self, state)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment