hgrecco / numbakit-ode

Leveraging numba to speed up ODE integration
Other
68 stars 3 forks source link

Implemented support for Events #14

Closed hgrecco closed 3 years ago

hgrecco commented 3 years ago

See #13

A few comments:

import numpy as np

def upward_cannon(t, y):
    return np.asarray([y[1], -0.5])

def hit_ground(t, y):
    return y[0]

def apex(t, y):
    return y[1]

hit_ground.terminal = True
hit_ground.direction = -1

step = .11

import nbkode

print('\nrun')
sol = nbkode.AdamsBashforth1(upward_cannon, 0., [0., 10.], h=step)
t, y = sol.run(100)

print(t)
print(y)

print('\nrun_events empty')
sol = nbkode.AdamsBashforth1(upward_cannon, 0., [0., 10.], h=step)
t, y, t_events, y_events = sol.run_events(100, events=())

print(t)
print(y)
print(t_events)
print(y_events)

print('\nhit ground')
sol = nbkode.AdamsBashforth1(upward_cannon, 0., [0., 10.], h=step)
t, y, t_events, y_events = sol.run_events(100, events=hit_ground)

print(t)
print(y)
print(t_events)
print(y_events)

print('\nhit ground and apex')
sol = nbkode.AdamsBashforth1(upward_cannon, 0., [0., 10.], h=step)
t, y, t_events, y_events = sol.run_events(100, events=(hit_ground, apex))

print(t)
print(y)
print(t_events)
print(y_events)
hgrecco commented 3 years ago

If there are no complains I will merge this early next week.