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

feat: add hooks for OverrideInit #517

Merged
merged 13 commits into from
Dec 15, 2024

Conversation

AayushSabharwal
Copy link
Member

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

Add any other context about the problem here.

kwargs...
)
return NonlinearSolveNoInitCache(prob, alg, args, kwargs)
cache = NonlinearSolveNoInitCache(
prob, alg, args, kwargs, initializealg, ReturnCode.Success)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
prob, alg, args, kwargs, initializealg, ReturnCode.Success)
prob, alg, args, kwargs, initializealg, ReturnCode.Default)

Probably should be this

)
end

NonlinearSolveBase.run_initialization!(cache)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be inside the @static_timeit?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

)
end

NonlinearSolveBase.run_initialization!(cache)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again with the @static_timeit

)
end

NonlinearSolveBase.run_initialization!(cache)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@static_timeit

@avik-pal
Copy link
Member

Can you briefly mention the motivation behind this? I am not too familiar with the background (other PRs for DAEs and stuff)

@AayushSabharwal
Copy link
Member Author

Sure. MTK currently generates initialization problems for ODEs. SciML/ModelingToolkit.jl#3253 aims to expand this to all such problems. The basic use case is that users can specify initial conditions for variables that MTK eliminates from the system, and the initialization process solves for the states of the system. As a contrived example, consider:

y ~ x^3
x^2 + y^2 ~ 2

MTK will reduce this to a system with a single unknown x and a single equation (the latter). It will dynamically calculate y inside the f provided to NonlinearFunction. Currently, the user has to specify an initial condition for x. With OverrideInit, the user can specify an initial condition for y (say 3) and MTK will generate the following InitializationProblem (NonlinearProblem):

y ~ 3
y ~ x^3

and solve for x. As an added check, it ensures overspecified initial conditions are consistent. e.g. in the example above, if the initial condition [x => 3, y => 3] is specified, MTK generates a NonlinearLeastSquaresProblem:

x ~ 3
y ~ 3
y ~ x^3

This fails the nonlinear solve with ReturnCode.InitialFailure telling the user their initial conditions are inconsistent/unsatisfiable.

MTK can also potentially solve for parameters, such as if x + y ~ p^2 is an equation, the user can specify x and y and MTK will generate a NonlinearProblem to solve for p.

The core infrastructure is in SciMLBase, but all solver libraries need to hook into it. In most cases, this is a trivial change: https://github.com/SciML/StochasticDelayDiffEq.jl/pull/84/files. NonlinearSolve has a billion entry points, multiple different "integrators" (caches) and a whole host of other intricacies which make this PR more complicated.

@AayushSabharwal AayushSabharwal marked this pull request as ready for review December 14, 2024 07:43
@AayushSabharwal AayushSabharwal changed the title [WIP] feat: add hooks for OverrideInit feat: add hooks for OverrideInit Dec 14, 2024
@ChrisRackauckas ChrisRackauckas merged commit 8295933 into SciML:master Dec 15, 2024
75 of 93 checks passed
@avik-pal
Copy link
Member

@AayushSabharwal feel free to tag the appropriate releases. All of the (sub)-packages probably need a version bump I think

@AayushSabharwal AayushSabharwal deleted the as/initialization branch December 16, 2024 05:38
@AayushSabharwal
Copy link
Member Author

I'll PR to bump versions, but I don't have write access to be able to tag them

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.

3 participants