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