This repository was archived by the owner on Jul 19, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathbc_concretizations.jl
More file actions
158 lines (124 loc) · 5.21 KB
/
bc_concretizations.jl
File metadata and controls
158 lines (124 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using SparseArrays, DiffEqOperators, LinearAlgebra, Random,
Test, BandedMatrices, FillArrays, LazyArrays, BlockBandedMatrices, Compat
@testset "Concretizations of BCs" begin
T = Float64
L = 10one(T)
N = 9
δx = L/(N+1)
@testset "Affine BCs" begin
@testset "Dirichlet0BC" begin
Q = Dirichlet0BC(T)
correct = vcat(zeros(T,1,N),
Diagonal(ones(T,N)),
zeros(T,1,N))
@testset "$mode concretization" for (mode,Mat,Expected,ExpectedBandwidths) in [
("sparse -> Banded", sparse, BandedMatrix{T}, (1,-1)),
("Banded", BandedMatrix, BandedMatrix{T}, (1,-1)),
("Sparse", SparseMatrixCSC, SparseMatrixCSC{T}, nothing),
("Dense", Array, Matrix{T}, nothing)
]
Qm,Qu = Mat(Q,N)
@test Qm == correct
@test Qm isa Expected
@test Qu == zeros(T,N+2)
!isnothing(ExpectedBandwidths) &&
@test bandwidths(Qm) == ExpectedBandwidths
end
end
@testset "Neumann0BC" begin
Q = Neumann0BC(δx)
correct = vcat(hcat(one(T),zeros(T,1,N-1)),
Diagonal(ones(T,N)),
hcat(zeros(T,1,N-1),one(T)))
@testset "$mode concretization" for (mode,Mat,Expected,ExpectedBandwidths) in [
("sparse -> Banded", sparse, BandedMatrix{T}, (2,0)),
("Banded", BandedMatrix, BandedMatrix{T}, (2,0)),
("Sparse", SparseMatrixCSC, SparseMatrixCSC{T}, nothing),
("Dense", Array, Matrix{T}, nothing)
]
Qm,Qu = Mat(Q,N)
@test Qm == correct
@test Qm isa Expected
@test Qu == zeros(T,N+2)
!isnothing(ExpectedBandwidths) &&
@test bandwidths(Qm) == ExpectedBandwidths
end
@testset "Banded concretization, extra zeros" begin
@testset "lz = $lz" for lz = 0:3
@testset "rz = $rz" for rz = 0:3
Q′ = Neumann0BC(δx)
# Artificially add some zero coefficients, which should
# not increase the bandwidth of the concretized BC.
append!(Q′.a_l, zeros(lz))
append!(Q′.a_r, zeros(rz))
Q′m,Q′u = sparse(Q′,N)
@test bandwidths(Q′m) == (2,0)
@test Q′m == correct
@test Q′u == zeros(T,N+2)
end
end
end
end
@testset "General BCs" begin
@testset "Left BC order = $ld" for ld = 2:5
@testset "Right BC order = $rd" for rd = 2:5
αl = 0.0:ld-1
αr = 0.0:rd-1
Q = GeneralBC(αl, αr, δx)
correct = vcat(hcat(Q.a_l',zeros(T,1,N-(ld-2))),
Diagonal(ones(T,N)),
hcat(zeros(T,1,N-(rd-2)),Q.a_r'))
Qm,Qu = sparse(Q,N)
@test Qm == correct
@test Qm isa BandedMatrix{T}
@test bandwidths(Qm) == (rd-1,ld-3)
@test Qu == vcat(Q.b_l,zeros(T,N),Q.b_r)
end
end
end
@testset "Dirichlet0BC" begin
# This is equivalent to a Dirichlet0BC; the trailing zeros
# should be dropped and the bandwidths optimal.
Q = GeneralBC([0.0, 1.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0, 0.0], δx)
correct = vcat(zeros(T,1,N),
Diagonal(ones(T,N)),
zeros(T,1,N))
Qm = first(sparse(Q,N))
@test Qm == correct
@test bandwidths(Qm) == (1,-1)
end
@testset "Almost DirichletBC" begin
Q = GeneralBC([1.0, 1.0, 0.0, 0.0, eps(Float64)],
[1.0, 1.0, 0.0, 0.0, 0.0], δx)
correct = vcat(zeros(T,1,N),
Diagonal(ones(T,N)),
zeros(T,1,N))
Qm,Qu = sparse(Q,N)
@test Qm ≈ correct
@test bandwidths(Qm) == (1,2)
@test Qu ≈ vcat(-one(T),zeros(T,N),-one(T))
end
end
@testset "Periodic BCs" begin
Q = PeriodicBC(T)
@test_throws ArgumentError BandedMatrix(Q,N)
correct = vcat(hcat(zeros(T,1,N-1),one(T)),
Diagonal(ones(T,N)),
hcat(one(T),zeros(T,1,N-1)))
@testset "Sparse concretization" begin
Qm,Qu = SparseMatrixCSC(Q,N)
@test Qm == correct
@test Qm isa SparseMatrixCSC{T}
@test Qu == zeros(T,N+2)
Qm′ = first(sparse(Q, N))
@test Qm′ == correct
@test Qm′ isa SparseMatrixCSC{T}
end
@testset "Dense concretization" begin
Qm,Qu = Array(Q,N)
@test Qm == correct
@test Qm isa Matrix{T}
@test Qu == zeros(T,N+2)
end
end
end