comparison src/SbpOperators/stencil.jl @ 1863:516eaabf1169 refactor/grids/iterable_boundary_indices

Merge default
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 22 Jan 2025 09:00:40 +0100
parents 244311761969
children
comparison
equal deleted inserted replaced
1845:aa7da5c899d8 1863:516eaabf1169
1 struct Stencil{T,N} 1 struct Stencil{T,N}
2 range::UnitRange{Int64} 2 range::UnitRange{Int64}
3 weights::NTuple{N,T} 3 weights::NTuple{N,T}
4 4
5 function Stencil(range::UnitRange,weights::NTuple{N,T}) where {T, N} 5 function Stencil(range::UnitRange,weights::NTuple{N,Any}) where N
6 T = eltype(weights)
7
6 @assert length(range) == N 8 @assert length(range) == N
7 new{T,N}(range,weights) 9 new{T,N}(range,weights)
8 end 10 end
9 end 11 end
10 12
11 """ 13 """
12 Stencil(weights::NTuple; center::Int) 14 Stencil(weights...; center::Int)
13 15
14 Create a stencil with the given weights with element `center` as the center of the stencil. 16 Create a stencil with the given weights with element `center` as the center of the stencil.
15 """ 17 """
16 function Stencil(weights...; center::Int) 18 function Stencil(weights...; center::Int)
17 weights = promote(weights...) 19 weights = promote(weights...)
107 109
108 struct NestedStencil{T,N,M} 110 struct NestedStencil{T,N,M}
109 s::Stencil{Stencil{T,N},M} 111 s::Stencil{Stencil{T,N},M}
110 end 112 end
111 113
114 NestedStencil(;center) = NestedStencil(Stencil(;center))
115 CenteredNestedStencil() = NestedStencil(CenteredStencil())
116
112 # Stencil input 117 # Stencil input
113 NestedStencil(s::Vararg{Stencil}; center) = NestedStencil(Stencil(s... ; center)) 118 NestedStencil(s::Vararg{Stencil}; center) = NestedStencil(Stencil(s... ; center))
114 CenteredNestedStencil(s::Vararg{Stencil}) = NestedStencil(CenteredStencil(s...)) 119 CenteredNestedStencil(s::Vararg{Stencil}) = NestedStencil(CenteredStencil(s...))
115 120
116 # Tuple input 121 # Tuple input
117 function NestedStencil(weights::Vararg{NTuple{N,Any}}; center) where N 122 function NestedStencil(weights::Vararg{NTuple{N,Any} where N}; center)
118 inner_stencils = map(w -> Stencil(w...; center), weights) 123 inner_stencils = map(w -> Stencil(w...; center), weights)
119 return NestedStencil(Stencil(inner_stencils... ; center)) 124 return NestedStencil(Stencil(inner_stencils... ; center))
120 end 125 end
121 function CenteredNestedStencil(weights::Vararg{NTuple{N,Any}}) where N 126
127 function CenteredNestedStencil(weights::Vararg{NTuple{N,Any} where N})
122 inner_stencils = map(w->CenteredStencil(w...), weights) 128 inner_stencils = map(w->CenteredStencil(w...), weights)
123 return CenteredNestedStencil(inner_stencils...) 129 return CenteredNestedStencil(inner_stencils...)
124 end 130 end
125
126 131
127 # Conversion 132 # Conversion
128 function NestedStencil{T,N,M}(ns::NestedStencil{S,N,M}) where {T,S,N,M} 133 function NestedStencil{T,N,M}(ns::NestedStencil{S,N,M}) where {T,S,N,M}
129 return NestedStencil(Stencil{Stencil{T}}(ns.s)) 134 return NestedStencil(Stencil{Stencil{T}}(ns.s))
130 end 135 end
134 end 139 end
135 140
136 function Base.convert(::Type{NestedStencil{T,N,M}}, s::NestedStencil{S,N,M}) where {T,S,N,M} 141 function Base.convert(::Type{NestedStencil{T,N,M}}, s::NestedStencil{S,N,M}) where {T,S,N,M}
137 return NestedStencil{T,N,M}(s) 142 return NestedStencil{T,N,M}(s)
138 end 143 end
139 Base.convert(::Type{NestedStencil{T}}, stencil) where T = NestedStencil{T}(stencil) 144 Base.convert(::Type{NestedStencil{T}}, stencil::NestedStencil) where T = NestedStencil{T}(stencil)
140 145
141 function Base.promote_rule(::Type{NestedStencil{T,N,M}}, ::Type{NestedStencil{S,N,M}}) where {T,S,N,M} 146 function Base.promote_rule(::Type{NestedStencil{T,N,M}}, ::Type{NestedStencil{S,N,M}}) where {T,S,N,M}
142 return NestedStencil{promote_type(T,S),N,M} 147 return NestedStencil{promote_type(T,S),N,M}
143 end 148 end
144 149