6060
6161function Base. showerror (io:: IO , e:: CheckInitFailureError )
6262 print (io,
63- " DAE initialization failed: your u0 did not satisfy the initialization requirements,
64- normresid = $(e. normresid) > abstol = $(e. abstol) ."
65- )
63+ """
64+ DAE initialization failed: your u0 did not satisfy the initialization requirements, \
65+ normresid = $(e. normresid) > abstol = $(e. abstol) .
66+ """ )
6667
6768 if e. isdae
68- print (io, " If you wish for the system to
69- automatically change the algebraic variables to satisfy the algebraic constraints,
70- please pass `initializealg = BrownBasicInit()` to solve (this option will require
71- `using OrdinaryDiffEqNonlinearSolve`). If you wish to perform an initialization on the
72- complete u0, please pass `initializealg = ShampineCollocationInit()` to solve. Note that
73- initialization can be a very difficult process for DAEs and in many cases can be
74- numerically intractable without symbolic manipulation of the system. For an automated
75- system that will generate numerically stable initializations, see ModelingToolkit.jl
76- structural simplification for more details."
77- )
69+ print (io,
70+ """
71+ If you wish for the system to automatically change the algebraic variables to \
72+ satisfy the algebraic constraints, please pass `initializealg = BrownBasicInit()` \
73+ to solve (this option will require `using OrdinaryDiffEqNonlinearSolve`). If you \
74+ wish to perform an initialization on the complete u0, please pass \
75+ `initializealg = ShampineCollocationInit()` to `solve`. Note that initialization \
76+ can be a very difficult process for DAEs and in many cases can be numerically \
77+ intractable without symbolic manipulation of the system. For an automated \
78+ system that will generate numerically stable initializations, see \
79+ ModelingToolkit.jl structural simplification for more details.
80+ """ )
7881 end
7982end
8083
@@ -188,6 +191,9 @@ Keyword arguments:
188191 provided to the `OverrideInit` constructor takes priority over this keyword argument.
189192 If the former is `nothing`, this keyword argument will be used. If it is also not provided,
190193 an error will be thrown.
194+
195+ In case the initialization problem is trivial, `nlsolve_alg`, `abstol` and `reltol` are
196+ not required.
191197"""
192198function get_initial_values (prob, valp, f, alg:: OverrideInit ,
193199 iip:: Union{Val{true}, Val{false}} ; nlsolve_alg = nothing , abstol = nothing , reltol = nothing , kwargs... )
@@ -201,35 +207,55 @@ function get_initial_values(prob, valp, f, alg::OverrideInit,
201207 initdata:: OverrideInitData = f. initialization_data
202208 initprob = initdata. initializeprob
203209
204- nlsolve_alg = something (nlsolve_alg, alg. nlsolve, Some (nothing ))
205- if nlsolve_alg === nothing && state_values (initprob) != = nothing
206- throw (OverrideInitMissingAlgorithm ())
207- end
208-
209210 if initdata. update_initializeprob! != = nothing
210211 initdata. update_initializeprob! (initprob, valp)
211212 end
212213
213- if alg. abstol != = nothing
214- _abstol = alg. abstol
215- elseif abstol != = nothing
216- _abstol = abstol
214+ if is_trivial_initialization (initdata)
215+ nlsol = initprob
216+ success = true
217217 else
218- throw (OverrideInitNoTolerance (:abstol ))
218+ nlsolve_alg = something (nlsolve_alg, alg. nlsolve, Some (nothing ))
219+ if nlsolve_alg === nothing && state_values (initprob) != = nothing
220+ throw (OverrideInitMissingAlgorithm ())
221+ end
222+ if alg. abstol != = nothing
223+ _abstol = alg. abstol
224+ elseif abstol != = nothing
225+ _abstol = abstol
226+ else
227+ throw (OverrideInitNoTolerance (:abstol ))
228+ end
229+ if alg. reltol != = nothing
230+ _reltol = alg. reltol
231+ elseif reltol != = nothing
232+ _reltol = reltol
233+ else
234+ throw (OverrideInitNoTolerance (:reltol ))
235+ end
236+ nlsol = solve (initprob, nlsolve_alg; abstol = _abstol, reltol = _reltol)
237+ success = SciMLBase. successful_retcode (nlsol)
219238 end
220- if alg. reltol != = nothing
221- _reltol = alg. reltol
222- elseif reltol != = nothing
223- _reltol = reltol
224- else
225- throw (OverrideInitNoTolerance (:reltol ))
226- end
227- nlsol = solve (initprob, nlsolve_alg; abstol = _abstol, reltol = _reltol)
228239
229240 u0 = initdata. initializeprobmap (nlsol)
230241 if initdata. initializeprobpmap != = nothing
231242 p = initdata. initializeprobpmap (valp, nlsol)
232243 end
233244
234- return u0, p, SciMLBase. successful_retcode (nlsol)
245+ return u0, p, success
246+ end
247+
248+ is_trivial_initialization (:: Nothing ) = true
249+
250+ function is_trivial_initialization (initdata:: OverrideInitData )
251+ ! (initdata. initializeprob isa NonlinearLeastSquaresProblem) &&
252+ state_values (initdata. initializeprob) === nothing
253+ end
254+
255+ function is_trivial_initialization (f:: AbstractSciMLFunction )
256+ has_initialization_data (f) && is_trivial_initialization (f. initialization_data)
257+ end
258+
259+ function is_trivial_initialization (prob:: AbstractSciMLProblem )
260+ is_trivial_initialization (prob. f)
235261end
0 commit comments