Mercurial > repos > public > sbplib_julia
comparison src/SbpOperators/boundaryops/boundary_operator.jl @ 2057:8a2a0d678d6f feature/lazy_tensors/pretty_printing
Merge default
| author | Jonatan Werpers <jonatan@werpers.com> |
|---|---|
| date | Tue, 10 Feb 2026 22:41:19 +0100 |
| parents | 3714a391545a |
| children |
comparison
equal
deleted
inserted
replaced
| 1110:c0bff9f6e0fb | 2057:8a2a0d678d6f |
|---|---|
| 1 """ | 1 """ |
| 2 boundary_operator(grid,closure_stencil,boundary) | 2 BoundaryOperator{T,B,N} <: LazyTensor{T,0,1} |
| 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 `IdentityTensors` 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, closure_stencil, boundary::CartesianBoundary) | |
| 13 #TODO:Check that dim(boundary) <= Dim? | |
| 14 | |
| 15 # Create 1D boundary operator | |
| 16 r = region(boundary) | |
| 17 d = dim(boundary) | |
| 18 op = BoundaryOperator(restrict(grid, d), closure_stencil, r) | |
| 19 | |
| 20 # Create 1D IdentityTensors for each coordinate direction | |
| 21 one_d_grids = restrict.(Ref(grid), Tuple(1:dimension(grid))) | |
| 22 Is = IdentityTensor{eltype(grid)}.(size.(one_d_grids)) | |
| 23 | |
| 24 # Formulate the correct outer product sequence of the identity mappings and | |
| 25 # the boundary operator | |
| 26 parts = Base.setindex(Is, op, d) | |
| 27 return foldl(⊗, parts) | |
| 28 end | |
| 29 | |
| 30 """ | |
| 31 BoundaryOperator{T,R,N} <: LazyTensor{T,0,1} | |
| 32 | 3 |
| 33 Implements the boundary operator `op` for 1D as a `LazyTensor` | 4 Implements the boundary operator `op` for 1D as a `LazyTensor` |
| 34 | 5 |
| 35 `op` is the restriction of a grid function to the boundary using some closure `Stencil{T,N}`. | 6 `op` is the restriction of a grid function to the boundary using some closure |
| 36 The boundary to restrict to is determined by `R`. | 7 `Stencil{T,N}`. The boundary to restrict to is determined by `B`. `op'` is the |
| 37 `op'` is the prolongation of a zero dimensional array to the whole grid using the same closure stencil. | 8 prolongation of a zero dimensional array to the whole grid using the same |
| 9 closure stencil. | |
| 38 """ | 10 """ |
| 39 struct BoundaryOperator{T,R<:Region,N} <: LazyTensor{T,0,1} | 11 struct BoundaryOperator{T,B<:BoundaryIdentifier,N} <: LazyTensor{T,0,1} |
| 40 stencil::Stencil{T,N} | 12 stencil::Stencil{T,N} |
| 41 size::Int | 13 size::Int |
| 42 end | 14 end |
| 43 | 15 |
| 44 BoundaryOperator{R}(stencil::Stencil{T,N}, size::Int) where {T,R,N} = BoundaryOperator{T,R,N}(stencil, size) | 16 """ |
| 17 BoundaryOperator(grid::EquidistantGrid, closure_stencil, boundary) | |
| 45 | 18 |
| 19 Constructs the BoundaryOperator with stencil `closure_stencil` for a | |
| 20 `EquidistantGrid` `grid`, restricting to to the boundary specified by | |
| 21 `boundary`. | |
| 46 """ | 22 """ |
| 47 BoundaryOperator(grid::EquidistantGrid{1}, closure_stencil, region) | 23 function BoundaryOperator(grid::EquidistantGrid, closure_stencil::Stencil{T,N}, boundary::BoundaryIdentifier) where {T,N} |
| 48 | 24 return BoundaryOperator{T,typeof(boundary),N}(closure_stencil,size(grid)[1]) |
| 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 | 25 end |
| 55 | 26 |
| 56 """ | 27 """ |
| 57 closure_size(::BoundaryOperator) | 28 closure_size(::BoundaryOperator) |
| 29 | |
| 58 The size of the closure stencil. | 30 The size of the closure stencil. |
| 59 """ | 31 """ |
| 60 closure_size(::BoundaryOperator{T,R,N}) where {T,R,N} = N | 32 closure_size(::BoundaryOperator{T,B,N}) where {T,B,N} = N |
| 61 | 33 |
| 62 LazyTensors.range_size(op::BoundaryOperator) = () | 34 LazyTensors.range_size(op::BoundaryOperator) = () |
| 63 LazyTensors.domain_size(op::BoundaryOperator) = (op.size,) | 35 LazyTensors.domain_size(op::BoundaryOperator) = (op.size,) |
| 64 | 36 |
| 65 function LazyTensors.apply(op::BoundaryOperator{<:Any,Lower}, v::AbstractVector) | 37 function LazyTensors.apply(op::BoundaryOperator{<:Any,LowerBoundary}, v::AbstractVector) |
| 66 apply_stencil(op.stencil,v,1) | 38 apply_stencil(op.stencil,v,1) |
| 67 end | 39 end |
| 68 | 40 |
| 69 function LazyTensors.apply(op::BoundaryOperator{<:Any,Upper}, v::AbstractVector) | 41 function LazyTensors.apply(op::BoundaryOperator{<:Any,UpperBoundary}, v::AbstractVector) |
| 70 apply_stencil_backwards(op.stencil,v,op.size) | 42 apply_stencil_backwards(op.stencil,v,op.size) |
| 71 end | 43 end |
| 72 | 44 |
| 73 function LazyTensors.apply_transpose(op::BoundaryOperator{<:Any,Lower}, v::AbstractArray{<:Any,0}, i::Index{Lower}) | 45 function LazyTensors.apply_transpose(op::BoundaryOperator{<:Any,LowerBoundary}, v::AbstractArray{<:Any,0}, i::Index{Lower}) |
| 74 return op.stencil[Int(i)-1]*v[] | 46 return op.stencil[Int(i)-1]*v[] |
| 75 end | 47 end |
| 76 | 48 |
| 77 function LazyTensors.apply_transpose(op::BoundaryOperator{<:Any,Upper}, v::AbstractArray{<:Any,0}, i::Index{Upper}) | 49 function LazyTensors.apply_transpose(op::BoundaryOperator{<:Any,UpperBoundary}, v::AbstractArray{<:Any,0}, i::Index{Upper}) |
| 78 return op.stencil[op.size[1] - Int(i)]*v[] | 50 return op.stencil[op.size[1] - Int(i)]*v[] |
| 79 end | 51 end |
| 80 | 52 |
| 81 # Catch all combinations of Lower, Upper and Interior not caught by the two previous methods. | 53 # Catch all combinations of Lower, Upper and Interior not caught by the two previous methods. |
| 82 function LazyTensors.apply_transpose(op::BoundaryOperator, v::AbstractArray{<:Any,0}, i::Index) | 54 function LazyTensors.apply_transpose(op::BoundaryOperator, v::AbstractArray{<:Any,0}, i::Index) |
