Skip to content

Commit 0f1cc08

Browse files
Implement CheckInit
Needs tests
1 parent 35ac8d7 commit 0f1cc08

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

lib/OrdinaryDiffEqCore/src/OrdinaryDiffEqCore.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ using DiffEqBase: check_error!, @def, _vec, _reshape
5858

5959
using FastBroadcast: @.., True, False
6060

61-
using SciMLBase: NoInit, _unwrap_val
61+
using SciMLBase: NoInit, CheckInit, _unwrap_val
6262

6363
import SciMLBase: alg_order
6464

lib/OrdinaryDiffEqCore/src/initialize_dae.jl

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,75 @@ function _initialize_dae!(integrator, prob::Union{ODEProblem, DAEProblem},
176176
ReturnCode.InitialFailure)
177177
end
178178
end
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

lib/OrdinaryDiffEqNonlinearSolve/src/OrdinaryDiffEqNonlinearSolve.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ end
3434
using OrdinaryDiffEqCore: resize_nlsolver!, _initialize_dae!,
3535
AbstractNLSolverAlgorithm, AbstractNLSolverCache,
3636
AbstractNLSolver, NewtonAlgorithm, @unpack,
37-
OverrideInit, ShampineCollocationInit, BrownFullBasicInit, _vec,
38-
_unwrap_val, DAEAlgorithm,
37+
OverrideInit, ShampineCollocationInit, BrownFullBasicInit,
38+
_vec, _unwrap_val, DAEAlgorithm,
3939
_reshape, calculate_residuals, calculate_residuals!,
4040
has_special_newton_error, isadaptive,
4141
TryAgain, DIRK, COEFFICIENT_MULTISTEP, NORDSIECK_MULTISTEP, GLM,

0 commit comments

Comments
 (0)