Skip to content

Commit

Permalink
Merge e2b7247 into 44e810d
Browse files Browse the repository at this point in the history
  • Loading branch information
ranocha committed Mar 25, 2020
2 parents 44e810d + e2b7247 commit 9382caf
Show file tree
Hide file tree
Showing 9 changed files with 1,117 additions and 501 deletions.
52 changes: 52 additions & 0 deletions dev/Mattsson2017.jl
@@ -0,0 +1,52 @@
using LinearAlgebra

# order 2
Qp = [
-1//4 5//4 -1//2 0 0 0
-1//4 -5//4 2 -1//2 0 0
0 0 -3//2 2 -1//2 0
0 0 0 -3//2 2 -1//2
0 0 0 0 -5//4 5//4
0 0 0 0 -1//4 -1//4
]
Qm = Matrix(-Qp')
B = zero(Qp); B[1,1] = -1; B[end,end] = 1
M = Diagonal([1//4, 5//4, 1, 1, 5//4, 1//4])
Dp = M \ (Qp + B / 2)
Dm = M \ (Qm + B / 2)

# order 3
Qp = [
-1//12 3//4 -1//6 0 0 0
-5//12 -5//12 1 -1//6 0 0
0 -1//3 -1//2 1 -1//6 0
0 0 -1//3 -1//2 1 -1//6
0 0 0 -1//3 -5//12 3//4
0 0 0 0 -5//12 -1//12
]
Qm = Matrix(-Qp')
B = zero(Qp); B[1,1] = -1; B[end,end] = 1
M = Diagonal([5//12, 13//12, 1, 1, 13//12, 5//12])
Dp = M \ (Qp + B / 2)
Dm = M \ (Qm + B / 2)

# order 4
Qp = [
-1//48 205//288 -29//144 1//96 0 0 0 0 0 0 0
-169//288 -11//48 33//32 -43//144 1//12 0 0 0 0 0 0
11//144 -13//32 -29//48 389//288 -1//2 1//12 0 0 0 0 0
1//32 -11//144 -65//288 -13//16 3//2 -1//2 1//12 0 0 0 0
0 0 0 -1//4 -5//6 3//2 -1//2 1//12 0 0 0
0 0 0 0 -1//4 -5//6 3//2 -1//2 1//12 0 0
0 0 0 0 0 -1//4 -5//6 3//2 -1//2 1//12 0
0 0 0 0 0 0 -1//4 -13//16 389//288 -43//144 1//96
0 0 0 0 0 0 0 -65//288 -29//48 33//32 -29//144
0 0 0 0 0 0 0 -11//144 -13//32 -11//48 205//288
0 0 0 0 0 0 0 1//32 11//144 -169//288 -1//48
]
Qm = Matrix(-Qp')
B = zero(Qp); B[1,1] = -1; B[end,end] = 1
m = [49//144, 61//48, 41//48, 149//144]
M = Diagonal(vcat(m, ones(Int, size(Qp,1)-2*length(m)), m[end:-1:1]))
Dp = M \ (Qp + B / 2)
Dm = M \ (Qm + B / 2)
481 changes: 481 additions & 0 deletions src/SBP_coefficients/Mattsson2017.jl

Large diffs are not rendered by default.

974 changes: 487 additions & 487 deletions src/SBP_coefficients/MattssonNordström2004.jl

Large diffs are not rendered by default.

58 changes: 49 additions & 9 deletions src/SBP_operators.jl
Expand Up @@ -130,10 +130,42 @@ function offset(::DerivativeCoefficientRow{T,Start,Length}) where {T,Start,Lengt
Start
end

function -(coef_row::DerivativeCoefficientRow{T,Start,Length}) where {T,Start,Length}
function Base.:-(coef_row::DerivativeCoefficientRow{T,Start,Length}) where {T,Start,Length}
DerivativeCoefficientRow{T,Start,Length}(-coef_row.coef)
end

function widening_plus(row1::SVector{Length1,T}, row2::SVector{Length2,T}) where {T,Length1,Length2}
Length = max(Length1, Length2)
row = SVector{Length,T}(ntuple(i -> zero(T), Length))
for i in 1:Length1
row = Base.setindex(row, row[i] + row1[i], i)
end
for i in 1:Length2
row = Base.setindex(row, row[i] + row2[i], i)
end
row
end

function Base.:+(coef_row1::DerivativeCoefficientRow{T,Start1,Length1}, coef_row2::DerivativeCoefficientRow{T,Start2,Length2}) where {T,Start1,Length1,Start2,Length2}
End = max(Start1+Length1, Start2+Length2)
Start = min(Start1, Start2)
Length = End - Start
row = SVector{Length,T}(ntuple(i -> zero(T), Length))
for i in 1:Length1
j = i-Start1+Start
row = Base.setindex(row, row[j] + coef_row1.coef[i], j)
end
for i in 1:Length2
j = i-Start2+Start
row = Base.setindex(row, row[j] + coef_row2.coef[i], j)
end
DerivativeCoefficientRow{T,Start,Length}(row)
end

function Base.:/(coef_row::DerivativeCoefficientRow{T,Start,Length}, α::Number) where {T,Start,Length}
DerivativeCoefficientRow{T,Start,Length}(coef_row.coef / α)
end

@inline function convolve_left_row(coef_row::DerivativeCoefficientRow{T,Start,Length}, u) where {T,Start,Length}
@inbounds tmp = coef_row.coef[1]*u[Start]
@inbounds for i in 2:Length
Expand Down Expand Up @@ -175,11 +207,15 @@ end


@generated function convolve_interior_coefficients!(dest::AbstractVector, lower_coef::SVector{LowerOffset}, central_coef, upper_coef::SVector{UpperOffset}, u::AbstractVector, α, β, left_boundary_width, right_boundary_width, parallel) where {LowerOffset, UpperOffset}
ex = :( lower_coef[$LowerOffset]*u[i-$LowerOffset] )
for j in LowerOffset-1:-1:1
ex = :( $ex + lower_coef[$j]*u[i-$j] )
if LowerOffset > 0
ex = :( lower_coef[$LowerOffset]*u[i-$LowerOffset] )
for j in LowerOffset-1:-1:1
ex = :( $ex + lower_coef[$j]*u[i-$j] )
end
ex = :( $ex + central_coef*u[i] )
else
ex = :( central_coef*u[i] )
end
ex = :( $ex + central_coef*u[i] )
for j in 1:UpperOffset
ex = :( $ex + upper_coef[$j]*u[i+$j] )
end
Expand All @@ -206,11 +242,15 @@ function convolve_interior_coefficients!(dest::AbstractVector{T}, lower_coef::SV
end

@generated function convolve_interior_coefficients!(dest::AbstractVector, lower_coef::SVector{LowerOffset}, central_coef, upper_coef::SVector{UpperOffset}, u::AbstractVector, α, left_boundary_width, right_boundary_width, parallel) where {LowerOffset, UpperOffset}
ex = :( lower_coef[$LowerOffset]*u[i-$LowerOffset] )
for j in LowerOffset-1:-1:1
ex = :( $ex + lower_coef[$j]*u[i-$j] )
if LowerOffset > 0
ex = :( lower_coef[$LowerOffset]*u[i-$LowerOffset] )
for j in LowerOffset-1:-1:1
ex = :( $ex + lower_coef[$j]*u[i-$j] )
end
ex = :( $ex + central_coef*u[i] )
else
ex = :( central_coef*u[i] )
end
ex = :( $ex + central_coef*u[i] )
for j in 1:UpperOffset
ex = :( $ex + upper_coef[$j]*u[i+$j] )
end
Expand Down
6 changes: 3 additions & 3 deletions src/SummationByPartsOperators.jl
Expand Up @@ -14,9 +14,7 @@ using StaticArrays
@reexport using DiffEqBase
using DiffEqCallbacks

import Base: *, -
import LinearAlgebra: mul!
export mul!
@reexport using PolynomialBases
import PolynomialBases: integrate, evaluate_coefficients, evaluate_coefficients!,
compute_coefficients, compute_coefficients!
Expand Down Expand Up @@ -59,6 +57,7 @@ include("SBP_coefficients/Mattsson2012.jl")
include("SBP_coefficients/Mattsson2014.jl")
include("SBP_coefficients/MattssonAlmquistCarpenter2014Extended.jl")
include("SBP_coefficients/MattssonAlmquistCarpenter2014Optimal.jl")
include("SBP_coefficients/Mattsson2017.jl")

include("conservation_laws/general_laws.jl")
include("conservation_laws/burgers.jl")
Expand Down Expand Up @@ -91,7 +90,8 @@ export periodic_central_derivative_operator, periodic_derivative_operator, deriv
export Fornberg1998, Holoborodko2008, BeljaddLeFlochMishraParés2017
export MattssonNordström2004, MattssonSvärdNordström2004, MattssonSvärdShoeybi2008,
Mattsson2012, Mattsson2014,
MattssonAlmquistCarpenter2014Extended, MattssonAlmquistCarpenter2014Optimal
MattssonAlmquistCarpenter2014Extended, MattssonAlmquistCarpenter2014Optimal,
Mattsson2017
export Tadmor1989, MadayTadmor1989, Tadmor1993,
TadmorWaagan2012Standard, TadmorWaagan2012Convergent

Expand Down
2 changes: 1 addition & 1 deletion src/general_operators.jl
Expand Up @@ -31,7 +31,7 @@ Base.@propagate_inbounds function mul!(dest, D::AbstractDerivativeOperator, u)
mul!(dest, D, u, one(eltype(dest)))
end

@noinline function *(D::AbstractDerivativeOperator, u)
@noinline function Base.:*(D::AbstractDerivativeOperator, u)
@boundscheck begin
@argcheck size(D,1) == size(D,2) == length(u) DimensionMismatch
end
Expand Down
2 changes: 1 addition & 1 deletion test/periodic_operators_test.jl
Expand Up @@ -880,7 +880,7 @@ let N = 5
D = periodic_derivative_operator(1, 2, 0//1, (N-1)//1, N, -2)
@test Matrix(D) [
3//2 0//1 1//2 -2//1
-2//1 3//2 0//1 1//2
-2//1 3//2 0//1 1//2
1//2 -2//1 3//2 0//1
0//1 1//2 -2//1 3//2]
end
1 change: 1 addition & 0 deletions test/runtests.jl
Expand Up @@ -8,6 +8,7 @@ using Test
@time @testset "Fourier Operators" begin include("fourier_operators_test.jl") end
@time @testset "Legendre Operators" begin include("legendre_operators_test.jl") end
@time @testset "Sum of Operators" begin include("sum_of_operators_test.jl") end
@time @testset "Upwind Operators" begin include("upwind_operators.jl") end
@time @testset "Conservation Laws" begin
include("conservation_laws/burgers_test.jl")
include("conservation_laws/cubic_test.jl")
Expand Down
42 changes: 42 additions & 0 deletions test/upwind_operators.jl
@@ -0,0 +1,42 @@
using Test
using LinearAlgebra
using SummationByPartsOperators

# check construction of interior part of upwind operators
@testset "Check interior parts" begin
N = 21
xmin = 0//1
xmax = (N + 1) // 1
interior = 10:N-10


for acc_order in 2:4
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)
println(devnull, Dp_bounded)
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, -(acc_order - 1) ÷ 2)
Dm_periodic = periodic_derivative_operator(1, acc_order, xmin, xmax, N, -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]
res = M * Dp + Dm' * M
res[1,1] += 1
res[end,end] -= 1
@test norm(res) < 10N * eps()
x = grid(Dp_bounded)
for D in (Dp_bounded, Dm_bounded, Dc_bounded)
@test norm(D * x.^0) < 10N * 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
end
end

0 comments on commit 9382caf

Please sign in to comment.