-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver_tools.py
753 lines (678 loc) · 26.9 KB
/
solver_tools.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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
import low_level_tools as llt
import numpy as np
import equation_tools as et
from scipy.linalg import solve_banded
def solve_pentadiagonal(coeff1, coeff2, bc_left, bc_right, rhs):
"""
This function solve two boundary value problems in 1D
a1 u_{xx} + b1 u_{x} + c1 u + d1 v_{xx} + e1 v_{x} + f1 v = r1
d2 u_{xx} + e2 u_{x} + f2 u + a2 v_{xx} + b2 v_{x} + c2 v = r2
u(0) = alpha1, v(0) = alpha2
u(1) = beta1, v(1) = beta2
using the standard scipy solver `solve_banded`.
Parameters
----------
coeff1: ndarray
[a1, b1, c1, a2, b2, c2]
coeff2: ndarray
[d1, e1, f1, d2, e2, f2]
bc_left: ndarray
[alpha1, alpha2]
bc_right: ndarray
[beta1, beta2]
rhs: ndarray
[r1, r2]
Returns
-------
res: ndarray
Solution of the boundary value problem [u, v]. Shape (2, N).
"""
r1, r2 = rhs
N = len(r1)
h = 1/(N+1)
R = np.zeros(2*N)
R[::2] = r1
R[1::2] = r2
a1, b1, c1, a2, b2, c2 = coeff1
d1, e1, f1, d2, e2, f2 = coeff2
M_left = np.array([[-b1[0]/(2*h) + a1[0]/(h**2), -e1[0]/(2*h) + d1[0]/(h**2)],
[-e2[0]/(2*h) + d2[0]/(h**2), -b2[0]/(2*h) + a2[0]/(h**2)]])
M_right = np.array([[b1[-1]/(2*h) + a1[-1]/(h**2), e1[-1]/(2*h) + d1[-1]/(h**2)],
[e2[-1]/(2*h) + d2[-1]/(h**2), b2[-1]/(2*h) + a2[-1]/(h**2)]])
bc_left_correct = M_left @ bc_left
bc_right_correct = M_right @ bc_right
R[:2] -= bc_left_correct
R[-2:] -= bc_right_correct
###
diag = np.zeros(2*N)
diag[::2] = -2*a1/(h**2) + c1
diag[1::2] = -2*a2/(h**2) + c2
###
dp1 = np.zeros(2*N)
dp1[::2] = -2*d1/(h**2) + f1
dp1[1::2] = e2/(2*h) + d2/(h**2)
dp1 = np.roll(dp1, 1)
dp1[:1] = 0
###
dp2 = np.zeros(2*N)
dp2[::2] = b1/(2*h) + a1/(h**2)
dp2[1::2] = b2/(2*h) + a2/(h**2)
dp2 = np.roll(dp2, 2)
dp2[:2] = 0
###
dp3 = np.zeros(2*N)
dp3[::2] = e1/(2*h) + d1/(h**2)
dp3[1::2] = 0
dp3 = np.roll(dp3, 3)
dp3[:3] = 0
###
dn1 = np.zeros(2*N)
dn1[::2] = -e1/(2*h) + d1/(h**2)
dn1[1::2] = -2*d2/(h**2) + f2
dn1 = np.roll(dn1, -1)
dn1[-1:] = 0
###
dn2 = np.zeros(2*N)
dn2[::2] = -b1/(2*h) + a1/(h**2)
dn2[1::2] = -b2/(2*h) + a2/(h**2)
dn2 = np.roll(dn2, -2)
dn2[-2:] = 0
###
dn3 = np.zeros(2*N)
dn3[::2] = 0
dn3[1::2] = -e2/(2*h) + d2/(h**2)
dn3 = np.roll(dn3, -3)
dn3[-3:] = 0
###
A = np.vstack((dp3, dp2, dp1, diag, dn1, dn2, dn3))
solution = solve_banded((3, 3), A, R, overwrite_ab=True, overwrite_b=True, check_finite=False)
return solution[::2], solution[1::2]
def solve_tridiagonal(coeff1, bc_left, bc_right, rhs):
"""
This function solve the single boundary value problems in 1D
a1 u_{xx} + b1 u_{x} + c1 u = r1
u(0) = alpha1
u(1) = beta1
using the standard scipy solver `solve_banded`.
Parameters
----------
coeff1: ndarray
[a1, b1, c1]
bc_left: double
alpha1
bc_right: double
beta1
rhs: ndarray
r1
Returns
-------
res: ndarray
Solution of the boundary value problem u. Shape (N,).
"""
R = np.copy(rhs)
N = len(R)
h = 1/(N+1)
a1, b1, c1 = coeff1
M_left = -b1[0]/(2*h) + a1[0]/(h**2)
M_right = b1[-1]/(2*h) + a1[-1]/(h**2)
bc_left_correct = M_left*bc_left
bc_right_correct = M_right*bc_right
R[:1] -= bc_left_correct
R[-1:] -= bc_right_correct
###
diag = np.zeros(N)
diag = -2*a1/(h**2) + c1
###
dp1 = np.zeros(N)
dp1 = b1/(2*h) + a1/(h**2)
dp1 = np.roll(dp1, 1)
dp1[:1] = 0
###
dn1 = np.zeros(N)
dn1 = -b1/(2*h) + a1/(h**2)
dn1 = np.roll(dn1, -1)
dn1[-1:] = 0
###
A = np.vstack((dp1, diag, dn1))
solution = solve_banded((1, 1), A, R, overwrite_ab=True, overwrite_b=True, check_finite=False)
return solution
def xZGS_2d_coupled(current, coeff, rhs):
'''
This is the coupled smoother that collectively update variables along x. Coupled
means that the pentadiagonal system is solved along each line. Equations to be
solved have the form
a11 u_{yy} + b11 u_{xx} + c11 u_{xy} + d11 u_{x} + e11 u_{y} + f11 u +
a12 v_{yy} + b12 v_{xx} + c12 v_{xy} + d12 v_{x} + e12 v_{y} + f12 v = r1
a21 u_{yy} + b21 u_{xx} + c21 u_{xy} + d21 u_{x} + e21 u_{y} + f21 u +
a22 v_{yy} + b22 v_{xx} + c22 v_{xy} + d22 v_{x} + e22 v_{y} + f22 v = r2
Parameters
----------
current: ndarray
Current value of the solution. Should have shape (2, N, N) Second index is
the coordinate in x direction and the third - in y direction. `current`
should contain proper boundary values.
coeff: array_like
[[a11, ... ,f11, a12, ... ,f12], [a21, ... ,f21, a22, ... ,f22]
rhs: ndarray
[r1, r2]
Returns
-------
next_current: ndarray
The solution after one iteration. The shape is the same as `current`.
'''
r1, r2 = rhs
coeff1, coeff2 = coeff
a11, b11, c11, d11, e11, f11, a12, b12, c12, d12, e12, f12 = coeff1
a21, b21, c21, d21, e21, f21, a22, b22, c22, d22, e22, f22 = coeff2
u, v = np.copy(current)
N, M = u.shape
h = 1/(N-1)
u_mod = llt.xRed_correction_2d(u, v, N, [a11, c11, e11, a12, c12, e12])
v_mod = llt.xRed_correction_2d(v, u, N, [a22, c22, e22, a21, c21, e21])
for i in range(int((N-1)/2)):
i = 2*i + 1
co1 = [b11[1:-1, i], d11[1:-1, i], f11[1:-1, i] - 2*a11[1:-1, i]/h**2, b22[1:-1, i], d22[1:-1, i], f22[1:-1, i] - 2*a22[1:-1, i]/h**2]
co2 = [b12[1:-1, i], d12[1:-1, i], f12[1:-1, i] - 2*a12[1:-1, i]/h**2, b21[1:-1, i], d21[1:-1, i], f21[1:-1, i] - 2*a21[1:-1, i]/h**2]
RHS = [r1[1:-1, i] - u_mod[1:-1, i], r2[1:-1, i] - v_mod[1:-1, i]]
bc_left = [u[0, i], v[0, i]]
bc_right = [u[-1, i], v[-1, i]]
u[1:-1, i], v[1:-1, i] = solve_pentadiagonal(co1, co2, bc_left, bc_right, RHS)
u_mod = llt.xBlack_correction_2d(u, v, N, [a11, c11, e11, a12, c12, e12])
v_mod = llt.xBlack_correction_2d(v, u, N, [a22, c22, e22, a21, c21, e21])
for i in range(int((N-1)/2)-1):
i = 2*i + 2
co1 = [b11[1:-1, i], d11[1:-1, i], f11[1:-1, i] - 2*a11[1:-1, i]/h**2, b22[1:-1, i], d22[1:-1, i], f22[1:-1, i] - 2*a22[1:-1, i]/h**2]
co2 = [b12[1:-1, i], d12[1:-1, i], f12[1:-1, i] - 2*a12[1:-1, i]/h**2, b21[1:-1, i], d21[1:-1, i], f21[1:-1, i] - 2*a21[1:-1, i]/h**2]
RHS = [r1[1:-1, i] - u_mod[1:-1, i], r2[1:-1, i] - v_mod[1:-1, i]]
bc_left = [u[0, i], v[0, i]]
bc_right = [u[-1, i], v[-1, i]]
u[1:-1, i], v[1:-1, i] = solve_pentadiagonal(co1, co2, bc_left, bc_right, RHS)
return np.array([u, v])
def yZGS_2d_coupled(current, coeff, rhs):
'''
This is the coupled smoother that collectively update variables along y. Coupled
means that the pentadiagonal system is solved along each line. Equations to be
solved have the form
a11 u_{yy} + b11 u_{xx} + c11 u_{xy} + d11 u_{x} + e11 u_{y} + f11 u +
a12 v_{yy} + b12 v_{xx} + c12 v_{xy} + d12 v_{x} + e12 v_{y} + f12 v = r1
a21 u_{yy} + b21 u_{xx} + c21 u_{xy} + d21 u_{x} + e21 u_{y} + f21 u +
a22 v_{yy} + b22 v_{xx} + c22 v_{xy} + d22 v_{x} + e22 v_{y} + f22 v = r2
Parameters
----------
current: ndarray
Current value of the solution. Should have shape (2, N, N) Second index is
the coordinate in x direction and the third - in y direction. `current`
should contain proper boundary values.
coeff: array_like
[[a11, ... ,f11, a12, ... ,f12], [a21, ... ,f21, a22, ... ,f22]]
rhs: ndarray
[r1, r2]
Returns
-------
current+1: ndarray
The solution after one iteration. The shape is the same as `current`.
'''
r1, r2 = rhs
coeff1, coeff2 = coeff
a11, b11, c11, d11, e11, f11, a12, b12, c12, d12, e12, f12 = coeff1
a21, b21, c21, d21, e21, f21, a22, b22, c22, d22, e22, f22 = coeff2
u, v = np.copy(current)
N, M = u.shape
h = 1/(N-1)
u_mod = llt.yRed_correction_2d(u, v, N, [b11, c11, d11, b12, c12, d12])
v_mod = llt.yRed_correction_2d(v, u, N, [b22, c22, d22, b21, c21, d21])
for i in range(int((N-1)/2)):
i = 2*i + 1
co1 = [a11[i, 1:-1], e11[i, 1:-1], f11[i, 1:-1] - 2*b11[i, 1:-1]/h**2, a22[i, 1:-1], e22[i, 1:-1], f22[i, 1:-1] - 2*b22[i, 1:-1]/h**2]
co2 = [a12[i, 1:-1], e12[i, 1:-1], f12[i, 1:-1] - 2*b12[i, 1:-1]/h**2, a21[i, 1:-1], e21[i, 1:-1], f21[i, 1:-1] - 2*b21[i, 1:-1]/h**2]
RHS = [r1[i, 1:-1] - u_mod[i, 1:-1], r2[i, 1:-1] - v_mod[i, 1:-1]]
bc_left = [u[i, 0], v[i, 0]]
bc_right = [u[i, -1], v[i, -1]]
u[i, 1:-1], v[i, 1:-1] = solve_pentadiagonal(co1, co2, bc_left, bc_right, RHS)
u_mod = llt.yBlack_correction_2d(u, v, N, [b11, c11, d11, b12, c12, d12])
v_mod = llt.yBlack_correction_2d(v, u, N, [b22, c22, d22, b21, c21, d21])
for i in range(int((N-1)/2)-1):
i = 2*i + 2
co1 = [a11[i, 1:-1], e11[i, 1:-1], f11[i, 1:-1] - 2*b11[i, 1:-1]/h**2, a22[i, 1:-1], e22[i, 1:-1], f22[i, 1:-1] - 2*b22[i, 1:-1]/h**2]
co2 = [a12[i, 1:-1], e12[i, 1:-1], f12[i, 1:-1] - 2*b12[i, 1:-1]/h**2, a21[i, 1:-1], e21[i, 1:-1], f21[i, 1:-1] - 2*b21[i, 1:-1]/h**2]
RHS = [r1[i, 1:-1] - u_mod[i, 1:-1], r2[i, 1:-1] - v_mod[i, 1:-1]]
bc_left = [u[i, 0], v[i, 0]]
bc_right = [u[i, -1], v[i, -1]]
u[i, 1:-1], v[i, 1:-1] = solve_pentadiagonal(co1, co2, bc_left, bc_right, RHS)
return np.array([u, v])
def xZGS_2d_coupled_boundary_correction(current, coeff, rhs):
r1, r2 = rhs
coeff1, coeff2 = coeff
a11, b11, c11, d11, e11, f11, a12, b12, c12, d12, e12, f12 = coeff1
a21, b21, c21, d21, e21, f21, a22, b22, c22, d22, e22, f22 = coeff2
u, v = np.copy(current)
N, M = u.shape
h = 1/(N-1)
u_mod = llt.xRed_boundary_correction_2d(u, v, N, [a11, c11, e11, a12, c12, e12])
v_mod = llt.xRed_boundary_correction_2d(v, u, N, [a22, c22, e22, a21, c21, e21])
i = 1
co1 = [b11[1:-1, i], d11[1:-1, i], f11[1:-1, i] - 2*a11[1:-1, i]/h**2, b22[1:-1, i], d22[1:-1, i], f22[1:-1, i] - 2*a22[1:-1, i]/h**2]
co2 = [b12[1:-1, i], d12[1:-1, i], f12[1:-1, i] - 2*a12[1:-1, i]/h**2, b21[1:-1, i], d21[1:-1, i], f21[1:-1, i] - 2*a21[1:-1, i]/h**2]
RHS = [r1[1:-1, i] - u_mod[1:-1, i], r2[1:-1, i] - v_mod[1:-1, i]]
bc_left = [u[0, i], v[0, i]]
bc_right = [u[-1, i], v[-1, i]]
u[1:-1, i], v[1:-1, i] = solve_pentadiagonal(co1, co2, bc_left, bc_right, RHS)
i = -2
co1 = [b11[1:-1, i], d11[1:-1, i], f11[1:-1, i] - 2*a11[1:-1, i]/h**2, b22[1:-1, i], d22[1:-1, i], f22[1:-1, i] - 2*a22[1:-1, i]/h**2]
co2 = [b12[1:-1, i], d12[1:-1, i], f12[1:-1, i] - 2*a12[1:-1, i]/h**2, b21[1:-1, i], d21[1:-1, i], f21[1:-1, i] - 2*a21[1:-1, i]/h**2]
RHS = [r1[1:-1, i] - u_mod[1:-1, i], r2[1:-1, i] - v_mod[1:-1, i]]
bc_left = [u[0, i], v[0, i]]
bc_right = [u[-1, i], v[-1, i]]
u[1:-1, i], v[1:-1, i] = solve_pentadiagonal(co1, co2, bc_left, bc_right, RHS)
return np.array([u, v])
def yZGS_2d_coupled_boundary_correction(current, coeff, rhs):
'''
This is the coupled smoother that collectively update variables along y. Coupled
means that the pentadiagonal system is solved along each line. Equations to be
solved have the form
a11 u_{yy} + b11 u_{xx} + c11 u_{xy} + d11 u_{x} + e11 u_{y} + f11 u +
a12 v_{yy} + b12 v_{xx} + c12 v_{xy} + d12 v_{x} + e12 v_{y} + f12 v = r1
a21 u_{yy} + b21 u_{xx} + c21 u_{xy} + d21 u_{x} + e21 u_{y} + f21 u +
a22 v_{yy} + b22 v_{xx} + c22 v_{xy} + d22 v_{x} + e22 v_{y} + f22 v = r2
Parameters
----------
current: ndarray
Current value of the solution. Should have shape (2, N, N) Second index is
the coordinate in x direction and the third - in y direction. `current`
should contain proper boundary values.
coeff: array_like
[[a11, ... ,f11, a12, ... ,f12], [a21, ... ,f21, a22, ... ,f22]]
rhs: ndarray
[r1, r2]
Returns
-------
current+1: ndarray
The solution after one iteration. The shape is the same as `current`.
'''
r1, r2 = rhs
coeff1, coeff2 = coeff
a11, b11, c11, d11, e11, f11, a12, b12, c12, d12, e12, f12 = coeff1
a21, b21, c21, d21, e21, f21, a22, b22, c22, d22, e22, f22 = coeff2
u, v = np.copy(current)
N, M = u.shape
h = 1/(N-1)
u_mod = llt.yRed_boundary_correction_2d(u, v, N, [b11, c11, d11, b12, c12, d12])
v_mod = llt.yRed_boundary_correction_2d(v, u, N, [b22, c22, d22, b21, c21, d21])
i = 1
co1 = [a11[i, 1:-1], e11[i, 1:-1], f11[i, 1:-1] - 2*b11[i, 1:-1]/h**2, a22[i, 1:-1], e22[i, 1:-1], f22[i, 1:-1] - 2*b22[i, 1:-1]/h**2]
co2 = [a12[i, 1:-1], e12[i, 1:-1], f12[i, 1:-1] - 2*b12[i, 1:-1]/h**2, a21[i, 1:-1], e21[i, 1:-1], f21[i, 1:-1] - 2*b21[i, 1:-1]/h**2]
RHS = [r1[i, 1:-1] - u_mod[i, 1:-1], r2[i, 1:-1] - v_mod[i, 1:-1]]
bc_left = [u[i, 0], v[i, 0]]
bc_right = [u[i, -1], v[i, -1]]
u[i, 1:-1], v[i, 1:-1] = solve_pentadiagonal(co1, co2, bc_left, bc_right, RHS)
i = -2
co1 = [a11[i, 1:-1], e11[i, 1:-1], f11[i, 1:-1] - 2*b11[i, 1:-1]/h**2, a22[i, 1:-1], e22[i, 1:-1], f22[i, 1:-1] - 2*b22[i, 1:-1]/h**2]
co2 = [a12[i, 1:-1], e12[i, 1:-1], f12[i, 1:-1] - 2*b12[i, 1:-1]/h**2, a21[i, 1:-1], e21[i, 1:-1], f21[i, 1:-1] - 2*b21[i, 1:-1]/h**2]
RHS = [r1[i, 1:-1] - u_mod[i, 1:-1], r2[i, 1:-1] - v_mod[i, 1:-1]]
bc_left = [u[i, 0], v[i, 0]]
bc_right = [u[i, -1], v[i, -1]]
u[i, 1:-1], v[i, 1:-1] = solve_pentadiagonal(co1, co2, bc_left, bc_right, RHS)
return np.array([u, v])
def aZGS_2d_coupled_boundary_correction(current, coeff, rhs):
current = yZGS_2d_coupled(current, coeff, rhs)
current = xZGS_2d_coupled(current, coeff, rhs)
for i in range(2):
current = yZGS_2d_coupled_boundary_correction(current, coeff, rhs)
current = xZGS_2d_coupled_boundary_correction(current, coeff, rhs)
return current
def aZGS_2d_coupled(current, coeff, rhs):
'''
This is the coupled smoother that collectively update variables along x and then
along y. Coupled means that the pentadiagonal system is solved along each line.
Equations to be solved have the form
a11 u_{yy} + b11 u_{xx} + c11 u_{xy} + d11 u_{x} + e11 u_{y} + f11 u +
a12 v_{yy} + b12 v_{xx} + c12 v_{xy} + d12 v_{x} + e12 v_{y} + f12 v = r1
a21 u_{yy} + b21 u_{xx} + c21 u_{xy} + d21 u_{x} + e21 u_{y} + f21 u +
a22 v_{yy} + b22 v_{xx} + c22 v_{xy} + d22 v_{x} + e22 v_{y} + f22 v = r2
Parameters
----------
current: ndarray
Current value of the solution. Should have shape (2, N, N) Second index is
the coordinate in x direction and the third - in y direction. `current`
should contain proper boundary values.
coeff: ndarray
[[a11, ... ,f11, a12, ... ,f12], [a21, ... ,f21, a22, ... ,f22]]
rhs: ndarray
[r1, r2]
Returns
-------
next_current: ndarray
The solution after one iteration. The shape is the same as `current`.
'''
current = yZGS_2d_coupled(current, coeff, rhs)
current = xZGS_2d_coupled(current, coeff, rhs)
return current
def yZGS_1d(current, coeff, rhs):
'''
This is the GS smoother that collectively update variables along y.
Tridiagonal system is solved each iteration.
a11 u_{yy} + b11 u_{xx} + c11 u_{xy} + d11 u_{x} + e11 u_{y} + f11 u = r1
Parameters
----------
current: ndarray
Current value of the solution. Should have shape (1, N, N) Second index is
the coordinate in x direction and the third - in y direction. `current`
should contain proper boundary values.
coeff: ndarray
[[a11, b11, c11, d11, e11, f11], ]
rhs: ndarray
[r1, ]
Returns
-------
next_current: ndarray
The solution after one iteration. The shape is the same as `current`.
'''
a, b, c, d, e, f = coeff[0]
u = np.copy(current[0])
r1 = rhs[0]
N, M = u.shape
h = 1/(N-1)
u_mod = llt.yRed_correction_1d(u, N, [b, c, d])
for i in range(int((N-1)/2)):
i = 2*i + 1
co1 = [a[i, 1:-1], e[i, 1:-1], f[i, 1:-1] - 2*b[i, 1:-1]/h**2]
RHS = r1[i, 1:-1] - u_mod[i, 1:-1]
bc_left = u[i, 0]
bc_right = u[i, -1]
u[i, 1:-1] = solve_tridiagonal(co1, bc_left, bc_right, RHS)
u_mod = llt.yBlack_correction_1d(u, N, [b, c, d])
for i in range(int((N-1)/2)-1):
i = 2*i + 2
co1 = [a[i, 1:-1], e[i, 1:-1], f[i, 1:-1] - 2*b[i, 1:-1]/h**2]
RHS = r1[i, 1:-1] - u_mod[i, 1:-1]
bc_left = u[i, 0]
bc_right = u[i, -1]
u[i, 1:-1] = solve_tridiagonal(co1, bc_left, bc_right, RHS)
return np.array([u])
def xZGS_1d(current, coeff, rhs):
'''
This is the GS smoother that collectively update variables along x.
Tridiagonal system is solved each iteration.
a11 u_{yy} + b11 u_{xx} + c11 u_{xy} + d11 u_{x} + e11 u_{y} + f11 u = r1
Parameters
----------
current: ndarray
Current value of the solution. Should have shape (1, N, N) Second index is
the coordinate in x direction and the third - in y direction. `current`
should contain proper boundary values.
coeff: ndarray
[[a11, b11, c11, d11, e11, f11], ]
rhs: ndarray
[r1, ]
Returns
-------
next_current: ndarray
The solution after one iteration. The shape is the same as `current`.
'''
a, b, c, d, e, f = coeff[0]
u = np.copy(current[0])
r1 = rhs[0]
N, M = u.shape
h = 1/(N-1)
u_mod = llt.xRed_correction_1d(u, N, [a, c, e])
for i in range(int((N-1)/2)):
i = 2*i + 1
co1 = [b[1:-1, i], d[1:-1, i], f[1:-1, i] - 2*a[1:-1, i]/h**2]
RHS = r1[1:-1, i] - u_mod[1:-1, i]
bc_left = u[0, i]
bc_right = u[-1, i]
u[1:-1, i] = solve_tridiagonal(co1, bc_left, bc_right, RHS)
u_mod = llt.xBlack_correction_1d(u, N, [a, c, e])
for i in range(int((N-1)/2)-1):
i = 2*i + 2
co1 = [b[1:-1, i], d[1:-1, i], f[1:-1, i] - 2*a[1:-1, i]/h**2]
RHS = r1[1:-1, i] - u_mod[1:-1, i]
bc_left = u[0, i]
bc_right = u[-1, i]
u[1:-1, i] = solve_tridiagonal(co1, bc_left, bc_right, RHS)
return np.array([u])
def xGS_1d(current, coeff, rhs):
a, b, c, d, e, f = coeff[0]
u = np.copy(current[0])
r1 = rhs[0]
N, M = u.shape
h = 1/(N-1)
for i in range(1, N-1):
u_mod = llt.xGS_correction(u, N, [a, c, e], i)
co1 = [b[1:-1, i], d[1:-1, i], f[1:-1, i] - 2*a[1:-1, i]/h**2]
RHS = r1[1:-1, i] - u_mod[1:-1]
bc_left = u[0, i]
bc_right = u[-1, i]
u[1:-1, i] = solve_tridiagonal(co1, bc_left, bc_right, RHS)
# Back sweep
for i in range(N-2, 0, -1):
u_mod = llt.xGS_correction(u, N, [a, c, e], i)
co1 = [b[1:-1, i], d[1:-1, i], f[1:-1, i] - 2*a[1:-1, i]/h**2]
RHS = r1[1:-1, i] - u_mod[1:-1]
bc_left = u[0, i]
bc_right = u[-1, i]
u[1:-1, i] = solve_tridiagonal(co1, bc_left, bc_right, RHS)
return np.array([u])
def yGS_1d(current, coeff, rhs):
a, b, c, d, e, f = coeff[0]
u = np.copy(current[0])
r1 = rhs[0]
N, M = u.shape
h = 1/(N-1)
for i in range(1, N-1):
u_mod = llt.yGS_correction(u, N, [b, c, d], i)
co1 = [a[i, 1:-1], e[i, 1:-1], f[i, 1:-1] - 2*b[i, 1:-1]/h**2]
RHS = r1[i, 1:-1] - u_mod[1:-1]
bc_left = u[i, 0]
bc_right = u[i, -1]
u[i, 1:-1] = solve_tridiagonal(co1, bc_left, bc_right, RHS)
# Back sweep
for i in range(N-2, 0, -1):
u_mod = llt.yGS_correction(u, N, [b, c, d], i)
co1 = [a[i, 1:-1], e[i, 1:-1], f[i, 1:-1] - 2*b[i, 1:-1]/h**2]
RHS = r1[i, 1:-1] - u_mod[1:-1]
bc_left = u[i, 0]
bc_right = u[i, -1]
u[i, 1:-1] = solve_tridiagonal(co1, bc_left, bc_right, RHS)
return np.array([u])
def aGS_1d(current, coeff, rhs):
current = xGS_1d(current, coeff, rhs)
current = yGS_1d(current, coeff, rhs)
return current
def decoupled_xGS_2d(current, coeff, rhs):
r1, r2 = rhs
coeff1, coeff2 = coeff
co11 = coeff1[:6]
co12 = coeff1[6:]
co22 = coeff2[6:]
co21 = coeff2[:6]
u, v = current
###
mod_r1 = r1 - et.operator(np.array([v]), np.array([co12]))[0]
u = xGS_1d([u], [co11], [mod_r1])[0]
###
mod_r2 = r2 - et.operator(np.array([u]), np.array([co21]))[0]
v = xGS_1d([v], [co22], [mod_r2])[0]
###
return np.array([u, v])
def decoupled_yGS_2d(current, coeff, rhs):
r1, r2 = rhs
coeff1, coeff2 = coeff
co11 = coeff1[:6]
co12 = coeff1[6:]
co22 = coeff2[6:]
co21 = coeff2[:6]
u, v = current
###
mod_r1 = r1 - et.operator(np.array([v]), np.array([co12]))[0]
u = yGS_1d([u], [co11], [mod_r1])[0]
###
mod_r2 = r2 - et.operator(np.array([u]), np.array([co21]))[0]
v = yGS_1d([v], [co22], [mod_r2])[0]
###
return np.array([u, v])
def decoupled_aGS_2d(current, coeff, rhs):
current = decoupled_xGS_2d(current, coeff, rhs)
current = decoupled_yGS_2d(current, coeff, rhs)
return current
def aGS(current, coeff, rhs):
M, N, K = current.shape
if M == 1:
current = aGS_1d(current, coeff, rhs)
if M == 2:
current = decoupled_aGS_2d(current, coeff, rhs)
return current
def GS_1d(current, coeff, rhs):
N, M = rhs[0].shape
u = llt.xGS(current[0], rhs[0], N, coeff[0])
return np.array([u])
def decoupled_GS_2d(current, coeff, rhs):
r1, r2 = rhs
coeff1, coeff2 = coeff
co11 = coeff1[:6]
co12 = coeff1[6:]
co22 = coeff2[6:]
co21 = coeff2[:6]
u, v = current
###
mod_r1 = r1 - et.operator(np.array([v]), np.array([co12]))[0]
u = GS_1d([u], [co11], [mod_r1])[0]
###
mod_r2 = r2 - et.operator(np.array([u]), np.array([co21]))[0]
v = GS_1d([v], [co22], [mod_r2])[0]
###
return np.array([u, v])
def GS(current, coeff, rhs):
M, N, K = current.shape
if M == 1:
current = GS_1d(current, coeff, rhs)
if M == 2:
current = decoupled_GS_2d(current, coeff, rhs)
return current
def aZGS_1d(current, coeff, rhs):
'''
This is the GS smoother that collectively update variables along x and
then along y. Tridiagonal system is solved each iteration.
a11 u_{yy} + b11 u_{xx} + c11 u_{xy} + d11 u_{x} + e11 u_{y} + f11 u = r1
Parameters
----------
current: ndarray
Current value of the solution. Should have shape (1, N, N) Second index is
the coordinate in x direction and the third - in y direction. `current`
should contain proper boundary values.
coeff: ndarray
[[a11, b11, c11, d11, e11, f11], ]
rhs: ndarray
[r1, ]
Returns
-------
next_current: ndarray
The solution after one iteration. The shape is the same as `current`.
'''
current = xZGS_1d(current, coeff, rhs)
current = yZGS_1d(current, coeff, rhs)
return current
def aZGS(current, coeff, rhs):
'''
This function either solve the system of equations
a11 u_{yy} + b11 u_{xx} + c11 u_{xy} + d11 u_{x} + e11 u_{y} + f11 u +
a12 v_{yy} + b12 v_{xx} + c12 v_{xy} + d12 v_{x} + e12 v_{y} + f12 v = r1
a21 u_{yy} + b21 u_{xx} + c21 u_{xy} + d21 u_{x} + e21 u_{y} + f21 u +
a22 v_{yy} + b22 v_{xx} + c22 v_{xy} + d22 v_{x} + e22 v_{y} + f22 v = r2
using coupled smoother that collectively update variables along x and then
along y. Coupled means that the pentadiagonal system is solved along each line.
Equations to be solved have the form
or the scalar equation
a11 u_{yy} + b11 u_{xx} + c11 u_{xy} + d11 u_{x} + e11 u_{y} + f11 u = r1
usin GS smoother that collectively update variables along x and
then along y. Tridiagonal system is solved each iteration.
Parameters
----------
current: array_like
Current value of the solution. Should have shape (2, N, N) or (1, N, N).
coeff: array_like
[[a11, ... ,f11, a12, ... ,f12], [a21, ... ,f21, a22, ... ,f22]] or
[[a11, b11, c11, d11, e11, f11],]
rhs: array_like
[r1, r2] or [r1, ]
Returns
-------
next_current: array_like
The solution after one iteration. The shape is the same as `current`.
'''
M, N, K = current.shape
if M == 1:
current = aZGS_1d(current, coeff, rhs)
if M == 2:
current = aZGS_2d_coupled(current, coeff, rhs)
return current
def aZGS_2d_decoupled(current, coeff, rhs):
'''
This is the decoupled smoother that collectively update variables along x and then
along y. Deoupled means that the standard GS splitting is used on the level
of equation variables i.e. for the first equation the second variable is fixed
and for the second the first one is fixed.
a11 u_{yy} + b11 u_{xx} + c11 u_{xy} + d11 u_{x} + e11 u_{y} + f11 u +
a12 v_{yy} + b12 v_{xx} + c12 v_{xy} + d12 v_{x} + e12 v_{y} + f12 v = r1
a21 u_{yy} + b21 u_{xx} + c21 u_{xy} + d21 u_{x} + e21 u_{y} + f21 u +
a22 v_{yy} + b22 v_{xx} + c22 v_{xy} + d22 v_{x} + e22 v_{y} + f22 v = r2
Parameters
----------
current: ndarray
Current value of the solution. Should have shape (2, N, N) Second index is
the coordinate in x direction and the third - in y direction. `current`
should contain proper boundary values.
coeff: ndarray
[[a11, ... ,f11, a12, ... ,f12], [a21, ... ,f21, a22, ... ,f22]]
rhs: ndarray
[r1, r2]
Returns
-------
next_current: ndarray
The solution after one iteration. The shape is the same as `current`.
'''
r1, r2 = rhs
coeff1, coeff2 = coeff
co11 = coeff1[:6]
co12 = coeff1[6:]
co22 = coeff2[6:]
co21 = coeff2[:6]
u, v = current
###
mod_r1 = r1 - et.operator(np.array([v]), np.array([co12]))[0]
u = aZGS_1d([u], [co11], [mod_r1])[0]
###
mod_r2 = r2 - et.operator(np.array([u]), np.array([co21]))[0]
v = aZGS_1d([v], [co22], [mod_r2])[0]
###
return np.array([u, v])
def FAS(equation, current, rhs, pre_smoother, pre_n, post_smoother, post_n, restriction, interpolation, coarse_solver, J_min):
# Pre-smoothing
for i in range(pre_n):
current = pre_smoother(equation, current, rhs)
# Extract a defect
fine_defects = equation.rhs_residuals(current, rhs)
# Restrict the defect
coarse_defects = restriction(fine_defects)
# Restrict the solution
coarse_current = et.injection(current)
# Modify the defect
coarse_defects = coarse_defects + equation.operator(coarse_current)
# Solve error equation
N = coarse_current.shape
if N[1] == 2**J_min + 1:
coarse_errors = coarse_solver.detailed_solve(equation, coarse_current, coarse_defects)
else:
args = (pre_smoother, pre_n, post_smoother, post_n, restriction, interpolation, coarse_solver, J_min)
coarse_errors = FAS(equation, coarse_current, coarse_defects, *args)
coarse_errors = FAS(equation, coarse_errors, coarse_defects, *args)
# Modify an errror
coarse_errors = coarse_errors - coarse_current
# Interpolate an error
fine_errors = interpolation(coarse_errors, 0*current)
# Correct
current = current + fine_errors
# Post-smoothing
for i in range(post_n):
current = post_smoother(equation, current, rhs)
return current