-
-
Notifications
You must be signed in to change notification settings - Fork 204
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
Errors in affect codegen #2994
Comments
The problem appears related to the equation 0 ~ ifelse((contact == 1), hdd + a1*hd + a0*h + a2*h^3, λ) Since using ModelingToolkit, OrdinaryDiffEq
import ModelingToolkit: t_nounits as t, D_nounits as D
@component function ContactForce2(; name, surface=nothing)
vars = @variables begin
q(t) = 1
v(t) = 0
f(t)
(h(t) = 1), [irreducible=true]
hd(t)
hdd(t)
(λ(t)=0), [irreducible=true]
end
pars = @parameters begin
contact::Int = 0 # discrete.time state variable
a0 = 100
a1 = 20
a2 = 1e6
end
equations = [
0 ~ λ# ifelse((contact == 1), hdd + a1*hd + a0*h + a2*h^3, λ)
f ~ contact*λ
D(q) ~ v
1 * D(v) ~ -1 * 9.81 + f
h ~ q
hd ~ D(h)
hdd ~ D(hd)
]
function affect!(integ, u, p, _)
end
continuous_events = [h ~ 0] => (affect!, [h], [], [], nothing)
ODESystem(equations, t, vars, pars; name, continuous_events)
end
@named model = ContactForce2()
model = complete(model)
ssys = structural_simplify(model)
prob = ODEProblem(ssys, [], (0.0, 5.0))
sol = solve(prob, Rodas4())
using Plots
plot(sol, layout=5) |
Even more weird stuff going on. If I set using ModelingToolkit, OrdinaryDiffEq
import ModelingToolkit: t_nounits as t, D_nounits as D
@component function ContactForce2(; name, surface=nothing)
vars = @variables begin
(q(t) = 1), [irreducible=true]
v(t) = 0
(f(t)=0), [irreducible=true]
(h(t) = 1), [irreducible=true]
(hd(t) = 0)#, [irreducible=true]
hdd(t)#, [irreducible=true]
(λ(t)=0), [irreducible=true]
end
pars = @parameters begin
contact::Int = 0 # discrete.time state variable
a0 = 100
a1 = 20
a2 = 0*1e6
end
equations = [
0 ~ ifelse((contact == 1), hdd + a1*hd + a0*h + a2*h^3, λ)
f ~ contact*λ
D(q) ~ v
1 * D(v) ~ -1 * 9.81 + f
h ~ q
hd ~ D(h)
hdd ~ D(hd)
]
function affect!(integ, u, p, _)
end
continuous_events = [h ~ 0] => (affect!, [h], [], [], nothing)
ODESystem(equations, t, vars, pars; name, continuous_events)
end
@named model = ContactForce2()
model = complete(model)
ssys = structural_simplify(model)
prob = ODEProblem(ssys, [], (0.0, 2.0))
sol = solve(prob, Rodas4(), dtmax=0.001)
using Plots
plot(sol, layout=6, size=(1000, 1000)) |
@BenChung can you take this one? |
This is the bug we talked about on Slack where we reinitialize the system after the affect fires using the initialization system for the initial condition, rather than the "running" condition. I probably can take it - eventually - but I don't know enough about the initialization system to really be able to fix it properly. |
Oh I see. This needs to be handled through what I had mentioned with the ImplicitDiscreteSystem thing if we're to make it robust. |
Yeah. My feeling - for now - is that it's better to not run initialization (or error if there is an initialization) after an affect so that the user doesn't get surprised by it and takes on the responsibility for ensuring that the requirements of a DAE are satisfied, and then we need to revisit this problem once we have better handling for system "live" initialization. |
That's simply impossible. |
Why is running the initialization system required at all? If there are no algebraic equations, the state cannot be in feasible. If there are algebraic equations, the DAE solver will have to find a root to progress anyways and will solve the problem then? The initialization system would be the incorrect system to solve anyways, parameters might have changed and equations for initial condition should no longer be included, it would be a completely different system. |
You always have to run initialization after any
That's the point of the implicit discrete form. |
I have the same probleme with a discrete Callback, states are set to initial value every time the callback is fired. What can I do to get the same behaviour as with MTK v8.73? |
I have a model which is developed with MTK v8.72.0. With v8.75.0 it becomes slow an interrupts without in error after 3 seconds. With v9.19.0 it becomes much slower and interrupts with error at the same time. With >v9.36 it has the Problem described above. I will try to shrink down the system for a example that I can share. But it seems that it's something with the descrete callback. Are there similar known Issues yet? |
@HKruenaegel We should be fixing the incorrect initialization system issue that causes the system to go to all-0s after an affect fires soon(^tm) when my development branch gets merged. I haven't had an opportunity to look into performance, though, so can't comment on that. |
I tried again to run my model with MTKv9.30.0. If I give all states in u0 and the guesses are empty the model works, but as mentioned before it is 30 times slower than with MTKv8.73.2. Is that a general observation with MTK9, @ChrisRackauckas? |
The following system simulates correctly without the event. The event affect function of the event is empty, so I would have expected it to have no effect.
without event an object is falling freely due to gravity
with empty event
The text was updated successfully, but these errors were encountered: