Skip to content

Commit 2b65cad

Browse files
authored
Merge pull request #1188 from SciML/sm/hist
Add the `hist` function
2 parents a8609ff + f9e1fd8 commit 2b65cad

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

src/systems/abstractsystem.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -945,12 +945,14 @@ by default.
945945
function extend(sys::AbstractSystem, basesys::AbstractSystem; name::Symbol=nameof(sys))
946946
T = SciMLBase.parameterless_type(basesys)
947947
ivs = independent_variables(basesys)
948-
if length(ivs) == 0
949-
sys = convert_system(T, sys)
950-
elseif length(ivs) == 1
951-
sys = convert_system(T, sys, ivs[1])
952-
else
953-
throw("Extending multivariate systems is not supported")
948+
if !(typeof(sys) <: T)
949+
if length(ivs) == 0
950+
sys = convert_system(T, sys)
951+
elseif length(ivs) == 1
952+
sys = convert_system(T, sys, ivs[1])
953+
else
954+
throw("Extending multivariate systems is not supported")
955+
end
954956
end
955957

956958
eqs = union(equations(basesys), equations(sys))

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)