-
Notifications
You must be signed in to change notification settings - Fork 0
/
mgrit.py
355 lines (321 loc) · 17.2 KB
/
mgrit.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import numpy as np
from pint_task_graph import PintGraph
class Mgrit(PintGraph):
def __init__(self,
coarsening,
cost_step,
cf_iter,
cycle_type,
placing_conv_crit,
nested_iterations=False,
skip_down=False,
cost_res=None,
cost_pro=None,
*args, **kwargs):
super().__init__(*args, **kwargs)
# MGRIT parameter
self.coarsening = coarsening
self.cf_iter = cf_iter
self.cycle_type = cycle_type
self.L = len(self.coarsening)
self.nested_iterations = nested_iterations
self.skip_down = skip_down
self.placing_conv_crit = placing_conv_crit
if nested_iterations and skip_down:
print('Combination of nested iterations and skip down not really useful')
# MGRIT specific costs
if cost_res is None:
self.cost_res = np.zeros(self.L)
else:
self.cost_res = cost_res
if cost_pro is None:
self.cost_pro = np.zeros(self.L)
else:
self.cost_pro = cost_pro
self.time_steppers = cost_step
# Dummies for datastructures etc
self.f_blocks_per_level = []
self.points_per_level = []
self.c_points_per_level = []
#Compute time hierarchy
self.pre_and_after_info_per_level = [{} for _ in range(self.L)]
for level in range(self.L):
if level == 0:
self.points_per_level.append(np.linspace(0, self.nt - 1, self.nt, dtype=int))
else:
self.points_per_level.append(np.copy(self.c_points_per_level[-1]))
self.c_points_per_level.append(np.copy(self.points_per_level[-1][::self.coarsening[level]]))
f_points_per_level = np.sort(np.array(list(
set(self.points_per_level[-1].tolist()) - set(self.c_points_per_level[-1].tolist()))))
tmp = self.consecutive(f_points_per_level, stepsize=np.prod(self.coarsening[:level]))
tmp.reverse()
self.f_blocks_per_level.append(tmp)
for j in range(len(self.points_per_level[level])):
pre = self.points_per_level[level][j - 1] if j != 0 else -99
self.pre_and_after_info_per_level[level][self.points_per_level[level][j]] = pre
# Initialize approximation at time points
for level in range(self.L):
for i in self.points_per_level[level]:
set_val = self.create_node_name(var_name='u', var_dict=self.cr_dict(level=level, time_point=i))
set_val += self.create_node_name(var_name='v', var_dict=self.cr_dict(level=level, time_point=i))
set_val += self.create_node_name(var_name='g', var_dict=self.cr_dict(level=level, time_point=i))
if i == 0:
self.add_node(name="Q|" + str(level),set_values=set_val,predecessors=['u_0'],cost=0,point=i)
else:
self.add_node(name="Q|" + str(level), set_values=set_val, predecessors=['0'], cost=0, point=i)
# Setup convergence criterion
self.cc = {}
if self.placing_conv_crit == 0:
for i in self.c_points_per_level[0]:
if i > 0:
cc = self.create_node_name(var_name='u', var_dict=self.cr_dict(level=0, time_point=i))
cc += self.create_node_name(var_name='u', var_dict=self.cr_dict(level=0, time_point=i - 1))
self.cc[i] = cc
elif self.placing_conv_crit == 1:
for i in self.c_points_per_level[0]:
if i > 0:
self.cc[i] = self.create_node_name(var_name='r', var_dict=self.cr_dict(level=0, time_point=i))
else:
raise Exception('Unknown placing')
def f_relax(self, level: int) -> None:
"""
F-relaxation
:param level: Level
"""
for block in self.f_blocks_per_level[level]:
for i in block:
pre = self.pre_and_after_info_per_level[level][i]
if level == 0:
self.add_node(name="F|" + str(level),
predecessors=self.create_node_name(var_name='u',
var_dict=self.cr_dict(level=level,
time_point=pre)),
set_values=self.create_node_name(var_name='u',
var_dict=self.cr_dict(level=level, time_point=i)),
cost=self.time_steppers[level],
point=i,
description='mgrit_step_' + str(level))
else:
pre = self.create_node_name(var_name='u', var_dict=self.cr_dict(level=level, time_point=pre))
pre += self.create_node_name(var_name='g', var_dict=self.cr_dict(level=level, time_point=i))
self.add_node(name="F|" + str(level),
predecessors=pre,
set_values=self.create_node_name(var_name='u',
var_dict=self.cr_dict(level=level, time_point=i)),
cost=self.time_steppers[level],
point=i,
description='mgrit_step_' + str(level))
def c_relax(self, level: int) -> None:
"""
C-relaxation
:param level: Level
"""
for i in self.c_points_per_level[level]:
pre = self.pre_and_after_info_per_level[level][i]
if i != 0:
if level == 0:
self.add_node(name="C|" + str(level),
predecessors=self.create_node_name(var_name='u',
var_dict=self.cr_dict(level=level,
time_point=pre)),
set_values=self.create_node_name(var_name='u',
var_dict=self.cr_dict(level=level, time_point=i)),
cost=self.time_steppers[level],
point=i,
description='mgrit_step_' + str(level))
else:
pred = self.create_node_name(var_name='u', var_dict=self.cr_dict(level=level, time_point=pre))
pred += self.create_node_name(var_name='g', var_dict=self.cr_dict(level=level, time_point=i))
self.add_node(name="C|" + str(level),
predecessors=pred,
set_values=self.create_node_name(var_name='u',
var_dict=self.cr_dict(level=level, time_point=i)),
cost=self.time_steppers[level],
point=i,
description='mgrit_step_' + str(level))
def restrict(self, level: int) -> None:
"""
Restrict C-points to next coarser level
:param level: Level
"""
for i in self.c_points_per_level[level]:
self.add_node(name="R|" + str(level),
predecessors=self.create_node_name(var_name='u',
var_dict=self.cr_dict(level=level, time_point=i)),
set_values=self.create_node_name(var_name='u',
var_dict=self.cr_dict(level=level + 1, time_point=i)),
cost=self.cost_res[level],
point=i,
description='mgrit_res_' + str(level))
self.add_node(name="R|" + str(level),
predecessors=['u_' + str(level + 1) + '_' + str(i)],
set_values=["v_" + str(level + 1) + "_" + str(i)],
cost=0,
point=i,
description='mgrit_copy_' + str(level))
def residual(self, level: int) -> None:
"""
Compute residual
:param level: Level
"""
for i in self.c_points_per_level[level]:
pre_fine = self.pre_and_after_info_per_level[level][i]
if i != 0:
pred = self.create_node_name(var_name='g', var_dict=self.cr_dict(level=level, time_point=i))
pred += self.create_node_name(var_name='u', var_dict=self.cr_dict(level=level, time_point=pre_fine))
pred += self.create_node_name(var_name='u', var_dict=self.cr_dict(level=level, time_point=i))
self.add_node(name="FR|" + str(level),
predecessors=pred,
set_values=self.create_node_name(var_name='r',
var_dict=self.cr_dict(level=level, time_point=i)),
cost=self.time_steppers[level],
point=i,
description='mgrit_step_' + str(level))
def fas_residual(self, level: int) -> None:
"""
Compute FAS residual
:param level: Level
"""
for i in self.c_points_per_level[level]:
pre_coarse = self.pre_and_after_info_per_level[level + 1][i]
if i != 0:
self.add_node(name="R|" + str(level),
predecessors=self.create_node_name(var_name='r',
var_dict=self.cr_dict(level=level, time_point=i)),
set_values=self.create_node_name(var_name='g',
var_dict=self.cr_dict(level=level + 1, time_point=i)),
cost=self.cost_res[level],
point=i,
description='mgrit_res_' + str(level))
pred = self.create_node_name(var_name='g', var_dict=self.cr_dict(level=level + 1, time_point=i))
pred += self.create_node_name(var_name='u',
var_dict=self.cr_dict(level=level + 1, time_point=pre_coarse))
pred += self.create_node_name(var_name='u', var_dict=self.cr_dict(level=level + 1, time_point=i))
self.add_node(name="FR|" + str(level + 1),
predecessors=pred,
set_values=self.create_node_name(var_name='g',
var_dict=self.cr_dict(level=level + 1, time_point=i)),
cost=self.time_steppers[level + 1],
point=i,
description='mgrit_step_' + str(level + 1))
def coarsest_level(self, level: int) -> None:
"""
Coarsest level
:param level: Level
"""
for i in self.points_per_level[level]:
pre = self.pre_and_after_info_per_level[level][i]
if i != 0:
pred = self.create_node_name(var_name='u', var_dict=self.cr_dict(level=level, time_point=pre))
pred += self.create_node_name(var_name='g', var_dict=self.cr_dict(level=level, time_point=i))
self.add_node(name="TS|" + str(level),
predecessors=pred,
set_values=self.create_node_name(var_name='u',
var_dict=self.cr_dict(level=level, time_point=i)),
cost=self.time_steppers[level],
point=i,
description='mgrit_step_' + str(level))
def consecutive(self, data: np.ndarray, stepsize: int = 1) -> np.ndarray:
"""
Auxiliary function to obtain blocks of F points
:param data: List of time points
:param stepsize: Stepsize between points
:return: F-blocks
"""
return np.split(data, np.where(np.diff(data) != stepsize)[0] + 1)
def error_correction(self, level: int) -> None:
"""
Error correction
:param level: Current level
"""
for i in self.c_points_per_level[level]:
if i != 0:
pred = self.create_node_name(var_name='u', var_dict=self.cr_dict(level=level + 1, time_point=i))
pred += self.create_node_name(var_name='v', var_dict=self.cr_dict(level=level + 1, time_point=i))
pred += self.create_node_name(var_name='u', var_dict=self.cr_dict(level=level, time_point=i))
self.add_node(name="E|" + str(level),
predecessors=pred,
set_values=self.create_node_name(var_name='u',
var_dict=self.cr_dict(level=level, time_point=i)),
cost=self.cost_pro[level],
point=i,
description='mgrit_pro_' + str(level))
def nested_iteration_interpolation(self, level: int) -> None:
"""
Interpolation within nested iterations
:param level: Current level
"""
for i in self.c_points_per_level[level]:
pre = self.pre_and_after_info_per_level[level][i]
if i != 0:
self.add_node(name="I|" + str(level),
predecessors=self.create_node_name(var_name='u',
var_dict=self.cr_dict(level=level, time_point=i)),
set_values=self.create_node_name(var_name='u',
var_dict=self.cr_dict(level=level - 1, time_point=i)),
cost=self.cost_pro[level],
point=i,
description='mgrit_pro_' + str(level))
def restrict_for_skip_down(self) -> None:
"""
Restriction within skip down
"""
for level in range(0, self.L - 1):
for i in self.c_points_per_level[level]:
self.add_node(name="R|" + str(level),
predecessors=self.create_node_name(var_name='u',
var_dict=self.cr_dict(level=level, time_point=i)),
set_values=self.create_node_name(var_name='u',
var_dict=self.cr_dict(level=level + 1, time_point=i)),
cost=self.cost_res[level],
point=i,
description='mgrit_res_' + str(level))
def compute(self) -> None:
"""
Computes the graph
"""
if self.nested_iterations:
self.restrict_for_skip_down()
for level in range(self.L - 1, 0, -1):
self.mgrit(level=level, first_f=True, it=-1, cycle_type='V')
self.nested_iteration_interpolation(level - 1)
for it in range(self.iterations):
if it == 0 and self.skip_down:
self.restrict_for_skip_down()
self.mgrit(level=0, first_f=True, it=0, skip_down=True, cycle_type=self.cycle_type)
else:
self.mgrit(level=0, first_f=True, it=it, cycle_type=self.cycle_type)
if self.placing_conv_crit == 0:
self.convergence_criterion(poins_with_dependencies=self.cc)
def mgrit(self, level: object, cycle_type: object, first_f: object, it: object,
skip_down: object = False) -> None:
"""
MGRIT iteration
:param level: Current level
:param cycle_type: Cycle type
:param first_f: Perform first F-relaxation
:param it: Iteration count
:param skip_down: Skip down
:return:
"""
if level == self.L - 1:
self.coarsest_level(level=level)
return
else:
if not skip_down:
if (level > 0 or (it == 0 and level == 0)) and first_f and not skip_down:
self.f_relax(level=level)
for _ in range(self.cf_iter[level]):
self.c_relax(level=level)
self.f_relax(level=level)
self.residual(level=level)
self.restrict(level=level)
self.fas_residual(level=level)
if self.placing_conv_crit == 1 and level == 0:
self.convergence_criterion(poins_with_dependencies=self.cc)
self.mgrit(level=level + 1, cycle_type=cycle_type, first_f=True, it=it, skip_down=skip_down)
self.error_correction(level=level)
self.f_relax(level=level)
if level != 0 and cycle_type == 'F':
self.mgrit(level=level, cycle_type='V', it=it, first_f=False)
return