-
Notifications
You must be signed in to change notification settings - Fork 0
/
components.py
560 lines (396 loc) · 20 KB
/
components.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
from assignment_functions import lobatto_quad, lagrange_basis, edge_basis
from typing import Callable
from mapping import TimeMapping, DomainMapping
import matplotlib.pyplot as plt
import numpy as np
import time
def cls():
import os
os.system("cls")
class UnitTopology(object):
def __init__(self, N:int) -> None:
# Distribute attributes
self.N = N
def __E_3_2_incorrect(self, verbose:bool=True)->np.ndarray:
"""
Note: This function returns erroneous E32!
"""
# Start the clock
start_time = time.perf_counter()
# Retrieve data
N = self.N
# Auxiliary slip
aux_slip = (-1, 1, np.zeros(N ** 2 * (N + 1) - 2),\
-1, np.zeros(N - 1), 1, np.zeros(N ** 3 + N **2 - N - 1),\
-1, np.zeros(N ** 2 -1), 1, np.zeros(N ** 3 - 1))
aux_slip = np.hstack(aux_slip)
# Prelocate memory
E = np.zeros((N ** 3, 3 * N **2 * (N + 1)))
# Roll the auxiliary slip
for i in range(N ** 3):
E[i, :] = np.roll(aux_slip, i)
end_time = time.perf_counter()
# Report time
if verbose:
print(f"E_3_2 incidence matrix: {end_time - start_time} seconds.\n")
return E
def E_3_2_x(self, verbose:bool=True)->np.ndarray:
# Start the clock
start_time = time.perf_counter()
# Retrieve data
N = self.N
aux_slip = (-1, 1, np.zeros(N ** 2 * (N + 1) - 2))
aux_slip = np.hstack(aux_slip)
E = np.zeros((N ** 3, N ** 2 * (N + 1)))
# Roll the auxiliary slip
for i in range(N ** 3):
E[i, :] = aux_slip
if (i + 1) % N == 0:
aux_slip = np.roll(aux_slip, 2)
else:
aux_slip = np.roll(aux_slip, 1)
end_time = time.perf_counter()
# Report time
if verbose:
print(f"E_3_2_x incidence matrix: {end_time - start_time} seconds.\n")
return E
def E_3_2_y(self, verbose:bool=True)->np.ndarray:
# Start the clock
start_time = time.perf_counter()
# Retrieve data
N = self.N
aux_slip = (-1, np.zeros(N - 1), 1, np.zeros(N ** 3 + N **2 - N - 1))
aux_slip = np.hstack(aux_slip)
E = np.zeros((N ** 3, N ** 2 * (N + 1)))
# Roll the auxiliary slip
for i in range(N ** 3):
E[i, :] = aux_slip
if (i + 1) % (N * N) == 0:
aux_slip = np.roll(aux_slip, N + 1)
else:
aux_slip = np.roll(aux_slip, 1)
end_time = time.perf_counter()
# Report time
if verbose:
print(f"E_3_2_y incidence matrix: {end_time - start_time} seconds.\n")
return E
def E_3_2_t(self, verbose:bool=True)->np.ndarray:
# Start the clock
start_time = time.perf_counter()
# Retrieve data
N = self.N
aux_slip = (-1, np.zeros(N ** 2 -1), 1, np.zeros(N ** 3 - 1))
aux_slip = np.hstack(aux_slip)
E = np.zeros((N ** 3, N ** 2 * (N + 1)))
# Roll the auxiliary slip
for i in range(N ** 3):
E[i, :] = aux_slip
aux_slip = np.roll(aux_slip, 1)
end_time = time.perf_counter()
# Report time
if verbose:
print(f"E_3_2_t incidence matrix: {end_time - start_time} seconds.\n")
return E
def N_2_0(self, verbose:bool=True)->np.ndarray:
# Start the clock
start_time = time.perf_counter()
# Retrieve data
N = self.N
E = np.vstack((-np.identity(N ** 2), np.zeros((N ** 3, N **2))))
end_time = time.perf_counter()
# Report time
if verbose:
print(f"N_2_0 inclusion matrix: {end_time - start_time} seconds.\n")
return E
def N_2_T(self, verbose:bool=True)->np.ndarray:
# Start the clock
start_time = time.perf_counter()
# Retrieve data
N = self.N
E = np.vstack((np.zeros((N ** 3, N **2)), np.identity(N ** 2)))
end_time = time.perf_counter()
# Report time
if verbose:
print(f"N_2_T inclusion matrix: {end_time - start_time} seconds.\n")
return E
class Metric(object):
def __init__( self, N:int, N_int_x:int, N_int_y:int, N_int_t:int,\
c:Callable, r:Callable,\
d_map: DomainMapping, t_map: TimeMapping) -> None:
# Distribute attributes
self.N = N
self.N_int_x = N_int_x
self.N_int_y = N_int_y
self.N_int_t = N_int_t
self.c = c
self.r = r
self.d_map = d_map
self.t_map = t_map
# Gauss-Lobatto options
self.GL, self.weights = lobatto_quad(self.N)
self.GL_int_x, self.weights_int_x = lobatto_quad(self.N_int_x)
self.GL_int_y, self.weights_int_y = lobatto_quad(self.N_int_y)
self.GL_int_t, self.weights_int_t = lobatto_quad(self.N_int_t)
# Unit integration grid
self.XI_INT, self.ETA_INT, self.TAU_INT = np.meshgrid(self.GL_int_x, self.GL_int_y, self.GL_int_t)
# NOTE; definition: physical integration grid = transformed(unit integration grid)
# Transformed physical integration grid
self.X_INT = self.d_map.x(self.XI_INT, self.ETA_INT)
self.Y_INT = self.d_map.y(self.XI_INT, self.ETA_INT)
self.T_INT = self.t_map.t(self.TAU_INT)
# Evaluate c and r on the physical integration grid
self.C_EVAL = self.c(self.X_INT, self.Y_INT, self.T_INT)
self.R_EVAL = self.r(self.X_INT, self.Y_INT, self.T_INT)
# Bases evaluated at integration nodes
self.H_MATRIX_X = lagrange_basis(self.GL, self.GL_int_x)
self.E_MATRIX_X = edge_basis(self.GL, self.GL_int_x)
self.H_MATRIX_Y = lagrange_basis(self.GL, self.GL_int_y)
self.E_MATRIX_Y = edge_basis(self.GL, self.GL_int_y)
self.H_MATRIX_T = lagrange_basis(self.GL, self.GL_int_t)
self.E_MATRIX_T = edge_basis(self.GL, self.GL_int_t)
# Evaluate transformation metrics at unit integration nodes
self.X_XI_INT = self.d_map.x_xi(self.XI_INT, self.ETA_INT)
self.X_ETA_INT = self.d_map.x_eta(self.XI_INT, self.ETA_INT)
self.Y_XI_INT = self.d_map.y_xi(self.XI_INT, self.ETA_INT)
self.Y_ETA_INT = self.d_map.y_eta(self.XI_INT, self.ETA_INT)
self.T_TAU_INT = self.t_map.t_tau(self.TAU_INT)
# The Jacobian evaluated at integration nodes
self.J_INT = self.T_TAU_INT * (self.X_XI_INT * self.Y_ETA_INT - self.Y_XI_INT * self.X_ETA_INT)
def M_0(self, verbose:bool=True)->np.ndarray:
# Start the clock
start_time = time.perf_counter()
# Retrieve data
N = self.N
w_x = self.weights_int_x
w_y = self.weights_int_y
w_t = self.weights_int_t
# Transformation
# Begin by defining the 'Transforming Modifier' (TM): The additional term that needs to be integrated.
# It usially consists of two pairs of two partials multiples and summed together (see notes), and the Jacobian inverse.
# Specifically, here: TM = J
TM = self.J_INT
# Appropriate combinations
h_x = self._extend_repeat_matrix(self.H_MATRIX_X, n=(N + 1) ** 2, m=1)
h_y = self._extend_repeat_matrix(self.H_MATRIX_Y, n=N + 1, m=N + 1)
h_t = self._extend_repeat_matrix(self.H_MATRIX_T, n=1, m=(N + 1) ** 2)
# Tile the combinations ("normally span" three vectors containg vectors: e_x, e_y, h_t) and create
# the T-tensor. The T-tensor is designed in such a way that it can be viewed as N^2 * (N+1)-long row vector,
# whose entries are 3D tensors containg evaluated bases at the integration points ('integration cube').
# When you outer-product T with T, that is T * T^(transpose), then the ij-th entry of constructed
# such matrix is the entry of the mass matrix after integrating each ij-th 'integration cube' with appropriate weights.
T = self._tile_3_matrix(h_x, h_y, h_t)
M = np.einsum("iabc,jabc,bac,a,b,c->ij", T, T, TM, w_x, w_y, w_t, optimize="greedy")
end_time = time.perf_counter()
# report time
if verbose:
print(f"M_0 mass matrix: {end_time - start_time} seconds.\n")
return M
def M_1_r(self, verbose:bool=True)->np.ndarray:
# Start the clock
start_time = time.perf_counter()
# Retrieve data
N = self.N
w_x = self.weights_int_x
w_y = self.weights_int_y
w_t = self.weights_int_t
R = self.R_EVAL
# Transformation
# Begin by defining the 'Transforming Modifier' (TM): The additional term that needs to be integrated.
# It usially consists of two pairs of two partials multiples and summed together (see notes), and the Jacobian inverse.
# Specifically, here: TM = (1 / J) * (dt/dtau) ^ 2
TM = self.T_TAU_INT * self. T_TAU_INT / self.J_INT
# Appropriate combinations
e_x = self._extend_repeat_matrix(self.E_MATRIX_X, n=N * (N + 1), m=1)
e_y = self._extend_repeat_matrix(self.E_MATRIX_Y, n=N + 1, m=N)
h_t = self._extend_repeat_matrix(self.H_MATRIX_T, n=1, m=N ** 2)
# Tile the combinations ("normally span" three vectors containg vectors: e_x, e_y, h_t) and create
# the T-tensor. The T-tensor is designed in such a way that it can be viewed as N^2 * (N+1)-long row vector,
# whose entries are 3D tensors containg evaluated bases at the integration points ('integration cube').
# When you outer-product T with T, that is T * T^(transpose), then the ij-th entry of constructed
# such matrix is the entry of the mass matrix after integrating each ij-th 'integration cube' with appropriate weights.
T = self._tile_3_matrix(e_x, e_y, h_t)
M = np.einsum("iabc,jabc,bac,a,b,c->ij", T, T, TM / R, w_x, w_y, w_t, optimize="greedy")
end_time = time.perf_counter()
# report time
if verbose:
print(f"M_1_r mass matrix: {end_time - start_time} seconds.\n")
return M
def M_1_c_00(self, verbose:bool=True)->np.ndarray:
# Start the clock
start_time = time.perf_counter()
# Retrieve data
N = self.N
w_x = self.weights_int_x
w_y = self.weights_int_y
w_t = self.weights_int_t
C = self.C_EVAL
# Transformation
# Begin by defining the 'Transforming Modifier' (TM): The additional term that needs to be integrated.
# It usially consists of two pairs of two partials multiples and summed together (see notes), and the Jacobian inverse.
# Specifically, here: TM = (1 / J) * (dx/dxi * dx/dxi + dy/dxi * dy/dxi)
TM = (self.X_XI_INT * self.X_XI_INT + self.Y_XI_INT * self.Y_XI_INT) / self.J_INT
# Appropriate combinations
e_t = self._extend_repeat_matrix(self.E_MATRIX_T, n=1, m=N * (N + 1))
h_x = self._extend_repeat_matrix(self.H_MATRIX_X, n=N ** 2, m=1)
e_y = self._extend_repeat_matrix(self.E_MATRIX_Y, n=N, m=N + 1)
# Tile the combinations ("normally span" three vectors containg vectors: e_x, e_y, h_t) and create
# the T-tensor. The T-tensor is designed in such a way that it can be viewed as N^2 * (N+1)-long row vector,
# whose entries are 3D tensors containg evaluated bases at the integration points ('integration cube').
# When you outer-product T with T, that is T * T^(transpose), then the ij-th entry of constructed
# such matrix is the entry of the mass matrix after integrating each ij-th 'integration cube' with appropriate weights.
T = self._tile_3_matrix(h_x, e_y, e_t)
M = np.einsum("iabc,jabc,bac,a,b,c->ij", T, T, TM / C, w_x, w_y, w_t, optimize="greedy")
end_time = time.perf_counter()
# report time
if verbose:
print(f"M_1_c_00 mass matrix: {end_time - start_time} seconds.\n")
return M
def M_1_c_01(self, verbose:bool=True)->np.ndarray:
# Start the clock
start_time = time.perf_counter()
# Retrieve data
N = self.N
w_x = self.weights_int_x
w_y = self.weights_int_y
w_t = self.weights_int_t
C = self.C_EVAL
# Transformation
# Begin by defining the 'Transforming Modifier' (TM): The additional term that needs to be integrated.
# It usially consists of two pairs of two partials multiples and summed together (see notes), and the Jacobian inverse.
# Specifically, here: TM = (1 / J) * (dx/deta * dx/dxi + dy/deta * dy/dxi)
TM = (self.X_ETA_INT * self. X_XI_INT + self.Y_ETA_INT * self.Y_XI_INT) / self.J_INT
# Appropriate combinations
e_x_1 = self._extend_repeat_matrix(self.E_MATRIX_X, n=N * (N + 1), m=1)
h_y_1 = self._extend_repeat_matrix(self.H_MATRIX_Y, n=N, m=N)
e_t_1 = self._extend_repeat_matrix(self.E_MATRIX_T, n=1, m=N * (N + 1))
h_x_2 = self._extend_repeat_matrix(self.H_MATRIX_X, n=N ** 2, m=1)
e_y_2 = self._extend_repeat_matrix(self.E_MATRIX_Y, n=N, m=N + 1)
e_t_2 = self._extend_repeat_matrix(self.E_MATRIX_T, n=1, m=N * (N + 1))
# Tile the combinations ("normally span" three vectors containg vectors: e_x, e_y, h_t) and create
# the T_k-tensor. The T_k-tensor is designed in such a way that it can be viewed as N^2 * (N+1)-long row vector,
# whose entries are 3D tensors containg evaluated bases at the integration points ('integration cube').
# When you outer-product T1 with T2, that is T1 * T2, then the ij-th entry of constructed
# such matrix is the entry of the mass matrix after integrating each ij-th 'integration cube' with appropriate weights.
T1 = self._tile_3_matrix(e_x_1, h_y_1, e_t_1)
T2 = self._tile_3_matrix(h_x_2, e_y_2, e_t_2)
M = np.einsum("iabc,jabc,bac,a,b,c->ij", T2, T1, TM / C, w_x, w_y, w_t, optimize="greedy")
end_time = time.perf_counter()
# report time
if verbose:
print(f"M_1_c_01 mass matrix: {end_time - start_time} seconds.\n")
return M
def M_1_c_11(self, verbose:bool=True)->np.ndarray:
# Start the clock
start_time = time.perf_counter()
# Retrieve data
N = self.N
w_x = self.weights_int_x
w_y = self.weights_int_y
w_t = self.weights_int_t
C = self.C_EVAL
# Transformation
# Begin by defining the 'Transforming Modifier' (TM): The additional term that needs to be integrated.
# It usially consists of two pairs of two partials multiples and summed together (see notes), and the Jacobian inverse.
# Specifically, here: TM = (1 / J) * (dx/deta * dx/deta + dy/deta * dy/deta)
TM = (self.X_ETA_INT * self.X_ETA_INT + self.Y_ETA_INT * self.Y_ETA_INT) / self.J_INT
# Appropriate combinations
e_t = self._extend_repeat_matrix(self.E_MATRIX_T, n=1, m=N * (N + 1))
e_x = self._extend_repeat_matrix(self.E_MATRIX_X, n=N * (N + 1), m=1)
h_y = self._extend_repeat_matrix(self.H_MATRIX_Y, n=N, m=N)
# Tile the combinations ("normally span" three vectors containg vectors: e_x, e_y, h_t) and create
# the T-tensor. The T-tensor is designed in such a way that it can be viewed as N^2 * (N+1)-long row vector,
# whose entries are 3D tensors containg evaluated bases at the integration points ('integration cube').
# When you outer-product T with T, that is T * T^(transpose), then the ij-th entry of constructed
# such matrix is the entry of the mass matrix after integrating each ij-th 'integration cube' with appropriate weights.
T = self._tile_3_matrix(e_x, h_y, e_t)
M = np.einsum("iabc,jabc,bac,a,b,c->ij", T, T, TM / C, w_x, w_y, w_t, optimize="greedy")
end_time = time.perf_counter()
# report time
if verbose:
print(f"M_1_c_11 mass matrix: {end_time - start_time} seconds.\n")
return M
def M_3(self, verbose:bool=True)->np.ndarray:
# Start the clock
start_time = time.perf_counter()
# Retrieve data
N = self.N
w_x = self.weights_int_x
w_y = self.weights_int_y
w_t = self.weights_int_t
# Appropriate combinations
e_x = self._extend_repeat_matrix(self.E_MATRIX_X, n=N, m=N)
e_y = self._extend_repeat_matrix(self.E_MATRIX_Y, n=N ** 2, m=1)
e_t = self._extend_repeat_matrix(self.E_MATRIX_T, n=1, m=N ** 2)
# Tile the combinations ("normally span" three vectors containg vectors: e_x, e_y, e_t) and create
# the T-tensor. The T-tensor is designed in such a way that it can be viewed as N^2 * (N+1)-long row vector,
# whose entries are 3D tensors containg evaluated bases at the integration points ('integration cube').
# When you outer-product T with T, that is T * T^(transpose), then the ij-th entry of constructed
# such matrix is the entry of the mass matrix after integrating each ij-th 'integration cube' with appropriate weights.
T = self._tile_3_matrix(e_x, e_y, e_t)
M = np.einsum("iabc,jabc,a,b,c->ij", T, T, w_x, w_y, w_t, optimize="greedy")
end_time = time.perf_counter()
# report time
if verbose:
print(f"M_3 mass matrix: {end_time - start_time} seconds.\n")
return M
def _depr_M_3_1D(self, verbose:bool=True)->np.ndarray:
# Start the clock
start_time = time.perf_counter()
# Retrieve data
N = self.N
w_x = self.weights_int_x
# Appropriate combinations
e_x = self._extend_repeat_matrix(self.E_MATRIX_X, n=1, m=1)
T = e_x
# Tile the combinations ("normally span" three vectors containg vectors: e_x, e_y, e_t) and create
# the T-tensor. The T-tensor is designed in such a way that it can be viewed as N^2 * (N+1)-long row vector,
# whose entries are 3D tensors containg evaluated bases at the integration points ('integration cube').
# When you outer-product T with T, that is T * T^(transpose), then the ij-th entry of constructed
# such matrix is the entry of the mass matrix after integrating each ij-th 'integration cube' with appropriate weights.
M = np.einsum("ia,ja,a->ij", T, T, w_x, optimize="greedy")
end_time = time.perf_counter()
# report time
if verbose:
print(f"M_3 mass matrix: {end_time - start_time} seconds.\n")
return M
# Private _-methods
@staticmethod
def _extend_repeat_matrix(T, n:int, m:int)->np.ndarray:
"""
Args:
T ([type]): tensor (2x2 matrix) to be repeated. Rows are the vectors to be repeated
n (int): how many times to repeat the whole tensor (matrix)
m (int): at each entry (matrix row=vector) how many times to repeat it
"""
M1 = np.repeat(T, m, axis=0)
M2 = np.tile(M1, (n, 1))
return M2
@staticmethod
def _tile_2_matrix(T1, T2)->np.ndarray:
return np.einsum("ij,ik->ijk", T1, T2)
@staticmethod
def _tile_3_matrix(T1, T2, T3)->np.ndarray:
return np.einsum("ij,ik,il->ijkl", T1, T2, T3)
if __name__ == "__main__":
from mapping import StandardDomainMapping, StandardTimeMapping
cls()
c = lambda x, y, t: 0 * x + 0 * y + 0 * t + 1
r = lambda x, y, t: 0 * x + 0 * y + 0 * t + 1
d_map = StandardDomainMapping("crazy_mesh", c=0.1)
t_map = StandardTimeMapping("linear", t_begin=0., t_end=3.)
N0 = 2
metric = Metric(N=N0, N_int_x=N0+5, N_int_y=N0, N_int_t=N0, c=c, r=r, d_map=d_map, t_map=t_map)
utop = UnitTopology(N0)
np.set_printoptions(linewidth=np.inf)
print(utop.E_3_2_x(), utop.E_3_2_y(), utop.E_3_2_t())
N_plt = 100
xi = np.linspace(-1, 1, N_plt)
M3 = metric._depr_M_3_1D()
P_P3 = edge_basis(metric.GL, xi)
P_D0 = (P_P3.T @ np.linalg.inv(M3)).T
plt.figure()
for i in range(N0):
plt.plot(xi, P_D0[i, :], label=f"i={i}")
plt.legend()
plt.grid()
plt.show()