comparison src/SbpOperators/stencil.jl @ 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 18f63f1a0c44
comparison
equal deleted inserted replaced
1060:ff0e819f2075 1061:d1ddcdc098bf
88 88
89 struct NestedStencil{T,N,M} 89 struct NestedStencil{T,N,M}
90 s::Stencil{Stencil{T,N},M} 90 s::Stencil{Stencil{T,N},M}
91 end 91 end
92 92
93 # The exessive use of type parameters for the following constructors are to catch errors earlier with clearer error messages
94
95 # Stencil input 93 # Stencil input
96 NestedStencil(s::Vararg{Stencil{T,N}}; center) where {T,N} = NestedStencil(Stencil(s... ; center)) 94 NestedStencil(s::Vararg{Stencil}; center) = NestedStencil(Stencil(s... ; center))
97 CenteredNestedStencil(s::Vararg{Stencil{T,N}}) where {T,N} = NestedStencil(CenteredStencil(s...)) 95 CenteredNestedStencil(s::Vararg{Stencil}) = NestedStencil(CenteredStencil(s...))
98 96
99 # Tuple input 97 # Tuple input
100 function NestedStencil(weights::Vararg{NTuple{N,T}}; center) where {T,N} 98 function NestedStencil(weights::Vararg{NTuple{N,Any}}; center) where N
101 inner_stencils = map(w -> Stencil(w...; center), weights) 99 inner_stencils = map(w -> Stencil(w...; center), weights)
102 return NestedStencil(Stencil(inner_stencils... ; center)) 100 return NestedStencil(Stencil(inner_stencils... ; center))
103 end 101 end
104 function CenteredNestedStencil(weights::Vararg{NTuple{N,T}}) where {T,N} 102 function CenteredNestedStencil(weights::Vararg{NTuple{N,Any}}) where N
105 inner_stencils = map(w->CenteredStencil(w...), weights) 103 inner_stencils = map(w->CenteredStencil(w...), weights)
106 return CenteredNestedStencil(inner_stencils...) 104 return CenteredNestedStencil(inner_stencils...)
107 end 105 end
108 106
109 107
110 # Conversion 108 # Conversion
111 function NestedStencil{T}(ns::NestedStencil) where T 109 function NestedStencil{T,N,M}(ns::NestedStencil{S,N,M}) where {T,S,N,M}
112 return NestedStencil(Stencil{Stencil{T}}(ns.s)) 110 return NestedStencil(Stencil{Stencil{T}}(ns.s))
113 end 111 end
114 112
113 function NestedStencil{T}(ns::NestedStencil{S,N,M}) where {T,S,N,M}
114 NestedStencil{T,N,M}(ns)
115 end
116
117 function Base.convert(::Type{NestedStencil{T,N,M}}, s::NestedStencil{S,N,M}) where {T,S,N,M}
118 return NestedStencil{T,N,M}(s)
119 end
115 Base.convert(::Type{NestedStencil{T}}, stencil) where T = NestedStencil{T}(stencil) 120 Base.convert(::Type{NestedStencil{T}}, stencil) where T = NestedStencil{T}(stencil)
121
122 function Base.promote_rule(::Type{NestedStencil{T,N,M}}, ::Type{NestedStencil{S,N,M}}) where {T,S,N,M}
123 return NestedStencil{promote_type(T,S),N,M}
124 end
116 125
117 Base.eltype(::NestedStencil{T}) where T = T 126 Base.eltype(::NestedStencil{T}) where T = T
118 127
119 function scale(ns::NestedStencil, a) 128 function scale(ns::NestedStencil, a)
120 range = ns.s.range 129 range = ns.s.range