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

Custom independent variable leads to underdetermined initialization system #2818

Closed
hersle opened this issue Jun 25, 2024 · 3 comments · Fixed by #2862
Closed

Custom independent variable leads to underdetermined initialization system #2818

hersle opened this issue Jun 25, 2024 · 3 comments · Fixed by #2862
Labels
bug Something isn't working

Comments

@hersle
Copy link
Contributor

hersle commented Jun 25, 2024

This trivial example works as it should, where the independent parameter t is imported as ModelingToolkit.t_nounits:

using ModelingToolkit
using DifferentialEquations
using ModelingToolkit: t_nounits as t, D_nounits as d
@variables x(t)
@named sys1 = ODESystem([d(x) ~ 0], t; initialization_eqs = [x ~ t], guesses = [x => 0.0])
prob1 = ODEProblem(structural_simplify(sys1), [], (1.0, 2.0), [])
sol1 = solve(prob1)

Now run the same example, but using our own independent parameter (T instead of t) and differential (Dinstead of d):

using ModelingToolkit
using DifferentialEquations
@variables T; D = Differential(T) # only difference from first example
@variables x(T)
@named sys2 = ODESystem([D(x) ~ 0], T; initialization_eqs = [x ~ T], guesses = [x => 0.0])
prob2 = ODEProblem(structural_simplify(sys2), [], (1.0, 2.0), [])
sol2 = solve(prob2)

In the second example (and not the first), the initialization system becomes underdetermined, as reported by the warning

┌ Warning: Initialization system is underdetermined. 1 equations for 2 unknowns. Initialization will default to using least squares. To suppress this warning pass warn_initialize_determined = false.
└ @ ModelingToolkit ~/.julia/packages/ModelingToolkit/353ne/src/systems/diffeqs/abstractodesystem.jl:1564

By inspecting structural_simplify(generate_initializesystem(sys2); fully_determined = false), I see that the bug is caused by the independent parameter appearing twice, as both a parameter and unknown:

Model sys2 with 1 equations
Unknowns (2):
  T
  x(T) [defaults to 0.0]
Parameters (1):
  T

I expected the second example to behave like the first. This is with ModelingToolkit v9.19.0.

@hersle hersle added the bug Something isn't working label Jun 25, 2024
@ChrisRackauckas
Copy link
Member

Yeah that's an odd bug. I'll see if I can look during JuliaCon.

@AayushSabharwal
Copy link
Member

The problem (I'm fairly certain) is structural_simplify uses metadata to identify variables vs parameters. Since T is an @variable, it doesn't have the metadata and structural_simplify gets confused. The default indepvar works because it's an @parameter.

@hersle
Copy link
Contributor Author

hersle commented Jul 16, 2024

Thanks! You are right. Making it a parameter makes everything work as expected:

using ModelingToolkit
using DifferentialEquations
@parameters T
D = Differential(T) # only difference from first example
@variables x(T)
@named sys2 = ODESystem([D(x) ~ 0], T; initialization_eqs = [x ~ T], guesses = [x => 0.0])
prob2 = ODEProblem(structural_simplify(sys2), [], (1.0, 2.0), [])
sol2 = solve(prob2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants