@@ -21,7 +21,46 @@ function second_derivative_stencil(N)
2121 A
2222end
2323
24- @testset " Differenting first three dimensions" begin
24+ @testset " Differentiation on 2D array" begin
25+ M = zeros (22 ,22 )
26+ M_temp = zeros (20 ,22 )
27+ indices = Iterators. drop (CartesianIndices ((22 ,22 )), 0 )
28+ for idx in indices
29+ M[idx] = sin (idx[1 ]* 0.1 )
30+ end
31+ L = CenteredDifference (4 ,4 ,0.1 ,20 )
32+
33+ # test mul!
34+ mul! (M_temp, L, M)
35+
36+ correct_row = 10000.0 * fourth_deriv_approx_stencil (20 )* M[:,1 ]
37+
38+ for i in 1 : 22
39+ @test M_temp[:,i] ≈ correct_row
40+ end
41+
42+ # Test that * agrees will mul!
43+ @test M_temp == L* M
44+
45+ # Differentiation along second dimension
46+ L = CenteredDifference {2} (4 ,4 ,0.1 ,20 )
47+ M_temp_2 = zeros (22 ,20 )
48+ indices = Iterators. drop (CartesianIndices ((22 ,22 )), 0 )
49+ for idx in indices
50+ M[idx] = sin (idx[2 ]* 0.1 )
51+ end
52+
53+ # test mul!
54+ mul! (M_temp_2, L, M)
55+ for i in 1 : 22
56+ @test M_temp_2[i,:] ≈ correct_row
57+ end
58+
59+ # Test that * agrees will mul!
60+ @test M_temp_2 == L* M
61+ end
62+
63+ @testset " Differenting on 3D array with L2" begin
2564 M = zeros (22 ,22 ,22 )
2665 M_temp = zeros (20 ,22 ,22 )
2766 indices = Iterators. drop (CartesianIndices ((22 ,22 ,22 )), 0 )
83122 @test M_temp_3 == L* M
84123end
85124
125+ @testset " Differentiation on 3D array with L4" begin
126+ M = zeros (22 ,22 ,22 )
127+ M_temp = zeros (20 ,22 ,22 )
128+ indices = Iterators. drop (CartesianIndices ((22 ,22 ,22 )), 0 )
129+ for idx in indices
130+ M[idx] = sin (idx[1 ]* 0.1 )
131+ end
132+ L = CenteredDifference (4 ,4 ,0.1 ,20 )
133+
134+ # test mul!
135+ mul! (M_temp, L, M)
136+
137+ correct_row = 10000.0 * fourth_deriv_approx_stencil (20 )* M[:,1 ,1 ]
138+
139+ for i in 1 : 22
140+ for j in 1 : 22
141+ @test M_temp[:,i,j] ≈ correct_row
142+ end
143+ end
144+
145+ # Test that * agrees will mul!
146+ @test M_temp == L* M
147+
148+ # Differentiation along second dimension
149+ L = CenteredDifference {2} (4 ,4 ,0.1 ,20 )
150+ M_temp_2 = zeros (22 ,20 ,22 )
151+ for idx in indices
152+ M[idx] = sin (idx[2 ]* 0.1 )
153+ end
154+
155+ # test mul!
156+ mul! (M_temp_2, L, M)
157+ for i in 1 : 22
158+ for j in 1 : 22
159+ @test M_temp_2[i,:,j] ≈ correct_row
160+ end
161+ end
162+
163+ # Test that * agrees will mul!
164+ @test M_temp_2 == L* M
165+
166+ # Differentiation along third dimension
167+ L = CenteredDifference {3} (4 ,4 ,0.1 ,20 )
168+ M_temp_3 = zeros (22 ,22 ,20 )
169+ for idx in indices
170+ M[idx] = sin (idx[3 ]* 0.1 )
171+ end
172+
173+ # test mul!
174+ mul! (M_temp_3, L, M)
175+ for i in 1 : 22
176+ for j in 1 : 22
177+ @test M_temp_3[i,j,:] ≈ correct_row
178+ end
179+ end
180+
181+ # Test that * agrees will mul!
182+ @test M_temp_3 == L* M
183+ end
184+
86185@testset " Differentiating an arbitrary higher dimension" begin
87186 N = 6
88187 L = CenteredDifference {N} (4 ,4 ,0.1 ,30 )
89- M = zeros (5 ,5 ,5 ,5 ,5 ,32 )
90- M_temp = zeros (5 ,5 ,5 ,5 ,5 ,30 )
91- indices = Iterators. drop (CartesianIndices ((5 ,5 ,5 ,5 ,5 ,32 )), 0 )
188+ M = zeros (5 ,5 ,5 ,5 ,5 ,32 , 5 );
189+ M_temp = zeros (5 ,5 ,5 ,5 ,5 ,30 , 5 );
190+ indices = Iterators. drop (CartesianIndices ((5 ,5 ,5 ,5 ,5 ,32 , 5 )), 0 );
92191 for idx in indices
93192 M[idx] = cos (idx[N]* 0.1 )
94193 end
95194
96- correct_row = (10.0 ^ 4 )* fourth_deriv_approx_stencil (30 )* M[1 ,1 ,1 ,1 ,1 ,:]
195+ correct_row = (10.0 ^ 4 )* fourth_deriv_approx_stencil (30 )* M[1 ,1 ,1 ,1 ,1 ,:, 1 ]
97196
98197 # test mul!
99198 mul! (M_temp, L, M)
100- indices = Iterators. drop (CartesianIndices ((5 ,5 ,5 ,5 ,5 ,30 )), 0 )
199+ indices = Iterators. drop (CartesianIndices ((5 ,5 ,5 ,5 ,5 ,30 , 5 )), 0 );
101200 for idx in indices
102201 @test M_temp[idx] ≈ correct_row[idx[N]]
103202 end
0 commit comments