This repository was archived by the owner on Jul 19, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathrobin.jl
More file actions
218 lines (166 loc) · 7.09 KB
/
robin.jl
File metadata and controls
218 lines (166 loc) · 7.09 KB
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
using LinearAlgebra, DiffEqOperators, Random, Test
# Generate random parameters
al = rand(5)
bl = rand(5)
cl = rand(5)
dx = rand(5)
ar = rand(5)
br = rand(5)
cr = rand(5)
# Construct 5 arbitrary RobinBC operators
for i in 1:5
Q = RobinBC((al[i], bl[i], cl[i]), (ar[i], br[i], cr[i]), dx[i])
Q_L, Q_b = Array(Q,5i)
#Check that Q_L is is correctly computed
@test Q_L[2:5i+1,1:5i] ≈ Array(I, 5i, 5i)
@test Q_L[1,:] ≈ [1 / (1-al[i]*dx[i]/bl[i]); zeros(5i-1)]
@test Q_L[5i+2,:] ≈ [zeros(5i-1); 1 / (1+ar[i]*dx[i]/br[i])]
#Check that Q_b is computed correctly
@test Q_b ≈ [cl[i]/(al[i]-bl[i]/dx[i]); zeros(5i); cr[i]/(ar[i]+br[i]/dx[i])]
# Construct the extended operator and check that it correctly extends u to a (5i+2)
# vector, along with encoding boundary condition information.
u = rand(5i)
Qextended = Q*u
CorrectQextended = [(cl[i]-(bl[i]/dx[i])*u[1])/(al[i]-bl[i]/dx[i]); u; (cr[i]+ (br[i]/dx[i])*u[5i])/(ar[i]+br[i]/dx[i])]
@test length(Qextended) ≈ 5i+2
# Check concretization
@test Array(Qextended) ≈ CorrectQextended
# Check that Q_L and Q_b correctly compute BoundaryPaddedVector
@test Q_L*u + Q_b ≈ CorrectQextended
@test [Qextended[1]; Qextended.u; Qextended[5i+2]] ≈ CorrectQextended
end
# Construct 5 arbitrary RobinBC operators w/non-uniform grid
al = rand(5)
bl = rand(5)
cl = rand(5)
dx = rand(5)
ar = rand(5)
br = rand(5)
cr = rand(5)
for j in 1:2
for i in 1:5
if j == 1
Q = RobinBC((al[i], bl[i], cl[i]), (ar[i], br[i], cr[i]),
dx[i] .* ones(5 * i))
else
Q = RobinBC([al[i], bl[i], cl[i]], [ar[i], br[i], cr[i]],
dx[i] .* ones(5 * i))
end
Q_L, Q_b = Array(Q,5i)
#Check that Q_L is is correctly computed
@test Q_L[2:5i+1,1:5i] ≈ Array(I, 5i, 5i)
@test Q_L[1,:] ≈ [1 / (1-al[i]*dx[i]/bl[i]); zeros(5i-1)]
@test Q_L[5i+2,:] ≈ [zeros(5i-1); 1 / (1+ar[i]*dx[i]/br[i])]
#Check that Q_b is computed correctly
@test Q_b ≈ [cl[i]/(al[i]-bl[i]/dx[i]); zeros(5i); cr[i]/(ar[i]+br[i]/dx[i])]
# Construct the extended operator and check that it correctly extends u to a (5i+2)
# vector, along with encoding boundary condition information.
u = rand(5i)
Qextended = Q*u
CorrectQextended = [(cl[i]-(bl[i]/dx[i])*u[1])/(al[i]-bl[i]/dx[i]); u; (cr[i]+ (br[i]/dx[i])*u[5i])/(ar[i]+br[i]/dx[i])]
@test length(Qextended) ≈ 5i+2
# Check concretization
@test Array(Qextended) ≈ CorrectQextended
# Check that Q_L and Q_b correctly compute BoundaryPaddedVector
@test Q_L*u + Q_b ≈ CorrectQextended
@test [Qextended[1]; Qextended.u; Qextended[5i+2]] ≈ CorrectQextended
end
end
#3rd order RobinBC, calculated with left stencil [-11/6 3 -3/2 1/3], right stencil [-1/3 3/2 -3 11/6] and [α,β,γ] = [1 6 10]
u0 = -4/10
uend = 125/12
u = Vector(1.0:10.0)
Q = RobinBC((1.0, 6.0, 10.0), (1.0, 6.0, 10.0), 1.0, 3)
urobinextended = Q*u
@test urobinextended.l ≈ u0
@test urobinextended.r ≈ uend
# General BC should be equivalent
G = GeneralBC([-10.0, 1.0, 6.0], [-10.0, 1.0, 6.0], 1.0, 3)
ugeneralextended = G*u
@test ugeneralextended.l ≈ u0
@test ugeneralextended.r ≈ uend
#TODO: Implement tests for BC's that are contingent on the sign of the coefficient on the operator near the boundary
# Test complex Robin BC, uniform grid
# Generate random parameters
al = rand(ComplexF64,5)
bl = rand(ComplexF64,5)
cl = rand(ComplexF64,5)
dx = rand(Float64,5)
ar = rand(ComplexF64,5)
br = rand(ComplexF64,5)
cr = rand(ComplexF64,5)
# Construct 5 arbitrary RobinBC operators for each parameter set
for i in 1:5
Q = RobinBC((al[i], bl[i], cl[i]), (ar[i], br[i], cr[i]), dx[i])
Q_L, Q_b = Array(Q,5i)
#Check that Q_L is is correctly computed
@test Q_L[2:5i+1,1:5i] ≈ Array(I, 5i, 5i)
@test Q_L[1,:] ≈ [1 / (1-al[i]*dx[i]/bl[i]); zeros(5i-1)]
@test Q_L[5i+2,:] ≈ [zeros(5i-1); 1 / (1+ar[i]*dx[i]/br[i])]
#Check that Q_b is computed correctly
@test Q_b ≈ [cl[i]/(al[i]-bl[i]/dx[i]); zeros(5i); cr[i]/(ar[i]+br[i]/dx[i])]
# Construct the extended operator and check that it correctly extends u to a (5i+2)
# vector, along with encoding boundary condition information.
u = rand(ComplexF64,5i)
Qextended = Q*u
CorrectQextended = [(cl[i]-(bl[i]/dx[i])*u[1])/(al[i]-bl[i]/dx[i]); u; (cr[i]+ (br[i]/dx[i])*u[5i])/(ar[i]+br[i]/dx[i])]
@test length(Qextended) ≈ 5i+2
# Check concretization
@test Array(Qextended) ≈ CorrectQextended # Q.a_l ⋅ u[1:length(Q.a_l)] + Q.b_l, Q.a_r ⋅ u[(end-length(Q.a_r)+1):end] + Q.b_r
# Check that Q_L and Q_b correctly compute BoundaryPaddedVector
@test Q_L*u + Q_b ≈ CorrectQextended
@test [Qextended[1]; Qextended.u; Qextended[5i+2]] ≈ CorrectQextended
end
# Test complex Robin BC, w/non-uniform grid
al = rand(ComplexF64,5)
bl = rand(ComplexF64,5)
cl = rand(ComplexF64,5)
dx = rand(Float64,5)
ar = rand(ComplexF64,5)
br = rand(ComplexF64,5)
cr = rand(ComplexF64,5)
for j in 1:2
for i in 1:5
if j == 1
Q = RobinBC((al[i], bl[i], cl[i]), (ar[i], br[i], cr[i]),
dx[i] .* ones(5 * i))
else
Q = RobinBC([al[i], bl[i], cl[i]], [ar[i], br[i], cr[i]],
dx[i] .* ones(5 * i))
end
Q_L, Q_b = Array(Q,5i)
#Check that Q_L is is correctly computed
@test Q_L[2:5i+1,1:5i] ≈ Array(I, 5i, 5i)
@test Q_L[1,:] ≈ [1 / (1-al[i]*dx[i]/bl[i]); zeros(5i-1)]
@test Q_L[5i+2,:] ≈ [zeros(5i-1); 1 / (1+ar[i]*dx[i]/br[i])]
#Check that Q_b is computed correctly
@test Q_b ≈ [cl[i]/(al[i]-bl[i]/dx[i]); zeros(5i); cr[i]/(ar[i]+br[i]/dx[i])]
# Construct the extended operator and check that it correctly extends u to a (5i+2)
# vector, along with encoding boundary condition information.
u = rand(ComplexF64,5i)
Qextended = Q*u
CorrectQextended = [(cl[i]-(bl[i]/dx[i])*u[1])/(al[i]-bl[i]/dx[i]); u; (cr[i]+ (br[i]/dx[i])*u[5i])/(ar[i]+br[i]/dx[i])]
@test length(Qextended) ≈ 5i+2
# Check concretization
@test Array(Qextended) ≈ CorrectQextended
# Check that Q_L and Q_b correctly compute BoundaryPaddedVector
@test Q_L*u + Q_b ≈ CorrectQextended
@test [Qextended[1]; Qextended.u; Qextended[5i+2]] ≈ CorrectQextended
end
end
# Test NeumannBC, DirichletBC as special cases of RobinBC
let
dx = [0.121, 0.783, 0.317, 0.518, 0.178]
αC = (0.539 + 0.653im, 0.842 + 0.47im)
αR = (0.045, 0.577)
@test NeumannBC(αC, dx).b_l ≈ -0.065219 - 0.079013im
@test DirichletBC(αR...).b_r ≈ 0.577
@test DirichletBC(Float64, αC...).b_l ≈ 0.539 + 0.653im
@test DirichletBC(Float64, αC...).a_r ≈ [-0.0 + 0.0im, 0.0 + 0.0im]
@test Dirichlet0BC(Float64).a_r ≈ [-0.0,0.0]
@test Neumann0BC(dx).a_r ≈ [0.3436293436293436]
@test Neumann0BC(ComplexF64,dx).a_l ≈ [0.15453384418901658 + 0.0im]
@test NeumannBC(αC, first(dx)).b_r ≈ 0.101882 + 0.05687im
@test Neumann0BC(first(dx)).a_r ≈ [1.0 - 0.0im]
@test Neumann0BC(ComplexF64,first(dx)).a_l ≈ [1.0 + 0.0im]
end