-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaves.py
247 lines (187 loc) · 7.87 KB
/
saves.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
import os
import yaml
from firedrake import VTKFile, Function, CheckpointFile, DumbCheckpoint, FILE_READ, FILE_CREATE
outfile = None
SaveMode = None
save_modes = ('r','o')
save_modes_legacy = ('resume','overwrite','OVERWRITE')
def initialize(save_mode,save_path,remote=False):
""" Establishes the SaveMode, as well as a SavePath which depends on
whether current or legacy SaveMode in use, then repairs the save if
needed. """
global SaveMode, SavePath
SaveMode = save_mode
if SaveMode in save_modes:
SavePath = save_path
repair_save(SavePath)
return
if SaveMode in save_modes_legacy:
save_name = save_path
SavePath = f'saves-remote/{save_name}' if remote else f'saves/{save_name}'
repair_save(SavePath)
return
if SaveMode is None:
raise ValueError('Must have a SaveMode other than None')
raise ValueError('Must choose current ("' + '","'.join(save_modes) + '") or legacy ("' + '","'.join(save_modes_legacy) + '") save mode')
def repair_save(save_path):
import os
directories = (f'{save_path}/chk',f'{save_path}/energy',f'{save_path}/vis')
for directory in directories:
if not os.path.exists(directory):
os.makedirs(directory)
def load_checkpoint(*names):
path = f'{SavePath}/chk/checkpoint.h5'
if not os.path.exists(path):
raise FileNotFoundError
with CheckpointFile(path,'r') as file:
mesh = file.load_mesh()
functions = []
for name in names:
function = file.load_function(mesh,name)
functions.append(function)
return (mesh, *functions)
def save_checkpoint(mesh,*functions):
path = f'{SavePath}/chk/checkpoint.h5'
with CheckpointFile(path,'w') as file:
file.save_mesh(mesh)
for function in functions:
file.save_function(function)
def load_dumb_checkpoint(vector_space,name='dump'):
""" Loads a checkpoint and outputs the function with name specified. """
path = f'{SavePath}/chk/{name}'
if not os.path.exists(f'{path}.h5'):
raise FileNotFoundError
q_dump = Function(vector_space,name=name)
with DumbCheckpoint(path,mode=FILE_READ) as chk:
chk.load(q_dump)
return q_dump
def save_dumb_checkpoint(q_dump,name='dump'):
""" Saves a checkpoint the input being the function with name specified. """
path = f'{SavePath}/chk/{name}'
if not isinstance(q_dump,Function):
raise TypeError('Must be a Firedrake Function.')
q_dump.rename(name)
with DumbCheckpoint(path,mode=FILE_CREATE) as chk:
chk.store(q_dump)
def load_energies():
""" This loader for the energies ensures easy backwards compatibility with older YAML
documents named 'energies.yml' that did not contain the !EnergyList and !TimeList
constructors. These older documents were very much dependent on the relative import
structure of the q-tensor-3d module, and thus imported from 'saves' directly as if
it were a module in the . directory. A good fix to this is to make the replacement
that I have specified in the code below. Thi is dependent on the name of the package
for q-tensor-3d being 'q3d', but I don't plan on changing this name. So this should
get rid of all backwards-compatibility issues. """
with open(f'{SavePath}/energy/energies.yml') as energies_file:
# first ensure backwards compatibility
energies_file = energies_file.read()
energies_file = energies_file.replace('!!python/object/new:saves','!!python/object/new:q3d.saves')
# then load the file into yaml
yaml_load = yaml.load(energies_file, Loader=yaml.Loader)
times = yaml_load['times']
energies = yaml_load['energies']
if len(times) != len(energies):
raise ValueError(f'Number of times {len(times)} and number of energies {len(energies)} not equal.')
return TimeList(times), EnergyList(energies)
def save_energies(times,energies):
if not isinstance(times,TimeList):
raise TypeError
if not isinstance(energies,EnergyList):
raise TypeError
yaml_dump = {'times':times, 'energies':energies}
with open(f'{SavePath}/energy/energies.yml','w') as energies_file:
energies_file.write(yaml.dump(yaml_dump))
def save_pvd(*args, time=None, path=None, mode=None):
# default kwarg
path = path if path is not None else 'vis/vis.pvd'
mode = mode if mode is not None else 'a' if SaveMode in ('r','resume') else 'w'
# check if outfile already created earlier. If not, create it
global outfile
if outfile is None:
outfile = VTKFile(f'{SavePath}/{path}', mode=mode)
# write to outfile
outfile.write(*args, time=time)
# Classes for custom data types
class CustomList(list):
name = 'Custom list'
# Dunder methods
def __init__(self,custom_list):
if not isinstance(custom_list,list):
raise TypeError('Must be list type.')
for i in range(len(custom_list)):
if not isinstance(custom_list[i],int) and not isinstance(custom_list[i],float):
raise TypeError('List items must be type int or float.')
return super().__init__(custom_list)
def __repr__(self):
return self.__class__.name + ': ' + super().__repr__()
def __add__(self,other):
if not isinstance(other,self.__class__):
raise TypeError(f'Cannot add {self.__class__} to {other.__class__}')
return self.__class__(super().__add__(other))
def append(self,other):
super().append(other)
return self.__class__(self)
class EnergyList(CustomList):
name = 'Energy list'
class TimeList(CustomList):
name = 'Time list'
# Dunder methods
def __init__(self,times):
self._enforce_ordered(times)
return super().__init__(times)
# Static methods
@staticmethod
def _enforce_ordered(times):
for i in range(len(times)-1):
if times[i] >= times[i+1]:
raise ValueError('List is not ordered.')
# Class methods
@classmethod
def by_range(cls,t_initial,t_final=None,step=1):
if t_final is None:
t_final = t_initial
t_initial = 0
times = list(range(t_initial,t_final,step))
return cls(times)
@classmethod
def by_prev(cls,t_prev,num_times=0,step=1):
return cls.by_range(num_times).stretch(step).shift(t_prev+step)
# Properties
# @property
# def initial(self):
# return self[0]
@property
def final(self):
return self[-1]
# Regular methods
def shift(self,shift_factor):
if not isinstance(shift_factor,int) and not isinstance(shift_factor,float):
raise TypeError('Shift factor must be type int or float.')
for i in range(len(self)):
self[i] += shift_factor
return self
def stretch(self,stretch_factor):
if not isinstance(stretch_factor,int) and not isinstance(stretch_factor,float):
raise TypeError('Stretch factor must be type int or float.')
for i in range(len(self)):
self[i] *= stretch_factor
return self
def truncate(self,truncation_length):
if not isinstance(truncation_length,int):
raise TypeError('Truncation length must be type int.')
return self.__class__(self[:truncation_length])
def list_constructor(cls):
def constructor(loader,node):
value = loader.construct_sequence(node)
return cls(value)
return constructor
def list_representer(tag):
def representer(dumper,data):
return dumper.represent_sequence(tag,data)
return representer
constructors = ('EnergyList','TimeList')
for constructor in constructors:
constructor_name = '!' + constructor
constructor = eval(constructor)
yaml.add_constructor(constructor_name,list_constructor(constructor))
yaml.add_representer(constructor,list_representer(constructor_name))