Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Oct 25, 2024
1 parent 29ec4ab commit 2e67d59
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 71 deletions.
2 changes: 0 additions & 2 deletions core/src/Ribasim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ using OrdinaryDiffEqCore:
AbstractNLSolver,
calculate_residuals!
using DiffEqBase: DiffEqBase
using OrdinaryDiffEqNonlinearSolve: OrdinaryDiffEqNonlinearSolve, relax!, _compute_rhs!
using DiffEqBase: remake

# Interface for defining and solving the ODE problem of the physical layer.
using SciMLBase:
Expand Down
5 changes: 1 addition & 4 deletions core/src/config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ using LineSearch: BackTracking
using Logging: LogLevel, Debug, Info, Warn, Error
using ..Ribasim: Ribasim, isnode, nodetype
using OrdinaryDiffEqCore: OrdinaryDiffEqAlgorithm, OrdinaryDiffEqNewtonAdaptiveAlgorithm
using OrdinaryDiffEqNonlinearSolve: NLNewton, NonlinearSolveAlg, NewtonRaphson
using OrdinaryDiffEqNonlinearSolve: NonlinearSolveAlg, NewtonRaphson
using OrdinaryDiffEqLowOrderRK: Euler, RK4
using OrdinaryDiffEqTsit5: Tsit5
using OrdinaryDiffEqSDIRK: ImplicitEuler, KenCarp4, TRBDF2
Expand Down Expand Up @@ -289,9 +289,6 @@ function algorithm(solver::Solver; u0 = [])::OrdinaryDiffEqAlgorithm
kwargs = Dict{Symbol, Any}()

if algotype <: OrdinaryDiffEqNewtonAdaptiveAlgorithm
# kwargs[:nlsolve] = NLNewton(;
# relax = Ribasim.MonitoredBackTracking(; z_tmp = copy(u0), dz_tmp = copy(u0)),
# )
kwargs[:nlsolve] = NonlinearSolveAlg(
NewtonRaphson(; linesearch = BackTracking(), autodiff = AutoFiniteDiff()),
)
Expand Down
65 changes: 0 additions & 65 deletions core/src/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -859,71 +859,6 @@ relaxed_root(x::GradientTracer, threshold::Real) = x
get_level_from_storage(basin::Basin, state_idx::Int, storage::GradientTracer) = storage
stop_declining_negative_storage!(du, u::ComponentVector{<:GradientTracer}) = nothing

@kwdef struct MonitoredBackTracking{B, V}
linesearch::B = BackTracking()
dz_tmp::V = []
z_tmp::V = []
end

"""
Compute the residual of the non-linear solver, i.e. a measure of the
error in the solution to the implicit equation defined by the solver algorithm
"""
function residual(z, integrator, nlsolver, f)
(; uprev, t, p, dt, opts, isdae) = integrator
(; tmp, ztmp, γ, α, cache, method) = nlsolver
(; ustep, atmp, tstep, k, invγdt, tstep, k, invγdt) = cache
if isdae
_uprev = get_dae_uprev(integrator, uprev)
b, ustep2 =
_compute_rhs!(tmp, ztmp, ustep, α, tstep, k, invγdt, p, _uprev, f::TF, z)
else
b, ustep2 =
_compute_rhs!(tmp, ztmp, ustep, γ, α, tstep, k, invγdt, method, p, dt, f, z)
end
calculate_residuals!(
atmp,
b,
uprev,
ustep2,
opts.abstol,
opts.reltol,
opts.internalnorm,
t,
)
ndz = opts.internalnorm(atmp, t)
return ndz
end

"""
MonitoredBackTracing is a thin wrapper of BackTracking, making sure that
the BackTracking relaxation is rejected if it results in a residual increase
"""
function OrdinaryDiffEqNonlinearSolve.relax!(
dz,
nlsolver::AbstractNLSolver,
integrator::DEIntegrator,
f,
linesearch::MonitoredBackTracking,
)
(; linesearch, dz_tmp, z_tmp) = linesearch

# Store step before relaxation
@. dz_tmp = dz

# Apply relaxation and measure the residual change
@. z_tmp = nlsolver.z + dz
resid_before = residual(z_tmp, integrator, nlsolver, f)
relax!(dz, nlsolver, integrator, f, linesearch)
@. z_tmp = nlsolver.z + dz
resid_after = residual(z_tmp, integrator, nlsolver, f)

# If the residual increased due to the relaxation, reject it
if resid_after > resid_before
@. dz = dz_tmp
end
end

function build_state_vector(p::Parameters)
# It is assumed that the horizontal flow states come first in
# p.state_inflow_edge and p.state_outflow_edge
Expand Down

0 comments on commit 2e67d59

Please sign in to comment.