Skip to content

Commit f9e1fd8

Browse files
YingboMashashi
andcommitted
Add history metadata and the hist function
Co-authored-by: "Shashi Gowda" <[email protected]>
1 parent 32864c6 commit f9e1fd8

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

src/utils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ end
108108
function check_variables(dvs, iv)
109109
for dv in dvs
110110
isequal(iv, dv) && throw(ArgumentError("Independent variable $iv not allowed in dependent variables."))
111-
isequal(iv, iv_from_nested_derivative(dv)) || throw(ArgumentError("Variable $dv is not a function of independent variable $iv."))
111+
occursin(iv, iv_from_nested_derivative(dv)) || throw(ArgumentError("Variable $dv is not a function of independent variable $iv."))
112112
end
113113
end
114114

src/variables.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,9 @@ function _varmap_to_vars(varmap::Dict, varlist; defaults=Dict(), check=false, to
7575
end
7676

7777
@noinline throw_missingvars(vars) = throw(ArgumentError("$vars are missing from the variable map."))
78+
79+
struct IsHistory end
80+
ishistory(x) = ishistory(unwrap(x))
81+
ishistory(x::Symbolic) = getmetadata(x, IsHistory, false)
82+
hist(x, t) = wrap(hist(unwrap(x), t))
83+
hist(x::Symbolic, t) = setmetadata(toparam(similarterm(x, operation(x), [unwrap(t)], metadata=metadata(x))), IsHistory, true)

test/odesystem.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,3 +458,12 @@ end
458458

459459
@variables t
460460
@named sys = ODESystem([0 ~ sys1.y + sys2.y ], t; systems=[sys1, sys2])
461+
462+
# DelayDiffEq
463+
using ModelingToolkit: hist
464+
@variables t x(t) y(t)
465+
D = Differential(t)
466+
xₜ₋₁ = hist(x, t-1)
467+
eqs = [D(x) ~ x * y
468+
D(y) ~ y * x - xₜ₋₁]
469+
@named sys = ODESystem(eqs, t)

0 commit comments

Comments
 (0)