annotate src/SbpOperators/stencil.jl @ 637:4a81812150f4 feature/volume_and_boundary_operators

Change qudrature closure from tuple of reals to tuple of Stencils. Also remove parametrization of stencil width in D2 since this was illformed for the 2nd order case.
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Sun, 03 Jan 2021 18:15:14 +0100
parents 03ef4d4740ab
children e14627e79a54
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
84
48079bd39969 Change to using tuples in stencils and ops
Jonatan Werpers <jonatan@werpers.com>
parents: 69
diff changeset
1 struct Stencil{T<:Real,N}
48079bd39969 Change to using tuples in stencils and ops
Jonatan Werpers <jonatan@werpers.com>
parents: 69
diff changeset
2 range::Tuple{Int,Int}
48079bd39969 Change to using tuples in stencils and ops
Jonatan Werpers <jonatan@werpers.com>
parents: 69
diff changeset
3 weights::NTuple{N,T}
126
66c239678a21 Add Assertion in stencil constructor
Ylva Rydin <ylva.rydin@telia.com>
parents: 85
diff changeset
4
66c239678a21 Add Assertion in stencil constructor
Ylva Rydin <ylva.rydin@telia.com>
parents: 85
diff changeset
5 function Stencil(range::Tuple{Int,Int},weights::NTuple{N,T}) where {T <: Real, N}
66c239678a21 Add Assertion in stencil constructor
Ylva Rydin <ylva.rydin@telia.com>
parents: 85
diff changeset
6 @assert range[2]-range[1]+1 == N
66c239678a21 Add Assertion in stencil constructor
Ylva Rydin <ylva.rydin@telia.com>
parents: 85
diff changeset
7 new{T,N}(range,weights)
66c239678a21 Add Assertion in stencil constructor
Ylva Rydin <ylva.rydin@telia.com>
parents: 85
diff changeset
8 end
8
433008d3b7d3 Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
9 end
433008d3b7d3 Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
10
584
4aa7fe13a984 Add scale() and eltype() methods for stencils
Jonatan Werpers <jonatan@werpers.com>
parents: 333
diff changeset
11 """
595
03ef4d4740ab Add a constructor for Stencil where you can specify the center of the stencil
Jonatan Werpers <jonatan@werpers.com>
parents: 584
diff changeset
12 Stencil(weights::NTuple; center::Int)
03ef4d4740ab Add a constructor for Stencil where you can specify the center of the stencil
Jonatan Werpers <jonatan@werpers.com>
parents: 584
diff changeset
13
03ef4d4740ab Add a constructor for Stencil where you can specify the center of the stencil
Jonatan Werpers <jonatan@werpers.com>
parents: 584
diff changeset
14 Create a stencil with the given weights with element `center` as the center of the stencil.
03ef4d4740ab Add a constructor for Stencil where you can specify the center of the stencil
Jonatan Werpers <jonatan@werpers.com>
parents: 584
diff changeset
15 """
03ef4d4740ab Add a constructor for Stencil where you can specify the center of the stencil
Jonatan Werpers <jonatan@werpers.com>
parents: 584
diff changeset
16 function Stencil(weights::NTuple; center::Int)
03ef4d4740ab Add a constructor for Stencil where you can specify the center of the stencil
Jonatan Werpers <jonatan@werpers.com>
parents: 584
diff changeset
17 N = length(weights)
03ef4d4740ab Add a constructor for Stencil where you can specify the center of the stencil
Jonatan Werpers <jonatan@werpers.com>
parents: 584
diff changeset
18 range = (1, N) .- center
03ef4d4740ab Add a constructor for Stencil where you can specify the center of the stencil
Jonatan Werpers <jonatan@werpers.com>
parents: 584
diff changeset
19
03ef4d4740ab Add a constructor for Stencil where you can specify the center of the stencil
Jonatan Werpers <jonatan@werpers.com>
parents: 584
diff changeset
20 return Stencil(range, weights)
03ef4d4740ab Add a constructor for Stencil where you can specify the center of the stencil
Jonatan Werpers <jonatan@werpers.com>
parents: 584
diff changeset
21 end
03ef4d4740ab Add a constructor for Stencil where you can specify the center of the stencil
Jonatan Werpers <jonatan@werpers.com>
parents: 584
diff changeset
22
03ef4d4740ab Add a constructor for Stencil where you can specify the center of the stencil
Jonatan Werpers <jonatan@werpers.com>
parents: 584
diff changeset
23 """
584
4aa7fe13a984 Add scale() and eltype() methods for stencils
Jonatan Werpers <jonatan@werpers.com>
parents: 333
diff changeset
24 scale(s::Stencil, a)
4aa7fe13a984 Add scale() and eltype() methods for stencils
Jonatan Werpers <jonatan@werpers.com>
parents: 333
diff changeset
25
4aa7fe13a984 Add scale() and eltype() methods for stencils
Jonatan Werpers <jonatan@werpers.com>
parents: 333
diff changeset
26 Scale the weights of the stencil `s` with `a` and return a new stencil.
4aa7fe13a984 Add scale() and eltype() methods for stencils
Jonatan Werpers <jonatan@werpers.com>
parents: 333
diff changeset
27 """
4aa7fe13a984 Add scale() and eltype() methods for stencils
Jonatan Werpers <jonatan@werpers.com>
parents: 333
diff changeset
28 function scale(s::Stencil, a)
4aa7fe13a984 Add scale() and eltype() methods for stencils
Jonatan Werpers <jonatan@werpers.com>
parents: 333
diff changeset
29 return Stencil(s.range, a.*s.weights)
4aa7fe13a984 Add scale() and eltype() methods for stencils
Jonatan Werpers <jonatan@werpers.com>
parents: 333
diff changeset
30 end
4aa7fe13a984 Add scale() and eltype() methods for stencils
Jonatan Werpers <jonatan@werpers.com>
parents: 333
diff changeset
31
4aa7fe13a984 Add scale() and eltype() methods for stencils
Jonatan Werpers <jonatan@werpers.com>
parents: 333
diff changeset
32 Base.eltype(::Stencil{T}) where T = T
4aa7fe13a984 Add scale() and eltype() methods for stencils
Jonatan Werpers <jonatan@werpers.com>
parents: 333
diff changeset
33
8
433008d3b7d3 Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
34 function flip(s::Stencil)
433008d3b7d3 Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
35 range = (-s.range[2], -s.range[1])
84
48079bd39969 Change to using tuples in stencils and ops
Jonatan Werpers <jonatan@werpers.com>
parents: 69
diff changeset
36 return Stencil(range, reverse(s.weights))
8
433008d3b7d3 Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
37 end
433008d3b7d3 Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
38
433008d3b7d3 Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
39 # Provides index into the Stencil based on offset for the root element
128
7c0b9bb7ab4d Improve stencil application code to make it more friendly to compiler optimizations
Jonatan Werpers <jonatan@werpers.com>
parents: 122
diff changeset
40 @inline function Base.getindex(s::Stencil, i::Int)
71
18d0d794d3bb Make stencils respond to @ inbounds
Jonatan Werpers <jonatan@werpers.com>
parents: 67
diff changeset
41 @boundscheck if i < s.range[1] || s.range[2] < i
584
4aa7fe13a984 Add scale() and eltype() methods for stencils
Jonatan Werpers <jonatan@werpers.com>
parents: 333
diff changeset
42 return zero(eltype(s))
8
433008d3b7d3 Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
43 end
71
18d0d794d3bb Make stencils respond to @ inbounds
Jonatan Werpers <jonatan@werpers.com>
parents: 67
diff changeset
44 return s.weights[1 + i - s.range[1]]
8
433008d3b7d3 Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
45 end
433008d3b7d3 Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
46
269
ccef055233a2 Move general methods for D2 to ConstantStencilOperator and increase verbosity of method names for clarity
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 219
diff changeset
47 Base.@propagate_inbounds @inline function apply_stencil(s::Stencil{T,N}, v::AbstractVector, i::Int) where {T,N}
131
8569c637d923 Enable compiler loop-unrolling in apply_backwards
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 129
diff changeset
48 w = s.weights[1]*v[i + s.range[1]]
129
1aaeb46ba5f4 Improve efficiency of apply by the following:
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 128
diff changeset
49 @simd for k ∈ 2:N
131
8569c637d923 Enable compiler loop-unrolling in apply_backwards
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 129
diff changeset
50 w += s.weights[k]*v[i + s.range[1] + k-1]
8
433008d3b7d3 Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
51 end
433008d3b7d3 Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
52 return w
433008d3b7d3 Move stencil to its own file
Jonatan Werpers <jonatan@werpers.com>
parents:
diff changeset
53 end
122
6c6979ff17f4 Introduce and use apply_backwards for stencils
Jonatan Werpers <jonatan@werpers.com>
parents: 85
diff changeset
54
269
ccef055233a2 Move general methods for D2 to ConstantStencilOperator and increase verbosity of method names for clarity
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 219
diff changeset
55 Base.@propagate_inbounds @inline function apply_stencil_backwards(s::Stencil{T,N}, v::AbstractVector, i::Int) where {T,N}
131
8569c637d923 Enable compiler loop-unrolling in apply_backwards
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 129
diff changeset
56 w = s.weights[N]*v[i - s.range[2]]
8569c637d923 Enable compiler loop-unrolling in apply_backwards
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 129
diff changeset
57 @simd for k ∈ N-1:-1:1
8569c637d923 Enable compiler loop-unrolling in apply_backwards
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 129
diff changeset
58 w += s.weights[k]*v[i - s.range[1] - k + 1]
122
6c6979ff17f4 Introduce and use apply_backwards for stencils
Jonatan Werpers <jonatan@werpers.com>
parents: 85
diff changeset
59 end
6c6979ff17f4 Introduce and use apply_backwards for stencils
Jonatan Werpers <jonatan@werpers.com>
parents: 85
diff changeset
60 return w
6c6979ff17f4 Introduce and use apply_backwards for stencils
Jonatan Werpers <jonatan@werpers.com>
parents: 85
diff changeset
61 end