comparison test/SbpOperators/stencil_test.jl @ 1076:101b88d13cf3 feature/first_derivative

Merge default
author Jonatan Werpers <jonatan@werpers.com>
date Thu, 07 Apr 2022 20:28:08 +0200
parents fdd594b2a15e
children 14cb97284373
comparison
equal deleted inserted replaced
1047:d12ab8120d29 1076:101b88d13cf3
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
64
65 @testset "NestedStencil" begin
66
67 @testset "Constructors" begin
68 s1 = CenteredStencil(-1, 1, 0)
69 s2 = CenteredStencil(-1, 0, 1)
70 s3 = CenteredStencil( 0,-1, 1)
71
72 ns = NestedStencil(CenteredStencil(s1,s2,s3))
73 @test ns isa NestedStencil{Int,3}
74
75 @test CenteredNestedStencil(s1,s2,s3) == ns
76
77 @test NestedStencil(s1,s2,s3, center = 2) == ns
78 @test NestedStencil(s1,s2,s3, center = 1) == NestedStencil(Stencil(s1,s2,s3, center=1))
79
80 @test NestedStencil((-1,1,0),(-1,0,1),(0,-1,1), center=2) == ns
81 @test CenteredNestedStencil((-1,1,0),(-1,0,1),(0,-1,1)) == ns
82 @test NestedStencil((-1,1,0),(-1,0,1),(0,-1,1), center=1) == NestedStencil(Stencil(
83 Stencil(-1, 1, 0; center=1),
84 Stencil(-1, 0, 1; center=1),
85 Stencil( 0,-1, 1; center=1);
86 center=1
87 ))
88
89 @testset "Error handling" begin
90 end
91 end
92
93 @testset "scale" begin
94 ns = NestedStencil((-1,1,0),(-1,0,1),(0,-1,1), center=2)
95 @test SbpOperators.scale(ns, 2) == NestedStencil((-2,2,0),(-2,0,2),(0,-2,2), center=2)
96 end
97
98 @testset "conversion" begin
99 ns = NestedStencil((-1,1,0),(-1,0,1),(0,-1,1), center=2)
100 @test NestedStencil{Float64}(ns) == NestedStencil((-1.,1.,0.),(-1.,0.,1.),(0.,-1.,1.), center=2)
101 @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)
102
103 @test convert(NestedStencil{Float64}, ns) == NestedStencil((-1.,1.,0.),(-1.,0.,1.),(0.,-1.,1.), center=2)
104 @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)
105 end
106
107 @testset "promotion of weights" begin
108 @test NestedStencil((-1,1,0),(-1.,0.,1.),(0,-1,1), center=2) isa NestedStencil{Float64,3,3}
109 @test NestedStencil((-1,1,0),(-1,0,1),(0//1,-1,1), center=2) isa NestedStencil{Rational{Int64},3,3}
110 end
111
112 @testset "promotion" begin
113 promote(
114 CenteredNestedStencil((-1,1,0),(-1,0,1),(0,-1,1)),
115 CenteredNestedStencil((-1.,1.,0.),(-1.,0.,1.),(0.,-1.,1.))
116 ) == (
117 CenteredNestedStencil((-1.,1.,0.),(-1.,0.,1.),(0.,-1.,1.)),
118 CenteredNestedStencil((-1.,1.,0.),(-1.,0.,1.),(0.,-1.,1.))
119 )
120 end
121
122 @testset "apply" begin
123 c = [ 1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
124 v = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
125
126 # Centered
127 ns = NestedStencil((-1,1,0),(-1,0,1),(0,-2,2), center=2)
128 @test SbpOperators.apply_inner_stencils(ns, c, 4) == Stencil(4,9,10; center=2)
129 @test SbpOperators.apply_inner_stencils_backwards(ns, c, 4) == Stencil(-5,-9,-8; center=2)
130
131 @test SbpOperators.apply_stencil(ns, c, v, 4) == 4*5 + 9*7 + 10*11
132 @test SbpOperators.apply_stencil_backwards(ns, c, v, 4) == -8*5 - 9*7 - 5*11
133
134 # Non-centered
135 ns = NestedStencil((-1,1,0),(-1,0,1),(0,-1,1), center=1)
136 @test SbpOperators.apply_inner_stencils(ns, c, 4) == Stencil(5,11,6; center=1)
137 @test SbpOperators.apply_inner_stencils_backwards(ns, c, 4) == Stencil(-4,-7,-3; center=1)
138
139 @test SbpOperators.apply_stencil(ns, c, v, 4) == 5*7 + 11*11 + 6*13
140 @test SbpOperators.apply_stencil_backwards(ns, c, v, 4) == -3*3 - 7*5 - 4*7
141 end
142
143 @testset "type stability" begin
144 s_int = CenteredNestedStencil((1,2,3),(1,2,3),(1,2,3))
145 s_float = CenteredNestedStencil((1.,2.,3.),(1.,2.,3.),(1.,2.,3.))
146
147 v_int = rand(1:10,10);
148 v_float = rand(10);
149
150 c_int = rand(1:10,10);
151 c_float = rand(10);
152
153 @inferred SbpOperators.apply_stencil(s_int, c_int, v_int, 2)
154 @inferred SbpOperators.apply_stencil(s_float, c_int, v_float, 2)
155 @inferred SbpOperators.apply_stencil(s_int, c_int, v_float, 2)
156 @inferred SbpOperators.apply_stencil(s_float, c_int, v_int, 2)
157
158 @inferred SbpOperators.apply_stencil(s_int, c_float, v_int, 2)
159 @inferred SbpOperators.apply_stencil(s_float, c_float, v_float, 2)
160 @inferred SbpOperators.apply_stencil(s_int, c_float, v_float, 2)
161 @inferred SbpOperators.apply_stencil(s_float, c_float, v_int, 2)
162
163 @inferred SbpOperators.apply_stencil_backwards(s_int, c_int, v_int, 2)
164 @inferred SbpOperators.apply_stencil_backwards(s_float, c_int, v_float, 2)
165 @inferred SbpOperators.apply_stencil_backwards(s_int, c_int, v_float, 2)
166 @inferred SbpOperators.apply_stencil_backwards(s_float, c_int, v_int, 2)
167
168 @inferred SbpOperators.apply_stencil_backwards(s_int, c_float, v_int, 2)
169 @inferred SbpOperators.apply_stencil_backwards(s_float, c_float, v_float, 2)
170 @inferred SbpOperators.apply_stencil_backwards(s_int, c_float, v_float, 2)
171 @inferred SbpOperators.apply_stencil_backwards(s_float, c_float, v_int, 2)
172 end
173
174 end