-
-
Notifications
You must be signed in to change notification settings - Fork 217
Setu is ignored by solve #3552
New issue
Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “No 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? No Sign in to your account
Comments
See https://docs.sciml.ai/ModelingToolkit/stable/tutorials/initialization/#Constant-constraints-in-initialization. Constant constraints in initialization have to be updated by setting |
Thanks, that works! |
@AayushSabharwal is it correct that |
|
I don't think this is a sufficient answer. Users using |
|
But what if I want MTK to respect |
I really like the |
Sorry for the delay. In such a case, you could pass |
This is also now resolved in MTK@9.76. You should always set |
This works with the original mwe I provided, using a using ModelingToolkit, OrdinaryDiffEq
using ModelingToolkit: D_nounits as D, t_nounits as t, setu, setp, getu, getp
Ts = 0.1
@mtkmodel Pendulum begin
@parameters begin
g = 9.8
L = 0.4
K = 1.2
m = 0.3
τ = 0.0 # input
end
@variables begin
θ(t) = 0.0 # state
ω(t) = 0.0 # state
y(t) # output
end
@equations begin
D(θ) ~ ω
D(ω) ~ -g/L*sin(θ) - K/m*ω + τ/m/L^2
y ~ θ * 180 / π
end
end
@mtkbuild mtk_model = Pendulum()
prob = ODEProblem(mtk_model, nothing, (0.0, Ts); initializealg=CheckInit())
integ = init(prob, Tsit5())
set_x = setu(mtk_model, Initial.(unknowns(mtk_model)))
get_x = getu(mtk_model, unknowns(mtk_model))
set_u = setp(mtk_model, [mtk_model.τ])
get_u = getp(mtk_model, [mtk_model.τ])
get_h = getu(mtk_model, [mtk_model.y])
p = (integ, set_x, set_u, get_h)
function f!(xnext, x, u, _, p)
(integ, set_x, set_u, _) = p
set_x(integ, x)
set_u(integ, u)
reinit!(integ; reinit_dae=true)
step!(integ)
xnext .= integ.u # sol.u is the state, called x in the function
nothing
end
xnext = zeros(2)
println("Initial")
f!(xnext, zeros(2), 0.0, nothing, p)
@show xnext
println("Just changing state x - doesn't work, xnext stays at zero")
f!(xnext, ones(2), 0.0, nothing, p)
@show xnext
println("Just changing input u - does work, xnext is non-zero")
f!(xnext, zeros(2), 1.0, nothing, p)
@show xnext
nothing output:
|
When using setu to update the problem state, this new state is simply ignored by solve. Using setp to update the parameters works fine.
MWE:
From this discussion
The text was updated successfully, but these errors were encountered: