Skip to content

Commit

Permalink
Fix for differential_vars update (#278)
Browse files Browse the repository at this point in the history
* Fix for differential_vars update

* bump versions

* Update Project.toml
  • Loading branch information
ChrisRackauckas authored Dec 16, 2023
1 parent 3e9c423 commit 122af53
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 25 deletions.
1 change: 0 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ jobs:
matrix:
version:
- '1'
- '1.6'
os:
- ubuntu-latest
arch:
Expand Down
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ SimpleNonlinearSolve = "727e6d20-b764-4bd8-a329-72de5adea6c7"
SimpleUnPack = "ce78b400-467f-4804-87d8-8f486da07d0a"

[compat]
ArrayInterface = "6, 7"
ArrayInterface = "7"
DataStructures = "0.18"
DiffEqBase = "6.122"
OrdinaryDiffEq = "6.49.1"
OrdinaryDiffEq = "6.63"
RecursiveArrayTools = "2, 3"
Reexport = "0.2, 1.0"
SciMLBase = "1.90, 2"
SimpleNonlinearSolve = "0.1, 1"
SimpleUnPack = "1"
julia = "1.6"
julia = "1.9"
15 changes: 9 additions & 6 deletions src/integrators/type.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mutable struct HistoryODEIntegrator{algType, IIP, uType, tType, tdirType, ksEltype, SolType,
CacheType} <:
CacheType, DV} <:
AbstractODEIntegrator{algType, IIP, uType, tType}
sol::SolType
u::uType
Expand All @@ -14,6 +14,7 @@ mutable struct HistoryODEIntegrator{algType, IIP, uType, tType, tdirType, ksElty
saveiter::Int
saveiter_dense::Int
cache::CacheType
differential_vars::DV
end

function (integrator::HistoryODEIntegrator)(t, deriv::Type = Val{0}; idxs = nothing)
Expand All @@ -31,7 +32,7 @@ mutable struct DDEIntegrator{algType, IIP, uType, tType, P, eigenType, tTypeNoUn
ksEltype, SolType, F, CacheType, IType, FP, O, dAbsType,
dRelType, H,
tstopsType, discType, FSALType, EventErrorType,
CallbackCacheType} <:
CallbackCacheType, DV} <:
AbstractDDEIntegrator{algType, IIP, uType, tType}
sol::SolType
u::uType
Expand Down Expand Up @@ -89,6 +90,7 @@ mutable struct DDEIntegrator{algType, IIP, uType, tType, P, eigenType, tTypeNoUn
opts::O
stats::DDEStats
history::H
differential_vars::DV
integrator::IType
fsalfirst::FSALType
fsallast::FSALType
Expand All @@ -97,7 +99,7 @@ mutable struct DDEIntegrator{algType, IIP, uType, tType, P, eigenType, tTypeNoUn
function DDEIntegrator{algType, IIP, uType, tType, P, eigenType, tTypeNoUnits,
tdirType, ksEltype, SolType, F, CacheType, IType, FP,
O, dAbsType, dRelType, H, tstopsType, discType,
FSALType, EventErrorType, CallbackCacheType}(sol, u, k, t, dt, f,
FSALType, EventErrorType, CallbackCacheType, DV}(sol, u, k, t, dt, f,
p, uprev, uprev2,
tprev, prev_idx,
prev2_idx,
Expand Down Expand Up @@ -133,15 +135,16 @@ mutable struct DDEIntegrator{algType, IIP, uType, tType, P, eigenType, tTypeNoUn
u_modified, isdae,
opts, stats,
history,
differential_vars,
integrator) where
{algType, IIP, uType, tType, P, eigenType, tTypeNoUnits, tdirType, ksEltype,
SolType, F,
CacheType, IType, FP, O, dAbsType, dRelType, H, tstopsType, discType,
FSALType, EventErrorType, CallbackCacheType}
FSALType, EventErrorType, CallbackCacheType, DV}
new{algType, IIP, uType, tType, P, eigenType, tTypeNoUnits, tdirType, ksEltype,
SolType, F,
CacheType, IType, FP, O, dAbsType, dRelType, H, tstopsType, discType, FSALType,
EventErrorType, CallbackCacheType}(sol, u, k, t, dt, f, p, uprev, uprev2, tprev,
EventErrorType, CallbackCacheType, DV}(sol, u, k, t, dt, f, p, uprev, uprev2, tprev,
prev_idx, prev2_idx, fpsolver,
order_discontinuity_t0,
tracked_discontinuities,
Expand All @@ -159,7 +162,7 @@ mutable struct DDEIntegrator{algType, IIP, uType, tType, P, eigenType, tTypeNoUn
vector_event_last_time,
last_event_error, accept_step, isout,
reeval_fsal, u_modified, isdae, opts,
stats, history, integrator)
stats, history, differential_vars, integrator)
end
end

Expand Down
24 changes: 15 additions & 9 deletions src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,6 @@ function DiffEqBase.__init(prob::DiffEqBase.AbstractDDEProblem,
# get rate prototype
rate_prototype = rate_prototype_of(u0, tspan)

# create a history function
history = build_history_function(prob, alg, rate_prototype, reltol_internal;
dt = dt, dtmin = dtmin, calck = false,
adaptive = adaptive, internalnorm = internalnorm)
f_with_history = ODEFunctionWrapper(f, history)

# get states (possibly different from the ODE integrator!)
u, uprev, uprev2 = u_uprev_uprev2(u0, alg;
alias_u0 = alias_u0,
Expand All @@ -143,6 +137,16 @@ function DiffEqBase.__init(prob::DiffEqBase.AbstractDDEProblem,
uEltypeNoUnits = recursive_unitless_eltype(u)
uBottomEltypeNoUnits = recursive_unitless_bottom_eltype(u)

# get the differential vs algebraic variables
differential_vars = prob isa DAEProblem ? prob.differential_vars : OrdinaryDiffEq.get_differential_vars(f, u)

# create a history function
history = build_history_function(prob, alg, rate_prototype, reltol_internal,
differential_vars;
dt = dt, dtmin = dtmin, calck = false,
adaptive = adaptive, internalnorm = internalnorm)
f_with_history = ODEFunctionWrapper(f, history)

# initialize output arrays of the solution
k = typeof(rate_prototype)[]
ts, timeseries, ks = solution_arrays(u, tspan, rate_prototype;
Expand All @@ -166,14 +170,14 @@ function DiffEqBase.__init(prob::DiffEqBase.AbstractDDEProblem,
# create solution
if iscomposite(alg)
id = OrdinaryDiffEq.CompositeInterpolationData(f_with_history, timeseries, ts, ks,
Int[], dense, cache)
Int[], dense, cache, differential_vars)
sol = DiffEqBase.build_solution(prob, alg.alg, ts, timeseries;
dense = dense, k = ks, interp = id,
alg_choice = id.alg_choice, calculate_error = false,
stats = stats)
else
id = OrdinaryDiffEq.InterpolationData(f_with_history, timeseries, ts, ks, dense,
cache)
cache, differential_vars)
sol = DiffEqBase.build_solution(prob, alg.alg, ts, timeseries;
dense = dense, k = ks, interp = id,
calculate_error = false, stats = stats)
Expand Down Expand Up @@ -406,7 +410,8 @@ end
typeof(tstops_propagated),
typeof(d_discontinuities_propagated),
OrdinaryDiffEq.fsal_typeof(alg.alg, rate_prototype),
typeof(last_event_error), typeof(callback_cache)}(sol, u, k,
typeof(last_event_error), typeof(callback_cache),
typeof(differential_vars)}(sol, u, k,
t0,
tType(dt),
f_with_history,
Expand Down Expand Up @@ -457,6 +462,7 @@ end
opts,
stats,
history,
differential_vars,
ode_integrator)

# initialize DDE integrator
Expand Down
14 changes: 9 additions & 5 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ function Base.sizehint!(sol::DESolution, alg, tspan, tstops, saveat;
nothing
end

function build_history_function(prob, alg, rate_prototype, reltol;
function build_history_function(prob, alg, rate_prototype, reltol, differential_vars;
dt, dtmin, adaptive, calck, internalnorm)
@unpack f, u0, tspan, p = prob

Expand Down Expand Up @@ -303,15 +303,17 @@ function build_history_function(prob, alg, rate_prototype, reltol;
ode_alg_choice = Int[]
ode_id = OrdinaryDiffEq.CompositeInterpolationData(ode_f, ode_timeseries, ode_ts,
ode_ks,
ode_alg_choice, true, ode_cache) # dense = true
ode_alg_choice, true, ode_cache,
differential_vars) # dense = true
ode_sol = DiffEqBase.build_solution(ode_prob, alg.alg, ode_ts, ode_timeseries;
dense = true, k = ode_ks, interp = ode_id,
alg_choice = ode_alg_choice,
calculate_error = false,
stats = DiffEqBase.Stats(0))
else
ode_id = OrdinaryDiffEq.InterpolationData(ode_f, ode_timeseries, ode_ts, ode_ks,
true, ode_cache) # dense = true
true, ode_cache,
differential_vars) # dense = true
ode_sol = DiffEqBase.build_solution(ode_prob, alg.alg, ode_ts, ode_timeseries;
dense = true, k = ode_ks, interp = ode_id,
calculate_error = false,
Expand All @@ -327,15 +329,17 @@ function build_history_function(prob, alg, rate_prototype, reltol;
tdirType = typeof(sign(zero(tType)))
ode_integrator = HistoryODEIntegrator{typeof(alg.alg), isinplace(prob), typeof(prob.u0),
tType, tdirType, typeof(ode_k),
typeof(ode_sol), typeof(ode_cache)}(ode_sol,
typeof(ode_sol), typeof(ode_cache),
typeof(differential_vars)}(ode_sol,
ode_u, ode_k,
t0,
zero(tType),
ode_uprev,
t0, alg.alg,
zero(tType),
tdir, 1, 1,
ode_cache)
ode_cache,
differential_vars)

# combine the user-provided history function and the ODE integrator with dense solution
# to a joint dense history of the DDE
Expand Down
2 changes: 1 addition & 1 deletion test/interface/dependent_delays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ end

@test sol3.errors[:l∞] < 3.0e-6
@test sol3.errors[:final] < 1.4e-7
@test sol3.errors[:l2] < 8.4e-7
@test sol3.errors[:l2] < 9.6e-7

sol4 = solve(prob2, alg; abstol = 1e-13, reltol = 1e-13)

Expand Down

0 comments on commit 122af53

Please sign in to comment.