Skip to content
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ BinaryHeaps = "1.0.0"
CommonSolve = "0.2.4"
DiffEqBase = "7.5"
ModelingToolkit = "11.26"
OrdinaryDiffEqCore = "4.4"
OrdinaryDiffEqCore = "4.14.3"
OrdinaryDiffEqLowOrderRK = "2"
OrdinaryDiffEqTsit5 = "2"
PrecompileTools = "1.1"
Expand Down
126 changes: 120 additions & 6 deletions src/OrdinaryDiffEqOperatorSplitting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,146 @@ import SciMLBase: ReturnCode
import SciMLBase: DEIntegrator, NullParameters, isadaptive
import SymbolicIndexingInterface: variable_symbols

import DiffEqBase: set_proposed_dt!

import RecursiveArrayTools

import OrdinaryDiffEqCore: OrdinaryDiffEqCore, isdtchangeable,
stepsize_controller!, step_accept_controller!, step_reject_controller!,
accept_step_controller

# `@verbosity_specifier` expands to code referring to SciMLLogging's names unqualified,
# so they have to be in scope: the presets handed to it below, plus `Standard`,
# `MessageLevel`, `AbstractVerbosityPreset` and `AbstractVerbositySpecifier`, which the
# generated constructor validates a caller's arguments against. Of those, only
# `MessageLevel` appears in no source line here, so source analysis reads it as a stale
# import -- hence the `no_stale_explicit_imports` ignore in test/qa/qa.jl.
import SciMLLogging: @SciMLMessage, @verbosity_specifier, AbstractVerbosityPreset,
AbstractVerbositySpecifier, All, Detailed, InfoLevel, MessageLevel, Minimal, None,
Silent, Standard

# In OrdinaryDiffEq v7 / DiffEqBase v7, passing verbose::Bool to inner ODE
# integrators is no longer supported. Convert Bool → DEVerbosity when available.
@static if isdefined(DiffEqBase, :DEVerbosity)
"""
OperatorSplittingVerbosity

Verbosity configuration for the splitting nodes of an operator-splitting solve.

A splitting node has diagnostics of its own -- the step size it chose and the
splitting error estimate behind that choice -- which are separate from anything the
inner integrators report. Those are toggled here, while `inner_verbosity` holds the
`DiffEqBase.DEVerbosity` handed to every inner integrator, mirroring the way
`DEVerbosity` itself nests `linear_verbosity` and `nonlinear_verbosity`.

# Toggles

- `splitting_step_accepted`: `t`, `dt` and `EEst` of each accepted splitting step.
- `splitting_step_rejected`: the same for a step the controller rejected.
- `inner_solver_stats`: a per-operator summary of the inner integrator work --
steps, rejections, `f` evaluations, Jacobians, `W` factorizations, linear solves
-- emitted once when the solve finishes.

# Examples

```julia
# A preset applies to this node and travels to the inner integrators.
verbose = OperatorSplittingVerbosity(SciMLLogging.All())

# Splitting diagnostics on, inner integrators quiet.
verbose = OperatorSplittingVerbosity(
splitting_step_accepted = SciMLLogging.InfoLevel(),
inner_solver_stats = SciMLLogging.InfoLevel(),
inner_verbosity = DiffEqBase.DEVerbosity(SciMLLogging.None()),
)
```

A `DEVerbosity`, a `SciMLLogging` preset or a `Bool` passed as `verbose` is accepted
and converted. A `DEVerbosity` becomes the `inner_verbosity` with the splitting
diagnostics left silent, so existing code keeps exactly its previous output.
"""
@verbosity_specifier OperatorSplittingVerbosity begin
toggles = (
:splitting_step_accepted, :splitting_step_rejected, :inner_solver_stats,
)

sub_specifiers = (:inner_verbosity,)

groups = (
step_control = (:splitting_step_accepted, :splitting_step_rejected),
performance = (:inner_solver_stats,),
)

presets = (
None = (
inner_verbosity = None(),
splitting_step_accepted = Silent(),
splitting_step_rejected = Silent(),
inner_solver_stats = Silent(),
),
Minimal = (
inner_verbosity = Minimal(),
splitting_step_accepted = Silent(),
splitting_step_rejected = Silent(),
inner_solver_stats = Silent(),
),
Standard = (
inner_verbosity = Standard(),
splitting_step_accepted = Silent(),
splitting_step_rejected = Silent(),
inner_solver_stats = Silent(),
),
Detailed = (
inner_verbosity = Detailed(),
splitting_step_accepted = Silent(),
splitting_step_rejected = InfoLevel(),
inner_solver_stats = InfoLevel(),
),
All = (
inner_verbosity = All(),
splitting_step_accepted = InfoLevel(),
splitting_step_rejected = InfoLevel(),
inner_solver_stats = InfoLevel(),
),
)
end

