diff --git a/docs/src/globalerrorcontrol/GlobalDiffEq.md b/docs/src/globalerrorcontrol/GlobalDiffEq.md index 32974ab9b27..abb8c7a43ed 100644 --- a/docs/src/globalerrorcontrol/GlobalDiffEq.md +++ b/docs/src/globalerrorcontrol/GlobalDiffEq.md @@ -34,7 +34,8 @@ and [`GlobalErrorMode`](@ref) selects how it is integrated — [`InterpolatingMo (store the dense solution, integrate the companion in a second pass) or [`SimultaneousMode`](@ref) (advance the companion inline over each step for `O(1)` extra memory and no second solve). [`global_error_estimate`](@ref)`(prob, alg)` -exposes the standalone estimator. +exposes the standalone estimator. With `DefectCorrection`, `solve` returns the estimated +error of each saved state in `sol.global_error`. To control the endpoint global error to a tolerance `gtol`, wrap any adaptive solver in [`GlobalAdjoint`](@ref) (adjoint-based, for endpoint functionals; @@ -46,6 +47,15 @@ every saved time), and its [`GlobalAdjointControl`](@ref) selects whether `gtol` is met by tolerance tightening ([`ToleranceRefinement`](@ref)) or by re-solving non-adaptively on the accepted step grid ([`StepGridRefinement`](@ref)). +`DefectCorrection` and `GlobalAdjoint` support callbacks (`ContinuousCallback`, +`VectorContinuousCallback`, `DiscreteCallback`) and semi-explicit mass-matrix DAEs, for +example ModelingToolkit models with events. `DefectCorrection` applies the callbacks to +the true solution, so its estimate includes a change of the event times, and it supports +affects with state of their own. `GlobalAdjoint` linearizes at the computed solution: at +each event the adjoint jumps with the saltation matrix of the event, and an affect must be +a function of the state, the parameters and the time. The warnings of these methods are +the `global_error` group of [`DEVerbosity`](@ref) (the `verbose` keyword of `solve`). + [`GlobalRichardson`](@ref) wraps any fixed-step method in global Richardson extrapolation over whole solves, interpreting `abstol` and `reltol` as global tolerances. It is the most robust and most expensive option. diff --git a/lib/DiffEqBase/Project.toml b/lib/DiffEqBase/Project.toml index cd42ad3abe9..b5ebcb75b3a 100644 --- a/lib/DiffEqBase/Project.toml +++ b/lib/DiffEqBase/Project.toml @@ -1,7 +1,7 @@ name = "DiffEqBase" uuid = "2b5f629d-d688-5b77-993f-72d75c75574e" authors = ["Chris Rackauckas "] -version = "7.21.7" +version = "7.22.0" [deps] ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9" diff --git a/lib/DiffEqBase/src/verbosity.jl b/lib/DiffEqBase/src/verbosity.jl index c2f1ba629c5..85d787775bd 100644 --- a/lib/DiffEqBase/src/verbosity.jl +++ b/lib/DiffEqBase/src/verbosity.jl @@ -18,6 +18,12 @@ :residual_control, :neutral_delay, :state_dependent_delay, + # Global error fields (GlobalDiffEq) + :global_error_event_mismatch, + :global_error_companion_order, + :global_error_final_solve, + :global_error_ad_fallback, + :global_error_grazing_event, ) presets = ( @@ -59,6 +65,12 @@ residual_control = Silent(), neutral_delay = Silent(), state_dependent_delay = Silent(), + # Global error fields (GlobalDiffEq) + global_error_event_mismatch = Silent(), + global_error_companion_order = Silent(), + global_error_final_solve = Silent(), + global_error_ad_fallback = Silent(), + global_error_grazing_event = Silent(), ), Minimal = ( linear_verbosity = Minimal(), @@ -98,6 +110,12 @@ residual_control = Silent(), neutral_delay = Silent(), state_dependent_delay = WarnLevel(), + # Global error fields (GlobalDiffEq) + global_error_event_mismatch = WarnLevel(), + global_error_companion_order = Silent(), + global_error_final_solve = WarnLevel(), + global_error_ad_fallback = Silent(), + global_error_grazing_event = WarnLevel(), ), Standard = ( linear_verbosity = Minimal(), @@ -137,6 +155,12 @@ residual_control = Silent(), neutral_delay = Silent(), state_dependent_delay = Silent(), + # Global error fields (GlobalDiffEq) + global_error_event_mismatch = WarnLevel(), + global_error_companion_order = WarnLevel(), + global_error_final_solve = WarnLevel(), + global_error_ad_fallback = WarnLevel(), + global_error_grazing_event = WarnLevel(), ), Detailed = ( linear_verbosity = Detailed(), @@ -176,6 +200,12 @@ residual_control = InfoLevel(), neutral_delay = InfoLevel(), state_dependent_delay = WarnLevel(), + # Global error fields (GlobalDiffEq) + global_error_event_mismatch = WarnLevel(), + global_error_companion_order = WarnLevel(), + global_error_final_solve = WarnLevel(), + global_error_ad_fallback = WarnLevel(), + global_error_grazing_event = WarnLevel(), ), All = ( linear_verbosity = All(), @@ -215,6 +245,12 @@ residual_control = InfoLevel(), neutral_delay = InfoLevel(), state_dependent_delay = InfoLevel(), + # Global error fields (GlobalDiffEq) + global_error_event_mismatch = WarnLevel(), + global_error_companion_order = WarnLevel(), + global_error_final_solve = WarnLevel(), + global_error_ad_fallback = WarnLevel(), + global_error_grazing_event = WarnLevel(), ), ) @@ -245,6 +281,13 @@ :neutral_delay, :state_dependent_delay, ), + global_error = ( + :global_error_event_mismatch, + :global_error_companion_order, + :global_error_final_solve, + :global_error_ad_fallback, + :global_error_grazing_event, + ), ) end @@ -304,6 +347,13 @@ diagnostic messages, warnings, and errors during ODE solution. - `neutral_delay`: Messages specific to neutral delay equations - `state_dependent_delay`: Messages when state-dependent delays are detected/evaluated +## Global Error Group (used by GlobalDiffEq.jl) +- `global_error_event_mismatch`: Messages when the events or the terminations of the forward solve and of the true solution, as the companion of `GlobalErrorEstimation` computes it, differ +- `global_error_companion_order`: Messages when `GlobalErrorEstimation` solves its companion with a solver of order 3 or lower, which meets the tight companion tolerances poorly +- `global_error_final_solve`: Messages when the final forward solve under global error control does not repeat the accepted solve (a callback with state outside of it, or a solve that is not deterministic) +- `global_error_ad_fallback`: Messages when `GlobalAdjoint` cannot differentiate the model or a callback with ReverseDiff or ForwardDiff, and uses finite differences or a numerical VJP +- `global_error_grazing_event`: Messages when an event of `GlobalAdjoint` is grazing (the vector field is tangent to the event surface), so that its saltation matrix has no time-shift term + # Constructors DEVerbosity(preset::AbstractVerbosityPreset) @@ -315,7 +365,7 @@ Create an `DEVerbosity` using a preset configuration: - `SciMLLogging.Detailed()`: Comprehensive debugging information - `SciMLLogging.All()`: Maximum verbosity - DEVerbosity(; preset = nothing, error_control = nothing, performance = nothing, numerical = nothing, sde_specific = nothing, dde_specific = nothing, kwargs...) + DEVerbosity(; preset = nothing, error_control = nothing, performance = nothing, numerical = nothing, sde_specific = nothing, dde_specific = nothing, global_error = nothing, kwargs...) Create an `DEVerbosity` with group-level or individual field control. diff --git a/lib/GlobalDiffEq/Project.toml b/lib/GlobalDiffEq/Project.toml index 57b9a12d953..0eb7f7a7d20 100644 --- a/lib/GlobalDiffEq/Project.toml +++ b/lib/GlobalDiffEq/Project.toml @@ -1,7 +1,7 @@ name = "GlobalDiffEq" uuid = "1d72d19b-84cc-4cb7-a099-7cbdb9ccc67c" authors = ["Chris Rackauckas and contributors"] -version = "1.7.3" +version = "1.8.0" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" @@ -20,28 +20,38 @@ RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd" Reexport = "189a3867-3050-52da-a836-e630ba90ab69" Richardson = "708f8203-808e-40c0-ba2d-98a6953ed40d" SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462" +SciMLLogging = "a6db7da4-7206-11f0-1eab-35f2a5dbe1d1" +SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5" [weakdeps] +DiffEqCallbacks = "459566f4-90b8-5000-8ac3-15dfb0a30def" SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1" +SimpleNonlinearSolve = "727e6d20-b764-4bd8-a329-72de5adea6c7" [extensions] -GlobalDiffEqSciMLSensitivityExt = "SciMLSensitivity" +GlobalDiffEqSciMLSensitivityExt = ["SciMLSensitivity", "DiffEqCallbacks"] +GlobalDiffEqSimpleNonlinearSolveExt = "SimpleNonlinearSolve" [sources] DiffEqBase = {path = "../DiffEqBase"} OrdinaryDiffEqCore = {path = "../OrdinaryDiffEqCore"} +OrdinaryDiffEqNonlinearSolve = {path = "../OrdinaryDiffEqNonlinearSolve"} +OrdinaryDiffEqRosenbrock = {path = "../OrdinaryDiffEqRosenbrock"} OrdinaryDiffEqTsit5 = {path = "../OrdinaryDiffEqTsit5"} [compat] ADTypes = "1" Accessors = "0.1.42" -DiffEqBase = "7" +DiffEqBase = "7.22" +DiffEqCallbacks = "4.18" DifferentiationInterface = "0.6, 0.7" FastBroadcast = "1.3" ForwardDiff = "0.10, 1" LinearAlgebra = "1" MuladdMacro = "0.2.4" OrdinaryDiffEqCore = "4" +OrdinaryDiffEqNonlinearSolve = "2" +OrdinaryDiffEqRosenbrock = "2.7" OrdinaryDiffEqSSPRK = "2" OrdinaryDiffEqTsit5 = "2" PrecompileTools = "1.1" @@ -51,21 +61,28 @@ Reexport = "1.0" Richardson = "1.2" SafeTestsets = "0.0.1, 0.1" SciMLBase = "3.49" -SciMLSensitivity = "7.116" +SciMLLogging = "2" +SciMLSensitivity = "7.119" SciMLTesting = "2.4" +SimpleNonlinearSolve = "2.11.1" +SymbolicIndexingInterface = "0.3.43" Test = "1" julia = "1.10" [extras] +DiffEqCallbacks = "459566f4-90b8-5000-8ac3-15dfb0a30def" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +OrdinaryDiffEqNonlinearSolve = "127b3ac7-2247-4354-8eb6-78cf4e7c58e8" +OrdinaryDiffEqRosenbrock = "43230ef6-c299-4910-a778-202eb28ce4ce" OrdinaryDiffEqSSPRK = "669c94d9-1f4b-4b64-b377-1aa079aa2388" OrdinaryDiffEqTsit5 = "b1df2697-797e-41e3-8120-5422d3b24e4a" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd" SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f" +SciMLLogging = "a6db7da4-7206-11f0-1eab-35f2a5dbe1d1" SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1" SciMLTesting = "09d9d899-5365-40a9-917a-5f67fddea283" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["LinearAlgebra", "OrdinaryDiffEqSSPRK", "OrdinaryDiffEqTsit5", "Random", "RecursiveArrayTools", "SafeTestsets", "SciMLSensitivity", "SciMLTesting", "Test"] +test = ["DiffEqCallbacks", "LinearAlgebra", "OrdinaryDiffEqNonlinearSolve", "OrdinaryDiffEqRosenbrock", "OrdinaryDiffEqSSPRK", "OrdinaryDiffEqTsit5", "Random", "RecursiveArrayTools", "SafeTestsets", "SciMLLogging", "SciMLSensitivity", "SciMLTesting", "Test"] diff --git a/lib/GlobalDiffEq/ext/GlobalDiffEqSciMLSensitivityExt.jl b/lib/GlobalDiffEq/ext/GlobalDiffEqSciMLSensitivityExt.jl index 7b564b31502..b80059bc444 100644 --- a/lib/GlobalDiffEq/ext/GlobalDiffEqSciMLSensitivityExt.jl +++ b/lib/GlobalDiffEq/ext/GlobalDiffEqSciMLSensitivityExt.jl @@ -1,25 +1,296 @@ module GlobalDiffEqSciMLSensitivityExt -import GlobalDiffEq, SciMLBase, SciMLSensitivity +import DiffEqCallbacks, GlobalDiffEq, LinearAlgebra, SciMLBase, SciMLSensitivity import Accessors: @set +using SciMLLogging: @SciMLMessage +import LinearAlgebra: dot -# Endpoint global-error projection along `direction`, computed entirely by the -# adjoint system. The dual-weighted residual ∫ λ(t)ᵀ (f(P(t)) − P'(t)) dt is -# obtained as the sensitivity of the discrete cost g = ⟨direction, u(T)⟩ to a -# scalar forcing ε that adds the numerical solution's defect r(t) = f(P) − P' to -# the dynamics: with `u' = f(u,p,t) + ε·r(t)`, dG/dε|₀ = ∫ λᵀ r dt exactly. The -# adjoint (λ' = −(∂f/∂u)ᵀλ, λ(T) = direction) is unchanged because r depends only -# on t, so `adjoint_sensitivities` returns that integral as the parameter -# gradient — no separate adjoint solve or hand-rolled quadrature. +# The adjoint methods that integrate the error integral with a quadrature callback. +const GaussAdjoints = Union{SciMLSensitivity.GaussAdjoint, SciMLSensitivity.GaussKronrodAdjoint} + +# The adjoint method of the algebraic projection in the DAE saltation products +# (`GlobalDiffEq._algebraic_solver`). +projection_sensealg() = + SciMLSensitivity.SteadyStateAdjoint(autojacvec = SciMLSensitivity.ReverseDiffVJP()) + +# The adjoint error identity of GlobalAdjoint. For the dual -Mᵀ ż = Jᵀ z along the forward +# solution, with z(T) = v, +# +# ⟨v, M e(T)⟩ = z(t₀)ᵀ M e(t₀) + ∫ zᵀ r dt - Σⱼ z(tⱼ)ᵀ M [P]ⱼ + (event terms), +# +# where r = f(P) - M P' is the defect of the dense output P and [P]ⱼ its jump at knot tⱼ. +# The integral is the sensitivity of ⟨v, u(T)⟩ to a scalar forcing ε in +# `u' = f(u) + ε r(t)`, so SciMLSensitivity returns it as the parameter gradient. + +# Move the integrator to `t`. The tstops before `t` are dropped first (`first_tstop` returns +# tdir·t), because the integrator checks after the callbacks that it passed no tstop. A +# tstop at `t` stays; the caller handles it before the skip. +function _skip_to!(integrator, t) + while SciMLBase.has_tstop(integrator) && + SciMLBase.first_tstop(integrator) < integrator.tdir * t + SciMLBase.pop_tstop!(integrator) + end + SciMLBase.set_t!(integrator, t) + SciMLBase.derivative_discontinuity!(integrator, true) + return nothing +end + +# The terminal value of the dual. The constraint at T moves an algebraic part of v to the +# differential part: with A the Jacobian (or secant Jacobian) of f at T and w = A_aa⁻ᵀ v_a, +# ⟨v, e⟩ = ⟨v_d - A_adᵀ w, e_d⟩ - wᵀ f_a(P(T⁻)). +function _terminal_value(v, A, fa, dif, alg_idx) + isempty(alg_idx) && return copy(v), zero(eltype(v)) + w = A[alg_idx, alg_idx]' \ v[alg_idx] + vterm = zero(v) + vterm[dif] .= v[dif] .- A[alg_idx, dif]' * w + return vterm, -dot(w, fa[alg_idx]) +end + +# One backward solve of the dual along `forced_sol` (whose problem has the forced +# right-hand side), with the events of the forward solve. `sensealg` is a Gauss adjoint, so +# the adjoint state is λ only. A quadrature callback of DiffEqCallbacks integrates -λᵀ r on +# each adjoint step: Gauss–Legendre for a GaussAdjoint, Gauss–Kronrod for a +# GaussKronrodAdjoint. The adjoint steps stop at each knot, so the integrand is smooth on +# each step and the quadrature is accurate relative to the integral. The callbacks act in +# the order of SciMLSensitivity's Gauss adjoints: the quadrature reads each step before the +# terminal cost and the bridge change the state. +# +# At each knot before the terminal time, the bridge records z and skips one ulp. The next +# step then reads the forward state and P on its own side, and an FSAL solver does not reuse +# the evaluation at the knot. At the end b of each event window, the bridge records the pair +# term, applies z_d ← Sᵀ z_d, and skips to just before a, so the dual never enters the +# window. If the new time is also a stop, the same call handles it. For a DAE, the callback +# initialization makes z_a consistent. +function _bridged_adjoint( + forced_sol, sensealg, adjoint_alg, vterm, terminal_time; + M, dif, alg_idx, knots, jumps, windows, abstol, reltol, reinit_abstol, defect + ) + n = length(vterm) + isdae = !isempty(alg_idx) + T = eltype(vterm) + terminal! = (out, u, p, t, i) -> (copyto!(out, vterm); nothing) + # The integrand uses the exact r = ∂F/∂ε, with the sign of SciMLSensitivity's adjoint + # convention. A VJP through F (SciMLSensitivity's `GaussIntegrand`) is a finite + # difference in ε for `autojacvec = false`, which loses the small r against f(u). + quad_values = DiffEqCallbacks.IntegrandValuesSum(zeros(T, 1)) + quad_f = (out, u, t, integrator) -> (out[1] = -dot(u, defect(t)); nothing) + # The Gauss–Kronrod callback keeps its default absolute subdivision tolerance (1e-7). + # Below it, it is a fixed Kronrod rule, which the validation catalog shows to be + # sufficient; a tolerance near the roundoff of the integrand would never stop. + quad = sensealg isa SciMLSensitivity.GaussAdjoint ? + DiffEqCallbacks.IntegratingSumCallback(quad_f, quad_values, zeros(T, 1); integrand_inplace = true) : + DiffEqCallbacks.IntegratingGKSumCallback(quad_f, quad_values, zeros(T, 1); integrand_inplace = true) + # The Gauss method of `ODEAdjointProblem` needs a `GaussIntegrand`, but only uses it for + # checkpointing. `autojacvec = false` builds it without a ReverseDiff tape of f. + integrand = SciMLSensitivity.GaussIntegrand( + forced_sol, SciMLSensitivity.GaussAdjoint(autojacvec = false), forced_sol.t, nothing + ) + aprob, cost, _ = SciMLSensitivity.ODEAdjointProblem( + forced_sol, sensealg, adjoint_alg, integrand, quad, [terminal_time], terminal!, + nothing, nothing, nothing, nothing, Val(true); abstol, reltol + ) + # The windows are sorted and do not overlap, so a binary search finds the window of a + # time. A linear search costs O(N²) for a callback that acts at each step. + starts = [w.a for w in windows] + inwindow(t) = (k = searchsortedlast(starts, t); k > 0 && t <= windows[k].b) + outer_knots = filter(t -> t < terminal_time && !inwindow(t), knots) + active = filter(w -> w.b < terminal_time, windows) + ends = [w.b for w in active] + stops = sort(unique([outer_knots; ends])) + z_at_knots = Dict{eltype(knots), typeof(vterm)}() + pair_terms = T[] + # Handle the stop `t` (knot record and bridges); return the time to skip to, or nothing. + cross! = function (integrator, t) + target = nothing + if insorted(t, outer_knots) + z_at_knots[t] = integrator.u[1:n] + target = prevfloat(t) + end + for k in searchsorted(ends, t) + w = active[k] + zb = integrator.u[dif] + za = w.St(zb) + push!(pair_terms, dot(zb, w.eb[dif]) - dot(za, w.ea[dif])) + integrator.u[dif] .= za + target = prevfloat(w.a) + end + return target + end + affect! = function (integrator) + target = cross!(integrator, integrator.t) + while target !== nothing && insorted(target, stops) + target = cross!(integrator, target) + end + target === nothing || _skip_to!(integrator, target) + return nothing + end + bridge = SciMLBase.DiscreteCallback( + (u, t, integrator) -> insorted(t, stops), affect!; + save_positions = (false, false), + initializealg = isdae ? GlobalDiffEq.BrownFullBasicInit(abstol = reinit_abstol) : SciMLBase.NoInit() + ) + asol = SciMLBase.solve( + aprob, adjoint_alg; callback = SciMLBase.CallbackSet(quad, cost, bridge), tstops = stops, + save_everystep = false, save_start = false, save_end = true, abstol, reltol + ) + SciMLBase.successful_retcode(asol) || throw( + ErrorException("the adjoint solve failed with retcode $(asol.retcode) at t = $(asol.t[end])") + ) + integral = quad_values.integrand[1] + knot_terms = [-dot(z_at_knots[t][dif], (M * J)[dif]) for (t, J) in zip(knots, jumps) if t in keys(z_at_knots)] + return (; integral, knot_terms, pair_terms, z0 = asol.u[end][1:n]) +end + +# The GlobalAdjoint projection of the error at the saved entry `terminal_index` of the +# recorded forward solve `data`. The dual is linearized at P. Each forward event is a window +# of zero width with its saltation matrix, so it has no pair term. An event time is saved +# twice; for the second entry (after the event), the dual crosses the event first. function GlobalDiffEq._adjoint_defect_projection( - sol, sensealg, adjoint_alg, direction; - abstol, reltol, terminal_time = sol.prob.tspan[2] + data, sensealg, adjoint_alg, direction; + abstol, reltol, terminal_index = lastindex(data.sol.t) + ) + (; prob, sol, knots, jumps, windows, dif, alg_idx, params) = data + terminal_time = sol.t[terminal_index] + sensealg === nothing || + sensealg isa SciMLSensitivity.InterpolatingAdjoint || + sensealg isa GaussAdjoints || + return _plain_adjoint_projection(data, sensealg, adjoint_alg, direction; abstol, reltol, terminal_time) + # The default is a ReverseDiff VJP, with a numerical VJP if ReverseDiff cannot run the + # model. `adjoint_sensitivities` makes the same choice for an out-of-place problem with + # callbacks. + automatic = sensealg === nothing + sensealg = something(sensealg, SciMLSensitivity.GaussKronrodAdjoint(autojacvec = SciMLSensitivity.ReverseDiffVJP())) + # The dual must follow the dense output P. A checkpointed adjoint solves the forward + # problem again between checkpoints and so follows another curve. + sensealg.checkpointing && throw( + ArgumentError( + "GlobalAdjoint does not support an adjoint with `checkpointing = true`: the " * + "adjoint must follow the stored forward solution" + ) + ) + sensealg isa SciMLSensitivity.InterpolatingAdjoint && (sensealg = _as_gauss_kronrod(sensealg)) + f = SciMLBase.unwrapped_f(prob.f) + iip = SciMLBase.isinplace(prob) + # `_semiexplicit_components` allows only the identity as a UniformScaling mass matrix, + # and SciMLSensitivity recognizes the identity only as the default `I`. + M = prob.f.mass_matrix isa LinearAlgebra.UniformScaling ? LinearAlgebra.I : prob.f.mass_matrix + T = sol.t[end] + # SciMLSensitivity reads the forward solution from the right, so r and the parameters + # (an affect can change them) do too. At T there is no right limit: the derivative of a + # Rosenbrock interpolant is Inf there. + side(t) = t < T ? :right : :left + p_at(t, s) = GlobalDiffEq._parameters_at(params, t, s) + rhs(x, t, s = side(t)) = GlobalDiffEq._rhs(f, iip, x, p_at(t, s), t) + r(t) = rhs(sol(t; continuity = side(t)), t) .- M * sol(t, Val{1}; continuity = side(t)) + F = (u, εp, t) -> rhs(u, t) .+ εp[1] .* r(t) + forced = SciMLBase.ODEProblem( + SciMLBase.ODEFunction{false}(F; mass_matrix = M), sol.prob.u0, sol.prob.tspan, + [zero(eltype(sol.prob.u0))] ) - prob = sol.prob + # Work around a SciMLSensitivity bug: the finite-difference Jacobian fails for an + # out-of-place DAE (`autojacvec = false, autodiff = false` errors in `default_relstep`). + # `autodiff` is not public, so every DAE with `autojacvec = false` uses an in-place view + # of F; with `autodiff = true` the view gives the same result. + F! = (du, u, εp, t) -> (du .= F(u, εp, t); nothing) + forced_inplace = isempty(alg_idx) ? nothing : SciMLBase.ODEProblem( + SciMLBase.ODEFunction{true}(F!; mass_matrix = M), forced.u0, forced.tspan, forced.p + ) + # `before_entry` holds the event calls at the terminal time that act before the saved + # entry. With none, the entry is the state on the left side. Otherwise the dual first + # crosses these calls in reverse order, with their pair terms, as the bridge does. The + # entry is then P(t⁺) up to the saved state, or the saved state between two calls. + before_entry = GlobalDiffEq._calls_before_entry(data, terminal_index) + after_event = !isempty(before_entry) + between = after_event && length(before_entry) < count(w -> w.b == terminal_time, data.calls) + terminal_side = after_event ? :right : :left + PT = between ? sol.u[terminal_index] : sol(terminal_time; continuity = terminal_side) + A = isempty(alg_idx) ? nothing : + GlobalDiffEq._rhs_jacobian(f, iip, PT, p_at(terminal_time, terminal_side), terminal_time, data.verbose) + vterm, endpoint_term = _terminal_value(direction, A, rhs(PT, terminal_time, terminal_side), dif, alg_idx) + # The identity gives the error against P(t∓), and the saved state can differ from it + # (for example, the dense output of Rosenbrock32 does not end at the step's state). + shift = dot(direction, PT .- sol.u[terminal_index]) + for w in reverse(before_entry) + bridge = GlobalDiffEq._entry_bridge(w, terminal_index) + za = bridge.St(vterm[dif]) + endpoint_term += dot(vterm[dif], bridge.eb[dif]) - dot(za, w.ea[dif]) + vterm[dif] .= za + end + # The dual starts one ulp before an interior terminal time, so that its first step reads + # the forward solution and the parameters before the knot (as at the other knots). + start = terminal_time < T ? prevfloat(terminal_time) : terminal_time + solve_with(problem, sensealg) = _bridged_adjoint( + (@set sol.prob = problem), sensealg, adjoint_alg, vterm, start; + M, dif, alg_idx, knots, jumps, windows, abstol, reltol, reinit_abstol = abstol, + defect = r, + ) + # One call per problem type, so that each adjoint solve has a concrete type. + solve_terms(sensealg) = !isempty(alg_idx) && sensealg.autojacvec === false ? + solve_with(forced_inplace, sensealg) : solve_with(forced, sensealg) + # The term z(t₀)ᵀ M e(t₀) is nonzero if a callback initialization changes the state at + # t₀ without a save, so that the dense output starts at another state. + e0 = data.e0 + # A new binding: the closure would box `endpoint_term`, which is reassigned. + fixed_terms = endpoint_term + shift + total(terms) = terms.integral + sum(terms.knot_terms; init = zero(terms.integral)) + + sum(terms.pair_terms; init = zero(terms.integral)) + fixed_terms + + dot(terms.z0[dif], (M * e0)[dif]) + return try + total(solve_terms(sensealg)) + catch err + (automatic && !(err isa InterruptException)) || rethrow() + @SciMLMessage( + "GlobalAdjoint: the adjoint solve with a ReverseDiff VJP failed; it uses a " * + "numerical VJP (pass a `sensealg` to choose)", + data.verbose, :global_error_ad_fallback, maxlog = 1 + ) + numerical = SciMLSensitivity.GaussKronrodAdjoint(autojacvec = false, autodiff = false) + # If the numerical VJP fails too, the first error is the likely cause (for example a + # missing package for the DAE reinitialization). `rethrow()` throws it with its own + # backtrace, and the second error goes to a warning. + fallback = try + total(solve_terms(numerical)) + catch second + second isa InterruptException && rethrow() + @SciMLMessage( + "GlobalAdjoint: the adjoint solve with a numerical VJP failed too; the error " * + "of the ReverseDiff VJP follows", + data.verbose, :global_error_ad_fallback, exception = (second, catch_backtrace()) + ) + nothing + end + fallback === nothing && rethrow() + fallback + end +end + +# The dual of an InterpolatingAdjoint is the dual of the Gauss adjoints; only its own +# parameter integral differs, and GlobalAdjoint does not use it. So it maps to the +# GaussKronrodAdjoint with the same settings, which has no such state component. +_as_gauss_kronrod(s::SciMLSensitivity.InterpolatingAdjoint{CS, AD, FDT}) where {CS, AD, FDT} = + SciMLSensitivity.GaussKronrodAdjoint(; + chunk_size = CS, autodiff = AD, diff_type = FDT, autojacvec = s.autojacvec, + checkpointing = s.checkpointing +) + +GlobalDiffEq._gauss_rule_order(::SciMLSensitivity.GaussAdjoint, adjoint_alg) = + SciMLBase.alg_order(adjoint_alg) + +GlobalDiffEq._supports_events(::SciMLSensitivity.InterpolatingAdjoint) = true +GlobalDiffEq._supports_events(::GaussAdjoints) = true + +# Any other adjoint method uses SciMLSensitivity's `adjoint_sensitivities`, without events +# or mass matrices. It has no knot terms, so it is accurate only for a dense output that is +# continuous at the step ends. `_recorded_forward_solve` rejects events and DAEs for it; a +# call that changes nothing, such as a SavingCallback, is no event. The fixed-grid solution +# of StepGridRefinement has no events but can be a DAE, so the check below rejects it. +function _plain_adjoint_projection(data, sensealg, adjoint_alg, direction; abstol, reltol, terminal_time) + (; prob, sol, alg_idx, params) = data + isempty(alg_idx) || throw(GlobalDiffEq._unsupported_events_error()) f_orig = SciMLBase.unwrapped_f(prob.f) - realp = prob.p + # No call changed the parameters, so all values in the record are equal. + realp = first(params.values) inplace = SciMLBase.isinplace(prob) - forced_f = if inplace let f = f_orig, realp = realp, sol = sol function (du, u, forcing, t) @@ -39,28 +310,21 @@ function GlobalDiffEq._adjoint_defect_projection( end end end - forced_prob = SciMLBase.remake( - prob; + sol.prob; f = SciMLBase.ODEFunction{inplace}(forced_f), - p = [zero(eltype(prob.u0))] + p = [zero(eltype(sol.prob.u0))] ) forced_sol = @set sol.prob = forced_prob - terminal_gradient! = let direction = direction function (out, u, p, t, i) copyto!(out, direction) return nothing end end - # A `nothing` sensealg defers to `adjoint_sensitivities`' own default; any - # user-passed adjoint method is forwarded unchanged. - sensealg_kwargs = sensealg === nothing ? (;) : (; sensealg) _, defect_gradient = SciMLSensitivity.adjoint_sensitivities( forced_sol, adjoint_alg; - sensealg_kwargs..., - t = [terminal_time], dgdu_discrete = terminal_gradient!, - abstol, reltol + sensealg, t = [terminal_time], dgdu_discrete = terminal_gradient!, abstol, reltol ) return only(defect_gradient) end diff --git a/lib/GlobalDiffEq/ext/GlobalDiffEqSimpleNonlinearSolveExt.jl b/lib/GlobalDiffEq/ext/GlobalDiffEqSimpleNonlinearSolveExt.jl new file mode 100644 index 00000000000..4061dd041c8 --- /dev/null +++ b/lib/GlobalDiffEq/ext/GlobalDiffEqSimpleNonlinearSolveExt.jl @@ -0,0 +1,9 @@ +module GlobalDiffEqSimpleNonlinearSolveExt + +using SimpleNonlinearSolve: SimpleNewtonRaphson + +# The solver of the algebraic projection in the DAE saltation products +# (`GlobalDiffEq._algebraic_solver`). +algebraic_solver() = SimpleNewtonRaphson() + +end diff --git a/lib/GlobalDiffEq/src/GlobalDiffEq.jl b/lib/GlobalDiffEq/src/GlobalDiffEq.jl index ce27de25f68..5f0b08f2d63 100644 --- a/lib/GlobalDiffEq/src/GlobalDiffEq.jl +++ b/lib/GlobalDiffEq/src/GlobalDiffEq.jl @@ -5,13 +5,14 @@ using Reexport: @reexport import ADTypes, DifferentiationInterface, ForwardDiff, LinearAlgebra, OrdinaryDiffEqCore, OrdinaryDiffEqTsit5, Random, RecursiveArrayTools, - Richardson, SciMLBase + Richardson, SciMLBase, SymbolicIndexingInterface import DiffEqBase: initialize!, calculate_residuals, calculate_residuals! import OrdinaryDiffEqCore: perform_step!, @cache -import Accessors: @set +import Accessors: @set, setproperties using FastBroadcast: FastBroadcast, @.. using MuladdMacro: MuladdMacro, @muladd using PrecompileTools: @setup_workload, @compile_workload +using SciMLLogging: AbstractVerbosityPreset, @SciMLMessage abstract type GlobalDiffEqAlgorithm <: SciMLBase.AbstractODEAlgorithm end @@ -24,9 +25,20 @@ function _is_empty_callback(cb::SciMLBase.CallbackSet) end _is_empty_callback(cb) = false +# The `DEVerbosity` of a solve from its `verbose` keyword, as in DiffEqBase. A SciMLLogging +# preset maps to its `DEVerbosity`. GlobalDiffEq messages use the `global_error` group. +_verbosity(kwargs) = _verbosity_spec(get(kwargs, :verbose, DEVerbosity())) +_verbosity_spec(verbose::DEVerbosity) = verbose +_verbosity_spec(verbose::AbstractVerbosityPreset) = DEVerbosity(verbose) +_verbosity_spec(verbose) = throw( + ArgumentError("`verbose` must be a `DEVerbosity` or a SciMLLogging preset, not $(typeof(verbose))") +) + include("richardson.jl") include("companion.jl") +include("defect_correction.jl") include("estimation.jl") +include("adjoint_events.jl") include("adjoint.jl") include("glee/tableaus.jl") include("glee/algorithms.jl") diff --git a/lib/GlobalDiffEq/src/adjoint.jl b/lib/GlobalDiffEq/src/adjoint.jl index a434e6fa6af..285a50cae5e 100644 --- a/lib/GlobalDiffEq/src/adjoint.jl +++ b/lib/GlobalDiffEq/src/adjoint.jl @@ -70,16 +70,18 @@ small-sample estimate in Cao and Petzold (2004). The estimate is a dual-weighted residual: for a projection direction `v`, the error component `⟨v, y(T) - P(T)⟩` equals `∫ λ(t)ᵀ (f(P(t)) - P'(t)) dt`, where `P` is the dense numerical solution, `d(t) = f(P) - P'` its defect, and `λ` the -adjoint with terminal condition `λ(T) = v`. This is computed by the adjoint -system itself (via `SciMLSensitivity.adjoint_sensitivities`), not by a -hand-rolled quadrature: the defect is introduced as a scalar forcing whose -sensitivity is exactly that integral. +adjoint with terminal condition `λ(T) = v`. One backward adjoint solve of +SciMLSensitivity computes it: the defect enters the problem as a scalar forcing +`ε d(t)`, and the sensitivity to `ε` is that integral. When solved with a `gtol`, the returned solution carries the estimate in its `global_error` field (`SciMLBase.has_global_error` is `true` for `GlobalAdjoint`). `scope` ([`GlobalAdjointScope`](@ref)) selects what is estimated: [`EndpointError`](@ref) (default) fills only `sol.global_error[end]`, while -[`TrajectoryError`](@ref) fills `sol.global_error[i]` at every saved time. +[`TrajectoryError`](@ref) fills `sol.global_error[i]` at every saved time. The +`TrajectoryError` solution is dense and saved at each step. An event time is saved twice, +with the error before and after the event. The state after each event that changes the +state is saved even if the callback's `save_positions` does not save it. `control` ([`GlobalAdjointControl`](@ref)) selects how `gtol` is met: [`ToleranceRefinement`](@ref) (default) tightens tolerances and re-solves adaptively, while [`StepGridRefinement`](@ref) re-solves non-adaptively on the @@ -92,14 +94,81 @@ controls each local-tolerance reduction. The `adjoint_abstol` and The adjoint machinery lives in a package extension: SciMLSensitivity must be loaded to solve with `GlobalAdjoint`. `sensealg` selects the adjoint sensitivity -algorithm and is passed straight through to `SciMLSensitivity.adjoint_sensitivities`: -any adjoint method works, and the default (`nothing`) defers to that function's -own default. `adjoint_alg` -selects the solver for the reverse-time adjoint problems. - -This implementation supports forward-time, standard-mass-matrix ODEs with real -vector states. The wrapped solver must provide a dense first-derivative -interpolation. +algorithm. The default (`nothing`) is `GaussKronrodAdjoint(autojacvec = ReverseDiffVJP())`: +one backward solve of the dual with the jump terms of the knots and the events, and a +Gauss–Kronrod quadrature of the error integral on each adjoint step. If ReverseDiff +cannot run the model, it uses a numerical VJP with a warning, as `adjoint_sensitivities` +does. The `sensealg` also selects the quadrature: + + - `GaussKronrodAdjoint(...)`: Gauss–Kronrod on each adjoint step (DiffEqCallbacks' + `IntegratingGKSumCallback`). + - `GaussAdjoint(...)`: Gauss–Legendre on each adjoint step (`IntegratingSumCallback`), a + little cheaper. The rule has `(p + 1) ÷ 2` points for `p = alg_order(adjoint_alg)` and + no error control, so its accuracy depends on the adjoint solver. Use an adjoint solver + of a fixed order of 5 or higher. With a lower order the estimate is less accurate: with + Rosenbrock23 (the midpoint rule), it can be wrong by much more than the tolerance. A + variable-order solver such as FBDF reports order 1, so it also gets the midpoint rule. + The solve warns about an adjoint solver of an order below 5. + - `InterpolatingAdjoint(...)`: the same as `GaussKronrodAdjoint` with the same settings. + Its dual is the same, and its own parameter integral is not used. + +The adjoint steps stop at each step end of the forward solve, so the integrand is smooth on +each adjoint step and the fixed quadrature rules are accurate relative to the integral. +Other adjoint methods go to `SciMLSensitivity.adjoint_sensitivities`, without jump terms, +events or DAEs. `adjoint_alg` selects the solver for the reverse-time adjoint problems. + +The identity also has a term `-⟨λ(tⱼ), M [P]ⱼ⟩` for each jump of the dense output at a +step end. So solvers with a dense output that is not continuous between steps (for +example Rosenbrock32) are supported. + +## Events and DAEs + +Events (`ContinuousCallback`, `VectorContinuousCallback`, `DiscreteCallback`) and +semi-explicit mass-matrix DAEs (a diagonal mass matrix of zeros and ones) are supported. +At each event, the adjoint jumps with the saltation matrix `S` of the event, `λ ← Sᵀ λ`. +`S` is the linearized map of an error across the event, with the shift of the event time. + +As in SciMLSensitivity's callback adjoints, the `autojacvec` of the `sensealg` selects how +`Sᵀλ` is computed. With a VJP choice (the default `ReverseDiffVJP()`), ReverseDiff and +ForwardDiff products through the affect and the condition give `Sᵀλ` without `S`. The cost +per event then does not grow with the number of states `n`. An event where the affect or +the condition is not differentiable uses finite differences, with a warning. For a DAE, +the products also go through the algebraic projection after each affect, as a nonlinear +solve that AD differentiates with the implicit function theorem. This needs +SimpleNonlinearSolve, which the DAE initialization of OrdinaryDiffEqNonlinearSolve loads; +without it, a DAE uses finite differences. With `autojacvec = false`, finite differences +of the callbacks give `S`: about `n` runs of the affect and the condition per event, and an +`n × n` matrix. + +An affect can change the parameters; the adjoint uses the forward parameters on each +interval between events. As in SciMLSensitivity, an affect must be a function of the +state, the parameters and the time, and it can run more than once. The trial runs use one +copy of the callbacks for the whole solve. An affect that depends on state kept in the +callback (for example a counter) gives an `ArgumentError`; [`GlobalErrorEstimation`](@ref) +supports such affects. + +Like the rest of the estimate, this is a linearization at the forward solution. It assumes +that the true solution has the same sequence of events, so it does not see an event that +the forward solve misses or adds. For a DAE, only the differential components of the error +enter the identity. The constraint at the final time moves an algebraic component of a +projection direction to the differential ones. The adjoint problem has the exact +Jacobian, so any implicit `adjoint_alg` works for a DAE. + +Events, DAEs and the jump terms need a `GaussKronrodAdjoint` (the default), `GaussAdjoint` +or `InterpolatingAdjoint` `sensealg` without `checkpointing`, because the adjoint must +follow the stored forward solution. +`StepGridRefinement` does not support callbacks. + +This implementation supports forward-time problems with real vector states. The wrapped +solver must provide a dense first-derivative interpolation. + +Warnings follow the `verbose` keyword of `solve` (a [`DEVerbosity`](@ref)). The toggle +`global_error_ad_fallback` controls the message when ReverseDiff or ForwardDiff cannot run +the model or a callback, and finite differences or a numerical VJP replace it. The toggle +`global_error_grazing_event` controls the message at an event where the vector field is +tangent to the event surface; the saltation matrix then has no time-shift term. The toggle +`global_error_companion_order` controls the message about a `GaussAdjoint` with an adjoint +solver of an order below 5. ## References @@ -173,9 +242,50 @@ function _resolve_sensealg(alg::GlobalAdjoint) return alg.sensealg end +# The events follow the VJP choice of the adjoint method, as in SciMLSensitivity's callback +# adjoints: `autojacvec = false` gives the finite-difference saltation matrix, and any other +# choice a ReverseDiff VJP (see `_saltation_vjp`). +function _event_vjp(alg::GlobalAdjoint) + sensealg = alg.sensealg + sensealg === nothing && return true + return !hasproperty(sensealg, :autojacvec) || sensealg.autojacvec !== false +end + +# Only the default, GaussAdjoint, GaussKronrodAdjoint and InterpolatingAdjoint (methods in +# the extension) support DAEs and events that change the state. The others use +# `adjoint_sensitivities` without event terms. +_supports_events(sensealg) = sensealg === nothing + +# The order `p` that sets the Gauss rule of the error integral, or `nothing` for a rule with +# error control. The extension adds a method for GaussAdjoint. +_gauss_rule_order(sensealg, adjoint_alg) = nothing + +# GaussAdjoint integrates the error with DiffEqCallbacks' `IntegratingSumCallback`, which +# uses (p + 1) ÷ 2 Gauss points for p = `alg_order(adjoint_alg)` and has no error control. +# The integrand has much cancellation inside a step, so below p = 5 the estimate can be much +# less accurate: with Rosenbrock23 (the midpoint rule), it was wrong by up to 100 %. A +# variable-order solver (FBDF, QNDF) reports p = 1, so it gets the midpoint rule too. +function _warn_if_low_order_quadrature(alg::GlobalAdjoint, kwargs) + order = _gauss_rule_order(alg.sensealg, alg.adjoint_alg) + (order === nothing || order >= 5) && return nothing + @SciMLMessage( + "GlobalAdjoint: GaussAdjoint integrates the error with (p + 1) ÷ 2 Gauss points for " * + "p = alg_order(adjoint_alg), and $(nameof(typeof(alg.adjoint_alg))) has order " * + "$order (a variable-order solver reports its lowest order), which can give a poor " * + "estimate. Use GaussKronrodAdjoint (the default) or an `adjoint_alg` of a fixed " * + "order of 5 or higher.", + _verbosity(kwargs), :global_error_companion_order + ) + return nothing +end + SciMLBase.allows_arbitrary_number_types(::GlobalAdjoint) = false SciMLBase.allowscomplex(::GlobalAdjoint) = false SciMLBase.isautodifferentiable(::GlobalAdjoint) = false +# As for GlobalErrorEstimation: without these traits, an implicit inner solver (needed for +# a DAE) finds no dual signature in the wrapper of `f`. +SciMLBase.forwarddiffs_model(alg::GlobalAdjoint) = SciMLBase.forwarddiffs_model(alg.alg) +SciMLBase.forwarddiff_chunksize(alg::GlobalAdjoint) = SciMLBase.forwarddiff_chunksize(alg.alg) SciMLBase.has_global_error(::GlobalAdjoint) = true function _validate_adjoint_problem(prob) @@ -184,8 +294,7 @@ function _validate_adjoint_problem(prob) isempty(prob.u0) && throw(ArgumentError("GlobalAdjoint requires a nonempty state")) prob.tspan[1] < prob.tspan[2] || throw(ArgumentError("GlobalAdjoint requires a forward-time ODEProblem")) - prob.f.mass_matrix == LinearAlgebra.I || - throw(ArgumentError("GlobalAdjoint currently requires the standard mass matrix")) + _semiexplicit_components(prob, "GlobalAdjoint") return nothing end @@ -238,33 +347,41 @@ function _sphere_projection_expectation(dimension, ::Type{T}) where {T} return expectation end +# `data` is the recorded forward solve (`_recorded_forward_solve`). `terminal_index` is the +# saved entry whose error is estimated. function _adjoint_error_estimate( - sol, alg, directions; - adjoint_abstol, adjoint_reltol, terminal_time = sol.prob.tspan[2] + data, alg, directions; + adjoint_abstol, adjoint_reltol, terminal_index = lastindex(data.sol.t) ) + sol = data.sol sensealg = _resolve_sensealg(alg) - projections = map(directions) do direction - _adjoint_defect_projection( - sol, sensealg, alg.adjoint_alg, direction; - abstol = adjoint_abstol, reltol = adjoint_reltol, terminal_time - ) - end T = eltype(sol.prob.u0) sample_count = length(directions) factor = _sphere_projection_expectation(sample_count, T) / _sphere_projection_expectation(length(sol.prob.u0), T) + projections = map(directions) do direction + _adjoint_defect_projection( + data, sensealg, alg.adjoint_alg, direction; + abstol = adjoint_abstol, reltol = adjoint_reltol, terminal_index + ) + end return factor * LinearAlgebra.norm(projections) end # Per-time global error along the trajectory: the estimate at each saved time, # obtained by placing the adjoint's discrete cost at that time. `sol.t[1]` (the -# initial time) carries no error. -function _adjoint_trajectory_errors(sol, alg, directions; adjoint_abstol, adjoint_reltol) +# initial time) carries no error. At an event time, the first entry has the error before +# the event and the second the error after it. +function _adjoint_trajectory_errors(data, alg, directions; adjoint_abstol, adjoint_reltol) + sol = data.sol errors = zeros(eltype(sol.prob.u0), length(sol.t)) for j in 2:length(sol.t) + # The true solution starts at the saved state, even if a callback initialization + # changed it. + sol.t[j] == sol.t[1] && continue errors[j] = _adjoint_error_estimate( - sol, alg, directions; - adjoint_abstol, adjoint_reltol, terminal_time = sol.t[j] + data, alg, directions; + adjoint_abstol, adjoint_reltol, terminal_index = j ) end return errors @@ -323,11 +440,11 @@ function adjoint_error_estimate( ) _require_adjoint_ext() _validate_adjoint_problem(prob) - solve_kwargs = merge((; kwargs...), _DENSE_SOLVE_KWARGS) - sol = SciMLBase.solve(prob, alg.alg, args...; abstol, reltol, solve_kwargs...) - _validate_adjoint_solution(sol) + _warn_if_low_order_quadrature(alg, kwargs) + data = _recorded_forward_solve(prob, alg, args...; abstol, reltol, kwargs...) + _validate_adjoint_solution(data.sol) directions = _orthogonal_directions(prob.u0, alg.samples, alg.rng) - return _adjoint_error_estimate(sol, alg, directions; adjoint_abstol, adjoint_reltol) + return _adjoint_error_estimate(data, alg, directions; adjoint_abstol, adjoint_reltol) end # Endpoint-mode global error: a vector aligned with the saved times carrying the @@ -350,25 +467,31 @@ function SciMLBase.__solve( _require_adjoint_ext() _validate_tolerances(abstol, reltol, "local") _validate_adjoint_problem(prob) + _warn_if_low_order_quadrature(alg, kwargs) + alg.control === StepGridRefinement && _has_callbacks(kwargs) && throw( + ArgumentError("GlobalAdjoint does not support StepGridRefinement with callbacks") + ) + # As for GlobalErrorEstimation: the solve pipeline has already merged the problem's + # callback into `kwargs`, so remove it from the problem. + prob = @set prob.kwargs = _without_callback(prob.kwargs) gtol = alg.gtol options = alg.options directions = _orthogonal_directions(prob.u0, alg.samples, alg.rng) local_abstol = abstol local_reltol = reltol - trial_kwargs = merge((; kwargs...), _DENSE_SOLVE_KWARGS) last_estimate = oftype(float(gtol), Inf) for _ in 1:options.maxiters - trial_sol = SciMLBase.solve( - prob, alg.alg, args...; - abstol = local_abstol, reltol = local_reltol, trial_kwargs... + trial = _recorded_forward_solve( + prob, alg, args...; + abstol = local_abstol, reltol = local_reltol, _with_copied_callback(kwargs)... ) + trial_sol = trial.sol _validate_adjoint_solution(trial_sol) last_estimate = _adjoint_error_estimate( - trial_sol, alg, directions; - adjoint_abstol = options.adjoint_abstol, - adjoint_reltol = options.adjoint_reltol + trial, alg, directions; + adjoint_abstol = options.adjoint_abstol, adjoint_reltol = options.adjoint_reltol ) isfinite(last_estimate) || throw(ErrorException("the adjoint global error estimate is not finite")) @@ -378,16 +501,17 @@ function SciMLBase.__solve( # (StepGridRefinement) or re-solves adaptively with the user's saving # options (ToleranceRefinement). TrajectoryError needs the dense, # every-step solution to estimate along the whole path. + final_data = nothing final_sol = if alg.control === StepGridRefinement _solve_on_grid( prob, alg.alg, trial_sol.t, args...; abstol = local_abstol, reltol = local_reltol ) elseif alg.scope === TrajectoryError - SciMLBase.solve( - prob, alg.alg, args...; - abstol = local_abstol, reltol = local_reltol, trial_kwargs... + final_data = _recorded_forward_solve( + prob, alg, args...; abstol = local_abstol, reltol = local_reltol, kwargs... ) + final_data.sol else SciMLBase.solve( prob, alg.alg, args...; @@ -395,8 +519,10 @@ function SciMLBase.__solve( ) end global_error = if alg.scope === TrajectoryError + # StepGridRefinement has no callbacks, so no recorded data. + data = final_data === nothing ? _unrecorded_data(prob, final_sol, _verbosity(kwargs)) : final_data _adjoint_trajectory_errors( - final_sol, alg, directions; + data, alg, directions; adjoint_abstol = options.adjoint_abstol, adjoint_reltol = options.adjoint_reltol ) diff --git a/lib/GlobalDiffEq/src/adjoint_events.jl b/lib/GlobalDiffEq/src/adjoint_events.jl new file mode 100644 index 00000000000..2eb363f423e --- /dev/null +++ b/lib/GlobalDiffEq/src/adjoint_events.jl @@ -0,0 +1,643 @@ +# Events and semi-explicit DAEs in GlobalAdjoint. At a forward event the dual jumps by +# z ← Sᵀz, with S the saltation matrix: the linearized map of an error from just before the +# event to just after it, with the shift of the event time. The bridge, the knot terms and +# the DAE parts of the adjoint solve are in the SciMLSensitivity extension. + +# The differential and algebraic indices of a semi-explicit DAE (a diagonal mass matrix of +# zeros and ones). Reject λI with λ ≠ 1: the error identity and the event terms assume +# M = I. +function _semiexplicit_components(prob, name) + M = prob.f.mass_matrix + n = length(prob.u0) + unsupported = ArgumentError( + "$name supports only semi-explicit DAEs: a diagonal mass matrix of zeros and ones" + ) + if M isa LinearAlgebra.UniformScaling + isone(M.λ) || throw(unsupported) + return collect(1:n), Int[] + end + m = Matrix(M) + (LinearAlgebra.isdiag(m) && all(x -> iszero(x) || isone(x), LinearAlgebra.diag(m))) || + throw(unsupported) + d = LinearAlgebra.diag(m) + return findall(isone, d), findall(iszero, d) +end + +_rhs(f, iip, u, p, t) = iip ? (du = similar(u); f(du, u, p, t); du) : f(u, p, t) +# Fall back to finite differences if ForwardDiff cannot run f, as the default sensealg falls +# back to a numerical VJP. If f itself throws, `_fd_jacobian` throws the error again. +function _rhs_jacobian(f, iip, u, p, t, verbose) + g = x -> _rhs(f, iip, x, p, t) + return try + DifferentiationInterface.jacobian(g, ADTypes.AutoForwardDiff(), u) + catch err + err isa InterruptException && rethrow() + @SciMLMessage( + "GlobalAdjoint: ForwardDiff cannot differentiate the right-hand side; the " * + "Jacobian of the DAE at the final time uses finite differences", + verbose, :global_error_ad_fallback, exception = err, maxlog = 1 + ) + _fd_jacobian(g, u) + end +end + +# The forward events grouped into calls (t, labels). `marks[k]` is (t, number of events +# before call k). +function _forward_calls(marks, events) + return map(eachindex(marks)) do k + t, start = marks[k] + stop = k < length(marks) ? marks[k + 1][2] : length(events) + (t, Tuple(events[(start + 1):stop])) + end +end + +# The parent integrator of the trial runs: the options and time axis of the companion +# integrator, but no side effects (a PresetTimeCallback would add a tstop at each run). +struct _FrozenParent{O, T, E} + opts::O + tdir::T + dt::T + tprev::T + iter::Int + e::E +end +(parent::_FrozenParent)(t) = parent.e +SciMLBase.add_tstop!(::_FrozenParent, t) = nothing +DiffEqBase.get_tstops_max(parent::_FrozenParent) = typemax(parent.dt) +function _frozen_parent(integrator, e = copy(integrator.u)) + return _FrozenParent( + integrator.opts, oftype(integrator.t, integrator.tdir), integrator.dt, + integrator.tprev, integrator.iter, e + ) +end + +# Newton for f_a(u) = 0 with a finite-difference Jacobian. Only the saltation matrix uses +# it, and it needs no high accuracy. +function _project_algebraic(fu, u, alg_idx) + isempty(alg_idx) && return u + v = copy(u) + with_algebraic = x -> (w = copy(v); w[alg_idx] .= x; w) + for _ in 1:20 + ra = fu(v)[alg_idx] + LinearAlgebra.norm(ra) <= 1.0e-13 * (1 + LinearAlgebra.norm(v)) && break + J = _fd_jacobian(x -> fu(with_algebraic(x))[alg_idx], v[alg_idx], ra) + v[alg_idx] .-= J \ ra + end + return v +end + +# `g0` is g(x), if the caller has it. +function _fd_jacobian(g, x, g0 = g(x)) + J = similar(x, length(g0), length(x)) + for j in eachindex(x) + h = sqrt(eps(eltype(x))) * max(1, abs(x[j])) + w = copy(x) + w[j] += h + J[:, j] .= (g(w) .- g0) ./ h + end + return J +end + +# Run the affect of one call on copies of u and p. As in SciMLSensitivity, the affect is a +# function of u, p and t, so one trial copy of the callback serves all calls. For a +# VectorContinuousCallback, `args` holds the fired components and their signs; without it, +# `_fired_mask` builds them from `labels`. +function _trial_affect(callback, labels, u, t, parent, p, sol, P; args = nothing) + # A new copy for each run, so that all finite differences of one saltation matrix start + # from the same callback state. The trial callback never advances, so the copy is small. + callback = deepcopy(callback) + integrator = _TrueStateIntegrator(copy(u), deepcopy(p), t, parent, sol, P) + _, kind = first(labels) + if callback isa SciMLBase.ContinuousCallback + (kind == 1 ? callback.affect! : callback.affect_neg!)(integrator) + elseif callback isa SciMLBase.VectorContinuousCallback + callback.affect!(integrator, args === nothing ? _fired_mask(callback, labels) : copy(only(args))) + else + callback.affect!(integrator) + end + return integrator.u, integrator.p +end + +function _fired_mask(callback, labels) + mask = zeros(Int8, callback.len) + for (_, component) in labels + mask[component] = 1 + end + return mask +end + +function _trial_condition(callback, labels, u, t, parent, p, sol, P) + integrator = _TrueStateIntegrator(copy(u), p, t, parent, sol, P) + if callback isa SciMLBase.VectorContinuousCallback + out = similar(u, callback.len) + callback.condition(out, _true_state_view(u, callback.idxs), t, integrator) + return out[last(first(labels))] + end + return callback.condition(_true_state_view(u, callback.idxs), t, integrator) +end + +# The affects of a call and of its `tied` calls, in order, as a map (u, q) ↦ (u⁺, q⁺). +# `reinit(u, q)` runs after each affect (the reinitialization of a DAE). +function _tied_affects(callback, labels, t, parent, args, tied, sol, P, reinit) + return function (u, q) + u, q = _trial_affect(callback, labels, u, t, parent, q, sol, P; args) + u = reinit(u, q) + for d in tied + u, q = _trial_affect(d.callback, d.labels, u, t, d.parent, q, sol, P; d.args) + u = reinit(u, q) + end + return u, q + end +end + +# The saltation matrix of a forward event on the differential components, +# S = A + (φ⁺ - A φ⁻) ∇γᵀ / (∇γ ⋅ φ⁻ + γ_t), +# with A the Jacobian of the affects (and the DAE reinitialization), γ the event condition +# and φ the vector field of the reduced ODE x' = f_d(x, z(x)). Also the offset +# c = (φ⁺ - A φ⁻) γ₀ / (∇γ ⋅ φ⁻ + γ_t), so that e⁺ = S e⁻ + c. Here γ₀ = γ(`u_event`) can be +# nonzero: the forward root finding reads an interpolant, which is not consistent for a +# condition on an algebraic variable. A DiscreteCallback does not move its time, so S = A +# and c = 0. +# A, ∇γ, γ_t and φ⁻ use the state before the call: P(t⁻), or `u_before` for a later call at +# the same time. φ⁺ uses the state `u_after` (default P(t⁺)) and the parameters `p_after`. +function _event_saltation( + callback, labels, t, p, parent, P, sol, prob, dif, alg_idx; + u_event, p_after, u_after = nothing, u_before = nothing, args = nothing, tied = (), + verbose = DEVerbosity() + ) + f = SciMLBase.unwrapped_f(prob.f) + iip = SciMLBase.isinplace(prob) + p = deepcopy(p) + rhs_with = q -> (u -> _rhs(f, iip, u, q, t)) + fu = rhs_with(p) + Pm = u_before === nothing ? sol(t; continuity = :left) : u_before + Pp = u_after === nothing ? sol(t; continuity = :right) : u_after + full = x -> (u = copy(Pm); u[dif] .= x; _project_algebraic(fu, u, alg_idx)) + xm = Pm[dif] + affect_all = _tied_affects( + callback, labels, t, parent, args, tied, sol, P, (u, q) -> _project_algebraic(rhs_with(q), u, alg_idx) + ) + affected = x -> first(affect_all(full(x), p))[dif] + A = _fd_jacobian(affected, xm) + callback isa SciMLBase.DiscreteCallback && return A, zero(xm) + γ = x -> _trial_condition(callback, labels, full(x), t, parent, p, sol, P) + ∇γ = vec(_fd_jacobian(x -> [γ(x)], xm)) + h = sqrt(eps(typeof(t))) * max(1, abs(t)) + γt = ( + _trial_condition(callback, labels, Pm, t + h, parent, p, sol, P) - + _trial_condition(callback, labels, Pm, t - h, parent, p, sol, P) + ) / (2h) + φm = fu(full(xm))[dif] + φp = rhs_with(p_after)(Pp)[dif] + jump = (φp .- A * φm) ./ (LinearAlgebra.dot(∇γ, φm) + γt) + S = A .+ jump * ∇γ' + # γ₀ at the forward event state, not at P(t⁻): the mismatch m = P(t⁻) - u_f has its own + # source term. + c = jump .* γ(u_event[dif]) + all(isfinite, S) && all(isfinite, c) || return (_grazing_message(t, verbose); (A, zero(xm))) + return S, c +end + +# At a grazing event ∇γ ⋅ φ⁻ + γ_t is zero, so the time-shift term is not finite and S +# falls back to A. +function _grazing_message(t, verbose) + @SciMLMessage( + "GlobalAdjoint: the event at t = $t is grazing (the vector field is tangent to the " * + "event surface), so its saltation matrix has no time-shift term and the estimate " * + "is less accurate there.", + verbose, :global_error_grazing_event + ) + return nothing +end + +# The two products with S that the dual needs: z ↦ Sᵀz for the jump of the dual, and +# x ↦ S x to carry the source of an earlier call at the same time through this call. +_saltation_maps(S) = (z -> S' * z, x -> S * x) + +# The solver and the adjoint method of the algebraic projection in `_saltation_vjp`. The +# solver comes from the SimpleNonlinearSolve extension; the DAE initialization +# (OrdinaryDiffEqNonlinearSolve) loads SimpleNonlinearSolve. Without it, a DAE uses the +# finite-difference saltation matrix. The adjoint method comes from the SciMLSensitivity +# extension: the residual writes into arrays (an in-place f), so it needs ReverseDiff, not +# the default Zygote VJP. +function _algebraic_solver() + solver = Base.get_extension(@__MODULE__, :GlobalDiffEqSimpleNonlinearSolveExt) + adjoint = Base.get_extension(@__MODULE__, :GlobalDiffEqSciMLSensitivityExt) + (solver === nothing || adjoint === nothing) && return nothing + return (; alg = solver.algebraic_solver(), sensealg = adjoint.projection_sensealg()) +end + +# The algebraic projection of a semi-explicit DAE: the state with the differential +# components x and the algebraic components z that solve f_a(x, z) = 0 with parameters q. +# The solve is a NonlinearProblem in z with x as its parameters, so AD through it uses the +# implicit function theorem (the rules of NonlinearSolveBase and SciMLSensitivity), +# dz/dx = -(∂f_a/∂z)⁻¹ ∂f_a/∂x, not the Newton iterations. Newton starts from the values of +# z0; z0 is only a start value, so AD does not go through it. +function _algebraic_projection(f, iip, t, dif, alg_idx, solver) + n = length(dif) + length(alg_idx) + function assemble(x, z) + u = Vector{promote_type(eltype(x), eltype(z))}(undef, n) + u[dif] .= x + u[alg_idx] .= z + return u + end + return function (x, z0, q) + residual = (z, x) -> _rhs(f, iip, assemble(x, z), q, t)[alg_idx] + nlsol = SciMLBase.solve( + SciMLBase.NonlinearProblem(residual, SciMLBase.value.(z0), x), solver.alg; + solver.sensealg, abstol = 1.0e-13, reltol = 1.0e-13 + ) + # Under ReverseDiff, the solve returns the tracked solution vector itself. + if nlsol isa SciMLBase.AbstractNonlinearSolution + SciMLBase.successful_retcode(nlsol) || + error("the algebraic projection did not converge ($(nlsol.retcode))") + return assemble(x, nlsol.u) + end + return assemble(x, nlsol) + end +end + +# The matrix-free form of `_event_saltation` (see `_event_vjp`). With +# j = (φ⁺ - A φ⁻) / (∇γ ⋅ φ⁻ + γ_t): +# Sᵀz = Aᵀz + ∇γ (j ⋅ z), S x = A x + j (∇γ ⋅ x), c = j γ₀. +# Aᵀz uses ReverseDiff (a tracked-real state lets an affect write into it); A x uses +# ForwardDiff. For a DAE, the maps act on the differential components, and the algebraic +# projection (`_algebraic_projection`) comes before the affects and after each of them, as +# in `_event_saltation`. The cost per event does not grow with the state size. Returns +# nothing if the AD fails on the user callback: the trial products below catch that here, +# not in the adjoint solve. +function _saltation_vjp( + callback, labels, t, p, parent, P, sol, prob, dif, alg_idx; + u_event, p_after, u_after = nothing, u_before = nothing, args = nothing, tied = (), + verbose = DEVerbosity() + ) + f = SciMLBase.unwrapped_f(prob.f) + iip = SciMLBase.isinplace(prob) + p = deepcopy(p) + Pm = u_before === nothing ? sol(t; continuity = :left) : u_before + Pp = u_after === nothing ? sol(t; continuity = :right) : u_after + if isempty(alg_idx) + reinit, full, restrict, x0 = (u, q) -> u, identity, identity, Pm + else + project = _algebraic_projection(f, iip, t, dif, alg_idx, _algebraic_solver()) + reinit = (u, q) -> project(u[dif], u[alg_idx], q) + full = x -> project(x, Pm[alg_idx], p) + restrict = u -> u[dif] + x0 = Pm[dif] + end + affect_all = _tied_affects(callback, labels, t, parent, args, tied, sol, P, reinit) + affect = x -> restrict(first(affect_all(full(x), p))) + condition = (u, s) -> _trial_condition(callback, labels, u, s, parent, p, sol, P) + Atz = z -> DifferentiationInterface.gradient( + x -> LinearAlgebra.dot(z, affect(collect(x))), ADTypes.AutoReverseDiff(), x0 + ) + Ax = x -> DifferentiationInterface.derivative( + h -> affect(x0 .+ h .* x), ADTypes.AutoForwardDiff(), zero(t) + ) + return try + Atz(one.(x0)) + Ax(one.(x0)) + callback isa SciMLBase.DiscreteCallback && return Atz, Ax, zero(x0) + φm = restrict(_rhs(f, iip, full(x0), p, t)) + φp = restrict(_rhs(f, iip, Pp, p_after, t)) + ∇γ = DifferentiationInterface.gradient( + x -> condition(full(collect(x)), t), ADTypes.AutoReverseDiff(), x0 + ) + h = sqrt(eps(typeof(t))) * max(1, abs(t)) + γt = (condition(Pm, t + h) - condition(Pm, t - h)) / (2h) + j = (φp .- Ax(φm)) ./ (LinearAlgebra.dot(∇γ, φm) + γt) + all(isfinite, j) || return (_grazing_message(t, verbose); (Atz, Ax, zero(x0))) + (z -> Atz(z) .+ ∇γ .* LinearAlgebra.dot(j, z)), + (x -> Ax(x) .+ j .* LinearAlgebra.dot(∇γ, x)), j .* condition(full(restrict(u_event)), t) + catch err + err isa InterruptException && rethrow() + nothing + end +end + +# Merge the zero-width windows of the calls at one event time. The calls act in order, so +# the bridge composes their saltation maps, the first call's e(a⁻) stays, and an earlier +# source goes through the later maps: e(b⁺) = S₂ e₁(b⁺) + e₂(b⁺) on `dif`. +function _merge_windows(windows, dif) + merged = Any[] + for w in sort(windows; by = w -> w.a) + if !isempty(merged) && w.a == merged[end].b + m = merged[end] + eb = copy(w.eb) + eb[dif] .+= w.Sx(m.eb[dif]) + St1, St2, Sx1, Sx2 = m.St, w.St, m.Sx, w.Sx + merged[end] = (; + a = m.a, b = w.b, ea = m.ea, eb, + St = z -> St1(St2(z)), Sx = x -> Sx2(Sx1(x)), + ) + else + push!(merged, w) + end + end + return merged +end + +# The forward solve of GlobalAdjoint with its event records. Each call is a zero-width +# window whose bridge is its saltation matrix. The trial runs use the snapshots before each +# affect and one copy of the callbacks, made after the initialization +# (`_check_trial_affect`). The affect acts on the integrator state u_f, which can differ +# from P(t_f⁻) if the event cuts the step short. With m = P(t_f⁻) - u_f and the offset c of +# `_event_saltation`, +# e⁺ = S (e⁻ + m) + c, +# so the pair term z⁺ᵀ (e(b⁺) - S e(a⁻)) with e(a⁻) = -m and e(b⁺) = c gives z⁺ᵀ (S m + c). +function _recorded_forward_solve(prob, alg, args...; abstol, reltol, kwargs...) + name = "GlobalAdjoint" + inner_alg = alg.alg + event_vjp = _event_vjp(alg) + supports_events = _supports_events(alg.sensealg) + verbose = _verbosity(kwargs) + dif, alg_idx = _semiexplicit_components(prob, name) + # Without event support (a sensealg other than the Gauss adjoints and + # InterpolatingAdjoint), fail before the solve for a DAE, and at the first call that + # changes u or p. A call that changes nothing (such as a SavingCallback) is not an + # event. + supports_events || isempty(alg_idx) || throw(_unsupported_events_error()) + callback_list = _callback_list(_user_callbacks(prob, kwargs)) + events = Tuple{Int, Int}[] + marks = Tuple{float(eltype(prob.tspan)), Int}[] + snapshots = Any[] + # The saved-state count before each call places the saved entries at an event time + # between the calls at that time (a callback can save before or after its affect). + saves = Int[] + # For each affect: the state after it, whether it changed u or p, and p after it if it + # did (for `_check_trial_affect`). + after_states = Any[] + # Whether an affect at the current time changed u or p (for `_save_after_events`). + changed_here = Ref(false) + after = function (integrator) + before_call = snapshots[end] + changed = integrator.u != before_call.u || !_approx_same(before_call.p, integrator.p) + p = changed ? deepcopy(integrator.p) : nothing + push!(after_states, (; u = copy(integrator.u), p, changed)) + changed && !supports_events && throw(_unsupported_events_error()) + changed_here[] |= changed + return nothing + end + before = function (integrator, label, args...) + push!(marks, (integrator.t, length(events))) + push!(saves, length(integrator.sol.t)) + push!( + snapshots, (; + p = deepcopy(integrator.p), parent = _frozen_parent(integrator, zero(integrator.u)), + u = copy(integrator.u), args = deepcopy(args), + ) + ) + return after + end + forward_prob, forward_callbacks = _forward_setup(prob, callback_list, events, before) + # `init` and `solve!`, so that the integrator holds the final parameters. + integrator = SciMLBase.init( + forward_prob, inner_alg, args...; + abstol, reltol, + callback = SciMLBase.CallbackSet(forward_callbacks, _save_after_events(changed_here)), + merge(_without_callback(kwargs), _DENSE_SOLVE_KWARGS)... + ) + # A change by an initialization goes into e0, not into an event window. + changed_here[] = false + # e0 is the state after the callback initialization minus the start of the dense + # output. It is zero unless an initialization changes the state without a save. + u_init = copy(integrator.u) + trial_callbacks = deepcopy(callback_list) + sol = SciMLBase.solve!(integrator) + _validate_estimation_solution(sol, name) + knots, jumps = _knot_jumps(sol) + P = _IntervalInterp(sol, [knots; sol.t[end]], 1) + e0 = u_init .- sol(sol.t[1]; continuity = :right) + calls = _forward_calls(marks, events) + params = _parameter_record(calls, snapshots, deepcopy(integrator.p)) + zero_e = zero(prob.u0) + # Calls at one time act in order: the state and parameters after a call are those before + # the next one, and only the first call has the cut-step mismatch. After the last call, + # the dense output starts at P(t⁺) (`_save_after_events`). A call that changes neither u + # nor p has S = I and no source to first order, so it needs no trial runs. + # A DiscreteCallback call right after a continuous call at the same time acts at the + # step end that the event makes in any nearby solve, so it moves with the event time. + # Hence the continuous call's window takes the affects of these tied calls too (one + # saltation matrix, with φ⁺ after all of them), and their own windows are the identity. + discrete(k) = _is_discrete(first(last(calls[k]))) + tied_to = zeros(Int, length(calls)) + last_tied = collect(eachindex(calls)) + for k in eachindex(calls) + discrete(k) && continue + j = k + while j < length(calls) && first(calls[j + 1]) == first(calls[k]) && discrete(j + 1) + j += 1 + tied_to[j] = k + end + last_tied[k] = j + end + windows = map(enumerate(calls)) do (k, (tf, labels)) + (; p, parent, args) = snapshots[k] + callback = trial_callbacks[first(first(labels))] + u_f = snapshots[k].u + # Δ of `_check_trial_affect`, or zero for an affect that changes nothing. + affect_error = after_states[k].changed ? + _check_trial_affect(callback, labels, tf, snapshots[k], after_states[k], P, sol) : + zero_e + if tied_to[k] != 0 + # Add Δ after the group, not through its later affects; this drops a term of + # size Δ (A - I). A new name, so that the closure `bridge` does not capture this + # variable in a box. + source = copy(zero_e) + source[dif] .-= affect_error[dif] + return (; + a = tf, b = tf, ea = copy(zero_e), eb = source, St = identity, + Sx = identity, saves = saves[k], group_saves = [saves[k]], partial = nothing, + after_states[k].changed, + ) + end + group = k:last_tied[k] + first_at_time = k == 1 || first(calls[k - 1]) != tf + u_before = first_at_time ? nothing : u_f + # The bridge of the first `m` calls of the group. The state and parameters after + # them are those of the next call, or P(t⁺) and the parameters after the event time. + function bridge(m) + part = group[1:m] + # Only the calls that changed something: the others have A = I, and the affect + # of a SavingCallback cannot take AD dual numbers. + tied = [ + (; + callback = trial_callbacks[first(first(calls[i][2]))], labels = calls[i][2], + snapshots[i].parent, snapshots[i].args, + ) for i in part[2:end] if after_states[i].changed + ] + changed = any(i -> after_states[i].changed, part) + next = last(part) < length(calls) && first(calls[last(part) + 1]) == tf ? + snapshots[last(part) + 1] : nothing + p_after = next === nothing ? _parameters_at(params, tf, :right) : next.p + u_after = next === nothing ? nothing : next.u + vjp = event_vjp && changed && (isempty(alg_idx) || _algebraic_solver() !== nothing) + maps = if !changed + identity, identity, zero(u_f[dif]) + elseif vjp + _saltation_vjp( + callback, labels, tf, p, parent, P, sol, prob, dif, alg_idx; + u_event = u_f, p_after, u_after, u_before, args, tied, verbose + ) + else + nothing + end + if maps === nothing + vjp && @SciMLMessage( + "GlobalAdjoint uses the finite-difference saltation matrix at the event at " * + "t = $tf: ReverseDiff and ForwardDiff cannot differentiate the callback (or, for a " * + "DAE, the algebraic projection). " * + "Set `autojacvec = false` in the `sensealg` to use finite differences " * + "without this warning.", + verbose, :global_error_ad_fallback, maxlog = 1 + ) + S, c = _event_saltation( + callback, labels, tf, p, parent, P, sol, prob, dif, alg_idx; + u_event = u_f, p_after, u_after, u_before, args, tied, verbose + ) + St, Sx = _saltation_maps(S) + else + St, Sx, c = maps + end + eb = copy(zero_e) + eb[dif] .= c .- affect_error[dif] + return (; St, Sx, eb) + end + # Not `(; St, Sx, eb) = ...`: the closure `bridge` would then write to them. + whole = bridge(length(group)) + # A saved entry between the calls of a group needs the bridge of the calls before + # it (`_entry_bridge`). Only an entry where an estimate ends uses it, so compute it + # lazily. + group_saves = saves[group] + partial = if length(group) == 1 + nothing + else + partials = Dict{Int, Any}() + m -> get!(() -> bridge(m), partials, m) + end + ea = first_at_time ? u_f .- sol(tf; continuity = :left) : copy(zero_e) + (; + a = tf, b = tf, ea, whole.eb, whole.St, whole.Sx, saves = saves[k], group_saves, + partial, after_states[k].changed, + ) + end + return (; + prob, sol, knots, jumps, windows = _merge_windows(windows, dif), calls = windows, dif, + alg_idx, params, e0, verbose, + ) +end + +# The trial runs assume that the affect is a function of u, p and t. Check this at each +# call that changes u or p: a rerun at the state `before` the call must give the state +# `after` it. An affect that reads the derivative or the interpolant differs by about the +# local error, so the tolerance is 10 times the solver tolerances. An affect that reads a +# counter fails. Returns the forward affect error Δ = u⁺ - affect(u_f), so that +# e⁺ = S e⁻ + c - Δ. +function _check_trial_affect(callback, labels, t, before, after, P, sol) + u, p = _trial_affect( + callback, labels, before.u, t, before.parent, before.p, sol, P; args = before.args + ) + opts = before.parent.opts + rtol = 10 * maximum(opts.reltol) + atol = 10 * maximum(opts.abstol) + (_approx_same(u, after.u; rtol, atol) && _approx_same(p, after.p; rtol, atol)) || throw( + ArgumentError( + "GlobalAdjoint: the affect of the event at t = $t gives another result when it " * + "runs again at the same state, parameters and time. GlobalAdjoint needs an " * + "affect that is a function of these, as SciMLSensitivity's adjoints do, not " * + "of state kept in the callback (such as a counter). GlobalErrorEstimation " * + "supports such affects." + ) + ) + return after.u .- u +end + +# Equality up to the tolerances (exact by default). Compare element by element or field by +# field if `isequal` fails: a struct without `==` is not `isequal` to its copy. A comparison +# that throws counts as a difference. +function _approx_same(a, b; rtol = 0, atol = 0) + return try + _approx_same_values(a, b, rtol, atol) + catch err + err isa InterruptException && rethrow() + false + end +end + +function _approx_same_values(a, b, rtol, atol) + isequal(a, b) && return true + numeric = x -> x isa Union{Number, AbstractArray{<:Number}} + numeric(a) && numeric(b) && return isapprox(a, b; rtol, atol, nans = true) + a isa AbstractArray && b isa AbstractArray && return size(a) == size(b) && + all(_approx_same_values(x, y, rtol, atol) for (x, y) in zip(a, b)) + T = typeof(a) + return T === typeof(b) && isstructtype(T) && nfields(a) > 0 && + all(i -> _approx_same_values(getfield(a, i), getfield(b, i), rtol, atol), 1:nfields(a)) +end + +function _unsupported_events_error() + return ArgumentError( + "GlobalAdjoint supports DAEs and events that change the state or the parameters only " * + "with a GaussKronrodAdjoint (the default), GaussAdjoint or InterpolatingAdjoint sensealg" + ) +end + +# The dense output of the step after an event starts from the last saved state. Save the +# state after the calls that changed u or p, so that P(t⁺) is that state. Otherwise P starts +# from the state before the affect, and the error identity cancels large terms. This +# callback comes after the user callbacks, so after the DAE reinitialization. +function _save_after_events(changed_here) + return SciMLBase.DiscreteCallback( + (u, t, integrator) -> changed_here[], + function (integrator) + changed_here[] = false + sol = integrator.sol + sol.t[end] == integrator.t && sol.u[end] == integrator.u || + SciMLBase.savevalues!(integrator, true) + SciMLBase.derivative_discontinuity!(integrator, false) + return nothing + end; + save_positions = (false, false), initializealg = SciMLBase.NoInit() + ) +end + +# The calls at the time of saved entry `index` that act before the save, in order. +function _calls_before_entry(data, index) + t = data.sol.t[index] + return [w for w in data.calls if w.b == t && w.saves < index] +end + +# The bridge (St, Sx, eb) of call window `w` for saved entry `index`. An entry between the +# calls of a group gets the bridge of the calls before it. +function _entry_bridge(w, index) + m = count(<(index), w.group_saves) + return m < length(w.group_saves) ? w.partial(m) : w +end + +# The parameters on each interval between event times, since an affect can change them. +# The next snapshot comes after a DAE reinitialization, so it includes any change from it. +function _parameter_record(calls, snapshots, p_end) + first_call = [k for k in eachindex(calls) if k == 1 || first(calls[k]) != first(calls[k - 1])] + times = [first(calls[k]) for k in first_call] + values = [[snapshots[k].p for k in first_call]; [p_end]] + return (; times, values = map(identity, values)) +end + +# The parameters at t, on the left or right side of an event time. +function _parameters_at(params, t, side) + (; times, values) = params + k = side === :left ? searchsortedfirst(times, t) : searchsortedlast(times, t) + 1 + return values[k] +end + +# The same data for a solution without events, such as the fixed-grid solve of +# StepGridRefinement. +function _unrecorded_data(prob, sol, verbose) + dif, alg_idx = _semiexplicit_components(prob, "GlobalAdjoint") + knots, jumps = _knot_jumps(sol) + params = (; times = eltype(sol.t)[], values = [sol.prob.p]) + return (; + prob, sol, knots, jumps, windows = Any[], calls = Any[], dif, alg_idx, params, + e0 = zero(sol.u[1]), verbose, + ) +end diff --git a/lib/GlobalDiffEq/src/companion.jl b/lib/GlobalDiffEq/src/companion.jl index da3022b702a..c1504d4b396 100644 --- a/lib/GlobalDiffEq/src/companion.jl +++ b/lib/GlobalDiffEq/src/companion.jl @@ -17,19 +17,28 @@ function _validate_tolerances(abstol, reltol, name) return nothing end -function _validate_estimation_problem(prob, name) +# `events = true` for the DefectCorrection companion, which supports callbacks and mass +# matrices (see defect_correction.jl). +function _validate_estimation_problem(prob, name; events = false) prob.u0 isa AbstractVector{<:AbstractFloat} || throw(ArgumentError("$name requires a real floating-point vector state")) isempty(prob.u0) && throw(ArgumentError("$name requires a nonempty state")) prob.tspan[1] < prob.tspan[2] || throw(ArgumentError("$name requires a forward-time ODEProblem")) - prob.f.mass_matrix == LinearAlgebra.I || - throw(ArgumentError("$name currently requires the standard mass matrix")) - _has_callbacks(values(prob.kwargs)) && - throw(ArgumentError("$name does not currently support callbacks")) + events && return nothing + prob.f.mass_matrix == LinearAlgebra.I || throw( + ArgumentError( + "$name supports mass matrices only with equation = DefectCorrection" + ) + ) + _has_callbacks(values(prob.kwargs)) && throw(_callback_support_error(name)) return nothing end +function _callback_support_error(name) + return ArgumentError("$name supports callbacks only with equation = DefectCorrection") +end + # The estimator differentiates the dense output (P' = interp(t, Val{1})) to form # the defect. A solver without genuine dense output leaves the trivial # Linear/Constant fallback interpolation, whose derivative does not approximate @@ -83,9 +92,11 @@ error. One of [`DefectCorrection`](@ref) or [`ErrorTransport`](@ref). @doc """ DefectCorrection -Integrate the nonlinear correction equation `ε' = f(P + ε, p, t) - P'(t)`, -`ε(t₀) = 0`, where `P` is the numerical solution's dense interpolant. Requires -no Jacobian, only extra evaluations of `f` on the perturbed argument `P + ε`. +Integrate the nonlinear correction equation `M ε' = f(P + ε, p, t) - M P'(t)`, +`ε(t₀) = 0`, where `P` is the numerical solution's dense interpolant and `M` the +mass matrix. Requires no Jacobian, only extra evaluations of `f` on the perturbed +argument `P + ε`. The user's callbacks act on `P + ε`, the estimate of the exact +solution, so the estimate follows the true event times. The "solving for the correction" technique of Zadunaisky (1976) and Dormand, Duckers and Prince (1984). """ DefectCorrection @@ -123,23 +134,19 @@ and runs a second solve. Co-integrate the error estimate in a single forward pass: drive the forward integrator one accepted step at a time and, over each just-completed step, advance the companion ODE using that step's live dense output as the interpolant. -Stores no dense solution (`O(1)` extra memory) and runs no second solve. Gives an +For [`global_error_estimate`](@ref) and in the `gtol` control loop, it stores no dense +solution (`O(1)` extra memory) and runs no second solve. The solution from `solve` has +the error of each saved state (`sol.global_error`) and saves each step; under `gtol`, +this costs one more estimate after the control loop. Gives an estimate closely agreeing with [`InterpolatingMode`](@ref). """ SimultaneousMode -# Out-of-place view of the (possibly in-place) user RHS, safe for dual numbers. +# Out-of-place view of the (possibly in-place) user RHS, safe for dual numbers (`_rhs`). function _oop_rhs(prob) f = SciMLBase.unwrapped_f(prob.f) - if SciMLBase.isinplace(prob) - return let f = f - function (u, p, t) - du = similar(u) - f(du, u, p, t) - return du - end - end - else - return f + SciMLBase.isinplace(prob) || return f + return let f = f + (u, p, t) -> _rhs(f, true, u, p, t) end end @@ -152,18 +159,6 @@ _interp_value(interp, t, right_continuity) = _interp_deriv(interp, t, right_continuity) = right_continuity ? interp(t, Val{1}, continuity = :right) : interp(t, Val{1}) -# Nonlinear defect-correction companion RHS: ε' = f(P + ε) - P'. -function _defect_correction_rhs(prob, interp, right_continuity) - foop = _oop_rhs(prob) - return let interp = interp, foop = foop, p = prob.p, rc = right_continuity - function (ε, _, t) - u = _interp_value(interp, t, rc) - du = _interp_deriv(interp, t, rc) - return foop(u + ε, p, t) - du - end - end -end - # Linearized error-transport companion RHS: ε' = J(P) ε + (f(P) - P'), with the # Jacobian applied matrix-free as a JVP through `autodiff`. function _error_transport_rhs(prob, interp, right_continuity, autodiff) @@ -185,13 +180,12 @@ function _error_transport_rhs(prob, interp, right_continuity, autodiff) end end -# Return `make_rhs(interp, prob, right_continuity)` selecting the companion ODE. +# Return `make_rhs(interp, prob, right_continuity)` for the ErrorTransport companion. +# DefectCorrection has its own implementation with callbacks in defect_correction.jl. function _companion_rhs_builder(equation::GlobalErrorEquation, autodiff) - if equation === DefectCorrection - return (interp, prob, rc) -> _defect_correction_rhs(prob, interp, rc) - else - return (interp, prob, rc) -> _error_transport_rhs(prob, interp, rc, autodiff) - end + equation === ErrorTransport || + throw(ArgumentError("only ErrorTransport uses the generic companion")) + return (interp, prob, rc) -> _error_transport_rhs(prob, interp, rc, autodiff) end const _DENSE_SOLVE_KWARGS = (; @@ -205,21 +199,25 @@ const _DENSE_SOLVE_KWARGS = (; # Endpoint-error refinement loop: solve, estimate the endpoint global error, and # tighten the local tolerances until the estimate is at most gtol; then redo the -# solve with the user's original saving options. +# solve with the user's original saving options. If given, `final_solve(abstol, reltol, +# result)` does that solve instead, with the estimator result at the accepted tolerances. +# The estimator returns a number or `(; estimate, ...)`. function _refine_to_gtol( estimator, prob, inner_alg, gtol, options, args...; - abstol, reltol, kwargs... + final_solve = nothing, abstol, reltol, kwargs... ) local_abstol = abstol local_reltol = reltol last_estimate = oftype(float(gtol), Inf) for _ in 1:options.maxiters - last_estimate = estimator(local_abstol, local_reltol) + result = estimator(local_abstol, local_reltol) + last_estimate = result isa Number ? result : result.estimate isfinite(last_estimate) || throw(ErrorException("the global error estimate is not finite")) if last_estimate <= gtol + final_solve === nothing || return final_solve(local_abstol, local_reltol, result) return SciMLBase.solve( prob, inner_alg, args...; abstol = local_abstol, reltol = local_reltol, kwargs... @@ -241,6 +239,21 @@ function _refine_to_gtol( ) end +# Default companion tolerances. The companion `reltol` does not set the relative accuracy +# of the estimate: the error can have a transient peak much larger than its final value, +# and the companion errors at the peak do not shrink with it. The companion `abstol` must +# be below the size of the error, which the forward `abstol` bounds only roughly. +const _DEFAULT_COMPANION_RELTOL = 1.0e-8 +const _COMPANION_ABSTOL_FACTOR = 1.0e-4 + +function _companion_tolerances(companion_abstol, companion_reltol, abstol, gtol) + reltol = something(companion_reltol, _DEFAULT_COMPANION_RELTOL) + companion_abstol === nothing || return companion_abstol, reltol + default_abstol = _COMPANION_ABSTOL_FACTOR .* abstol + gtol === nothing || (default_abstol = min.(default_abstol, gtol / 100)) + return default_abstol, reltol +end + function _companion_options(gtol, maxiters, safety, companion_abstol, companion_reltol) maxiters isa Integer && maxiters > 0 || throw(ArgumentError("maxiters must be a positive integer")) @@ -248,7 +261,8 @@ function _companion_options(gtol, maxiters, safety, companion_abstol, companion_ throw(ArgumentError("safety must be between zero and one")) (gtol === nothing || _positive_finite_real(gtol)) || throw(ArgumentError("gtol must be a positive finite real number")) - _validate_tolerances(companion_abstol, companion_reltol, "companion") + # `nothing` selects the default (see `_companion_tolerances`). + _validate_tolerances(something(companion_abstol, 1.0), something(companion_reltol, 1.0), "companion") return (; maxiters = Int(maxiters), safety, companion_abstol, companion_reltol, @@ -262,8 +276,7 @@ function _companion_error_estimate( make_rhs, name, prob, inner_alg, companion_alg, args...; abstol, reltol, companion_abstol, companion_reltol, kwargs... ) - _has_callbacks(kwargs) && - throw(ArgumentError("$name does not currently support callbacks")) + _has_callbacks(kwargs) && throw(_callback_support_error(name)) _validate_estimation_problem(prob, name) solve_kwargs = merge((; kwargs...), _DENSE_SOLVE_KWARGS) sol = SciMLBase.solve(prob, inner_alg, args...; abstol, reltol, solve_kwargs...) @@ -283,8 +296,7 @@ function _companion_error_estimate_streaming( make_rhs, name, prob, inner_alg, companion_alg, args...; abstol, reltol, companion_abstol, companion_reltol, kwargs... ) - _has_callbacks(kwargs) && - throw(ArgumentError("$name does not currently support callbacks")) + _has_callbacks(kwargs) && throw(_callback_support_error(name)) _validate_estimation_problem(prob, name) integrator = SciMLBase.init( prob, inner_alg, args...; diff --git a/lib/GlobalDiffEq/src/defect_correction.jl b/lib/GlobalDiffEq/src/defect_correction.jl new file mode 100644 index 00000000000..3f699ee1d50 --- /dev/null +++ b/lib/GlobalDiffEq/src/defect_correction.jl @@ -0,0 +1,903 @@ +# The DefectCorrection companion with callbacks and mass matrices (for example +# ModelingToolkit models with events), in both modes. The companion is the true problem in +# the coordinates e = u - P(t): +# +# M e' = f(P(t) + e, p̃, t) - M P'(t), e(t₀) = 0, +# +# where P is the dense output of the forward solve and p̃ is a companion copy of the initial +# parameters. Then P + e is the exact solution of the true problem. Besides its equation, +# the companion needs three rules: +# +# - P and P' come from the curve of the current interval of P (see `_IntervalInterp`). +# - P jumps at its knots and the true solution does not, so e ← e - [P] at each knot. +# At an event knot, [P] includes the forward affect; the user callbacks below give the +# true solution its own affect. +# - The user callbacks see the true state P + e and p̃ (`_TrueStateIntegrator`). The +# companion follows the true event times, which can differ from the forward ones; e is +# O(1) in the short gap between the two. +# +# A DiscreteCallback with a state condition is checked only at the knots of P, as in the +# forward solve, so the true solution does not depend on the companion step grid. It is +# also checked at the true event times, because a continuous event ends a step in every +# solve. +# +# InterpolatingMode integrates the companion after the forward solve, against the stored +# dense output (`_IntervalInterp`). SimultaneousMode advances one companion integrator over +# each forward step, against the live interpolant of that step (`_LiveInterp`). Both types +# give P(t), P'(t), the forward step ends and the forward solution, so the callback +# wrappers below serve both. + +# P and P' on the current interval [ends[k - 1], ends[k]] of the forward solution: the +# right limit at the interval start and the left limit at its end. The left limit matters +# at a step end on an event knot. There e still has its value from before the knot update, +# so P after the forward affect would shift the true state by the affect, and the companion +# would miss events. The knot callback advances `k`. +mutable struct _IntervalInterp{S, T} + sol::S + ends::Vector{T} + k::Int +end + +function (P::_IntervalInterp)(t) + return t >= P.ends[P.k] ? P.sol(t; continuity = :left) : P.sol(t; continuity = :right) +end + +function _interval_deriv(P::_IntervalInterp, t) + return t >= P.ends[P.k] ? P.sol(t, Val{1}; continuity = :left) : + P.sol(t, Val{1}; continuity = :right) +end + +# In-place forms for the companion RHS, which runs at every stage. +_interval_continuity(P::_IntervalInterp, t) = t >= P.ends[P.k] ? :left : :right +function _interval_value!(out, P::_IntervalInterp, t) + return P.sol(out, t; continuity = _interval_continuity(P, t)) +end +function _interval_deriv!(out, P::_IntervalInterp, t) + return P.sol(out, t, Val{1}; continuity = _interval_continuity(P, t)) +end + +_is_step_end(P::_IntervalInterp, t) = insorted(t, P.ends) +_forward_solution(P::_IntervalInterp) = P.sol + +# P and P' on the current step of the forward integrator in SimultaneousMode. +struct _LiveInterp{I} + integrator::I +end + +(P::_LiveInterp)(t) = P.integrator(t) +_interval_deriv(P::_LiveInterp, t) = P.integrator(t, Val{1}) +_interval_value!(out, P::_LiveInterp, t) = P.integrator(out, t) +_interval_deriv!(out, P::_LiveInterp, t) = P.integrator(out, t, Val{1}) +_is_step_end(P::_LiveInterp, t) = t == P.integrator.t +_forward_solution(P::_LiveInterp) = P.integrator.sol + +# The interior knots (unique saved times strictly inside the span) and the jump [P] of the +# dense output at each one. +function _knot_jumps(sol) + times = unique(sol.t) + knots = times[2:(end - 1)] + jumps = [sol(t; continuity = :right) .- sol(t; continuity = :left) for t in knots] + return knots, jumps +end + +function _knot_update!(integrator, P, knots, jumps) + j = searchsortedfirst(knots, integrator.t) + P.k == j || throw( + ErrorException( + "the companion solve lost track of the forward intervals at t = $(integrator.t)" + ) + ) + integrator.u .-= jumps[j] + P.k += 1 + return nothing +end + +# The error trajectory. At each forward step end, the companion records P(t) + e after the +# knot update (before the user DiscreteCallbacks), after each of their calls, and after all +# of them. The values differ only if a DiscreteCallback has a true event at the knot. +function _true_state_recorder(P, records, first::Bool) + affect! = function (integrator) + _record_true_state!(records, P, integrator, first) + SciMLBase.derivative_discontinuity!(integrator, false) + return nothing + end + return SciMLBase.DiscreteCallback( + (e, t, integrator) -> _is_step_end(P, t), affect!; + save_positions = (false, false), initializealg = SciMLBase.NoInit() + ) +end + +function _record_true_state!(records, P, integrator, first = false) + t = integrator.t + true_state = P(t) .+ integrator.u + first ? get!(() -> [true_state], records, t) : push!(records[t], true_state) + return nothing +end + +# The trajectory recorders, or nothing without a trajectory. The first recorder runs before +# the user callbacks and the last one after them. `after_call` runs after each companion +# DiscreteCallback call; it records only at a knot, because only knots have entries. +function _trajectory_recorders(P, trajectory, T) + trajectory || return nothing, (), (), nothing + records = Dict{T, Any}() + after_call = function (integrator) + _is_step_end(P, integrator.t) && _record_true_state!(records, P, integrator) + return nothing + end + return records, (_true_state_recorder(P, records, true),), + (_true_state_recorder(P, records, false),), after_call +end + +# The global error y(tᵢ) - uᵢ of each saved forward state. A callback can save before and +# after its affect, so an event time can have several saves; `calls` places each save +# between the calls at its time. A save after k DiscreteCallback calls gets the true state +# after k companion calls there. A continuous event needs no such match, because its true +# counterpart is at another time. At t₀ the error is zero. +function _trajectory_errors(sol, records, calls) + t = sol.t + # The number of saves before each DiscreteCallback call, grouped by time. + discrete_saves = Dict{eltype(t), Vector{Int}}() + for c in calls + c.discrete && push!(get!(discrete_saves, c.t, Int[]), c.saves) + end + return map(eachindex(t)) do i + t[i] == t[begin] && return zero(sol.u[i]) + discrete = get(discrete_saves, t[i], Int[]) + k = count(<(i), discrete) + states = records[t[i]] + true_state = k == length(discrete) && k > 0 ? states[end] : states[min(1 + k, end)] + return true_state .- sol.u[i] + end +end + +# The forward calls, each with its time, whether it is a DiscreteCallback, and the number +# of saves before it. `marks` holds (t, events before, saves before). +function _forward_call_saves(marks, events) + calls = _forward_calls([(t, n) for (t, n, _) in marks], events) + return [ + (; t, discrete = _is_discrete(first(labels)), saves = marks[k][3]) + for (k, (t, labels)) in enumerate(calls) + ] +end + +# The integrator that the user callbacks see: the true state u = P + e, the companion +# parameters p̃, and the time. `_run_on_true_state!` writes changes to `u` back to e. +# Integrator functions without a meaning for the true state throw a clear error. +mutable struct _TrueStateIntegrator{U, P, T, I, S, C} + u::U + p::P + t::T + parent::I + sol::S + interp::C + # Set by `terminate!` (see below). + terminated::Bool +end +function _TrueStateIntegrator(u, p, t, parent, sol, interp) + return _TrueStateIntegrator(u, p, t, parent, sol, interp, false) +end + +# The true state at a time in the current forward interval (a SavingCallback with `saveat` +# asks for it): the curve of P on that interval plus the companion interpolant of e. That +# interpolant covers only the last companion step and extrapolates before it, so values +# there are off by about the forward error. The user never sees such values, because the +# companion runs on a copy of the user callbacks. +_step_curve(P::_IntervalInterp, t) = P.sol(t; continuity = :left) +_step_curve(P::_LiveInterp, t) = P.integrator(t) +function (integrator::_TrueStateIntegrator)(t) + return _step_curve(integrator.interp, t) .+ integrator.parent(t) +end +(integrator::_TrueStateIntegrator)(out, t) = (out .= integrator(t)) + +# Read-only properties of the time axis and the options mean the same for the true state, +# so they come from the companion integrator. ModelingToolkit implicit affects read the +# tolerances; PresetTimeCallback, PeriodicCallback and IterativeCallback read `tdir`. +const _COMPANION_PROPERTIES = (:opts, :tdir, :dt, :tprev, :iter) + +function Base.getproperty(integrator::_TrueStateIntegrator, name::Symbol) + name in _COMPANION_PROPERTIES && return getproperty(getfield(integrator, :parent), name) + return getfield(integrator, name) +end + +function Base.propertynames(integrator::_TrueStateIntegrator, private::Bool = false) + return (fieldnames(typeof(integrator))..., _COMPANION_PROPERTIES...) +end + +SymbolicIndexingInterface.state_values(integrator::_TrueStateIntegrator) = integrator.u +SymbolicIndexingInterface.parameter_values(integrator::_TrueStateIntegrator) = integrator.p +SymbolicIndexingInterface.current_time(integrator::_TrueStateIntegrator) = integrator.t +SymbolicIndexingInterface.is_time_dependent(::_TrueStateIntegrator) = true +function SymbolicIndexingInterface.symbolic_container(integrator::_TrueStateIntegrator) + return SymbolicIndexingInterface.symbolic_container(integrator.sol) +end +function SymbolicIndexingInterface.set_state!(integrator::_TrueStateIntegrator, val, idx) + return integrator.u[idx] = val +end +function SymbolicIndexingInterface.set_parameter!(integrator::_TrueStateIntegrator, val, idx) + return SymbolicIndexingInterface.set_parameter!(integrator.p, val, idx) +end + +# Compiled ModelingToolkit conditions read `get_sol(integrator).prob`. +SciMLBase.get_sol(integrator::_TrueStateIntegrator) = integrator.sol +SciMLBase.get_tmp_cache(integrator::_TrueStateIntegrator) = (similar(integrator.u),) +SciMLBase.u_modified!(::_TrueStateIntegrator, ::Bool) = nothing +SciMLBase.derivative_discontinuity!(::_TrueStateIntegrator, ::Bool) = nothing +SciMLBase.reeval_internals_due_to_modification!(::_TrueStateIntegrator, args...; kwargs...) = nothing +# A tstop is a time, so it means the same for the true state (PresetTimeCallback). +function SciMLBase.add_tstop!(integrator::_TrueStateIntegrator, t) + return SciMLBase.add_tstop!(integrator.parent, t) +end +# PeriodicCallback reads the last tstop before it adds the next one. +DiffEqBase.get_tstops_max(integrator::_TrueStateIntegrator) = DiffEqBase.get_tstops_max(integrator.parent) +# A terminating affect ends the forward solve at its event, and the estimate is the error +# at that time. So the companion does not stop; it only records the call for +# `_warn_if_events_differ`. +function SciMLBase.terminate!(integrator::_TrueStateIntegrator, args...; kwargs...) + integrator.terminated = true + return nothing +end +# The derivative of the true state of an ODE is f(u, p, t) (TerminateSteadyState reads it). +# A DAE has no derivative in the algebraic components. +function SciMLBase.get_du(integrator::_TrueStateIntegrator) + prob = integrator.sol.prob + prob.f.mass_matrix isa LinearAlgebra.UniformScaling || throw( + ArgumentError( + "GlobalErrorEstimation applies the user callbacks to the true state in its " * + "companion solve; a callback that calls `get_du` is not supported for a " * + "problem with a mass matrix" + ) + ) + return _rhs(SciMLBase.unwrapped_f(prob.f), SciMLBase.isinplace(prob), integrator.u, integrator.p, integrator.t) +end +SciMLBase.get_du!(out, integrator::_TrueStateIntegrator) = (out .= SciMLBase.get_du(integrator)) + +for name in ( + :add_saveat!, :set_proposed_dt!, :get_proposed_dt, :reinit!, + :auto_dt_reset!, :savevalues!, :set_u!, :set_t!, + ) + @eval function SciMLBase.$name(::_TrueStateIntegrator, args...; kwargs...) + throw( + ArgumentError( + "GlobalErrorEstimation applies the user callbacks to the true state in its " * + "companion solve; a callback that calls `" * $(string(name)) * + "` is not supported" + ) + ) + end +end + +function _true_state_integrator(ũ, integrator, P) + return _TrueStateIntegrator( + ũ, integrator.p, integrator.t, integrator, _forward_solution(P), P + ) +end + +# Run `f!(true_state_integrator, args...)` on u = P(t) + e and write the result back to e. +# Returns whether `f!` called `terminate!`. +function _run_on_true_state!(f!, integrator, P, args...) + Pt = P(integrator.t) + true_state = _true_state_integrator(Pt .+ integrator.u, integrator, P) + f!(true_state, args...) + integrator.u .= true_state.u .- Pt + true_state.p === integrator.p || (integrator.p = true_state.p) + return true_state.terminated +end + +function _true_state_initialize(callback, P) + return (_, e, t, integrator) -> _run_on_true_state!( + true_state -> callback.initialize(callback, true_state.u, t, true_state), + integrator, P + ) +end + +# An event label is (callback number, kind), for the comparison of the forward and the +# companion events. The kind is +1 or -1 for the up- and down-crossing affect of a +# ContinuousCallback and 0 for a DiscreteCallback. For a VectorContinuousCallback it is the +# fired component; the affect gets one index or a mask of the components that fired +# together. +_fired_components(index::Integer) = (index,) +_fired_components(mask) = findall(!iszero, mask) + +# A DiscreteCallback label has kind 0. A VectorContinuousCallback label has kind `nothing` +# until `_record_event!` expands it. +_is_discrete(label) = last(label) === 0 + +function _record_event!(events, label, args...) + callback_number, kind = label + if kind === nothing + for component in _fired_components(only(args)) + push!(events, (callback_number, component)) + end + else + push!(events, (callback_number, kind)) + end + return nothing +end + +# The companion form of the DAE reinitialization that the forward solve applies after an +# affect (the callback algorithm, else the solve algorithm). The companion is a DAE in e +# with the algebraic equations 0 = g(P + e), so BrownFullBasicInit gives the reinitialized +# true state; its tolerance must be below the size of e. CheckInit and NoInit change +# neither the forward nor the true state. OverrideInit solves for u, which has no meaning +# for e. +function _companion_reinit(alg, prob, kwargs, abstol) + if alg === nothing + problem_kwargs = values(prob.kwargs) + alg = get(kwargs, :initializealg, get(problem_kwargs, :initializealg, DefaultInit())) + end + if alg isa DefaultInit + alg = SciMLBase.has_initializeprob(prob.f) ? SciMLBase.OverrideInit() : + SciMLBase.CheckInit() + end + return _companion_reinit(alg, abstol) +end +_companion_reinit(::Union{SciMLBase.NoInit, SciMLBase.CheckInit}, _) = SciMLBase.NoInit() +function _companion_reinit(alg::BrownFullBasicInit, abstol) + return BrownFullBasicInit(min(alg.abstol, minimum(abstol)), alg.nlsolve) +end +_companion_reinit(alg, _) = alg + +# The reinitialization runs in the affect wrapper, not as the callback `initializealg`, +# because only the wrapper knows if the companion is a DAE. +_reinitialize_true_state!(integrator, ::SciMLBase.NoInit) = nothing +function _reinitialize_true_state!(integrator, alg) + integrator.isdae || return nothing + alg isa SciMLBase.OverrideInit && throw( + ArgumentError( + "the reinitialization of a DAE with OverrideInit after an event is not " * + "supported in the companion error solve (OverrideInit is the default for " * + "a ModelingToolkit problem when the callback has no `initializealg`). " * + "Give the callback `initializealg = BrownFullBasicInit()`, `CheckInit()` " * + "or `NoInit()`." + ) + ) + SciMLBase.initialize_dae!(integrator, alg) + integrator.sol.retcode == SciMLBase.ReturnCode.InitialFailure && throw( + ErrorException("the companion reinitialization of the true state after an event failed") + ) + return nothing +end + +# A user affect applied to the true state. `record` collects the companion events, the +# times of the terminating calls, and the time of the last continuous event. `after_call` +# runs after each call (the trajectory recorder). +function _recording_true_state_affect( + affect!, P, record, label, reinit, after_call = nothing + ) + affect! === nothing && return nothing + return function (integrator, args...) + _record_event!(record.events, label, args...) + _run_on_true_state!(affect!, integrator, P, args...) && + push!(record.terminations, integrator.t) + _is_discrete(label) || (record.event_time[] = integrator.t) + _reinitialize_true_state!(integrator, reinit) + after_call === nothing || after_call(integrator) + return nothing + end +end + +# The companion callbacks have no finalizer: the user finalizers run in the forward solve. +_finalize_nothing(_, _, _, _) = nothing + +# The fields that differ from the user callback. The initialize runs on the true state, the +# user finalizers run in the forward solve, and the companion saves only its final value. +# `reinit` reinitializes the true state, so the companion state needs no `initializealg`. +# The condition applies `idxs` itself. Root finding and tolerances stay the same. +function _companion_fields(callback, P) + fields = (; + initialize = _true_state_initialize(callback, P), finalize = _finalize_nothing, + save_positions = (false, false), initializealg = SciMLBase.NoInit(), + saved_clock_partitions = (), initialize_save_discretes = false, + ) + # A DiscreteCallback has no `idxs` field. + return callback isa SciMLBase.DiscreteCallback ? fields : (; idxs = nothing, fields...) +end + +_true_state_view(ũ, ::Nothing) = ũ +_true_state_view(ũ, idxs) = view(ũ, idxs) + +function _companion_callback( + callback::SciMLBase.ContinuousCallback, P, record, number, reinit, _ = nothing + ) + condition = function (e, t, integrator) + ũ = P(t) .+ e + return callback.condition( + _true_state_view(ũ, callback.idxs), t, _true_state_integrator(ũ, integrator, P) + ) + end + return setproperties( + callback, (; + condition, + affect! = _recording_true_state_affect(callback.affect!, P, record, (number, 1), reinit), + affect_neg! = _recording_true_state_affect(callback.affect_neg!, P, record, (number, -1), reinit), + _companion_fields(callback, P)..., + ) + ) +end + +function _companion_callback( + callback::SciMLBase.VectorContinuousCallback, P, record, number, reinit, _ = nothing + ) + condition = function (out, e, t, integrator) + ũ = P(t) .+ e + return callback.condition( + out, _true_state_view(ũ, callback.idxs), t, + _true_state_integrator(ũ, integrator, P) + ) + end + return setproperties( + callback, (; + condition, + affect! = _recording_true_state_affect(callback.affect!, P, record, (number, nothing), reinit), + _companion_fields(callback, P)..., + ) + ) +end + +# The forward solve checks a DiscreteCallback only at its own step ends, so the companion +# checks it only at the knots of P and at the final time. P(t) + e is the true state there +# on either side of the knot update. A continuous event ends a step in every solve, so the +# companion checks a DiscreteCallback at the true event time (`record.event_time`), not at +# the forward event time (`record.event_knots`). +function _companion_callback( + callback::SciMLBase.DiscreteCallback, P, record, number, reinit, after_call = nothing + ) + condition = function (e, t, integrator) + knot = _is_step_end(P, t) && !(t in record.event_knots) + knot || record.event_time[] == t || return false + ũ = P(t) .+ e + return callback.condition(ũ, t, _true_state_integrator(ũ, integrator, P)) + end + return setproperties( + callback, (; + condition, + affect! = _recording_true_state_affect(callback.affect!, P, record, (number, 0), reinit, after_call), + _companion_fields(callback, P)..., + ) + ) +end + +# The forward solve runs the user callbacks unchanged, except that each affect records its +# event label and first calls `before(integrator, label, args...)`. SimultaneousMode +# advances the companion there, while the interpolant of the cut step is still valid. If +# `before` returns a function, it runs after the affect. +function _recording_affect(affect!, events, label, before) + affect! === nothing && return nothing + return function (integrator, args...) + after = before === nothing ? nothing : before(integrator, label, args...) + _record_event!(events, label, args...) + result = affect!(integrator, args...) + after === nothing || after(integrator) + return result + end +end + +# The user initialize and finalize functions get the user callback, not the recording one: +# some of them read its fields (the SavingCallback initialize reads `cb.affect!`). +_original_initialize(callback) = + (_, u, t, integrator) -> callback.initialize(callback, u, t, integrator) +_original_finalize(callback) = + (_, u, t, integrator) -> callback.finalize(callback, u, t, integrator) + +# The initialize and finalize of a recording callback, which call the user functions. +_original_hooks(callback) = + (; initialize = _original_initialize(callback), finalize = _original_finalize(callback)) + +function _recording_callback(callback::SciMLBase.ContinuousCallback, events, number, before) + return setproperties( + callback, (; + affect! = _recording_affect(callback.affect!, events, (number, 1), before), + affect_neg! = _recording_affect(callback.affect_neg!, events, (number, -1), before), + _original_hooks(callback)..., + ) + ) +end + +function _recording_callback(callback::SciMLBase.VectorContinuousCallback, events, number, before) + return setproperties( + callback, (; + affect! = _recording_affect(callback.affect!, events, (number, nothing), before), + _original_hooks(callback)..., + ) + ) +end + +function _recording_callback(callback::SciMLBase.DiscreteCallback, events, number, before) + return setproperties( + callback, (; + affect! = _recording_affect(callback.affect!, events, (number, 0), before), + _original_hooks(callback)..., + ) + ) +end + +_callback_list(callbacks) = (callbacks.continuous_callbacks..., callbacks.discrete_callbacks...) + +# The user callbacks from the problem and from the solve keywords, as the solve would +# merge them. +function _user_callbacks(prob, kwargs) + problem_kwargs = values(prob.kwargs) + from_problem = haskey(problem_kwargs, :callback) ? problem_kwargs.callback : nothing + from_solve = haskey(kwargs, :callback) ? kwargs[:callback] : nothing + return SciMLBase.CallbackSet(from_problem, from_solve) +end + +_with_copied_callback(kwargs) = + haskey(kwargs, :callback) ? (; kwargs..., callback = deepcopy(kwargs[:callback])) : kwargs + +# The forward problem and callbacks. The forward affects change the parameters in place, so +# the forward solve gets its own copy. The callbacks are passed explicitly, so they are +# removed from the problem keywords to avoid a second merge. +function _forward_setup(prob, callback_list, forward_events, before) + forward_prob = @set prob.p = deepcopy(prob.p) + forward_prob = @set forward_prob.kwargs = _without_callback(prob.kwargs) + forward_callbacks = SciMLBase.CallbackSet( + ( + _recording_callback(c, forward_events, n, before) + for (n, c) in enumerate(callback_list) + )... + ) + return forward_prob, forward_callbacks +end + +_subtract_mass_times!(de, M::LinearAlgebra.UniformScaling, v, _) = (de .-= M.λ .* v) +function _subtract_mass_times!(de, M, v, buffer) + eltype(v) === eltype(buffer) || return de .-= M * v + LinearAlgebra.mul!(buffer, M, v) + return de .-= buffer +end + +# The companion problem M e' = f(P + e, p̃, t) - M P', with p̃ a copy of the initial +# parameters. P, P' and P + e go into buffers. An implicit solver can pass dual numbers for +# e (the Jacobian) or for t (the Rosenbrock time derivative); then new arrays of that type +# hold them. +function _companion_problem(prob, P, tspan) + f = SciMLBase.unwrapped_f(prob.f) + M = prob.f.mass_matrix + iip = SciMLBase.isinplace(prob) + Pt, dP, ũ, MdP = (similar(prob.u0) for _ in 1:4) + rhs! = function (de, e, p̃, t) + if t isa eltype(Pt) + _interval_value!(Pt, P, t) + _interval_deriv!(dP, P, t) + Pv, dPv = Pt, dP + else + Pv = P(t) + dPv = _interval_deriv(P, t) + end + x = eltype(e) === eltype(ũ) === eltype(Pv) ? (ũ .= Pv .+ e) : Pv .+ e + iip ? f(de, x, p̃, t) : (de .= f(x, p̃, t)) + _subtract_mass_times!(de, M, dPv, MdP) + return nothing + end + return SciMLBase.ODEProblem{true, SciMLBase.FullSpecialize}( + SciMLBase.ODEFunction{true, SciMLBase.FullSpecialize}(rhs!; mass_matrix = M), + zero(prob.u0), tspan, deepcopy(prob.p) + ) +end + +# The companion solves the problem a second time, so it gets its own copy of the user +# callbacks, made before the forward solve changes them. The callback state (a counter, the +# values of a SavingCallback) then starts from the beginning, and the user's callbacks see +# only the forward solve. `reinit` maps a callback `initializealg` to the companion +# reinitialization (`_companion_reinit`). Only the DiscreteCallbacks use `after_call`. +function _companion_callbacks( + callback_list, P, record, reinit, extra...; after_call = nothing + ) + return SciMLBase.CallbackSet( + extra..., + ( + _companion_callback(c, P, record, n, reinit(c.initializealg), after_call) + for (n, c) in enumerate(callback_list) + )... + ) +end + +function _check_companion_retcode(retcode) + SciMLBase.successful_retcode(retcode) || throw( + ErrorException("the companion error solve failed with retcode $retcode") + ) + return nothing +end + +# Warn if the forward events differ from the true events that the companion computes. +# `marks` holds the forward calls as (t, events before, saves before). If the forward solve +# terminates, the companion can miss its continuous events at the end time, because the +# true event can come later. The same holds for the DiscreteCallback events that they +# trigger. Other DiscreteCallback events at the end time must agree. Also warn if only one +# of the two solves terminates at an event. +function _warn_if_events_differ(name, forward_events, record, marks, retcode, t_end, verbose) + companion_events = record.events + terminated = retcode == SciMLBase.ReturnCode.Terminated + final_call = findfirst(m -> m[1] == t_end, marks) + first_final = final_call === nothing ? length(forward_events) + 1 : marks[final_call][2] + 1 + final = forward_events[first_final:end] + first_continuous = something(findfirst(!_is_discrete, final), length(final) + 1) + required = terminated ? + [forward_events[1:(first_final - 1)]; final[1:(first_continuous - 1)]] : forward_events + same = sort(companion_events) == sort(forward_events) + same || sort(companion_events) == sort(required) || @SciMLMessage( + "$name: the events of the forward solve and of the true solution, as the " * + "companion computes it, are different (forward: $(length(forward_events)), " * + "true: $(length(companion_events))). The estimate includes the difference, " * + "but it is sensitive to the event times.", + verbose, :global_error_event_mismatch + ) + if !terminated && !isempty(record.terminations) + @SciMLMessage( + "$name: the true solution, as the companion computes it, terminates at " * + "t = $(first(record.terminations)), but the forward solve does not. The " * + "estimate is the error at the end time of the forward solve.", + verbose, :global_error_event_mismatch + ) + elseif terminated && same && isempty(record.terminations) + @SciMLMessage( + "$name: the forward solve terminates at t = $t_end, but the true solution, as " * + "the companion computes it, does not terminate at the same event. The " * + "estimate is the error at the end time of the forward solve.", + verbose, :global_error_event_mismatch + ) + end + return nothing +end + +# The forward part of a DefectCorrection estimate, shared by both modes. The companion copy +# of the callbacks is made before the forward solve changes them. `hook(marks, events)` +# gives the mode-specific rest of the `before` function (`_forward_setup`). The times are +# floats, also for an integer time span. +function _defect_correction_forward(name, prob, kwargs, hook) + _validate_estimation_problem(prob, name; events = true) + callback_list = _callback_list(_user_callbacks(prob, kwargs)) + companion_list = deepcopy(callback_list) + events = Tuple{Int, Int}[] + T = float(eltype(prob.tspan)) + marks = Tuple{T, Int, Int}[] + event_knots = Set{T}() + rest = hook(marks, events) + before = function (integrator, label, args...) + _is_discrete(label) || push!(event_knots, integrator.t) + return rest(integrator, label, args...) + end + forward_prob, forward_callbacks = _forward_setup(prob, callback_list, events, before) + return (; companion_list, events, marks, event_knots, forward_prob, forward_callbacks) +end + +# The companion callbacks: the user callbacks on the true state, the `extra` callbacks (the +# InterpolatingMode knot update) and, with `trajectory`, the true-state recorders. `record` +# also holds the forward continuous event times (see `_companion_callback`). +function _defect_correction_callbacks(forward, P, prob, kwargs, companion_abstol, trajectory, extra...) + T = float(eltype(prob.tspan)) + record = (; + events = Tuple{Int, Int}[], terminations = T[], event_time = Ref(T(NaN)), + forward.event_knots, + ) + records, first_recorder, last_recorder, after_call = _trajectory_recorders(P, trajectory, T) + callback = SciMLBase.CallbackSet( + _companion_callbacks( + forward.companion_list, P, record, + alg -> _companion_reinit(alg, prob, kwargs, companion_abstol), + extra..., first_recorder...; after_call + ), + last_recorder... + ) + return (; record, records, callback) +end + +# The 2-norm of the endpoint error, or with `trajectory` also the forward solution with the +# error of each saved state in `global_error`, as `(; estimate, sol)`. It also warns about +# event differences. `t_end` is the forward end time; a solution that saves only some steps +# can end before it. +function _defect_correction_result( + name, forward, callbacks, sol, endpoint_error, trajectory; verbose, t_end = sol.t[end] + ) + _warn_if_events_differ( + name, forward.events, callbacks.record, forward.marks, sol.retcode, t_end, verbose + ) + estimate = LinearAlgebra.norm(endpoint_error) + trajectory || return estimate + calls = _forward_call_saves(forward.marks, forward.events) + return (; estimate, sol = @set sol.global_error = _trajectory_errors(sol, callbacks.records, calls)) +end + +# InterpolatingMode: a dense forward solve, then the companion against its interpolant. +# The result is that of `_defect_correction_result`. +function _defect_correction_estimate( + name, prob, inner_alg, companion_alg, args...; + abstol, reltol, companion_abstol, companion_reltol, trajectory = false, kwargs... + ) + forward = _defect_correction_forward( + name, prob, kwargs, + (marks, events) -> (integrator, _...) -> _mark_call!(marks, events, integrator) + ) + integrator = SciMLBase.init( + forward.forward_prob, inner_alg, args...; + abstol, reltol, callback = forward.forward_callbacks, + merge(_without_callback(kwargs), _DENSE_SOLVE_KWARGS)... + ) + u_init = copy(integrator.u) + sol = SciMLBase.solve!(integrator) + _validate_estimation_solution(sol, name) + + knots, jumps = _knot_jumps(sol) + T = sol.t[end] + P = _IntervalInterp(sol, [knots; T], 1) + knot_callback = SciMLBase.DiscreteCallback( + (e, t, integrator) -> insorted(t, knots), + integrator -> _knot_update!(integrator, P, knots, jumps); + save_positions = (false, false), initializealg = SciMLBase.NoInit() + ) + callbacks = _defect_correction_callbacks( + forward, P, prob, kwargs, companion_abstol, trajectory, knot_callback + ) + companion = SciMLBase.init( + _companion_problem(prob, P, (prob.tspan[1], T)), companion_alg; + abstol = companion_abstol, reltol = companion_reltol, callbacks.callback, + tstops = knots, dense = false, save_everystep = false, save_start = false, + save_end = true, initializealg = SciMLBase.NoInit() + ) + # The callback initializations are exact (the true solution starts at the same state), + # so e after them is the initialized forward state minus P(t₀). The companion + # initializations only set the state inside the callback copies; `set_u!` replaces any + # change that they make to e. + SciMLBase.set_u!(companion, u_init .- P(prob.tspan[1])) + companion_sol = SciMLBase.solve!(companion) + _check_companion_retcode(companion_sol.retcode) + + # The companion gives the error against P(T⁻); the saved state u(T) can differ from + # P(T⁻), for example after an event at the final time. + endpoint_error = companion_sol.u[end] .- (sol.u[end] .- sol(T; continuity = :left)) + return _defect_correction_result( + name, forward, callbacks, sol, endpoint_error, trajectory; verbose = _verbosity(kwargs) + ) +end + +# Record a forward call: its time, the number of events before it, and the number of saved +# states before it (see `_trajectory_errors`). +function _mark_call!(marks, events, integrator) + push!(marks, (integrator.t, length(events), length(integrator.sol.t))) + return nothing +end + +# SimultaneousMode: the state of the streaming companion between forward steps. +mutable struct _StreamingCompanion{C, V} + companion::C + # P(t⁻) at the end of the last completed forward step, after the first step. + last_end::V + started::Bool + storage::IdDict{Any, Any} +end + +# Some callbacks keep state in task-local storage under a key that their copies share (the +# DiffEqCallbacks PeriodicCallback does). In SimultaneousMode both solves run in one task, +# so the companion copies need their own entries. The companion initialization runs with an +# empty storage, which shows the keys of its callbacks (`_with_task_storage`). Each +# companion step swaps only those keys (`_with_companion_keys`), so other task-local state +# stays visible. +function _with_task_storage(f, storage) + tls = task_local_storage() + outer = copy(tls) + empty!(tls) + merge!(tls, storage) + try + return f() + finally + empty!(storage) + merge!(storage, tls) + empty!(tls) + merge!(tls, outer) + end +end + +const _NO_ENTRY = gensym(:no_entry) + +function _with_companion_keys(f, storage) + isempty(storage) && return f() + tls = task_local_storage() + outer = Dict{Any, Any}(k => get(tls, k, _NO_ENTRY) for k in keys(storage)) + merge!(tls, storage) + try + return f() + finally + for k in keys(storage) + storage[k] = tls[k] + end + for (k, v) in outer + v === _NO_ENTRY ? delete!(tls, k) : (tls[k] = v) + end + end +end + +# Advance the companion over the forward step [tprev, t] that just completed, against the +# live interpolant of that step. First subtract the jump of P at tprev; at an event it +# includes the forward affect. The companion stops at t, so its DiscreteCallbacks are +# checked there. +function _advance_streaming_companion!(state, forward) + companion = state.companion + t = forward.t + companion.t < t || return nothing + companion.t == forward.tprev || throw( + ErrorException( + "the companion solve lost track of the forward steps at t = $(forward.tprev)" + ) + ) + if state.started + SciMLBase.set_u!(companion, companion.u .- (forward(forward.tprev) .- state.last_end)) + end + _with_companion_keys(() -> _step_to!(companion, t), state.storage) + _check_companion_retcode(SciMLBase.check_error(companion)) + state.last_end = forward(t) + state.started = true + return nothing +end + +# Step the integrator to exactly t, the forward step end, with a tstop at t. +# `step!(integrator, t - integrator.t, true)` can miss t by rounding if t > 2 integrator.t. +function _step_to!(integrator, t) + SciMLBase.add_tstop!(integrator, t) + while integrator.t < t + SciMLBase.step!(integrator) + integrator.sol.retcode in (SciMLBase.ReturnCode.Default, SciMLBase.ReturnCode.Success) || + break + end + return nothing +end + +function _defect_correction_estimate_streaming( + name, prob, inner_alg, companion_alg, args...; + abstol, reltol, companion_abstol, companion_reltol, trajectory = false, kwargs... + ) + # A forward affect first advances the companion to the event time. An affect in the + # forward initialization needs no advance, because the companion does not exist yet. + state_ref = Ref{Any}(nothing) + forward = _defect_correction_forward( + name, prob, kwargs, + (marks, events) -> function (integrator, _...) + # Without a trajectory, `_warn_if_events_differ` needs only the marks at the + # latest call time, so the memory stays O(1). + trajectory || isempty(marks) || first(last(marks)) == integrator.t || empty!(marks) + _mark_call!(marks, events, integrator) + state_ref[] === nothing || _advance_streaming_companion!(state_ref[], integrator) + return nothing + end + ) + # The error trajectory needs the saved state at each forward step. + save_kwargs = trajectory ? merge(_without_callback(kwargs), _DENSE_SOLVE_KWARGS) : + (; + dense = true, save_everystep = false, save_start = true, save_end = true, + _without_callback(kwargs)..., + ) + integrator = SciMLBase.init( + forward.forward_prob, inner_alg, args...; + abstol, reltol, callback = forward.forward_callbacks, save_kwargs... + ) + _validate_streaming_interpolation(integrator.sol, name) + + P = _LiveInterp(integrator) + callbacks = _defect_correction_callbacks(forward, P, prob, kwargs, companion_abstol, trajectory) + storage = IdDict{Any, Any}() + companion = _with_task_storage(storage) do + SciMLBase.init( + _companion_problem(prob, P, prob.tspan), companion_alg; + abstol = companion_abstol, reltol = companion_reltol, callbacks.callback, + dense = false, save_everystep = false, save_start = false, save_end = false, + initializealg = SciMLBase.NoInit() + ) + end + # As in InterpolatingMode, e after the initializations is the forward state minus + # P(t₀). That is zero here, because the first step of the live P starts at that state. + SciMLBase.set_u!(companion, zero(companion.u)) + state = _StreamingCompanion(companion, copy(prob.u0), false, storage) + state_ref[] = state + for _ in integrator + _advance_streaming_companion!(state, integrator) + end + SciMLBase.successful_retcode(integrator.sol) || + throw(ErrorException("the forward solve failed with retcode $(integrator.sol.retcode)")) + + # As in InterpolatingMode: the error against P(T⁻), corrected to the saved state. + endpoint_error = companion.u .- (integrator.u .- state.last_end) + return _defect_correction_result( + name, forward, callbacks, integrator.sol, endpoint_error, trajectory; + verbose = _verbosity(kwargs), t_end = integrator.t + ) +end diff --git a/lib/GlobalDiffEq/src/estimation.jl b/lib/GlobalDiffEq/src/estimation.jl index b028d8b0866..bbbe7107c46 100644 --- a/lib/GlobalDiffEq/src/estimation.jl +++ b/lib/GlobalDiffEq/src/estimation.jl @@ -38,15 +38,57 @@ solve(prob, GlobalErrorEstimation(Tsit5(); gtol = 1.0e-6)) ``` The solver then tightens local tolerances until the estimated endpoint global -error 2-norm is at most `gtol`. Omit `gtol` when using the algorithm only with -[`global_error_estimate`](@ref). `companion_alg` selects the solver for the -companion equation, and `companion_abstol` / `companion_reltol` control its -tolerances. +error 2-norm is at most `gtol`. Without `gtol`, the solve does one estimate at the +given `abstol` and `reltol`, with no control. + +With `equation = DefectCorrection`, the returned solution has the error trajectory +(`SciMLBase.has_global_error` is `true`): `sol.global_error[i]` is the estimated global +error `y(sol.t[i]) - sol.u[i]` of each saved state, as a vector. The solution is dense +and saved at each step of the forward solve (`saveat` does not apply). If an event time +is saved twice, the first entry has the error before the events at that time and the +second the error after them. Under global error control, the final solution costs one +more forward solve in `InterpolatingMode` and one more estimate in `SimultaneousMode`. +If the solve is deterministic, the user's callback objects see only that final solve. +[`global_error_estimate`](@ref) gives only the endpoint error 2-norm, with no solution. + +`companion_alg` selects the solver for the companion equation (by default the forward +solver `alg`), and `companion_abstol` / `companion_reltol` control its tolerances. The +companion runs at much tighter tolerances than the forward solve. So with a forward solver +of order 3 or lower (such as Rosenbrock23 or Rosenbrock32), pass a higher-order +`companion_alg`, for example `Tsit5()`, or `Rodas5P()` for a stiff problem or a DAE. The +solve warns about such a low-order companion solver. The companion state is the global +error itself. `companion_reltol` defaults to `1e-8`, because the error can have a +transient peak much larger than its final value (for example at a fast transition of a +stiff problem). `companion_abstol` must be below the size of the error. It defaults to +`1e-4` times the forward `abstol`, and at most `gtol / 100` under global error control. Every combination **requires** the wrapped solver to provide a genuine dense first-derivative interpolation (a solver with only the trivial fallback -interpolation is rejected). This implementation supports forward-time, -standard-mass-matrix ODEs with real vector states and no callbacks. +interpolation is rejected), a forward time span, and a real vector state. + +`DefectCorrection` (the default, in either mode) also supports mass-matrix DAEs and +callbacks (`ContinuousCallback`, `VectorContinuousCallback`, `DiscreteCallback`), for +example ModelingToolkit models with events. The companion applies the user's callbacks +to the true state `P + ε`, with its own copy of the parameters. So the estimate includes +the effect of the event times and of events that the forward solve missed or added. A +warning reports when the events of the forward solve and of the true solution differ. A +`DiscreteCallback` is checked only at the step ends of the forward solve and at the times +of the true continuous events. + +After an event in a DAE, the companion reinitializes the true state with the algorithm of +the forward solve (for example `BrownFullBasicInit`). `OverrideInit`, the default for a +ModelingToolkit problem when a callback has no `initializealg`, is not supported there. +A callback can terminate the solve; the estimate is then the error at the time where the +forward solve stops. A warning reports when the true solution does not terminate at the +same event, or terminates where the forward solve does not. Callbacks that change the +step size are not supported in the companion. `ErrorTransport` supports only +standard-mass-matrix ODEs without callbacks. + +Warnings follow the `verbose` keyword of `solve` and of `global_error_estimate` (a +[`DEVerbosity`](@ref)), with these toggles: `global_error_event_mismatch` (the events or +the terminations of the forward solve and of the true solution differ), +`global_error_companion_order` (a companion solver of order 3 or lower) and +`global_error_final_solve` (the final forward solve does not repeat the accepted one). ## References @@ -74,13 +116,13 @@ function GlobalErrorEstimation( alg; equation = DefectCorrection, mode = InterpolatingMode, - companion_alg = alg, + companion_alg = nothing, autodiff = ADTypes.AutoForwardDiff(), gtol = nothing, maxiters = 6, safety = 0.8, - companion_abstol = gtol === nothing ? 1.0e-10 : min(gtol / 100, 1.0e-10), - companion_reltol = gtol === nothing ? 1.0e-8 : min(gtol / 100, 1.0e-8) + companion_abstol = nothing, + companion_reltol = nothing ) (equation === DefectCorrection || equation === ErrorTransport) || throw( ArgumentError("`equation` must be DefectCorrection or ErrorTransport") @@ -91,12 +133,36 @@ function GlobalErrorEstimation( options = _companion_options( gtol, maxiters, safety, companion_abstol, companion_reltol ) - return GlobalErrorEstimation(alg, companion_alg, equation, mode, autodiff, gtol, options) + return GlobalErrorEstimation( + alg, something(companion_alg, alg), equation, mode, autodiff, gtol, options + ) +end + +# The companion runs at much tighter tolerances than the forward problem, and a low-order +# solver meets them poorly or with very many steps. +function _warn_if_low_order_companion(alg::GlobalErrorEstimation, kwargs) + order = try + SciMLBase.alg_order(alg.companion_alg) + catch + return nothing + end + order isa Integer && order <= 3 && @SciMLMessage( + "GlobalErrorEstimation solves the companion with $(nameof(typeof(alg.companion_alg))) " * + "(order $order) at tight tolerances, which can give a poor estimate. Pass a " * + "higher-order `companion_alg`, for example `Tsit5()`, or `Rodas5P()` for a stiff " * + "problem or a DAE.", + _verbosity(kwargs), :global_error_companion_order + ) + return nothing end SciMLBase.allows_arbitrary_number_types(::GlobalErrorEstimation) = false SciMLBase.allowscomplex(::GlobalErrorEstimation) = false SciMLBase.isautodifferentiable(::GlobalErrorEstimation) = false +# The outer solve wraps `f` based on these traits, and the inner solves keep that wrapper. +# Without them, an implicit solver finds no dual signature. +SciMLBase.forwarddiffs_model(alg::GlobalErrorEstimation) = SciMLBase.forwarddiffs_model(alg.alg) +SciMLBase.forwarddiff_chunksize(alg::GlobalErrorEstimation) = SciMLBase.forwarddiff_chunksize(alg.alg) """ global_error_estimate(prob, alg::GlobalErrorEstimation; abstol=1e-6, reltol=1e-3, kwargs...) @@ -114,9 +180,32 @@ function global_error_estimate( companion_reltol = alg.options.companion_reltol, kwargs... ) + _warn_if_low_order_companion(alg, kwargs) + return _estimate(prob, alg, args...; abstol, reltol, companion_abstol, companion_reltol, kwargs...) +end + +SciMLBase.has_global_error(alg::GlobalErrorEstimation) = alg.equation === DefectCorrection + +# With `trajectory = true` (DefectCorrection only), returns `(; estimate, sol)`, where `sol` +# has the estimated error of each saved state in `global_error`. +function _estimate( + prob, alg::GlobalErrorEstimation, args...; + abstol, reltol, companion_abstol = alg.options.companion_abstol, + companion_reltol = alg.options.companion_reltol, trajectory = false, kwargs... + ) + companion_abstol, companion_reltol = _companion_tolerances( + companion_abstol, companion_reltol, abstol, alg.gtol + ) + simultaneous = alg.mode === SimultaneousMode + if alg.equation === DefectCorrection + estimate = simultaneous ? _defect_correction_estimate_streaming : _defect_correction_estimate + return estimate( + "GlobalErrorEstimation", prob, alg.alg, alg.companion_alg, args...; + abstol, reltol, companion_abstol, companion_reltol, trajectory, kwargs... + ) + end make_rhs = _companion_rhs_builder(alg.equation, alg.autodiff) - estimate = alg.mode === SimultaneousMode ? - _companion_error_estimate_streaming : _companion_error_estimate + estimate = simultaneous ? _companion_error_estimate_streaming : _companion_error_estimate return estimate( make_rhs, "GlobalErrorEstimation", prob, alg.alg, alg.companion_alg, args...; @@ -124,24 +213,68 @@ function global_error_estimate( ) end +# The returned solution under global error control with DefectCorrection. The accepted +# estimate ran on a copy of the callbacks, so this solve runs on the user's callbacks. In +# InterpolatingMode, only the forward solve runs again and reuses the accepted error +# trajectory, because a companion solve costs several forward solves. In SimultaneousMode, +# `accepted` is only the norm, so the full estimate runs again. +function _final_solve(prob, alg, accepted, args...; abstol, reltol, kwargs...) + full_estimate(kw) = _estimate(prob, alg, args...; abstol, reltol, trajectory = true, kw...).sol + accepted isa Number && return full_estimate(kwargs) + # Copy the callbacks before the forward solve changes them, for the fallback below. + unused = _with_copied_callback(kwargs) + # As in the estimate, the forward affects change their own copy of the parameters. + sol = SciMLBase.solve( + (@set prob.p = deepcopy(prob.p)), alg.alg, args...; + abstol, reltol, merge((; kwargs...), _DENSE_SOLVE_KWARGS)... + ) + sol.t == accepted.sol.t && sol.u == accepted.sol.u && + return @set sol.global_error = accepted.sol.global_error + @SciMLMessage( + "GlobalErrorEstimation: the final forward solve does not repeat the accepted one (a " * + "callback with state outside of it, or a solve that is not deterministic). The " * + "returned solution comes from a new estimate on a copy of the callbacks, so the " * + "callback objects saw another solve.", + _verbosity(kwargs), :global_error_final_solve + ) + return full_estimate(unused) +end + function SciMLBase.__solve( prob::SciMLBase.AbstractODEProblem, alg::GlobalErrorEstimation, args...; abstol = something(alg.gtol, 1.0e-6), reltol = something(alg.gtol, 1.0e-3), kwargs... ) - alg.gtol === nothing && throw( - ArgumentError("GlobalErrorEstimation requires a positive `gtol` constructor keyword") + alg.gtol === nothing && alg.equation !== DefectCorrection && throw( + ArgumentError( + "GlobalErrorEstimation with equation = ErrorTransport requires a positive " * + "`gtol` constructor keyword" + ) ) _validate_tolerances(abstol, reltol, "local") - _has_callbacks(kwargs) && - throw(ArgumentError("GlobalErrorEstimation does not currently support callbacks")) - estimator = (local_abstol, local_reltol) -> global_error_estimate( + alg.equation === DefectCorrection || !_has_callbacks(kwargs) || + throw(_callback_support_error("GlobalErrorEstimation")) + _warn_if_low_order_companion(alg, kwargs) + # The solve pipeline has already merged the problem's callback into `kwargs`. Remove it + # from the problem, or the inner solves merge it again and an affect runs twice. + prob = @set prob.kwargs = _without_callback(prob.kwargs) + alg.gtol === nothing && + return _estimate(prob, alg, args...; abstol, reltol, trajectory = true, kwargs...).sol + # Each estimate runs on a copy of the callbacks, so that the user's callback objects + # (for example the values of a SavingCallback) see only the final solve. Only + # InterpolatingMode keeps the trajectory; SimultaneousMode keeps its O(1) memory. + trajectory = alg.equation === DefectCorrection && alg.mode === InterpolatingMode + estimator = (local_abstol, local_reltol) -> _estimate( prob, alg, args...; - abstol = local_abstol, reltol = local_reltol, kwargs... + abstol = local_abstol, reltol = local_reltol, trajectory, _with_copied_callback(kwargs)... ) + final_solve = alg.equation === DefectCorrection ? + (local_abstol, local_reltol, accepted) -> _final_solve( + prob, alg, accepted, args...; abstol = local_abstol, reltol = local_reltol, kwargs... + ) : nothing return _refine_to_gtol( estimator, prob, alg.alg, alg.gtol, alg.options, args...; - abstol, reltol, kwargs... + final_solve, abstol, reltol, kwargs... ) end diff --git a/lib/GlobalDiffEq/src/glee/solve.jl b/lib/GlobalDiffEq/src/glee/solve.jl index d48142f25e0..a6b72249e40 100644 --- a/lib/GlobalDiffEq/src/glee/solve.jl +++ b/lib/GlobalDiffEq/src/glee/solve.jl @@ -136,7 +136,8 @@ end global_error_estimate(sol, i) Extract the global error estimate from a solution computed with a GLEE method -([`GLEE23`](@ref), [`GLEE24`](@ref), [`GLEE35`](@ref), [`MM5GEE`](@ref)). +([`GLEE23`](@ref), [`GLEE24`](@ref), [`GLEE35`](@ref), [`MM5GEE`](@ref)) or with +[`GlobalErrorEstimation`](@ref) and `equation = DefectCorrection`. These solvers report the global error through the standard SciMLBase interface: `sol.global_error` is a vector matching `sol.u`, with `sol.global_error[i]` the @@ -147,7 +148,8 @@ vector and `global_error_estimate(sol, i)` its `i`-th element. function global_error_estimate(sol::SciMLBase.AbstractODESolution) sol.global_error === nothing && throw( ArgumentError( - "global_error_estimate expects a solution produced by a GLEE method" + "global_error_estimate expects a solution produced by a GLEE method or " * + "by GlobalErrorEstimation with equation = DefectCorrection" ) ) return sol.global_error diff --git a/lib/GlobalDiffEq/src/richardson.jl b/lib/GlobalDiffEq/src/richardson.jl index 2ba0ad3d3c5..94eb6d8903c 100644 --- a/lib/GlobalDiffEq/src/richardson.jl +++ b/lib/GlobalDiffEq/src/richardson.jl @@ -15,12 +15,21 @@ SciMLBase.allowscomplex(alg::GlobalRichardson) = SciMLBase.allowscomplex(alg.alg) SciMLBase.isautodifferentiable(alg::GlobalRichardson) = SciMLBase.isautodifferentiable(alg.alg) +SciMLBase.forwarddiffs_model(alg::GlobalRichardson) = + SciMLBase.forwarddiffs_model(alg.alg) +SciMLBase.forwarddiff_chunksize(alg::GlobalRichardson) = + SciMLBase.forwarddiff_chunksize(alg.alg) + +_without_callback(kwargs) = (; (k => v for (k, v) in pairs(kwargs) if k !== :callback)...) function SciMLBase.__solve( prob::Union{SciMLBase.AbstractODEProblem, SciMLBase.AbstractDAEProblem}, alg::GlobalRichardson, args...; dt, kwargs... ) + # The solve pipeline has already merged the problem's callback into `kwargs`. Remove it + # from the problem, or the inner solves merge it a second time. + prob = @set prob.kwargs = _without_callback(prob.kwargs) opt = Dict(kwargs) otheropts = delete!(copy(opt), :dt) tstops = get(opt, :tstops, range(prob.tspan[1], stop = prob.tspan[2], step = dt)) diff --git a/lib/GlobalDiffEq/test/adjoint_events_tests.jl b/lib/GlobalDiffEq/test/adjoint_events_tests.jl new file mode 100644 index 00000000000..69c6be5ac15 --- /dev/null +++ b/lib/GlobalDiffEq/test/adjoint_events_tests.jl @@ -0,0 +1,627 @@ +using GlobalDiffEq, OrdinaryDiffEqTsit5, OrdinaryDiffEqRosenbrock, OrdinaryDiffEqNonlinearSolve +using SciMLSensitivity, LinearAlgebra, Test +using Random: Xoshiro +using DiffEqCallbacks: PresetTimeCallback, PeriodicCallback, SavingCallback, SavedValues, + TerminateSteadyState +import ADTypes, SciMLBase, SciMLLogging + +relerr(estimate, actual) = abs(estimate - actual) / abs(actual) + +# Return the estimate and the actual error of a plain solve at the final time. One +# projection per state gives the 2-norm of the linearized error, with no sampling error. +# The plain solve uses a copy because an affect can change the parameters in place. +function compare(prob, alg, tol, exact; adjoint_alg = Tsit5()) + ga = GlobalAdjoint(alg; samples = length(prob.u0), adjoint_alg) + estimate = adjoint_error_estimate(prob, ga; abstol = tol, reltol = tol) + sol = solve(deepcopy(prob), alg; abstol = tol, reltol = tol) + return estimate, norm(exact(sol.t[end]) .- sol.u[end]) +end + +# A parameter struct without `==` (`isequal` of two copies is false). +mutable struct RateParameter + k::Vector{Float64} +end +rate(p::RateParameter) = p.k[1] +rate(p::Vector{RateParameter}) = rate(only(p)) +rate(p) = p[1] +set_rate!(p::RateParameter) = (p.k[1] = 2.0) +set_rate!(p::Vector{RateParameter}) = set_rate!(only(p)) +set_rate!(p) = (p[1] = 2.0) + +# The tests use named right-hand sides, conditions and affects. Each new function type +# compiles the solver and the adjoint stack again, so the tests share these functions. +decay(u, p, t) = -p[1] .* u +decay_pair(u, p, t) = [-u[1], -u[2]] +rate_decay(u, p, t) = [-rate(p) * u[1], -u[2]] +oscillator(u, p, t) = [u[2], -u[1]] +damped(u, p, t) = [u[2], -u[1] - p[1] * u[2]] +lotka_volterra(u, p, t) = [1.5u[1] - u[1] * u[2], -3.0u[2] + u[1] * u[2]] +relax(u, p, t) = [0.45 - u[1]] +# A DAE with the algebraic equation z³ + z = x, and its solution z(x). +cubic!(du, u, p, t) = (du[1] = -p[1] * u[1]; du[2] = u[2]^3 + u[2] - u[1]; nothing) +cubic_z(x) = (q = sqrt(x^2 / 4 + 1 / 27); cbrt(x / 2 + q) + cbrt(x / 2 - q)) +cubic_dae(u0, tspan; kwargs...) = + ODEProblem(ODEFunction(cubic!; mass_matrix = Diagonal([1.0, 0.0])), u0, tspan, [1.0]; kwargs...) +# ReverseDiff cannot run these right-hand sides, because they write to a Float64 cache. +const CACHE = zeros(2) +cached_decay(u, p, t) = (CACHE .= (-u[1], -2u[2]); copy(CACHE)) +cached_dae(u, p, t) = (CACHE .= (-u[1], u[2] - 2u[1]); copy(CACHE)) + +below_half(u, t, i) = u[1] - 0.5 +double!(i) = (i.u .*= 2; nothing) +fd = Rodas5P(autodiff = ADTypes.AutoFiniteDiff()) + +# Problems that more than one test set uses. Each call gives new parameters, because some +# affects change the parameters in place. +reset_callback() = ContinuousCallback(below_half, i -> (i.u[1] = 1.0; nothing)) +saving_callback() = SavingCallback((u, t, i) -> copy(u), SavedValues(Float64, Vector{Float64})) +lotka_volterra_problem() = ODEProblem( + lotka_volterra, [1.0, 1.0], (0.0, 10.0); + callback = ContinuousCallback((u, t, i) -> u[1] - 3.0, i -> (i.u[1] = 1.5; nothing), nothing) +) +# The damping switches at each zero of x. +damped_problem() = ODEProblem( + damped, [1.0, 0.0], (0.0, 10.0), [0.1]; + callback = ContinuousCallback((u, t, i) -> u[1], i -> (i.p[1] = i.p[1] == 0.1 ? 1.0 : 0.1; nothing)) +) +walls_problem() = ODEProblem( + oscillator, [0.0, 1.0], (0.0, 10.0); + callback = VectorContinuousCallback( + (out, u, t, i) -> (out[1] = u[1] - 0.8; out[2] = u[1] + 0.8; nothing), + (i, idx) -> (i.u[2] = -0.9 * i.u[2]; nothing), 2 + ) +) +# x relaxes to 0.45 and resets to 1 at 0.46, with the period log(55). +relax_problem(t_end) = ODEProblem( + relax, [1.0], (0.0, t_end); + callback = ContinuousCallback((u, t, i) -> u[1] - 0.46, nothing, i -> (i.u[1] = 1.0; nothing)) +) +relax_exact(t) = (period = log(55); 0.45 + 0.55exp(-(t - fld(t, period) * period))) +# A GlobalAdjoint that returns the error of each saved state. +trajectory_alg(samples) = GlobalAdjoint(Tsit5(); gtol = 1.0, scope = TrajectoryError, samples, adjoint_alg = Tsit5()) +# Two DiscreteCallbacks that double u at t = 0.5. +doubling_pair() = CallbackSet( + DiscreteCallback((u, t, i) -> t == 0.5, double!), DiscreteCallback((u, t, i) -> t == 0.5, double!) +) + +@testset "GlobalAdjoint: jumps of the dense output" begin + # The dense output of Rosenbrock32 does not end at the state of the step, so the error + # identity needs the knot terms and the endpoint shift. + prob = ODEProblem(decay, [1.0], (0.0, 3.0), [1.0]) + @test relerr(compare(prob, Rosenbrock32(), 1.0e-5, t -> [exp(-t)])...) < 1.0e-5 +end + +@testset "GlobalAdjoint: events" begin + lv = lotka_volterra_problem() + lvref = solve(lv, Tsit5(); abstol = 1.0e-13, reltol = 1.0e-13) + # The estimate is a linearization, so its relative error falls with the tolerance. + @test relerr(compare(lv, Tsit5(), 1.0e-3, lvref)...) < 1.0e-2 + @test relerr(compare(lv, Tsit5(), 1.0e-5, lvref)...) < 1.0e-3 + + @test relerr(compare(relax_problem(10.0), Tsit5(), 1.0e-3, t -> [relax_exact(t)])...) < 1.0e-3 + + preset = ODEProblem( + decay, [1.0], (0.0, 3.5), [1.0]; + callback = DiscreteCallback((u, t, i) -> t in (1.0, 2.0, 3.0), double!), tstops = [1.0, 2.0, 3.0] + ) + @test relerr(compare(preset, Tsit5(), 1.0e-4, t -> [exp(-t) * 2.0^min(3, floor(Int, t))])...) < 1.0e-4 + + walls = walls_problem() + wallref = solve(walls, Tsit5(); abstol = 1.0e-13, reltol = 1.0e-13) + @test relerr(compare(walls, Tsit5(), 1.0e-5, wallref)...) < 1.0e-3 + + # Global error control with events. + controlled = solve(lv, GlobalAdjoint(Tsit5(); gtol = 1.0e-5, samples = 2, adjoint_alg = Tsit5())) + @test norm(lvref(10.0) .- controlled.u[end]) < 2.0e-5 +end + +@testset "GlobalAdjoint: DAEs" begin + smooth = cubic_dae([2.0, 1.0], (0.0, 5.0)) + # The Gauss adjoint problem has the exact Jacobian, so the adjoint solver can use AD. + estimate, actual = compare(smooth, Rodas5P(), 1.0e-4, t -> (x = 2exp(-t); [x, cubic_z(x)]); adjoint_alg = Rodas5P()) + @test relerr(estimate, actual) < 1.0e-4 + per = log(4.0) + reset = cubic_dae( + [2.0, 1.0], (0.0, 5.0); + callback = ContinuousCallback( + (u, t, i) -> u[1] - 0.5, nothing, i -> (i.u[1] = 2.0; nothing); + initializealg = BrownFullBasicInit() + ) + ) + exact = t -> (x = 2exp(-(t - fld(t, per) * per)); [x, cubic_z(x)]) + @test relerr(compare(reset, Rodas5P(), 1.0e-4, exact; adjoint_alg = fd)...) < 1.0e-3 + # Global error control through `solve`. The wrapper of `f` needs the dual signatures + # for the Jacobian of Rodas5P. + controlled = solve(smooth, GlobalAdjoint(Rodas5P(); gtol = 1.0e-4, adjoint_alg = fd)) + @test SciMLBase.successful_retcode(controlled) + controlled_error = norm(controlled.u[end] .- [2exp(-5.0), cubic_z(2exp(-5.0))]) + @test controlled_error < 1.0e-4 + @test relerr(controlled.global_error[end], controlled_error) < 1.0e-4 + # A mass matrix of `1.0I` counts as the identity. + scaled_identity = ODEProblem(ODEFunction(decay; mass_matrix = 1.0I), [1.0, 1.0], (0.0, 1.0), [1.0]) + plain = ODEProblem(decay, [1.0, 1.0], (0.0, 1.0), [1.0]) + @test adjoint_error_estimate(scaled_identity, GlobalAdjoint(Tsit5(); rng = Xoshiro(4))) ≈ + adjoint_error_estimate(plain, GlobalAdjoint(Tsit5(); rng = Xoshiro(4))) rtol = 1.0e-6 +end + +@testset "GlobalAdjoint: events that change the parameters" begin + # The rate changes from 1 to 3 when u reaches 1/2. The dual uses the parameters of each + # interval. The saltation matrix uses the vector field after the event (S = φ⁺/φ⁻ = 3). + ts = log(2.0) + switch_exact = t -> [t < ts ? exp(-t) : 0.5exp(-3(t - ts))] + switch = ODEProblem( + decay, [1.0], (0.0, 2.0), [1.0]; callback = ContinuousCallback(below_half, i -> (i.p[1] = 3.0; nothing)) + ) + @test relerr(compare(switch, Tsit5(), 1.0e-5, switch_exact)...) < 1.0e-5 + @test relerr(compare(switch, Rosenbrock32(), 1.0e-5, switch_exact)...) < 1.0e-5 + # An affect that replaces the parameter object. + replaced = remake(switch; callback = ContinuousCallback(below_half, i -> (i.p = [3.0]; nothing))) + @test relerr(compare(replaced, Tsit5(), 1.0e-5, switch_exact)...) < 1.0e-5 + + preset = ODEProblem( + decay, [1.0], (0.0, 2.0), [1.0]; + callback = DiscreteCallback((u, t, i) -> t == 1.0, i -> (i.p[1] = 3.0; nothing)), tstops = [1.0] + ) + @test relerr(compare(preset, Tsit5(), 1.0e-5, t -> [t < 1 ? exp(-t) : exp(-1) * exp(-3(t - 1))])...) < 1.0e-5 + + # The parameters and the saltation matrix change at each event. + damped = damped_problem() + dref = solve(deepcopy(damped), Tsit5(); abstol = 1.0e-13, reltol = 1.0e-13) + @test relerr(compare(damped, Tsit5(), 1.0e-5, dref)...) < 1.0e-4 + + # A DAE whose rate changes at the event. + dae = cubic_dae( + [2.0, cubic_z(2.0)], (0.0, 2.0); + callback = ContinuousCallback( + (u, t, i) -> u[1] - 1.0, i -> (i.p[1] = 3.0; nothing); initializealg = BrownFullBasicInit() + ) + ) + dae_exact = t -> (x = t < ts ? 2exp(-t) : exp(-3(t - ts)); [x, cubic_z(x)]) + @test relerr(compare(dae, Rodas5P(), 1.0e-5, dae_exact; adjoint_alg = fd)...) < 1.0e-4 +end + +@testset "GlobalAdjoint: TrajectoryError with events" begin + # The solution saves each event time twice. The second entry is the error after the + # event, e⁺ = S e⁻ with S = φ⁺/φ⁻ = 55, so its dual must cross the event. The first + # entry is the linearized error before the event, which cannot see the true reset. + sol = solve(relax_problem(6.0), trajectory_alg(1); abstol = 1.0e-5, reltol = 1.0e-5) + k = findfirst(i -> sol.t[i] == sol.t[i + 1], 1:(length(sol.t) - 1)) + @test relerr(sol.global_error[k + 1], abs(relax_exact(sol.t[k + 1]) - sol.u[k + 1][1])) < 1.0e-3 + @test sol.global_error[k + 1] > 10sol.global_error[k] + # The entries away from the event match the actual error. + away = [j for j in 2:length(sol.t) if abs(sol.t[j] - sol.t[k]) > 0.1] + @test all(j -> relerr(sol.global_error[j], abs(relax_exact(sol.t[j]) - sol.u[j][1])) < 1.0e-3, away) +end + +@testset "GlobalAdjoint: TrajectoryError with several calls or one-sided saves" begin + # Each case doubles u at t = 0.5. A callback can save before its affect, after it, or + # both; two callbacks at one time give four saves. Each entry is the state after the + # calls before it, so its exact value follows from its number of doublings. + cases = ( + DiscreteCallback((u, t, i) -> t == 0.5, double!; save_positions = (true, false)), + DiscreteCallback((u, t, i) -> t == 0.5, double!; save_positions = (false, true)), + doubling_pair(), + ) + for callback in cases + sol = solve( + ODEProblem(decay, [1.0], (0.0, 1.0), [1.0]), trajectory_alg(1); + callback, tstops = [0.5], abstol = 1.0e-6, reltol = 1.0e-6 + ) + doublings = [round(Int, log2(u[1] / exp(-t))) for (t, u) in zip(sol.t, sol.u)] + actual = [2.0^k * exp(-t) - u[1] for (k, t, u) in zip(doublings, sol.t, sol.u)] + @test all(abs(e - abs(a)) <= 1.0e-2 * abs(a) + 1.0e-14 for (e, a) in zip(sol.global_error, actual)) + end +end + +@testset "GlobalAdjoint: a later call at one time is linearized after the earlier ones" begin + # At t = 1 the first callback doubles x and the second adds x² to y. The derivative of + # the second affect must be 2x at the state after the doubling, not before it. The test + # checks both the VJP and the finite-difference saltation matrix. + pair = () -> CallbackSet( + DiscreteCallback((u, t, i) -> t == 1.0, i -> (i.u[1] *= 2; nothing)), + DiscreteCallback((u, t, i) -> t == 1.0, i -> (i.u[2] += i.u[1]^2; nothing)) + ) + prob = ODEProblem(decay_pair, [1.0, 1.0], (0.0, 2.0)) + tol = 1.0e-6 + ref = solve(prob, Tsit5(); callback = pair(), tstops = [1.0], abstol = 1.0e-13, reltol = 1.0e-13) + sol = solve(prob, Tsit5(); callback = pair(), tstops = [1.0], abstol = tol, reltol = tol) + actual = norm(ref.u[end] .- sol.u[end]) + for sensealg in (nothing, GaussKronrodAdjoint(autojacvec = false)) + ga = GlobalAdjoint(Tsit5(); samples = 2, adjoint_alg = Tsit5(), sensealg) + estimate = adjoint_error_estimate( + prob, ga; callback = pair(), tstops = [1.0], abstol = tol, reltol = tol + ) + @test relerr(estimate, actual) < 1.0e-3 + end +end + +@testset "GlobalAdjoint: a DiscreteCallback that a continuous event triggers" begin + # x' = -x, y' = -y. At x = 0.5 the continuous event doubles x and sets a flag. At the + # same step end, a DiscreteCallback adds x² to y and clears the flag. The discrete + # affect moves with the event time, so both affects need one shared saltation matrix. + tied = () -> CallbackSet( + ContinuousCallback(below_half, i -> (i.u[1] *= 2; i.p[1] = 1.0; nothing)), + DiscreteCallback((u, t, i) -> i.p[1] == 1.0, i -> (i.u[2] += i.u[1]^2; i.p[1] = 0.0; nothing)) + ) + prob = () -> ODEProblem(decay_pair, [1.0, 1.0], (0.0, 2.0), [0.0]) + tol = 1.0e-6 + ref = solve(prob(), Tsit5(); callback = tied(), abstol = 1.0e-13, reltol = 1.0e-13) + sol = solve(prob(), Tsit5(); callback = tied(), abstol = tol, reltol = tol) + actual = norm(ref.u[end] .- sol.u[end]) + for sensealg in (nothing, GaussKronrodAdjoint(autojacvec = false)) + ga = GlobalAdjoint(Tsit5(); samples = 2, adjoint_alg = Tsit5(), sensealg) + estimate = adjoint_error_estimate(prob(), ga; callback = tied(), abstol = tol, reltol = tol) + @test relerr(estimate, actual) < 1.0e-3 + end + # Both callbacks save before and after their affects, so the first event time has four + # entries. Each entry matches the exact branch before the event, between the two + # affects, or after both, continued to the event time. + trajectory = solve(prob(), trajectory_alg(2); callback = tied(), abstol = tol, reltol = tol) + at_event = findall(t -> abs(t - log(2)) < 1.0e-5, trajectory.t) + @test length(at_event) == 4 + te = trajectory.t[first(at_event)] + branches = ( + [exp(-te), exp(-te)], [2exp(-te), exp(-te)], [2exp(-te), exp(-te)], + [2exp(-te), exp(-te) + exp(-(te - log(2)))], + ) + for (j, branch) in zip(at_event, branches) + @test relerr(trajectory.global_error[j], norm(branch .- trajectory.u[j])) < 1.0e-3 + end +end + +@testset "GlobalAdjoint: parameter structs without ==, a SavingCallback at an event" begin + # The affect resets x and sets the rate k = 2. The check of the trial affect compares + # the parameters field by field, because `isequal` of two copies of a struct without + # `==` is false. The test uses such a struct and a vector of them. + reset = () -> ContinuousCallback(below_half, i -> (i.u[1] = 1.0; set_rate!(i.p); nothing)) + prob = p -> ODEProblem(rate_decay, [1.0, 1.0], (0.0, 2.0), p) + # A fixed rng gives the same directions in each estimate. + ga = () -> GlobalAdjoint(Tsit5(); samples = 2, adjoint_alg = Tsit5(), rng = Xoshiro(5)) + tol = 1.0e-6 + vector = adjoint_error_estimate(prob([1.0]), ga(); callback = reset(), abstol = tol, reltol = tol) + for p in (RateParameter([1.0]), [RateParameter([1.0])]) + @test adjoint_error_estimate(prob(p), ga(); callback = reset(), abstol = tol, reltol = tol) ≈ + vector rtol = 1.0e-6 + end + # With another adjoint method, a SavingCallback with such a struct is not an event. + quadrature = () -> GlobalAdjoint(Tsit5(); sensealg = QuadratureAdjoint(), rng = Xoshiro(5)) + @test adjoint_error_estimate(prob(RateParameter([1.0])), quadrature(); callback = saving_callback()) ≈ + adjoint_error_estimate(prob(RateParameter([1.0])), quadrature()) rtol = 1.0e-6 + # The SavingCallback also acts at the step end of the event. Its affect must stay out + # of the saltation matrix: it cannot take the dual numbers of the VJP. + both = CallbackSet(reset(), saving_callback()) + estimate = @test_logs min_level = Base.CoreLogging.Warn adjoint_error_estimate( + prob([1.0]), ga(); callback = both, abstol = tol, reltol = tol + ) + @test estimate ≈ vector rtol = 1.0e-6 +end + +@testset "GlobalAdjoint: affects that read the derivative, integer time span" begin + # In the trial run, get_du gives f(u), which differs from the forward derivative by + # about the local error. The check of the trial affect accepts this, and the source + # term counts the difference as an error of the forward affect. + kick = ContinuousCallback( + (u, t, i) -> u[1], i -> (i.u[2] = -0.8 * i.u[2] + 0.1 * SciMLBase.get_du(i)[1]; nothing) + ) + osc = ODEProblem(oscillator, [1.0, 0.0], (0.0, 6.0); callback = kick) + ref = solve(osc, Tsit5(); abstol = 1.0e-13, reltol = 1.0e-13) + estimate, actual = compare(osc, Tsit5(), 1.0e-7, t -> ref(t)) + @test relerr(estimate, actual) < 1.0e-4 + # An integer time span with an event gives the same estimate as a float one. + ga = GlobalAdjoint(Tsit5(); samples = 1, adjoint_alg = Tsit5()) + integer_span = ODEProblem(decay, [1.0], (0, 2), [1.0]) + float_span = ODEProblem(decay, [1.0], (0.0, 2.0), [1.0]) + @test adjoint_error_estimate(integer_span, ga; callback = reset_callback()) ≈ + adjoint_error_estimate(float_span, ga; callback = reset_callback()) rtol = 1.0e-6 +end + +@testset "GlobalAdjoint: callbacks that do not save after the affect" begin + # Without a save after the affect, the dense output of the next step starts from the + # state before the affect. The recorded solve must save the state after the event. + prob = ODEProblem(decay_pair, [1.0, 1.0], (0.0, 2.0)) + xexact(t) = 2.0^floor(t / log(2)) * exp(-t) + for save_positions in ((true, true), (false, true), (true, false), (false, false)) + callback = () -> ContinuousCallback(below_half, i -> (i.u[1] *= 2; nothing); save_positions) + sol = solve(prob, Tsit5(); callback = callback(), abstol = 1.0e-7, reltol = 1.0e-7) + estimate = adjoint_error_estimate( + prob, GlobalAdjoint(Tsit5(); samples = 2, adjoint_alg = Tsit5()); + callback = callback(), abstol = 1.0e-7, reltol = 1.0e-7 + ) + @test relerr(estimate, norm([xexact(2.0), exp(-2.0)] .- sol.u[end])) < 1.0e-3 + end +end + +@testset "GlobalAdjoint: event edge cases" begin + tol = 1.0e-6 + # Two DiscreteCallbacks at one time (their windows merge). + both = ODEProblem( + decay, [1.0], (0.0, 2.0), [1.0]; + callback = CallbackSet( + DiscreteCallback((u, t, i) -> t == 1.0, double!), + DiscreteCallback((u, t, i) -> t == 1.0, i -> (i.p[1] = 3.0; nothing)) + ), tstops = [1.0] + ) + @test relerr(compare(both, Tsit5(), tol, t -> [t < 1 ? exp(-t) : 2exp(-1) * exp(-3(t - 1))])...) < 1.0e-5 + # A VectorContinuousCallback with two components that have the same root. + vcc = ODEProblem( + rate_decay, [1.0, 1.0], (0.0, 2.0), [1.0]; + callback = VectorContinuousCallback( + (out, u, t, i) -> (out[1] = u[1] - 0.5; out[2] = u[1] - 0.5; nothing), + (i, idx) -> (idx == 1 ? (i.u[2] *= 2) : (i.p[1] = 3.0); nothing), 2 + ) + ) + vccref = solve(deepcopy(vcc), Tsit5(); abstol = 1.0e-13, reltol = 1.0e-13) + @test relerr(compare(vcc, Tsit5(), tol, vccref)...) < 1.0e-5 + # An event in the first step (t ≈ 0.001). The reset makes the event repeat. + t1, per = log(1 / 0.999), log(2 / 0.999) + first_step = ODEProblem( + decay, [1.0], (0.0, 2.0), [1.0]; + callback = ContinuousCallback((u, t, i) -> u[1] - 0.999, i -> (i.u[1] = 2.0; nothing)) + ) + @test relerr(compare(first_step, Tsit5(), tol, t -> [2exp(-((t - t1) - fld(t - t1, per) * per))])...) < 1.0e-5 + # An event at the final time: the saved state is after the affect. + at_end = ODEProblem( + decay, [1.0], (0.0, 2.0), [1.0]; + callback = DiscreteCallback((u, t, i) -> t == 2.0, double!), tstops = [2.0] + ) + @test relerr(compare(at_end, Tsit5(), tol, t -> [2exp(-t)])...) < 1.0e-5 + # DiffEqCallbacks callbacks with internal state, and a SavingCallback that does not + # change the state. + preset = ODEProblem(decay, [1.0], (0.0, 3.5), [1.0]; callback = PresetTimeCallback([1.0, 2.0, 3.0], double!)) + @test relerr(compare(preset, Tsit5(), tol, t -> [exp(-t) * 2.0^min(3, floor(Int, t))])...) < 1.0e-5 + periodic = ODEProblem( + decay, [1.0], (0.0, 2.0), [1.0]; callback = PeriodicCallback(i -> (i.u .*= 1.5; nothing), 0.6) + ) + @test relerr(compare(periodic, Tsit5(), tol, t -> [1.5^3 * exp(-t)])...) < 1.0e-5 + saving = ODEProblem( + oscillator, [1.0, 0.0], (0.0, 5.0); + callback = SavingCallback((u, t, i) -> u[1], SavedValues(Float64, Float64)) + ) + @test relerr(compare(saving, Tsit5(), tol, t -> [cos(t), -sin(t)])...) < 1.0e-5 + # Terminating callbacks: the estimate is the error at the forward event. + for callback in (ContinuousCallback(below_half, terminate!), TerminateSteadyState(1.0e-4, 1.0e-4)) + stop = ODEProblem(decay, [1.0], (0.0, 100.0), [1.0]; callback) + @test relerr(compare(stop, Tsit5(), tol, t -> [exp(-t)])...) < 1.0e-5 + end +end + +@testset "GlobalAdjoint: VJP through the events" begin + # The default autojacvec (ReverseDiffVJP) computes Sᵀλ from AD products of the affect + # and the condition. autojacvec = false uses the finite-difference saltation matrix. + # The two estimates agree. + estimate(prob, autojacvec; tol = 1.0e-6) = adjoint_error_estimate( + prob, GlobalAdjoint( + Tsit5(); samples = length(prob.u0), adjoint_alg = Tsit5(), + sensealg = GaussKronrodAdjoint(; autojacvec) + ); + abstol = tol, reltol = tol + ) + problems = ( + lotka_volterra_problem(), damped_problem(), walls_problem(), + ODEProblem(decay, [1.0], (0.0, 1.0), [1.0]; callback = doubling_pair(), tstops = [0.5]), + ) + for prob in problems + @test relerr(estimate(prob, ReverseDiffVJP()), estimate(prob, false)) < 1.0e-5 + end + + # ForwardDiff and ReverseDiff cannot run this affect, so the saltation matrix uses + # finite differences and a warning says so. + rigid = ODEProblem( + decay, [1.0], (0.0, 2.0), [1.0]; + callback = ContinuousCallback(below_half, i -> (i.u[1] = 2 * Float64(i.u[1]); nothing)) + ) + fallback = @test_logs (:warn, r"finite-difference saltation") match_mode = :any estimate(rigid, ReverseDiffVJP()) + # The two runs differ only in the VJP of the adjoint ODE, so they agree to the adjoint + # tolerance. + @test fallback ≈ estimate(rigid, false) rtol = 1.0e-6 +end + +@testset "GlobalAdjoint: the quadrature of the error integral" begin + # The sensealg selects the quadrature of the error integral: Gauss-Kronrod (the + # default) or Gauss-Legendre. An InterpolatingAdjoint is the GaussKronrodAdjoint with + # the same settings. All estimate the error relative to its size, also for a small + # error, with events and for a DAE. A fixed rng gives the same directions in each + # estimate. + lv = lotka_volterra_problem() + lvref = solve(lv, Tsit5(); abstol = 1.0e-13, reltol = 1.0e-13) + small = ODEProblem(decay, [1.0], (0.0, 3.0), [1.0]) + dae = cubic_dae( + [2.0, 1.0], (0.0, 5.0); + callback = ContinuousCallback( + (u, t, i) -> u[1] - 0.5, nothing, i -> (i.u[1] = 2.0; nothing); + initializealg = BrownFullBasicInit() + ) + ) + dae_exact = t -> (x = 2exp(-(t - fld(t, log(4.0)) * log(4.0))); [x, cubic_z(x)]) + for (prob, alg, adjoint_alg, tol, exact) in ( + (lv, Tsit5(), Tsit5(), 1.0e-6, lvref), + (small, Rosenbrock32(), Tsit5(), 1.0e-8, t -> [exp(-t)]), + (dae, Rodas5P(), fd, 1.0e-6, dae_exact), + ) + sol = solve(deepcopy(prob), alg; abstol = tol, reltol = tol) + actual = norm(exact(sol.t[end]) .- sol.u[end]) + estimate(sensealg, adjoint_alg = adjoint_alg) = adjoint_error_estimate( + prob, GlobalAdjoint(alg; samples = length(prob.u0), adjoint_alg, rng = Xoshiro(8), sensealg); + abstol = tol, reltol = tol + ) + kronrod = estimate(nothing) + @test kronrod == estimate(GaussKronrodAdjoint(autojacvec = ReverseDiffVJP())) + @test relerr(kronrod, actual) < 1.0e-3 + @test relerr(estimate(GaussAdjoint(autojacvec = ReverseDiffVJP())), kronrod) < 1.0e-4 + @test estimate(InterpolatingAdjoint(autojacvec = ReverseDiffVJP())) == kronrod + # With the forward solver as adjoint solver (the default `adjoint_alg`), + # Gauss-Kronrod stays accurate also for a solver of low order. + @test relerr(estimate(nothing, alg), kronrod) < 1.0e-4 + end + # Gauss-Legendre with an adjoint solver of order below 5 is allowed, but less accurate, + # so the solve warns. The `global_error_companion_order` toggle controls the warning. + coarse = GlobalAdjoint(Rosenbrock32(); sensealg = GaussAdjoint(autojacvec = ReverseDiffVJP())) + @test_logs (:warn, r"GaussAdjoint integrates the error") match_mode = :any @test( + isfinite(adjoint_error_estimate(small, coarse)) + ) + @test_logs min_level = Base.CoreLogging.Warn adjoint_error_estimate( + small, coarse; verbose = DEVerbosity(global_error_companion_order = SciMLLogging.Silent()) + ) + @test_logs min_level = Base.CoreLogging.Warn adjoint_error_estimate( + small, GlobalAdjoint(Rosenbrock32(); sensealg = GaussAdjoint(autojacvec = ReverseDiffVJP()), adjoint_alg = Tsit5()) + ) +end + +@testset "GlobalAdjoint: VJP through the events of a DAE" begin + # The VJP form goes through the algebraic projection after each affect with the implicit + # function theorem. It agrees with the finite-difference saltation matrix. Without a + # warning, no event fell back to finite differences. + @test GlobalDiffEq._algebraic_solver() !== nothing + estimate(prob, autojacvec) = adjoint_error_estimate( + prob, GlobalAdjoint( + Rodas5P(); samples = 1, adjoint_alg = fd, rng = Xoshiro(6), + sensealg = GaussKronrodAdjoint(; autojacvec) + ); + abstol = 1.0e-6, reltol = 1.0e-6 + ) + problems = ( + # A reset of x, with the reinitialization of z. + cubic_dae( + [2.0, 1.0], (0.0, 3.0); + callback = ContinuousCallback( + below_half, nothing, i -> (i.u[1] = 2.0; nothing); initializealg = BrownFullBasicInit() + ) + ), + # The rate changes at the event. + cubic_dae( + [2.0, cubic_z(2.0)], (0.0, 2.0); + callback = ContinuousCallback( + (u, t, i) -> u[1] - 1.0, i -> (i.p[1] = 3.0; nothing); initializealg = BrownFullBasicInit() + ) + ), + # Two calls at one time: the second sees the state after the first. + cubic_dae( + [2.0, cubic_z(2.0)], (0.0, 2.0); + callback = CallbackSet( + DiscreteCallback((u, t, i) -> t == 1.0, double!; initializealg = BrownFullBasicInit()), + DiscreteCallback( + (u, t, i) -> t == 1.0, i -> (i.p[1] = 3.0; nothing); initializealg = BrownFullBasicInit() + ) + ), tstops = [1.0] + ), + # The affect reads the algebraic variable, so A contains dz/dx. + cubic_dae( + [2.0, cubic_z(2.0)], (0.0, 2.0); + callback = ContinuousCallback( + below_half, i -> (i.u[1] = 1.0 + i.u[2]; nothing); initializealg = BrownFullBasicInit() + ) + ), + ) + for prob in problems + vjp = @test_logs min_level = Base.CoreLogging.Warn estimate(prob, ReverseDiffVJP()) + @test relerr(vjp, estimate(prob, false)) < 1.0e-5 + end +end + +@testset "GlobalAdjoint: affect arguments, callback state, initialization, default VJP" begin + # The affect of a VectorContinuousCallback gets the fired components with their signs, + # as in the forward solve. + walls = () -> ODEProblem( + oscillator, [0.0, 1.0], (0.0, 10.0); + callback = VectorContinuousCallback( + (out, u, t, i) -> (out[1] = u[1] - 0.8; out[2] = u[1] + 0.8; nothing), + (i, ev) -> (ev[1] != 0 && (i.u[2] = -0.9 * i.u[2]); ev[2] != 0 && (i.u[2] = -0.8 * i.u[2]); nothing), 2 + ) + ) + wallref = solve(walls(), Tsit5(); abstol = 1.0e-13, reltol = 1.0e-13) + @test relerr(compare(walls(), Tsit5(), 1.0e-6, wallref)...) < 1.0e-4 + + # This affect has internal state: it halves the velocity only at its second call. As in + # SciMLSensitivity, the trial runs use one copy of the callback for the whole solve, so + # the check rejects such an affect. + calls = Ref(0) + counted = ODEProblem( + oscillator, [1.0, 0.0], (0.0, 10.0); + callback = ContinuousCallback( + (u, t, i) -> u[1], i -> (calls[] += 1; calls[] == 2 && (i.u[2] *= 0.5); nothing) + ) + ) + @test_throws ArgumentError adjoint_error_estimate( + counted, GlobalAdjoint(Tsit5(); samples = 2, adjoint_alg = Tsit5()); abstol = 1.0e-6, reltol = 1.0e-6 + ) + # This affect halves the velocity when its flag is true, then flips the flag. The calls + # that change the state all see the flag true, as the trial copy does, so the check + # passes. Each trial run starts from its own copy, so all finite differences of one + # saltation matrix see the same flag. + toggling = () -> ( + flag = Ref(true); + ContinuousCallback((u, t, i) -> u[1], i -> (flag[] && (i.u[2] *= 0.5); flag[] = !flag[]; nothing)) + ) + swing = ODEProblem(oscillator, [1.0, 0.0], (0.0, 10.0)) + swing_ref = solve(swing, Tsit5(); callback = toggling(), abstol = 1.0e-13, reltol = 1.0e-13) + swing_sol = solve(swing, Tsit5(); callback = toggling(), abstol = 1.0e-6, reltol = 1.0e-6) + for sensealg in (nothing, GaussKronrodAdjoint(autojacvec = false)) + estimate = adjoint_error_estimate( + swing, GlobalAdjoint(Tsit5(); samples = 2, adjoint_alg = Tsit5(), sensealg); + callback = toggling(), abstol = 1.0e-6, reltol = 1.0e-6 + ) + @test relerr(estimate, norm(swing_ref.u[end] .- swing_sol.u[end])) < 1.0e-4 + end + + # An affect at initialization: the true solution starts with the same affect. + initial = ODEProblem( + decay, [1.0], (0.0, 1.0), [1.0]; callback = PeriodicCallback(double!, 10.0; initial_affect = true) + ) + @test relerr(compare(initial, Tsit5(), 1.0e-6, t -> [2exp(-t)])...) < 1.0e-5 + + # ReverseDiff cannot run this right-hand side, so the default sensealg uses a + # numerical VJP and warns. + cached = ODEProblem(cached_decay, [1.0, 1.0], (0.0, 1.0)) + estimate, actual = @test_logs (:warn, r"numerical VJP") match_mode = :any compare( + cached, Tsit5(), 1.0e-6, t -> [exp(-t), exp(-2t)] + ) + @test relerr(estimate, actual) < 1.0e-4 + # The `global_error_ad_fallback` toggle of `DEVerbosity` silences the warning. + @test_logs min_level = Base.CoreLogging.Warn adjoint_error_estimate( + cached, GlobalAdjoint(Tsit5()); abstol = 1.0e-6, reltol = 1.0e-6, + verbose = DEVerbosity(global_error_ad_fallback = SciMLLogging.Silent()) + ) + # The same fallback for a DAE. The Jacobian at the final time uses finite differences, + # and the numerical VJP uses an in-place right-hand side. The finite-difference Jacobian + # of SciMLSensitivity fails for an out-of-place DAE. + dae_cached = ODEProblem(ODEFunction(cached_dae; mass_matrix = Diagonal([1.0, 0.0])), [1.0, 2.0], (0.0, 1.0)) + estimate, actual = @test_logs (:warn, r"numerical VJP") match_mode = :any compare( + dae_cached, fd, 1.0e-6, t -> [exp(-t), 2exp(-t)]; adjoint_alg = fd + ) + @test relerr(estimate, actual) < 1.0e-3 +end + +@testset "GlobalAdjoint: the message at a grazing event" begin + # At a grazing event the saltation matrix has no time-shift term, and a warning says so. + # The `global_error_grazing_event` toggle of `DEVerbosity` silences it. + @test_logs (:warn, r"t = 1.5 is grazing") GlobalDiffEq._grazing_message(1.5, DEVerbosity()) + @test_logs GlobalDiffEq._grazing_message( + 1.5, DEVerbosity(global_error_grazing_event = SciMLLogging.Silent()) + ) +end + +@testset "GlobalAdjoint: unsupported combinations" begin + prob = ODEProblem(decay, [1.0], (0.0, 1.0), [1.0]; callback = ContinuousCallback(below_half, i -> nothing)) + @test_throws ArgumentError solve(prob, GlobalAdjoint(Tsit5(); gtol = 1.0e-4, control = StepGridRefinement)) + # With another adjoint method, an event that changes the state fails at its call, and a + # DAE fails before the solve. A SavingCallback changes nothing, so it is not an event. + quadrature = GlobalAdjoint(Tsit5(); sensealg = QuadratureAdjoint(), samples = 1) + plain = ODEProblem(decay, [1.0], (0.0, 1.0), [1.0]) + @test_throws ArgumentError adjoint_error_estimate(plain, quadrature; callback = reset_callback()) + @test_throws ArgumentError adjoint_error_estimate( + cubic_dae([2.0, 1.0], (0.0, 1.0)), GlobalAdjoint(Rodas5P(); sensealg = QuadratureAdjoint()) + ) + @test adjoint_error_estimate(plain, quadrature; callback = saving_callback()) ≈ + adjoint_error_estimate(plain, quadrature) rtol = 1.0e-6 + @test_throws ArgumentError adjoint_error_estimate( + prob, GlobalAdjoint(Tsit5(); sensealg = InterpolatingAdjoint(checkpointing = true)) + ) + full = ODEProblem( + ODEFunction(decay; mass_matrix = [1.0 0.5; 0.0 0.0]), [1.0, 1.0], (0.0, 1.0), [1.0] + ) + @test_throws ArgumentError adjoint_error_estimate(full, GlobalAdjoint(Rodas5P())) + # A scaled identity fails, because the error identity assumes M = I. + scaled = ODEProblem(ODEFunction(decay; mass_matrix = 2.0I), [1.0, 1.0], (0.0, 1.0), [1.0]) + @test_throws ArgumentError adjoint_error_estimate(scaled, GlobalAdjoint(Rodas5P())) + @test_throws ArgumentError solve(scaled, GlobalAdjoint(Rodas5P(); gtol = 1.0e-4)) +end diff --git a/lib/GlobalDiffEq/test/adjoint_tests.jl b/lib/GlobalDiffEq/test/adjoint_tests.jl index cb56a41cb14..277e47e8e0d 100644 --- a/lib/GlobalDiffEq/test/adjoint_tests.jl +++ b/lib/GlobalDiffEq/test/adjoint_tests.jl @@ -118,3 +118,16 @@ end @test abs(grid_sol.u[end][1] - exact(tspan[2])) <= gtol @test grid_sol.global_error[end] <= gtol end + +@testset "GlobalAdjoint: the reported endpoint error under global error control" begin + # The reported endpoint error is the estimate of the accepted iteration. Here the first + # iteration is accepted, so the reported error is the estimate at + # abstol = reltol = gtol. + decay = ODEProblem((u, p, t) -> [-u[1], -2u[2]], [1.0, 1.0], (0.0, 3.0)) + # A fixed rng gives the same directions in both estimates. + ga = () -> GlobalAdjoint(Tsit5(); gtol = 1.0e-4, samples = 2, adjoint_alg = Tsit5(), rng = Xoshiro(7)) + sol = solve(decay, ga()) + direct = adjoint_error_estimate(decay, ga(); abstol = 1.0e-4, reltol = 1.0e-4) + @test direct < 1.0e-4 + @test sol.global_error[end] ≈ direct rtol = 1.0e-8 +end diff --git a/lib/GlobalDiffEq/test/algorithm_traits_tests.jl b/lib/GlobalDiffEq/test/algorithm_traits_tests.jl index 3b1ab1bea9e..33ddfe6eb3e 100644 --- a/lib/GlobalDiffEq/test/algorithm_traits_tests.jl +++ b/lib/GlobalDiffEq/test/algorithm_traits_tests.jl @@ -1,5 +1,6 @@ using GlobalDiffEq, OrdinaryDiffEqTsit5 -using OrdinaryDiffEqSSPRK +using OrdinaryDiffEqSSPRK, OrdinaryDiffEqRosenbrock +using ADTypes: AutoForwardDiff using Test import DiffEqBase: SciMLBase @@ -11,4 +12,17 @@ import DiffEqBase: SciMLBase SciMLBase.allows_arbitrary_number_types(alg_inner) @test SciMLBase.allowscomplex(alg) == SciMLBase.allowscomplex(alg_inner) @test SciMLBase.isautodifferentiable(alg) == SciMLBase.isautodifferentiable(alg_inner) + + # The outer solve wraps f according to these traits. An implicit inner solver needs the + # dual signatures for its Jacobian. + for inner in (SSPRK33(), Rodas5P(), Rodas5P(autodiff = AutoForwardDiff(chunksize = 2))) + for alg in (GlobalRichardson(inner), GlobalErrorEstimation(inner), GlobalAdjoint(inner)) + @test SciMLBase.forwarddiffs_model(alg) == SciMLBase.forwarddiffs_model(inner) + @test SciMLBase.forwarddiff_chunksize(alg) == SciMLBase.forwarddiff_chunksize(inner) + end + end + @test SciMLBase.forwarddiffs_model(GlobalErrorEstimation(Rodas5P())) + decay!(du, u, p, t) = (du[1] = -u[1]; nothing) + prob = ODEProblem(decay!, [1.0], (0.0, 1.0)) + @test SciMLBase.successful_retcode(solve(prob, GlobalRichardson(Rodas5P()); dt = 0.01)) end diff --git a/lib/GlobalDiffEq/test/basic_functionality_tests.jl b/lib/GlobalDiffEq/test/basic_functionality_tests.jl index 1ca65ef31f9..94560dcf6e0 100644 --- a/lib/GlobalDiffEq/test/basic_functionality_tests.jl +++ b/lib/GlobalDiffEq/test/basic_functionality_tests.jl @@ -27,4 +27,10 @@ using Test prob, GlobalRichardson(SSPRK33()), dt = 0.2, reltol = 1.0e-12, abstol = 0 ).(1:10) @test norm(ve - v0) / norm(v0) < 1.0e-10 + + # The callback of the problem acts once, not a second time through the solve keywords. + double = DiscreteCallback((u, t, i) -> t == 0.5, i -> (i.u[1] *= 2; nothing)) + decay = ODEProblem((u, p, t) -> -u, [1.0], (0.0, 1.0); callback = double, tstops = [0.5]) + sol = solve(decay, GlobalRichardson(SSPRK33()); dt = 0.05, reltol = 1.0e-10, abstol = 0) + @test sol.u[end][1] ≈ 2exp(-1.0) rtol = 1.0e-6 end diff --git a/lib/GlobalDiffEq/test/companion_events_tests.jl b/lib/GlobalDiffEq/test/companion_events_tests.jl new file mode 100644 index 00000000000..8b7d55bd564 --- /dev/null +++ b/lib/GlobalDiffEq/test/companion_events_tests.jl @@ -0,0 +1,519 @@ +using GlobalDiffEq, OrdinaryDiffEqTsit5, OrdinaryDiffEqRosenbrock, LinearAlgebra +# Loads the BrownFullBasicInit method. +import OrdinaryDiffEqNonlinearSolve +using DiffEqCallbacks: PresetTimeCallback, PeriodicCallback, SavingCallback, SavedValues, + TerminateSteadyState +using Test +import SciMLBase, SciMLLogging + +# DefectCorrection with callbacks, in both modes. The companion applies the user +# callbacks to the true state, so the estimate follows the true event times, which differ +# from the forward ones. Each case has an exact solution. +const T = 2.0 +const MODES = (InterpolatingMode, SimultaneousMode) +decay!(du, u, p, t) = (du[1] = -u[1]; nothing) +decay(u, p, t) = [-u[1]] +switch!(du, u, p, t) = (du[1] = p[1] - u[1]; nothing) +decay_and_switch!(du, u, p, t) = (du[1] = -u[1]; du[2] = p[1] - u[2]; nothing) +# x' = -x, 0 = z - 2x: a mass-matrix DAE. +decay_dae!(du, u, p, t) = (du[1] = -u[1]; du[2] = u[2] - 2u[1]; nothing) +# x' = -x, 0 = z + z^3 - 2x: a mass-matrix DAE with a nonlinear constraint. +cubic_dae!(du, u, p, t) = (du[1] = -u[1]; du[2] = u[2] + u[2]^3 - 2u[1]; nothing) +function cubic_z(x) + z = x + for _ in 1:50 + z -= (z + z^3 - 2x) / (1 + 3z^2) + end + return z +end +reset_callback() = ContinuousCallback((u, t, i) -> u[1] - 0.5, i -> (i.u[1] = 1.0; nothing)) +# x' = -x from x = 1, with a reset to 1 at x = 0.5. The resets are at log 2 and 2 log 2. +const RESET_EXACT = exp(-(T - 2log(2))) + +# A VectorContinuousCallback affect gets the index of the fired component, or a mask of +# the components that fired at the same time. +fired(index::Integer, k) = index == k +fired(mask, k) = !iszero(mask[k]) +function reset_and_switch!(integrator, index) + fired(index, 1) && (integrator.u[1] = 1.0) + fired(index, 2) && (integrator.p[1] = 0.0) + return nothing +end + +# (name, problem, callback, exact u(T), extra solve keywords, forward solver) +const CASES = [ + ("reset", () -> ODEProblem(decay!, [1.0], (0.0, T)), reset_callback, [RESET_EXACT], (;), Tsit5()), + ( + "reset, implicit solver", () -> ODEProblem(decay!, [1.0], (0.0, T)), reset_callback, + [RESET_EXACT], (;), Rodas5P(), + ), + ("reset, out of place", () -> ODEProblem(decay, [1.0], (0.0, T)), reset_callback, [RESET_EXACT], (;), Tsit5()), + ( + # The affect keeps the algebraic state consistent. + "reset, mass-matrix DAE", + () -> ODEProblem( + ODEFunction(decay_dae!; mass_matrix = Diagonal([1.0, 0.0])), [1.0, 2.0], (0.0, T) + ), + () -> ContinuousCallback( + (u, t, i) -> u[1] - 0.5, i -> (i.u[1] = 1.0; i.u[2] = 2.0; nothing) + ), + [RESET_EXACT, 2RESET_EXACT], (;), Rodas5P(), + ), + ( + # The affect resets only x, and the reinitialization solves the nonlinear + # constraint for z. The companion must reinitialize the true state too. + "reset, reinitialized nonlinear DAE", + () -> ODEProblem( + ODEFunction(cubic_dae!; mass_matrix = Diagonal([1.0, 0.0])), + [1.0, cubic_z(1.0)], (0.0, T) + ), + () -> ContinuousCallback( + (u, t, i) -> u[1] - 0.5, i -> (i.u[1] = 1.0; nothing); + initializealg = BrownFullBasicInit() + ), + [RESET_EXACT, cubic_z(RESET_EXACT)], (;), Rodas5P(), + ), + ( + # The companion runs on a copy of the callback, so its counter starts at zero. + "affect with internal state", + () -> ODEProblem(decay!, [1.0], (0.0, T)), + function () + fired = Ref(0) + return ContinuousCallback( + (u, t, i) -> u[1] - 0.5, + i -> (fired[] == 0 && (i.u[1] = 1.0); fired[] += 1; nothing) + ) + end, + [exp(-(T - log(2)))], (;), Tsit5(), + ), + ( + # Up-crossing of x = 1 at log 2: jump to 1.5 and switch off the source. + # Down-crossing at log 2 + log 1.5: switch the source to 0.5. + "up- and down-crossing affects", + () -> ODEProblem(switch!, [0.0], (0.0, T), [2.0]), + () -> ContinuousCallback( + (u, t, i) -> u[1] - 1.0, + i -> (i.u[1] = 1.5; i.p[1] = 0.0; nothing), i -> (i.p[1] = 0.5; nothing) + ), + [0.5 + 0.5exp(-(T - log(2) - log(1.5)))], (;), Tsit5(), + ), + ( + "parameter switch", () -> ODEProblem(switch!, [0.0], (0.0, T), [2.0]), + () -> ContinuousCallback((u, t, i) -> u[1] - 1.0, i -> (i.p[1] = 0.0; nothing)), + [exp(-(T - log(2)))], (;), Tsit5(), + ), + ( + "vector callback", () -> ODEProblem(decay_and_switch!, [1.0, 0.0], (0.0, T), [2.0]), + () -> VectorContinuousCallback( + (out, u, t, i) -> (out[1] = u[1] - 0.5; out[2] = u[2] - 1.0; nothing), + reset_and_switch!, 2 + ), + [exp(-(T - 2log(2))), exp(-(T - log(2)))], (;), Tsit5(), + ), + ( + "fixed-time doubling", () -> ODEProblem(decay!, [1.0], (0.0, T)), + () -> DiscreteCallback((u, t, i) -> t == 0.5, i -> (i.u[1] *= 2; nothing)), + [2exp(-T)], (; tstops = [0.5]), Tsit5(), + ), + ( + "PresetTimeCallback", () -> ODEProblem(decay!, [1.0], (0.0, T)), + () -> PresetTimeCallback(0.5, i -> (i.u[1] *= 2; nothing)), [2exp(-T)], (;), Tsit5(), + ), + ( + # Its state is in task-local storage. In SimultaneousMode, the copy in the companion + # must not share that state with the forward solve. Events at 0.6, 1.2 and 1.8. + "PeriodicCallback", () -> ODEProblem(decay!, [1.0], (0.0, T)), + () -> PeriodicCallback(i -> (i.u[1] *= 1.5; nothing), 0.6), [1.5^3 * exp(-T)], (;), Tsit5(), + ), + ( + # The saved state at T is after the affect; P(T⁻) is before it. + "event at the final time", () -> ODEProblem(decay!, [1.0], (0.0, T)), + () -> DiscreteCallback((u, t, i) -> t == T, i -> (i.u[1] *= 2; nothing)), + [2exp(-T)], (;), Tsit5(), + ), +] + +@testset "Estimate with events: $name, $mode" for (name, make_prob, make_callback, exact, extra, solver) in CASES, + mode in MODES + + alg = GlobalErrorEstimation(solver; mode) + for tol in (1.0e-4, 1.0e-6) + prob = make_prob() + p_before = deepcopy(prob.p) + # In the VectorContinuousCallback case, both components fire at the same time. At + # 1e-4 the forward solve can miss one of them, and the estimate then warns. + est = global_error_estimate( + prob, alg; callback = make_callback(), abstol = tol, reltol = tol, extra... + ) + # The estimate does not change the user's parameters. + @test prob.p == p_before + sol = solve(make_prob(), solver; callback = make_callback(), abstol = tol, reltol = tol, extra...) + @test est / norm(exact - sol.u[end]) ≈ 1 rtol = 1.0e-2 + end + + # The callback can also come from the problem keywords. + prob = make_prob() + in_problem = ODEProblem(prob.f, prob.u0, prob.tspan, prob.p; callback = make_callback()) + est = global_error_estimate(in_problem, alg; abstol = 1.0e-6, reltol = 1.0e-6, extra...) + sol = solve(make_prob(), solver; callback = make_callback(), abstol = 1.0e-6, reltol = 1.0e-6, extra...) + @test est / norm(exact - sol.u[end]) ≈ 1 rtol = 1.0e-2 + + # Global error control with events. + gtol = 1.0e-7 + sol = solve( + make_prob(), GlobalErrorEstimation(solver; mode, gtol); + callback = make_callback(), abstol = 1.0e-3, reltol = 1.0e-3, extra... + ) + @test SciMLBase.successful_retcode(sol) + @test norm(exact - sol.u[end]) <= gtol +end + +@testset "A missed event is estimated and reported: $mode" for mode in MODES + # Both components of the vector callback cross at t = log 2. At 1e-4 the forward solve + # can fire only the first one, which depends on rounding and on the Julia version. Its + # second state is then wrong by O(1); the companion fires both, and the estimator warns. + prob = ODEProblem(decay_and_switch!, [1.0, 0.0], (0.0, T), [2.0]) + callback = VectorContinuousCallback( + (out, u, t, i) -> (out[1] = u[1] - 0.5; out[2] = u[2] - 1.0; nothing), + reset_and_switch!, 2 + ) + sol = solve(deepcopy(prob), Tsit5(); callback = deepcopy(callback), abstol = 1.0e-4, reltol = 1.0e-4) + actual = norm([exp(-(T - 2log(2))), exp(-(T - log(2)))] - sol.u[end]) + alg = GlobalErrorEstimation(Tsit5(); mode) + estimate() = global_error_estimate(prob, alg; callback, abstol = 1.0e-4, reltol = 1.0e-4) + if actual > 0.1 + est = @test_logs (:warn, r"events of the forward solve") estimate() + @test est / actual ≈ 1 rtol = 1.0e-3 + else + est = @test_logs estimate() + @test est / actual ≈ 1 rtol = 1.0e-2 + end +end + +@testset "DiscreteCallback with a state condition: $mode" for mode in MODES + # The condition is checked at the step ends of the forward solve, so the true + # solution switches at the first forward step end where the true x is at least 1. + x_before(t) = 2(1 - exp(-t)) + make_callback() = DiscreteCallback( + (u, t, i) -> u[1] >= 1 && i.p[1] == 2.0, i -> (i.p[1] = 0.0; nothing) + ) + alg = GlobalErrorEstimation(Tsit5(); mode) + for tol in (1.0e-4, 1.0e-6) + prob = ODEProblem(switch!, [0.0], (0.0, T), [2.0]) + est = global_error_estimate(prob, alg; callback = make_callback(), abstol = tol, reltol = tol) + sol = solve(prob, Tsit5(); callback = make_callback(), abstol = tol, reltol = tol) + step_ends = [unique(sol.t)[2:(end - 1)]; T] + t_switch = step_ends[findfirst(t -> x_before(t) >= 1, step_ends)] + actual = abs(x_before(t_switch) * exp(-(T - t_switch)) - sol.u[end][1]) + @test est / actual ≈ 1 rtol = 1.0e-2 + end +end + +@testset "Error trajectory with several calls or one-sided saves: $mode" for mode in MODES + # Each case doubles u at t = 0.5. A callback can save before its affect, after it, or + # both; two callbacks at one time give four saves. Each entry is the state after the + # calls before it, so its exact value follows from its number of doublings. + double(i) = (i.u .*= 2; nothing) + cases = ( + DiscreteCallback((u, t, i) -> t == 0.5, double; save_positions = (true, false)), + DiscreteCallback((u, t, i) -> t == 0.5, double; save_positions = (false, true)), + CallbackSet( + DiscreteCallback((u, t, i) -> t == 0.5, double), DiscreteCallback((u, t, i) -> t == 0.5, double) + ), + ) + for callback in cases + sol = solve( + ODEProblem((u, p, t) -> -u, [1.0], (0.0, 1.0)), GlobalErrorEstimation(Tsit5(); mode); + callback, tstops = [0.5], abstol = 1.0e-6, reltol = 1.0e-6 + ) + doublings = [round(Int, log2(u[1] / exp(-t))) for (t, u) in zip(sol.t, sol.u)] + actual = [2.0^k * exp(-t) - u[1] for (k, t, u) in zip(doublings, sol.t, sol.u)] + @test all(abs(e[1] - a) <= 1.0e-2 * abs(a) + 1.0e-14 for (e, a) in zip(sol.global_error, actual)) + end +end + +@testset "Initialization affects: $mode" for mode in MODES + # The forward initialization doubles u. The true solution starts with the same affect, + # so the companion does not apply it to the true state again. + callback = () -> PeriodicCallback(i -> (i.u .*= 2; nothing), 10.0; initial_affect = true) + prob = ODEProblem(decay!, [1.0], (0.0, T)) + est = global_error_estimate( + prob, GlobalErrorEstimation(Tsit5(); mode); callback = callback(), abstol = 1.0e-6, reltol = 1.0e-6 + ) + sol = solve(prob, Tsit5(); callback = callback(), abstol = 1.0e-6, reltol = 1.0e-6) + @test est / abs(2exp(-T) - sol.u[end][1]) ≈ 1 rtol = 1.0e-2 +end + +@testset "Terminating callbacks: $mode" for mode in MODES + # The solve ends at the forward event, and the estimate is the error at that time. The + # true state must not terminate, even if the true event comes first. + # TerminateSteadyState also reads the derivative of the true state. + callbacks = ( + () -> ContinuousCallback((u, t, i) -> u[1] - 0.5, SciMLBase.terminate!), + () -> TerminateSteadyState(1.0e-4, 1.0e-4), + ) + # No warning: when the true event comes after the forward one, the companion does not + # reach it, and that is not an event mismatch. + for make_callback in callbacks, tol in (1.0e-4, 1.0e-6) + prob = ODEProblem(decay!, [1.0], (0.0, 100.0)) + est = @test_logs min_level = Base.CoreLogging.Warn global_error_estimate( + prob, GlobalErrorEstimation(Tsit5(); mode); callback = make_callback(), abstol = tol, reltol = tol + ) + sol = solve(prob, Tsit5(); callback = make_callback(), abstol = tol, reltol = tol) + @test sol.t[end] < 100 + @test est / abs(exp(-sol.t[end]) - sol.u[end][1]) ≈ 1 rtol = 1.0e-2 + end + + # At a forward step end, a DiscreteCallback terminates if x is below (or above) a value + # between the forward and the true state. The two solutions decide differently, and a + # warning reports it. + tol = 1.0e-3 + prob = ODEProblem(decay!, [1.0], (0.0, 2.0)) + plain = solve(prob, Tsit5(); abstol = tol, reltol = tol) + k = findfirst(>(0.7), plain.t) + ts = plain.t[k] + c = (plain.u[k][1] + exp(-ts)) / 2 + @test plain.u[k][1] > exp(-ts) + for (below, message) in ( + (true, r"true solution.*terminates at t = .*but the forward solve does not"), + (false, r"forward solve terminates at t = .*does not terminate at the same event"), + ) + cb = DiscreteCallback( + (u, t, i) -> t == ts, + i -> ((below ? i.u[1] < c : i.u[1] > c) && SciMLBase.terminate!(i); nothing) + ) + est = @test_logs (:warn, message) global_error_estimate( + prob, GlobalErrorEstimation(Tsit5(); mode); callback = cb, abstol = tol, reltol = tol + ) + # The `global_error_event_mismatch` toggle of `DEVerbosity` silences it. + @test_logs min_level = Base.CoreLogging.Warn global_error_estimate( + prob, GlobalErrorEstimation(Tsit5(); mode); callback = cb, abstol = tol, reltol = tol, + verbose = DEVerbosity(global_error_event_mismatch = SciMLLogging.Silent()) + ) + sol = solve(prob, Tsit5(); callback = cb, abstol = tol, reltol = tol) + @test sol.t[end] == (below ? 2.0 : ts) + @test est / abs(exp(-sol.t[end]) - sol.u[end][1]) ≈ 1 rtol = 1.0e-2 + end +end + +@testset "Unsupported callback functions fail clearly: $mode" for mode in MODES + # An affect that changes the step size has no meaning for the true state. + prob = ODEProblem(decay!, [1.0], (0.0, T)) + callback = ContinuousCallback( + (u, t, i) -> u[1] - 0.5, + i -> (i.u[1] = 1.0; SciMLBase.set_proposed_dt!(i, 0.01); nothing) + ) + @test_throws ArgumentError global_error_estimate( + prob, GlobalErrorEstimation(Tsit5(); mode); callback + ) +end + +@testset "Companion reinitialization after an event" begin + prob = ODEProblem(decay!, [1.0], (0.0, T)) + reinit(alg, kwargs = (;)) = GlobalDiffEq._companion_reinit(alg, prob, kwargs, 1.0e-12) + # Without initialization data, the default reinitialization is CheckInit, which + # changes nothing. + @test reinit(nothing) === SciMLBase.NoInit() + @test reinit(SciMLBase.CheckInit()) === SciMLBase.NoInit() + # The initializealg of the solve applies when the callback has none. The companion uses + # its own tolerance. + @test reinit(BrownFullBasicInit(abstol = 1.0e-7)).abstol == 1.0e-12 + @test reinit(nothing, (; initializealg = BrownFullBasicInit())) isa BrownFullBasicInit + # OverrideInit (the default for a ModelingToolkit problem) solves for u, not for the + # companion state, so a DAE rejects it. An ODE does not reinitialize. + @test_throws ArgumentError GlobalDiffEq._reinitialize_true_state!( + (; isdae = true), SciMLBase.OverrideInit() + ) + @test GlobalDiffEq._reinitialize_true_state!( + (; isdae = false), SciMLBase.OverrideInit() + ) === nothing +end + +@testset "SimultaneousMode matches InterpolatingMode with events: $name" for (name, make_prob, make_callback, _, extra, solver) in CASES + for tol in (1.0e-4, 1.0e-6) + estimates = map(MODES) do mode + global_error_estimate( + make_prob(), GlobalErrorEstimation(solver; mode); + callback = make_callback(), abstol = tol, reltol = tol, extra... + ) + end + @test estimates[2] ≈ estimates[1] rtol = 1.0e-3 + end +end + +@testset "A DiscreteCallback that a continuous event triggers: $mode" for mode in MODES + # At x = 0.5 the continuous event doubles x and sets a flag. At the same step end, a + # DiscreteCallback adds x² to y and clears the flag. The true event ends a step of the + # true solution, so the companion must check the DiscreteCallbacks there too. + tied = () -> CallbackSet( + ContinuousCallback((u, t, i) -> u[1] - 0.5, i -> (i.u[1] *= 2; i.p[1] = 1.0; nothing)), + DiscreteCallback((u, t, i) -> i.p[1] == 1.0, i -> (i.u[2] += i.u[1]^2; i.p[1] = 0.0; nothing)) + ) + prob = () -> ODEProblem((u, p, t) -> [-u[1], -u[2]], [1.0, 1.0], (0.0, 2.0), [0.0]) + ref = solve(prob(), Tsit5(); callback = tied(), abstol = 1.0e-13, reltol = 1.0e-13) + for tol in (1.0e-5, 1.0e-7) + sol = solve(prob(), Tsit5(); callback = tied(), abstol = tol, reltol = tol) + est = global_error_estimate( + prob(), GlobalErrorEstimation(Tsit5(); mode); callback = tied(), abstol = tol, reltol = tol + ) + @test est / norm(ref.u[end] .- sol.u[end]) ≈ 1 rtol = 1.0e-3 + end +end + +@testset "Callbacks at the forward event knots, integer time span, termination: $mode" for mode in MODES + # The companion does not call a callback that acts at each step, such as a + # SavingCallback, at the forward event time. The true solution ends a step at its own + # event time instead. An extra call would give a false event-mismatch warning. + reset = () -> ContinuousCallback((u, t, i) -> u[1] - 0.5, i -> (i.u[1] = 1.0; nothing)) + saving = () -> SavingCallback((u, t, i) -> copy(u), SavedValues(Float64, Vector{Float64})) + prob = ODEProblem((u, p, t) -> [-u[1]], [1.0], (0.0, 2.0)) + alg = GlobalErrorEstimation(Tsit5(); mode) + est = @test_logs min_level = Base.CoreLogging.Warn global_error_estimate( + prob, alg; callback = CallbackSet(reset(), saving()), abstol = 1.0e-6, reltol = 1.0e-6 + ) + # The copy of the SavingCallback in the companion acts at each knot, which can change + # the companion steps slightly. + est_reset = global_error_estimate(prob, alg; callback = reset(), abstol = 1.0e-6, reltol = 1.0e-6) + @test est ≈ est_reset rtol = 1.0e-3 + + # An integer time span gives the same estimate; the event times are floats. + int_prob = ODEProblem((u, p, t) -> [-u[1]], [1.0], (0, 2)) + @test global_error_estimate(int_prob, alg; callback = reset(), abstol = 1.0e-6, reltol = 1.0e-6) ≈ + est_reset rtol = 1.0e-6 + + # A terminating event triggers a DiscreteCallback at its step end. When the true event + # comes after the forward end, the companion runs neither callback and does not warn. + triggered = () -> CallbackSet( + ContinuousCallback((u, t, i) -> u[1] - 0.5, i -> (i.p[1] = 1.0; SciMLBase.terminate!(i))), + DiscreteCallback((u, t, i) -> i.p[1] == 1.0, i -> (i.u[1] += 0.1; i.p[1] = 0.0; nothing)) + ) + flagged = () -> ODEProblem((u, p, t) -> [-u[1]], [1.0], (0.0, 2.0), [0.0]) + for tol in (1.0e-4, 1.0e-6) + sol = solve(flagged(), Tsit5(); callback = triggered(), abstol = tol, reltol = tol) + @test sol.t[end] < log(2) + @test_logs min_level = Base.CoreLogging.Warn global_error_estimate( + flagged(), alg; callback = triggered(), abstol = tol, reltol = tol + ) + end +end + +# A forward step a → b with b > 2a: a + (b - a) is not b, so the companion must stop at b +# itself, not at the end of a step of length b - a. +@testset "SimultaneousMode stops the companion at the forward step end" begin + a, b = 0.008476769222961617, 0.34529383286546195 + @test a + (b - a) != b + prob = ODEProblem((u, p, t) -> -0.1 .* u, [1.0, 2.0], (0.0, 1.0)) + kwargs = (; abstol = 1.0e-3, reltol = 1.0e-3, tstops = [a, b]) + @test b in solve(prob, Tsit5(); kwargs...).t + estimates = map(MODES) do mode + global_error_estimate(prob, GlobalErrorEstimation(Tsit5(); mode); kwargs...) + end + @test estimates[2] ≈ estimates[1] rtol = 1.0e-6 + trajectories = map(MODES) do mode + solve(prob, GlobalErrorEstimation(Tsit5(); mode); kwargs...).global_error + end + @test trajectories[2] ≈ trajectories[1] rtol = 1.0e-6 +end + +@testset "SavingCallback sees only the forward solve: $mode, saveat = $saveat" for mode in MODES, + saveat in ((), [0.3, 0.7, 1.0, 1.5, 1.9]) + + saving(values) = CallbackSet( + reset_callback(), SavingCallback((u, t, i) -> u[1], values; saveat) + ) + make_prob() = ODEProblem(decay!, [1.0], (0.0, T)) + # The estimate saves the same values as a plain solve. + values = SavedValues(Float64, Float64) + est = global_error_estimate( + make_prob(), GlobalErrorEstimation(Tsit5(); mode); + callback = saving(values), abstol = 1.0e-6, reltol = 1.0e-6 + ) + plain = SavedValues(Float64, Float64) + sol = solve(make_prob(), Tsit5(); callback = saving(plain), abstol = 1.0e-6, reltol = 1.0e-6) + @test values.t == plain.t + @test values.saveval == plain.saveval + @test est / abs(RESET_EXACT - sol.u[end][1]) ≈ 1 rtol = 1.0e-2 + # Under global error control, only the final solve saves values. It repeats the + # accepted solve, so it does not warn. + values = SavedValues(Float64, Float64) + sol = @test_logs min_level = Base.CoreLogging.Warn solve( + make_prob(), GlobalErrorEstimation(Tsit5(); mode, gtol = 1.0e-7); + callback = saving(values), abstol = 1.0e-3, reltol = 1.0e-3 + ) + @test SciMLBase.successful_retcode(sol) + # The solution saves an event time twice (before and after the affect), the + # SavingCallback once. + @test values.t == (saveat == () ? unique(sol.t) : saveat) +end + +@testset "A final forward solve that does not repeat the accepted one" begin + # A callback with outside state, or a solve that is not deterministic, can make the + # final forward solve differ from the accepted one. The test calls `_final_solve` + # directly: a global counter in an affect would also move the true event, and the + # control loop would not meet gtol. + accepted = (; estimate = 0.0, sol = (; t = [0.0], u = [[1.0]])) + values = SavedValues(Float64, Float64) + callback = CallbackSet(reset_callback(), SavingCallback((u, t, i) -> u[1], values)) + sol = @test_logs (:warn, r"does not repeat the accepted one") GlobalDiffEq._final_solve( + ODEProblem(decay!, [1.0], (0.0, T)), GlobalErrorEstimation(Tsit5()), accepted; + callback, abstol = 1.0e-6, reltol = 1.0e-6 + ) + @test length(sol.global_error) == length(sol.t) + # The new estimate runs on a copy of the callbacks made before the forward solve, so + # the SavingCallback of the user has the values of the forward solve only. + @test values.t == unique(sol.t) + # The `global_error_final_solve` toggle of `DEVerbosity` silences the warning. + @test_logs min_level = Base.CoreLogging.Warn GlobalDiffEq._final_solve( + ODEProblem(decay!, [1.0], (0.0, T)), GlobalErrorEstimation(Tsit5()), accepted; + callback = reset_callback(), abstol = 1.0e-6, reltol = 1.0e-6, + verbose = DEVerbosity(global_error_final_solve = SciMLLogging.Silent()) + ) + @test sol.global_error[end][1] / (RESET_EXACT - sol.u[end][1]) ≈ 1 rtol = 1.0e-2 +end + +@testset "Error trajectory with events: $mode" for mode in MODES + # The true solution resets at log 2 and 2 log 2, and the forward solution at nearby + # times. The solution saves each forward event time twice. Between a true and a forward + # event, the error is 0.5. + reset_x(t) = exp(-(t - fld(t, log(2)) * log(2))) + for tol in (1.0e-4, 1.0e-6) + sol = solve( + ODEProblem(decay!, [1.0], (0.0, T)), GlobalErrorEstimation(Tsit5(); mode); + callback = reset_callback(), abstol = tol, reltol = tol + ) + actual = [reset_x(t) - u[1] for (t, u) in zip(sol.t, sol.u)] + @test maximum(abs, actual) ≈ 0.5 rtol = 1.0e-3 + @test all(abs(e[1] - a) <= 1.0e-3 * tol for (e, a) in zip(sol.global_error, actual)) + end + + # A fixed-time doubling: the true solution jumps at the same time, so the entry before + # the event and the entry after it have different errors. + sol = solve( + ODEProblem(decay!, [1.0], (0.0, T)), GlobalErrorEstimation(Tsit5(); mode); + callback = PresetTimeCallback([1.0], i -> (i.u[1] *= 2; nothing)), + abstol = 1.0e-6, reltol = 1.0e-6 + ) + before_doubling(i) = sol.t[i] < 1 || (sol.t[i] == 1 && sol.t[i + 1] == 1) + actual = [(before_doubling(i) ? 1 : 2) * exp(-sol.t[i]) - sol.u[i][1] for i in eachindex(sol.t)] + @test count(==(1.0), sol.t) == 2 + @test all( + abs(e[1] - a) <= 1.0e-2 * maximum(abs, actual) for (e, a) in zip(sol.global_error, actual) + ) + + # A DAE with a reinitialization after the event: both states have an error trajectory. + sol = solve( + ODEProblem( + ODEFunction(cubic_dae!; mass_matrix = Diagonal([1.0, 0.0])), + [1.0, cubic_z(1.0)], (0.0, T) + ), + GlobalErrorEstimation(Rodas5P(); mode); + callback = ContinuousCallback( + (u, t, i) -> u[1] - 0.5, i -> (i.u[1] = 1.0; nothing); + initializealg = BrownFullBasicInit() + ), + abstol = 1.0e-6, reltol = 1.0e-6 + ) + actual = [[reset_x(t), cubic_z(reset_x(t))] .- u for (t, u) in zip(sol.t, sol.u)] + @test maximum(norm, actual) > 0.1 + @test all(norm(e - a) <= 1.0e-6 for (e, a) in zip(sol.global_error, actual)) +end diff --git a/lib/GlobalDiffEq/test/companion_tests.jl b/lib/GlobalDiffEq/test/companion_tests.jl index e850502f902..673c8a6f7ff 100644 --- a/lib/GlobalDiffEq/test/companion_tests.jl +++ b/lib/GlobalDiffEq/test/companion_tests.jl @@ -1,6 +1,6 @@ -using GlobalDiffEq, OrdinaryDiffEqTsit5, LinearAlgebra +using GlobalDiffEq, OrdinaryDiffEqTsit5, OrdinaryDiffEqRosenbrock, LinearAlgebra using Test -import SciMLBase +import SciMLBase, SciMLLogging lv!(du, u, p, t) = (du[1] = 1.5u[1] - u[1] * u[2]; du[2] = -3.0u[2] + u[1] * u[2]; nothing) lv(u, p, t) = [1.5u[1] - u[1] * u[2], -3.0u[2] + u[1] * u[2]] @@ -59,6 +59,59 @@ end end end +@testset "Error trajectory: $mode" for mode in MODES + prob = ODEProblem(lv!, [1.0, 1.0], lv_tspan) + ref = solve(prob, Tsit5(); abstol = 1.0e-13, reltol = 1.0e-13) + alg = GlobalErrorEstimation(Tsit5(); mode) + @test SciMLBase.has_global_error(alg) + @test !SciMLBase.has_global_error(GlobalErrorEstimation(Tsit5(); equation = ErrorTransport)) + + # Without gtol, the solve gives the error of each saved state at the given tolerances. + sol = solve(prob, alg; abstol = 1.0e-6, reltol = 1.0e-6) + @test length(sol.global_error) == length(sol.u) + @test global_error_estimate(sol) === sol.global_error + @test norm(sol.global_error[end]) ≈ + global_error_estimate(prob, alg; abstol = 1.0e-6, reltol = 1.0e-6) + actual = [ref(t) .- u for (t, u) in zip(sol.t, sol.u)] + @test all( + norm(e - a) <= 1.0e-3 * maximum(norm, actual) for (e, a) in zip(sol.global_error, actual) + ) + + # Under global error control, the final solution has the error trajectory too. + gsol = solve( + prob, GlobalErrorEstimation(Tsit5(); mode, gtol = 1.0e-7); abstol = 1.0e-3, reltol = 1.0e-3 + ) + @test length(gsol.global_error) == length(gsol.u) + gactual = [ref(t) .- u for (t, u) in zip(gsol.t, gsol.u)] + @test all( + norm(e - a) <= 1.0e-3 * maximum(norm, gactual) for (e, a) in zip(gsol.global_error, gactual) + ) +end + +@testset "Stiff problem with an implicit solver" begin + # Van der Pol with mu = 1000. At the fast transition near t = 807 the global error + # passes through a peak 1e7 times its final value, so a loose default reltol for the + # companion gives a wrong estimate. + svdp!(du, u, p, t) = (du[1] = u[2]; du[2] = 1.0e3 * ((1 - u[1]^2) * u[2]) - u[1]; nothing) + prob = ODEProblem(svdp!, [2.0, 0.0], (0.0, 1000.0)) + ref = solve(prob, Rodas5P(); abstol = 1.0e-12, reltol = 1.0e-12) + sol = solve(prob, Rodas5P(); abstol = 1.0e-4, reltol = 1.0e-4) + gtol = 1.0e-6 + for mode in MODES + est = global_error_estimate( + prob, GlobalErrorEstimation(Rodas5P(); mode); abstol = 1.0e-4, reltol = 1.0e-4 + ) + @test est / norm(sol.u[end] - ref.u[end]) ≈ 1 rtol = 1.0e-2 + # The outer solve wraps f for the ForwardDiff traits of the inner solver, which + # differentiates f for its Jacobian. + gsol = solve( + prob, GlobalErrorEstimation(Rodas5P(); mode, gtol); abstol = 1.0e-3, reltol = 1.0e-3 + ) + @test SciMLBase.successful_retcode(gsol) + @test norm(gsol.u[end] - ref.u[end]) <= gtol + end +end + @testset "Companion estimator argument validation" begin prob = ODEProblem(lv!, [1.0, 1.0], lv_tspan) alg = GlobalErrorEstimation(Tsit5()) @@ -73,12 +126,19 @@ end @test_throws ArgumentError GlobalErrorEstimation(Tsit5(); gtol = -1.0) @test_throws ArgumentError GlobalErrorEstimation(Tsit5(); maxiters = 0) @test_throws ArgumentError GlobalErrorEstimation(Tsit5(); safety = 2.0) - # gtol is required to solve with the wrapper - @test_throws ArgumentError solve(prob, alg) + # Without gtol, ErrorTransport has nothing to return (DefectCorrection returns the + # error trajectory). + @test_throws ArgumentError solve(prob, GlobalErrorEstimation(Tsit5(); equation = ErrorTransport)) + # Callbacks are supported only by DefectCorrection (see companion_events_tests.jl). callback = DiscreteCallback((u, t, integrator) -> false, integrator -> nothing) callback_prob = ODEProblem(lv!, [1.0, 1.0], lv_tspan; callback) - @test_throws ArgumentError global_error_estimate(callback_prob, alg) + for mode in MODES + @test global_error_estimate(callback_prob, GlobalErrorEstimation(Tsit5(); mode)) > 0 + unsupported = GlobalErrorEstimation(Tsit5(); equation = ErrorTransport, mode) + @test_throws ArgumentError global_error_estimate(callback_prob, unsupported) + @test_throws ArgumentError global_error_estimate(prob, unsupported; callback) + end backwards_prob = ODEProblem(lv!, [1.0, 1.0], (10.0, 0.0)) @test_throws ArgumentError global_error_estimate(backwards_prob, alg) @@ -102,3 +162,28 @@ end ) end end + +@testset "Companion solver warning" begin + # The companion runs at tight tolerances, which a low-order solver meets poorly. The + # solve warns, not the constructor, because only the solve has `verbose`. The + # `global_error_companion_order` toggle of `DEVerbosity` controls the warning. + decay = ODEProblem((u, p, t) -> -u, [1.0], (0.0, 1.0)) + @test_logs GlobalErrorEstimation(Rosenbrock32()) + @test_logs (:warn, r"solves the companion with Rosenbrock32") match_mode = :any global_error_estimate( + decay, GlobalErrorEstimation(Rosenbrock32()) + ) + @test_logs (:warn, r"solves the companion with Rosenbrock32") match_mode = :any solve( + decay, GlobalErrorEstimation(Rosenbrock32()) + ) + @test_logs min_level = Base.CoreLogging.Warn global_error_estimate( + decay, GlobalErrorEstimation(Rosenbrock32(); companion_alg = Tsit5()) + ) + # The toggle or a preset silences the warning. + for verbose in (DEVerbosity(global_error_companion_order = SciMLLogging.Silent()), SciMLLogging.None()) + @test_logs min_level = Base.CoreLogging.Warn global_error_estimate( + decay, GlobalErrorEstimation(Rosenbrock32()); verbose + ) + end + @test_throws ArgumentError global_error_estimate(decay, GlobalErrorEstimation(Tsit5()); verbose = false) + @test GlobalErrorEstimation(Tsit5()).companion_alg isa Tsit5 +end diff --git a/lib/GlobalDiffEq/test/qa/qa.jl b/lib/GlobalDiffEq/test/qa/qa.jl index a2f02317f6a..31fca6de2fd 100644 --- a/lib/GlobalDiffEq/test/qa/qa.jl +++ b/lib/GlobalDiffEq/test/qa/qa.jl @@ -18,6 +18,13 @@ run_qa( # SciMLSensitivity extension adds methods to these internal # functions of its parent package. :_adjoint_defect_projection, :_default_adjoint_sensealg, + # Internal helpers of GlobalDiffEq that the SciMLSensitivity + # extension calls. + :_rhs, :_rhs_jacobian, :_parameters_at, :_calls_before_entry, :_entry_bridge, + :_supports_events, :_unsupported_events_error, :_gauss_rule_order, + # SciMLSensitivity's integrand type of the Gauss adjoints: the method of + # `ODEAdjointProblem` for a Gauss adjoint needs it. + :GaussIntegrand, ), ), ), diff --git a/lib/GlobalDiffEq/test/runtests.jl b/lib/GlobalDiffEq/test/runtests.jl index 00d4e511326..1243a334928 100644 --- a/lib/GlobalDiffEq/test/runtests.jl +++ b/lib/GlobalDiffEq/test/runtests.jl @@ -14,9 +14,15 @@ run_tests(; @safetestset "Companion error estimators" begin include("companion_tests.jl") end + @safetestset "Companion error estimators with events" begin + include("companion_events_tests.jl") + end @safetestset "Adjoint error estimation and control" begin include("adjoint_tests.jl") end + @safetestset "Adjoint error estimation with events and DAEs" begin + include("adjoint_events_tests.jl") + end @safetestset "GLEE solvers" begin include("glee_tests.jl") end