comparison src/SbpOperators/boundaryops/boundary_operator.jl @ 1207:f1c2a4fa0ee1 performance/get_region_type_inference

Merge default
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 03 Feb 2023 22:14:47 +0100
parents b41180efb6c2 716e721ce3eb
children
comparison
equal deleted inserted replaced
919:b41180efb6c2 1207:f1c2a4fa0ee1
1 """ 1 """
2 boundary_operator(grid,closure_stencil,boundary) 2 BoundaryOperator{T,R,N} <: LazyTensor{T,0,1}
3 3
4 Creates a boundary operator on a `Dim`-dimensional grid for the 4 Implements the boundary operator `op` for 1D as a `LazyTensor`
5 specified `boundary`. The action of the operator is determined by `closure_stencil`.
6
7 When `Dim=1`, the corresponding `BoundaryOperator` tensor mapping is returned.
8 When `Dim>1`, the `BoundaryOperator` `op` is inflated by the outer product
9 of `IdentityMappings` in orthogonal coordinate directions, e.g for `Dim=3`,
10 the boundary restriction operator in the y-direction direction is `Ix⊗op⊗Iz`.
11 """
12 function boundary_operator(grid::EquidistantGrid, closure_stencil, boundary::CartesianBoundary)
13 #TODO:Check that dim(boundary) <= Dim?
14
15 # Create 1D boundary operator
16 r = region(boundary)
17 d = dim(boundary)
18 op = BoundaryOperator(restrict(grid, d), closure_stencil, r)
19
20 # Create 1D IdentityMappings for each coordinate direction
21 one_d_grids = restrict.(Ref(grid), Tuple(1:dimension(grid)))
22 Is = IdentityMapping{eltype(grid)}.(size.(one_d_grids))
23
24 # Formulate the correct outer product sequence of the identity mappings and
25 # the boundary operator
26 parts = Base.setindex(Is, op, d)
27 return foldl(⊗, parts)
28 end
29
30 """
31 BoundaryOperator{T,R,N} <: TensorMapping{T,0,1}
32
33 Implements the boundary operator `op` for 1D as a `TensorMapping`
34 5
35 `op` is the restriction of a grid function to the boundary using some closure `Stencil{T,N}`. 6 `op` is the restriction of a grid function to the boundary using some closure `Stencil{T,N}`.
36 The boundary to restrict to is determined by `R`. 7 The boundary to restrict to is determined by `R`.
37 `op'` is the prolongation of a zero dimensional array to the whole grid using the same closure stencil. 8 `op'` is the prolongation of a zero dimensional array to the whole grid using the same closure stencil.
38 """ 9 """
39 struct BoundaryOperator{T,R<:Region,N} <: TensorMapping{T,0,1} 10 struct BoundaryOperator{T,R<:Region,N} <: LazyTensor{T,0,1}
40 stencil::Stencil{T,N} 11 stencil::Stencil{T,N}
41 size::Int 12 size::Int
42 end 13 end
43
44 BoundaryOperator{R}(stencil::Stencil{T,N}, size::Int) where {T,R,N} = BoundaryOperator{T,R,N}(stencil, size)
45 14
46 """ 15 """
47 BoundaryOperator(grid::EquidistantGrid{1}, closure_stencil, region) 16 BoundaryOperator(grid::EquidistantGrid{1}, closure_stencil, region)
48 17
49 Constructs the BoundaryOperator with stencil `closure_stencil` for a one-dimensional `grid`, restricting to 18 Constructs the BoundaryOperator with stencil `closure_stencil` for a one-dimensional `grid`, restricting to
53 return BoundaryOperator{T,typeof(region),N}(closure_stencil,size(grid)[1]) 22 return BoundaryOperator{T,typeof(region),N}(closure_stencil,size(grid)[1])
54 end 23 end
55 24
56 """ 25 """
57 closure_size(::BoundaryOperator) 26 closure_size(::BoundaryOperator)
27
58 The size of the closure stencil. 28 The size of the closure stencil.
59 """ 29 """
60 closure_size(::BoundaryOperator{T,R,N}) where {T,R,N} = N 30 closure_size(::BoundaryOperator{T,R,N}) where {T,R,N} = N
61 31
62 LazyTensors.range_size(op::BoundaryOperator) = () 32 LazyTensors.range_size(op::BoundaryOperator) = ()
63 LazyTensors.domain_size(op::BoundaryOperator) = (op.size,) 33 LazyTensors.domain_size(op::BoundaryOperator) = (op.size,)
64 34
65 function LazyTensors.apply(op::BoundaryOperator{T,Lower}, v::AbstractVector{T}) where T 35 function LazyTensors.apply(op::BoundaryOperator{<:Any,Lower}, v::AbstractVector)
66 apply_stencil(op.stencil,v,1) 36 apply_stencil(op.stencil,v,1)
67 end 37 end
68 38
69 function LazyTensors.apply(op::BoundaryOperator{T,Upper}, v::AbstractVector{T}) where T 39 function LazyTensors.apply(op::BoundaryOperator{<:Any,Upper}, v::AbstractVector)
70 apply_stencil_backwards(op.stencil,v,op.size) 40 apply_stencil_backwards(op.stencil,v,op.size)
71 end 41 end
72 42
73 function LazyTensors.apply_transpose(op::BoundaryOperator{T,Lower}, v::AbstractArray{T,0}, i::Index{Lower}) where T 43 function LazyTensors.apply_transpose(op::BoundaryOperator{<:Any,Lower}, v::AbstractArray{<:Any,0}, i::Index{Lower})
74 return op.stencil[Int(i)-1]*v[] 44 return op.stencil[Int(i)-1]*v[]
75 end 45 end
76 46
77 function LazyTensors.apply_transpose(op::BoundaryOperator{T,Upper}, v::AbstractArray{T,0}, i::Index{Upper}) where T 47 function LazyTensors.apply_transpose(op::BoundaryOperator{<:Any,Upper}, v::AbstractArray{<:Any,0}, i::Index{Upper})
78 return op.stencil[op.size[1] - Int(i)]*v[] 48 return op.stencil[op.size[1] - Int(i)]*v[]
79 end 49 end
80 50
81 # Catch all combinations of Lower, Upper and Interior not caught by the two previous methods. 51 # Catch all combinations of Lower, Upper and Interior not caught by the two previous methods.
82 function LazyTensors.apply_transpose(op::BoundaryOperator{T}, v::AbstractArray{T,0}, i::Index) where T 52 function LazyTensors.apply_transpose(op::BoundaryOperator, v::AbstractArray{<:Any,0}, i::Index)
83 return zero(T) 53 return zero(eltype(v))
84 end 54 end
85 55
86 function LazyTensors.apply_transpose(op::BoundaryOperator{T}, v::AbstractArray{T,0}, i) where T 56 function LazyTensors.apply_transpose(op::BoundaryOperator, v::AbstractArray{<:Any,0}, i)
87 return LazyTensors.apply_transpose_with_region(op, v, closure_size(op), op.size[1], i) 57 return LazyTensors.apply_transpose_with_region(op, v, closure_size(op), op.size[1], i)
88 end 58 end