Kramer-type SDE - conditionally stop solution process

Hi there,

I am trying to solve a Kramer-type SDE, simulating a particle in a double well.
I use the DifferentialEquations package and it all works well until the particle reaches the top of the barrier and start rolling down, at which point understandably Julia stops and issues the warning

โ”Œ Warning: Interrupted. Larger maxiters is needed.
โ”” @ DiffEqBase C:\Users\Musto Family.julia\packages\DiffEqBase\gQwlE\src\integrator_interface.jl:319

The question is, is there a way to stop the solution after a certain condition is attained?
For example, in the simple test case I describe below, I use a double well potential

  x^2-x^4

and once the position is well past the top of the barrier at approx. 0.707, the solution should be stopped.
The general problem I have in mind is more complex and the condition might be more complicated of course.
I thought about adding an artificial energy barrier, but I was wondering if there is a more elegant way. Besides, in the multi-dimensional case I am aiming at, it would be rather cumbersome.

I reproduced the problem in a minimal working example, which I copy below, if it could be of any use.
Thanks a lot, looking forward to any suggestions and hints.

    using DifferentialEquations
   using Plots

   function kramer_1D(du,u,p,t)
   du[1] = u[2]
   du[2] = -(2*u[1]+4*u[1]^3)
   end

  function ฯƒ_kramer_1D(du,u,p,t)
  du[2] = 0.03
  end

 u0 = zeros(2,1)
 tspan = (0.0,30000.0)

 prob_sde_kramer_1D = SDEProblem(kramer_1D,ฯƒ_kramer_1D,u0,tspan)
 sol = solve(prob_sde_kramer_1D)
 plot(sol,vars=(1))

https://docs.sciml.ai/latest/features/callback_functions/#Example-2:-Terminating-an-Integration-1

1 Like

Ah well that is just stunning. I should have searched the documentation better, I got to that section but overlooked the example.
Thanks a lot.

Updated: Event Handling and Callback Functions ยท DifferentialEquations.jl