1
- using PiecewiseOrthogonalPolynomials, MatrixFactorizations
1
+ using PiecewiseOrthogonalPolynomials, MatrixFactorizations, HypergeometricFunctions
2
2
using Elliptic
3
3
using ClassicalOrthogonalPolynomials, StaticArrays, LinearAlgebra
4
+ using Base: oneto
4
5
5
6
"""
6
7
Solve the Poisson equation with zero Dirichlet boundary conditions on the square
@@ -16,34 +17,44 @@ function mobius(z, a, b, c, d, α)
16
17
(t₁* z + t₂)/ (t₃* z + t₄)
17
18
end
18
19
20
+ ellipticK (z) = convert (eltype (α),π)/ 2 * HypergeometricFunctions. _₂F₁ (one (α)/ 2 ,one (α)/ 2 ,1 , z)
19
21
20
- function ADI_shifts (J, a, b, c, d, tol)
22
+
23
+ function ADI_shifts (J, a, b, c, d, tol= 1e-15 )
21
24
γ = (c- a)* (d- b)/ ((c- b)* (d- a))
22
25
α = - 1 + 2 γ + 2 √ Complex (γ^ 2 - γ)
23
26
α = Real (α)
24
27
25
- K = Elliptic. K (1 - 1 / α^ 2 )
26
- dn = [Elliptic. Jacobi. dn ((2 * j + 1 )* K/ (2 J), 1 - 1 / α^ 2 ) for j = 0 : J- 1 ]
28
+ # K = ellipticK(1-1/big(α)^2)
29
+ if α < 1e7
30
+ K = Elliptic. K (1 - 1 / α^ 2 )
31
+ dn = Elliptic. Jacobi. dn .((2 * (0 : J- 1 ) + 1 )* K/ (2 J), 1 - 1 / α^ 2 )
32
+ else
33
+ K = 2 log (2 )+ log (α) + (- 1 + 2 log (2 )+ log (α))/ α^ 2 / 4
34
+ m1 = 1 / α^ 2
35
+ u = (1 / 2 : J- 1 / 2 ) * K/ J
36
+ dn = @. sech (u) + m1/ 4 * (sinh (u)cosh (u) + u) * tanh (u) * sech (u)
37
+ end
27
38
28
39
[mobius (- α* i, a, b, c, d, α) for i = dn], [mobius (α* i, a, b, c, d, α) for i = dn]
29
40
end
30
41
31
- function ADI (A, B, M , F, a, b, c, d, tol)
42
+ function ADI (A, B, C , F, a, b, c, d, tol= 1e-15 )
32
43
" ADI method for solving standard sylvester AX - XB = F"
33
44
# Modified slightly by John to allow for the mass matrix
34
45
n = size (A)[1 ]
35
- X = zeros ((n, n ))
46
+ X = zeros (axes (A ))
36
47
37
48
γ = (c- a)* (d- b)/ ((c- b)* (d- a))
38
49
J = Int (ceil (log (16 γ)* log (4 / tol)/ π^ 2 ))
39
50
# J = 200
40
51
p, q = ADI_shifts (J, a, b, c, d, tol)
41
52
42
53
for j = 1 : J
43
- X = (F - (A - p[j]* M) * X) / (B - p[j]* M )
44
- X = (A - q[j]* M) \ (F - X* (B - q[j]* M) )
54
+ X = ((A / p[j] - C) * X - F / p[j]) / reversecholesky ( Symmetric (C - B / p[j]) )
55
+ X = reversecholesky ( Symmetric (C - A / q[j])) \ ( X* (B/ q[j] - C) - F / q[j])
45
56
end
46
-
57
+
47
58
X
48
59
end
49
60
@@ -56,8 +67,8 @@ function analysis_2D(f, n, p)
56
67
57
68
for i = 0 : n- 1 # loop over cells in positive x direction
58
69
for j = 0 : n- 1 # loop over cells in positive y direction
59
- local f_ = z -> (( x,y)= z; f (( x + i* dx - 1 , y + j* dx - 1 )) ) # define f on reference cell
60
- F[i+ 1 : n: n* p, j+ 1 : n: n* p] = T * f_ .(SVector .( z, z' ) ) # interpolate f into 2D tensor Legendre polynomials on reference cell
70
+ f_ = ( x,y) -> f (x + i* dx - 1 , y + j* dx - 1 ) # define f on reference cell
71
+ F[i+ 1 : n: n* p, j+ 1 : n: n* p] = T * f_ .(z, z' ) # interpolate f into 2D tensor Legendre polynomials on reference cell
61
72
end
62
73
end
63
74
67
78
r = range (- 1 , 1 , 5 )
68
79
K = length (r)- 1
69
80
70
- C = ContinuousPolynomial {1} (r)
71
81
P = ContinuousPolynomial {0} (r)
72
- D = Derivative (axes (C,1 ))
73
- Δ = - weaklaplacian (C)
74
- M = grammatrix (C)
75
- e1s, e2s = [], []
76
- p = 40 # truncation degree on each cell
77
- N = K+ 1 + K* (p+ 1 ) # amount of basis functions in C
78
-
79
- # Truncated Laplacian + Dirichlet bcs
80
-
81
-
82
+ Q = DirichletPolynomial (r)
83
+ Δ = - weaklaplacian (Q)
84
+ M = grammatrix (Q)
82
85
86
+ p = 300 # truncation degree on each cell
87
+ KR = Block .(oneto (p))
88
+ Δₙ = Δ[KR,KR]
89
+ Mₙ = M[KR,KR]
83
90
84
- pΔ = Matrix (Δ[Block .(1 : p), Block .(1 : p)]);
85
- pΔ[:,1 ] .= 0 ; pΔ[1 ,:] .= 0 ; pΔ[1 ,1 ] = 1. ;
86
- pΔ[:,K+ 1 ] .= 0 ; pΔ[K+ 1 ,:] .= 0 ; pΔ[K+ 1 ,K+ 1 ] = 1. ;
87
-
88
- # Truncated mass + Dirichlet bcs
89
- pM = Matrix (M[Block .(1 : p), Block .(1 : p)]);
90
- pM[:,1 ] .= 0 ; pM[1 ,:] .= 0 ; pM[1 ,1 ] = 1. ;
91
- pM[:,K+ 1 ] .= 0 ; pM[K+ 1 ,:] .= 0 ; pM[K+ 1 ,K+ 1 ] = 1. ;
92
-
93
- """
94
- Via the standard route ADI
95
- """
96
- # Reverse Cholesky
97
- rpΔ = pΔ[end : - 1 : 1 , end : - 1 : 1 ]
98
- L = cholesky (Symmetric (rpΔ)). L
99
- L = L[end : - 1 : 1 , end : - 1 : 1 ]
100
- L * L' ≈ pΔ
91
+ U = reversecholesky (Symmetric (Δₙ)). U
101
92
102
- # Compute spectrum
103
- A = (L \ (L \ pM)' ) # = L⁻¹ pΔ L⁻ᵀ
93
+ A = (U \ (U \ Mₙ)' ) # = L⁻¹ pΔ L⁻ᵀ
104
94
e1s, e2s = eigmin (A), eigmax (A)
105
95
106
96
z = SVector .(- 1 : 0.01 : 1 , (- 1 : 0.01 : 1 )' )
107
97
108
98
# RHS
109
- f (z) = (( x,y)= z; - 2 .* sin .(pi * x) .* (2pi * y .* cos .(pi * y) .+ (1 - pi ^ 2 * y^ 2 ) .* sin .(pi * y) ))
99
+ f = (x,y) -> - 2 .* sin .(pi * x) .* (2pi * y .* cos .(pi * y) .+ (1 - pi ^ 2 * y^ 2 ) .* sin .(pi * y))
110
100
fp = analysis_2D (f, K, p) # interpolate F into P⊗P
111
- Fa = P[first .(z)[:,1 ], Block .( 1 : p) ] * fp * P[first .(z)[:,1 ], Block .( 1 : p) ]'
112
- norm (f .(z) - Fa)
101
+ Fa = P[first .(z)[:,1 ], KR ] * fp * P[first .(z)[:,1 ], KR ]'
102
+ norm (splat (f) .(z) - Fa)
113
103
114
104
# weak form for RHS
115
- F = (C' * P)[Block .(1 : p), Block .(1 : p)]* fp* ((C' * P)[Block .(1 : p), Block .(1 : p)])' # RHS <f,v>
116
- F[1 , :] .= 0 ; F[K+ 1 , :] .= 0 ; F[:, 1 ] .= 0 ; F[:, K+ 1 ] .= 0 # Dirichlet bcs
105
+ F = (Q' * P)[KR, KR]* fp* ((Q' * P)[KR, KR])' # RHS <f,v>
117
106
118
- tol = 1e-15 # ADI tolerance
119
- A, B, a, b, c, d = pM, - pM, e1s, e2s, - e2s, - e1s
120
- X = ADI (A, B, pΔ, F, a, b, c, d, tol)
107
+ A, B, a, b, c, d = Mₙ, - Mₙ, e1s, e2s, - e2s, - e1s
108
+ @time X = ADI (A, B, Δₙ, F, a, b, c, d)
121
109
122
110
# X = UΔ
123
- U = (L ' \ (L \ X' ))'
111
+ Y = (U ' \ (U \ X' ))'
124
112
125
113
u_exact = z -> ((x,y)= z; sin .(π* x)* sin .(π* y)* y^ 2 )
126
- Ua = C [first .(z)[:,1 ], Block .(1 : p)] * U * C [first .(z)[:,1 ], Block .(1 : p)]'
114
+ Ua = Q [first .(z)[:,1 ], Block .(1 : p)] * Y * Q [first .(z)[:,1 ], Block .(1 : p)]'
127
115
128
- norm ( u_exact .(z) - Ua) # ℓ^∞ error.
116
+ @test u_exact .(z) ≈ Ua # ℓ^∞ error.
129
117
130
118
"""
131
119
Via (5.3) and (5.6) of Kars' thesis.
@@ -153,7 +141,7 @@ F = (C'*P)[Block.(1:p), Block.(1:p)]*fp*((C'*P)[Block.(1:p), Block.(1:p)])' # R
153
141
F[1 , :] .= 0 ; F[K+ 1 , :] .= 0 ; F[:, 1 ] .= 0 ; F[:, K+ 1 ] .= 0 # Dirichlet bcs
154
142
155
143
tol = 1e-15 # ADI tolerance
156
- A, B, a, b, c, d = pΔ, - pΔ, e1s, e2s, - e2s, - e1s
144
+ A, B, a, b, c, d = pΔ, - pΔ, e1s, e2s, - e2s, - e1s
157
145
X = ADI (A, B, pM, F, a, b, c, d, tol)
158
146
159
147
# X = UM
0 commit comments