@@ -2,9 +2,132 @@ using SparseArrays, DiffEqOperators, LinearAlgebra, Random,
22 Test, BandedMatrices, FillArrays, LazyArrays, BlockBandedMatrices
33
44@testset " Concretizations of BCs" begin
5+ T = Float64
6+ L = 10 one (T)
7+ N = 9
8+ δx = L/ (N+ 1 )
9+
10+ @testset " Affine BCs" begin
11+ @testset " Dirichlet0BC" begin
12+ Q = Dirichlet0BC (T)
13+
14+ correct = vcat (zeros (T,1 ,N),
15+ Diagonal (ones (T,N)),
16+ zeros (T,1 ,N))
17+
18+ @testset " $mode concretization" for (mode,Mat,Expected,ExpectedBandwidths) in [
19+ (" sparse -> Banded" , sparse, BandedMatrix{T}, (1 ,- 1 )),
20+ (" Banded" , BandedMatrix, BandedMatrix{T}, (1 ,- 1 )),
21+ (" Sparse" , SparseMatrixCSC, SparseMatrixCSC{T}, nothing ),
22+ (" Dense" , Array, Matrix{T}, nothing )
23+ ]
24+ Qm,Qu = Mat (Q,N)
25+
26+ @test Qm == correct
27+ @test Qm isa Expected
28+ @test Qu == zeros (T,N+ 2 )
29+
30+ ! isnothing (ExpectedBandwidths) &&
31+ @test bandwidths (Qm) == ExpectedBandwidths
32+ end
33+ end
34+
35+ @testset " Neumann0BC" begin
36+ Q = Neumann0BC (δx)
37+
38+ correct = vcat (hcat (one (T),zeros (T,1 ,N- 1 )),
39+ Diagonal (ones (T,N)),
40+ hcat (zeros (T,1 ,N- 1 ),one (T)))
41+
42+ @testset " $mode concretization" for (mode,Mat,Expected,ExpectedBandwidths) in [
43+ (" sparse -> Banded" , sparse, BandedMatrix{T}, (2 ,0 )),
44+ (" Banded" , BandedMatrix, BandedMatrix{T}, (2 ,0 )),
45+ (" Sparse" , SparseMatrixCSC, SparseMatrixCSC{T}, nothing ),
46+ (" Dense" , Array, Matrix{T}, nothing )
47+ ]
48+ Qm,Qu = Mat (Q,N)
49+
50+ @test Qm == correct
51+ @test Qm isa Expected
52+ @test Qu == zeros (T,N+ 2 )
53+
54+ ! isnothing (ExpectedBandwidths) &&
55+ @test bandwidths (Qm) == ExpectedBandwidths
56+ end
57+
58+ @testset " Banded concretization, extra zeros" begin
59+ @testset " lz = $lz " for lz = 0 : 3
60+ @testset " rz = $rz " for rz = 0 : 3
61+ Q′ = Neumann0BC (δx)
62+ # Artificially add some zero coefficients, which should
63+ # not increase the bandwidth of the concretized BC.
64+ append! (Q′. a_l, zeros (lz))
65+ append! (Q′. a_r, zeros (rz))
66+
67+ Q′m,Q′u = sparse (Q′,N)
68+ @test bandwidths (Q′m) == (2 ,0 )
69+
70+ @test Q′m == correct
71+ @test Q′u == zeros (T,N+ 2 )
72+ end
73+ end
74+ end
75+ end
76+
77+ @testset " General BCs" begin
78+ @testset " Left BC order = $ld " for ld = 2 : 5
79+ @testset " Right BC order = $rd " for rd = 2 : 5
80+ αl = 0.0 : ld- 1
81+ αr = 0.0 : rd- 1
82+
83+ Q = GeneralBC (αl, αr, δx)
84+
85+ correct = vcat (hcat (Q. a_l' ,zeros (T,1 ,N- (ld- 2 ))),
86+ Diagonal (ones (T,N)),
87+ hcat (zeros (T,1 ,N- (rd- 2 )),Q. a_r' ))
88+
89+ Qm,Qu = sparse (Q,N)
90+
91+ @test Qm == correct
92+ @test Qm isa BandedMatrix{T}
93+ @test bandwidths (Qm) == (rd- 1 ,ld- 3 )
94+
95+ @test Qu == vcat (Q. b_l,zeros (T,N),Q. b_r)
96+ end
97+ end
98+ end
99+
100+ @testset " Dirichlet0BC" begin
101+ # This is equivalent to a Dirichlet0BC; the trailing zeros
102+ # should be dropped and the bandwidths optimal.
103+ Q = GeneralBC ([0.0 , 1.0 , 0.0 , 0.0 ], [0.0 , 1.0 , 0.0 , 0.0 , 0.0 ], δx)
104+
105+ correct = vcat (zeros (T,1 ,N),
106+ Diagonal (ones (T,N)),
107+ zeros (T,1 ,N))
108+
109+ Qm = first (sparse (Q,N))
110+ @test Qm == correct
111+ @test bandwidths (Qm) == (1 ,- 1 )
112+ end
113+
114+ @testset " Almost DirichletBC" begin
115+ Q = GeneralBC ([1.0 , 1.0 , 0.0 , 0.0 , eps (Float64)],
116+ [1.0 , 1.0 , 0.0 , 0.0 , 0.0 ], δx)
117+
118+ correct = vcat (zeros (T,1 ,N),
119+ Diagonal (ones (T,N)),
120+ zeros (T,1 ,N))
121+
122+ Qm,Qu = sparse (Q,N)
123+
124+ @test Qm ≈ correct
125+ @test bandwidths (Qm) == (1 ,2 )
126+ @test Qu ≈ vcat (- one (T),zeros (T,N),- one (T))
127+ end
128+ end
129+
5130 @testset " Periodic BCs" begin
6- N = 9
7- T = Float64
8131 Q = PeriodicBC (T)
9132 @test_throws ArgumentError BandedMatrix (Q,N)
10133
@@ -17,15 +140,19 @@ using SparseArrays, DiffEqOperators, LinearAlgebra, Random,
17140
18141 @test Qm == correct
19142 @test Qm isa SparseMatrixCSC{T}
20- @test Qu == zeros (T,N)
143+ @test Qu == zeros (T,N+ 2 )
144+
145+ Qm′ = first (sparse (Q, N))
146+ @test Qm′ == correct
147+ @test Qm′ isa SparseMatrixCSC{T}
21148 end
22149
23150 @testset " Dense concretization" begin
24151 Qm,Qu = Array (Q,N)
25152
26153 @test Qm == correct
27154 @test Qm isa Matrix{T}
28- @test Qu == zeros (T,N)
155+ @test Qu == zeros (T,N+ 2 )
29156 end
30157 end
31158end
0 commit comments