checkpointreporter.py 6.36 KB
Newer Older
Robert McGibbon's avatar
Robert McGibbon committed
1
2
3
4
5
6
7
8
"""
checkpointreporter.py: Saves checkpoint files for a simulation

This is part of the OpenMM molecular simulation toolkit originating from
Simbios, the NIH National Center for Physics-Based Simulation of
Biological Structures at Stanford, funded under the NIH Roadmap for
Medical Research, grant U54 GM072970. See https://simtk.org.

9
10
Portions copyright (c) 2014-2021 Stanford University and the Authors.
Authors: Robert McGibbon, Peter Eastman
Robert McGibbon's avatar
Robert McGibbon committed
11
Contributors:
Robert McGibbon's avatar
Robert McGibbon committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
31
from __future__ import absolute_import
Robert McGibbon's avatar
Robert McGibbon committed
32
33
34
__author__ = "Robert McGibbon"
__version__ = "1.0"

35
import openmm as mm
peastman's avatar
peastman committed
36
37
38
import os
import os.path

Robert McGibbon's avatar
Robert McGibbon committed
39
40
41
42
43
__all__ = ['CheckpointReporter']


class CheckpointReporter(object):
    """CheckpointReporter saves periodic checkpoints of a simulation.
Robert McGibbon's avatar
Robert McGibbon committed
44
    The checkpoints will overwrite one another -- only the last checkpoint
45
46
47
    will be saved in the file.  Optionally you can saved serialized State
    objects instead of checkpoints.  This is a more portable but less
    thorough way of recording the state of a simulation.
Robert McGibbon's avatar
Robert McGibbon committed
48
49

    To use it, create a CheckpointReporter, then add it to the Simulation's
Robert McGibbon's avatar
Robert McGibbon committed
50
    list of reporters. To load a checkpoint file and continue a simulation,
Robert McGibbon's avatar
Robert McGibbon committed
51
52
    use the following recipe:

53
54
55
56
57
    >>> simulation.loadCheckpoint('checkpoint.chk')

    Reloading a saved State can be done like this:
    
    >>> simulation.loadState('state.xml')
Robert McGibbon's avatar
Robert McGibbon committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

    Notes:
    A checkpoint contains not only publicly visible data such as the particle
    positions and velocities, but also internal data such as the states of
    random number generators.  Ideally, loading a checkpoint should restore the
    Context to an identical state to when it was written, such that continuing
    the simulation will produce an identical trajectory.  This is not strictly
    guaranteed to be true, however, and should not be relied on.  For most
    purposes, however, the internal state should be close enough to be
    reasonably considered equivalent.

    A checkpoint contains data that is highly specific to the Context from
    which it was created. It depends on the details of the System, the
    Platform being used, and the hardware and software of the computer it was
    created on.  If you try to load it on a computer with different hardware,
    or for a System that is different in any way, loading is likely to fail.
    Checkpoints created with different versions of OpenMM are also often
    incompatible.  If a checkpoint cannot be loaded, that is signaled by
    throwing an exception.

78
79
80
81
82
    In contrast, a State contains only the publicly visible data: positions,
    velocities, global parameters, box vectors, etc.  This makes it much more
    portable.  Reloading the State will put the Simulation back into approximately
    the same state it had before, but you should not expect it to produce an
    identical trajectory to the original Simulation.
Robert McGibbon's avatar
Robert McGibbon committed
83
    """
84
    def __init__(self, file, reportInterval, writeState=False):
Robert McGibbon's avatar
Robert McGibbon committed
85
86
        """Create a CheckpointReporter.

Robert McGibbon's avatar
Robert McGibbon committed
87
88
89
        Parameters
        ----------
        file : string or open file object
90
91
92
            The file to write to.  Any current contents will be overwritten.  If this
            is a file object, it should have been opened in binary mode if writeState
            is false, or in text mode if writeState is true.
Robert McGibbon's avatar
Robert McGibbon committed
93
94
        reportInterval : int
            The interval (in time steps) at which to write checkpoints.
95
96
        writeState : bool=False
            If true, write serialized State objects.  If false, write checkpoints.
Robert McGibbon's avatar
Robert McGibbon committed
97
98
99
        """

        self._reportInterval = reportInterval
peastman's avatar
peastman committed
100
        self._file = file
101
        self._writeState = writeState
Robert McGibbon's avatar
Robert McGibbon committed
102
103
104
105

    def describeNextReport(self, simulation):
        """Get information about the next report this object will generate.

Robert McGibbon's avatar
Robert McGibbon committed
106
107
108
109
110
111
112
        Parameters
        ----------
        simulation : Simulation
            The Simulation to generate a report for

        Returns
        -------
113
114
        dict
            A dictionary describing the required information for the next report
Robert McGibbon's avatar
Robert McGibbon committed
115
116
        """
        steps = self._reportInterval - simulation.currentStep%self._reportInterval
117
        return {'steps':steps, 'periodic':None, 'include':[]}
Robert McGibbon's avatar
Robert McGibbon committed
118
119
120
121

    def report(self, simulation, state):
        """Generate a report.

Robert McGibbon's avatar
Robert McGibbon committed
122
123
124
125
126
127
        Parameters
        ----------
        simulation : Simulation
            The Simulation to generate a report for
        state : State
            The current state of the simulation
Robert McGibbon's avatar
Robert McGibbon committed
128
        """
peastman's avatar
peastman committed
129
130
131
132
133
        if isinstance(self._file, str):
            # Do a safe save.

            tempFilename1 = self._file+".backup1"
            tempFilename2 = self._file+".backup2"
134
135
136
137
            if self._writeState:
                simulation.saveState(tempFilename1)
            else:
                simulation.saveCheckpoint(tempFilename1)
peastman's avatar
peastman committed
138
139
140
141
142
143
144
145
            exists = os.path.exists(self._file)
            if exists:
                os.rename(self._file, tempFilename2)
            os.rename(tempFilename1, self._file)
            if exists:
                os.remove(tempFilename2)
        else:
            # Replace the contents of the file.
146

peastman's avatar
peastman committed
147
            self._file.seek(0)
148
            if self._writeState:
Peter Eastman's avatar
Peter Eastman committed
149
                state = simulation.context.getState(positions=True, velocities=True, parameters=True, integratorParameters=True)
150
151
152
                self._file.write(mm.XmlSerializer.serialize(state))
            else:
                self._file.write(simulation.context.createCheckpoint())
peastman's avatar
peastman committed
153
154
            self._file.truncate()
            self._file.flush()