comparison test/testSbpOperators.jl @ 404:48d57f185f86

Merge in refactor/sbp_operators_tests/collect_and_compare
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 07 Oct 2020 12:33:19 +0200
parents 3cecbfb3d623
children 21fba50cb5b0 547639572208 4aa7fe13a984
comparison
equal deleted inserted replaced
402:1936e38fe51e 404:48d57f185f86
1 using Test 1 using Test
2 using Sbplib.SbpOperators 2 using Sbplib.SbpOperators
3 using Sbplib.Grids 3 using Sbplib.Grids
4 using Sbplib.RegionIndices 4 using Sbplib.RegionIndices
5 using Sbplib.LazyTensors 5 using Sbplib.LazyTensors
6 6 using LinearAlgebra
7 # TODO: Remove collects for all the tests with TensorApplications
8 7
9 @testset "SbpOperators" begin 8 @testset "SbpOperators" begin
10 9
11 # @testset "apply_quadrature" begin 10 # @testset "apply_quadrature" begin
12 # op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") 11 # op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
30 # end 29 # end
31 30
32 @testset "SecondDerivative" begin 31 @testset "SecondDerivative" begin
33 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") 32 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
34 L = 3.5 33 L = 3.5
35 g = EquidistantGrid((101,), (0.0,), (L,)) 34 g = EquidistantGrid(101, 0.0, L)
36 h_inv = inverse_spacing(g)
37 h = 1/h_inv[1];
38 Dₓₓ = SecondDerivative(g,op.innerStencil,op.closureStencils) 35 Dₓₓ = SecondDerivative(g,op.innerStencil,op.closureStencils)
39 36
40 f0(x::Float64) = 1. 37 f0(x) = 1.
41 f1(x::Float64) = x 38 f1(x) = x
42 f2(x::Float64) = 1/2*x^2 39 f2(x) = 1/2*x^2
43 f3(x::Float64) = 1/6*x^3 40 f3(x) = 1/6*x^3
44 f4(x::Float64) = 1/24*x^4 41 f4(x) = 1/24*x^4
45 f5(x::Float64) = sin(x) 42 f5(x) = sin(x)
46 f5ₓₓ(x::Float64) = -f5(x) 43 f5ₓₓ(x) = -f5(x)
47 44
48 v0 = evalOn(g,f0) 45 v0 = evalOn(g,f0)
49 v1 = evalOn(g,f1) 46 v1 = evalOn(g,f1)
50 v2 = evalOn(g,f2) 47 v2 = evalOn(g,f2)
51 v3 = evalOn(g,f3) 48 v3 = evalOn(g,f3)
53 v5 = evalOn(g,f5) 50 v5 = evalOn(g,f5)
54 51
55 @test Dₓₓ isa TensorMapping{T,1,1} where T 52 @test Dₓₓ isa TensorMapping{T,1,1} where T
56 @test Dₓₓ' isa TensorMapping{T,1,1} where T 53 @test Dₓₓ' isa TensorMapping{T,1,1} where T
57 54
58 # TODO: Should perhaps set tolerance level for isapporx instead?
59 # Are these tolerance levels resonable or should tests be constructed
60 # differently?
61 equalitytol = 0.5*1e-10
62 accuracytol = 0.5*1e-3
63 # 4th order interior stencil, 2nd order boundary stencil, 55 # 4th order interior stencil, 2nd order boundary stencil,
64 # implies that L*v should be exact for v - monomial up to order 3. 56 # implies that L*v should be exact for v - monomial up to order 3.
65 # Exact differentiation is measured point-wise. For other grid functions 57 # Exact differentiation is measured point-wise. For other grid functions
66 # the error is measured in the l2-norm. 58 # the error is measured in the l2-norm.
67 @test all(abs.(collect(Dₓₓ*v0)) .<= equalitytol) 59 @test norm(Dₓₓ*v0) ≈ 0.0 atol=5e-10
68 @test all(abs.(collect(Dₓₓ*v1)) .<= equalitytol) 60 @test norm(Dₓₓ*v1) ≈ 0.0 atol=5e-10
69 @test all(abs.((collect(Dₓₓ*v2) - v0)) .<= equalitytol) 61 @test Dₓₓ*v2 ≈ v0 atol=5e-11
70 @test all(abs.((collect(Dₓₓ*v3) - v1)) .<= equalitytol) 62 @test Dₓₓ*v3 ≈ v1 atol=5e-11
71 e4 = collect(Dₓₓ*v4) - v2 63
72 e5 = collect(Dₓₓ*v5) + v5 64 h = spacing(g)[1];
73 @test sqrt(h*sum(collect(e4.^2))) <= accuracytol 65 l2(v) = sqrt(h*sum(v.^2))
74 @test sqrt(h*sum(collect(e5.^2))) <= accuracytol 66 @test Dₓₓ*v4 ≈ v2 atol=5e-4 norm=l2
75 end 67 @test Dₓₓ*v5 ≈ -v5 atol=5e-4 norm=l2
68 end
69
76 70
77 @testset "Laplace2D" begin 71 @testset "Laplace2D" begin
78 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") 72 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
79 Lx = 1.5 73 Lx = 1.5
80 Ly = 3.2 74 Ly = 3.2
81 g = EquidistantGrid((102,131), (0.0, 0.0), (Lx,Ly)) 75 g = EquidistantGrid((102,131), (0.0, 0.0), (Lx,Ly))
82 L = Laplace(g, op.innerStencil, op.closureStencils) 76 L = Laplace(g, op.innerStencil, op.closureStencils)
83 77
84 78
85 f0(x::Float64,y::Float64) = 2. 79 f0(x,y) = 2.
86 f1(x::Float64,y::Float64) = x+y 80 f1(x,y) = x+y
87 f2(x::Float64,y::Float64) = 1/2*x^2 + 1/2*y^2 81 f2(x,y) = 1/2*x^2 + 1/2*y^2
88 f3(x::Float64,y::Float64) = 1/6*x^3 + 1/6*y^3 82 f3(x,y) = 1/6*x^3 + 1/6*y^3
89 f4(x::Float64,y::Float64) = 1/24*x^4 + 1/24*y^4 83 f4(x,y) = 1/24*x^4 + 1/24*y^4
90 f5(x::Float64,y::Float64) = sin(x) + cos(y) 84 f5(x,y) = sin(x) + cos(y)
91 f5ₓₓ(x::Float64,y::Float64) = -f5(x,y) 85 f5ₓₓ(x,y) = -f5(x,y)
92 86
93 v0 = evalOn(g,f0) 87 v0 = evalOn(g,f0)
94 v1 = evalOn(g,f1) 88 v1 = evalOn(g,f1)
95 v2 = evalOn(g,f2) 89 v2 = evalOn(g,f2)
96 v3 = evalOn(g,f3) 90 v3 = evalOn(g,f3)
99 v5ₓₓ = evalOn(g,f5ₓₓ) 93 v5ₓₓ = evalOn(g,f5ₓₓ)
100 94
101 @test L isa TensorMapping{T,2,2} where T 95 @test L isa TensorMapping{T,2,2} where T
102 @test L' isa TensorMapping{T,2,2} where T 96 @test L' isa TensorMapping{T,2,2} where T
103 97
104 # TODO: Should perhaps set tolerance level for isapporx instead?
105 # Are these tolerance levels resonable or should tests be constructed
106 # differently?
107 equalitytol = 0.5*1e-10
108 accuracytol = 0.5*1e-3
109 # 4th order interior stencil, 2nd order boundary stencil, 98 # 4th order interior stencil, 2nd order boundary stencil,
110 # implies that L*v should be exact for v - monomial up to order 3. 99 # implies that L*v should be exact for v - monomial up to order 3.
111 # Exact differentiation is measured point-wise. For other grid functions 100 # Exact differentiation is measured point-wise. For other grid functions
112 # the error is measured in the H-norm. 101 # the error is measured in the H-norm.
113 @test all(abs.(collect(L*v0)) .<= equalitytol) 102 @test norm(L*v0) ≈ 0 atol=5e-10
114 @test all(abs.(collect(L*v1)) .<= equalitytol) 103 @test norm(L*v1) ≈ 0 atol=5e-10
115 @test all(collect(L*v2) .≈ v0) # Seems to be more accurate 104 @test L*v2 ≈ v0 # Seems to be more accurate
116 @test all(abs.((collect(L*v3) - v1)) .<= equalitytol) 105 @test L*v3 ≈ v1 atol=5e-10
117 e4 = collect(L*v4) - v2
118 e5 = collect(L*v5) - v5ₓₓ
119 106
120 h = spacing(g) 107 h = spacing(g)
121 @test sqrt(prod(h)*sum(collect(e4.^2))) <= accuracytol 108 l2(v) = sqrt(prod(h)*sum(v.^2))
122 @test sqrt(prod(h)*sum(collect(e5.^2))) <= accuracytol 109 @test L*v4 ≈ v2 atol=5e-4 norm=l2
110 @test L*v5 ≈ v5ₓₓ atol=5e-4 norm=l2
123 end 111 end
124 112
125 @testset "DiagonalInnerProduct" begin 113 @testset "DiagonalInnerProduct" begin
126 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") 114 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
127 L = 2.3 115 L = 2.3
128 g = EquidistantGrid((77,), (0.0,), (L,)) 116 g = EquidistantGrid(77, 0.0, L)
129 H = DiagonalInnerProduct(g,op.quadratureClosure) 117 H = DiagonalInnerProduct(g,op.quadratureClosure)
130 v = ones(Float64, size(g)) 118 v = ones(Float64, size(g))
131 119
132 @test H isa TensorMapping{T,1,1} where T 120 @test H isa TensorMapping{T,1,1} where T
133 @test H' isa TensorMapping{T,1,1} where T 121 @test H' isa TensorMapping{T,1,1} where T
134 @test sum(collect(H*v)) ≈ L 122 @test sum(H*v) ≈ L
135 @test collect(H*v) == collect(H'*v) 123 @test H*v == H'*v
136 end 124 end
137 125
138 @testset "Quadrature" begin 126 @testset "Quadrature" begin
139 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") 127 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
140 Lx = 2.3 128 Lx = 2.3
156 end 144 end
157 145
158 @testset "InverseDiagonalInnerProduct" begin 146 @testset "InverseDiagonalInnerProduct" begin
159 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") 147 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
160 L = 2.3 148 L = 2.3
161 g = EquidistantGrid((77,), (0.0,), (L,)) 149 g = EquidistantGrid(77, 0.0, L)
162 H = DiagonalInnerProduct(g, op.quadratureClosure) 150 H = DiagonalInnerProduct(g, op.quadratureClosure)
163 Hi = InverseDiagonalInnerProduct(g,op.quadratureClosure) 151 Hi = InverseDiagonalInnerProduct(g,op.quadratureClosure)
164 v = evalOn(g, x->sin(x)) 152 v = evalOn(g, x->sin(x))
165 153
166 @test Hi isa TensorMapping{T,1,1} where T 154 @test Hi isa TensorMapping{T,1,1} where T
167 @test Hi' isa TensorMapping{T,1,1} where T 155 @test Hi' isa TensorMapping{T,1,1} where T
168 @test collect(Hi*H*v) ≈ v 156 @test Hi*H*v ≈ v
169 @test collect(Hi*v) == collect(Hi'*v) 157 @test Hi*v == Hi'*v
170 end 158 end
171 159
172 @testset "InverseQuadrature" begin 160 @testset "InverseQuadrature" begin
173 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") 161 op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
174 Lx = 7.3 162 Lx = 7.3
179 Qinv = InverseQuadrature(g, op.quadratureClosure) 167 Qinv = InverseQuadrature(g, op.quadratureClosure)
180 v = evalOn(g, (x,y)-> x^2 + (y-1)^2 + x*y) 168 v = evalOn(g, (x,y)-> x^2 + (y-1)^2 + x*y)
181 169
182 @test Qinv isa TensorMapping{T,2,2} where T 170 @test Qinv isa TensorMapping{T,2,2} where T
183 @test Qinv' isa TensorMapping{T,2,2} where T 171 @test Qinv' isa TensorMapping{T,2,2} where T
184 @test_broken collect(Qinv*(Q*v)) ≈ v 172 @test_broken Qinv*(Q*v) ≈ v
185 @test collect(Qinv*v) == collect(Qinv'*v) 173 @test Qinv*v == Qinv'*v
186 end 174 end
187 # 175 #
188 # @testset "BoundaryValue" begin 176 # @testset "BoundaryValue" begin
189 # op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") 177 # op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
190 # g = EquidistantGrid((4,5), (0.0, 0.0), (1.0,1.0)) 178 # g = EquidistantGrid((4,5), (0.0, 0.0), (1.0,1.0))
212 # @test size(e_w'*v) == (5,) 200 # @test size(e_w'*v) == (5,)
213 # @test size(e_e'*v) == (5,) 201 # @test size(e_e'*v) == (5,)
214 # @test size(e_s'*v) == (4,) 202 # @test size(e_s'*v) == (4,)
215 # @test size(e_n'*v) == (4,) 203 # @test size(e_n'*v) == (4,)
216 # 204 #
217 # @test collect(e_w'*v) == [10,7,4,1.0,1] 205 # @test e_w'*v == [10,7,4,1.0,1]
218 # @test collect(e_e'*v) == [13,10,7,4,4.0] 206 # @test e_e'*v == [13,10,7,4,4.0]
219 # @test collect(e_s'*v) == [10,11,12,13.0] 207 # @test e_s'*v == [10,11,12,13.0]
220 # @test collect(e_n'*v) == [1,2,3,4.0] 208 # @test e_n'*v == [1,2,3,4.0]
221 # 209 #
222 # g_x = [1,2,3,4.0] 210 # g_x = [1,2,3,4.0]
223 # g_y = [5,4,3,2,1.0] 211 # g_y = [5,4,3,2,1.0]
224 # 212 #
225 # G_w = zeros(Float64, (4,5)) 213 # G_w = zeros(Float64, (4,5))
238 # @test size(e_e*g_y) == (UnknownDim,5) 226 # @test size(e_e*g_y) == (UnknownDim,5)
239 # @test size(e_s*g_x) == (4,UnknownDim) 227 # @test size(e_s*g_x) == (4,UnknownDim)
240 # @test size(e_n*g_x) == (4,UnknownDim) 228 # @test size(e_n*g_x) == (4,UnknownDim)
241 # 229 #
242 # # These tests should be moved to where they are possible (i.e we know what the grid should be) 230 # # These tests should be moved to where they are possible (i.e we know what the grid should be)
243 # @test_broken collect(e_w*g_y) == G_w 231 # @test_broken e_w*g_y == G_w
244 # @test_broken collect(e_e*g_y) == G_e 232 # @test_broken e_e*g_y == G_e
245 # @test_broken collect(e_s*g_x) == G_s 233 # @test_broken e_s*g_x == G_s
246 # @test_broken collect(e_n*g_x) == G_n 234 # @test_broken e_n*g_x == G_n
247 # end 235 # end
248 # 236 #
249 # @testset "NormalDerivative" begin 237 # @testset "NormalDerivative" begin
250 # op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") 238 # op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
251 # g = EquidistantGrid((5,6), (0.0, 0.0), (4.0,5.0)) 239 # g = EquidistantGrid((5,6), (0.0, 0.0), (4.0,5.0))
271 # @test size(d_w'*v) == (6,) 259 # @test size(d_w'*v) == (6,)
272 # @test size(d_e'*v) == (6,) 260 # @test size(d_e'*v) == (6,)
273 # @test size(d_s'*v) == (5,) 261 # @test size(d_s'*v) == (5,)
274 # @test size(d_n'*v) == (5,) 262 # @test size(d_n'*v) == (5,)
275 # 263 #
276 # @test collect(d_w'*v) ≈ v∂x[1,:] 264 # @test d_w'*v .≈ v∂x[1,:]
277 # @test collect(d_e'*v) ≈ v∂x[5,:] 265 # @test d_e'*v .≈ v∂x[5,:]
278 # @test collect(d_s'*v) ≈ v∂y[:,1] 266 # @test d_s'*v .≈ v∂y[:,1]
279 # @test collect(d_n'*v) ≈ v∂y[:,6] 267 # @test d_n'*v .≈ v∂y[:,6]
280 # 268 #
281 # 269 #
282 # d_x_l = zeros(Float64, 5) 270 # d_x_l = zeros(Float64, 5)
283 # d_x_u = zeros(Float64, 5) 271 # d_x_u = zeros(Float64, 5)
284 # for i ∈ eachindex(d_x_l) 272 # for i ∈ eachindex(d_x_l)
315 # @test size(d_e*g_y) == (UnknownDim,6) 303 # @test size(d_e*g_y) == (UnknownDim,6)
316 # @test size(d_s*g_x) == (5,UnknownDim) 304 # @test size(d_s*g_x) == (5,UnknownDim)
317 # @test size(d_n*g_x) == (5,UnknownDim) 305 # @test size(d_n*g_x) == (5,UnknownDim)
318 # 306 #
319 # # These tests should be moved to where they are possible (i.e we know what the grid should be) 307 # # These tests should be moved to where they are possible (i.e we know what the grid should be)
320 # @test_broken collect(d_w*g_y) ≈ G_w 308 # @test_broken d_w*g_y .≈ G_w
321 # @test_broken collect(d_e*g_y) ≈ G_e 309 # @test_broken d_e*g_y .≈ G_e
322 # @test_broken collect(d_s*g_x) ≈ G_s 310 # @test_broken d_s*g_x .≈ G_s
323 # @test_broken collect(d_n*g_x) ≈ G_n 311 # @test_broken d_n*g_x .≈ G_n
324 # end 312 # end
325 # 313 #
326 # @testset "BoundaryQuadrature" begin 314 # @testset "BoundaryQuadrature" begin
327 # op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt") 315 # op = readOperator(sbp_operators_path()*"d2_4th.txt",sbp_operators_path()*"h_4th.txt")
328 # g = EquidistantGrid((10,11), (0.0, 0.0), (1.0,1.0)) 316 # g = EquidistantGrid((10,11), (0.0, 0.0), (1.0,1.0))
360 # @test size(H_w*v_w) == (11,) 348 # @test size(H_w*v_w) == (11,)
361 # @test size(H_e*v_e) == (11,) 349 # @test size(H_e*v_e) == (11,)
362 # @test size(H_s*v_s) == (10,) 350 # @test size(H_s*v_s) == (10,)
363 # @test size(H_n*v_n) == (10,) 351 # @test size(H_n*v_n) == (10,)
364 # 352 #
365 # @test collect(H_w*v_w) ≈ q_y.*v_w 353 # @test H_w*v_w .≈ q_y.*v_w
366 # @test collect(H_e*v_e) ≈ q_y.*v_e 354 # @test H_e*v_e .≈ q_y.*v_e
367 # @test collect(H_s*v_s) ≈ q_x.*v_s 355 # @test H_s*v_s .≈ q_x.*v_s
368 # @test collect(H_n*v_n) ≈ q_x.*v_n 356 # @test H_n*v_n .≈ q_x.*v_n
369 # 357 #
370 # @test collect(H_w'*v_w) == collect(H_w'*v_w) 358 # @test H_w'*v_w == H_w'*v_w
371 # @test collect(H_e'*v_e) == collect(H_e'*v_e) 359 # @test H_e'*v_e == H_e'*v_e
372 # @test collect(H_s'*v_s) == collect(H_s'*v_s) 360 # @test H_s'*v_s == H_s'*v_s
373 # @test collect(H_n'*v_n) == collect(H_n'*v_n) 361 # @test H_n'*v_n == H_n'*v_n
374 # end 362 # end
375 363
376 end 364 end