changeset 1061:d1ddcdc098bf feature/nested_stencils

Add promotion functionality for NestedTuples (grafted from cd6d71781137b29d5a9a16b7162051163e4314a4)
author Jonatan Werpers <jonatan@werpers.com>
date Sun, 13 Feb 2022 21:40:23 +0100
parents ff0e819f2075
children a76830879c63
files src/SbpOperators/stencil.jl test/SbpOperators/stencil_test.jl
diffstat 2 files changed, 31 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/SbpOperators/stencil.jl	Sat Feb 12 22:02:06 2022 +0100
+++ b/src/SbpOperators/stencil.jl	Sun Feb 13 21:40:23 2022 +0100
@@ -90,30 +90,39 @@
     s::Stencil{Stencil{T,N},M}
 end
 
-# The exessive use of type parameters for the following constructors are to catch errors earlier with clearer error messages
-
 # Stencil input
-NestedStencil(s::Vararg{Stencil{T,N}}; center) where {T,N} = NestedStencil(Stencil(s... ; center))
-CenteredNestedStencil(s::Vararg{Stencil{T,N}}) where {T,N} = NestedStencil(CenteredStencil(s...))
+NestedStencil(s::Vararg{Stencil}; center) = NestedStencil(Stencil(s... ; center))
+CenteredNestedStencil(s::Vararg{Stencil}) = NestedStencil(CenteredStencil(s...))
 
 # Tuple input
-function NestedStencil(weights::Vararg{NTuple{N,T}}; center) where {T,N}
+function NestedStencil(weights::Vararg{NTuple{N,Any}}; center) where N
     inner_stencils = map(w -> Stencil(w...; center), weights)
     return NestedStencil(Stencil(inner_stencils... ; center))
 end
-function CenteredNestedStencil(weights::Vararg{NTuple{N,T}}) where {T,N}
+function CenteredNestedStencil(weights::Vararg{NTuple{N,Any}}) where N
     inner_stencils = map(w->CenteredStencil(w...), weights)
     return CenteredNestedStencil(inner_stencils...)
 end
 
 
 # Conversion
-function NestedStencil{T}(ns::NestedStencil) where T
+function NestedStencil{T,N,M}(ns::NestedStencil{S,N,M}) where {T,S,N,M}
     return NestedStencil(Stencil{Stencil{T}}(ns.s))
 end
 
+function NestedStencil{T}(ns::NestedStencil{S,N,M}) where {T,S,N,M}
+    NestedStencil{T,N,M}(ns)
+end
+
+function Base.convert(::Type{NestedStencil{T,N,M}}, s::NestedStencil{S,N,M}) where {T,S,N,M}
+    return NestedStencil{T,N,M}(s)
+end
 Base.convert(::Type{NestedStencil{T}}, stencil) where T = NestedStencil{T}(stencil)
 
+function Base.promote_rule(::Type{NestedStencil{T,N,M}}, ::Type{NestedStencil{S,N,M}}) where {T,S,N,M}
+    return NestedStencil{promote_type(T,S),N,M}
+end
+
 Base.eltype(::NestedStencil{T}) where T = T
 
 function scale(ns::NestedStencil, a)
--- a/test/SbpOperators/stencil_test.jl	Sat Feb 12 22:02:06 2022 +0100
+++ b/test/SbpOperators/stencil_test.jl	Sun Feb 13 21:40:23 2022 +0100
@@ -87,6 +87,21 @@
         @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)
     end
 
+    @testset "promotion of weights" begin
+        @test NestedStencil((-1,1,0),(-1.,0.,1.),(0,-1,1), center=2) isa NestedStencil{Float64,3,3}
+        @test NestedStencil((-1,1,0),(-1,0,1),(0//1,-1,1), center=2) isa NestedStencil{Rational{Int64},3,3}
+    end
+
+    @testset "promotion" begin
+        promote(
+            CenteredNestedStencil((-1,1,0),(-1,0,1),(0,-1,1)),
+            CenteredNestedStencil((-1.,1.,0.),(-1.,0.,1.),(0.,-1.,1.))
+        ) == (
+            CenteredNestedStencil((-1.,1.,0.),(-1.,0.,1.),(0.,-1.,1.)),
+            CenteredNestedStencil((-1.,1.,0.),(-1.,0.,1.),(0.,-1.,1.))
+        )
+    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]