Mercurial > repos > public > sbplib_julia
comparison test/SbpOperators/stencil_test.jl @ 2057:8a2a0d678d6f feature/lazy_tensors/pretty_printing
Merge default
| author | Jonatan Werpers <jonatan@werpers.com> |
|---|---|
| date | Tue, 10 Feb 2026 22:41:19 +0100 |
| parents | 471a948cd2b2 |
| children |
comparison
equal
deleted
inserted
replaced
| 1110:c0bff9f6e0fb | 2057:8a2a0d678d6f |
|---|---|
| 1 using Test | 1 using Test |
| 2 using Sbplib.SbpOperators | 2 using Diffinitive.SbpOperators |
| 3 import Sbplib.SbpOperators.Stencil | 3 using StaticArrays |
| 4 import Sbplib.SbpOperators.NestedStencil | 4 import Diffinitive.SbpOperators.Stencil |
| 5 import Sbplib.SbpOperators.scale | 5 import Diffinitive.SbpOperators.NestedStencil |
| 6 import Diffinitive.SbpOperators.scale | |
| 7 import Diffinitive.SbpOperators: apply_stencil, apply_stencil_backwards | |
| 6 | 8 |
| 7 @testset "Stencil" begin | 9 @testset "Stencil" begin |
| 8 s = Stencil(-2:2, (1.,2.,2.,3.,4.)) | 10 s = Stencil(-2:2, (1.,2.,2.,3.,4.)) |
| 9 @test s isa Stencil{Float64, 5} | 11 @test s isa Stencil{Float64, 5} |
| 10 | 12 |
| 42 | 44 |
| 43 @testset "promotion" begin | 45 @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)) | 46 @test promote(Stencil(1,1;center=1), Stencil(2.,2.;center=2)) == (Stencil(1.,1.;center=1), Stencil(2.,2.;center=2)) |
| 45 end | 47 end |
| 46 | 48 |
| 49 @testset "apply_stencil" begin | |
| 50 v = [1, 2, 4, 8, 16, 32, 64, 128] | |
| 51 s = Stencil(1,2,3,4, center = 2) | |
| 52 @test apply_stencil(s,v, 2) == v[1] + 2*v[2] + 3*v[3] + 4*v[4] | |
| 53 @test apply_stencil(s,v, 4) == v[3] + 2*v[4] + 3*v[5] + 4*v[6] | |
| 54 @test apply_stencil_backwards(s,v, 3) == 4*v[1] + 3*v[2] + 2*v[3] + 1*v[4] | |
| 55 @test apply_stencil_backwards(s,v, 7) == 4*v[5] + 3*v[6] + 2*v[7] + 1*v[8] | |
| 56 @test apply_stencil(s,v, 2) isa Int | |
| 57 @test apply_stencil_backwards(s,v, 7) isa Int | |
| 58 | |
| 59 v = [1, 2, 4, 8, 16, 32, 64, 128] | |
| 60 s = Stencil(1.,2.,3.,4., center = 2) | |
| 61 @test apply_stencil(s,v, 4) == v[3] + 2. *v[4] + 3. *v[5] + 4. *v[6] | |
| 62 @test apply_stencil_backwards(s,v, 7) == 4. *v[5] + 3. *v[6] + 2. *v[7] + v[8] | |
| 63 @test apply_stencil(s,v, 2) isa Float64 | |
| 64 @test apply_stencil_backwards(s,v, 7) isa Float64 | |
| 65 | |
| 66 v = [1., 2., 4., 8., 16., 32., 64., 128.] | |
| 67 s = Stencil(1,2,3,4, center = 2) | |
| 68 @test apply_stencil(s,v, 2) == v[1] + 2*v[2] + 3*v[3] + 4*v[4] | |
| 69 @test apply_stencil_backwards(s,v, 3) == 4*v[1] + 3*v[2] + 2*v[3] + 1*v[4] | |
| 70 @test apply_stencil(s,v, 2) isa Float64 | |
| 71 @test apply_stencil_backwards(s,v, 3) isa Float64 | |
| 72 | |
| 73 v = [@SVector[1, 2], @SVector[3, 4], @SVector[5, 6], @SVector[7, 8]] | |
| 74 s = Stencil(1,2, center = 1) | |
| 75 @test apply_stencil(s,v,1) == @SVector[7, 10] | |
| 76 @test apply_stencil_backwards(s,v,3) == @SVector[11, 14] | |
| 77 @test apply_stencil(s,v,1) isa SVector{2, Int} | |
| 78 @test apply_stencil_backwards(s,v,3) isa SVector{2, Int} | |
| 79 | |
| 80 v = [@SVector[1., 2.], @SVector[3., 4.], @SVector[5., 6.], @SVector[7., 8.]] | |
| 81 s = Stencil(1,2, center = 1) | |
| 82 @test apply_stencil(s,v,1) == @SVector[7., 10.] | |
| 83 @test apply_stencil_backwards(s,v,3) == @SVector[11., 14.] | |
| 84 @test apply_stencil(s,v,1) isa SVector{2, Float64} | |
| 85 @test apply_stencil_backwards(s,v,3) isa SVector{2, Float64} | |
| 86 | |
| 87 v = [@SVector[1, 2], @SVector[3, 4], @SVector[5, 6], @SVector[7, 8]] | |
| 88 s = Stencil(1.,2., center = 1) | |
| 89 @test apply_stencil(s,v,1) == @SVector[7., 10.] | |
| 90 @test apply_stencil_backwards(s,v,3) == @SVector[11., 14.] | |
| 91 @test apply_stencil(s,v,1) isa SVector{2, Float64} | |
| 92 @test apply_stencil_backwards(s,v,3) isa SVector{2, Float64} | |
| 93 end | |
| 94 | |
| 47 @testset "type stability" begin | 95 @testset "type stability" begin |
| 48 s_int = CenteredStencil(1,2,3) | 96 s_int = CenteredStencil(1,2,3) |
| 49 s_float = CenteredStencil(1.,2.,3.) | 97 s_float = CenteredStencil(1.,2.,3.) |
| 50 v_int = rand(1:10,10); | 98 v_int = rand(1:10,10); |
| 51 v_float = rand(10); | 99 v_float = rand(10); |
| 59 @inferred SbpOperators.apply_stencil_backwards(s_float, v_float, 5) | 107 @inferred SbpOperators.apply_stencil_backwards(s_float, v_float, 5) |
| 60 @inferred SbpOperators.apply_stencil_backwards(s_int, v_float, 5) | 108 @inferred SbpOperators.apply_stencil_backwards(s_int, v_float, 5) |
| 61 @inferred SbpOperators.apply_stencil_backwards(s_float, v_int, 5) | 109 @inferred SbpOperators.apply_stencil_backwards(s_float, v_int, 5) |
| 62 end | 110 end |
| 63 end | 111 end |
| 112 | |
| 113 @testset "left_pad" begin | |
| 114 @test SbpOperators.left_pad(Stencil(1,1, center = 1), 2) == Stencil(1,1, center=1) | |
| 115 @test SbpOperators.left_pad(Stencil(1,1, center = 1), 3) == Stencil(0,1,1, center=2) | |
| 116 @test SbpOperators.left_pad(Stencil(2,3, center = 2), 4) == Stencil(0,0,2,3, center=4) | |
| 117 | |
| 118 @test SbpOperators.left_pad(Stencil(2.,3., center = 2), 4) == Stencil(0.,0.,2.,3., center=4) | |
| 119 end | |
| 120 | |
| 121 @testset "right_pad" begin | |
| 122 @test SbpOperators.right_pad(Stencil(1,1, center = 1), 2) == Stencil(1,1, center=1) | |
| 123 @test SbpOperators.right_pad(Stencil(1,1, center = 1), 3) == Stencil(1,1,0, center=1) | |
| 124 @test SbpOperators.right_pad(Stencil(2,3, center = 2), 4) == Stencil(2,3,0,0, center=2) | |
| 125 | |
| 126 @test SbpOperators.right_pad(Stencil(2.,3., center = 2), 4) == Stencil(2.,3.,0.,0., center=2) | |
| 127 end | |
| 128 | |
| 64 | 129 |
| 65 @testset "NestedStencil" begin | 130 @testset "NestedStencil" begin |
| 66 | 131 |
| 67 @testset "Constructors" begin | 132 @testset "Constructors" begin |
| 68 s1 = CenteredStencil(-1, 1, 0) | 133 s1 = CenteredStencil(-1, 1, 0) |
| 136 @test SbpOperators.apply_inner_stencils(ns, c, 4) == Stencil(5,11,6; center=1) | 201 @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) | 202 @test SbpOperators.apply_inner_stencils_backwards(ns, c, 4) == Stencil(-4,-7,-3; center=1) |
| 138 | 203 |
| 139 @test SbpOperators.apply_stencil(ns, c, v, 4) == 5*7 + 11*11 + 6*13 | 204 @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 | 205 @test SbpOperators.apply_stencil_backwards(ns, c, v, 4) == -3*3 - 7*5 - 4*7 |
| 206 | |
| 207 # Different types in vector and stencil | |
| 208 ns = NestedStencil((-1.,1.,0.),(-1.,0.,1.),(0.,-2.,2.), center=2) | |
| 209 @test SbpOperators.apply_inner_stencils(ns, c, 4) isa Stencil{Float64, 3} | |
| 210 @test SbpOperators.apply_inner_stencils(ns, c, 4) == Stencil(4.,9.,10.; center=2) | |
| 211 @test SbpOperators.apply_inner_stencils_backwards(ns, c, 4) isa Stencil{Float64, 3} | |
| 212 @test SbpOperators.apply_inner_stencils_backwards(ns, c, 4) == Stencil(-5.,-9.,-8.; center=2) | |
| 213 | |
| 214 @test SbpOperators.apply_stencil(ns, c, v, 4) isa Float64 | |
| 215 @test SbpOperators.apply_stencil(ns, c, v, 4) == 193. | |
| 216 @test SbpOperators.apply_stencil_backwards(ns, c, v, 4) isa Float64 | |
| 217 @test SbpOperators.apply_stencil_backwards(ns, c, v, 4) == -158. | |
| 218 | |
| 219 # Arrays of vectors | |
| 220 ns = NestedStencil((-1.,1.,0.),(-1.,0.,1.),(0.,-2.,2.), center=2) | |
| 221 c = [ 1, 3, 6, 10] | |
| 222 v = [@SVector[1, 2], @SVector[3, 4], @SVector[5, 6], @SVector[7, 8]] | |
| 223 @test SbpOperators.apply_stencil(ns, c, v, 2) isa SVector{2,Float64} | |
| 224 @test SbpOperators.apply_stencil(ns, c, v, 2) == 2v[1] + 5v[2] + 6v[3] | |
| 225 @test SbpOperators.apply_stencil_backwards(ns, c, v, 2) isa SVector{2,Float64} | |
| 226 @test SbpOperators.apply_stencil_backwards(ns, c, v, 2) == -4v[1] - 5v[2] - 3v[3] | |
| 141 end | 227 end |
| 142 | 228 |
| 143 @testset "type stability" begin | 229 @testset "type stability" begin |
| 144 s_int = CenteredNestedStencil((1,2,3),(1,2,3),(1,2,3)) | 230 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.)) | 231 s_float = CenteredNestedStencil((1.,2.,3.),(1.,2.,3.),(1.,2.,3.)) |
| 168 @inferred SbpOperators.apply_stencil_backwards(s_int, c_float, v_int, 2) | 254 @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) | 255 @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) | 256 @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) | 257 @inferred SbpOperators.apply_stencil_backwards(s_float, c_float, v_int, 2) |
| 172 end | 258 end |
| 173 | 259 end |
| 174 end |
