-
-
Notifications
You must be signed in to change notification settings - Fork 221
don't error on implicit parameter equation #3686
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
base: master
Are you sure you want to change the base?
Conversation
The MWE doesn't error for me on MTK#master? julia> using ModelingToolkit
[ Info: Auto-importing using ModelingToolkit: t_nounits as t, D_nounits as D, ModelingToolkit as MTK, observed
julia> @mtkmodel ImplicitForcing begin
@variables begin
u(t), [description = "Input Variable", input=true]
y(t), [description = "fully implicit output", output=true]
end
@equations begin
0 ~ sqrt(u) # implicitly forces output y because u=f(y) in closed loop
end
end
ModelingToolkit.Model{typeof(__ImplicitForcing__), Dict{Symbol, Any}}(__ImplicitForcing__, Dict{Symbol, Any}(:variables => Dict{Symbol, Dict{Symbol, Any}}(:y => Dict(:output => true, :type => Real, :description => "fully implicit output"), :u => Dict(:type => Real, :description => "Input Variable", :input => true)), :kwargs => Dict{Symbol, Dict}(:y => Dict{Symbol, Union{Nothing, DataType}}(:value => nothing, :type => Real), :u => Dict{Symbol, Union{Nothing, DataType}}(:value => nothing, :type => Real)), :independent_variable => :t, :equations => Any["0 ~ sqrt(u)"]), false)
julia> @named implicit = ImplicitForcing()
Model implicit:
Equations (1):
1 standard: see equations(implicit)
Unknowns (2): see unknowns(implicit)
u(t): Input Variable
y(t): fully implicit output
julia> mtkcompile(implicit; inputs = ModelingToolkit.unbound_inputs(implicit)) # errors
Model implicit:
Parameters (1): see parameters(implicit)
u(t): Input Variable |
I'm confused. On d1246c0 i get the error I reported. On master from 1h ago (847c41f) I reproduce your behavior and the equation vanishes completely. Thats also strange behavior because I think it cannot be resolved for Now MTK seems to just ignore the equation and is happy to solve with a parameter which does not fullfil the constraint. See below MWE where I default u to 1 which is incompatible with the equations so it should throw init failure. using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D, unbound_inputs
using OrdinaryDiffEqTsit5
@mtkmodel ImplicitForcing begin
@variables begin
u(t)=1, [description = "Input Variable", input=true]
y(t), [description = "fully implicit output", output=true]
end
@equations begin
0 ~ sqrt(u)
end
end
@named implicit = ImplicitForcing()
sys = mtkcompile(implicit; inputs = unbound_inputs(implicit)) # test for no error
prob = ODEProblem(sys, [], (0.0, 1.0))
sol = solve(prob, Tsit5()) # happy to solve even though constrsaint is not fulfilled |
It won't automatically throw an initial failure. The equation needs to be moved to |
I mean i don't think the equation should be dropped, and why is it? I would expect it to be a a system with one state |
Because it doesn't involve any variables.
Exactly. It should throw an initialization failure. So the equation should be an initialization equation. It's not just "one state" and "one equation". The state has nothing to do with that particular equation. |
But isn't the fact that an input variable is transformed into a parameter in structural simplify more of an implementation detail? My whole point is that I think equations containing inputs cannot be handled the same way as equations only containing fixed parameters. |
Hmmm. Alright, so now the question is where this equation disappears to. |
I guess the recent change which drops the equations rather than keeping it was introduced in #3681 (or any other PR in the last 2 days but i think this one is the most promient as it changes alot in how tearing is handled) |
Okay so the way this worked previously is arguably by accident. First, the system is technically overdetermined since it has one equation for zero unknowns ( Now, tearing for systems goes through Thus in the MWE of this PR, the input equation is an "extra" equation that should be handled by the I believe the fix here is to make the consistency check more strict and simplify this MWE with |
Thanks for the clarification, this explains why it just worked in v9. Regarding the bigger picture: the actual problem seems to be the handling of fully implicit outputs. Previously I hacked around this problem using _to_zero(x) = 0
@register_symbolic _to_zero(x)
eqs = _tozero(y) ~ sqrt(u) which "tricks" MTK into doing the right thing but is not very obvious to users. This still seems to work on master: using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D, unbound_inputs, unbound_outputs
_tozero(x) = 0
@register_symbolic _tozero(x)
@mtkmodel ImplicitForcing begin
@variables begin
u(t), [description = "Input Variable", input=true]
y(t), [description = "fully implicit output", output=true]
end
@equations begin
_tozero(y) ~ sqrt(u) # implicitly forces output y because u=f(y) in closed loop
end
end
@named implicit = ImplicitForcing()
mtkcompile(implicit; inputs = unbound_inputs(implicit), outputs=unbound_outputs(implicit)) # works Maybe the better solution for the problem would be to somehow allow to specify fully implicit outputs on the |
I use MTK to generate "open loop" models. In closed loop loop, it is possible that the input of a system directly depends on its output, i.e.$u_a = f(y_a)$ .
This means you may implicitly force$y_a$ by constraining $u_a$ . Under those conditions, it makes sense to define implicit equations on inputs (which are handled as parameters internally). MTK@10 does not allow this anymore, because it sees an equation containing only parameters and requires it to be an explicit parameter equation.
Instead of throwing, I think it should just leave the equation as it is.
Alternatively, one could check if any of the parameters is an input and throws otherwise.
Example:
Checklist
contributor guidelines, in particular the SciML Style Guide and
COLPRAC.