Commit 6f6bee35 authored by Peter Eastman's avatar Peter Eastman
Browse files

DCDReporter writes the current periodic box size rather than the original one

parent 2304af5d
......@@ -49,11 +49,14 @@ class DCDFile(object):
header += struct.pack('<4i', 164, 4, len(list(topology.atoms())), 4)
file.write(header)
def writeModel(self, positions):
def writeModel(self, positions, unitCellDimensions=None):
"""Write out a model to the DCD file.
Parameters:
- positions (list) The list of atomic positions to write
- unitCellDimensions (Vec3=None) The dimensions of the crystallographic unit cell. If None, the dimensions specified in
the Topology will be used. Regardless of the value specified, no dimensions will be written if the Topology does not
represent a periodic system.
"""
if len(list(self._topology.atoms())) != len(positions):
raise ValueError('The number of positions must match the number of atoms')
......@@ -74,6 +77,8 @@ class DCDFile(object):
file.seek(0, os.SEEK_END)
boxSize = self._topology.getUnitCellDimensions()
if boxSize is not None:
if unitCellDimensions is not None:
boxSize = unitCellDimensions
size = boxSize.value_in_unit(angstroms)
file.write(struct.pack('<i6di', 48, size[0], 0, size[1], 0, 0, size[2], 48))
length = struct.pack('<i', 4*len(positions))
......
......@@ -6,6 +6,7 @@ __version__ = "1.0"
import simtk.openmm as mm
from simtk.openmm.app import DCDFile
from simtk.unit import nanometer
class DCDReporter(object):
"""DCDReporter outputs a series of frames from a Simulation to a DCD file.
......@@ -45,7 +46,8 @@ class DCDReporter(object):
"""
if self._dcd is None:
self._dcd = DCDFile(self._out, simulation.topology, simulation.integrator.getStepSize(), 0, self._reportInterval)
self._dcd.writeModel(state.getPositions())
a,b,c = state.getPeriodicBoxVectors()
self._dcd.writeModel(state.getPositions(), mm.Vec3(a[0].value_in_unit(nanometer), b[1].value_in_unit(nanometer), c[2].value_in_unit(nanometer))*nanometer)
def __del__(self):
self._out.close()
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