Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialization on non-DAE models #2512

Merged
merged 27 commits into from
Mar 1, 2024
Merged

Initialization on non-DAE models #2512

merged 27 commits into from
Mar 1, 2024

Conversation

ChrisRackauckas
Copy link
Member

@ChrisRackauckas ChrisRackauckas commented Feb 29, 2024

#2508 pointed out that the heuristic of implicit_dae || calculate_massmatrix(sys) !== I, i.e. "only do initialization on DAEs", while sensible for the DAE model, doesn't quite fit in the MTK context. Instead, what we need to do is check whether the varmap is correct and consistent for the chosen ODE. If it's not consistent for the selected ODE, then we still need to run an initialization step for the resulting ODE solve.

This needs a few changes in the ODE solver as though, since currently only DAE solvers will run the initialization step. Thus the test case:

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

function System(;name)
    vars = @variables begin
        dx(t), [guess=0]
        ddx(t), [guess=0]
    end
    eqs = [
        D(dx) ~ ddx
        0 ~ ddx + dx + 1
    ]
    return ODESystem(eqs, t, vars, []; name)
end

@mtkbuild sys = System()
prob = ODEProblem(sys, [sys.dx => 1], (0,1)) # OK
prob = ODEProblem(sys, [sys.ddx => -2], (0,1), guesses = [sys.dx => 1])
sol = solve(prob, Rodas5P())
sol = solve(prob, Tsit5())

gives an erroneous success as it skips initialization, using prob.u0 == [0.0], the sentinel value since initialization is only run on DAEs. We will need to override that so that initialization is always run on any system that provides an initializeprob.

Fixes #2508

#2508 pointed out that the heuristic of `implicit_dae || calculate_massmatrix(sys) !== I`, i.e. "only do initialization on DAEs", while sensible for the DAE model, doesn't quite fit in the MTK context. Instead, what we need to do is check whether the varmap is correct and consistent for the chosen ODE. If it's not consistent for the selected ODE, then we still need to run an initialization step for the resulting ODE solve.

This needs a few changes in the ODE solver as though, since currently only DAE solvers will run the initialization step. Thus the test case:

```julia
using ModelingToolkit, OrdinaryDiffEq, Test
using ModelingToolkit: t_nounits as t, D_nounits as D

function System(;name)
    vars = @variables begin
        dx(t), [guess=0]
        ddx(t), [guess=0]
    end
    eqs = [
        D(dx) ~ ddx
        0 ~ ddx + dx + 1
    ]
    return ODESystem(eqs, t, vars, []; name)
end

@mtkbuild sys = System()
prob = ODEProblem(sys, [sys.dx => 1], (0,1)) # OK
prob = ODEProblem(sys, [sys.ddx => -2], (0,1), guesses = [sys.dx => 1])
sol = solve(prob, Rodas5P())
sol = solve(prob, Tsit5())
```

gives an erroneous success as it skips initialization, using `prob.u0 == [0.0]`, the sentinel value since initialization is only run on DAEs. We will need to override that so that initialization is always run on any system that provides an initializeprob.
ChrisRackauckas added a commit to SciML/OrdinaryDiffEq.jl that referenced this pull request Feb 29, 2024
@ChrisRackauckas ChrisRackauckas changed the title WIP: Initialization on non-DAE models Initialization on non-DAE models Feb 29, 2024
@ChrisRackauckas
Copy link
Member Author

This also coincidentally adds steady state initializations as almost a free piece, and late binding initialization_eqs

@ChrisRackauckas
Copy link
Member Author

Ran locally since CI is taken over by codecov bumps. Passes!

@ChrisRackauckas ChrisRackauckas merged commit f3de1aa into master Mar 1, 2024
14 of 17 checks passed
@ChrisRackauckas ChrisRackauckas deleted the initialize_nondae branch March 1, 2024 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Non-DAE ODEs Bypass Initialization
1 participant