forked from CURENT/andes
/
snapshot.py
46 lines (31 loc) 路 896 Bytes
/
snapshot.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
"""
Utility functions for saving and loading snapshots.
"""
import dill
import andes
def save_ss(path, system):
"""
Save a system with all internal states as a snapshot.
Returns
-------
Path to the saved snapshot.
Warnings
--------
One limitation of the current implementation is version dependency.
The snapshots only work with the specific ANDES version that created it.
"""
system.remove_pycapsule()
with open(path, 'wb') as file:
dill.dump(system, file, recurse=True)
return path
def load_ss(path):
"""
Load an ANDES snapshot and return a System object.
"""
# the line below is needed to properly import `pycode`.
# TODO: properly import `pycode` beforehand
system = andes.System()
with open(path, 'rb') as file:
system = dill.load(file)
system.fix_address()
return system