I want to solve for the time taken to return to the initial condition. As a toy example, let's take a simple harmonic oscillator d/dt (x, y) = (y, -x), with initial condition (x,y) = (1,0).
I want the code to return T = 6.28... (= 2π), since this is the time taken to return to the initial condition with y positive. It should then stop integration when it reaches this point.
import numpy as np
from scipy.integrate import solve_ivp
def f(t,x):
return [x[1], -x[0]]
def event(t,x):
return x[0]
event.direction = 1
event.terminal = True # Try false, this gives the result but integrates beyond the event I need.
initial_condition = [0, 1]
sol = solve_ivp(f, [0, 10], initial_condition, method='RK45',dense_output=True, \
events = event, rtol=1e-6, atol=3e-6)
print(sol.t_events)
The trouble is setting event.terminal = True
boots the integration straight away since the event is satisfied at t = 0. Is there any way to have the integration stopping only once the event has been satisfied twice?