comparison src/SbpOperators/stencil.jl @ 345:2fcc960836c6

Merge branch refactor/combine_to_one_package.
author Jonatan Werpers <jonatan@werpers.com>
date Sat, 26 Sep 2020 15:22:13 +0200
parents 01b851161018
children 4aa7fe13a984
comparison
equal deleted inserted replaced
343:e12c9a866513 345:2fcc960836c6
1 struct Stencil{T<:Real,N}
2 range::Tuple{Int,Int}
3 weights::NTuple{N,T}
4
5 function Stencil(range::Tuple{Int,Int},weights::NTuple{N,T}) where {T <: Real, N}
6 @assert range[2]-range[1]+1 == N
7 new{T,N}(range,weights)
8 end
9 end
10
11 function flip(s::Stencil)
12 range = (-s.range[2], -s.range[1])
13 return Stencil(range, reverse(s.weights))
14 end
15
16 # Provides index into the Stencil based on offset for the root element
17 @inline function Base.getindex(s::Stencil, i::Int)
18 @boundscheck if i < s.range[1] || s.range[2] < i
19 return eltype(s.weights)(0)
20 end
21 return s.weights[1 + i - s.range[1]]
22 end
23
24 Base.@propagate_inbounds @inline function apply_stencil(s::Stencil{T,N}, v::AbstractVector, i::Int) where {T,N}
25 w = s.weights[1]*v[i + s.range[1]]
26 @simd for k ∈ 2:N
27 w += s.weights[k]*v[i + s.range[1] + k-1]
28 end
29 return w
30 end
31
32 Base.@propagate_inbounds @inline function apply_stencil_backwards(s::Stencil{T,N}, v::AbstractVector, i::Int) where {T,N}
33 w = s.weights[N]*v[i - s.range[2]]
34 @simd for k ∈ N-1:-1:1
35 w += s.weights[k]*v[i - s.range[1] - k + 1]
36 end
37 return w
38 end