A minor issue first: you can just easily write
terminate_affect!(integrator) = terminate!(integrator)
Now for the major issue: why aren’t you happy with the result you get? If I run the code with the terminate_condition
relaxed, that is, I set
function terminate_condition(u,t,integrator)
u[2] < -10e6
end
and I plot the results (but just the points that were obtained by the solver, nothing interpolated afterwards), I get this:
Note that the ninth component of the simulated sequence of u[2]
s (in the figure labelled as x
) is already below 0.5. Hence the eighth u[2]
was the last one that satisfied the condition.
For convenience the whole code here:
using DifferentialEquations
u0 = [1.,0.]
harmonic! = @ode_def HarmonicOscillator begin
dv = -x
dx = v
end
tspan = (0.0,10.0)
prob = ODEProblem(harmonic!,u0,tspan)
function terminate_condition(u,t,integrator)
u[2] < -10e6
end
terminate_affect!(integrator) = terminate!(integrator)
terminate_cb = ContinuousCallback(terminate_condition,terminate_affect!)
sol = solve(prob,callback=terminate_cb,dense=true)
using Plots
scatter(sol,denseplot=false)
savefig("discourse.png")