Skip to content

Commit a05a778

Browse files
committed
Fix #911
1 parent e1fcaf2 commit a05a778

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ModelingToolkit"
22
uuid = "961ee093-0014-501f-94e3-6117800e7a78"
33
authors = ["Chris Rackauckas <[email protected]>"]
4-
version = "5.13.5"
4+
version = "5.13.6"
55

66
[deps]
77
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"

src/systems/diffeqs/modelingtoolkitize.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ function modelingtoolkitize(prob::DiffEqBase.ODEProblem)
1414
p = prob.p
1515
end
1616

17+
has_p = !(p isa Union{DiffEqBase.NullParameters,Nothing})
18+
1719
var(x, i) = Num(Sym{FnType{Tuple{symtype(t)}, Real}}(nameof(Variable(x, i))))
1820
vars = ArrayInterface.restructure(prob.u0,[var(:x, i)(ModelingToolkit.value(t)) for i in eachindex(prob.u0)])
19-
params = p isa DiffEqBase.NullParameters ? [] :
20-
reshape([Num(Sym{Real}(nameof(Variable(, i)))) for i in eachindex(p)],size(p))
21+
params = has_p ? reshape([Num(Sym{Real}(nameof(Variable(, i)))) for i in eachindex(p)],size(p)) : []
2122
var_set = Set(vars)
2223

2324
D = Differential(t)
@@ -52,10 +53,12 @@ function modelingtoolkitize(prob::DiffEqBase.ODEProblem)
5253
else
5354
vec(collect(params))
5455
end
56+
default_u0 = Dict(sts .=> vec(collect(prob.u0)))
57+
default_p = has_p ? Dict(params .=> vec(collect(prob.p))) : Dict()
5558

5659
de = ODESystem(
5760
eqs, t, sts, params,
58-
defaults=merge(Dict(sts .=> vec(collect(prob.u0))), Dict(params .=> vec(collect(prob.p)))),
61+
defaults=merge(default_u0, default_p),
5962
)
6063

6164
de

test/modelingtoolkitize.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,10 @@ prob = ODEProblem(pendulum_sys, Pair[], tspan)
185185
sol = solve(prob, Rodas4())
186186
l2 = sol[sts[1]].^2 + sol[sts[3]].^2
187187
@test all(l->abs(sqrt(l) - 1) < 0.05, l2)
188+
189+
ff911 = (du,u,p,t) -> begin
190+
du[1] = u[2] + 1.0
191+
du[2] = u[1] - 1.0
192+
end
193+
prob = ODEProblem(ff911, zeros(2), (0, 1.0))
194+
@test_nowarn modelingtoolkitize(prob)

0 commit comments

Comments
 (0)