comparison test/testDiffOps.jl @ 345:2fcc960836c6

Merge branch refactor/combine_to_one_package.
author Jonatan Werpers <jonatan@werpers.com>
date Sat, 26 Sep 2020 15:22:13 +0200
parents 2b0c9b30ea3b
children ffddaf053085
comparison
equal deleted inserted replaced
343:e12c9a866513 345:2fcc960836c6
1 using Test
2 using Sbplib.DiffOps
3 using Sbplib.Grids
4 using Sbplib.SbpOperators
5 using Sbplib.RegionIndices
6 using Sbplib.LazyTensors
7
8 @testset "DiffOps" begin
9
10 @testset "Laplace2D" begin
11 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
12 Lx = 3.5
13 Ly = 7.2
14 g = EquidistantGrid((42,41), (0.0, 0.0), (Lx,Ly))
15 L = Laplace(g, 1., op)
16 H = quadrature(L)
17
18 f0(x::Float64,y::Float64) = 2.
19 f1(x::Float64,y::Float64) = x+y
20 f2(x::Float64,y::Float64) = 1/2*x^2 + 1/2*y^2
21 f3(x::Float64,y::Float64) = 1/6*x^3 + 1/6*y^3
22 f4(x::Float64,y::Float64) = 1/24*x^4 + 1/24*y^4
23 f5(x::Float64,y::Float64) = sin(x) + cos(y)
24 f5ₓₓ(x::Float64,y::Float64) = -f5(x,y)
25
26 v0 = evalOn(g,f0)
27 v1 = evalOn(g,f1)
28 v2 = evalOn(g,f2)
29 v3 = evalOn(g,f3)
30 v4 = evalOn(g,f4)
31 v5 = evalOn(g,f5)
32 v5ₓₓ = evalOn(g,f5ₓₓ)
33
34 @test L isa TensorOperator{T,2} where T
35 @test L' isa TensorMapping{T,2,2} where T
36
37 # TODO: Should perhaps set tolerance level for isapporx instead?
38 # Are these tolerance levels resonable or should tests be constructed
39 # differently?
40 equalitytol = 0.5*1e-10
41 accuracytol = 0.5*1e-3
42 # 4th order interior stencil, 2nd order boundary stencil,
43 # implies that L*v should be exact for v - monomial up to order 3.
44 # Exact differentiation is measured point-wise. For other grid functions
45 # the error is measured in the H-norm.
46 @test all(abs.(collect(L*v0)) .<= equalitytol)
47 @test all(abs.(collect(L*v1)) .<= equalitytol)
48 @test all(collect(L*v2) .≈ v0) # Seems to be more accurate
49 @test all(abs.((collect(L*v3) - v1)) .<= equalitytol)
50 e4 = collect(L*v4) - v2
51 e5 = collect(L*v5) - v5ₓₓ
52 @test sum(collect(H*e4.^2)) <= accuracytol
53 @test sum(collect(H*e5.^2)) <= accuracytol
54 end
55
56 @testset "Quadrature" begin
57 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
58 Lx = 2.3
59 Ly = 5.2
60 g = EquidistantGrid((77,66), (0.0, 0.0), (Lx,Ly))
61 H = Quadrature(op,g)
62 v = ones(Float64, size(g))
63
64 @test H isa TensorOperator{T,2} where T
65 @test H' isa TensorMapping{T,2,2} where T
66 @test sum(collect(H*v)) ≈ (Lx*Ly)
67 @test collect(H*v) == collect(H'*v)
68 end
69
70 @testset "InverseQuadrature" begin
71 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
72 Lx = 7.3
73 Ly = 8.2
74 g = EquidistantGrid((77,66), (0.0, 0.0), (Lx,Ly))
75 H = Quadrature(op,g)
76 Hinv = InverseQuadrature(op,g)
77 v = evalOn(g, (x,y)-> x^2 + (y-1)^2 + x*y)
78
79 @test Hinv isa TensorOperator{T,2} where T
80 @test Hinv' isa TensorMapping{T,2,2} where T
81 @test collect(Hinv*H*v) ≈ v
82 @test collect(Hinv*v) == collect(Hinv'*v)
83 end
84
85 @testset "BoundaryValue" begin
86 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
87 g = EquidistantGrid((4,5), (0.0, 0.0), (1.0,1.0))
88
89 e_w = BoundaryValue(op, g, CartesianBoundary{1,Lower}())
90 e_e = BoundaryValue(op, g, CartesianBoundary{1,Upper}())
91 e_s = BoundaryValue(op, g, CartesianBoundary{2,Lower}())
92 e_n = BoundaryValue(op, g, CartesianBoundary{2,Upper}())
93
94 v = zeros(Float64, 4, 5)
95 v[:,5] = [1, 2, 3,4]
96 v[:,4] = [1, 2, 3,4]
97 v[:,3] = [4, 5, 6, 7]
98 v[:,2] = [7, 8, 9, 10]
99 v[:,1] = [10, 11, 12, 13]
100
101 @test e_w isa TensorMapping{T,2,1} where T
102 @test e_w' isa TensorMapping{T,1,2} where T
103
104 @test domain_size(e_w, (3,2)) == (2,)
105 @test domain_size(e_e, (3,2)) == (2,)
106 @test domain_size(e_s, (3,2)) == (3,)
107 @test domain_size(e_n, (3,2)) == (3,)
108
109 @test size(e_w'*v) == (5,)
110 @test size(e_e'*v) == (5,)
111 @test size(e_s'*v) == (4,)
112 @test size(e_n'*v) == (4,)
113
114 @test collect(e_w'*v) == [10,7,4,1.0,1]
115 @test collect(e_e'*v) == [13,10,7,4,4.0]
116 @test collect(e_s'*v) == [10,11,12,13.0]
117 @test collect(e_n'*v) == [1,2,3,4.0]
118
119 g_x = [1,2,3,4.0]
120 g_y = [5,4,3,2,1.0]
121
122 G_w = zeros(Float64, (4,5))
123 G_w[1,:] = g_y
124
125 G_e = zeros(Float64, (4,5))
126 G_e[4,:] = g_y
127
128 G_s = zeros(Float64, (4,5))
129 G_s[:,1] = g_x
130
131 G_n = zeros(Float64, (4,5))
132 G_n[:,5] = g_x
133
134 @test size(e_w*g_y) == (UnknownDim,5)
135 @test size(e_e*g_y) == (UnknownDim,5)
136 @test size(e_s*g_x) == (4,UnknownDim)
137 @test size(e_n*g_x) == (4,UnknownDim)
138
139 # These tests should be moved to where they are possible (i.e we know what the grid should be)
140 @test_broken collect(e_w*g_y) == G_w
141 @test_broken collect(e_e*g_y) == G_e
142 @test_broken collect(e_s*g_x) == G_s
143 @test_broken collect(e_n*g_x) == G_n
144 end
145
146 @testset "NormalDerivative" begin
147 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
148 g = EquidistantGrid((5,6), (0.0, 0.0), (4.0,5.0))
149
150 d_w = NormalDerivative(op, g, CartesianBoundary{1,Lower}())
151 d_e = NormalDerivative(op, g, CartesianBoundary{1,Upper}())
152 d_s = NormalDerivative(op, g, CartesianBoundary{2,Lower}())
153 d_n = NormalDerivative(op, g, CartesianBoundary{2,Upper}())
154
155
156 v = evalOn(g, (x,y)-> x^2 + (y-1)^2 + x*y)
157 v∂x = evalOn(g, (x,y)-> 2*x + y)
158 v∂y = evalOn(g, (x,y)-> 2*(y-1) + x)
159
160 @test d_w isa TensorMapping{T,2,1} where T
161 @test d_w' isa TensorMapping{T,1,2} where T
162
163 @test domain_size(d_w, (3,2)) == (2,)
164 @test domain_size(d_e, (3,2)) == (2,)
165 @test domain_size(d_s, (3,2)) == (3,)
166 @test domain_size(d_n, (3,2)) == (3,)
167
168 @test size(d_w'*v) == (6,)
169 @test size(d_e'*v) == (6,)
170 @test size(d_s'*v) == (5,)
171 @test size(d_n'*v) == (5,)
172
173 @test collect(d_w'*v) ≈ v∂x[1,:]
174 @test collect(d_e'*v) ≈ v∂x[5,:]
175 @test collect(d_s'*v) ≈ v∂y[:,1]
176 @test collect(d_n'*v) ≈ v∂y[:,6]
177
178
179 d_x_l = zeros(Float64, 5)
180 d_x_u = zeros(Float64, 5)
181 for i ∈ eachindex(d_x_l)
182 d_x_l[i] = op.dClosure[i-1]
183 d_x_u[i] = -op.dClosure[length(d_x_u)-i]
184 end
185
186 d_y_l = zeros(Float64, 6)
187 d_y_u = zeros(Float64, 6)
188 for i ∈ eachindex(d_y_l)
189 d_y_l[i] = op.dClosure[i-1]
190 d_y_u[i] = -op.dClosure[length(d_y_u)-i]
191 end
192
193 function prod_matrix(x,y)
194 G = zeros(Float64, length(x), length(y))
195 for I ∈ CartesianIndices(G)
196 G[I] = x[I[1]]*y[I[2]]
197 end
198
199 return G
200 end
201
202 g_x = [1,2,3,4.0,5]
203 g_y = [5,4,3,2,1.0,11]
204
205 G_w = prod_matrix(d_x_l, g_y)
206 G_e = prod_matrix(d_x_u, g_y)
207 G_s = prod_matrix(g_x, d_y_l)
208 G_n = prod_matrix(g_x, d_y_u)
209
210
211 @test size(d_w*g_y) == (UnknownDim,6)
212 @test size(d_e*g_y) == (UnknownDim,6)
213 @test size(d_s*g_x) == (5,UnknownDim)
214 @test size(d_n*g_x) == (5,UnknownDim)
215
216 # These tests should be moved to where they are possible (i.e we know what the grid should be)
217 @test_broken collect(d_w*g_y) ≈ G_w
218 @test_broken collect(d_e*g_y) ≈ G_e
219 @test_broken collect(d_s*g_x) ≈ G_s
220 @test_broken collect(d_n*g_x) ≈ G_n
221 end
222
223 @testset "BoundaryQuadrature" begin
224 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
225 g = EquidistantGrid((10,11), (0.0, 0.0), (1.0,1.0))
226
227 H_w = BoundaryQuadrature(op, g, CartesianBoundary{1,Lower}())
228 H_e = BoundaryQuadrature(op, g, CartesianBoundary{1,Upper}())
229 H_s = BoundaryQuadrature(op, g, CartesianBoundary{2,Lower}())
230 H_n = BoundaryQuadrature(op, g, CartesianBoundary{2,Upper}())
231
232 v = evalOn(g, (x,y)-> x^2 + (y-1)^2 + x*y)
233
234 function get_quadrature(N)
235 qc = op.quadratureClosure
236 q = (qc..., ones(N-2*closuresize(op))..., reverse(qc)...)
237 @assert length(q) == N
238 return q
239 end
240
241 v_w = v[1,:]
242 v_e = v[10,:]
243 v_s = v[:,1]
244 v_n = v[:,11]
245
246 q_x = spacing(g)[1].*get_quadrature(10)
247 q_y = spacing(g)[2].*get_quadrature(11)
248
249 @test H_w isa TensorOperator{T,1} where T
250
251 @test domain_size(H_w, (3,)) == (3,)
252 @test domain_size(H_n, (3,)) == (3,)
253
254 @test range_size(H_w, (3,)) == (3,)
255 @test range_size(H_n, (3,)) == (3,)
256
257 @test size(H_w*v_w) == (11,)
258 @test size(H_e*v_e) == (11,)
259 @test size(H_s*v_s) == (10,)
260 @test size(H_n*v_n) == (10,)
261
262 @test collect(H_w*v_w) ≈ q_y.*v_w
263 @test collect(H_e*v_e) ≈ q_y.*v_e
264 @test collect(H_s*v_s) ≈ q_x.*v_s
265 @test collect(H_n*v_n) ≈ q_x.*v_n
266
267 @test collect(H_w'*v_w) == collect(H_w'*v_w)
268 @test collect(H_e'*v_e) == collect(H_e'*v_e)
269 @test collect(H_s'*v_s) == collect(H_s'*v_s)
270 @test collect(H_n'*v_n) == collect(H_n'*v_n)
271 end
272
273 end