Skip to content
This repository was archived by the owner on Jul 19, 2023. It is now read-only.

Commit 8a2dce3

Browse files
committed
Got tests passing
1 parent 7b0b7fd commit 8a2dce3

File tree

4 files changed

+46
-41
lines changed

4 files changed

+46
-41
lines changed

src/boundary_padded_arrays.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,16 @@ decompose(A::ComposedBoundaryPaddedArray) = Tuple([BoundaryPaddedArray{gettype(A
100100

101101

102102
Base.length(Q::AbstractBoundaryPaddedArray) = reduce((*), size(Q))
103-
Base.lastindex(Q::AbstractBoundaryPaddedArray) = Base.length(Q)
103+
Base.firstindex(Q::AbstractBoundaryPaddedArray, d::Int) = 1
104+
Base.lastindex(Q::AbstractBoundaryPaddedArray) = length(Q)
105+
Base.lastindex(Q::AbstractBoundaryPaddedArray, d::Int) = size(Q)[d]
104106
gettype(Q::AbstractBoundaryPaddedArray{T,N}) where {T,N} = T
105107
Base.ndims(Q::AbstractBoundaryPaddedArray{T,N}) where {T,N} = N
106108

107109
add_dim(A::AbstractArray, i) = reshape(A, size(A)...,i)
108110
add_dim(i) = i
109111

110-
function Base.getindex(Q::BoundaryPaddedArray{T,D,N,M,V,B}, _inds::NTuple{N,Int64}...) where {T,D,N,M,V,B} #supports range and colon indexing!
112+
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!
111113
inds = [_inds...]
112114
S = size(Q)
113115
dim = D
@@ -131,7 +133,7 @@ function Base.getindex(Q::BoundaryPaddedArray{T,D,N,M,V,B}, _inds::NTuple{N,Int6
131133
end
132134
end
133135

134-
function Base.getindex(Q::ComposedBoundaryPaddedArray{T, N, M, V, B} , inds::NTuple{N, Int64}...) where {T, N, M, V, B} #as yet no support for range indexing or colon indexing
136+
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
135137
S = size(Q)
136138
@assert reduce((&), inds .<= S)
137139
for (dim, index) in enumerate(inds)
@@ -153,5 +155,3 @@ function Base.getindex(Q::ComposedBoundaryPaddedArray{T, N, M, V, B} , inds::NTu
153155
end
154156
return Q.u[(inds.-1)...]
155157
end
156-
157-
getindex(A::AbstractBoundaryPaddedArray{T,N}, I::CartesianIndex{N}) where {T,N} = A[Tuple(I)...]

src/derivative_operators/multi_dim_bc_operators.jl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ abstract type MultiDimensionalBC{T, N} <: AbstractBC{T} end
33

44

55
@noinline function _slice_rmul!(u_temp::AbstractArray{T,N}, A::AbstractDiffEqLinearOperator, u::AbstractArray{T,N}, dim::Int, pre, post) where {T,N}
6-
for J in CartesianIndices(post)
7-
for I in CartesianIndices(pre)
6+
for J in post
7+
for I in pre
88
u_temp[I, :, J] = A*u[I, :, J]
99
end
1010
end
@@ -14,13 +14,13 @@ end
1414
function slice_rmul(A::AbstractDiffEqLinearOperator, u::AbstractArray{T,N}, dim::Int) where {T,N}
1515
@assert N != 1
1616
u_temp = similar(u)
17-
_slice_rmul!(u_temp, A, u, dim, axes(u)[1:dim-1], axes(u)[dim+1:end])
17+
_slice_rmul!(u_temp, A, u, dim, CartesianIndices(axes(u)[1:dim-1]), CartesianIndices(axes(u)[(dim+1):end]))
1818
return u_temp
1919
end
2020

2121
@noinline function _slice_rmul!(lower::AbstractArray, upper::AbstractArray, A::AbstractArray{B,M}, u::AbstractArray{T,N}, dim::Int, pre, post) where {T,B,N,M}
22-
for J in CartesianIndices(post)
23-
for I in CartesianIndices(pre)
22+
for J in post
23+
for I in pre
2424
tmp = A[I,J]*u[I, :, J]
2525
lower[I,J], upper[I,J] = tmp.l, tmp.r
2626
end
@@ -33,7 +33,7 @@ function slice_rmul(A::AbstractArray{B,M}, u::AbstractArray{T,N}, dim::Int) wher
3333
@assert M == N-1
3434
lower = zeros(T,perpsize(u,dim))
3535
upper = zeros(T,perpsize(u,dim))
36-
_slice_rmul!(lower, upper, A, u, dim, axes(u)[1:dim-1], axes(u)[dim+1:end])
36+
_slice_rmul!(lower, upper, A, u, dim, CartesianIndices(axes(u)[1:dim-1]), CartesianIndices(axes(u)[(dim+1):end]))
3737
return (lower, upper)
3838
end
3939

@@ -68,10 +68,14 @@ In the case where you want to extend the same Robin/GeneralBC to the whole bound
6868
or
6969
Qx, Qy, Qz... = GeneralBC(αl, αr, (dx::Vector, dy::Vector, dz::Vector ...), approximation_order, size(u))
7070
71-
There are also constructors for NeumannBC, DirichletBC, Neumann0BC and Dirichlet0BC. Simply replace `dx` in the call with the tuple dxyz... as above, and append `size(u)`` to the argument signature.
71+
There are also constructors for NeumannBC, DirichletBC and Dirichlet0BC. Simply replace `dx` in the call with the tuple dxyz... as above, and append `size(u)`` to the argument signature.
7272
The order is a required argument in this case.
7373
7474
where dx, dy, and dz are vectors of grid steps.
75+
76+
For Neumann0BC, please use
77+
Qx, Qy, Qz... = Neumann0BC(T::Type, (dx::Vector, dy::Vector, dz::Vector ...), approximation_order, size(u))
78+
where T is the element type of the domain to be extended
7579
"""
7680
MultiDimBC(BC::Array{B,N}, dim::Integer) where {N, B<:AtomicBC} = MultiDimDirectionalBC{gettype(BC[1]), B, dim, N+1, N}(BC)
7781
#s should be size of the domain
@@ -138,11 +142,15 @@ getboundarytype(Q::MultiDimDirectionalBC{T, B, D, N, K}) where {T, B, D, N, K} =
138142
Base.ndims(Q::MultiDimensionalBC{T,N}) where {T,N} = N
139143

140144
function Base.:*(Q::MultiDimDirectionalBC{T, B, D, N, K}, u::AbstractArray{T, N}) where {T, B, D, N, K}
145+
@assert perpsize(u, D) == size(Q.BCs) "Size of the BCs array in the MultiDimBC is incorrect, needs to be $(perpsize(u,D)) to extend dimension $D, got $(size(Q.BCs))"
141146
lower, upper = slice_rmul(Q.BCs, u, D)
142147
return BoundaryPaddedArray{T, D, N, K, typeof(u), typeof(lower)}(lower, upper, u)
143148
end
144149

145150
function Base.:*(Q::ComposedMultiDimBC{T, B, N, K}, u::AbstractArray{T, N}) where {T, B, N, K}
151+
for dim in 1:N
152+
@assert perpsize(u, dim) == size(Q.BCs[dim]) "Size of the BCs array for dimension $dim in the MultiDimBC is incorrect, needs to be $(perpsize(u,dim)), got $(size(Q.BCs[dim]))"
153+
end
146154
out = slice_rmul.(Q.BCs, fill(u, N), 1:N)
147155
return ComposedBoundaryPaddedArray{T, N, K, typeof(u), typeof(out[1][1])}([A[1] for A in out], [A[2] for A in out], u)
148156
end

test/MultiDimBC_test.jl

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ using LinearAlgebra, DiffEqOperators, Random, Test
44
################################################################################
55

66
#Create Array
7-
n = 12
8-
m = 25
7+
n = 8
8+
m = 15
99
A = rand(n,m)
1010

1111
#Create atomic BC
1212
q1 = RobinBC([1.0, 2.0, 3.0], [0.0, -1.0, 2.0], 0.1, 4.0)
1313
q2 = PeriodicBC{Float64}()
1414

15-
BCx = vcat(fill(q1, div(m,2)), fill(q2, div(m,2))) #The size of BCx has to be all size components *except* for x
16-
BCy = vcat(fill(q1, div(n,2)), fill(q2, div(n,2)))
15+
BCx = vcat(fill(q1, div(m,2)), fill(q2, m-div(m,2))) #The size of BCx has to be all size components *except* for x
16+
BCy = vcat(fill(q1, div(n,2)), fill(q2, n-div(n,2)))
1717

1818

1919
Qx = MultiDimBC(BCx, 1)
@@ -38,29 +38,26 @@ end
3838
################################################################################
3939

4040
#Create Array
41-
n = 31
42-
m = 25
41+
n = 8
42+
m = 11
4343
o = 12
4444
A = rand(n,m, o)
4545

4646
#Create atomic BC
4747
q1 = RobinBC([1.0, 2.0, 3.0], [0.0, -1.0, 2.0], 0.1, 4.0)
4848
q2 = PeriodicBC{Float64}()
4949

50-
BCx = vcat(fill(q1, (div(m,2), o)), fill(q2, (div(m,2), o))) #The size of BCx has to be all size components *except* for x
51-
BCy = vcat(fill(q1, (div(n,2), o)), fill(q2, (div(n,2), o)))
52-
BCz = fill(MixedBC(q1,q2), (n,m))
50+
BCx = vcat(fill(q1, (div(m,2), o)), fill(q2, (m-div(m,2), o))) #The size of BCx has to be all size components *except* for x
51+
BCy = vcat(fill(q1, (div(n,2), o)), fill(q2, (n-div(n,2), o)))
52+
BCz = fill(Dirichlet0BC(Float64), (n,m))
5353

5454
Qx = MultiDimBC(BCx, 1)
5555
Qy = MultiDimBC(BCy, 2)
56-
Qz = MultiDimBC(MixedBC(q1, q2), size(A), 3) #Test the other constructor
56+
Qz = MultiDimBC(Dirichlet0BC(Float64), size(A), 3) #Test the other constructor
5757

5858
Ax = Qx*A
5959
Ay = Qy*A
6060
Az = Qz*A
61-
# Test padded array compositions
62-
63-
QA = Q*A
6461

6562
@test size(Ax)[1] == size(A)[1]+2
6663
@test size(Ay)[2] == size(A)[2]+2
@@ -77,10 +74,10 @@ end
7774

7875
#test compositions to higher dimension
7976
for N in 2:7
80-
sizes = rand(5:10)
77+
sizes = rand(4:7, N)
8178
A = rand(sizes...)
8279

83-
Q1_N = Neumann0BC(1.0, 3.0, size(A))
80+
Q1_N = Neumann0BC(Float64, Tuple(ones(N)), 3.0, size(A))
8481

8582
Q = compose(Q1_N...)
8683

test/boundary_padded_array.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ using LinearAlgebra, DiffEqOperators, Random, Test
55

66
for dimensionality in 2:5
77
for dim in 1:dimensionality
8-
sizes = rand(10:20, dimensionality)
8+
sizes = rand(4:10, dimensionality)
99
A = rand(sizes...)
10-
lower = selectdim(A, dim, 1)
11-
upper = selectdim(A, dim, size(A)[dim])
10+
lower = Array(selectdim(A, dim, 1))
11+
upper = Array(selectdim(A, dim, size(A)[dim]))
1212

1313
Apad = DiffEqOperators.BoundaryPaddedArray{Float64, dim, dimensionality, dimensionality-1, typeof(A), typeof(lower)}(lower, upper, selectdim(A, dim, 2:(size(A)[dim]-1)))
1414

@@ -24,15 +24,15 @@ end
2424
# Test ComposedBoundaryPaddedMatrix
2525
################################################################################
2626

27-
n = 10
28-
m = 21
27+
n = 5
28+
m = 7
2929
A = rand(n,m)
3030
A[1,1] = A[end,1] = A[1,end] = A[end,end] = 0.0
3131

3232
lower = Vector[A[1,2:(end-1)], A[2:(end-1),1]]
3333
upper = Vector[A[end,2:(end-1)], A[2:(end-1),end]]
3434

35-
Apad = ComposedBoundaryPaddedMatrix{Float64, typeof(A), typeof(lower[1])}(lower, upper, A[2:(end-1), 2:(end-1)])
35+
Apad = DiffEqOperators.ComposedBoundaryPaddedMatrix{Float64, typeof(A), typeof(lower[1])}(lower, upper, A[2:(end-1), 2:(end-1)])
3636

3737
@test A == Array(Apad) #test Concretization of BoundaryPaddedMatrix
3838

@@ -44,18 +44,18 @@ end
4444
# Test ComposedBoundaryPaddedTensor{3}
4545
################################################################################
4646

47-
n = 10
48-
m = 12
47+
n = 4
48+
m = 5
4949
o = 7
5050
A = rand(n,m,o)
51-
A[1,1,:] = A[end,1, :] = A[1,end, :] = A[end,end, :] = 0.0
52-
A[1,:,1] = A[end, :, 1] = A[1,:,end] = A[end,:,end] = 0.0
53-
A[:,1,1] = A[:,end,1] = A[:,1,end] = A[:,end,end] = 0.0
51+
A[1,1,:] = A[end,1, :] = A[1,end, :] = A[end,end, :] = zeros(o)
52+
A[1,:,1] = A[end, :, 1] = A[1,:,end] = A[end,:,end] = zeros(m)
53+
A[:,1,1] = A[:,end,1] = A[:,1,end] = A[:,end,end] = zeros(n)
5454

55-
lower = Matrix[A[1,2:(end-1), 2:(end-1)], A[2:(end-1),1, 2:(end-1)] A[2:(end-1), 2:(end-1), 1]]
56-
upper = Matrix[A[end, 2:(end-1), 2:(end-1)], A[2:(end-1), end, 2:(end-1)] A[2:(end-1), 2:(end-1), end]]
55+
lower = Matrix[A[1,2:(end-1), 2:(end-1)], A[2:(end-1),1, 2:(end-1)], A[2:(end-1), 2:(end-1), 1]]
56+
upper = Matrix[A[end, 2:(end-1), 2:(end-1)], A[2:(end-1), end, 2:(end-1)], A[2:(end-1), 2:(end-1), end]]
5757

58-
Apad = BoundaryPadded3Tensor{Float64, typeof(A), typeof(lower[1])}(lower, upper, A[2:(end-1), 2:(end-1), 2:(end-1)])
58+
Apad = DiffEqOperators.ComposedBoundaryPadded3Tensor{Float64, typeof(A), typeof(lower[1])}(lower, upper, A[2:(end-1), 2:(end-1), 2:(end-1)])
5959

6060
@test A == Array(Apad) #test Concretization of BoundaryPaddedMatrix
6161

0 commit comments

Comments
 (0)