comparison src/SbpOperators/stencil.jl @ 866:1784b1c0af3e feature/laplace_opset

Merge with default
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Wed, 19 Jan 2022 14:44:24 +0100
parents 4433be383840
children 313648b01504 11767fbb29f4
comparison
equal deleted inserted replaced
865:545a6c1a0a0e 866:1784b1c0af3e
1 export CenteredStencil 1 export CenteredStencil
2 2
3 struct Stencil{T<:Real,N} 3 struct Stencil{T,N}
4 range::Tuple{Int,Int} 4 range::Tuple{Int,Int}
5 weights::NTuple{N,T} 5 weights::NTuple{N,T}
6 6
7 function Stencil(range::Tuple{Int,Int},weights::NTuple{N,T}) where {T <: Real, N} 7 function Stencil(range::Tuple{Int,Int},weights::NTuple{N,T}) where {T, N}
8 @assert range[2]-range[1]+1 == N 8 @assert range[2]-range[1]+1 == N
9 new{T,N}(range,weights) 9 new{T,N}(range,weights)
10 end 10 end
11 end 11 end
12 12
13 """ 13 """
14 Stencil(weights::NTuple; center::Int) 14 Stencil(weights::NTuple; center::Int)
15 15
16 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.
17 """ 17 """
18 function Stencil(weights::Vararg{Number}; center::Int) 18 function Stencil(weights::Vararg{T}; center::Int) where T # Type parameter T makes sure the weights are valid for the Stencil constuctors and throws an earlier, more readable, error
19 N = length(weights) 19 N = length(weights)
20 range = (1, N) .- center 20 range = (1, N) .- center
21 21
22 return Stencil(range, weights) 22 return Stencil(range, weights)
23 end 23 end
24
25 function Stencil{T}(s::Stencil) where T
26 return Stencil(s.range, T.(s.weights))
27 end
28
29 Base.convert(::Type{Stencil{T}}, stencil) where T = Stencil{T}(stencil)
24 30
25 function CenteredStencil(weights::Vararg) 31 function CenteredStencil(weights::Vararg)
26 if iseven(length(weights)) 32 if iseven(length(weights))
27 throw(ArgumentError("a centered stencil must have an odd number of weights.")) 33 throw(ArgumentError("a centered stencil must have an odd number of weights."))
28 end 34 end