Mercurial > repos > public > sbplib_julia
changeset 1053:cb0a5f41be02 feature/nested_stencils
Add some tests and fix some bugs
(grafted from e37ee63bf9ace6cfb3bdd1c24c3e4a99e6650652)
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Thu, 20 Jan 2022 10:23:15 +0100 |
parents | ca718fd4e816 |
children | ce16519aa7ff |
files | src/SbpOperators/stencil.jl test/SbpOperators/stencil_test.jl |
diffstat | 2 files changed, 21 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/src/SbpOperators/stencil.jl Wed Jan 19 21:49:34 2022 +0100 +++ b/src/SbpOperators/stencil.jl Thu Jan 20 10:23:15 2022 +0100 @@ -88,7 +88,7 @@ NestedStencil(s::Vararg{Stencil}; center) = NestedStencil(Stencil(s... ; center)) function NestedStencil(weights::Vararg{Tuple}; center) - inner_stencils = map((w,c) -> Stencil(w...,center=c), weights, 1:length(weights)) + inner_stencils = map(w -> Stencil(w...; center), weights) return NestedStencil(Stencil(inner_stencils... ; center)) end @@ -104,25 +104,25 @@ Base.getindex(ns::NestedStencil, i::Int) = ns.s[i] "Apply inner stencils to `c` and get a concrete stencil" -Base.@propagate_inbounds function apply_stencil(ns::NestedStencil, c::AbstractVector, i::Int) +Base.@propagate_inbounds function apply_inner_stencils(ns::NestedStencil, c::AbstractVector, i::Int) weights = apply_stencil.(ns.s.weights, Ref(c), i) return Stencil(ns.s.range, weights) end "Apply the whole nested stencil" Base.@propagate_inbounds function apply_stencil(ns::NestedStencil, c::AbstractVector, v::AbstractVector, i::Int) - s = apply_stencil(ns,c,i) + s = apply_inner_stencils(ns,c,i) return apply_stencil(s, v, i) end "Apply inner stencils backwards to `c` and get a concrete stencil" -Base.@propagate_inbounds @inline function apply_stencil_backwards(ns::NestedStencil, c::AbstractVector, v::AbstractVector, i::Int) +Base.@propagate_inbounds @inline function apply_inner_stencils_backwards(ns::NestedStencil, c::AbstractVector, i::Int) weights = apply_stencil_backwards.(ns.s.weights, Ref(c), i) return Stencil(ns.s.range, weights) end "Apply the whole nested stencil backwards" Base.@propagate_inbounds @inline function apply_stencil_backwards(ns::NestedStencil, c::AbstractVector, v::AbstractVector, i::Int) - s = apply_stencil_backwards(ns,c,i) + s = apply_inner_stencils_backwards(ns,c,i) return apply_stencil_backwards(s, v, i) end
--- a/test/SbpOperators/stencil_test.jl Wed Jan 19 21:49:34 2022 +0100 +++ b/test/SbpOperators/stencil_test.jl Thu Jan 20 10:23:15 2022 +0100 @@ -35,9 +35,9 @@ @testset "NestedStencil" begin @testset "Constructors" begin - s1 = Stencil(-1, 1, 0; center = 1) - s2 = Stencil(-1, 0, 1; center = 2) - s3 = Stencil( 0,-1, 1; center = 3) + s1 = CenteredStencil(-1, 1, 0) + s2 = CenteredStencil(-1, 0, 1) + s3 = CenteredStencil( 0,-1, 1) ns = NestedStencil(CenteredStencil(s1,s2,s3)) @test ns isa NestedStencil{Int,3} @@ -51,7 +51,19 @@ @testset "Error handling" begin - end end + + @testset "apply" begin + c = [ 1, 3, 6, 10, 15, 21, 28, 36, 45, 55] + v = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29] + + ns = NestedStencil((-1,1,0),(-1,0,1),(0,-2,2), center=2) + + @test SbpOperators.apply_inner_stencils(ns, c, 4) == Stencil(4,9,10; center=2) + @test SbpOperators.apply_inner_stencils_backwards(ns, c, 4) == Stencil(-5,-9,-8; center=2) + + @test SbpOperators.apply_stencil(ns, c, v, 4) == 4*5 + 9*7 + 10*11 + @test SbpOperators.apply_stencil_backwards(ns, c, v, 4) == -8*5 - 9*7 - 5*11 + end end