Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Stalling and Use ReturnCode #994

Merged
merged 3 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DiffEqBase"
uuid = "2b5f629d-d688-5b77-993f-72d75c75574e"
authors = ["Chris Rackauckas <[email protected]>"]
version = "6.145.6"
version = "6.146.0"

[deps]
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
Expand Down Expand Up @@ -79,7 +79,7 @@ PrecompileTools = "1"
Printf = "1.9"
RecursiveArrayTools = "2, 3"
Reexport = "1.0"
SciMLBase = "2.12.0"
SciMLBase = "2.19.0"
SciMLOperators = "0.2, 0.3"
Setfield = "0.8, 1"
SparseArrays = "1.9"
Expand Down
145 changes: 121 additions & 24 deletions src/termination_conditions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
NonlinearSafeTerminationReturnCode

Return Codes for the safe nonlinear termination conditions.

These return codes have been deprecated. Termination Conditions will return
`SciMLBase.Retcode.T` starting from v7.
"""
@enumx NonlinearSafeTerminationReturnCode begin
"""
Expand Down Expand Up @@ -116,15 +119,16 @@

```julia
RelSafeTerminationMode(; protective_threshold = nothing, patience_steps = 100,
patience_objective_multiplier = 3, min_max_factor = 1.3)
patience_objective_multiplier = 3, min_max_factor = 1.3, max_stalled_steps = nothing)
```
"""
Base.@kwdef struct RelSafeTerminationMode{T1, T2, T3} <:
Base.@kwdef struct RelSafeTerminationMode{T1, T2, T3, T4 <: Union{Nothing, Int}} <:
AbstractSafeNonlinearTerminationMode
protective_threshold::T1 = nothing
patience_steps::Int = 100
patience_objective_multiplier::T2 = 3
min_max_factor::T3 = 1.3
max_stalled_steps::T4 = nothing
end

@doc doc"""
Expand All @@ -137,15 +141,16 @@

```julia
AbsSafeTerminationMode(; protective_threshold = nothing, patience_steps = 100,
patience_objective_multiplier = 3, min_max_factor = 1.3)
patience_objective_multiplier = 3, min_max_factor = 1.3, max_stalled_steps = nothing)
```
"""
Base.@kwdef struct AbsSafeTerminationMode{T1, T2, T3} <:
Base.@kwdef struct AbsSafeTerminationMode{T1, T2, T3, T4 <: Union{Nothing, Int}} <:
AbstractSafeNonlinearTerminationMode
protective_threshold::T1 = nothing
patience_steps::Int = 100
patience_objective_multiplier::T2 = 3
min_max_factor::T3 = 1.3
max_stalled_steps::T4 = nothing
end

@doc doc"""
Expand All @@ -157,15 +162,16 @@

```julia
RelSafeBestTerminationMode(; protective_threshold = nothing, patience_steps = 100,
patience_objective_multiplier = 3, min_max_factor = 1.3)
patience_objective_multiplier = 3, min_max_factor = 1.3, max_stalled_steps = nothing)
```
"""
Base.@kwdef struct RelSafeBestTerminationMode{T1, T2, T3} <:
Base.@kwdef struct RelSafeBestTerminationMode{T1, T2, T3, T4 <: Union{Nothing, Int}} <:
AbstractSafeBestNonlinearTerminationMode
protective_threshold::T1 = nothing
patience_steps::Int = 100
patience_objective_multiplier::T2 = 3
min_max_factor::T3 = 1.3
max_stalled_steps::T4 = nothing
end

@doc doc"""
Expand All @@ -177,21 +183,23 @@

