annotate src/SbpOperators/boundaryops/boundary_operator.jl @ 1023:52f07c77299d refactor/sbpoperators/inflation

Merge refactor/lazy_tensors
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 21 Mar 2022 09:51:07 +0100
parents bbbc31953367 1ba8a398af9c
children 5a3281429a48 62f321caa964 05a25a5063bb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
610
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
1 """
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
2 boundary_operator(grid,closure_stencil,boundary)
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
3
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
4 Creates a boundary operator on a `Dim`-dimensional grid for the
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
5 specified `boundary`. The action of the operator is determined by `closure_stencil`.
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
6
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
7 When `Dim=1`, the corresponding `BoundaryOperator` tensor mapping is returned.
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
8 When `Dim>1`, the `BoundaryOperator` `op` is inflated by the outer product
995
1ba8a398af9c Rename types
Jonatan Werpers <jonatan@werpers.com>
parents: 946
diff changeset
9 of `IdentityTensors` in orthogonal coordinate directions, e.g for `Dim=3`,
610
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
10 the boundary restriction operator in the y-direction direction is `Ix⊗op⊗Iz`.
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
11 """
820
b4acd25943f4 Remove some more types and type parameters
Jonatan Werpers <jonatan@werpers.com>
parents: 627
diff changeset
12 function boundary_operator(grid::EquidistantGrid, closure_stencil, boundary::CartesianBoundary)
627
9f27f451d0a0 Add todo for checking valid inputs to boundary_operator and volume_operators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 619
diff changeset
13 #TODO:Check that dim(boundary) <= Dim?
9f27f451d0a0 Add todo for checking valid inputs to boundary_operator and volume_operators
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents: 619
diff changeset
14
610
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
15 d = dim(boundary)
1022
bbbc31953367 Introduce an inflate function in lazy tensors and use it in volume_operator and boundary_operator
Jonatan Werpers <jonatan@werpers.com>
parents: 946
diff changeset
16 op = BoundaryOperator(restrict(grid, d), closure_stencil, region(boundary))
610
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
17
995
1ba8a398af9c Rename types
Jonatan Werpers <jonatan@werpers.com>
parents: 946
diff changeset
18 # Create 1D IdentityTensors for each coordinate direction
820
b4acd25943f4 Remove some more types and type parameters
Jonatan Werpers <jonatan@werpers.com>
parents: 627
diff changeset
19 one_d_grids = restrict.(Ref(grid), Tuple(1:dimension(grid)))
995
1ba8a398af9c Rename types
Jonatan Werpers <jonatan@werpers.com>
parents: 946
diff changeset
20 Is = IdentityTensor{eltype(grid)}.(size.(one_d_grids))
610
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
21
1022
bbbc31953367 Introduce an inflate function in lazy tensors and use it in volume_operator and boundary_operator
Jonatan Werpers <jonatan@werpers.com>
parents: 946
diff changeset
22 return LazyTensors.inflate(op, size(grid), d)
610
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
23 end
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
24
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
25 """
995
1ba8a398af9c Rename types
Jonatan Werpers <jonatan@werpers.com>
parents: 946
diff changeset
26 BoundaryOperator{T,R,N} <: LazyTensor{T,0,1}
610
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
27
995
1ba8a398af9c Rename types
Jonatan Werpers <jonatan@werpers.com>
parents: 946
diff changeset
28 Implements the boundary operator `op` for 1D as a `LazyTensor`
610
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
29
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
30 `op` is the restriction of a grid function to the boundary using some closure `Stencil{T,N}`.
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
31 The boundary to restrict to is determined by `R`.
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
32 `op'` is the prolongation of a zero dimensional array to the whole grid using the same closure stencil.
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
33 """
995
1ba8a398af9c Rename types
Jonatan Werpers <jonatan@werpers.com>
parents: 946
diff changeset
34 struct BoundaryOperator{T,R<:Region,N} <: LazyTensor{T,0,1}
610
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
35 stencil::Stencil{T,N}
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
36 size::Int
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
37 end
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
38
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
39 BoundaryOperator{R}(stencil::Stencil{T,N}, size::Int) where {T,R,N} = BoundaryOperator{T,R,N}(stencil, size)
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
40
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
41 """
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
42 BoundaryOperator(grid::EquidistantGrid{1}, closure_stencil, region)
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
43
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
44 Constructs the BoundaryOperator with stencil `closure_stencil` for a one-dimensional `grid`, restricting to
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
45 to the boundary specified by `region`.
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
46 """
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
47 function BoundaryOperator(grid::EquidistantGrid{1}, closure_stencil::Stencil{T,N}, region::Region) where {T,N}
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
48 return BoundaryOperator{T,typeof(region),N}(closure_stencil,size(grid)[1])
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
49 end
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
50
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
51 """
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
52 closure_size(::BoundaryOperator)
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
53 The size of the closure stencil.
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
54 """
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
55 closure_size(::BoundaryOperator{T,R,N}) where {T,R,N} = N
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
56
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
57 LazyTensors.range_size(op::BoundaryOperator) = ()
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
58 LazyTensors.domain_size(op::BoundaryOperator) = (op.size,)
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
59
946
469ed954b493 Allow volume_operator, boundary_operator, and constant_interior_scaling_operator to act on arbitrary arrays
Jonatan Werpers <jonatan@werpers.com>
parents: 834
diff changeset
60 function LazyTensors.apply(op::BoundaryOperator{<:Any,Lower}, v::AbstractVector)
610
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
61 apply_stencil(op.stencil,v,1)
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
62 end
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
63
946
469ed954b493 Allow volume_operator, boundary_operator, and constant_interior_scaling_operator to act on arbitrary arrays
Jonatan Werpers <jonatan@werpers.com>
parents: 834
diff changeset
64 function LazyTensors.apply(op::BoundaryOperator{<:Any,Upper}, v::AbstractVector)
610
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
65 apply_stencil_backwards(op.stencil,v,op.size)
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
66 end
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
67
946
469ed954b493 Allow volume_operator, boundary_operator, and constant_interior_scaling_operator to act on arbitrary arrays
Jonatan Werpers <jonatan@werpers.com>
parents: 834
diff changeset
68 function LazyTensors.apply_transpose(op::BoundaryOperator{<:Any,Lower}, v::AbstractArray{<:Any,0}, i::Index{Lower})
610
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
69 return op.stencil[Int(i)-1]*v[]
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
70 end
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
71
946
469ed954b493 Allow volume_operator, boundary_operator, and constant_interior_scaling_operator to act on arbitrary arrays
Jonatan Werpers <jonatan@werpers.com>
parents: 834
diff changeset
72 function LazyTensors.apply_transpose(op::BoundaryOperator{<:Any,Upper}, v::AbstractArray{<:Any,0}, i::Index{Upper})
610
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
73 return op.stencil[op.size[1] - Int(i)]*v[]
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
74 end
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
75
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
76 # Catch all combinations of Lower, Upper and Interior not caught by the two previous methods.
946
469ed954b493 Allow volume_operator, boundary_operator, and constant_interior_scaling_operator to act on arbitrary arrays
Jonatan Werpers <jonatan@werpers.com>
parents: 834
diff changeset
77 function LazyTensors.apply_transpose(op::BoundaryOperator, v::AbstractArray{<:Any,0}, i::Index)
469ed954b493 Allow volume_operator, boundary_operator, and constant_interior_scaling_operator to act on arbitrary arrays
Jonatan Werpers <jonatan@werpers.com>
parents: 834
diff changeset
78 return zero(eltype(v))
610
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
79 end
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
80
946
469ed954b493 Allow volume_operator, boundary_operator, and constant_interior_scaling_operator to act on arbitrary arrays
Jonatan Werpers <jonatan@werpers.com>
parents: 834
diff changeset
81 function LazyTensors.apply_transpose(op::BoundaryOperator, v::AbstractArray{<:Any,0}, i)
610
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
82 r = getregion(i, closure_size(op), op.size)
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
83 apply_transpose(op, v, Index(i,r))
e40e7439d1b4 Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
84 end