changeset 311:f2d6ec89dfc5

Make apply dispatch on Index instead of Tuples of Index
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Wed, 09 Sep 2020 21:16:13 +0200
parents ece3f6f8a1d4
children c1fcc35e19cb
files SbpOperators/src/laplace/secondderivative.jl
diffstat 1 files changed, 24 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/SbpOperators/src/laplace/secondderivative.jl	Wed Sep 09 21:14:17 2020 +0200
+++ b/SbpOperators/src/laplace/secondderivative.jl	Wed Sep 09 21:16:13 2020 +0200
@@ -3,48 +3,49 @@
 Implements the Laplace tensor operator `L` with constant grid spacing and coefficients
 in 1D dimension
 """
-struct SecondDerivative{T<:Real,N,M,K} <: TensorOperator{T,1}
+
+struct SecondDerivative{T,N,M,K} <: TensorOperator{T,1}
     h_inv::T # The grid spacing could be included in the stencil already. Preferable?
     innerStencil::Stencil{T,N}
     closureStencils::NTuple{M,Stencil{T,K}}
     parity::Parity
     #TODO: Write a nice constructor
 end
-
-@enum Parity begin
-    odd = -1
-    even = 1
-end
+export SecondDerivative
 
 LazyTensors.domain_size(D2::SecondDerivative, range_size::NTuple{1,Integer}) = range_size
 
-@inline function LazyTensors.apply(D2::SecondDerivative{T}, v::AbstractVector{T}, I::NTuple{1,Index}) where T
-    return @inbounds apply(D2, v, I[1])
-end
-
-function LazyTensors.apply_transpose(D2::SecondDerivative{T}, v::AbstractVector{T}, I::NTuple{1,Index}) where T = LazyTensors.apply(D2, v, I)
+#TODO: The 1D tensor mappings should not have to dispatch on 1D tuples if we write LazyTensor.apply for vararg right?!?!
+#      Currently have to index the Tuple{Index} in each method in order to call the stencil methods which is ugly.
+#      I thought I::Vararg{Index,R} fell back to just Index for R = 1
 
 # Apply for different regions Lower/Interior/Upper or Unknown region
-@inline function LazyTensors.apply(D2::SecondDerivative, v::AbstractVector, i::Index{Lower})
-    return @inbounds D2.h_inv*D2.h_inv*apply_stencil(D2.closureStencils[Int(i)], v, Int(i))
+@inline function LazyTensors.apply(D2::SecondDerivative{T}, v::AbstractVector{T}, I::Index{Lower}) where T
+    return @inbounds D2.h_inv*D2.h_inv*apply_stencil(D2.closureStencils[Int(I)], v, Int(I))
 end
 
-@inline function LazyTensors.apply(D2::SecondDerivative, v::AbstractVector, i::Index{Interior})
-    return @inbounds D2.h_inv*D2.h_inv*apply_stencil(D2.innerStencil, v, Int(i))
+@inline function LazyTensors.apply(D2::SecondDerivative{T}, v::AbstractVector{T}, I::Index{Interior}) where T
+    return @inbounds D2.h_inv*D2.h_inv*apply_stencil(D2.innerStencil, v, Int(I))
 end
 
-@inline function LazyTensors.apply(D2::SecondDerivative, v::AbstractVector, i::Index{Upper})
-    N = length(v) # TODO: Use domain_size here instead?
-    return @inbounds D2.h_inv*D2.h_inv*Int(D2.parity)*apply_stencil_backwards(D2.closureStencils[N-Int(i)+1], v, Int(i))
+@inline function LazyTensors.apply(D2::SecondDerivative{T}, v::AbstractVector{T}, I::Index{Upper}) where T
+    N = length(v) # TODO: Use domain_size here instead? N = domain_size(D2,size(v))
+    return @inbounds D2.h_inv*D2.h_inv*Int(D2.parity)*apply_stencil_backwards(D2.closureStencils[N-Int(I)+1], v, Int(I))
 end
 
-@inline function LazyTensors.apply(D2::SecondDerivative, v::AbstractVector, index::Index{Unknown})
+@inline function LazyTensors.apply(D2::SecondDerivative{T}, v::AbstractVector{T}, index::Index{Unknown}) where T
     N = length(v)  # TODO: Use domain_size here instead?
-    r = getregion(Int(index), closuresize(L), N)
-    i = Index(Int(index), r)
-    return apply(D2, v, i)
+    r = getregion(Int(index), closuresize(D2), N)
+    I = Index(Int(index), r)
+    return LazyTensors.apply(D2, v, I)
 end
 
-function closuresize(D2::SecondDerivative{T<:Real,N,M,K}) where {T,N,M,K}
+
+@inline function LazyTensors.apply_transpose(D2::SecondDerivative, v::AbstractVector, I::Index)
+    return LazyTensors.apply(D2, v, I)
+end
+
+
+function closuresize(D2::SecondDerivative{T,N,M,K}) where {T<:Real,N,M,K}
     return M
 end