SciML / ModelingToolkit.jl

An acausal modeling framework for automatically parallelized scientific machine learning (SciML) in Julia. A computer algebra system for integrated symbolics for physics-informed machine learning and automated transformations of differential equations
https://mtk.sciml.ai/dev/
Other
1.41k stars 203 forks source link

Solve does not solve initialization problem of remade problems #2842

Open hersle opened 2 months ago

hersle commented 2 months ago

I want to solve an ODESystem for many values of a parameter P, where the initial value for an unknown x depends on P (here I write a nonlinear initialization equation to emphasize the general case where defaults do not suffice):

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

@variables x(t)
@parameters P
@named sys = ODESystem([D(x) ~ 0], t, [x], [P]; initialization_eqs = [x^2 ~ P], guesses = [x => +1.0])
ssys = structural_simplify(sys)

prob1 = ODEProblem(ssys, [], (0.0, 1.0), [P => 1.0])
prob2 = remake(prob1, p = [P => 4.0])
sol2 = solve(prob2)
@test sol2[x][begin] == 2.0

This fails because remake (or the following solve) does not resolve the initialization problem. Currently, I am resorting to reconstructing the ODEProblem with new parameters every time, which is very slow.

I think there should be an efficient and well-supported way to remake and then (re)solve an ODEProblem, where the full initialization system is also solved before the ODE is integrated.

hersle commented 2 months ago

Is a fix to this already underway with the PRs linked in #2747? If so I apologize for the spam 😅 Anyway, I think the example above motivates this feature very well.