comparison test/testSbpOperators.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 0844069ab5ff
comparison
equal deleted inserted replaced
343:e12c9a866513 345:2fcc960836c6
1 using Test
2 using Sbplib.SbpOperators
3 using Sbplib.Grids
4 using Sbplib.RegionIndices
5 using Sbplib.LazyTensors
6
7 @testset "SbpOperators" begin
8
9 # @testset "apply_quadrature" begin
10 # op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
11 # h = 0.5
12 #
13 # @test apply_quadrature(op, h, 1.0, 10, 100) == h
14 #
15 # N = 10
16 # qc = op.quadratureClosure
17 # q = h.*(qc..., ones(N-2*closuresize(op))..., reverse(qc)...)
18 # @assert length(q) == N
19 #
20 # for i ∈ 1:N
21 # @test apply_quadrature(op, h, 1.0, i, N) == q[i]
22 # end
23 #
24 # v = [2.,3.,2.,4.,5.,4.,3.,4.,5.,4.5]
25 # for i ∈ 1:N
26 # @test apply_quadrature(op, h, v[i], i, N) == q[i]*v[i]
27 # end
28 # end
29
30 @testset "SecondDerivative" begin
31 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
32 L = 3.5
33 g = EquidistantGrid((101,), (0.0,), (L,))
34 h_inv = inverse_spacing(g)
35 h = 1/h_inv[1];
36 Dₓₓ = SecondDerivative(h_inv[1],op.innerStencil,op.closureStencils,op.parity)
37
38 f0(x::Float64) = 1.
39 f1(x::Float64) = x
40 f2(x::Float64) = 1/2*x^2
41 f3(x::Float64) = 1/6*x^3
42 f4(x::Float64) = 1/24*x^4
43 f5(x::Float64) = sin(x)
44 f5ₓₓ(x::Float64) = -f5(x)
45
46 v0 = evalOn(g,f0)
47 v1 = evalOn(g,f1)
48 v2 = evalOn(g,f2)
49 v3 = evalOn(g,f3)
50 v4 = evalOn(g,f4)
51 v5 = evalOn(g,f5)
52
53 @test Dₓₓ isa TensorOperator{T,1} where T
54 @test Dₓₓ' isa TensorMapping{T,1,1} where T
55
56 # TODO: Should perhaps set tolerance level for isapporx instead?
57 # Are these tolerance levels resonable or should tests be constructed
58 # differently?
59 equalitytol = 0.5*1e-10
60 accuracytol = 0.5*1e-3
61 # 4th order interior stencil, 2nd order boundary stencil,
62 # implies that L*v should be exact for v - monomial up to order 3.
63 # Exact differentiation is measured point-wise. For other grid functions
64 # the error is measured in the l2-norm.
65 @test all(abs.(collect(Dₓₓ*v0)) .<= equalitytol)
66 @test all(abs.(collect(Dₓₓ*v1)) .<= equalitytol)
67 @test all(abs.((collect(Dₓₓ*v2) - v0)) .<= equalitytol)
68 @test all(abs.((collect(Dₓₓ*v3) - v1)) .<= equalitytol)
69 e4 = collect(Dₓₓ*v4) - v2
70 e5 = collect(Dₓₓ*v5) + v5
71 @test sqrt(h*sum(collect(e4.^2))) <= accuracytol
72 @test sqrt(h*sum(collect(e5.^2))) <= accuracytol
73 end
74
75 @testset "Laplace2D" begin
76 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
77 Lx = 1.5
78 Ly = 3.2
79 g = EquidistantGrid((102,131), (0.0, 0.0), (Lx,Ly))
80
81 h_inv = inverse_spacing(g)
82 h = spacing(g)
83 D_xx = SecondDerivative(h_inv[1],op.innerStencil,op.closureStencils,op.parity)
84 D_yy = SecondDerivative(h_inv[2],op.innerStencil,op.closureStencils,op.parity)
85 L = Laplace((D_xx,D_yy))
86
87 f0(x::Float64,y::Float64) = 2.
88 f1(x::Float64,y::Float64) = x+y
89 f2(x::Float64,y::Float64) = 1/2*x^2 + 1/2*y^2
90 f3(x::Float64,y::Float64) = 1/6*x^3 + 1/6*y^3
91 f4(x::Float64,y::Float64) = 1/24*x^4 + 1/24*y^4
92 f5(x::Float64,y::Float64) = sin(x) + cos(y)
93 f5ₓₓ(x::Float64,y::Float64) = -f5(x,y)
94
95 v0 = evalOn(g,f0)
96 v1 = evalOn(g,f1)
97 v2 = evalOn(g,f2)
98 v3 = evalOn(g,f3)
99 v4 = evalOn(g,f4)
100 v5 = evalOn(g,f5)
101 v5ₓₓ = evalOn(g,f5ₓₓ)
102
103 @test L isa TensorOperator{T,2} where T
104 @test L' isa TensorMapping{T,2,2} where T
105
106 # TODO: Should perhaps set tolerance level for isapporx instead?
107 # Are these tolerance levels resonable or should tests be constructed
108 # differently?
109 equalitytol = 0.5*1e-10
110 accuracytol = 0.5*1e-3
111 # 4th order interior stencil, 2nd order boundary stencil,
112 # implies that L*v should be exact for v - monomial up to order 3.
113 # Exact differentiation is measured point-wise. For other grid functions
114 # the error is measured in the H-norm.
115 @test all(abs.(collect(L*v0)) .<= equalitytol)
116 @test all(abs.(collect(L*v1)) .<= equalitytol)
117 @test all(collect(L*v2) .≈ v0) # Seems to be more accurate
118 @test all(abs.((collect(L*v3) - v1)) .<= equalitytol)
119 e4 = collect(L*v4) - v2
120 e5 = collect(L*v5) - v5ₓₓ
121 @test sqrt(prod(h)*sum(collect(e4.^2))) <= accuracytol
122 @test sqrt(prod(h)*sum(collect(e5.^2))) <= accuracytol
123 end
124
125 @testset "DiagonalInnerProduct" begin
126 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
127 L = 2.3
128 g = EquidistantGrid((77,), (0.0,), (L,))
129 h = spacing(g)
130 H = DiagonalInnerProduct(h[1],op.quadratureClosure)
131 v = ones(Float64, size(g))
132
133 @test H isa TensorOperator{T,1} where T
134 @test H' isa TensorMapping{T,1,1} where T
135 @test sum(collect(H*v)) ≈ L
136 @test collect(H*v) == collect(H'*v)
137 end
138
139 @testset "Quadrature" begin
140 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
141 Lx = 2.3
142 Ly = 5.2
143 g = EquidistantGrid((77,66), (0.0, 0.0), (Lx,Ly))
144
145 h = spacing(g)
146 Hx = DiagonalInnerProduct(h[1],op.quadratureClosure);
147 Hy = DiagonalInnerProduct(h[2],op.quadratureClosure);
148 Q = Quadrature((Hx,Hy))
149
150 v = ones(Float64, size(g))
151
152 @test Q isa TensorOperator{T,2} where T
153 @test Q' isa TensorMapping{T,2,2} where T
154 @test sum(collect(Q*v)) ≈ (Lx*Ly)
155 @test collect(Q*v) == collect(Q'*v)
156 end
157
158 @testset "InverseDiagonalInnerProduct" begin
159 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
160 L = 2.3
161 g = EquidistantGrid((77,), (0.0,), (L,))
162 h = spacing(g)
163 H = DiagonalInnerProduct(h[1],op.quadratureClosure)
164
165 h_i = inverse_spacing(g)
166 Hi = InverseDiagonalInnerProduct(h_i[1],1 ./ op.quadratureClosure)
167 v = evalOn(g, x->sin(x))
168
169 @test Hi isa TensorOperator{T,1} where T
170 @test Hi' isa TensorMapping{T,1,1} where T
171 @test collect(Hi*H*v) ≈ v
172 @test collect(Hi*v) == collect(Hi'*v)
173 end
174
175 @testset "InverseQuadrature" begin
176 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
177 Lx = 7.3
178 Ly = 8.2
179 g = EquidistantGrid((77,66), (0.0, 0.0), (Lx,Ly))
180
181 h = spacing(g)
182 Hx = DiagonalInnerProduct(h[1], op.quadratureClosure);
183 Hy = DiagonalInnerProduct(h[2], op.quadratureClosure);
184 Q = Quadrature((Hx,Hy))
185
186 hi = inverse_spacing(g)
187 Hix = InverseDiagonalInnerProduct(hi[1], 1 ./ op.quadratureClosure);
188 Hiy = InverseDiagonalInnerProduct(hi[2], 1 ./ op.quadratureClosure);
189 Qinv = InverseQuadrature((Hix,Hiy))
190 v = evalOn(g, (x,y)-> x^2 + (y-1)^2 + x*y)
191
192 @test Qinv isa TensorOperator{T,2} where T
193 @test Qinv' isa TensorMapping{T,2,2} where T
194 @test collect(Qinv*Q*v) ≈ v
195 @test collect(Qinv*v) == collect(Qinv'*v)
196 end
197 #
198 # @testset "BoundaryValue" begin
199 # op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
200 # g = EquidistantGrid((4,5), (0.0, 0.0), (1.0,1.0))
201 #
202 # e_w = BoundaryValue(op, g, CartesianBoundary{1,Lower}())
203 # e_e = BoundaryValue(op, g, CartesianBoundary{1,Upper}())
204 # e_s = BoundaryValue(op, g, CartesianBoundary{2,Lower}())
205 # e_n = BoundaryValue(op, g, CartesianBoundary{2,Upper}())
206 #
207 # v = zeros(Float64, 4, 5)
208 # v[:,5] = [1, 2, 3,4]
209 # v[:,4] = [1, 2, 3,4]
210 # v[:,3] = [4, 5, 6, 7]
211 # v[:,2] = [7, 8, 9, 10]
212 # v[:,1] = [10, 11, 12, 13]
213 #
214 # @test e_w isa TensorMapping{T,2,1} where T
215 # @test e_w' isa TensorMapping{T,1,2} where T
216 #
217 # @test domain_size(e_w, (3,2)) == (2,)
218 # @test domain_size(e_e, (3,2)) == (2,)
219 # @test domain_size(e_s, (3,2)) == (3,)
220 # @test domain_size(e_n, (3,2)) == (3,)
221 #
222 # @test size(e_w'*v) == (5,)
223 # @test size(e_e'*v) == (5,)
224 # @test size(e_s'*v) == (4,)
225 # @test size(e_n'*v) == (4,)
226 #
227 # @test collect(e_w'*v) == [10,7,4,1.0,1]
228 # @test collect(e_e'*v) == [13,10,7,4,4.0]
229 # @test collect(e_s'*v) == [10,11,12,13.0]
230 # @test collect(e_n'*v) == [1,2,3,4.0]
231 #
232 # g_x = [1,2,3,4.0]
233 # g_y = [5,4,3,2,1.0]
234 #
235 # G_w = zeros(Float64, (4,5))
236 # G_w[1,:] = g_y
237 #
238 # G_e = zeros(Float64, (4,5))
239 # G_e[4,:] = g_y
240 #
241 # G_s = zeros(Float64, (4,5))
242 # G_s[:,1] = g_x
243 #
244 # G_n = zeros(Float64, (4,5))
245 # G_n[:,5] = g_x
246 #
247 # @test size(e_w*g_y) == (UnknownDim,5)
248 # @test size(e_e*g_y) == (UnknownDim,5)
249 # @test size(e_s*g_x) == (4,UnknownDim)
250 # @test size(e_n*g_x) == (4,UnknownDim)
251 #
252 # # These tests should be moved to where they are possible (i.e we know what the grid should be)
253 # @test_broken collect(e_w*g_y) == G_w
254 # @test_broken collect(e_e*g_y) == G_e
255 # @test_broken collect(e_s*g_x) == G_s
256 # @test_broken collect(e_n*g_x) == G_n
257 # end
258 #
259 # @testset "NormalDerivative" begin
260 # op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
261 # g = EquidistantGrid((5,6), (0.0, 0.0), (4.0,5.0))
262 #
263 # d_w = NormalDerivative(op, g, CartesianBoundary{1,Lower}())
264 # d_e = NormalDerivative(op, g, CartesianBoundary{1,Upper}())
265 # d_s = NormalDerivative(op, g, CartesianBoundary{2,Lower}())
266 # d_n = NormalDerivative(op, g, CartesianBoundary{2,Upper}())
267 #
268 #
269 # v = evalOn(g, (x,y)-> x^2 + (y-1)^2 + x*y)
270 # v∂x = evalOn(g, (x,y)-> 2*x + y)
271 # v∂y = evalOn(g, (x,y)-> 2*(y-1) + x)
272 #
273 # @test d_w isa TensorMapping{T,2,1} where T
274 # @test d_w' isa TensorMapping{T,1,2} where T
275 #
276 # @test domain_size(d_w, (3,2)) == (2,)
277 # @test domain_size(d_e, (3,2)) == (2,)
278 # @test domain_size(d_s, (3,2)) == (3,)
279 # @test domain_size(d_n, (3,2)) == (3,)
280 #
281 # @test size(d_w'*v) == (6,)
282 # @test size(d_e'*v) == (6,)
283 # @test size(d_s'*v) == (5,)
284 # @test size(d_n'*v) == (5,)
285 #
286 # @test collect(d_w'*v) ≈ v∂x[1,:]
287 # @test collect(d_e'*v) ≈ v∂x[5,:]
288 # @test collect(d_s'*v) ≈ v∂y[:,1]
289 # @test collect(d_n'*v) ≈ v∂y[:,6]
290 #
291 #
292 # d_x_l = zeros(Float64, 5)
293 # d_x_u = zeros(Float64, 5)
294 # for i ∈ eachindex(d_x_l)
295 # d_x_l[i] = op.dClosure[i-1]
296 # d_x_u[i] = -op.dClosure[length(d_x_u)-i]
297 # end
298 #
299 # d_y_l = zeros(Float64, 6)
300 # d_y_u = zeros(Float64, 6)
301 # for i ∈ eachindex(d_y_l)
302 # d_y_l[i] = op.dClosure[i-1]
303 # d_y_u[i] = -op.dClosure[length(d_y_u)-i]
304 # end
305 #
306 # function prod_matrix(x,y)
307 # G = zeros(Float64, length(x), length(y))
308 # for I ∈ CartesianIndices(G)
309 # G[I] = x[I[1]]*y[I[2]]
310 # end
311 #
312 # return G
313 # end
314 #
315 # g_x = [1,2,3,4.0,5]
316 # g_y = [5,4,3,2,1.0,11]
317 #
318 # G_w = prod_matrix(d_x_l, g_y)
319 # G_e = prod_matrix(d_x_u, g_y)
320 # G_s = prod_matrix(g_x, d_y_l)
321 # G_n = prod_matrix(g_x, d_y_u)
322 #
323 #
324 # @test size(d_w*g_y) == (UnknownDim,6)
325 # @test size(d_e*g_y) == (UnknownDim,6)
326 # @test size(d_s*g_x) == (5,UnknownDim)
327 # @test size(d_n*g_x) == (5,UnknownDim)
328 #
329 # # These tests should be moved to where they are possible (i.e we know what the grid should be)
330 # @test_broken collect(d_w*g_y) ≈ G_w
331 # @test_broken collect(d_e*g_y) ≈ G_e
332 # @test_broken collect(d_s*g_x) ≈ G_s
333 # @test_broken collect(d_n*g_x) ≈ G_n
334 # end
335 #
336 # @testset "BoundaryQuadrature" begin
337 # op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
338 # g = EquidistantGrid((10,11), (0.0, 0.0), (1.0,1.0))
339 #
340 # H_w = BoundaryQuadrature(op, g, CartesianBoundary{1,Lower}())
341 # H_e = BoundaryQuadrature(op, g, CartesianBoundary{1,Upper}())
342 # H_s = BoundaryQuadrature(op, g, CartesianBoundary{2,Lower}())
343 # H_n = BoundaryQuadrature(op, g, CartesianBoundary{2,Upper}())
344 #
345 # v = evalOn(g, (x,y)-> x^2 + (y-1)^2 + x*y)
346 #
347 # function get_quadrature(N)
348 # qc = op.quadratureClosure
349 # q = (qc..., ones(N-2*closuresize(op))..., reverse(qc)...)
350 # @assert length(q) == N
351 # return q
352 # end
353 #
354 # v_w = v[1,:]
355 # v_e = v[10,:]
356 # v_s = v[:,1]
357 # v_n = v[:,11]
358 #
359 # q_x = spacing(g)[1].*get_quadrature(10)
360 # q_y = spacing(g)[2].*get_quadrature(11)
361 #
362 # @test H_w isa TensorOperator{T,1} where T
363 #
364 # @test domain_size(H_w, (3,)) == (3,)
365 # @test domain_size(H_n, (3,)) == (3,)
366 #
367 # @test range_size(H_w, (3,)) == (3,)
368 # @test range_size(H_n, (3,)) == (3,)
369 #
370 # @test size(H_w*v_w) == (11,)
371 # @test size(H_e*v_e) == (11,)
372 # @test size(H_s*v_s) == (10,)
373 # @test size(H_n*v_n) == (10,)
374 #
375 # @test collect(H_w*v_w) ≈ q_y.*v_w
376 # @test collect(H_e*v_e) ≈ q_y.*v_e
377 # @test collect(H_s*v_s) ≈ q_x.*v_s
378 # @test collect(H_n*v_n) ≈ q_x.*v_n
379 #
380 # @test collect(H_w'*v_w) == collect(H_w'*v_w)
381 # @test collect(H_e'*v_e) == collect(H_e'*v_e)
382 # @test collect(H_s'*v_s) == collect(H_s'*v_s)
383 # @test collect(H_n'*v_n) == collect(H_n'*v_n)
384 # end
385
386 end