Mercurial > repos > public > sbplib_julia
annotate src/SbpOperators/boundaryops/boundary_operator.jl @ 995:1ba8a398af9c refactor/lazy_tensors
Rename types
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Fri, 18 Mar 2022 21:14:47 +0100 |
parents | 469ed954b493 |
children | 52f07c77299d 3bb94ce74697 fc57804c9bf4 |
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 | 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 # Create 1D boundary operator |
e40e7439d1b4
Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
16 r = region(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
|
17 d = dim(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
|
18 op = BoundaryOperator(restrict(grid, d), closure_stencil, 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
|
19 |
995 | 20 # 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
|
21 one_d_grids = restrict.(Ref(grid), Tuple(1:dimension(grid))) |
995 | 22 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
|
23 |
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 # Formulate the correct outer product sequence of the identity mappings and |
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 # the boundary operator |
e40e7439d1b4
Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
26 parts = Base.setindex(Is, op, d) |
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 return foldl(⊗, parts) |
e40e7439d1b4
Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
28 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
|
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 """ |
995 | 31 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
|
32 |
995 | 33 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
|
34 |
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 `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
|
36 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
|
37 `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
|
38 """ |
995 | 39 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
|
40 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
|
41 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
|
42 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
|
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 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
|
45 |
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 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
|
48 |
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 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
|
50 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
|
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 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
|
53 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
|
54 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
|
55 |
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 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
|
58 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
|
59 """ |
e40e7439d1b4
Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
60 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
|
61 |
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 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
|
63 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
|
64 |
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
|
65 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
|
66 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
|
67 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
|
68 |
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
|
69 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
|
70 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
|
71 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
|
72 |
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
|
73 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
|
74 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
|
75 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
|
76 |
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{<: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
|
78 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
|
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 |
e40e7439d1b4
Add a general boundary operator and make BoundaryRestriction a specialization of it.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
81 # 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
|
82 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
|
83 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
|
84 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
|
85 |
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
|
86 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
|
87 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
|
88 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
|
89 end |