Skip to content

Commit 0a0a7cb

Browse files
Merge pull request #1005 from SciML/mtk_scalar
Fix modelingtoolkitize on scalar functions
2 parents 3401696 + e325419 commit 0a0a7cb

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/systems/diffeqs/modelingtoolkitize.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,15 @@ function modelingtoolkitize(prob::DiffEqBase.ODEProblem)
1717
has_p = !(p isa Union{DiffEqBase.NullParameters,Nothing})
1818

1919
var(x, i) = Num(Sym{FnType{Tuple{symtype(t)}, Real}}(nameof(Variable(x, i))))
20-
vars = ArrayInterface.restructure(prob.u0,[var(:x, i)(ModelingToolkit.value(t)) for i in eachindex(prob.u0)])
21-
params = has_p ? reshape([Num(toparam(Sym{Real}(nameof(Variable(, i))))) for i in eachindex(p)],size(p)) : []
20+
_vars = [var(:x, i)(ModelingToolkit.value(t)) for i in eachindex(prob.u0)]
21+
vars = prob.u0 isa Number ? _vars : ArrayInterface.restructure(prob.u0,_vars)
22+
params = if has_p
23+
_params = [Num(toparam(Sym{Real}(nameof(Variable(, i))))) for i in eachindex(p)]
24+
p isa Number ? _params[1] : reshape(_params,size(p))
25+
else
26+
[]
27+
end
28+
2229
var_set = Set(vars)
2330

2431
D = Differential(t)
@@ -64,8 +71,6 @@ function modelingtoolkitize(prob::DiffEqBase.ODEProblem)
6471
de
6572
end
6673

67-
68-
6974
"""
7075
$(TYPEDSIGNATURES)
7176

test/modelingtoolkitize.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,16 @@ ff911 = (du,u,p,t) -> begin
192192
end
193193
prob = ODEProblem(ff911, zeros(2), (0, 1.0))
194194
@test_nowarn modelingtoolkitize(prob)
195+
196+
k(x,p,t) = p*x
197+
x0 = 1.0
198+
p = 0.98
199+
tspan = (0.0,1.0)
200+
prob = ODEProblem(k,x0,tspan,p)
201+
sys = modelingtoolkitize(prob)
202+
203+
k(x,p,t) = 0.98*x
204+
x0 = 1.0
205+
tspan = (0.0,1.0)
206+
prob = ODEProblem(k,x0,tspan)
207+
sys = modelingtoolkitize(prob)

0 commit comments

Comments
 (0)