Closed lixiao00093 closed 1 month ago
Looks to be due to an odd side effect in the new default ODE algorithm when used with mass matrix ODEs and specifically when it hits an FSAL algorithm as the initial choice. Fixed by https://github.com/SciML/OrdinaryDiffEq.jl/pull/2470
This issue still exists. similar to this issue: https://github.com/SciML/OrdinaryDiffEq.jl/issues/2441. For the example above, it works to choose the other solver, such as Rodas4(). But for the default solver, it still occurs the same errors.
using ModelingToolkit, DifferentialEquations, Plots
using ModelingToolkit: t_nounits as t, D_nounits as D
@parameters g
@variables x(t) y(t) [state_priority = 10] λ(t)
eqs = [D(D(x)) ~ λ * x
D(D(y)) ~ λ * y - g
x^2 + y^2 ~ 1]
function bb_affect!(integ, u, p, ctx)
println("change the parameters! t = <$(integ.t)> ")
integ.ps[p.g] = ctx
end
reflect = [1] => (bb_affect!, [], [g], [g], 1.1)
pend = ODESystem(eqs, t; name=:pend, discrete_events = reflect)
sys = structural_simplify(pend)
prob = ODEProblem(sys, [x =>1, λ => 0], (0, 1.5), [g => 1], guesses = [y => 1])
sol = solve(prob)
the output is:
change the parameters! t = <1.0>
change the parameters! t = <1.0>
UndefRefError: access to undefined reference
if I use solver Rodas4(), the output is:
change the parameters! t = <1.0>
retcode: Success
In addition, if I choose a solver like Rodas4(), I think still faces problems. I can solve my mass matrix ODEs on the ModelingToolkit v9.38.0, but I cannot solve that on the later version. The Rodas4() indicates an unstable problem just after the callback function. I know in v9.39 the default and guess scheme has changed, so I have also changed all the default values into guess values and given the initial condition to the ODE problem. Formulating the ODE problem is no problem. Solving ODE problem without the callback is no problem. So I guess there are still some issues there regarding the callback function or reinitialization. This issue I cannot reproduce in this simple case right now. I will find way to reproduce it.
using the code from the documents to reproduce the error:
I get the error as follows:
Is this a bug? Or do I not use it correctly? I am using the ModelingToolkit v9.39.1.
This is likely related to the reinitialization after the discrete events. If I make the time shorter, there is no error:
I think I am using the discrete event in a correct way, because it works in the ODE system. For example in the following codes:
I just create these cases to show this issue, there is no practical meaning in these cases. Cross your fingers to fix this problem sooner.