comparison src/SbpOperators/boundaryops/boundary_operator.jl @ 610:e40e7439d1b4 feature/volume_and_boundary_operators

Add a general boundary operator and make BoundaryRestriction a specialization of it.
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Sat, 05 Dec 2020 18:12:31 +0100
parents
children 332f65c1abf3
comparison
equal deleted inserted replaced
606:8f9b3eac128a 610:e40e7439d1b4
1 """
2 boundary_operator(grid,closure_stencil,boundary)
3
4 Creates a boundary operator on a `Dim`-dimensional grid for the
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{Dim,T}, closure_stencil::Stencil{T}, boundary::CartesianBoundary) where {Dim,T}
13 # Create 1D boundary operator
14 r = region(boundary)
15 d = dim(boundary)
16 op = BoundaryOperator(restrict(grid, d), closure_stencil, r)
17
18 # Create 1D IdentityMappings for each coordinate direction
19 one_d_grids = restrict.(Ref(grid), Tuple(1:Dim))
20 Is = IdentityMapping{T}.(size.(one_d_grids))
21
22 # Formulate the correct outer product sequence of the identity mappings and
23 # the boundary operator
24 parts = Base.setindex(Is, op, d)
25 return foldl(⊗, parts)
26 end
27 export boundary_operator
28
29 """
30 BoundaryOperator{T,R,N} <: TensorMapping{T,0,1}
31
32 Implements the boundary operator `op` for 1D as a `TensorMapping`
33
34 `op` is the restriction of a grid function to the boundary using some closure `Stencil{T,N}`.
35 The boundary to restrict to is determined by `R`.
36 `op'` is the prolongation of a zero dimensional array to the whole grid using the same closure stencil.
37 """
38 struct BoundaryOperator{T,R<:Region,N} <: TensorMapping{T,0,1}
39 stencil::Stencil{T,N}
40 size::Int
41 end
42 export BoundaryOperator
43
44 BoundaryOperator{R}(stencil::Stencil{T,N}, size::Int) where {T,R,N} = BoundaryOperator{T,R,N}(stencil, size)
45
46 """
47 BoundaryOperator(grid::EquidistantGrid{1}, closure_stencil, region)
48
49 Constructs the BoundaryOperator with stencil `closure_stencil` for a one-dimensional `grid`, restricting to
50 to the boundary specified by `region`.
51 """
52 function BoundaryOperator(grid::EquidistantGrid{1}, closure_stencil::Stencil{T,N}, region::Region) where {T,N}
53 return BoundaryOperator{T,typeof(region),N}(closure_stencil,size(grid)[1])
54 end
55
56 """
57 closure_size(::BoundaryOperator)
58 The size of the closure stencil.
59 """
60 closure_size(::BoundaryOperator{T,R,N}) where {T,R,N} = N
61
62 LazyTensors.range_size(op::BoundaryOperator) = ()
63 LazyTensors.domain_size(op::BoundaryOperator) = (op.size,)
64
65 function LazyTensors.apply(op::BoundaryOperator{T,Lower}, v::AbstractVector{T}) where T
66 apply_stencil(op.stencil,v,1)
67 end
68
69 function LazyTensors.apply(op::BoundaryOperator{T,Upper}, v::AbstractVector{T}) where T
70 apply_stencil_backwards(op.stencil,v,op.size)
71 end
72
73 function LazyTensors.apply_transpose(op::BoundaryOperator{T,Lower}, v::AbstractArray{T,0}, i::Index{Lower}) where T
74 return op.stencil[Int(i)-1]*v[]
75 end
76
77 function LazyTensors.apply_transpose(op::BoundaryOperator{T,Upper}, v::AbstractArray{T,0}, i::Index{Upper}) where T
78 return op.stencil[op.size[1] - Int(i)]*v[]
79 end
80
81 # 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
83 return zero(T)
84 end
85
86 function LazyTensors.apply_transpose(op::BoundaryOperator{T}, v::AbstractArray{T,0}, i) where T
87 r = getregion(i, closure_size(op), op.size)
88 apply_transpose(op, v, Index(i,r))
89 end