/
legendre_operators.jl
266 lines (205 loc) · 8.07 KB
/
legendre_operators.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""
LegendreDerivativeOperator{T<:Real}
A derivative operator on a nonperiodic Lobatto-Legendre grid with scalar type
`T` computing the first derivative using a Legendre expansion.
"""
@auto_hash_equals struct LegendreDerivativeOperator{T<:Real} <: AbstractNonperiodicDerivativeOperator{T}
jac::T
Δx::T
grid::Vector{T} # N nodes, including the left and the right boundary
basis::LobattoLegendre{T}
function LegendreDerivativeOperator(xmin::T, xmax::T, basis::LobattoLegendre{T}) where {T<:Real}
grid = map_from_canonical.(basis.nodes, xmin, xmax, basis)
jac = 2 / (xmax - xmin)
Δx = inv(jac)
new{T}(jac, Δx, grid, basis)
end
end
"""
LegendreDerivativeOperator(xmin::T, xmax::T, N::Int) where {T<:Real}
Construct the `LegendreDerivativeOperator` on a uniform grid between `xmin` and
`xmax` using `N` nodes and `N-1` Legendre modes.
"""
function LegendreDerivativeOperator(xmin::T, xmax::T, N::Int) where {T<:Real}
@argcheck N >= 2
basis = LobattoLegendre(N-1, T)
LegendreDerivativeOperator(xmin, xmax, basis)
end
"""
legendre_derivative_operator(xmin::Real, xmax::Real, N::Integer)
legendre_derivative_operator(; xmin::Real, xmax::Real, N::Integer)
Construct the `LegendreDerivativeOperator` on a uniform grid between `xmin` and
`xmax` using `N` nodes and `N-1` Legendre modes.
"""
function legendre_derivative_operator(xmin::Real, xmax::Real, N::Integer)
LegendreDerivativeOperator(promote(xmin, xmax)..., N)
end
function legendre_derivative_operator(; xmin::Real, xmax::Real, N::Integer)
legendre_derivative_operator(xmin, xmax, N)
end
derivative_order(D::LegendreDerivativeOperator) = 1
LinearAlgebra.issymmetric(D::LegendreDerivativeOperator) = false
"""
LegendreSecondDerivativeOperator{T<:Real}
A derivative operator on a nonperiodic Lobatto-Legendre grid with scalar type
`T` computing the second derivative using a Legendre expansion.
"""
@auto_hash_equals struct LegendreSecondDerivativeOperator{T<:Real} <: AbstractNonperiodicDerivativeOperator{T}
D2::Matrix{T}
jac::T
Δx::T
grid::Vector{T} # N nodes, including the left and the right boundary
basis::LobattoLegendre{T}
function LegendreSecondDerivativeOperator(xmin::T, xmax::T, N::Int) where {T<:Real}
@argcheck N >= 2
basis = LobattoLegendre(N-1, T)
D2 = basis.D^2
grid = map_from_canonical.(basis.nodes, xmin, xmax, basis)
jac = 2 / (xmax - xmin)
Δx = inv(jac)
new{T}(D2, jac, Δx, grid, basis)
end
end
"""
legendre_second_derivative_operator(xmin::Real, xmax::Real, N::Integer)
legendre_second_derivative_operator(; xmin::Real, xmax::Real, N::Integer)
Construct the `LegendreDerivativeOperator` on a uniform grid between `xmin` and
`xmax` using `N` nodes and `N-1` Legendre modes.
"""
function legendre_second_derivative_operator(xmin::Real, xmax::Real, N::Integer)
LegendreSecondDerivativeOperator(promote(xmin, xmax)..., N)
end
function legendre_second_derivative_operator(; xmin::Real, xmax::Real, N::Integer)
legendre_second_derivative_operator(xmin, xmax, N)
end
derivative_order(D::LegendreSecondDerivativeOperator) = 2
integrate(func, u, D::Union{LegendreDerivativeOperator,LegendreSecondDerivativeOperator}) = D.Δx*integrate(func, u, D.basis)
function evaluate_coefficients(u, D::Union{LegendreDerivativeOperator,LegendreSecondDerivativeOperator},
npoints=2*size(D,2)+1)
evaluate_coefficients(u, D.basis, npoints)
end
mass_matrix(D::Union{LegendreDerivativeOperator,LegendreSecondDerivativeOperator}) = Diagonal(D.Δx * D.basis.weights)
Base.eltype(D::Union{LegendreDerivativeOperator{T},LegendreSecondDerivativeOperator{T}}) where {T} = T
function scale_by_mass_matrix!(u::AbstractVector, D::Union{LegendreDerivativeOperator,LegendreSecondDerivativeOperator}, factor=true)
@unpack Δx, basis = D
N, _ = size(D)
@boundscheck begin
@argcheck N == length(u)
end
@inbounds @simd for i in eachindex(u, basis.weights)
u[i] = factor * u[i] * (Δx * basis.weights[i])
end
u
end
function scale_by_inverse_mass_matrix!(u::AbstractVector, D::Union{LegendreDerivativeOperator,LegendreSecondDerivativeOperator}, factor=true)
@unpack Δx, basis = D
N, _ = size(D)
@boundscheck begin
@argcheck N == length(u)
end
@inbounds @simd for i in eachindex(u, basis.weights)
u[i] = factor * u[i] / (Δx * basis.weights[i])
end
u
end
function get_weight(D::Union{LegendreDerivativeOperator,LegendreSecondDerivativeOperator}, i::Int)
@unpack Δx, basis = D
@unpack weights = basis
N, _ = size(D)
@boundscheck begin
@argcheck 1 <= i <= N
end
@inbounds ω = Δx * weights[i]
ω
end
function Base.show(io::IO, D::LegendreDerivativeOperator)
if get(io, :compact, false)
summary(io, D)
else
x = grid(D)
print(io, "First derivative operator {T=", eltype(D), "}")
print(io, " on ", length(x), " Lobatto Legendre nodes in [", first(x), ", ", last(x), "]")
end
end
function Base.show(io::IO, D::LegendreSecondDerivativeOperator)
if get(io, :compact, false)
summary(io, D)
else
x = grid(D)
print(io, "Second derivative operator {T=", eltype(D), "}")
print(io, " on ", length(x), " Lobatto Legendre nodes in [", first(x), ", ", last(x), "]")
end
end
function mul!(dest::AbstractVector, D::LegendreDerivativeOperator, u::AbstractVector, α=true, β=false)
@unpack jac, basis = D
N, _ = size(D)
@boundscheck begin
@argcheck N == length(u)
@argcheck N == length(dest)
end
mul!(dest, basis.D, u, α*jac, β)
end
function mul!(dest::AbstractVector, D::LegendreSecondDerivativeOperator, u::AbstractVector, α=true, β=false)
@unpack jac, D2 = D
N, _ = size(D2)
@boundscheck begin
@argcheck N == length(u)
@argcheck N == length(dest)
end
mul!(dest, D2, u, α*jac^2, β)
end
function derivative_left(D::LegendreSecondDerivativeOperator, u::AbstractVector, ::Val{1})
@unpack jac, basis = D
N, _ = size(D)
@boundscheck begin
@argcheck N == length(u)
end
jac * dot(view(basis.D, 1, :), u)
end
function derivative_right(D::LegendreSecondDerivativeOperator, u::AbstractVector, ::Val{1})
@unpack jac, basis = D
N, _ = size(D)
@boundscheck begin
@argcheck N == length(u)
end
jac * dot(view(basis.D, size(D,1), :), u)
end
function lower_bandwidth(D::Union{LegendreDerivativeOperator,LegendreSecondDerivativeOperator})
size(D, 1) - 1
end
function upper_bandwidth(D::Union{LegendreDerivativeOperator,LegendreSecondDerivativeOperator})
size(D, 1) - 1
end
function accuracy_order(D::Union{LegendreDerivativeOperator,LegendreSecondDerivativeOperator})
size(D, 1) - 1
end
function left_boundary_weight(D::Union{LegendreDerivativeOperator,LegendreSecondDerivativeOperator})
@inbounds retval = D.Δx * D.basis.weights[1]
retval
end
function right_boundary_weight(D::Union{LegendreDerivativeOperator,LegendreSecondDerivativeOperator})
@inbounds retval = D.Δx * D.basis.weights[end]
retval
end
function ConstantFilter(basis::LobattoLegendre{T}, filter, tmp::Array) where {T}
Np1 = length(basis.nodes)
coefficients = Array{T}(undef, Np1)
set_filter_coefficients!(coefficients, filter)
modal2nodal = legendre_vandermonde(basis)
nodal2modal = FactorisationWrapper(factorize(modal2nodal))
ConstantFilter(coefficients, nodal2modal, modal2nodal, tmp, filter)
end
function ConstantFilter(basis::LobattoLegendre{T}, filter, TmpEltype::DataType=T) where {T}
Np1 = length(basis.nodes)
tmp = Array{TmpEltype}(undef, Np1)
ConstantFilter(basis, filter, tmp)
end
"""
ConstantFilter(D::LegendreDerivativeOperator, filter, TmpEltype=T)
Create a modal filter with constant parameters adapted to the Legendre
derivative operator `D` with parameters given by the filter function `filter`.
"""
function ConstantFilter(D::LegendreDerivativeOperator{T}, filter, TmpEltype=T) where {T}
ConstantFilter(D.basis, filter, TmpEltype)
end
# TODO: LegendreSuperSpectralViscosity