/
checkpoint.py
327 lines (273 loc) · 11.2 KB
/
checkpoint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# ======================================================================
# matscipy - Python materials science tools
# https://github.com/libAtoms/matscipy
#
# Copyright (2014) James Kermode, King's College London
# Lars Pastewka, Karlsruhe Institute of Technology
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ======================================================================
"""
Checkpointing and restart functionality for Python scripts use ASE Atoms
objects.
Initialize checkpoint object:
CP = Checkpoint('checkpoints.db')
Checkpointed code block in try ... except notation:
try:
a, C, C_err = CP.load()
except NoCheckpoint:
C, C_err = fit_elastic_constants(a)
CP.save(a, C, C_err)
Checkpoint code block, shorthand notation:
C, C_err = CP(fit_elastic_constants)(a)
Example for checkpointing within an iterative loop, e.g. for searching crack
tip position:
try:
a, converged, tip_x, tip_y = CP.load()
except NoCheckpoint:
converged = False
tip_x = tip_x0
tip_y = tip_y0
while not converged:
... do something to find better crack tip position ...
converged = ...
CP.flush(a, converged, tip_x, tip_y)
The simplest way to use checkpointing is through the CheckpointCalculator. It
wraps any calculator object and
"""
import os
import numpy as np
import ase
from ase.db import connect
from ase.calculators.calculator import Calculator
from matscipy.logger import quiet
###
class NoCheckpoint(Exception):
pass
class Checkpoint(object):
_value_prefix = '_values_'
def __init__(self, db='checkpoints.db', logger=quiet):
self.db = db
self.logger = logger
self.checkpoint_id = [0]
self.in_checkpointed_region = False
def __call__(self, func, *args, **kwargs):
checkpoint_func_name = str(func)
def decorated_func(*args, **kwargs):
# Get the first ase.Atoms object.
atoms = None
for a in args:
if atoms is None and isinstance(a, ase.Atoms):
atoms = a
try:
retvals = self.load(atoms=atoms)
except NoCheckpoint:
retvals = func(*args, **kwargs)
if isinstance(retvals, tuple):
self.save(*retvals, atoms=atoms,
checkpoint_func_name=checkpoint_func_name)
else:
self.save(retvals, atoms=atoms,
checkpoint_func_name=checkpoint_func_name)
return retvals
return decorated_func
def _increase_checkpoint_id(self):
if self.in_checkpointed_region:
self.checkpoint_id += [1]
else:
self.checkpoint_id[-1] += 1
self.logger.pr('Entered checkpoint region '
'{0}.'.format(self.checkpoint_id))
self.in_checkpointed_region = True
def _decrease_checkpoint_id(self):
self.logger.pr('Leaving checkpoint region '
'{0}.'.format(self.checkpoint_id))
if not self.in_checkpointed_region:
self.checkpoint_id = self.checkpoint_id[:-1]
assert len(self.checkpoint_id) >= 1
self.in_checkpointed_region = False
assert self.checkpoint_id[-1] >= 1
def _mangled_checkpoint_id(self):
"""
Returns a mangled checkpoint id string:
c_1:c_2:c_3:...
E.g. if checkpoint is nested an id is [3,2,6] it returns:
'3:2:6'
"""
return reduce(lambda a, b: '{0}:{1}'.format(a, b), self.checkpoint_id)
def load(self, atoms=None):
"""
Retrieve checkpoint data from file. If atoms object is specified, then
the calculator connected to that object is copied to all returning
atoms object.
Returns tuple of values as passed to flush or save during checkpoint
write.
"""
self._increase_checkpoint_id()
retvals = []
with connect(self.db) as db:
try:
dbentry = db.get(checkpoint_id=self._mangled_checkpoint_id())
except KeyError:
raise NoCheckpoint
data = dbentry.data
atomsi = data['checkpoint_atoms_args_index']
i = 0
while i == atomsi or \
'{0}{1}'.format(self._value_prefix, i) in data:
if i == atomsi:
newatoms = dbentry.toatoms()
if atoms is not None:
# Assign calculator
newatoms.set_calculator(atoms.get_calculator())
retvals += [newatoms]
else:
retvals += [data['{0}{1}'.format(self._value_prefix, i)]]
i += 1
self.logger.pr('Successfully restored checkpoint '
'{0}.'.format(self.checkpoint_id))
self._decrease_checkpoint_id()
if len(retvals) == 1:
return retvals[0]
else:
return tuple(retvals)
def _flush(self, *args, **kwargs):
data = dict(('{0}{1}'.format(self._value_prefix, i), v)
for i, v in enumerate(args))
try:
atomsi = [isinstance(v, ase.Atoms) for v in args].index(True)
atoms = args[atomsi]
del data['{0}{1}'.format(self._value_prefix, atomsi)]
except ValueError:
atomsi = -1
try:
atoms = kwargs['atoms']
except:
raise RuntimeError('No atoms object provided in arguments.')
try:
del kwargs['atoms']
except:
pass
data['checkpoint_atoms_args_index'] = atomsi
data.update(kwargs)
with connect(self.db) as db:
try:
dbentry = db.get(checkpoint_id=self._mangled_checkpoint_id())
del db[dbentry.id]
except KeyError:
pass
db.write(atoms, checkpoint_id=self._mangled_checkpoint_id(),
data=data)
self.logger.pr('Successfully stored checkpoint '
'{0}.'.format(self.checkpoint_id))
def flush(self, *args, **kwargs):
"""
Store data to a checkpoint without increasing the checkpoint id. This
is useful to continously update the checkpoint state in an iterative
loop.
"""
# If we are flushing from a successfully restored checkpoint, then
# in_checkpointed_region will be set to False. We need to reset to True
# because a call to flush indicates that this checkpoint is still
# active.
self.in_checkpointed_region = False
self._flush(*args, **kwargs)
def save(self, *args, **kwargs):
"""
Store data to a checkpoint and increase the checkpoint id. This closes
the checkpoint.
"""
self._decrease_checkpoint_id()
self._flush(*args, **kwargs)
def atoms_almost_equal(a, b, tol=1e-9):
return np.abs(a.positions - b.positions).max() < tol and \
(a.numbers == b.numbers).all() and \
np.abs(a.cell - b.cell).max() < tol and \
(a.pbc == b.pbc).all()
class CheckpointCalculator(Calculator):
implemented_properties = ase.calculators.calculator.all_properties
default_parameters = {}
name = 'CheckpointCalculator'
property_to_method_name = {
'energy': 'get_potential_energy',
'energies': 'get_potential_energies',
'forces': 'get_forces',
'stress': 'get_stress',
'stresses': 'get_stresses'
}
def __init__(self, calculator, db='checkpoints.db', logger=quiet):
Calculator.__init__(self)
self.calculator = calculator
self.checkpoint = Checkpoint(db, logger)
self.logger = logger
def calculate(self, atoms, properties, system_changes):
Calculator.calculate(self, atoms, properties, system_changes)
try:
results = self.checkpoint.load(atoms)
prev_atoms, results = results[0], results[1:]
try:
assert atoms_almost_equal(atoms, prev_atoms)
except AssertionError:
raise AssertionError('mismatch between current atoms and those read from checkpoint file')
self.logger.pr('retrieved results for {0} from checkpoint'.format(properties))
# save results in calculator for next time
if isinstance(self.calculator, Calculator):
if not hasattr(self.calculator, 'results'):
self.calculator.results = {}
self.calculator.results.update(dict(zip(properties, results)))
except NoCheckpoint:
if isinstance(self.calculator, Calculator):
self.logger.pr('doing calculation of {0} with new-style calculator interface'.format(properties))
self.calculator.calculate(atoms, properties, system_changes)
results = [self.calculator.results[prop] for prop in properties]
else:
self.logger.pr('doing calculation of {0} with old-style calculator interface'.format(properties))
results = []
for prop in properties:
method_name = CheckpointCalculator.property_to_method_name[prop]
method = getattr(self.calculator, method_name)
results.append(method(atoms))
_calculator = atoms.get_calculator()
try:
atoms.set_calculator(self.calculator)
self.checkpoint.save(atoms, *results)
finally:
atoms.set_calculator(_calculator)
self.results = dict(zip(properties, results))
class BatchCalculator(Calculator):
default_parameters = {}
name = 'BatchCalculator'
property_to_method_name = {
'energy': 'get_potential_energy',
'energies': 'get_potential_energies',
'forces': 'get_forces',
'stress': 'get_stress',
'stresses': 'get_stresses'
}
def __init__(self, db='checkpoints.db'):
Calculator.__init__(self)
self.checkpoint = Checkpoint(db, logger)
self.id = 0
def calculate(self, atoms, properties, system_changes):
Calculator.calculate(self, atoms, properties, system_changes)
if system_changes:
with connect(self.db) as db:
db.write(atoms, checkpoint_id=self.id)
self.id += 1
dummy_results = {'energy': 0.0,
'forces': np.zeros((len(atoms), 3)),
'stress': np.zeros((3,3))}
self.results = {}
for key in properties:
self.results[key] = dummy_results[key]