diff src/SbpOperators/stencil.jl @ 898:cd6d71781137 feature/variable_derivatives

Add promotion functionality for NestedTuples
author Jonatan Werpers <jonatan@werpers.com>
date Sun, 13 Feb 2022 21:40:23 +0100
parents 737cd68318c7
children 18f63f1a0c44
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)