-
Notifications
You must be signed in to change notification settings - Fork 12
/
upwind_operators_test.jl
153 lines (136 loc) · 5.26 KB
/
upwind_operators_test.jl
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
using Test
using LinearAlgebra
using SummationByPartsOperators
# check construction of interior part of upwind operators
@testset "Check interior parts" begin
N = 21
xmin = 0.0
xmax = Float64(N + 1)
interior = 10:N-10
for acc_order in 2:9
Dp_bounded = derivative_operator(Mattsson2017(:plus ), 1, acc_order, xmin, xmax, N)
Dm_bounded = derivative_operator(Mattsson2017(:minus ), 1, acc_order, xmin, xmax, N)
Dc_bounded = derivative_operator(Mattsson2017(:central), 1, acc_order, xmin, xmax, N)
for compact in (true, false)
show(IOContext(devnull, :compact=>compact), Dp_bounded)
show(IOContext(devnull, :compact=>compact), Dm_bounded)
show(IOContext(devnull, :compact=>compact), Dc_bounded)
summary(IOContext(devnull, :compact=>compact), Dp_bounded)
summary(IOContext(devnull, :compact=>compact), Dm_bounded)
summary(IOContext(devnull, :compact=>compact), Dc_bounded)
end
M = mass_matrix(Dp_bounded)
@test M == mass_matrix(Dm_bounded)
@test M == mass_matrix(Dc_bounded)
Dp_periodic = periodic_derivative_operator(1, acc_order, xmin, xmax, N-1, -(acc_order - 1) ÷ 2)
Dm_periodic = periodic_derivative_operator(1, acc_order, xmin, xmax, N-1, -acc_order + (acc_order - 1) ÷ 2)
Dp = Matrix(Dp_bounded)
Dm = Matrix(Dm_bounded)
@test Dp[interior,interior] ≈ Matrix(Dp_periodic)[interior,interior]
@test Dm[interior,interior] ≈ Matrix(Dm_periodic)[interior,interior]
D_periodic = upwind_operators(periodic_derivative_operator;
accuracy_order = acc_order,
xmin, xmax, N = N - 1)
@test D_periodic.minus == Dm_periodic
@test D_periodic.plus == Dp_periodic
@test Matrix(D_periodic.central) ≈ (Matrix(Dm_periodic) + Matrix(Dp_periodic)) / 2
res = M * Dp + Dm' * M
res[1,1] += 1
res[end,end] -= 1
@test norm(res) < N * eps()
x = grid(Dp_bounded)
for D in (Dp_bounded, Dm_bounded, Dc_bounded)
@test norm(D * x.^0) < N * eps()
for k in 1:acc_order÷2
@test D * x.^k ≈ k .* x.^(k-1)
end
for k in acc_order÷2+1:acc_order
@test (D * x.^k)[interior] ≈ (k .* x.^(k-1))[interior]
end
end
diss = M * (Dp - Dm)
@test diss ≈ diss'
@test maximum(eigvals(Symmetric(diss))) < N * eps()
# mass matrix scaling
x1 = grid(D_periodic)
M = @inferred mass_matrix(D_periodic)
u = sinpi.(x1)
v = copy(u)
scale_by_mass_matrix!(v, D_periodic)
@test v ≈ M * u
scale_by_inverse_mass_matrix!(v, D_periodic)
@test v ≈ u
end
end
@testset "UpwindOperators" begin
N = 21
xmin = 0.
xmax = Float64(N + 1)
acc_order = 2
D = upwind_operators(Mattsson2017, derivative_order=1, accuracy_order=acc_order,
xmin=xmin, xmax=xmax, N=N)
Dp = derivative_operator(Mattsson2017(:plus ), 1, acc_order, xmin, xmax, N)
Dm = derivative_operator(Mattsson2017(:minus ), 1, acc_order, xmin, xmax, N)
Dc = derivative_operator(Mattsson2017(:central), 1, acc_order, xmin, xmax, N)
for compact in (true, false)
show(IOContext(devnull, :compact=>compact), D)
show(IOContext(devnull, :compact=>compact), Dp)
show(IOContext(devnull, :compact=>compact), Dm)
show(IOContext(devnull, :compact=>compact), Dc)
summary(IOContext(devnull, :compact=>compact), D)
summary(IOContext(devnull, :compact=>compact), Dp)
summary(IOContext(devnull, :compact=>compact), Dm)
summary(IOContext(devnull, :compact=>compact), Dc)
end
@test D.minus == Dm
@test D.plus == Dp
@test D.central == Dc
@test derivative_order(D) == 1
@test grid(D) == grid(Dp)
@test SummationByPartsOperators.xmin(D) == SummationByPartsOperators.xmin(Dp)
@test SummationByPartsOperators.xmax(D) == SummationByPartsOperators.xmax(Dp)
@test mass_matrix(D) == mass_matrix(Dp)
@test left_boundary_weight(D) == left_boundary_weight(Dp)
@test right_boundary_weight(D) == right_boundary_weight(Dp)
x = grid(D)
@test integrate(x, D) == integrate(x, Dp)
B = zeros(N, N)
B[1, 1] = -1.0
B[end, end] = 1.0
M = mass_matrix(D)
@test M * Matrix(Dp) + Matrix(Dm)' * M ≈ B
@test_throws ArgumentError UpwindOperators(
derivative_operator(Mattsson2017(:minus ), 1, acc_order, xmin, xmax, N),
derivative_operator(Mattsson2017(:central), 1, acc_order, xmin, xmax, N+1),
derivative_operator(Mattsson2017(:plus ), 1, acc_order, xmin, xmax, N)
)
# mass matrix scaling
x1 = grid(D)
M = @inferred mass_matrix(D)
u = sinpi.(x1)
v = copy(u)
scale_by_mass_matrix!(v, D)
@test v ≈ M * u
scale_by_inverse_mass_matrix!(v, D)
@test v ≈ u
end
@testset "Empty lower/upper coefficients" begin
D = upwind_operators(periodic_derivative_operator, accuracy_order = 2,
xmin = 0.0, xmax = 1.0, N = 10)
x = grid(D)
u = @. sinpi(2 * x)
du = zero(u)
@test_nowarn mul!(du, D.minus, u)
@test du ≈ D.minus * u
@test_nowarn mul!(du, D.minus, u, 2.0)
@test du ≈ 2 * D.minus * u
@test_nowarn mul!(du, D.minus, u, 2.0, 3.0)
@test du ≈ 8 * D.minus * u # 5 = 2 + 3 * 2
du = zero(u)
@test_nowarn mul!(du, D.plus, u)
@test du ≈ D.plus * u
@test_nowarn mul!(du, D.plus, u, 2.0)
@test du ≈ 2 * D.plus * u
@test_nowarn mul!(du, D.plus, u, 2.0, 3.0)
@test du ≈ 8 * D.plus * u # 5 = 2 + 3 * 2
end