Skip to content

Commit

Permalink
remove LU decomposition again
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaLampert committed Dec 11, 2024
1 parent f017619 commit ca345a5
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 68 deletions.
2 changes: 1 addition & 1 deletion src/DispersiveShallowWater.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ using DiffEqBase: DiffEqBase, SciMLBase, terminate!
using FastBroadcast: @..
using Interpolations: Interpolations, linear_interpolation
using LinearAlgebra: mul!, ldiv!, I, Diagonal, Symmetric, diag,
lu, lu!, cholesky, cholesky!, issuccess
lu, cholesky, cholesky!, issuccess
using PolynomialBases: PolynomialBases
using Printf: @printf, @sprintf
using RecipesBase: RecipesBase, @recipe, @series
Expand Down
9 changes: 1 addition & 8 deletions src/equations/equations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ function solve_system_matrix!(dv, system_matrix, rhs,
if issuccess(factorization)
scale_by_mass_matrix!(rhs, D1)
# see https://github.com/JoshuaLampert/DispersiveShallowWater.jl/issues/122
dv .= system_matrix \ rhs[2:(end - 1)]
dv .= factorization \ rhs[2:(end - 1)]
else
# The factorization may fail if the time step is too large
# and h becomes negative.
Expand All @@ -581,13 +581,6 @@ function solve_system_matrix!(dv, system_matrix, rhs,
return nothing
end

# Svärd-Kalisch equations for reflecting boundary conditions uses LU factorization
function solve_system_matrix_lu!(dv, system_matrix_lu, ::SvaerdKalischEquations1D, cache)
(; factorization_lu) = cache
lu!(factorization_lu, system_matrix_lu)
ldiv!(factorization_lu, dv)
end

function solve_system_matrix!(dv, system_matrix, ::Union{BBMEquation1D, BBMBBMEquations1D})
ldiv!(system_matrix, dv)
end
Expand Down
60 changes: 1 addition & 59 deletions src/equations/svaerd_kalisch_1d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,6 @@ function assemble_system_matrix!(cache, h,
return Symmetric(Diagonal((@view M_h[2:(end - 1)])) + minus_MD1betaD1)
end

function assemble_system_matrix_lu!(cache, h,
::SvaerdKalischEquations1D,
::BoundaryConditionReflecting)
(; D1betaD1d) = cache
return Diagonal((@view h[2:(end - 1)])) - D1betaD1d
end

function create_cache(mesh, equations::SvaerdKalischEquations1D,
solver, initial_condition,
boundary_conditions::BoundaryConditionPeriodic,
Expand Down Expand Up @@ -333,15 +326,10 @@ function create_cache(mesh, equations::SvaerdKalischEquations1D,
D1 isa UniformCoupledOperator
D1_central = D1
D1mat = sparse(D1_central)
D1betaD1d = sparse(D1mat * Diagonal(beta_hat) * D1mat * Pd)[2:(end - 1), :]
minus_MD1betaD1 = sparse(D1mat' * Diagonal(M_beta) * D1mat * Pd)[2:(end - 1), :]
system_matrix = let cache = (; D1, M_h, minus_MD1betaD1)
assemble_system_matrix!(cache, h, equations, boundary_conditions)
end
system_matrix_lu = let cache = (; D1betaD1d)
assemble_system_matrix_lu!(cache, h,
equations, boundary_conditions)
end
elseif D1 isa UpwindOperators
D1_central = D1.central
D1mat_minus = sparse(D1.minus)
Expand All @@ -354,9 +342,7 @@ function create_cache(mesh, equations::SvaerdKalischEquations1D,
throw(ArgumentError("unknown type of first-derivative operator: $(typeof(D1))"))
end
factorization = cholesky(system_matrix)
factorization_lu = lu(system_matrix_lu)
cache = (; factorization, factorization_lu, minus_MD1betaD1, D1betaD1d, D, h, hv, b,
eta_x, v_x,
cache = (; factorization, minus_MD1betaD1, D, h, hv, b, eta_x, v_x,
h_v_x, hv2_x, beta_hat, D1_central, D1, M_h)
return cache
end
Expand Down Expand Up @@ -510,50 +496,6 @@ function rhs!(dq, q, t, mesh, equations::SvaerdKalischEquations1D,
return nothing
end

function rhs_lu!(dq, q, t, mesh, equations::SvaerdKalischEquations1D,
initial_condition, boundary_conditions::BoundaryConditionReflecting,
source_terms, solver, cache)
# When constructing the `cache`, we assert that alpha and gamma are zero.
# We use this explicitly in the code below.
(; D, h, hv, b, eta_x, v_x, h_v_x, hv2_x, tmp1, D1_central, D1) = cache

g = gravity_constant(equations)
eta, v = q.x
deta, dv, dD = dq.x
fill!(dD, zero(eltype(dD)))

@trixi_timeit timer() "deta hyperbolic" begin
@.. b = equations.eta0 - D
@.. h = eta - b
@.. hv = h * v

mul!(deta, D1_central, -hv)
end

# split form
@trixi_timeit timer() "dv hyperbolic" begin
mul!(eta_x, D1_central, eta)
mul!(v_x, D1_central, v)
mul!(h_v_x, D1_central, hv)
@.. tmp1 = hv * v
mul!(hv2_x, D1_central, tmp1)
@.. dv = -(0.5 * (hv2_x + hv * v_x - v * h_v_x) +
g * h * eta_x)
end

@trixi_timeit timer() "source terms" calc_sources!(dq, q, t, source_terms, equations,
solver)
@trixi_timeit timer() "assemble system matrix" begin
system_matrix_lu = assemble_system_matrix_lu!(cache, h, equations, boundary_conditions)
end
@trixi_timeit timer() "solving elliptic system" begin
solve_system_matrix_lu!((@view dv[2:(end - 1)]), system_matrix_lu, equations, cache)
dv[1] = dv[end] = zero(eltype(dv))
end

return nothing
end

# The modified entropy/energy takes the whole `q` for every point in space
"""
DispersiveShallowWater.energy_total_modified!(e, q_global, equations::SvaerdKalischEquations1D, cache)
Expand Down

0 comments on commit ca345a5

Please sign in to comment.