TestCheckpointReporter.py 1.57 KB
Newer Older
1
import os
Robert McGibbon's avatar
Robert McGibbon committed
2
3
import unittest
import tempfile
4
import numpy as np
Robert McGibbon's avatar
Robert McGibbon committed
5
6
7
8
9
10
11
from simtk.openmm import app
import simtk.openmm as mm
from simtk import unit


class TestCheckpointReporter(unittest.TestCase):
    def setUp(self):
12
13
        with open('systems/alanine-dipeptide-implicit.pdb') as f:
            pdb = app.PDBFile(f)
Robert McGibbon's avatar
Robert McGibbon committed
14
        forcefield = app.ForceField('amber99sbildn.xml')
15
16
        system = forcefield.createSystem(pdb.topology,
            nonbondedMethod=app.CutoffNonPeriodic, nonbondedCutoff=1.0*unit.nanometers,
Robert McGibbon's avatar
Robert McGibbon committed
17
18
19
            constraints=app.HBonds)
        self.simulation = app.Simulation(pdb.topology, system, mm.VerletIntegrator(0.002*unit.picoseconds))
        self.simulation.context.setPositions(pdb.positions)
20

Robert McGibbon's avatar
Robert McGibbon committed
21
    def test_1(self):
22
        file = tempfile.NamedTemporaryFile(delete=False)
Robert McGibbon's avatar
Robert McGibbon committed
23
24
        self.simulation.reporters.append(app.CheckpointReporter(file, 1))
        self.simulation.step(1)
25
26
27
28
29
30

        # get the current positions
        positions = self.simulation.context.getState(getPositions=True).getPositions(asNumpy=True)._value
        # now set the positions into junk...
        self.simulation.context.setPositions(np.random.random(positions.shape))
        # then reload the right positions from the checkpoint
31
        file.close()
Robert McGibbon's avatar
Robert McGibbon committed
32
33
        with open(file.name, 'rb') as f:
            self.simulation.context.loadCheckpoint(f.read())
34
        os.unlink(file.name)
Robert McGibbon's avatar
Robert McGibbon committed
35

36
37
38
        newPositions = self.simulation.context.getState(getPositions=True).getPositions(asNumpy=True)._value
        np.testing.assert_array_equal(positions, newPositions)

Robert McGibbon's avatar
Robert McGibbon committed
39
if __name__ == '__main__':
Robert McGibbon's avatar
Robert McGibbon committed
40
    unittest.main()