comparison src/SbpOperators/boundaryops/boundary_restriction.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 0c411d865d66
children 332f65c1abf3
comparison
equal deleted inserted replaced
606:8f9b3eac128a 610:e40e7439d1b4
1 """ 1 """
2 boundary_restriction(grid,closureStencil,boundary) 2 BoundaryRestriction(grid::EquidistantGrid, closure_stencil::Stencil, boundary::CartesianBoundary)
3 BoundaryRestriction(grid::EquidistantGrid{1}, closure_stencil::Stencil, region::Region)
3 4
4 Creates a boundary restriction operator on a `Dim`-dimensional grid for the 5 Creates the boundary restriction operator `e` as a `TensorMapping`
5 specified `boundary`.
6 6
7 When `Dim=1`, the corresponding `BoundaryRestriction` tensor mapping is returned. 7 `e` is the restriction of a grid function to the boundary specified by `boundary` or `region` using some `closure_stencil`.
8 When `Dim>1`, the `BoundaryRestriction` `e` is inflated by the outer product 8 `e'` is the prolongation of a grid function on the boundary to the whole grid using the same `closure_stencil`.
9 of `IdentityMappings` in orthogonal coordinate directions, e.g for `Dim=3`, 9 On a one-dimensional `grid`, `e` is a `BoundaryOperator`. On a multi-dimensional `grid`, `e` is the inflation of
10 the boundary restriction operator in the y-direction direction is `Ix⊗e⊗Iz`. 10 a `BoundaryOperator`. Also see the documentation of `boundary_operator(...)` for more details.
11 """ 11 """
12 function boundary_restriction(grid::EquidistantGrid{Dim,T}, closureStencil::Stencil{T,M}, boundary::CartesianBoundary) where {Dim,T,M} 12 BoundaryRestriction(grid::EquidistantGrid, closure_stencil::Stencil, boundary::CartesianBoundary) = boundary_operator(grid, closure_stencil, boundary)
13 # Create 1D boundary restriction operator 13 BoundaryRestriction(grid::EquidistantGrid{1}, closure_stencil::Stencil, region::Region) = BoundaryRestriction(grid, closure_stencil, CartesianBoundary{1,typeof(region)}())
14 r = region(boundary)
15 d = dim(boundary)
16 e = BoundaryRestriction(restrict(grid, d), closureStencil, r)
17 14
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 restriction operator
24 parts = Base.setindex(Is, e, d)
25 return foldl(⊗, parts)
26 end
27
28 export boundary_restriction
29
30 """
31 BoundaryRestriction{T,R,N} <: TensorMapping{T,0,1}
32
33 Implements the boundary operator `e` for 1D as a `TensorMapping`
34
35 `e` is the restriction of a grid function to the boundary using some `closureStencil`.
36 The boundary to restrict to is determined by `R`.
37
38 `e'` is the prolongation of a zero dimensional array to the whole grid using the same `closureStencil`.
39 """
40 struct BoundaryRestriction{T,R<:Region,N} <: TensorMapping{T,0,1}
41 stencil::Stencil{T,N}
42 size::Int
43 end
44 export BoundaryRestriction 15 export BoundaryRestriction
45
46 BoundaryRestriction{R}(stencil::Stencil{T,N}, size::Int) where {T,R,N} = BoundaryRestriction{T,R,N}(stencil, size)
47
48 function BoundaryRestriction(grid::EquidistantGrid{1}, closureStencil::Stencil{T,N}, region::Region) where {T,N}
49 return BoundaryRestriction{T,typeof(region),N}(closureStencil,size(grid)[1])
50 end
51
52 closure_size(::BoundaryRestriction{T,R,N}) where {T,R,N} = N
53
54 LazyTensors.range_size(e::BoundaryRestriction) = ()
55 LazyTensors.domain_size(e::BoundaryRestriction) = (e.size,)
56
57 function LazyTensors.apply(e::BoundaryRestriction{T,Lower}, v::AbstractVector{T}) where T
58 apply_stencil(e.stencil,v,1)
59 end
60
61 function LazyTensors.apply(e::BoundaryRestriction{T,Upper}, v::AbstractVector{T}) where T
62 apply_stencil_backwards(e.stencil,v,e.size)
63 end
64
65 function LazyTensors.apply_transpose(e::BoundaryRestriction{T,Lower}, v::AbstractArray{T,0}, i::Index{Lower}) where T
66 return e.stencil[Int(i)-1]*v[]
67 end
68
69 function LazyTensors.apply_transpose(e::BoundaryRestriction{T,Upper}, v::AbstractArray{T,0}, i::Index{Upper}) where T
70 return e.stencil[e.size[1] - Int(i)]*v[]
71 end
72
73 # Catch all combinations of Lower, Upper and Interior not caught by the two previous methods.
74 function LazyTensors.apply_transpose(e::BoundaryRestriction{T}, v::AbstractArray{T,0}, i::Index) where T
75 return zero(T)
76 end
77
78 function LazyTensors.apply_transpose(e::BoundaryRestriction{T}, v::AbstractArray{T,0}, i) where T
79 r = getregion(i, closure_size(e), e.size)
80 apply_transpose(e, v, Index(i,r))
81 end