const DEFAULT_VERBOSITY = OperatorSplittingVerbosity(SciMLLogging.Minimal())

_inner_verbose(verbose::Bool) = verbose ?
DiffEqBase.DEVerbosity(SciMLLogging.Minimal()) :
DiffEqBase.DEVerbosity(SciMLLogging.None())
const DEFAULT_VERBOSITY = DiffEqBase.DEVerbosity(SciMLLogging.Minimal())
_inner_verbose(verbose::OperatorSplittingVerbosity) = verbose.inner_verbosity

# A splitting node owns an `OperatorSplittingVerbosity`; anything else a caller
# passes is the inner integrators' setting and is wrapped, leaving the splitting
# diagnostics off so that existing code keeps its previous output.
_process_verbose(verbose::OperatorSplittingVerbosity) = verbose
_process_verbose(verbose::DiffEqBase.DEVerbosity) = OperatorSplittingVerbosity(;
preset = SciMLLogging.Minimal(), inner_verbosity = verbose
)
_process_verbose(preset::AbstractVerbosityPreset) =
OperatorSplittingVerbosity(preset)
# A `Bool` says how loud the inner integrators should be and nothing about the
# splitting diagnostics, which both presets leave silent -- `verbose = true` keeps
# meaning what it meant before this node had diagnostics of its own. Left
# unconverted it would reach `@SciMLMessage` as a `Bool`, where `true` enables
# *every* toggle and logs a message per step.
_process_verbose(verbose::Bool) = OperatorSplittingVerbosity(
verbose ? SciMLLogging.Minimal() : SciMLLogging.None()
)
else
const DEFAULT_VERBOSITY = false
end
_inner_verbose(verbose) = verbose
_process_verbose(verbose) = verbose

# `verbose` reaches us either as a Bool or, through DiffEqBase v7's `init`, as a
# DEVerbosity whose first type parameter is the on/off flag. Neither can be used in
# a boolean context directly.
# verbosity specifier whose first type parameter is the on/off flag. Neither can be
# used in a boolean context directly.
_is_verbose(verbose::Bool) = verbose
_is_verbose(verbose) = true
@static if isdefined(DiffEqBase, :DEVerbosity)
_is_verbose(::DiffEqBase.DEVerbosity{B}) where {B} = B
end
_is_verbose(::AbstractVerbositySpecifier{B}) where {B} = B

