@@ -8,20 +8,15 @@ function modelingtoolkitize(prob::DiffEqBase.ODEProblem; kwargs...)
8
8
return prob. f. sys
9
9
@parameters t
10
10
11
- if prob. p isa Tuple || prob. p isa NamedTuple
12
- p = [x for x in prob. p]
13
- else
14
- p = prob. p
15
- end
16
-
11
+ p = prob. p
17
12
has_p = ! (p isa Union{DiffEqBase. NullParameters,Nothing})
18
13
19
14
_vars = define_vars (prob. u0,t)
20
15
21
16
vars = prob. u0 isa Number ? _vars : ArrayInterface. restructure (prob. u0,_vars)
22
17
params = if has_p
23
18
_params = define_params (p)
24
- p isa Number ? _params[1 ] : ArrayInterface. restructure (p,_params)
19
+ p isa Number ? _params[1 ] : (p isa Tuple || p isa NamedTuple ? _params : ArrayInterface. restructure (p,_params) )
25
20
else
26
21
[]
27
22
end
@@ -55,7 +50,8 @@ function modelingtoolkitize(prob::DiffEqBase.ODEProblem; kwargs...)
55
50
eqs = vcat ([lhs[i] ~ rhs[i] for i in eachindex (prob. u0)]. .. )
56
51
57
52
sts = vec (collect (vars))
58
- params = if ndims (params) == 0
53
+
54
+ params = if params isa Array && ndims (params) == 0
59
55
[params[1 ]]
60
56
else
61
57
vec (collect (params))
@@ -83,6 +79,14 @@ function define_vars(u::Union{SLArray,LArray},t)
83
79
_vars = [_defvar (x, t)(ModelingToolkit. value (t)) for x in LabelledArrays. symnames (typeof (u))]
84
80
end
85
81
82
+ function define_vars (u:: Tuple ,t)
83
+ _vars = tuple ((_defvaridx (:x , i, t)(ModelingToolkit. value (t)) for i in eachindex (u)). .. )
84
+ end
85
+
86
+ function define_vars (u:: NamedTuple ,t)
87
+ _vars = NamedTuple (x=> _defvar (x, t)(ModelingToolkit. value (t)) for x in keys (u))
88
+ end
89
+
86
90
function define_params (p)
87
91
[Num (toparam (Sym {Real} (nameof (Variable (:α , i))))) for i in eachindex (p)]
88
92
end
@@ -91,6 +95,14 @@ function define_params(p::Union{SLArray,LArray})
91
95
[Num (toparam (Sym {Real} (nameof (Variable (x))))) for x in LabelledArrays. symnames (typeof (p))]
92
96
end
93
97
98
+ function define_params (p:: Tuple )
99
+ tuple ((Num (toparam (Sym {Real} (nameof (Variable (:α , i))))) for i in eachindex (p)). .. )
100
+ end
101
+
102
+ function define_params (p:: NamedTuple )
103
+ @show NamedTuple (x=> Num (toparam (Sym {Real} (nameof (Variable (x))))) for x in keys (p))
104
+ end
105
+
94
106
95
107
"""
96
108
$(TYPEDSIGNATURES)
@@ -101,15 +113,19 @@ function modelingtoolkitize(prob::DiffEqBase.SDEProblem; kwargs...)
101
113
prob. f isa DiffEqBase. AbstractParameterizedFunction &&
102
114
return (prob. f. sys, prob. f. sys. states, prob. f. sys. ps)
103
115
@parameters t
104
- if prob. p isa Tuple || prob. p isa NamedTuple
105
- p = [x for x in prob. p]
116
+ p = prob. p
117
+ has_p = ! (p isa Union{DiffEqBase. NullParameters,Nothing})
118
+
119
+ _vars = define_vars (prob. u0,t)
120
+
121
+ vars = prob. u0 isa Number ? _vars : ArrayInterface. restructure (prob. u0,_vars)
122
+ params = if has_p
123
+ _params = define_params (p)
124
+ p isa Number ? _params[1 ] : (p isa Tuple || p isa NamedTuple ? _params : ArrayInterface. restructure (p,_params))
106
125
else
107
- p = prob . p
126
+ []
108
127
end
109
- var (x, i) = Num (Sym {FnType{Tuple{symtype(t)}, Real}} (nameof (Variable (x, i))))
110
- vars = ArrayInterface. restructure (prob. u0,[var (:x , i)(ModelingToolkit. value (t)) for i in eachindex (prob. u0)])
111
- params = p isa DiffEqBase. NullParameters ? [] :
112
- reshape ([Num (Sym {Real} (nameof (Variable (:α , i)))) for i in eachindex (p)],size (p))
128
+
113
129
114
130
D = Differential (t)
115
131
0 commit comments