@@ -3,19 +3,22 @@ $(TYPEDSIGNATURES)
3
3
4
4
Generate `ODESystem`, dependent variables, and parameters from an `ODEProblem`.
5
5
"""
6
- function modelingtoolkitize (prob:: DiffEqBase.ODEProblem ; kwargs... )
6
+ function modelingtoolkitize (prob:: DiffEqBase.ODEProblem ; u_names = nothing , p_names = nothing , kwargs... )
7
7
prob. f isa DiffEqBase. AbstractParameterizedFunction &&
8
8
return prob. f. sys
9
- @parameters t
10
-
9
+ t = t_nounits
11
10
p = prob. p
12
11
has_p = ! (p isa Union{DiffEqBase. NullParameters, Nothing})
13
12
14
- _vars = define_vars (prob. u0, t)
13
+ if u_names != = nothing
14
+ _vars = [_defvar (name)(t) for name in u_names]
15
+ else
16
+ _vars = define_vars (prob. u0, t)
17
+ end
15
18
16
19
vars = prob. u0 isa Number ? _vars : ArrayInterface. restructure (prob. u0, _vars)
17
20
params = if has_p
18
- _params = define_params (p)
21
+ _params = define_params (p, p_names )
19
22
p isa Number ? _params[1 ] :
20
23
(p isa Tuple || p isa NamedTuple || p isa AbstractDict ? _params :
21
24
ArrayInterface. restructure (p, _params))
@@ -25,7 +28,7 @@ function modelingtoolkitize(prob::DiffEqBase.ODEProblem; kwargs...)
25
28
26
29
var_set = Set (vars)
27
30
28
- D = Differential (t)
31
+ D = D_nounits
29
32
mm = prob. f. mass_matrix
30
33
31
34
if mm === I
@@ -125,41 +128,89 @@ function Base.showerror(io::IO, e::ModelingtoolkitizeParametersNotSupportedError
125
128
println (io, e. type)
126
129
end
127
130
128
- function define_params (p)
131
+ function varnames_length_check (vars, names)
132
+ if length (names) == length (p)
133
+ throw (ArgumentError ("""
134
+ Number of parameters ($(length (p)) ) does not match number of names \
135
+ ($(length (names)) ).
136
+ """ ))
137
+ end
138
+ end
139
+
140
+ function define_params (p, _ = nothing )
129
141
throw (ModelingtoolkitizeParametersNotSupportedError (typeof (p)))
130
142
end
131
143
132
- function define_params (p:: AbstractArray )
133
- [toparam (variable (:α , i)) for i in eachindex (p)]
144
+ function define_params (p:: AbstractArray , names = nothing )
145
+ if names === nothing
146
+ [toparam (variable (:α , i)) for i in eachindex (p)]
147
+ else
148
+ varnames_length_check (p, names)
149
+ [toparam (variable (name)) for name in names]
150
+ end
134
151
end
135
152
136
- function define_params (p:: Number )
137
- [toparam (variable (:α ))]
153
+ function define_params (p:: Number , names = nothing )
154
+ if names === nothing
155
+ [toparam (variable (:α ))]
156
+ elseif names isa AbstractArray
157
+ varnames_length_check (p, names)
158
+ [toparam (variable (name)) for name in names]
159
+ else
160
+ [toparam (variable (names))]
161
+ end
138
162
end
139
163
140
- function define_params (p:: AbstractDict )
141
- OrderedDict (k => toparam (variable (:α , i)) for (i, k) in zip (1 : length (p), keys (p)))
164
+ function define_params (p:: AbstractDict , names = nothing )
165
+ if names === nothing
166
+ OrderedDict (k => toparam (variable (:α , i)) for (i, k) in zip (1 : length (p), keys (p)))
167
+ else
168
+ varnames_length_check (p, names)
169
+ OrderedDict (k => toparam (variable (names[k])) for k in keys (p))
170
+ end
142
171
end
143
172
144
- function define_params (p:: Union{SLArray, LArray} )
145
- [toparam (variable (x)) for x in LabelledArrays. symnames (typeof (p))]
173
+ function define_params (p:: Union{SLArray, LArray} , names = nothing )
174
+ if names === nothing
175
+ [toparam (variable (x)) for x in LabelledArrays. symnames (typeof (p))]
176
+ else
177
+ varnames_length_check (p, names)
178
+ [toparam (variable (name)) for name in names]
179
+ end
146
180
end
147
181
148
- function define_params (p:: Tuple )
149
- tuple ((toparam (variable (:α , i)) for i in eachindex (p)). .. )
182
+ function define_params (p:: Tuple , names = nothing )
183
+ if names === nothing
184
+ tuple ((toparam (variable (:α , i)) for i in eachindex (p)). .. )
185
+ else
186
+ varnames_length_check (p, names)
187
+ tuple ((toparam (variable (name)) for name in names))
188
+ end
150
189
end
151
190
152
- function define_params (p:: NamedTuple )
153
- NamedTuple (x => toparam (variable (x)) for x in keys (p))
191
+ function define_params (p:: NamedTuple , names = nothing )
192
+ if names === nothing
193
+ NamedTuple (x => toparam (variable (x)) for x in keys (p))
194
+ else
195
+ varnames_length_check (p, names)
196
+ NamedTuple (x => toparam (variable (names[x])) for x in keys (p))
197
+ end
154
198
end
155
199
156
- function define_params (p:: MTKParameters )
200
+ function define_params (p:: MTKParameters , names = nothing )
157
201
bufs = (p... ,)
158
202
i = 1
159
203
ps = []
160
204
for buf in bufs
161
205
for _ in buf
162
- push! (ps, toparam (variable (:α , i)))
206
+ push! (
207
+ ps,
208
+ if names === nothing
209
+ toparam (variable (:α , i))
210
+ else
211
+ toparam (variable (names[i]))
212
+ end
213
+ )
163
214
end
164
215
end
165
216
return identity .(ps)
0 commit comments