4

I'm trying to solve an ODE problem (with Julia), that can stop early when a specific condition is satisfied.

Let's say I have a Lorenz system as below

using DifferentialEquations

function lorenz!(du,u)
    du[1] = 10.0*(u[2]-u[1]);
    du[2] = u[1]*(28.0-u[3]) - u[2];
    du[3] = u[1]*u[2] - (8/3)*u[3];
end

u0 = [1.0;0.0;0.0]
tspan = (0.0, 100.0)

prob = ODEProblem(lorenz!,u0, tspan);
sol = solve(prob);

And, For example, I want to stop the ODE solver when u[3] is higher than 10, like below.

sol = solve(prob, stopcondition = u[3]>10);

But I'm not sure that there is a possible way to stop ODE solver with a given condition.

Any relevant comments would be thankful :)

c0000000kie
  • 119
  • 3

1 Answers1

6

Yes, use the terminate!(integrator) functionality within the event handling system. That would look like this here:

condition(u,t,integrator) = u[3] - 10 # Is zero when u[3] = 10
affect!(integrator) = terminate!(integrator)
cb = ContinuousCallback(condition,affect!)
sol = solve(prob, callback = cb);
Chris Rackauckas
  • 18,645
  • 3
  • 50
  • 81