|
| 1 | +# Boundary Padded Arrays |
| 2 | +abstract type AbstractBoundaryPaddedArray{T, N} <: AbstractArray{T, N} end |
| 3 | +abstract type AbstractDirectionalBoundaryPaddedArray{T, N, D} <: AbstractBoundaryPaddedArray{T, N} end |
| 4 | +abstract type AbstractComposedBoundaryPaddedArray{T, N} <: AbstractBoundaryPaddedArray{T,N} end |
| 5 | +""" |
| 6 | +A vector type that extends a vector u with ghost points at either end |
| 7 | +""" |
| 8 | +struct BoundaryPaddedVector{T,T2 <: AbstractVector{T}} <: AbstractBoundaryPaddedArray{T, 1} |
| 9 | + l::T |
| 10 | + r::T |
| 11 | + u::T2 |
| 12 | +end |
| 13 | + |
| 14 | +Base.length(Q::BoundaryPaddedVector) = length(Q.u) + 2 |
| 15 | +Base.size(Q::BoundaryPaddedVector) = (length(Q.u) + 2,) |
| 16 | +Base.lastindex(Q::BoundaryPaddedVector) = Base.length(Q) |
| 17 | + |
| 18 | +function Base.getindex(Q::BoundaryPaddedVector,i) |
| 19 | + if i == 1 |
| 20 | + return Q.l |
| 21 | + elseif i == length(Q) |
| 22 | + return Q.r |
| 23 | + else |
| 24 | + return Q.u[i-1] |
| 25 | + end |
| 26 | +end |
| 27 | + |
| 28 | +""" |
| 29 | +Higher dimensional generalization of BoundaryPaddedVector, pads an array of dimension N along the dimension D with 2 Arrays of dimension N-1, stored in lower and upper |
| 30 | +
|
| 31 | +""" |
| 32 | +struct BoundaryPaddedArray{T, D, N, M, V<:AbstractArray{T, N}, B<: AbstractArray{T, M}} <: AbstractDirectionalBoundaryPaddedArray{T,N, D} |
| 33 | + lower::B #an array of dimension M = N-1, used to extend the lower index boundary |
| 34 | + upper::B #Ditto for the upper index boundary |
| 35 | + u::V |
| 36 | +end |
| 37 | + |
| 38 | +getaxis(Q::BoundaryPaddedArray{T,D,N,M,V,B}) where {T,D,N,M,V,B} = D |
| 39 | + |
| 40 | +function Base.size(Q::BoundaryPaddedArray) |
| 41 | + S = [size(Q.u)...] |
| 42 | + S[getaxis(Q)] += 2 |
| 43 | + return Tuple(S) |
| 44 | +end |
| 45 | + |
| 46 | +""" |
| 47 | +A = compose(padded_arrays::BoundaryPaddedArray...) |
| 48 | +
|
| 49 | +------------------------------------------------------------------------------------- |
| 50 | +
|
| 51 | +Example: |
| 52 | +A = compose(Ax, Ay, Az) # 3D domain |
| 53 | +A = compose(Ax, Ay) # 2D Domain |
| 54 | +
|
| 55 | +Composes BoundaryPaddedArrays that extend the same u for each different dimension that u has in to a ComposedBoundaryPaddedArray |
| 56 | +
|
| 57 | +Ax Ay and Az can be passed in any order, as long as there is exactly one BoundaryPaddedArray that extends each dimension. |
| 58 | +""" |
| 59 | +function compose(padded_arrays::BoundaryPaddedArray...) |
| 60 | + N = ndims(padded_arrays[1]) |
| 61 | + Ds = getaxis.(padded_arrays) |
| 62 | + (length(padded_arrays) == N) || throw(ArgumentError("The padded_arrays must cover every dimension - make sure that the number of padded_arrays is equal to ndims(u).")) |
| 63 | + for D in Ds |
| 64 | + length(setdiff(Ds, D)) < N || throw(ArgumentError("There are multiple Arrays that extend along dimension $D - make sure every dimension has a unique extension")) |
| 65 | + end |
| 66 | + any(fill(padded_arrays[1].u, (length(padded_arrays),)) .== getfield.(padded_arrays, :u)) || throw(ArgumentError("The padded_arrays do not all extend the same u!")) |
| 67 | + padded_arrays = padded_arrays[sortperm([Ds...])] |
| 68 | + lower = [padded_array.lower for padded_array in padded_arrays] |
| 69 | + upper = [padded_array.upper for padded_array in padded_arrays] |
| 70 | + |
| 71 | + ComposedBoundaryPaddedArray{gettype(padded_arrays[1]),N,N-1,typeof(padded_arrays[1].u),typeof(lower[1])}(lower, upper, padded_arrays[1].u) |
| 72 | +end |
| 73 | + |
| 74 | +# Composed BoundaryPaddedArray |
| 75 | + |
| 76 | +struct ComposedBoundaryPaddedArray{T, N, M, V<:AbstractArray{T, N}, B<: AbstractArray{T, M}} <: AbstractComposedBoundaryPaddedArray{T, N} |
| 77 | + lower::Vector{B} |
| 78 | + upper::Vector{B} |
| 79 | + u::V |
| 80 | +end |
| 81 | + |
| 82 | +# Aliases |
| 83 | +AbstractBoundaryPaddedMatrix{T} = AbstractBoundaryPaddedArray{T,2} |
| 84 | +AbstractBoundaryPadded3Tensor{T} = AbstractBoundaryPaddedArray{T,3} |
| 85 | + |
| 86 | +BoundaryPaddedMatrix{T, D, V, B} = BoundaryPaddedArray{T, D, 2, 1, V, B} |
| 87 | +BoundaryPadded3Tensor{T, D, V, B} = BoundaryPaddedArray{T, D, 3, 2, V, B} |
| 88 | + |
| 89 | +ComposedBoundaryPaddedMatrix{T,V,B} = ComposedBoundaryPaddedArray{T,2,1,V,B} |
| 90 | +ComposedBoundaryPadded3Tensor{T,V,B} = ComposedBoundaryPaddedArray{T,3,2,V,B} |
| 91 | + |
| 92 | +Base.size(Q::ComposedBoundaryPaddedArray) = size(Q.u).+2 |
| 93 | + |
| 94 | +""" |
| 95 | +Ax, Ay,... = decompose(A::ComposedBoundaryPaddedArray) |
| 96 | +
|
| 97 | +------------------------------------------------------------------------------------- |
| 98 | +
|
| 99 | +Decomposes a ComposedBoundaryPaddedArray in to components that extend along each dimension individually |
| 100 | +""" |
| 101 | +decompose(A::ComposedBoundaryPaddedArray) = Tuple([BoundaryPaddedArray{gettype(A), i, ndims(A), ndims(A)-1, typeof(lower[1])}(A.lower[i], A.upper[i], A.u) for i in 1:ndims(A)]) |
| 102 | + |
| 103 | +Base.length(Q::AbstractBoundaryPaddedArray) = reduce((*), size(Q)) |
| 104 | +Base.firstindex(Q::AbstractBoundaryPaddedArray, d::Int) = 1 |
| 105 | +Base.lastindex(Q::AbstractBoundaryPaddedArray) = length(Q) |
| 106 | +Base.lastindex(Q::AbstractBoundaryPaddedArray, d::Int) = size(Q)[d] |
| 107 | +gettype(Q::AbstractBoundaryPaddedArray{T,N}) where {T,N} = T |
| 108 | +Base.ndims(Q::AbstractBoundaryPaddedArray{T,N}) where {T,N} = N |
| 109 | + |
| 110 | +function Base.getindex(Q::BoundaryPaddedArray{T,D,N,M,V,B}, _inds::Vararg{Int,N}) where {T,D,N,M,V,B} #supports range and colon indexing! |
| 111 | + inds = [_inds...] |
| 112 | + S = size(Q) |
| 113 | + dim = D |
| 114 | + otherinds = inds[setdiff(1:N, dim)] |
| 115 | + @assert length(S) == N |
| 116 | + if inds[dim] == 1 |
| 117 | + return Q.lower[otherinds...] |
| 118 | + elseif inds[dim] == S[dim] |
| 119 | + return Q.upper[otherinds...] |
| 120 | + elseif typeof(inds[dim]) <: Integer |
| 121 | + inds[dim] = inds[dim] - 1 |
| 122 | + return Q.u[inds...] |
| 123 | + elseif typeof(inds[dim]) == Colon |
| 124 | + if mapreduce(x -> typeof(x) != Colon, (|), otherinds) |
| 125 | + return vcat(Q.lower[otherinds...], Q.u[inds...], Q.upper[otherinds...]) |
| 126 | + else |
| 127 | + throw("A colon on the extended dim is as yet incompatible with additional colons") |
| 128 | + end |
| 129 | + elseif typeof(inds[dim]) <: AbstractArray |
| 130 | + throw("Range indexing not yet supported!") |
| 131 | + end |
| 132 | +end |
| 133 | + |
| 134 | +function Base.getindex(Q::ComposedBoundaryPaddedArray{T, N, M, V, B} , inds::Vararg{Int, N}) where {T, N, M, V, B} #as yet no support for range indexing or colon indexing |
| 135 | + S = size(Q) |
| 136 | + @assert reduce((&), inds .<= S) |
| 137 | + for (dim, index) in enumerate(inds) |
| 138 | + if index == 1 |
| 139 | + _inds = inds[setdiff(1:N, dim)] |
| 140 | + if (1 ∈ _inds) | any(S[setdiff(1:N, dim)] .== _inds) |
| 141 | + return zero(T) |
| 142 | + else |
| 143 | + return Q.lower[dim][(_inds.-1)...] |
| 144 | + end |
| 145 | + elseif index == S[dim] |
| 146 | + _inds = inds[setdiff(1:N, dim)] |
| 147 | + if (1 ∈ _inds) | any(S[setdiff(1:N, dim)] .== _inds) |
| 148 | + return zero(T) |
| 149 | + else |
| 150 | + return Q.upper[dim][(_inds.-1)...] |
| 151 | + end |
| 152 | + end |
| 153 | + end |
| 154 | + return Q.u[(inds.-1)...] |
| 155 | +end |
0 commit comments