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

Commit 81a4002

Browse files
committed
Correct concretization of periodic BCs
1 parent 39a3e99 commit 81a4002

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-13
lines changed

src/derivative_operators/BC_operators.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Qx, Qy, ... = PeriodicBC{T}(size(u)) #When all dimensions are to be extended wit
1919
2020
-------------------------------------------------------------------------------------
2121
Creates a periodic boundary condition, where the lower index end of some u is extended with the upper index end and vice versa.
22-
It is not reccomended to concretize this BC type in to a BandedMatrix, since the vast majority of bands will be all 0s. SpatseMatrix concretization is reccomended.
22+
It is not recommended to concretize this BC type in to a BandedMatrix, since the vast majority of bands will be all 0s. SparseMatrix concretization is recommended.
2323
"""
2424
struct PeriodicBC{T} <: AtomicBC{T}
2525
PeriodicBC(T::Type) = new{T}()

src/derivative_operators/concretization.jl

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ end
101101
# Boundary Condition Operator concretizations
102102
################################################################################
103103

104-
#Atomic BCs
104+
# * Atomic BCs
105+
106+
# ** Affine BCs
107+
105108
function LinearAlgebra.Array(Q::AffineBC{T}, N::Int) where {T}
106109
Q_L = [transpose(Q.a_l) transpose(zeros(T, N-length(Q.a_l))); Diagonal(ones(T,N)); transpose(zeros(T, N-length(Q.a_r))) transpose(Q.a_r)]
107110
Q_b = [Q.b_l; zeros(T,N); Q.b_r]
@@ -126,18 +129,22 @@ function SparseArrays.sparse(Q::AffineBC{T}, N::Int) where {T}
126129
SparseMatrixCSC(Q,N)
127130
end
128131

129-
LinearAlgebra.Array(Q::PeriodicBC{T}, N::Int) where T = (Array([transpose(zeros(T, N-1)) one(T); Diagonal(ones(T,N)); one(T) transpose(zeros(T, N-1))]), zeros(T, N))
130-
SparseArrays.SparseMatrixCSC(Q::PeriodicBC{T}, N::Int) where T = ([transpose(zeros(T, N-1)) one(T); Diagonal(ones(T,N)); one(T) transpose(zeros(T, N-1))], zeros(T, N))
132+
# ** Periodic BCs
133+
134+
LinearAlgebra.Array(Q::PeriodicBC{T}, N::Int) where T =
135+
([transpose(zeros(T, N-1)) one(T)
136+
Diagonal(ones(T,N))
137+
one(T) transpose(zeros(T, N-1))], zeros(T, N))
138+
139+
SparseArrays.SparseMatrixCSC(Q::PeriodicBC{T}, N::Int) where T =
140+
(vcat(hcat(zeros(T, 1,N-1), one(T)),
141+
Diagonal(ones(T,N)),
142+
hcat(one(T), zeros(T, 1, N-1))), zeros(T, N))
143+
131144
SparseArrays.sparse(Q::PeriodicBC{T}, N::Int) where T = SparseMatrixCSC(Q,N)
132-
function BandedMatrices.BandedMatrix(Q::PeriodicBC{T}, N::Int) where T #Not reccomended!
133-
Q_array = BandedMatrix{T}(Eye(N), (N-1, N-1))
134-
Q_array[1, end] = one(T)
135-
Q_array[1, 1] = zero(T)
136-
Q_array[end, 1] = one(T)
137-
Q_array[end, end] = zero(T)
138-
139-
return (Q_array, zeros(T, N))
140-
end
145+
146+
BandedMatrices.BandedMatrix(::PeriodicBC, ::Int) =
147+
throw(ArgumentError("Periodic boundary conditions should be concretized as sparse matrices"))
141148

142149
function LinearAlgebra.Array(Q::BoundaryPaddedVector)
143150
return [Q.l; Q.u; Q.r]

test/bc_concretizations.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using SparseArrays, DiffEqOperators, LinearAlgebra, Random,
2+
Test, BandedMatrices, FillArrays, LazyArrays, BlockBandedMatrices
3+
4+
@testset "Concretizations of BCs" begin
5+
@testset "Periodic BCs" begin
6+
N = 9
7+
T = Float64
8+
Q = PeriodicBC(T)
9+
@test_throws ArgumentError BandedMatrix(Q,N)
10+
11+
correct = vcat(hcat(zeros(T,1,N-1),one(T)),
12+
Diagonal(ones(T,N)),
13+
hcat(one(T),zeros(T,1,N-1)))
14+
15+
@testset "Sparse concretization" begin
16+
Qm,Qu = SparseMatrixCSC(Q,N)
17+
18+
@test Qm == correct
19+
@test Qm isa SparseMatrixCSC{T}
20+
@test Qu == zeros(T,N)
21+
end
22+
23+
@testset "Dense concretization" begin
24+
Qm,Qu = Array(Q,N)
25+
26+
@test Qm == correct
27+
@test Qm isa Matrix{T}
28+
@test Qu == zeros(T,N)
29+
end
30+
end
31+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ import Base: isapprox
1919
@time @safetestset "Higher Dimensional Concretization" begin include("concretization.jl") end
2020
@time @safetestset "Upwind Operator Interface" begin include("upwind_operators_interface.jl") end
2121
@time @safetestset "MOLFiniteDifference Interface" begin include("MOLtest.jl") end
22+
@time @safetestset "BC concretizations" begin include("bc_concretizations.jl") end

0 commit comments

Comments
 (0)