@@ -102,36 +102,59 @@ function generate_initializesystem(sys::ODESystem;
102
102
if pmap isa SciMLBase. NullParameters
103
103
pmap = Dict ()
104
104
end
105
+ pmap = todict (pmap)
105
106
for p in parameters (sys)
106
- # If either of them are `missing` the parameter is an unknown
107
- # But if the parameter is passed a value, use that as an additional
108
- # equation in the system
109
- if (_val1 = get (pmap, p, nothing )) === missing || get (defs, p, nothing ) === missing
107
+ if is_parameter_solvable (p, pmap, defs, guesses)
108
+ # If either of them are `missing` the parameter is an unknown
109
+ # But if the parameter is passed a value, use that as an additional
110
+ # equation in the system
111
+ _val1 = get (pmap, p, nothing )
112
+ _val2 = get (defs, p, nothing )
113
+ _val3 = get (guesses, p, nothing )
110
114
varp = tovar (p)
111
115
paramsubs[p] = varp
112
- if _val1 != = nothing && _val1 != = missing
113
- push! (eqs_ics, varp ~ _val1)
114
- end
115
- if ! haskey (guesses, p)
116
- error (" Invalid setup: parameter $(p) has no default value or initial guess" )
116
+ # Has a default of `missing`, and (either an equation using the value passed to `ODEProblem` or a guess)
117
+ if _val2 === missing
118
+ if _val1 != = nothing && _val1 != = missing
119
+ push! (eqs_ics, varp ~ _val1)
120
+ push! (u0, varp => _val1)
121
+ elseif _val3 != = nothing
122
+ # assuming an equation exists (either via algebraic equations or initialization_eqs)
123
+ push! (u0, varp => _val1)
124
+ elseif check_defguess
125
+ error (" Invalid setup: parameter $(p) has no default value, initial value, or guess" )
126
+ end
127
+ # `missing` passed to `ODEProblem`, and (either an equation using default or a guess)
128
+ elseif _val1 === missing
129
+ if _val2 != = nothing && _val2 != = missing
130
+ push! (eqs_ics, varp ~ _val2)
131
+ push! (u0, varp => _val2)
132
+ elseif _val3 != = nothing
133
+ push! (u0, varp => _val1)
134
+ elseif check_defguess
135
+ error (" Invalid setup: parameter $(p) has no default value, initial value, or guess" )
136
+ end
137
+ # No value passed to `ODEProblem`, but a default and a guess are present
138
+ # _val2 !== missing is implied by it falling this far in the elseif chain
139
+ elseif _val1 === nothing && _val2 != = nothing && _val3 != = nothing
140
+ push! (eqs_ics, varp ~ _val2)
141
+ push! (u0, varp => _val3)
142
+ else
143
+ # _val1 !== missing and _val1 !== nothing, so a value was provided to ODEProblem
144
+ # This would mean `is_parameter_solvable` returned `false`, so we never end up
145
+ # here
146
+ error (" This should never be reached" )
117
147
end
118
- push! (u0, varp => guesses[p])
119
148
end
120
149
end
121
150
pars = vcat (
122
151
[get_iv (sys)],
123
152
[p for p in parameters (sys) if ! haskey (paramsubs, p)]
124
153
)
125
- pdeps = parameter_dependencies (sys)
126
- if ! isempty (pdeps)
127
- pdep_eqs = [k ~ v for (k, v) in pdeps]
128
- else
129
- pdep_eqs = Equation[]
130
- end
131
154
nleqs = if algebraic_only
132
- [eqs_ics; observed (sys); pdep_eqs ]
155
+ [eqs_ics; observed (sys)]
133
156
else
134
- [eqs_ics; get_initialization_eqs (sys); initialization_eqs; observed (sys); pdep_eqs ]
157
+ [eqs_ics; get_initialization_eqs (sys); initialization_eqs; observed (sys)]
135
158
end
136
159
nleqs = Symbolics. substitute .(nleqs, (paramsubs,))
137
160
unks = [full_states; collect (values (paramsubs))]
@@ -151,6 +174,15 @@ function generate_initializesystem(sys::ODESystem;
151
174
return sys_nl
152
175
end
153
176
177
+ function is_parameter_solvable (p, pmap, defs, guesses)
178
+ _val1 = pmap isa AbstractDict ? get (pmap, p, nothing ) : nothing
179
+ _val2 = get (defs, p, nothing )
180
+ _val3 = get (guesses, p, nothing )
181
+ # either (missing is a default or was passed to the ODEProblem) or (nothing was passed to
182
+ # the ODEProblem and it has a default and a guess)
183
+ return (_val1 === missing || _val2 === missing ) || (_val1 === nothing && _val2 != = nothing && _val3 != = nothing )
184
+ end
185
+
154
186
function SciMLBase. remake_initializeprob (sys:: ODESystem , odefn, u0, t0, p)
155
187
if (u0 === missing || ! (eltype (u0) <: Pair ) || isempty (u0)) &&
156
188
(p === missing || ! (eltype (p) <: Pair ) || isempty (p))
@@ -162,13 +194,16 @@ function SciMLBase.remake_initializeprob(sys::ODESystem, odefn, u0, t0, p)
162
194
if p === missing
163
195
p = Dict ()
164
196
end
197
+ if t0 === nothing
198
+ t0 = 0.0
199
+ end
165
200
u0 = todict (u0)
166
201
p = todict (p)
167
202
initprob = InitializationProblem (sys, t0, u0, p)
168
203
initprobmap = getu (initprob, unknowns (sys))
169
204
punknowns = [p for p in all_variable_symbols (initprob) if is_parameter (sys, p)]
170
205
getpunknowns = getu (initprob, punknowns)
171
- setpunknowns = setp_oop (sys, punknowns)
206
+ setpunknowns = setp (sys, punknowns)
172
207
initprobpmap = GetUpdatedMTKParameters (getpunknowns, setpunknowns)
173
208
return initprob, initprobmap, initprobpmap
174
209
end
0 commit comments