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

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

    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
30
31
32
33
34
35
    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__)
36
37
        # Check Force object contents are the same.
        self.assertEqual(XmlSerializer.serialize(force), XmlSerializer.serialize(force_copy))
John Chodera (MSKCC)'s avatar
John Chodera (MSKCC) committed
38
39
40

    def test_deepcopy(self):
        """Test that serialization/deserialization works (via deepcopy)."""
41
42
43
44
45
46
47
48

        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)
49
        integrator2 = copy.deepcopy(integrator)
50
        state2 = copy.deepcopy(state)
51

52
        str_state = pickle.dumps(state)
53
        str_integrator = pickle.dumps(integrator)
54

55
56
57
58
59
        state3 = pickle.loads(str_state)
        context.setState(state3)

        del context, integrator

John Chodera (MSKCC)'s avatar
John Chodera (MSKCC) committed
60
61
62
63
64
65
        # 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__)
66
67
            # Check Force object contents are the same.
            self.assertEqual(XmlSerializer.serialize(force), XmlSerializer.serialize(force_copy))
68

69
70
71
if __name__ == '__main__':
    unittest.main()