```julia
AbsSafeBestTerminationMode(; protective_threshold = nothing, patience_steps = 100,
patience_objective_multiplier = 3, min_max_factor = 1.3)
patience_objective_multiplier = 3, min_max_factor = 1.3, max_stalled_steps = nothing)
```
"""
Base.@kwdef struct AbsSafeBestTerminationMode{T1, T2, T3} <:
Base.@kwdef struct AbsSafeBestTerminationMode{T1, T2, T3, T4 <: Union{Nothing, Int}} <:
AbstractSafeBestNonlinearTerminationMode
protective_threshold::T1 = nothing
patience_steps::Int = 100
patience_objective_multiplier::T2 = 3
min_max_factor::T3 = 1.3
max_stalled_steps::T4 = nothing
end

mutable struct NonlinearTerminationModeCache{uType, T,
M <: AbstractNonlinearTerminationMode, I, OT, SV}
mutable struct NonlinearTerminationModeCache{uType, T, dep_retcode,
M <: AbstractNonlinearTerminationMode, I, OT, SV,
R <: Union{NonlinearSafeTerminationReturnCode.T, ReturnCode.T}, UN, ST, MSS}
u::uType
retcode::NonlinearSafeTerminationReturnCode.T
retcode::R
abstol::T
reltol::T
best_objective_value::T
Expand All @@ -200,6 +208,10 @@
objectives_trace::OT
nsteps::Int
saved_values::SV
u0_norm::UN
step_norm_trace::ST
max_stalled_steps::MSS
u_diff_cache::uType
end

get_termination_mode(cache::NonlinearTerminationModeCache) = cache.mode
Expand Down Expand Up @@ -227,7 +239,8 @@

function SciMLBase.init(du::Union{AbstractArray{T}, T}, u::Union{AbstractArray{T}, T},
mode::AbstractNonlinearTerminationMode, saved_value_prototype...;
abstol = nothing, reltol = nothing, kwargs...) where {T <: Number}
use_deprecated_retcodes::Val{D} = Val(true), # Remove in v8, warn in v7
abstol = nothing, reltol = nothing, kwargs...) where {D, T <: Number}
abstol = _get_tolerance(abstol, T)
reltol = _get_tolerance(reltol, T)
TT = typeof(abstol)
Expand All @@ -236,24 +249,75 @@
if mode isa AbstractSafeNonlinearTerminationMode
if mode isa AbsSafeTerminationMode || mode isa AbsSafeBestTerminationMode
initial_objective = maximum(abs, du)
u0_norm = nothing

Check warning on line 252 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L252

Added line #L252 was not covered by tests
else
initial_objective = maximum(abs, du) / (maximum(abs, du .+ u) + eps(TT))
u0_norm = mode.max_stalled_steps === nothing ? nothing : norm(u, 2)

Check warning on line 255 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L255

Added line #L255 was not covered by tests
end
objectives_trace = Vector{TT}(undef, mode.patience_steps)
step_norm_trace = mode.max_stalled_steps === nothing ? nothing :

Check warning on line 258 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L258

Added line #L258 was not covered by tests
Vector{TT}(undef, mode.max_stalled_steps)
best_value = initial_objective
max_stalled_steps = mode.max_stalled_steps
if ArrayInterface.can_setindex(u_) && !(u_ isa Number) && step_norm_trace !== nothing
u_diff_cache = similar(u_)

Check warning on line 263 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L261-L263

Added lines #L261 - L263 were not covered by tests
else
u_diff_cache = u_

Check warning on line 265 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L265

Added line #L265 was not covered by tests
end
else
initial_objective = nothing
objectives_trace = nothing
u0_norm = nothing
step_norm_trace = nothing

Check warning on line 271 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L270-L271

Added lines #L270 - L271 were not covered by tests
best_value = __cvt_real(T, Inf)
max_stalled_steps = nothing
u_diff_cache = u_

Check warning on line 274 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L273-L274

Added lines #L273 - L274 were not covered by tests
end

length(saved_value_prototype) == 0 && (saved_value_prototype = nothing)

return NonlinearTerminationModeCache{typeof(u_), TT, typeof(mode),
typeof(initial_objective), typeof(objectives_trace),
typeof(saved_value_prototype)}(u_, NonlinearSafeTerminationReturnCode.Default,
abstol, reltol, best_value, mode, initial_objective, objectives_trace, 0,
saved_value_prototype)
retcode = ifelse(D, NonlinearSafeTerminationReturnCode.Default, ReturnCode.Default)

Check warning on line 279 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L279

Added line #L279 was not covered by tests

return NonlinearTerminationModeCache{typeof(u_), TT, D, typeof(mode),

Check warning on line 281 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L281

Added line #L281 was not covered by tests
typeof(initial_objective), typeof(objectives_trace), typeof(saved_value_prototype),
typeof(retcode), typeof(u0_norm), typeof(step_norm_trace),
typeof(max_stalled_steps)}(u_, retcode, abstol, reltol, best_value, mode,
initial_objective, objectives_trace, 0, saved_value_prototype, u0_norm,
step_norm_trace, max_stalled_steps, u_diff_cache)
end

function SciMLBase.reinit!(cache::NonlinearTerminationModeCache{uType, T, dep_retcode}, du,

Check warning on line 289 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L289

Added line #L289 was not covered by tests
u, saved_value_prototype...; abstol = nothing, reltol = nothing,
kwargs...) where {uType, T, dep_retcode}
length(saved_value_prototype) != 0 && (cache.saved_values = saved_value_prototype)

Check warning on line 292 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L292

Added line #L292 was not covered by tests

u_ = cache.mode isa AbstractSafeBestNonlinearTerminationMode ?

Check warning on line 294 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L294

Added line #L294 was not covered by tests
(ArrayInterface.can_setindex(u) ? copy(u) : u) : nothing
cache.u = u_
cache.retcode = ifelse(dep_retcode, NonlinearSafeTerminationReturnCode.Default,

Check warning on line 297 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L296-L297

Added lines #L296 - L297 were not covered by tests
ReturnCode.Default)

cache.abstol = _get_tolerance(abstol, T)
cache.reltol = _get_tolerance(reltol, T)
cache.nsteps = 0

Check warning on line 302 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L300-L302

Added lines #L300 - L302 were not covered by tests

mode = get_termination_mode(cache)
if mode isa AbstractSafeNonlinearTerminationMode
if mode isa AbsSafeTerminationMode || mode isa AbsSafeBestTerminationMode
initial_objective = maximum(abs, du)

Check warning on line 307 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L304-L307

Added lines #L304 - L307 were not covered by tests
else
initial_objective = maximum(abs, du) / (maximum(abs, du .+ u) + eps(TT))
cache.max_stalled_steps !== nothing && (cache.u0_norm = norm(u_, 2))

Check warning on line 310 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L309-L310

Added lines #L309 - L310 were not covered by tests
end
best_value = initial_objective

Check warning on line 312 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L312

Added line #L312 was not covered by tests
else
initial_objective = nothing
objectives_trace = nothing
best_value = __cvt_real(T, Inf)

Check warning on line 316 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L314-L316

Added lines #L314 - L316 were not covered by tests
end
cache.best_objective_value = best_value
cache.initial_objective = initial_objective
return cache

Check warning on line 320 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L318-L320

Added lines #L318 - L320 were not covered by tests
end

# This dispatch is needed based on how Terminating Callback works!
Expand All @@ -273,8 +337,8 @@
return check_convergence(mode, du, u, uprev, cache.abstol, cache.reltol)
end

function (cache::NonlinearTerminationModeCache)(mode::AbstractSafeNonlinearTerminationMode,
du, u, uprev, args...)
function (cache::NonlinearTerminationModeCache{uType, TT, dep_retcode})(mode::AbstractSafeNonlinearTerminationMode,

Check warning on line 340 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L340

Added line #L340 was not covered by tests
du, u, uprev, args...) where {uType, TT, dep_retcode}
if mode isa AbsSafeTerminationMode || mode isa AbsSafeBestTerminationMode
objective = maximum(abs, du)
criteria = cache.abstol
Expand All @@ -285,13 +349,15 @@

# Protective Break
if isinf(objective) || isnan(objective)
cache.retcode = NonlinearSafeTerminationReturnCode.ProtectiveTermination
cache.retcode = ifelse(dep_retcode,

Check warning on line 352 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L352

Added line #L352 was not covered by tests
NonlinearSafeTerminationReturnCode.ProtectiveTermination, ReturnCode.Unstable)
return true
end
## By default we turn this off since it has the potential for false positives
if cache.mode.protective_threshold !== nothing &&
(objective > cache.initial_objective * cache.mode.protective_threshold * length(du))
cache.retcode = NonlinearSafeTerminationReturnCode.ProtectiveTermination
cache.retcode = ifelse(dep_retcode,

Check warning on line 359 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L359

Added line #L359 was not covered by tests
NonlinearSafeTerminationReturnCode.ProtectiveTermination, ReturnCode.Unstable)
return true
end

Expand All @@ -307,7 +373,8 @@

# Main Termination Condition
if objective ≤ criteria
cache.retcode = NonlinearSafeTerminationReturnCode.Success
cache.retcode = ifelse(dep_retcode,

Check warning on line 376 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L376

Added line #L376 was not covered by tests
NonlinearSafeTerminationReturnCode.Success, ReturnCode.Success)
return true
end

Expand All @@ -324,13 +391,43 @@
min_obj, max_obj = extrema(cache.objectives_trace)
end
if min_obj < cache.mode.min_max_factor * max_obj
cache.retcode = NonlinearSafeTerminationReturnCode.PatienceTermination
cache.retcode = ifelse(dep_retcode,

Check warning on line 394 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L394

Added line #L394 was not covered by tests
NonlinearSafeTerminationReturnCode.PatienceTermination,
ReturnCode.Stalled)
return true

Check warning on line 397 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L397

Added line #L397 was not covered by tests
end
end
end

# Test for stalling if that is not disabled
if cache.step_norm_trace !== nothing
if ArrayInterface.can_setindex(cache.u_diff_cache) && !(u isa Number)
@. cache.u_diff_cache = u - uprev

Check warning on line 405 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L403-L405

Added lines #L403 - L405 were not covered by tests
else
cache.u_diff_cache = u .- uprev

Check warning on line 407 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L407

Added line #L407 was not covered by tests
end
du_norm = norm(cache.u_diff_cache, 2)
cache.step_norm_trace[mod1(cache.nsteps, length(cache.step_norm_trace))] = du_norm
if cache.nsteps ≥ cache.mode.max_stalled_steps
max_step_norm = maximum(cache.step_norm_trace)
if cache.mode isa AbsSafeTerminationMode ||

Check warning on line 413 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L409-L413

Added lines #L409 - L413 were not covered by tests
cache.mode isa AbsSafeBestTerminationMode
stalled_step = max_step_norm ≤ cache.abstol

Check warning on line 415 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L415

Added line #L415 was not covered by tests
else
stalled_step = max_step_norm ≤

Check warning on line 417 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L417

Added line #L417 was not covered by tests
cache.reltol * (max_step_norm + cache.u0_norm)
end
if stalled_step
cache.retcode = ifelse(dep_retcode,

Check warning on line 421 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L420-L421

Added lines #L420 - L421 were not covered by tests
NonlinearSafeTerminationReturnCode.PatienceTermination,
ReturnCode.Stalled)
return true
end
end
end

cache.retcode = NonlinearSafeTerminationReturnCode.Failure
cache.retcode = ifelse(dep_retcode,

Check warning on line 429 in src/termination_conditions.jl

View check run for this annotation

Codecov / codecov/patch

src/termination_conditions.jl#L429

Added line #L429 was not covered by tests
NonlinearSafeTerminationReturnCode.Failure, ReturnCode.Failure)
return false
end

Expand Down
Loading