Indexing a residual of an InitializationProblem

I want to check the residual of the solution of an initializationproblem as done in this example: Initialization of ODESystems · ModelingToolkit.jl

However, my model is quite a bit more complex than the one in the example. So I would like to be able to index the residuals:

using ModelingToolkit, OrdinaryDiffEq
using ModelingToolkit: t_nounits as t, D_nounits as D

@parameters g
@variables x(t) y(t) [state_priority = 10] λ(t)
eqs = [D(D(x)) ~ λ * x
       D(D(y)) ~ λ * y - g
       x^2 + y^2 ~ 1]
@mtkbuild pend = ODESystem(eqs, t)

iprob = ModelingToolkit.InitializationProblem(pend, 0.0,
    [x => 1, y => 0.0, D(y) => 2.0, λ => 1], [g => 1], guesses = [λ => 1])
using NonlinearSolve
sol = solve(iprob)
sol.resid[x] # indexing the residual

But sol.resid[x] doesn’t work. Is this possible in any other way?

This should work, assuming the order of sol.resid is the same as the order of equations(sol.prob.f.sys)

for (u, e) in zip(sol.resid, equations(sol.prob.f.sys))
    println(u, "\t", e)
end

Yeah open an issue. This would be a good debugging tool, we just haven’t setup fancy indexing on this yet.

1 Like

Submitted an issue here: Indexing a residual of an InitializationProblem · Issue #3260 · SciML/ModelingToolkit.jl · GitHub