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.38k stars 196 forks source link

Cannot initialize system with default that depends on observed variable #2792

Closed hersle closed 3 weeks ago

hersle commented 3 weeks ago

I expect this to work:

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
@variables x(t) y(t) z(t)
@mtkbuild sys = ODESystem([
    D(x) ~ 0.0
    D(y) ~ 0.0
    z ~ 2.0
], t; defaults = [y => z])
prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [])

I specified x => 1.0, and the default should make y => z ~ 2.0. But (on master) it fails with

ERROR: LoadError: ArgumentError: Any[z(t)] are either missing from the variable map or missing from the system's unknowns/parameters list.
hersle commented 3 weeks ago

Sorry, think I disagree with myself. I should probably have used initialization_eqs = [y ~ z] instead and solve the problem:

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
using DifferentialEquations
@variables x(t) y(t) z(t)
@mtkbuild sys = ODESystem([
    D(x) ~ 0.0
    D(y) ~ 0.0
    z ~ 2.0
], t; initialization_eqs = [y ~ z])
prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [])
sol = solve(prob)

Now sol[y] == 2.0, as expected.

ChrisRackauckas commented 2 weeks ago

Okay so all of your issues are now handled?

hersle commented 2 weeks ago

Not quite 😅 I have some workarounds here and there in my real application. But I will try to cook them down to better MWEs first. But it is improving every day!