Skip to content

Commit 86fb530

Browse files
Make DiffEqBase.NAN_CHECK public API (#4093)
NAN_CHECK is the hook the integrators use to detect a step that produced NaN, and downstream state types need a method on it for that detection to reach their elements, so it is already used as public API without being declared one. Declare it public alongside the other default-callback hooks, document it, and render it on the DiffEqBase API page. Co-authored-by: Chris Rackauckas <accounts@chrisrackauckas.com>
1 parent 5781391 commit 86fb530

5 files changed

Lines changed: 27 additions & 3 deletions

File tree

docs/src/api/diffeqbase.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ DiffEqBase.ODE_DEFAULT_ISOUTOFDOMAIN
2727
DiffEqBase.ODE_DEFAULT_NORM
2828
DiffEqBase.ODE_DEFAULT_PROG_MESSAGE
2929
DiffEqBase.ODE_DEFAULT_UNSTABLE_CHECK
30+
DiffEqBase.NAN_CHECK
3031
```
3132

3233
## Runge-Kutta tableau types

lib/DiffEqBase/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DiffEqBase"
22
uuid = "2b5f629d-d688-5b77-993f-72d75c75574e"
33
authors = ["Chris Rackauckas <accounts@chrisrackauckas.com>"]
4-
version = "7.10.0"
4+
version = "7.11.0"
55

66
[deps]
77
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"

lib/DiffEqBase/src/DiffEqBase.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ export AutoDePSpecialize
211211
:max_vector_callback_length, :max_vector_callback_length_int, :get_condition,
212212
# Default-callback API (integrator defaults overridable via solver keywords)
213213
:ODE_DEFAULT_NORM, :ODE_DEFAULT_ISOUTOFDOMAIN, :ODE_DEFAULT_PROG_MESSAGE,
214-
:ODE_DEFAULT_UNSTABLE_CHECK,
214+
:ODE_DEFAULT_UNSTABLE_CHECK, :NAN_CHECK,
215215
# Tableau extension supertypes downstream tableau packages subtype
216216
:Tableau, :ODERKTableau,
217217
# Error-estimate residual hooks solvers call/extend

lib/DiffEqBase/src/common_defaults.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,25 @@ function ODE_DEFAULT_PROG_MESSAGE(dt, u, p, t)
159159
return "dt=" * string(dt) * "\nt=" * string(t) * "\nmax u=" * string(maximum(abs.(u)))
160160
end
161161

162+
"""
163+
NAN_CHECK(x)
164+
165+
Recursively test whether `x` holds a `NaN`. The integrators use this to detect a step
166+
that produced `NaN` and reject it.
167+
168+
Methods are provided for numbers, `AbstractArray`s, `RecursiveArrayTools.AbstractVectorOfArray`s
169+
and `RecursiveArrayTools.ArrayPartition`s; nested containers are descended into, so a state
170+
made of arrays of arrays reports `true` if any leaf is `NaN`. `Enum` values always report
171+
`false`, which keeps discrete components of a mixed state vector from being sent through
172+
`isnan`. Add a method to make the check reach the elements of a custom state type:
173+
174+
```julia
175+
DiffEqBase.NAN_CHECK(x::MyStateType) = any(DiffEqBase.NAN_CHECK, x.parts)
176+
```
177+
178+
`NaN` is only one of the ways a step can go bad; `Inf` and overflow are handled separately
179+
by [`ODE_DEFAULT_UNSTABLE_CHECK`](@ref).
180+
"""
162181
NAN_CHECK(x::Number) = isnan(x)
163182
NAN_CHECK(x::Enum) = false
164183
function NAN_CHECK(x::Union{AbstractArray, RecursiveArrayTools.AbstractVectorOfArray})

lib/DiffEqBase/test/ode_default_unstable_check.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
using Test, RecursiveArrayTools, StaticArrays, SparseArrays
22

3-
using DiffEqBase: NAN_CHECK
3+
using DiffEqBase: DiffEqBase, NAN_CHECK
4+
5+
@static if VERSION >= v"1.11"
6+
@test Base.ispublic(DiffEqBase, :NAN_CHECK)
7+
end
48

59
@test !NAN_CHECK(3.0 + 4.0im)
610
@test NAN_CHECK(NaN)

0 commit comments

Comments
 (0)