comparison src/SbpOperators/stencil.jl @ 880:f74189c954d6 feature/variable_derivatives

Add argument restrictions to stencil constructors to get clearer error messages
author Jonatan Werpers <jonatan@werpers.com>
date Thu, 20 Jan 2022 15:17:37 +0100
parents 011d3ff65e9f
children 0be29e65521e
comparison
equal deleted inserted replaced
879:011d3ff65e9f 880:f74189c954d6
27 return Stencil(s.range, T.(s.weights)) 27 return Stencil(s.range, T.(s.weights))
28 end 28 end
29 29
30 Base.convert(::Type{Stencil{T}}, stencil) where T = Stencil{T}(stencil) 30 Base.convert(::Type{Stencil{T}}, stencil) where T = Stencil{T}(stencil)
31 31
32 function CenteredStencil(weights::Vararg) 32 function CenteredStencil(weights::Vararg{T}) where T
33 if iseven(length(weights)) 33 if iseven(length(weights))
34 throw(ArgumentError("a centered stencil must have an odd number of weights.")) 34 throw(ArgumentError("a centered stencil must have an odd number of weights."))
35 end 35 end
36 36
37 r = length(weights) ÷ 2 37 r = length(weights) ÷ 2
79 end 79 end
80 return w 80 return w
81 end 81 end
82 82
83 83
84 struct NestedStencil{T,N} 84 struct NestedStencil{T,N,M}
85 s::Stencil{Stencil{T,N},N} 85 s::Stencil{Stencil{T,N},M}
86 end 86 end
87 87
88 # The exessive use of type parameters for the following constructors are to catch errors earlier with clearer error messages
89
88 # Stencil input 90 # Stencil input
89 NestedStencil(s::Vararg{Stencil}; center) = NestedStencil(Stencil(s... ; center)) 91 NestedStencil(s::Vararg{Stencil{T,N}}; center) where {T,N} = NestedStencil(Stencil(s... ; center))
90 CenteredNestedStencil(s::Vararg{Stencil}) = NestedStencil(CenteredStencil(s...)) 92 CenteredNestedStencil(s::Vararg{Stencil{T,N}}) where {T,N} = NestedStencil(CenteredStencil(s...))
91 93
92 # Tuple input 94 # Tuple input
93 function NestedStencil(weights::Vararg{Tuple}; center) 95 function NestedStencil(weights::Vararg{NTuple{N,T}}; center) where {T,N}
94 inner_stencils = map(w -> Stencil(w...; center), weights) 96 inner_stencils = map(w -> Stencil(w...; center), weights)
95 return NestedStencil(Stencil(inner_stencils... ; center)) 97 return NestedStencil(Stencil(inner_stencils... ; center))
96 end 98 end
97 function CenteredNestedStencil(weights::Vararg{Tuple}) 99 function CenteredNestedStencil(weights::Vararg{NTuple{N,T}}) where {T,N}
98 inner_stencils = map(w->CenteredStencil(w...), weights) 100 inner_stencils = map(w->CenteredStencil(w...), weights)
99 return CenteredNestedStencil(inner_stencils...) 101 return CenteredNestedStencil(inner_stencils...)
100 end 102 end
101 103
102 Base.eltype(::NestedStencil{T}) where T = T 104 Base.eltype(::NestedStencil{T}) where T = T