|
| 1 | +""" |
| 2 | + GlobalErrorTransport(alg; gtol=nothing, transport_alg=alg, |
| 3 | + autodiff=ADTypes.AutoForwardDiff(), maxiters=6, |
| 4 | + safety=0.8, companion_abstol, companion_reltol) |
| 5 | +
|
| 6 | +Wrap an ODE algorithm with global error estimation and control based on the |
| 7 | +linearized error-transport (first variational) equation. After a dense forward |
| 8 | +solve producing the interpolant `P(t)`, the global error `ε(t) ≈ y(t) - P(t)` |
| 9 | +is estimated by integrating the linear companion ODE |
| 10 | +
|
| 11 | +```math |
| 12 | +ε'(t) = J(P(t), p, t) \\, ε(t) + d(t), \\qquad ε(t_0) = 0, |
| 13 | +``` |
| 14 | +
|
| 15 | +where `d(t) = f(P(t), p, t) - P'(t)` is the defect of the dense output and `J` |
| 16 | +is the Jacobian of `f`, applied matrix-free through Jacobian-vector products |
| 17 | +computed with `autodiff` (any `ADTypes` backend supported by |
| 18 | +DifferentiationInterface). This is the error-transport approach of Shampine |
| 19 | +(1986) and Berzins (1988), in the continuous defect-driven form summarized by |
| 20 | +Lang and Verwer (2007). |
| 21 | +
|
| 22 | +Set the requested absolute endpoint error with `gtol`: |
| 23 | +
|
| 24 | +```julia |
| 25 | +solve(prob, GlobalErrorTransport(Tsit5(); gtol = 1.0e-6)) |
| 26 | +``` |
| 27 | +
|
| 28 | +The solver then tightens local tolerances until the estimated endpoint global |
| 29 | +error 2-norm is at most `gtol`, like [`GlobalAdjoint`](@ref). Omit `gtol` when |
| 30 | +using the algorithm only with [`global_error_estimate`](@ref). `transport_alg` |
| 31 | +selects the solver for the companion equation, and `companion_abstol` / |
| 32 | +`companion_reltol` control its tolerances. |
| 33 | +
|
| 34 | +This implementation supports forward-time, standard-mass-matrix ODEs with real |
| 35 | +vector states and no callbacks. The wrapped solver must provide a dense |
| 36 | +first-derivative interpolation. |
| 37 | +
|
| 38 | +## References |
| 39 | +
|
| 40 | + - L. F. Shampine, Global error estimation with one-step methods, Computers & |
| 41 | + Mathematics with Applications 12A (1986). |
| 42 | + - M. Berzins, Global error estimation in the method of lines for parabolic |
| 43 | + equations, SIAM Journal on Scientific and Statistical Computing 9 (1988). |
| 44 | + - J. Lang and J. Verwer, On global error estimation and control for initial |
| 45 | + value problems, SIAM Journal on Scientific Computing 29 (2007). |
| 46 | +""" |
| 47 | +struct GlobalErrorTransport{A, TA, AD, G, V} <: GlobalDiffEqAlgorithm |
| 48 | + alg::A |
| 49 | + transport_alg::TA |
| 50 | + autodiff::AD |
| 51 | + gtol::G |
| 52 | + options::V |
| 53 | +end |
| 54 | + |
| 55 | +function GlobalErrorTransport( |
| 56 | + alg; |
| 57 | + transport_alg = alg, |
| 58 | + autodiff = ADTypes.AutoForwardDiff(), |
| 59 | + gtol = nothing, |
| 60 | + maxiters = 6, |
| 61 | + safety = 0.8, |
| 62 | + companion_abstol = gtol === nothing ? 1.0e-10 : min(gtol / 100, 1.0e-10), |
| 63 | + companion_reltol = gtol === nothing ? 1.0e-8 : min(gtol / 100, 1.0e-8) |
| 64 | + ) |
| 65 | + options = _companion_options( |
| 66 | + gtol, maxiters, safety, companion_abstol, companion_reltol |
| 67 | + ) |
| 68 | + return GlobalErrorTransport(alg, transport_alg, autodiff, gtol, options) |
| 69 | +end |
| 70 | + |
| 71 | +SciMLBase.allows_arbitrary_number_types(::GlobalErrorTransport) = false |
| 72 | +SciMLBase.allowscomplex(::GlobalErrorTransport) = false |
| 73 | +SciMLBase.isautodifferentiable(::GlobalErrorTransport) = false |
| 74 | + |
| 75 | +function _transport_rhs(sol, autodiff) |
| 76 | + prob = sol.prob |
| 77 | + foop = _oop_rhs(prob) |
| 78 | + return let sol = sol, foop = foop, p = prob.p, backend = autodiff |
| 79 | + function (ε, _, t) |
| 80 | + u = sol(t, continuity = :right) |
| 81 | + du = sol(t, Val{1}, continuity = :right) |
| 82 | + defect = foop(u, p, t) - du |
| 83 | + jv = only( |
| 84 | + DifferentiationInterface.pushforward( |
| 85 | + x -> foop(x, p, t), backend, u, (ε,) |
| 86 | + ) |
| 87 | + ) |
| 88 | + return jv + defect |
| 89 | + end |
| 90 | + end |
| 91 | +end |
| 92 | + |
| 93 | +""" |
| 94 | + global_error_estimate(prob, alg::GlobalErrorTransport; abstol=1e-6, reltol=1e-3, kwargs...) |
| 95 | +
|
| 96 | +Solve an ODE problem and estimate the 2-norm of its global error at the final |
| 97 | +time by integrating the linearized error-transport equation driven by the |
| 98 | +dense-output defect. `abstol` and `reltol` control the forward solve; |
| 99 | +`companion_abstol` and `companion_reltol` control the companion error solve. |
| 100 | +""" |
| 101 | +function global_error_estimate( |
| 102 | + prob::SciMLBase.AbstractODEProblem, alg::GlobalErrorTransport, args...; |
| 103 | + abstol = 1.0e-6, |
| 104 | + reltol = 1.0e-3, |
| 105 | + companion_abstol = alg.options.companion_abstol, |
| 106 | + companion_reltol = alg.options.companion_reltol, |
| 107 | + kwargs... |
| 108 | + ) |
| 109 | + return _companion_error_estimate( |
| 110 | + sol -> _transport_rhs(sol, alg.autodiff), "GlobalErrorTransport", |
| 111 | + prob, alg.alg, alg.transport_alg, args...; |
| 112 | + abstol, reltol, companion_abstol, companion_reltol, kwargs... |
| 113 | + ) |
| 114 | +end |
| 115 | + |
| 116 | +function SciMLBase.__solve( |
| 117 | + prob::SciMLBase.AbstractODEProblem, alg::GlobalErrorTransport, args...; |
| 118 | + abstol = something(alg.gtol, 1.0e-6), |
| 119 | + reltol = something(alg.gtol, 1.0e-3), |
| 120 | + kwargs... |
| 121 | + ) |
| 122 | + alg.gtol === nothing && throw( |
| 123 | + ArgumentError("GlobalErrorTransport requires a positive `gtol` constructor keyword") |
| 124 | + ) |
| 125 | + _validate_tolerances(abstol, reltol, "local") |
| 126 | + haskey(kwargs, :callback) && |
| 127 | + throw(ArgumentError("GlobalErrorTransport does not currently support callbacks")) |
| 128 | + estimator = (local_abstol, local_reltol) -> global_error_estimate( |
| 129 | + prob, alg, args...; |
| 130 | + abstol = local_abstol, reltol = local_reltol, kwargs... |
| 131 | + ) |
| 132 | + return _refine_to_gtol( |
| 133 | + estimator, prob, alg.alg, alg.gtol, alg.options, args...; |
| 134 | + abstol, reltol, kwargs... |
| 135 | + ) |
| 136 | +end |
0 commit comments