Skip to content

Commit

Permalink
enable abstract matrices for MatrixDerivativeOperator (#286)
Browse files Browse the repository at this point in the history
* move function space operators test to own file

* enable abstract matrices for MatrixDerivativeOperator
  • Loading branch information
ranocha committed Aug 17, 2024
1 parent 39a5785 commit b33682d
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 59 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SummationByPartsOperators"
uuid = "9f78cca6-572e-554e-b819-917d2f1cf240"
author = ["Hendrik Ranocha"]
version = "0.5.67"
version = "0.5.68"

[deps]
ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197"
Expand Down
10 changes: 5 additions & 5 deletions src/matrix_operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ experiment with new operators given in matrix form.
An instance of this type can be constructed by passing the endpoints
`xmin`, `xmax` of the desired grid as well as the `nodes`, `weights`, and the
derivative operator `D::Matrix` on a reference interval, assuming that the
derivative operator `D::AbstractMatrix` on a reference interval, assuming that the
`nodes` contain the boundary points of the reference interval. `source` is
the source of coefficients and can be `nothing` for experimentation.
"""
@auto_hash_equals struct MatrixDerivativeOperator{T, SourceOfCoefficients} <: AbstractNonperiodicDerivativeOperator{T}
@auto_hash_equals struct MatrixDerivativeOperator{T, Dtype <: AbstractMatrix{T}, SourceOfCoefficients} <: AbstractNonperiodicDerivativeOperator{T}
grid::Vector{T}
weights::Vector{T}
D::Matrix{T}
D::Dtype
accuracy_order::Int
source::SourceOfCoefficients

function MatrixDerivativeOperator(xmin::T, xmax::T,
nodes::Vector{T},
weights::Vector{T},
D::Matrix{T},
D::AbstractMatrix{T},
accuracy_order::Int,
source::SourceOfCoefficients) where {T <: Real, SourceOfCoefficients}
# The `nodes`, `weights`, and `D` are given on a reference interval.
Expand All @@ -33,7 +33,7 @@ the source of coefficients and can be `nothing` for experimentation.
grid = (nodes .- first(nodes)) ./ jac .+ xmin
Δx = inv(jac)

new{T, SourceOfCoefficients}(grid, Δx * weights, jac * D, accuracy_order, source)
new{T, typeof(D), SourceOfCoefficients}(grid, Δx * weights, jac * D, accuracy_order, source)
end
end

Expand Down
63 changes: 63 additions & 0 deletions test/function_space_operators_test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Test
using LinearAlgebra
using SummationByPartsOperators
using Optim: Optim # to enable loading the function space operator optimization code

if VERSION >= v"1.9"
@testset "Function space operators" begin
N = 5
x_min = -1.0
x_max = 1.0
nodes = collect(range(x_min, x_max, length=N))
source = GlaubitzNordströmÖffner2023()
for compact in (true, false)
show(IOContext(devnull, :compact=>compact), source)
end
B = zeros(N, N)
B[1, 1] = -1.0
B[N, N] = 1.0
let basis_functions = [x -> x^i for i in 0:3]
D = function_space_operator(basis_functions, nodes, source)
# Only first-derivative operators are implemented yet
@test_throws ArgumentError function_space_operator(basis_functions, nodes, source; derivative_order = 2)

@test grid(D) nodes
@test all(isapprox.(D * ones(N), zeros(N); atol = 1e-13))
@test D * nodes ones(N)
@test D * (nodes .^ 2) 2 * nodes
@test D * (nodes .^ 3) 3 * (nodes .^ 2)
M = mass_matrix(D)
@test M * D.D + D.D' * M B
end

let basis_functions = [one, identity, exp]
D = function_space_operator(basis_functions, nodes, source)

@test grid(D) nodes
@test all(isapprox.(D * ones(N), zeros(N); atol = 1e-13))
@test D * nodes ones(N)
@test D * exp.(nodes) exp.(nodes)
M = mass_matrix(D)
@test M * D.D + D.D' * M B
end

# test non-equidistant nodes generated by `nodes = [0.0, rand(8)..., 1.0]`
nodes = [0.0, 0.01585580467018155, 0.18010381213204507, 0.270467434432868,
0.37699483985320303, 0.5600831197666554, 0.5698824835924449, 0.623949064816263,
0.8574665549914025, 1.0]
N = length(nodes)
B = zeros(N, N)
B[1, 1] = -1.0
B[N, N] = 1.0
let basis_functions = [one, identity, exp]
D = function_space_operator(basis_functions, nodes, source)

@test grid(D) nodes
@test all(isapprox.(D * ones(N), zeros(N); atol = 1e-11))
@test D * nodes ones(N)
@test D * exp.(nodes) exp.(nodes)
M = mass_matrix(D)
@test M * D.D + D.D' * M B
end
end
end
158 changes: 105 additions & 53 deletions test/matrix_operators_test.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Test
using SparseArrays
using LinearAlgebra
using SummationByPartsOperators
using Optim: Optim # to enable loading the function space operator optimization code

# check construction of interior part of upwind operators
@testset "Check against some upwind operators" begin
@testset "Check against some upwind operators (dense)" begin
N = 14
xmin_construction = 0.5
xmax_construction = 1.0
Expand Down Expand Up @@ -115,61 +115,113 @@ using Optim: Optim # to enable loading the function space operator optimization
end
end

if VERSION >= v"1.9"
@testset "Function space operators" begin
N = 5
x_min = -1.0
x_max = 1.0
nodes = collect(range(x_min, x_max, length=N))
source = GlaubitzNordströmÖffner2023()
@testset "Check against some upwind operators (sparse)" begin
N = 14
xmin_construction = 0.5
xmax_construction = 1.0
xmin_application = -0.25
xmax_application = 0.75

for acc_order in 2:6
Dm_bounded = derivative_operator(Mattsson2017(:minus ), 1, acc_order,
xmin_construction, xmax_construction, N)
Dp_bounded = derivative_operator(Mattsson2017(:plus ), 1, acc_order,
xmin_construction, xmax_construction, N)
Dc_bounded = derivative_operator(Mattsson2017(:central), 1, acc_order,
xmin_construction, xmax_construction, N)

nodes = collect(grid(Dc_bounded))
weights = diag(mass_matrix(Dc_bounded))
Dm = MatrixDerivativeOperator(xmin_application, xmax_application,
nodes, weights, sparse(Dm_bounded), acc_order,
source_of_coefficients(Dm_bounded))
Dp = MatrixDerivativeOperator(xmin_application, xmax_application,
nodes, weights, sparse(Dp_bounded), acc_order,
source_of_coefficients(Dp_bounded))
Dc = MatrixDerivativeOperator(xmin_application, xmax_application,
nodes, weights, sparse(Dc_bounded), acc_order,
source_of_coefficients(Dc_bounded))
D = UpwindOperators(Dm, Dc, Dp)

for compact in (true, false)
show(IOContext(devnull, :compact=>compact), source)
end
B = zeros(N, N)
B[1, 1] = -1.0
B[N, N] = 1.0
let basis_functions = [x -> x^i for i in 0:3]
D = function_space_operator(basis_functions, nodes, source)
# Only first-derivative operators are implemented yet
@test_throws ArgumentError function_space_operator(basis_functions, nodes, source; derivative_order = 2)

@test grid(D) nodes
@test all(isapprox.(D * ones(N), zeros(N); atol = 1e-13))
@test D * nodes ones(N)
@test D * (nodes .^ 2) 2 * nodes
@test D * (nodes .^ 3) 3 * (nodes .^ 2)
M = mass_matrix(D)
@test M * D.D + D.D' * M B
show(IOContext(devnull, :compact=>compact), D)
show(IOContext(devnull, :compact=>compact), Dm)
show(IOContext(devnull, :compact=>compact), Dp)
show(IOContext(devnull, :compact=>compact), Dc)
summary(IOContext(devnull, :compact=>compact), D)
summary(IOContext(devnull, :compact=>compact), Dm)
summary(IOContext(devnull, :compact=>compact), Dp)
summary(IOContext(devnull, :compact=>compact), Dc)
end
M = mass_matrix(D)
@test M == mass_matrix(Dm)
@test M == mass_matrix(Dp)
@test M == mass_matrix(Dc)

let basis_functions = [one, identity, exp]
D = function_space_operator(basis_functions, nodes, source)
x = grid(D)
@test x == grid(Dm)
@test x == grid(Dp)
@test x == grid(Dc)

@test grid(D) nodes
@test all(isapprox.(D * ones(N), zeros(N); atol = 1e-13))
@test D * nodes ones(N)
@test D * exp.(nodes) exp.(nodes)
M = mass_matrix(D)
@test M * D.D + D.D' * M B
end
@test issymmetric(Dm) == false
@test issymmetric(Dp) == false
@test issymmetric(Dc) == false

# test non-equidistant nodes generated by `nodes = [0.0, rand(8)..., 1.0]`
nodes = [0.0, 0.01585580467018155, 0.18010381213204507, 0.270467434432868,
0.37699483985320303, 0.5600831197666554, 0.5698824835924449, 0.623949064816263,
0.8574665549914025, 1.0]
N = length(nodes)
B = zeros(N, N)
B[1, 1] = -1.0
B[N, N] = 1.0
let basis_functions = [one, identity, exp]
D = function_space_operator(basis_functions, nodes, source)

@test grid(D) nodes
@test all(isapprox.(D * ones(N), zeros(N); atol = 1e-11))
@test D * nodes ones(N)
@test D * exp.(nodes) exp.(nodes)
M = mass_matrix(D)
@test M * D.D + D.D' * M B
end
D_reference = upwind_operators(Mattsson2017;
derivative_order = 1, accuracy_order = acc_order,
xmin = xmin_application, xmax = xmax_application,
N = N)
@test x grid(D_reference)
@test M mass_matrix(D_reference)

u = sinpi.(x)
@test D.minus * u D_reference.minus * u
@test D.plus * u D_reference.plus * u
@test D.central * u D_reference.central * u

du = copy(u)
du_reference = copy(u)
α = 1.23
mul!(du, D.minus, u, α)
mul!(du_reference, D_reference.minus, u, α)
@test du du_reference
mul!(du, D.plus, u, α)
mul!(du_reference, D_reference.plus, u, α)
@test du du_reference
mul!(du, D.central, u, α)
mul!(du_reference, D_reference.central, u, α)
@test du du_reference

du = copy(u)
du_reference = copy(u)
β = 4.56
mul!(du, D.minus, u, α, β)
mul!(du_reference, D_reference.minus, u, α, β)
@test du du_reference
mul!(du, D.plus, u, α, β)
mul!(du_reference, D_reference.plus, u, α, β)
@test du du_reference
mul!(du, D.central, u, α, β)
mul!(du_reference, D_reference.central, u, α, β)
@test du du_reference

@test integrate(abs2, u, Dm) integrate(abs2, u, D_reference.minus)
@test integrate(abs2, u, Dp) integrate(abs2, u, D_reference.plus)
@test integrate(abs2, u, Dc) integrate(abs2, u, D_reference.central)

u = sinpi.(x)
u_reference = copy(u)
SummationByPartsOperators.scale_by_mass_matrix!(u, Dm)
SummationByPartsOperators.scale_by_mass_matrix!(u_reference, D_reference.minus)
@test u u_reference
SummationByPartsOperators.scale_by_inverse_mass_matrix!(u, Dm)
SummationByPartsOperators.scale_by_inverse_mass_matrix!(u_reference, D_reference.minus)
@test u u_reference

@test SummationByPartsOperators.get_weight(Dm, 1) == left_boundary_weight(D)
@test SummationByPartsOperators.get_weight(Dm, N) == right_boundary_weight(D)

@test SummationByPartsOperators.lower_bandwidth(Dm) == size(Dm, 1) - 1
@test SummationByPartsOperators.upper_bandwidth(Dm) == size(Dm, 1) - 1
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const SBP_TEST = get(ENV, "SBP_TEST", "all")
@time @testset "Linear Combinations of Operators" begin include("linear_combinations_of_operators_test.jl") end
@time @testset "Upwind Operators" begin include("upwind_operators_test.jl") end
@time @testset "Matrix Operators" begin include("matrix_operators_test.jl") end
@time @testset "Function Sapce Operators" begin include("function_space_operators_test.jl") end
@time @testset "Special Matrix Types" begin include("special_matrix_types.jl") end
@time @testset "Aqua" begin include("aqua.jl") end
@time @testset "AD" begin include("ad_test.jl") end
Expand Down

2 comments on commit b33682d

@ranocha
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/113322

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.68 -m "<description of version>" b33682da10975d6d54fae40b1dfa4e7b7c88b1f1
git push origin v0.5.68

Please sign in to comment.