"""
AbstractOperatorSplitFunction
Expand Down
98 changes: 98 additions & 0 deletions src/integrator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ function SciMLBase.set_proposed_dt!(sub::SplitSubIntegrator, dt)
end
return nothing
end
SciMLBase.get_proposed_dt(sub::SplitSubIntegrator) = sub.dtcache


"""
Expand Down Expand Up @@ -288,6 +289,15 @@ function SciMLBase.__init(
# Every setting is either one value for the whole tree or a TreeOption carrying a
# value per node. Beyond the four this integrator handles itself, whatever the
# caller passes travels down to the leaf integrators.
# `dtmax` defaults to the length of the integration interval, as in
# OrdinaryDiffEqCore. Without a bound an adaptive splitting node can propose a step
# larger than the whole tspan: its error estimate only sees the *splitting* error,
# which vanishes identically wherever the operators commute, so a solution that has
# collapsed onto a common fixed point reports zero error and lets `dt` run away.
if !haskey(kwargs, :dtmax)
kwargs = merge(values(kwargs), (; dtmax = abs(tf - t0)))
end
verbose = _process_verbose(verbose)
config = build_config_tree(prob.f, (; dt, adaptive, verbose, controller, kwargs...))
validate_dt_tree(config)
tType = typeof(config.values.dt)
Expand Down Expand Up @@ -841,10 +851,90 @@ function try_snap_children_to_tstop!(integrator::DEIntegrator, tstop)
end
end

"""
log_step_diagnostics(integrator, accepted)

Report the step size a splitting node chose and the error estimate behind it, under the
`splitting_step_accepted` / `splitting_step_rejected` toggles of
[`OperatorSplittingVerbosity`](@ref).

The splitting error estimate is otherwise invisible from the outside, which makes a step
size driven by an estimate that does not cover the whole error -- see the note in
[`PalindromicPairLieTrotterGodunov`](@ref) -- very hard to diagnose.
"""
function log_step_diagnostics(integrator::AnySplitIntegrator, accepted::Bool)
integrator.controller_cache === nothing && return
verbose = integrator.opts.verbose
t, dt = integrator.t, integrator.dt
eest = OrdinaryDiffEqCore.get_EEst(integrator)
if accepted
@SciMLMessage(
lazy"splitting step accepted: t = $(t), dt = $(dt), EEst = $(eest)",
verbose, :splitting_step_accepted
)
else
@SciMLMessage(
lazy"splitting step rejected: t = $(t), dt = $(dt), EEst = $(eest)",
verbose, :splitting_step_rejected
)
end
return
end

"""
log_inner_solver_stats(integrator)

Summarise, once at the end of a solve, how much work each operator's inner integrator
did: steps, rejections and whatever of `nf`/`njacs`/`nw`/`nsolve` its solver tracks.

A splitting node counts only its own steps, so the cost of a solve is otherwise
invisible -- and it is the inner counters that show whether a step size is being paid
for in Jacobians and factorizations. Controlled by the `inner_solver_stats` toggle of
[`OperatorSplittingVerbosity`](@ref).
"""
function log_inner_solver_stats(integrator::OperatorSplittingIntegrator)
verbose = integrator.opts.verbose
@SciMLMessage(verbose, :inner_solver_stats) do
lines = ["inner integrator statistics"]
_collect_inner_stats!(lines, integrator.child_subintegrators, ())
join(lines, "\n ")
end
return
end

function _collect_inner_stats!(lines, children::Tuple, path::Tuple)
for (i, child) in enumerate(children)
_collect_inner_stats!(lines, child, (path..., i))
end
return lines
end
function _collect_inner_stats!(lines, child::SplitSubIntegrator, path::Tuple)
push!(lines, "$(_showpath(path)): $(child.stats.naccept) steps, $(child.stats.nreject) rejected")
return _collect_inner_stats!(lines, child.child_subintegrators, path)
end
function _collect_inner_stats!(lines, child::DEIntegrator, path::Tuple)
s = SciMLBase.has_stats(child) ? child.sol.stats : nothing
if s === nothing
push!(lines, "$(_showpath(path)): no statistics available")
return lines
end
counters = (
"steps" => :naccept, "rejected" => :nreject, "f" => :nf,
"jacs" => :njacs, "W" => :nw, "linsolve" => :nsolve,
)
parts = [
"$name=$(getfield(s, field))" for (name, field) in counters
if hasfield(typeof(s), field)
]
push!(lines, "$(_showpath(path)) ($(nameof(typeof(child.alg)))): " * join(parts, " "))
return lines
end

function step_footer!(integrator::AnySplitIntegrator)
ttmp = integrator.t + integrator.dt # dt is signed by the integration direction
footer_reset_flags!(integrator)
setup_validity_flags!(integrator, ttmp)
log_step_diagnostics(integrator, should_accept_step(integrator))
if should_accept_step(integrator)
OrdinaryDiffEqCore.increment_accept!(integrator.stats)
integrator.last_step_failed = false
Expand Down Expand Up @@ -1466,6 +1556,13 @@ function step_accept_controller!(
# introspection.
integrator.dt = dtnew
integrator.dtcache = abs(dtnew)
# Bound the stored proposal, not just the step about to be taken. `step_header!`
# clamps `dt` before stepping either way, so this does not change which steps are
# taken -- but an unclamped `dtcache` is what `reinit!` restores and what `dt`
# reports afterwards, and a proposal above `dtmax` there reads as the bound having
# been ignored.
_fix_dt_at_bounds!(integrator)
integrator.dtcache = abs(integrator.dt)
return nothing
end

Expand Down Expand Up @@ -1513,6 +1610,7 @@ function SciMLBase.postamble!(integrator::OperatorSplittingIntegrator)
# step may run the finalizers and close out the solution.
integrator.postamble_done && return nothing
integrator.postamble_done = true
log_inner_solver_stats(integrator)
DiffEqBase.finalize!(integrator.callback, integrator.u, integrator.t, integrator)
save_endpoint!(integrator)
# Drop whatever a previous, longer run left behind (`reinit!` without `erase_sol`).
Expand Down
Loading
Loading