Mercurial > repos > public > sbplib_julia
annotate src/SbpOperators/laplace/secondderivative.jl @ 371:241bd2512c20 feature/lazy_function
Add a LazyFunctionArray that evaluates a function for each index.
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Wed, 30 Sep 2020 19:48:17 +0200 |
parents | e73af120ad38 |
children | 81053b1992b6 |
rev | line source |
---|---|
286
7247e85dc1e8
Start separating ConstantStencilOp into multiple 1D tensor mappings, e.g. ConstantLaplaceOp. Sketch an implementation of the multi-D laplace tensor operator as a tuple of 1D laplace tensor operators.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
1 """ |
293
f63232aeb1c6
Move laplace.jl from DiffOps to SbpOperators. Rename constandlaplace to secondderivative
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
292
diff
changeset
|
2 SecondDerivative{T<:Real,N,M,K} <: TensorOperator{T,1} |
286
7247e85dc1e8
Start separating ConstantStencilOp into multiple 1D tensor mappings, e.g. ConstantLaplaceOp. Sketch an implementation of the multi-D laplace tensor operator as a tuple of 1D laplace tensor operators.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
3 Implements the Laplace tensor operator `L` with constant grid spacing and coefficients |
7247e85dc1e8
Start separating ConstantStencilOp into multiple 1D tensor mappings, e.g. ConstantLaplaceOp. Sketch an implementation of the multi-D laplace tensor operator as a tuple of 1D laplace tensor operators.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
4 in 1D dimension |
7247e85dc1e8
Start separating ConstantStencilOp into multiple 1D tensor mappings, e.g. ConstantLaplaceOp. Sketch an implementation of the multi-D laplace tensor operator as a tuple of 1D laplace tensor operators.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
5 """ |
311
f2d6ec89dfc5
Make apply dispatch on Index instead of Tuples of Index
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
299
diff
changeset
|
6 |
348
7fe43d902a27
Start trying to change LazyTensors
Jonatan Werpers <jonatan@werpers.com>
parents:
333
diff
changeset
|
7 struct SecondDerivative{T,N,M,K} <: TensorMapping{T,1,1} |
286
7247e85dc1e8
Start separating ConstantStencilOp into multiple 1D tensor mappings, e.g. ConstantLaplaceOp. Sketch an implementation of the multi-D laplace tensor operator as a tuple of 1D laplace tensor operators.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
8 h_inv::T # The grid spacing could be included in the stencil already. Preferable? |
7247e85dc1e8
Start separating ConstantStencilOp into multiple 1D tensor mappings, e.g. ConstantLaplaceOp. Sketch an implementation of the multi-D laplace tensor operator as a tuple of 1D laplace tensor operators.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
9 innerStencil::Stencil{T,N} |
7247e85dc1e8
Start separating ConstantStencilOp into multiple 1D tensor mappings, e.g. ConstantLaplaceOp. Sketch an implementation of the multi-D laplace tensor operator as a tuple of 1D laplace tensor operators.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
10 closureStencils::NTuple{M,Stencil{T,K}} |
7247e85dc1e8
Start separating ConstantStencilOp into multiple 1D tensor mappings, e.g. ConstantLaplaceOp. Sketch an implementation of the multi-D laplace tensor operator as a tuple of 1D laplace tensor operators.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
11 parity::Parity |
356
0844069ab5ff
Reinclude SbpOperators and fix most of the code and tests there.
Jonatan Werpers <jonatan@werpers.com>
parents:
348
diff
changeset
|
12 size::NTuple{1,Int} |
286
7247e85dc1e8
Start separating ConstantStencilOp into multiple 1D tensor mappings, e.g. ConstantLaplaceOp. Sketch an implementation of the multi-D laplace tensor operator as a tuple of 1D laplace tensor operators.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
13 end |
311
f2d6ec89dfc5
Make apply dispatch on Index instead of Tuples of Index
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
299
diff
changeset
|
14 export SecondDerivative |
286
7247e85dc1e8
Start separating ConstantStencilOp into multiple 1D tensor mappings, e.g. ConstantLaplaceOp. Sketch an implementation of the multi-D laplace tensor operator as a tuple of 1D laplace tensor operators.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
15 |
356
0844069ab5ff
Reinclude SbpOperators and fix most of the code and tests there.
Jonatan Werpers <jonatan@werpers.com>
parents:
348
diff
changeset
|
16 function SecondDerivative(grid::EquidistantGrid{1}, innerStencil, closureStencils) |
0844069ab5ff
Reinclude SbpOperators and fix most of the code and tests there.
Jonatan Werpers <jonatan@werpers.com>
parents:
348
diff
changeset
|
17 h_inv = grid.inverse_spacing[1] |
0844069ab5ff
Reinclude SbpOperators and fix most of the code and tests there.
Jonatan Werpers <jonatan@werpers.com>
parents:
348
diff
changeset
|
18 return SecondDerivative(h_inv, innerStencil, closureStencils, even, size(grid)) |
0844069ab5ff
Reinclude SbpOperators and fix most of the code and tests there.
Jonatan Werpers <jonatan@werpers.com>
parents:
348
diff
changeset
|
19 end |
0844069ab5ff
Reinclude SbpOperators and fix most of the code and tests there.
Jonatan Werpers <jonatan@werpers.com>
parents:
348
diff
changeset
|
20 |
0844069ab5ff
Reinclude SbpOperators and fix most of the code and tests there.
Jonatan Werpers <jonatan@werpers.com>
parents:
348
diff
changeset
|
21 LazyTensors.range_size(D2::SecondDerivative) = D2.size |
0844069ab5ff
Reinclude SbpOperators and fix most of the code and tests there.
Jonatan Werpers <jonatan@werpers.com>
parents:
348
diff
changeset
|
22 LazyTensors.domain_size(D2::SecondDerivative) = D2.size |
286
7247e85dc1e8
Start separating ConstantStencilOp into multiple 1D tensor mappings, e.g. ConstantLaplaceOp. Sketch an implementation of the multi-D laplace tensor operator as a tuple of 1D laplace tensor operators.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
23 |
311
f2d6ec89dfc5
Make apply dispatch on Index instead of Tuples of Index
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
299
diff
changeset
|
24 #TODO: The 1D tensor mappings should not have to dispatch on 1D tuples if we write LazyTensor.apply for vararg right?!?! |
f2d6ec89dfc5
Make apply dispatch on Index instead of Tuples of Index
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
299
diff
changeset
|
25 # Currently have to index the Tuple{Index} in each method in order to call the stencil methods which is ugly. |
f2d6ec89dfc5
Make apply dispatch on Index instead of Tuples of Index
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
299
diff
changeset
|
26 # I thought I::Vararg{Index,R} fell back to just Index for R = 1 |
299
27a0bca5e1f2
Add apply_transpose and fix minor issues
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
293
diff
changeset
|
27 |
286
7247e85dc1e8
Start separating ConstantStencilOp into multiple 1D tensor mappings, e.g. ConstantLaplaceOp. Sketch an implementation of the multi-D laplace tensor operator as a tuple of 1D laplace tensor operators.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
28 # Apply for different regions Lower/Interior/Upper or Unknown region |
328
9cc5d1498b2d
Refactor 1D diagonal inner product in quadrature.jl to separate file. Write tests for quadratures. Clean up laplace and secondderivative
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
311
diff
changeset
|
29 function LazyTensors.apply(D2::SecondDerivative{T}, v::AbstractVector{T}, I::Index{Lower}) where T |
311
f2d6ec89dfc5
Make apply dispatch on Index instead of Tuples of Index
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
299
diff
changeset
|
30 return @inbounds D2.h_inv*D2.h_inv*apply_stencil(D2.closureStencils[Int(I)], v, Int(I)) |
286
7247e85dc1e8
Start separating ConstantStencilOp into multiple 1D tensor mappings, e.g. ConstantLaplaceOp. Sketch an implementation of the multi-D laplace tensor operator as a tuple of 1D laplace tensor operators.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
31 end |
7247e85dc1e8
Start separating ConstantStencilOp into multiple 1D tensor mappings, e.g. ConstantLaplaceOp. Sketch an implementation of the multi-D laplace tensor operator as a tuple of 1D laplace tensor operators.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
32 |
328
9cc5d1498b2d
Refactor 1D diagonal inner product in quadrature.jl to separate file. Write tests for quadratures. Clean up laplace and secondderivative
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
311
diff
changeset
|
33 function LazyTensors.apply(D2::SecondDerivative{T}, v::AbstractVector{T}, I::Index{Interior}) where T |
311
f2d6ec89dfc5
Make apply dispatch on Index instead of Tuples of Index
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
299
diff
changeset
|
34 return @inbounds D2.h_inv*D2.h_inv*apply_stencil(D2.innerStencil, v, Int(I)) |
286
7247e85dc1e8
Start separating ConstantStencilOp into multiple 1D tensor mappings, e.g. ConstantLaplaceOp. Sketch an implementation of the multi-D laplace tensor operator as a tuple of 1D laplace tensor operators.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
35 end |
7247e85dc1e8
Start separating ConstantStencilOp into multiple 1D tensor mappings, e.g. ConstantLaplaceOp. Sketch an implementation of the multi-D laplace tensor operator as a tuple of 1D laplace tensor operators.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
36 |
328
9cc5d1498b2d
Refactor 1D diagonal inner product in quadrature.jl to separate file. Write tests for quadratures. Clean up laplace and secondderivative
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
311
diff
changeset
|
37 function LazyTensors.apply(D2::SecondDerivative{T}, v::AbstractVector{T}, I::Index{Upper}) where T |
311
f2d6ec89dfc5
Make apply dispatch on Index instead of Tuples of Index
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
299
diff
changeset
|
38 N = length(v) # TODO: Use domain_size here instead? N = domain_size(D2,size(v)) |
f2d6ec89dfc5
Make apply dispatch on Index instead of Tuples of Index
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
299
diff
changeset
|
39 return @inbounds D2.h_inv*D2.h_inv*Int(D2.parity)*apply_stencil_backwards(D2.closureStencils[N-Int(I)+1], v, Int(I)) |
286
7247e85dc1e8
Start separating ConstantStencilOp into multiple 1D tensor mappings, e.g. ConstantLaplaceOp. Sketch an implementation of the multi-D laplace tensor operator as a tuple of 1D laplace tensor operators.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
40 end |
7247e85dc1e8
Start separating ConstantStencilOp into multiple 1D tensor mappings, e.g. ConstantLaplaceOp. Sketch an implementation of the multi-D laplace tensor operator as a tuple of 1D laplace tensor operators.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
41 |
328
9cc5d1498b2d
Refactor 1D diagonal inner product in quadrature.jl to separate file. Write tests for quadratures. Clean up laplace and secondderivative
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
311
diff
changeset
|
42 function LazyTensors.apply(D2::SecondDerivative{T}, v::AbstractVector{T}, index::Index{Unknown}) where T |
287
dd621017b695
Change apply_2nd_derivative to Lazy Tensor apply
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
286
diff
changeset
|
43 N = length(v) # TODO: Use domain_size here instead? |
311
f2d6ec89dfc5
Make apply dispatch on Index instead of Tuples of Index
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
299
diff
changeset
|
44 r = getregion(Int(index), closuresize(D2), N) |
f2d6ec89dfc5
Make apply dispatch on Index instead of Tuples of Index
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
299
diff
changeset
|
45 I = Index(Int(index), r) |
f2d6ec89dfc5
Make apply dispatch on Index instead of Tuples of Index
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
299
diff
changeset
|
46 return LazyTensors.apply(D2, v, I) |
286
7247e85dc1e8
Start separating ConstantStencilOp into multiple 1D tensor mappings, e.g. ConstantLaplaceOp. Sketch an implementation of the multi-D laplace tensor operator as a tuple of 1D laplace tensor operators.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
47 end |
7247e85dc1e8
Start separating ConstantStencilOp into multiple 1D tensor mappings, e.g. ConstantLaplaceOp. Sketch an implementation of the multi-D laplace tensor operator as a tuple of 1D laplace tensor operators.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
48 |
328
9cc5d1498b2d
Refactor 1D diagonal inner product in quadrature.jl to separate file. Write tests for quadratures. Clean up laplace and secondderivative
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
311
diff
changeset
|
49 closuresize(D2::SecondDerivative{T,N,M,K}) where {T<:Real,N,M,K} = M |