Skip to content

Commit ea26d58

Browse files
feat: support observed function generation for DDEs
1 parent 928ee8d commit ea26d58

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/systems/abstractsystem.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,10 @@ function SymbolicIndexingInterface.observed(
805805
return let _fn = _fn
806806
fn1(u, p, t) = _fn(u, p, t)
807807
fn1(u, p::MTKParameters, t) = _fn(u, p..., t)
808+
809+
# DDEs
810+
fn1(u, histfn, p, t) = _fn(u, histfn, p, t)
811+
fn1(u, histfn, p::MTKParameters, t) = _fn(u, histfn, p..., t)
808812
fn1
809813
end
810814
else

src/systems/diffeqs/odesystem.jl

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,9 @@ function build_explicit_observed_function(sys, ts;
411411
ts = [ts]
412412
end
413413
ts = unwrap.(ts)
414+
if is_dde(sys)
415+
ts = map(x -> delay_to_function(sys, x), ts)
416+
end
414417

415418
vars = Set()
416419
foreach(v -> vars!(vars, v; op), ts)
@@ -483,8 +486,12 @@ function build_explicit_observed_function(sys, ts;
483486
end
484487
ts = map(t -> substitute(t, subs), ts)
485488
obsexprs = []
489+
486490
for i in 1:maxidx
487491
eq = obs[i]
492+
if is_dde(sys)
493+
eq = delay_to_function(sys, eq)
494+
end
488495
lhs = eq.lhs
489496
rhs = eq.rhs
490497
push!(obsexprs, lhs rhs)
@@ -505,12 +512,17 @@ function build_explicit_observed_function(sys, ts;
505512
ps = (DestructuredArgs(unwrap.(ps), inbounds = !checkbounds),)
506513
end
507514
dvs = DestructuredArgs(unknowns(sys), inbounds = !checkbounds)
515+
if is_dde(sys)
516+
dvs = (dvs, DDE_HISTORY_FUN)
517+
else
518+
dvs = (dvs,)
519+
end
508520
if inputs === nothing
509-
args = param_only ? [ps..., ivs...] : [dvs, ps..., ivs...]
521+
args = param_only ? [ps..., ivs...] : [dvs..., ps..., ivs...]
510522
else
511523
inputs = unwrap.(inputs)
512524
ipts = DestructuredArgs(inputs, inbounds = !checkbounds)
513-
args = param_only ? [ipts, ps..., ivs...] : [dvs, ipts, ps..., ivs...]
525+
args = param_only ? [ipts, ps..., ivs...] : [dvs..., ipts, ps..., ivs...]
514526
end
515527
pre = get_postprocess_fbody(sys)
516528

@@ -546,6 +558,20 @@ function build_explicit_observed_function(sys, ts;
546558
end
547559
end
548560

561+
function populate_delays(delays::Set, obsexprs, histfn, sys, sym)
562+
_vars_util = vars(sym)
563+
for v in _vars_util
564+
v in delays && continue
565+
iscall(v) && issym(operation(v)) && (args = arguments(v); length(args) == 1) &&
566+
iscall(only(args)) || continue
567+
568+
idx = variable_index(sys, operation(v)(get_iv(sys)))
569+
idx === nothing && error("Delay term $v is not an unknown in the system")
570+
push!(delays, v)
571+
push!(obsexprs, v histfn(only(args))[idx])
572+
end
573+
end
574+
549575
function _eq_unordered(a, b)
550576
length(a) === length(b) || return false
551577
n = length(a)

0 commit comments

Comments
 (0)