-
-
Notifications
You must be signed in to change notification settings - Fork 210
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
Error: ArgumentError: xxx
is neither an observed nor an unknown variable.
#3138
Comments
I can't reproduce your error. When running the example, I get: julia> @mtkbuild sys = System()
ERROR: UndefVarError: `Aₒ` not defined in `ModelingToolkitStandardLibrary.Hydraulic.IsothermalCompressible`
Suggestion: check for spelling errors or missing imports.
Stacktrace:
[1]
@ ModelingToolkitStandardLibrary.Hydraulic.IsothermalCompressible ~/Julia/SciML/ModelingToolkit.jl/src/systems/model_parsing.jl:908
[2] __Orifice__
@ ~/Julia/SciML/ModelingToolkit.jl/src/systems/model_parsing.jl:138 [inlined]
[3] #_#433
@ ~/Julia/SciML/ModelingToolkit.jl/src/systems/model_parsing.jl:25 [inlined]
[4] macro expansion
@ ~/Julia/SciML/ModelingToolkit.jl/src/systems/abstractsystem.jl:2051 [inlined]
[5]
@ Main ~/Julia/SciML/ModelingToolkit.jl/src/systems/model_parsing.jl:138
[6] __System__
@ ~/Julia/SciML/ModelingToolkit.jl/src/systems/model_parsing.jl:138 [inlined]
[7] #_#433
@ ~/Julia/SciML/ModelingToolkit.jl/src/systems/model_parsing.jl:25 [inlined]
[8] top-level scope
@ ~/Julia/SciML/ModelingToolkit.jl/src/systems/abstractsystem.jl:2051
Some type information was truncated. Use `show(err)` to see complete types. |
@AayushSabharwal I think that comment is related to the port names in your Orifice model. Can you share the code for your orifice model? |
I'm using whatever is on ModelingToolkitStandardLibrary#Hydraulic |
I made some changes to it that I haven't pushed yet. Also, I don't have the permissions to push them anymore and need to get them reinstated. I'll let you know when they are. |
Okay, I created a fork There are some changes that need to be made to the model toot though. #MWE Model Showing Initialization issue with a mass @mtkbuild sys = System() initialization_eqs = [ initsys = ModelingToolkit.generate_initializesystem(sys; initialization_eqs) prob = ODEProblem(sys, u0, (0, 1), []) |
I updated the using ModelingToolkitStandardLibrary.Hydraulic.IsothermalCompressible: FixedPressure,
Orifice, Volume,
HydraulicFluid, HydraulicPort, liquid_density
using ModelingToolkitStandardLibrary.Blocks: Ramp, Constant
using ModelingToolkitStandardLibrary.Mechanical.Translational: Force, MechanicalPort, Mass
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
using OrdinaryDiffEq
@mtkmodel System begin
@components begin
src = FixedPressure(p = 101325)
res = Orifice()
act = Volume(direction = -1, area = 0.1)
mass = Mass(m=1e-5)
fluid = HydraulicFluid(density = 1000, bulk_modulus = 2.0e9)
end
@equations begin
connect(src.port, res.port_a)
connect(res.port_b, act.port)
connect(act.flange, mass.flange)
connect(fluid, src.port)
end
end
@mtkbuild sys = System()
initialization_eqs = [
sys.act.x ~ 1
sys.act.dx ~ 0
]
initsys = ModelingToolkit.generate_initializesystem(sys; initialization_eqs)
initsys = structural_simplify(initsys)
initprob = NonlinearProblem(initsys, [t=>0], [])
initsol = solve(initprob; abstol=1e-6)
initsol[sys.mass.s] |
Apparently this happens because |
And yet it's an unknown of julia> unknowns(sys)
6-element Vector{SymbolicUtils.BasicSymbolic{Real}}:
act₊x(t)
act₊rho(t)
mass₊s(t)
mass₊v(t)
act₊p(t)
act₊drho(t) How is this possible? |
I don't understand why it isn't possible 😅 Let me ask this question: how is it meant to solve for the initial value of @variables x(t) y(t)
@mtkbuild sys = ODESystem([D(x) ~ y, y ~ 3t], t) If I now do |
The initialization system is formed from:
|
@AayushSabharwal Would mass.s be in the defaults mentioned above If we set a guess for s? @component function Mass(; name, m, g = 0)
end |
Guesses are different. Guesses are the |
Would another way of making your point here be that the system has not steady-state value - since there is no limit to x and no force resisting the motion of the mass? |
No, steady state values are unrelated. @variables x(t) y(t)
@mtkbuild sys = ODESystem([D(x) ~ y, y ~ 3t], t; defaults = [x => 2y + 1])
|
I think the best question to think about regarding your problem is, for the system in this issue if you were given initial values for all other variables, how would you then calculate the initial value of |
Oh, I think I got it. At t=0 there is a means to determine x. |
Well, if you have act.x then you know mass.s |
So the model works if you change the initial conditions accordingly:
However, could the initialization check have figured that out for us? |
MTK has no reason to assume and no basis to figure out that |
I think the question is, how are you expecting MTK to know that |
what I mean is that when I initialize, it would be good if I got an error indicated that sys.mass.s is missing. |
It tells you exactly that??? using ModelingToolkitStandardLibrary.Hydraulic.IsothermalCompressible: FixedPressure,
Orifice, Volume,
HydraulicFluid, HydraulicPort, liquid_density
using ModelingToolkitStandardLibrary.Blocks: Ramp, Constant
using ModelingToolkitStandardLibrary.Mechanical.Translational: Force, MechanicalPort, Mass
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
using OrdinaryDiffEq
@mtkmodel System begin
@components begin
src = FixedPressure(p = 101325)
res = Orifice()
act = Volume(direction = -1, area = 0.1)
mass = Mass(m=1e-5)
fluid = HydraulicFluid(density = 1000, bulk_modulus = 2.0e9)
end
@equations begin
connect(src.port, res.port_a)
connect(res.port_b, act.port)
connect(act.flange, mass.flange)
connect(fluid, src.port)
end
end
@mtkbuild sys = System()
initialization_eqs = [
sys.act.x ~ 1
sys.act.dx ~ 0
]
ODEProblem(sys, [], (0.0,1.0); initialization_eqs)
|
I get this error when I comment out the initial condition for mass.s:
|
Is this with the code in #3138 (comment)? |
I think I misunderstood you on the call then. The initializationsystem alone will never tell you what's missing or not. |
It is with this code: `@mtkmodel System begin @mtkbuild sys = System() initialization_eqs = [ initsys = ModelingToolkit.generate_initializesystem(sys; initialization_eqs) prob = ODEProblem(sys, u0, (0, 1), []) |
Yeah so you shouldn't be solving the initializationsystem manually. Constructing the |
That's because you're not initializing, you're purposefully skipping the standard pipeline to user lower level debugging / development utilities and those obviously cannot error because then there would be no way to look what is being computed. I don't know what to say other than, use the documented standard path instead of the internals if you want the standard errors. |
Let me bring this to a higher level. The way symbolic-numeric compilation works is:
As a debugging procedure, and because it's an open compiler that is built in Julia and thus generally all internals are accessible, we allow people to do (2) in an interactive session, which can be a nice way to ask "why is it not able to solve for Your question is essentially, "I did (2) so why didn't (3) run?". If the code that creates the symbolic representation errored if the representation is invalid, then it would be impossible to ever investigate why it's invalid. Thus it makes sense to have one function generate the initialization system, and then another function check whether the generated initialization system is fully well-defined and throw errors / warnings. You are skipping "normal MTK" and just calling that first part, so you get what you asked for. So then the next question is, why are you doing this? What led you to this code? Since the only place in the entire documentation which mentions this is right here https://docs.sciml.ai/ModelingToolkit/stable/tutorials/initialization/#Diving-Deeper:-Constructing-the-Initialization-System, and it says it's a debugging utility to do in order to investigate initialization, I do not understand how with our documentation how you can get to this code without already doing the "normal thing". So I'm really curious, what led you to write or try this code? If it's some tutorial that's outside the docs, we should really clean that up. |
Thanks @ChrisRackauckas and @AayushSabharwal this is clear now. I was using the prob = ODEProblem(sys, [], (0,1)) # ERROR: Initialization incomplete. Missing ... SymbolicUtils.BasicSymbolic{Real}[mass₊s(t)]
prob = ODEProblem(sys, [sys.mass.s => 1], (0,1)) # Warning: Initialization system is underdetermined. 2 equations for 4 unknowns.
prob = ODEProblem(sys, [sys.mass.s => 1, sys.act.x => 1], (0,1)) # Warning: Initialization system is underdetermined. 2 equations for 3 unknowns.
prob = ODEProblem(sys, [sys.mass.s => 1, sys.act.x => 1, sys.act.dx => 1], (0,1)) #OK |
prob = ODEProblem(sys, [sys.mass.s => 1], (0,1)) # Warning: Initialization system is underdetermined. 2 equations for 4 unknowns.
prob = ODEProblem(sys, [sys.mass.s => 1, sys.act.x => 1], (0,1)) # Warning: Initialization system is underdetermined. 2 equations for 3 unknowns. I wonder how hard in the underdetermined cases here it would be to bubble up some message of what equation is likely missing. The problem is that there's likely an unlimited number of things you could define to make it determined, so it might just not be possible. |
If we can get our hands on |
Yeah it's just a heuristic so I'm not sure how helpful it ends up being in practice. |
MWE (note this requires the Hydraulic branch for the ModelingToolkitStandardLibrary): The following code produces the error "ERROR: ArgumentError: mass₊s(t) is neither an observed nor an unknown variable."
The full stack trace is:
The current environment is:
The text was updated successfully, but these errors were encountered: