@@ -176,3 +176,75 @@ function _initialize_dae!(integrator, prob::Union{ODEProblem, DAEProblem},
176176 ReturnCode. InitialFailure)
177177 end
178178end
179+
180+ # # CheckInit
181+
182+ function _initialize_dae! (integrator, prob:: ODEProblem , alg:: CheckInit ,
183+ isinplace:: Val{true} )
184+ @unpack p, t, f = integrator
185+ M = integrator. f. mass_matrix
186+ tmp = first (get_tmp_cache (integrator))
187+ u0 = integrator. u
188+
189+ algebraic_vars = [all (iszero, x) for x in eachcol (M)]
190+ algebraic_eqs = [all (iszero, x) for x in eachrow (M)]
191+ (iszero (algebraic_vars) || iszero (algebraic_eqs)) && return
192+ update_coefficients! (M, u0, p, t)
193+ f (tmp, u0, p, t)
194+ tmp .= ArrayInterface. restructure (tmp, algebraic_eqs .* _vec (tmp))
195+
196+ normresid = integrator. opts. internalnorm (tmp, t)
197+ if normresid > integrator. opts. abstol
198+ error (" CheckInit specified but initialization not satisifed. normresid = $normresid > abstol = $(integrator. opts. abstol ) " )
199+ end
200+ end
201+
202+ function _initialize_dae! (integrator, prob:: ODEProblem , alg:: CheckInit ,
203+ isinplace:: Val{false} )
204+ @unpack p, t, f = integrator
205+ u0 = integrator. u
206+ M = integrator. f. mass_matrix
207+
208+ algebraic_vars = [all (iszero, x) for x in eachcol (M)]
209+ algebraic_eqs = [all (iszero, x) for x in eachrow (M)]
210+ (iszero (algebraic_vars) || iszero (algebraic_eqs)) && return
211+ update_coefficients! (M, u0, p, t)
212+ du = f (u0, p, t)
213+ resid = _vec (du)[algebraic_eqs]
214+
215+ normresid = integrator. opts. internalnorm (tmp, t)
216+ if normresid > integrator. opts. abstol
217+ error (" CheckInit specified but initialization not satisifed. normresid = $normresid > abstol = $(integrator. opts. abstol ) " )
218+ end
219+ end
220+
221+ function _initialize_dae! (integrator, prob:: DAEProblem ,
222+ alg:: CheckInit , isinplace:: Val{true} )
223+ @unpack p, t, f = integrator
224+ u0 = integrator. u
225+ resid = get_tmp_cache (integrator)[2 ]
226+
227+ f (resid, integrator. du, u0, p, t)
228+ normresid = integrator. opts. internalnorm (resid, t)
229+ if normresid > integrator. opts. abstol
230+ error (" CheckInit specified but initialization not satisifed. normresid = $normresid > abstol = $(integrator. opts. abstol ) " )
231+ end
232+ end
233+
234+ function _initialize_dae! (integrator, prob:: DAEProblem ,
235+ alg:: CheckInit , isinplace:: Val{false} )
236+ @unpack p, t, f = integrator
237+ u0 = integrator. u
238+
239+ nlequation_oop = u -> begin
240+ f ((u - u0) / dt, u, p, t)
241+ end
242+
243+ nlequation = (u, _) -> nlequation_oop (u)
244+
245+ resid = f (integrator. du, u0, p, t)
246+ normresid = integrator. opts. internalnorm (resid, t)
247+ if normresid > integrator. opts. abstol
248+ error (" CheckInit specified but initialization not satisifed. normresid = $normresid > abstol = $(integrator. opts. abstol ) " )
249+ end
250+ end
0 commit comments