comparison test/testDiffOps.jl @ 357:e22b061f5299 refactor/remove_dynamic_size_tensormapping

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