TestPickle.py 2.37 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
import unittest
from validateConstraints import *
from simtk.openmm.app import *
from simtk.openmm import *
from simtk.unit import *
import simtk.openmm.app.element as elem
import simtk.openmm.app.forcefield as forcefield
import copy
import pickle

11
class TestPickle(unittest.TestCase):
12
    """Pickling / deepcopy of OpenMM objects."""
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

    def setUp(self):
        """Set up the tests by loading the input pdb files and force field
        xml files.

        """
        # alanine dipeptide with explicit water
        self.pdb1 = PDBFile('systems/alanine-dipeptide-explicit.pdb')
        self.forcefield1 = ForceField('amber99sb.xml', 'tip3p.xml')
        self.topology1 = self.pdb1.topology
        self.topology1.setUnitCellDimensions(Vec3(2, 2, 2))

        # alalnine dipeptide with implicit water
        self.pdb2 = PDBFile('systems/alanine-dipeptide-implicit.pdb')
        self.forcefield2 = ForceField('amber99sb.xml', 'amber99_obc.xml')

John Chodera (MSKCC)'s avatar
John Chodera (MSKCC) committed
29
30
31
32
33
34
35
36
37
    def test_force_deepcopy(self):
        """Test that deep copying of forces works correctly."""
        force = NonbondedForce()
        force_copy = copy.deepcopy(force)
        # Check class name is same.
        self.assertEqual(force.__class__.__name__, force_copy.__class__.__name__)

    def test_deepcopy(self):
        """Test that serialization/deserialization works (via deepcopy)."""
38
39
40
41
42
43
44
45

        system = self.forcefield1.createSystem(self.pdb1.topology)
        integrator = VerletIntegrator(2*femtosecond)
        context = Context(system, integrator)
        context.setPositions(self.pdb1.positions)
        state = context.getState(getPositions=True, getForces=True, getEnergy=True)

        system2 = copy.deepcopy(system)
46
        integrator2 = copy.deepcopy(integrator)
47
        state2 = copy.deepcopy(state)
48

49
        str_state = pickle.dumps(state)
50
        str_integrator = pickle.dumps(integrator)
51

52
53
54
55
56
        state3 = pickle.loads(str_state)
        context.setState(state3)

        del context, integrator

John Chodera (MSKCC)'s avatar
John Chodera (MSKCC) committed
57
58
59
60
61
62
63
        # Check deep copy of each force.
        forces = [ system.getForce(index) for index in range(system.getNumForces()) ]
        for force in forces:
            force_copy = copy.deepcopy(force)
            # Check class name is same.
            self.assertEqual(force.__class__.__name__, force_copy.__class__.__name__)
            # TODO: Check to make sure all force data properly copied?
64

65
66
67
if __name__ == '__main__':
    unittest.main()