Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/OrdinaryDiffEqNordsieck/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ DiffEqDevTools = "3"
MuladdMacro = "0.2.4"
LinearAlgebra = "1.10"
SciMLBase = "3.39"
OrdinaryDiffEqCore = "4.4"
OrdinaryDiffEqCore = "4.7"
julia = "1.10"
RecursiveArrayTools = "4.2.0"
ODEProblemLibrary = "1"
Expand Down
5 changes: 3 additions & 2 deletions lib/OrdinaryDiffEqNordsieck/src/OrdinaryDiffEqNordsieck.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module OrdinaryDiffEqNordsieck

# `alg_order` is owned (and public) by SciMLBase; import it from its owner.
import SciMLBase: alg_order
import SciMLBase: alg_order, resize_non_user_cache!
# `initialize!`, `calculate_residuals`, `calculate_residuals!` are owned by
# DiffEqBase (re-provided through OrdinaryDiffEqCore); import them from the owner
# so the ExplicitImports owner check passes. `initialize!` is public in
Expand All @@ -22,7 +22,8 @@ import OrdinaryDiffEqCore: alg_adaptive_order,
CommonControllerOptions, resolve_basic, _resolved_QT,
get_current_adaptive_order, get_fsalfirstlast,
ode_interpolant, ode_interpolant!, trivial_limiter!,
generic_solver_docstring, default_controller
generic_solver_docstring, default_controller,
@cache, ODEIntegrator, reinit_controller!
using MuladdMacro: @muladd
using FastBroadcast: @.., Serial
using RecursiveArrayTools: recursivefill!
Expand Down
56 changes: 46 additions & 10 deletions lib/OrdinaryDiffEqNordsieck/src/nordsieck_caches.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ function alg_cache(
return AN5ConstantCache(z, l, m, c_LTE, c_conv, dts, Δ, tsit5tab, 1)
end

mutable struct AN5Cache{uType, dType, rateType, zType, lType, dtsType, tsit5Type} <:
OrdinaryDiffEqMutableCache
@cache mutable struct AN5Cache{
uType, uNoUnitsType, rateType, zType, lType, dtsType, tsit5Type,
} <: OrdinaryDiffEqMutableCache
u::uType
uprev::uType
tmp::uType
Δ::dType
Δ::uNoUnitsType
# Error estimation
atmp::dType
atmp::uNoUnitsType
fsalfirst::rateType
ratetmp::rateType
# `z` is the Nordsieck vector
Expand All @@ -60,6 +61,7 @@ mutable struct AN5Cache{uType, dType, rateType, zType, lType, dtsType, tsit5Type
# `Tsit5` for the first step
tsit5cache::tsit5Type
order::Int
resize_restart::Bool
end

function alg_cache(
Expand Down Expand Up @@ -104,7 +106,7 @@ function alg_cache(
return AN5Cache(
u, uprev, tmp, Δ, atmp, fsalfirst, ratetmp,
z, l, m, c_LTE, c_conv, dts,
tsit5cache, 1
tsit5cache, 1, false
)
end

Expand Down Expand Up @@ -170,13 +172,13 @@ function alg_cache(
)
end

mutable struct JVODECache{
@cache mutable struct JVODECache{
uType,
rateType,
zType,
lType,
dtsType,
dType,
uNoUnitsType,
etaType,
tsit5Type,
} <: OrdinaryDiffEqMutableCache
Expand Down Expand Up @@ -205,9 +207,9 @@ mutable struct JVODECache{
# `dts` stores `dt`s
dts::dtsType
# `Δ` is the difference between the predictor `uₙ₀` and `uₙ`
Δ::dType
Δ::uNoUnitsType
# Error estimation
atmp::dType
atmp::uNoUnitsType
# `Tsit5` for the first step
tsit5cache::tsit5Type
L::Int
Expand All @@ -223,6 +225,7 @@ mutable struct JVODECache{
η₊₁::etaType
η₋₁::etaType
maxη::etaType
resize_restart::Bool
end

function alg_cache(
Expand Down Expand Up @@ -281,10 +284,43 @@ function alg_cache(
u, uprev, tmp, fsalfirst, ratetmp,
z, l, m,
c_LTE₊₁, c_LTE, c_LTE₋₁, c_conv, c_𝒟, prev_𝒟,
dts, Δ, atmp, tsit5cache, 2, 1, 1, 2, η, η, η, η, η, η
dts, Δ, atmp, tsit5cache, 2, 1, 1, 2, η, η, η, η, η, η, false
)
end

function get_fsalfirstlast(cache::Union{JVODECache, AN5Cache}, u)
return get_fsalfirstlast(cache.tsit5cache, u)
end

function _resize_nordsieck_arrays!(integrator, cache, i)
for zi in cache.z
resize!(zi, i)
recursivefill!(zi, false)
end
tsit5 = cache.tsit5cache
for buf in (
tsit5.k1, tsit5.k2, tsit5.k3, tsit5.k4, tsit5.k5, tsit5.k6, tsit5.k7,
tsit5.utilde, tsit5.tmp, tsit5.atmp, cache.Δ, cache.ratetmp, cache.fsalfirst,
)
resize!(buf, i)
end
fill!(cache.l, 0)
fill!(cache.m, 0)
cache.order = 1
cache.resize_restart = true
integrator.derivative_discontinuity = true
reinit_controller!(integrator, integrator.controller_cache)
return nothing
end

function resize_non_user_cache!(integrator::ODEIntegrator, cache::AN5Cache, i)
return _resize_nordsieck_arrays!(integrator, cache, i)
end

function resize_non_user_cache!(integrator::ODEIntegrator, cache::JVODECache, i)
_resize_nordsieck_arrays!(integrator, cache, i)
cache.nextorder = 1
cache.L = 2
cache.n_wait = 2
return nothing
end
12 changes: 11 additions & 1 deletion lib/OrdinaryDiffEqNordsieck/src/nordsieck_perform_step.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ end
# Nordsieck form needs to build the history vector
if cache.order == 1
## Start the Nordsieck vector in two shots!
if cache.resize_restart
copyto!(integrator.uprev, integrator.u)
f(integrator.fsalfirst, integrator.uprev, integrator.p, integrator.t)
increment_nf!(integrator.stats, 1)
cache.resize_restart = false
end
perform_step!(integrator, tsit5cache, repeat_step)
copyto!(tmp, integrator.u)
cache.order = 4
Expand Down Expand Up @@ -276,7 +282,11 @@ end
(; t, dt, uprev, u, f, p, uprev2, differential_vars) = integrator
(; z, l, m, c_LTE, dts, tmp, ratetmp, atmp, tsit5cache) = cache
# handle callbacks, rewind back to order one.
if integrator.derivative_discontinuity || integrator.iter == 1
if integrator.derivative_discontinuity || integrator.iter == 1 || cache.resize_restart
if cache.resize_restart
copyto!(integrator.uprev, integrator.u)
cache.resize_restart = false
end
cache.order = 1
cache.nextorder = 1
@.. broadcast = false z[1] = integrator.uprev
Expand Down
63 changes: 63 additions & 0 deletions lib/OrdinaryDiffEqNordsieck/test/nordsieck_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,66 @@ end
@test norm(exact - sol.u[end], Inf) < 3.0e-3
end
end

const A_resize = [-1.0 0.35 0.1; 0.2 -1.4 0.25; 0.15 0.3 -1.2]
const U0_resize = [1.0, -0.5]
const UNEW_resize = 0.3
const TG_resize = 0.5
const TS_resize = 0.65
const TF_resize = 0.8

f_resize!(du, u, p, t) = (mul!(du, view(A_resize, 1:length(u), 1:length(u)), u); nothing)

function exact_resize(t)
u = exp(A_resize[1:2, 1:2] * min(t, TG_resize)) * U0_resize
t <= TG_resize && return u
u3 = exp(A_resize * (min(t, TS_resize) - TG_resize)) * vcat(u, UNEW_resize)
t <= TS_resize && return u3
return exp(A_resize[1:2, 1:2] * (t - TS_resize)) * u3[1:2]
end

resize_cases = (
(AN5(), 1.0e-10, 1.0e-12, 1.0e-7),
(JVODE_Adams(), 1.0e-8, 1.0e-10, 1.0e-6),
(JVODE_BDF(), 1.0e-8, 1.0e-10, 1.0e-6),
)
@testset "Nordsieck state resizing ($alg, u_modified=$modified)" for
(alg, reltol, abstol, check_rtol) in resize_cases, modified in (true, false)
grew = Ref(false)
shrank = Ref(false)
restart_orders = Int[]
function affect!(integrator)
n = grew[] ? 2 : 3
resize!(integrator, n)
n == 3 && (integrator.u[3] = UNEW_resize)
u_modified!(integrator, modified)
push!(restart_orders, integrator.cache.order)
grew[] ? (shrank[] = true) : (grew[] = true)
return nothing
end
cb = DiscreteCallback(
(u, t, integrator) -> (!grew[] && t >= TG_resize) || (!shrank[] && t >= TS_resize),
affect!;
save_positions = (false, false)
)
sol = solve(
ODEProblem(f_resize!, copy(U0_resize), (0.0, TF_resize)), alg;
callback = cb, tstops = [TG_resize, TS_resize], reltol = reltol, abstol = abstol
)
@test SciMLBase.successful_retcode(sol)
@test sol(0.58) ≈ exact_resize(0.58) rtol = check_rtol atol = 1.0e-10
@test sol.u[end] ≈ exact_resize(TF_resize) rtol = check_rtol atol = 1.0e-10
@test restart_orders == [1, 1]
end

@testset "resize before first step initializes restart state" begin
for alg in (AN5(), JVODE_Adams(), JVODE_BDF())
it = init(ODEProblem(f_resize!, [1.0, -0.5], (0.0, 0.1)), alg; adaptive = false, dt = 1.0e-4)
resize!(it, 3)
it.u[3] = 0.3
it.uprev[3] = NaN
u_modified!(it, true)
step!(it)
@test all(isfinite, it.u)
end
end
Loading