comparison src/SbpOperators/boundaryops/boundary_restriction.jl @ 633:a78bda7084f6 feature/quadrature_as_outer_product

Merge w. default
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Fri, 01 Jan 2021 16:34:55 +0100
parents 0c411d865d66
children e40e7439d1b4
comparison
equal deleted inserted replaced
561:04d7b4eb63ef 633:a78bda7084f6
1 """
2 boundary_restriction(grid,closureStencil,boundary)
3
4 Creates a boundary restriction operator on a `Dim`-dimensional grid for the
5 specified `boundary`.
6
7 When `Dim=1`, the corresponding `BoundaryRestriction` tensor mapping is returned.
8 When `Dim>1`, the `BoundaryRestriction` `e` 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⊗e⊗Iz`.
11 """
12 function boundary_restriction(grid::EquidistantGrid{Dim,T}, closureStencil::Stencil{T,M}, boundary::CartesianBoundary) where {Dim,T,M}
13 # Create 1D boundary restriction operator
14 r = region(boundary)
15 d = dim(boundary)
16 e = BoundaryRestriction(restrict(grid, d), closureStencil, 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 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
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