DAE initialization
A DAEProblem, or an ODEProblem whose mass matrix has zero rows and columns, has to start where its algebraic equations hold. The initializealg keyword picks how, with the algorithms OrdinaryDiffEq takes, which come from DiffEqBase:
CheckInit(), the default for a problem without initialization data, evaluates the residual atu0, and atdu0for aDAEProblem, and throws aCheckInitFailureErrorwhen its RMS norm exceedsabstol, taken per component whenabstolis a vector.OverrideInit(), the default for a problem that carries initialization data, as ModelingToolkit's do, solves the problem's own initialization system and takes the state and the parameters it gives. On a problem without such data it does nothing.BrownFullBasicInit()keeps the differential variables and solves for the algebraic ones, and for aDAEProblemalso for the derivatives of the differential ones, which needsdifferential_vars. Its ownabstol,1e-10unless given, decides whether to solve.ShampineCollocationInit(initdt)takes one backward Euler step and starts from where it lands. The step isinitdtas given. Without one it runs towardtf, and for anODEProblemit is OrdinaryDiffEq's:dt / 5, at mostdtmax, whendtis given, and a thousandth of the span otherwise. For aDAEProblemit is a tenth ofdtmax, the span by default, att0 = 0, and the smaller of that and|t0| / 1000elsewhere. OrdinaryDiffEq ignoresinitdtfor aDAEProblem, and steps differently there whent0is negative or the span runs backward.NoInit()starts fromu0as given.
The two that solve use PETSc's SNES with the Jacobian the solve itself uses: the problem's jac, the ForwardDiff one or PETSc's finite differences, sparse under a jac_prototype. So they take no nlsolve, and when SNES fails the solve returns at t0 with ReturnCode.InitialFailure. A start that already passes the check is left untouched, and reinit! initializes again unless given reinit_dae = false.
On a communicator other than MPI.COMM_SELF they run wherever they run serially, and SNES solves the whole state on the communicator, each rank its own rows, with the distributed Jacobian of the solve: the problem's jac filling this rank's rows of the sparse jac_prototype, or PETSc's colouring of that prototype, and with a dm the problem's jac filling the DM's matrix, or the DM's colouring without one. Its linear solves are GMRES with block Jacobi, one ILU(0) block on each rank, to a relative tolerance of the square root of the precision's spacing, and the solve's petsc_options do not reach them. A row BrownFullBasicInit() does not solve keeps its variable there, so it takes a mass matrix whose zero columns are its zero rows, as a Diagonal one's are, and refuses a sparse one where they differ. When SNES fails the solve returns at t0 with ReturnCode.InitialFailure on every rank, and an f or jac that throws on some ranks makes every rank throw, as in a solve.
OverrideInit() goes through SciMLBase's get_initial_values, as OrdinaryDiffEq's does, and PETSc's SNES solves the initialization system with a finite-difference Jacobian, to the solve's abstol unless OverrideInit(; abstol) gives its own, so no solver package has to be loaded. SNES takes a NonlinearProblem, and a NonlinearLeastSquaresProblem with as many equations as unknowns. An SCCNonlinearProblem, which ModelingToolkit builds for a fully determined system, goes block by block through SCCNonlinearSolve, which ModelingToolkit loads: SNES solves the nonlinear blocks and LinearSolve the linear ones. A system with more or fewer equations than unknowns, which ModelingToolkit warns about, is refused, as is any other kind of initialization problem; OverrideInit(; nlsolve = alg) hands the system to that solver instead, for those a least-squares one from NonlinearSolve. When the solver fails the solve returns at t0 with ReturnCode.InitialFailure.
The parameters the initialization gives are the ones the solve uses, and the ones in sol.prob.p and integrator.p. It runs where OrdinaryDiffEq runs it: at init and solve, on an ODEProblem without a mass matrix too, in reinit!, in initialize_dae!, after a callback on a DAEProblem or a mass-matrix problem, and before the step after set_u! or derivative_discontinuity!(integrator, true) on one. The problem's hooks are given the problem at init, solve and reinit! and the integrator afterwards. No check follows it, as none does in OrdinaryDiffEq. It does not run on a communicator other than MPI.COMM_SELF, and PETScAdjoint does not differentiate it; initializealg = CheckInit() starts both from the values as given.
initialize_dae!(integrator, initializealg) runs the same on the integrator's current state and time, with the initializealg the solve was given unless another is passed, and writes the result into PETSc. For a DAEProblem it takes the derivative the integrator holds, as OrdinaryDiffEq's takes integrator.du: the one its last step or initialization paired with the state, which get_du gives, or the problem's du0 where it holds none, as after set_t!. It takes the current abstol and, for ShampineCollocationInit() on an ODEProblem, the current dt / 5, as OrdinaryDiffEq's does. When SNES fails the integrator finishes where it is with ReturnCode.InitialFailure, and on an ODEProblem without a singular mass matrix only OverrideInit() does anything. After a callback's affect! runs without calling derivative_discontinuity!(integrator, false), or its initialize calls derivative_discontinuity!(integrator, true), the integrator is initialized again with the callback's initializealg, or the solve's when the callback has none, as OrdinaryDiffEq does. set_u! and derivative_discontinuity!(integrator, true) between steps have the next step do the same with the solve's initializealg, and restart PETSc's step as a callback does. So with the default a callback has to leave the algebraic equations satisfied or the solve throws CheckInitFailureError, and under BrownFullBasicInit() the algebraic variables are solved for again. On a DAEProblem the derivative is again the one the integrator holds, the slope at the end of the step the callback came after, which for a ContinuousCallback lies past the event, and CheckInit() takes a state the callback left alone as consistent. On a mass-matrix problem it takes a callback that leaves the residual of the algebraic equations as it was, and finite, as leaving them consistent, since the state PETSc gives at an event or a step end can miss them by more than abstol. The derivative the initialization ends with is the one get_du gives afterwards.