Skip to content

Commit

Permalink
More boundschecks for scaling by (inverse) mass matrices (#311)
Browse files Browse the repository at this point in the history
* more tests for scaling functions

* bump version

* add error message

* same check for coupled operators

* fix test

* fix test
  • Loading branch information
ranocha authored Dec 12, 2024
1 parent 00164c7 commit ee2393d
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 39 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.70"
version = "0.5.71"

[deps]
ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197"
Expand Down
6 changes: 3 additions & 3 deletions src/SBP_operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ the quadrature rule associated with the SBP derivative operator `D`.
"""
function integrate(func, u::AbstractVector, D::DerivativeOperator)
@boundscheck begin
length(u) == length(grid(D))
length(u) == length(grid(D)) || throw(DimensionMismatch("sizes of input vector and operator do not match"))
end
@unpack Δx = D
@unpack left_weights, right_weights = D.coefficients
Expand All @@ -658,7 +658,7 @@ end
function scale_by_mass_matrix!(u::AbstractVector, D::DerivativeOperator)
Base.require_one_based_indexing(u)
@boundscheck begin
length(u) == length(grid(D))
length(u) == size(D, 2) || throw(DimensionMismatch("sizes of input vector and operator do not match"))
end
@unpack Δx = D
@unpack left_weights, right_weights = D.coefficients
Expand All @@ -679,7 +679,7 @@ end
function scale_by_inverse_mass_matrix!(u::AbstractVector, D::DerivativeOperator)
Base.require_one_based_indexing(u)
@boundscheck begin
length(u) == length(grid(D))
length(u) == size(D, 2) || throw(DimensionMismatch("sizes of input vector and operator do not match"))
end
@unpack Δx = D
@unpack left_weights, right_weights = D.coefficients
Expand Down
3 changes: 3 additions & 0 deletions src/coupling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,9 @@ end

for (fname, op) in ((:scale_by_mass_matrix!, Base.:*), (:scale_by_inverse_mass_matrix!, Base.:/))
@eval function $fname(u::AbstractVector, cD::UniformCoupledOperator, factor=true)
@boundscheck begin
length(u) == size(cD, 2) || throw(DimensionMismatch("sizes of input vector and operator do not match"))
end
@unpack D, meshgrid = cD
@unpack mesh, grid = meshgrid
ymin, ymax = first(grid), last(grid)
Expand Down
6 changes: 3 additions & 3 deletions src/filter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ end
function (filter::ConstantFilter)(u::AbstractVector, tmp::AbstractVector)
@unpack coefficients, nodal2modal, modal2nodal = filter
@boundscheck begin
length(coefficients) == length(tmp)
length(coefficients) == length(tmp) || throw(DimensionMismatch())
end

mul!(tmp, nodal2modal, u)
Expand All @@ -57,7 +57,7 @@ end

function (filter::ConstantFilter)(u::AbstractMatrix, tmp::AbstractVector)
@boundscheck begin
length(tmp) == size(u,1)
length(tmp) == size(u,1) || throw(DimensionMismatch())
end

for j in Base.OneTo(size(u,2))
Expand All @@ -71,7 +71,7 @@ end
function (filter::ConstantFilter)(u::AbstractMatrix, tmp::AbstractMatrix)
@unpack coefficients, nodal2modal, modal2nodal = filter
@boundscheck begin
size(tmp) == size(u)
size(tmp) == size(u) || throw(DimensionMismatch())
end

mul!(tmp, nodal2modal, u)
Expand Down
10 changes: 9 additions & 1 deletion src/fourier_operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ end

function integrate(func, u::AbstractVector, D::FourierDerivativeOperator)
@boundscheck begin
length(u) == length(grid(D))
length(u) == length(grid(D)) || throw(DimensionMismatch("sizes of input vector and operator do not match"))
end
@unpack Δx = D

Expand All @@ -150,12 +150,20 @@ function mass_matrix(D::FourierDerivativeOperator)
end

function scale_by_mass_matrix!(u::AbstractVector, D::FourierDerivativeOperator)
Base.require_one_based_indexing(u)
@boundscheck begin
length(u) == size(D, 2) || throw(DimensionMismatch("sizes of input vector and operator do not match"))
end
@unpack Δx = D

u .*= Δx
end

function scale_by_inverse_mass_matrix!(u::AbstractVector, D::FourierDerivativeOperator)
Base.require_one_based_indexing(u)
@boundscheck begin
length(u) == size(D, 2) || throw(DimensionMismatch("sizes of input vector and operator do not match"))
end
@unpack Δx = D

u ./= Δx
Expand Down
2 changes: 1 addition & 1 deletion src/fourier_operators_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ end

function integrate(func, u::AbstractMatrix, D::FourierDerivativeOperator2D)
@boundscheck begin
length(u) == length(grid(D))
length(u) == length(grid(D)) || throw(DimensionMismatch())
end
@unpack Δx, Δy = D

Expand Down
2 changes: 1 addition & 1 deletion src/general_operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ the quadrature rule associated with the derivative operator `D`.
"""
function integrate(func, u::AbstractVector, D::AbstractPeriodicDerivativeOperator)
@boundscheck begin
length(u) == length(grid(D))
length(u) == length(grid(D)) || throw(DimensionMismatch("sizes of input vector and operator do not match"))
end
@unpack Δx = D

Expand Down
14 changes: 7 additions & 7 deletions src/legendre_operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,25 +109,25 @@ mass_matrix(D::Union{LegendreDerivativeOperator,LegendreSecondDerivativeOperator
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)
Base.require_one_based_indexing(u)
@boundscheck begin
@argcheck N == length(u)
length(u) == size(D, 2) || throw(DimensionMismatch("sizes of input vector and operator do not match"))
end
@unpack Δx, basis = D

@inbounds @simd for i in eachindex(u, basis.weights)
u[i] = factor * u[i] * (Δx * basis.weights[i])
end

u
return u
end

function scale_by_inverse_mass_matrix!(u::AbstractVector, D::Union{LegendreDerivativeOperator,LegendreSecondDerivativeOperator}, factor=true)
@unpack Δx, basis = D
N, _ = size(D)
Base.require_one_based_indexing(u)
@boundscheck begin
@argcheck N == length(u)
length(u) == size(D, 2) || throw(DimensionMismatch("sizes of input vector and operator do not match"))
end
@unpack Δx, basis = D

@inbounds @simd for i in eachindex(u, basis.weights)
u[i] = factor * u[i] / (Δx * basis.weights[i])
Expand Down
8 changes: 4 additions & 4 deletions src/matrix_operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ mass_matrix(D::MatrixDerivativeOperator) = Diagonal(D.weights)
Base.eltype(D::MatrixDerivativeOperator{T}) where {T} = T

function scale_by_mass_matrix!(u::AbstractVector, D::MatrixDerivativeOperator, factor=true)
N, _ = size(D)
Base.require_one_based_indexing(u)
@boundscheck begin
@argcheck N == length(u)
length(u) == size(D, 2) || throw(DimensionMismatch("sizes of input vector and operator do not match"))
end

@inbounds @simd for i in eachindex(u, D.weights)
Expand All @@ -64,9 +64,9 @@ function scale_by_mass_matrix!(u::AbstractVector, D::MatrixDerivativeOperator, f
end

function scale_by_inverse_mass_matrix!(u::AbstractVector, D::MatrixDerivativeOperator, factor=true)
N, _ = size(D)
Base.require_one_based_indexing(u)
@boundscheck begin
@argcheck N == length(u)
length(u) == size(D, 2) || throw(DimensionMismatch("sizes of input vector and operator do not match"))
end

@inbounds @simd for i in eachindex(u, D.weights)
Expand Down
10 changes: 9 additions & 1 deletion src/periodic_operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ the quadrature rule associated with the periodic derivative operator `D`.
"""
function integrate(func, u::AbstractVector, D::PeriodicDerivativeOperator)
@boundscheck begin
length(u) == length(grid(D))
length(u) == length(grid(D)) || throw(DimensionMismatch("sizes of input vector and operator do not match"))
end
@unpack Δx = D

Expand All @@ -724,12 +724,20 @@ function mass_matrix(D::PeriodicDerivativeOperator)
end

function scale_by_mass_matrix!(u::AbstractVector, D::PeriodicDerivativeOperator)
Base.require_one_based_indexing(u)
@boundscheck begin
length(u) == size(D, 2) || throw(DimensionMismatch("sizes of input vector and operator do not match"))
end
@unpack Δx = D

u .*= Δx
end

function scale_by_inverse_mass_matrix!(u::AbstractVector, D::PeriodicDerivativeOperator)
Base.require_one_based_indexing(u)
@boundscheck begin
length(u) == size(D, 2) || throw(DimensionMismatch("sizes of input vector and operator do not match"))
end
@unpack Δx = D

u ./= Δx
Expand Down
20 changes: 11 additions & 9 deletions src/var_coef_operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,13 @@ function mass_matrix(D::Union{DerivativeOperator,VarCoefDerivativeOperator})
end

function scale_by_mass_matrix!(u::AbstractVector, D::Union{DerivativeOperator,VarCoefDerivativeOperator}, factor=true)
@unpack Δx = D
@unpack left_weights, right_weights = D.coefficients
N, _ = size(D)
Base.require_one_based_indexing(u)
N = size(D, 1)
@boundscheck begin
@argcheck N == length(u)
length(u) == N || throw(DimensionMismatch("sizes of input vector and operator do not match"))
end
@unpack Δx = D
@unpack left_weights, right_weights = D.coefficients

@simd for i in eachindex(left_weights)
@inbounds u[i] = factor * u[i] * (Δx * left_weights[i])
Expand All @@ -126,16 +127,17 @@ function scale_by_mass_matrix!(u::AbstractVector, D::Union{DerivativeOperator,Va
@inbounds u[end-i+1] = factor * u[end-i+1] * (Δx * right_weights[i])
end

u
return u
end

function scale_by_inverse_mass_matrix!(u::AbstractVector, D::Union{DerivativeOperator,VarCoefDerivativeOperator}, factor=true)
@unpack Δx = D
@unpack left_weights, right_weights = D.coefficients
N, _ = size(D)
Base.require_one_based_indexing(u)
N = size(D, 1)
@boundscheck begin
@argcheck N == length(u)
length(u) == N || throw(DimensionMismatch("sizes of input vector and operator do not match"))
end
@unpack Δx = D
@unpack left_weights, right_weights = D.coefficients

@simd for i in eachindex(left_weights)
@inbounds u[i] = factor * u[i] / (Δx * left_weights[i])
Expand Down
Loading

2 comments on commit ee2393d

@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/121244

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.71 -m "<description of version>" ee2393d4099207160a9862c958222c60554206ab
git push origin v0.5.71

Please sign in to comment.