I have now tried all suggested strategies from (Handling Instability When Solving ODE Problems - #5 by ChrisRackauckas) and have not found a solution that works. It seems there is something unique about my problem that is throwing a wrench in ModelingToolkit/DifferentialEquations.
I feel like the below code should be able to handle the discontinuity of the set point, but it can’t get past 1s where the discontinuity occurs.
using ModelingToolkit
using DifferentialEquations
@parameters kp ki kd m k d
@variables t y(t) e(t) de(t) dde(t) x(t) dx(t) ddx(t)
D = Differential(t)
#set point
sp(t) = t > 1 ? 1.0 : 0.0
@register sp(t)
sys = ODESystem([
D(y) ~ kp*de + ki*e + kd*dde
D(e) ~ de
D(de) ~ dde
D(x) ~ dx
D(dx) ~ ddx
0.0 ~ ( y ) - ( m*ddx + d*dx + k*x )
0.0 ~ ( e ) - ( sp(t) - x )
])
u0 = zeros(7)
p = ones(6)
tspan = (0.0,5.0)
prob = ODEProblem(sys, u0, tspan, p)
condition(u,t,integrator) = t - 1
affect!(integrator) = nothing
cb = ContinuousCallback(condition,affect!,save_positions=(false,false))
sol = solve(prob,Rodas4(autodiff=false), callback=cb)
Am I doing something wrong here?