comparison test/SbpOperators/stencil_test.jl @ 1072:14cb97284373 feature/dissipation_operators

Merge default
author Jonatan Werpers <jonatan@werpers.com>
date Thu, 24 Mar 2022 12:00:12 +0100
parents 11767fbb29f4 fdd594b2a15e
children c779af4d02e5
comparison
equal deleted inserted replaced
1069:c89c6b63c7f4 1072:14cb97284373
1 using Test 1 using Test
2 using Sbplib.SbpOperators 2 using Sbplib.SbpOperators
3 import Sbplib.SbpOperators.Stencil 3 import Sbplib.SbpOperators.Stencil
4 import Sbplib.SbpOperators.NestedStencil
5 import Sbplib.SbpOperators.scale
4 6
5 @testset "Stencil" begin 7 @testset "Stencil" begin
6 s = Stencil((-2,2), (1.,2.,2.,3.,4.)) 8 s = Stencil(-2:2, (1.,2.,2.,3.,4.))
7 @test s isa Stencil{Float64, 5} 9 @test s isa Stencil{Float64, 5}
8 10
9 @test eltype(s) == Float64 11 @test eltype(s) == Float64
10 @test SbpOperators.scale(s, 2) == Stencil((-2,2), (2.,4.,4.,6.,8.))
11 12
12 @test Stencil(1,2,3,4; center=1) == Stencil((0, 3),(1,2,3,4)) 13 @test length(s) == 5
13 @test Stencil(1,2,3,4; center=2) == Stencil((-1, 2),(1,2,3,4)) 14 @test length(Stencil(-1:2, (1,2,3,4))) == 4
14 @test Stencil(1,2,3,4; center=4) == Stencil((-3, 0),(1,2,3,4))
15 15
16 @test CenteredStencil(1,2,3,4,5) == Stencil((-2, 2), (1,2,3,4,5)) 16 @test SbpOperators.scale(s, 2) == Stencil(-2:2, (2.,4.,4.,6.,8.))
17
18 @test Stencil(1,2,3,4; center=1) == Stencil(0:3,(1,2,3,4))
19 @test Stencil(1,2,3,4; center=2) == Stencil(-1:2,(1,2,3,4))
20 @test Stencil(1,2,3,4; center=4) == Stencil(-3:0,(1,2,3,4))
21
22 @test CenteredStencil(1,2,3,4,5) == Stencil(-2:2, (1,2,3,4,5))
17 @test_throws ArgumentError CenteredStencil(1,2,3,4) 23 @test_throws ArgumentError CenteredStencil(1,2,3,4)
18 24
19 # Changing the type of the weights 25 # Changing the type of the weights
20 @test Stencil{Float64}(Stencil(1,2,3,4,5; center=2)) == Stencil(1.,2.,3.,4.,5.; center=2) 26 @test Stencil{Float64}(Stencil(1,2,3,4,5; center=2)) == Stencil(1.,2.,3.,4.,5.; center=2)
21 @test Stencil{Float64}(CenteredStencil(1,2,3,4,5)) == CenteredStencil(1.,2.,3.,4.,5.) 27 @test Stencil{Float64}(CenteredStencil(1,2,3,4,5)) == CenteredStencil(1.,2.,3.,4.,5.)
22 @test Stencil{Int}(Stencil(1.,2.,3.,4.,5.; center=2)) == Stencil(1,2,3,4,5; center=2) 28 @test Stencil{Int}(Stencil(1.,2.,3.,4.,5.; center=2)) == Stencil(1,2,3,4,5; center=2)
23 @test Stencil{Rational}(Stencil(1.,2.,3.,4.,5.; center=2)) == Stencil(1//1,2//1,3//1,4//1,5//1; center=2) 29 @test Stencil{Rational}(Stencil(1.,2.,3.,4.,5.; center=2)) == Stencil(1//1,2//1,3//1,4//1,5//1; center=2)
24 30
25 @testset "convert" begin 31 @testset "convert" begin
26 @test convert(Stencil{Float64}, Stencil(1,2,3,4,5; center=2)) == Stencil(1.,2.,3.,4.,5.; center=2) 32 @test convert(Stencil{Float64}, Stencil(1,2,3,4,5; center=2)) == Stencil(1.,2.,3.,4.,5.; center=2)
27 @test convert(Stencil{Float64}, CenteredStencil(1,2,3,4,5)) == CenteredStencil(1.,2.,3.,4.,5.) 33 @test convert(Stencil{Float64,5}, CenteredStencil(1,2,3,4,5)) == CenteredStencil(1.,2.,3.,4.,5.)
28 @test convert(Stencil{Int}, Stencil(1.,2.,3.,4.,5.; center=2)) == Stencil(1,2,3,4,5; center=2) 34 @test convert(Stencil{Int,5}, Stencil(1.,2.,3.,4.,5.; center=2)) == Stencil(1,2,3,4,5; center=2)
29 @test convert(Stencil{Rational}, Stencil(1.,2.,3.,4.,5.; center=2)) == Stencil(1//1,2//1,3//1,4//1,5//1; center=2) 35 @test convert(Stencil{Rational,5}, Stencil(1.,2.,3.,4.,5.; center=2)) == Stencil(1//1,2//1,3//1,4//1,5//1; center=2)
36 end
37
38 @testset "promotion of weights" begin
39 @test Stencil(1.,2; center = 1) isa Stencil{Float64, 2}
40 @test Stencil(1,2//2; center = 1) isa Stencil{Rational{Int64}, 2}
41 end
42
43 @testset "promotion" begin
44 @test promote(Stencil(1,1;center=1), Stencil(2.,2.;center=2)) == (Stencil(1.,1.;center=1), Stencil(2.,2.;center=2))
45 end
46
47 @testset "type stability" begin
48 s_int = CenteredStencil(1,2,3)
49 s_float = CenteredStencil(1.,2.,3.)
50 v_int = rand(1:10,10);
51 v_float = rand(10);
52
53 @inferred SbpOperators.apply_stencil(s_int, v_int, 2)
54 @inferred SbpOperators.apply_stencil(s_float, v_float, 2)
55 @inferred SbpOperators.apply_stencil(s_int, v_float, 2)
56 @inferred SbpOperators.apply_stencil(s_float, v_int, 2)
57
58 @inferred SbpOperators.apply_stencil_backwards(s_int, v_int, 5)
59 @inferred SbpOperators.apply_stencil_backwards(s_float, v_float, 5)
60 @inferred SbpOperators.apply_stencil_backwards(s_int, v_float, 5)
61 @inferred SbpOperators.apply_stencil_backwards(s_float, v_int, 5)
30 end 62 end
31 end 63 end
32 64
33 @testset "left_pad" begin 65 @testset "left_pad" begin
34 @test SbpOperators.left_pad(Stencil(1,1, center = 1), 2) == Stencil(1,1, center=1) 66 @test SbpOperators.left_pad(Stencil(1,1, center = 1), 2) == Stencil(1,1, center=1)
43 @test SbpOperators.right_pad(Stencil(1,1, center = 1), 3) == Stencil(1,1,0, center=1) 75 @test SbpOperators.right_pad(Stencil(1,1, center = 1), 3) == Stencil(1,1,0, center=1)
44 @test SbpOperators.right_pad(Stencil(2,3, center = 2), 4) == Stencil(2,3,0,0, center=2) 76 @test SbpOperators.right_pad(Stencil(2,3, center = 2), 4) == Stencil(2,3,0,0, center=2)
45 77
46 @test SbpOperators.right_pad(Stencil(2.,3., center = 2), 4) == Stencil(2.,3.,0.,0., center=2) 78 @test SbpOperators.right_pad(Stencil(2.,3., center = 2), 4) == Stencil(2.,3.,0.,0., center=2)
47 end 79 end
80
81
82 @testset "NestedStencil" begin
83
84 @testset "Constructors" begin
85 s1 = CenteredStencil(-1, 1, 0)
86 s2 = CenteredStencil(-1, 0, 1)
87 s3 = CenteredStencil( 0,-1, 1)
88
89 ns = NestedStencil(CenteredStencil(s1,s2,s3))
90 @test ns isa NestedStencil{Int,3}
91
92 @test CenteredNestedStencil(s1,s2,s3) == ns
93
94 @test NestedStencil(s1,s2,s3, center = 2) == ns
95 @test NestedStencil(s1,s2,s3, center = 1) == NestedStencil(Stencil(s1,s2,s3, center=1))
96
97 @test NestedStencil((-1,1,0),(-1,0,1),(0,-1,1), center=2) == ns
98 @test CenteredNestedStencil((-1,1,0),(-1,0,1),(0,-1,1)) == ns
99 @test NestedStencil((-1,1,0),(-1,0,1),(0,-1,1), center=1) == NestedStencil(Stencil(
100 Stencil(-1, 1, 0; center=1),
101 Stencil(-1, 0, 1; center=1),
102 Stencil( 0,-1, 1; center=1);
103 center=1
104 ))
105
106 @testset "Error handling" begin
107 end
108 end
109
110 @testset "scale" begin
111 ns = NestedStencil((-1,1,0),(-1,0,1),(0,-1,1), center=2)
112 @test SbpOperators.scale(ns, 2) == NestedStencil((-2,2,0),(-2,0,2),(0,-2,2), center=2)
113 end
114
115 @testset "conversion" begin
116 ns = NestedStencil((-1,1,0),(-1,0,1),(0,-1,1), center=2)
117 @test NestedStencil{Float64}(ns) == NestedStencil((-1.,1.,0.),(-1.,0.,1.),(0.,-1.,1.), center=2)
118 @test NestedStencil{Rational}(ns) == NestedStencil((-1//1,1//1,0//1),(-1//1,0//1,1//1),(0//1,-1//1,1//1), center=2)
119
120 @test convert(NestedStencil{Float64}, ns) == NestedStencil((-1.,1.,0.),(-1.,0.,1.),(0.,-1.,1.), center=2)
121 @test convert(NestedStencil{Rational}, ns) == NestedStencil((-1//1,1//1,0//1),(-1//1,0//1,1//1),(0//1,-1//1,1//1), center=2)
122 end
123
124 @testset "promotion of weights" begin
125 @test NestedStencil((-1,1,0),(-1.,0.,1.),(0,-1,1), center=2) isa NestedStencil{Float64,3,3}
126 @test NestedStencil((-1,1,0),(-1,0,1),(0//1,-1,1), center=2) isa NestedStencil{Rational{Int64},3,3}
127 end
128
129 @testset "promotion" begin
130 promote(
131 CenteredNestedStencil((-1,1,0),(-1,0,1),(0,-1,1)),
132 CenteredNestedStencil((-1.,1.,0.),(-1.,0.,1.),(0.,-1.,1.))
133 ) == (
134 CenteredNestedStencil((-1.,1.,0.),(-1.,0.,1.),(0.,-1.,1.)),
135 CenteredNestedStencil((-1.,1.,0.),(-1.,0.,1.),(0.,-1.,1.))
136 )
137 end
138
139 @testset "apply" begin
140 c = [ 1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
141 v = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
142
143 # Centered
144 ns = NestedStencil((-1,1,0),(-1,0,1),(0,-2,2), center=2)
145 @test SbpOperators.apply_inner_stencils(ns, c, 4) == Stencil(4,9,10; center=2)
146 @test SbpOperators.apply_inner_stencils_backwards(ns, c, 4) == Stencil(-5,-9,-8; center=2)
147
148 @test SbpOperators.apply_stencil(ns, c, v, 4) == 4*5 + 9*7 + 10*11
149 @test SbpOperators.apply_stencil_backwards(ns, c, v, 4) == -8*5 - 9*7 - 5*11
150
151 # Non-centered
152 ns = NestedStencil((-1,1,0),(-1,0,1),(0,-1,1), center=1)
153 @test SbpOperators.apply_inner_stencils(ns, c, 4) == Stencil(5,11,6; center=1)
154 @test SbpOperators.apply_inner_stencils_backwards(ns, c, 4) == Stencil(-4,-7,-3; center=1)
155
156 @test SbpOperators.apply_stencil(ns, c, v, 4) == 5*7 + 11*11 + 6*13
157 @test SbpOperators.apply_stencil_backwards(ns, c, v, 4) == -3*3 - 7*5 - 4*7
158 end
159
160 @testset "type stability" begin
161 s_int = CenteredNestedStencil((1,2,3),(1,2,3),(1,2,3))
162 s_float = CenteredNestedStencil((1.,2.,3.),(1.,2.,3.),(1.,2.,3.))
163
164 v_int = rand(1:10,10);
165 v_float = rand(10);
166
167 c_int = rand(1:10,10);
168 c_float = rand(10);
169
170 @inferred SbpOperators.apply_stencil(s_int, c_int, v_int, 2)
171 @inferred SbpOperators.apply_stencil(s_float, c_int, v_float, 2)
172 @inferred SbpOperators.apply_stencil(s_int, c_int, v_float, 2)
173 @inferred SbpOperators.apply_stencil(s_float, c_int, v_int, 2)
174
175 @inferred SbpOperators.apply_stencil(s_int, c_float, v_int, 2)
176 @inferred SbpOperators.apply_stencil(s_float, c_float, v_float, 2)
177 @inferred SbpOperators.apply_stencil(s_int, c_float, v_float, 2)
178 @inferred SbpOperators.apply_stencil(s_float, c_float, v_int, 2)
179
180 @inferred SbpOperators.apply_stencil_backwards(s_int, c_int, v_int, 2)
181 @inferred SbpOperators.apply_stencil_backwards(s_float, c_int, v_float, 2)
182 @inferred SbpOperators.apply_stencil_backwards(s_int, c_int, v_float, 2)
183 @inferred SbpOperators.apply_stencil_backwards(s_float, c_int, v_int, 2)
184
185 @inferred SbpOperators.apply_stencil_backwards(s_int, c_float, v_int, 2)
186 @inferred SbpOperators.apply_stencil_backwards(s_float, c_float, v_float, 2)
187 @inferred SbpOperators.apply_stencil_backwards(s_int, c_float, v_float, 2)
188 @inferred SbpOperators.apply_stencil_backwards(s_float, c_float, v_int, 2)
189 end
190 end