patrick-kidger / patrick-kidger/diffrax
inf values after triggering event function.
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 2.1k
- Forks
- 189
- Avg merge
- 3d 18h
- Merged PRs (30d)
- 1
Description
I am solving a simple problem below using DiscreteTerminatingEvent. Once the event is triggered, the integration stops, but the solver returns 'inf' values for the time steps following the event's trigger time. Is there a way to avoid this, so that the solver returns function evaluations only for the time steps before the event-trigger time, similar to how solve_ivp in SciPy does?
"import jax.numpy as jnp
import matplotlib.pyplot as plt
from diffrax import diffeqsolve, ODETerm, SaveAt, Tsit5, Dopri5, DiscreteTerminatingEvent
def vector_field(t, y, args):
prey, predator = y
α, β, γ, δ = args
d_prey = α * prey - β * prey * predator
d_predator = -γ * predator + δ * prey * predator
return jnp.array([d_prey, d_predator])
'''Define the terminating event function with two conditions'''
def terminating_event_fxn(state, args, **kwargs):
prey_population = state.y[0]
predator_population = state.y[1]
A = (prey_population < 5) | (predator_population > 15)
return A
'''Set up the ODE term, solver, and the initial conditions'''
term = ODETerm(vector_field)
solver = Dopri5()
t0 = 0
t1 = 140
dt0 = 0.1
y0 = jnp.array([10.0, 10.0])
args = (0.1, 0.02, 0.4, 0.02)
saveat = SaveAt(ts=jnp.linspace(t0, t1, 1000))
'''Define the terminating event'''
terminating_event = DiscreteTerminatingEvent(terminating_event_fxn)
'''Solve the ODE with the terminating event'''
sol = diffeqsolve(term, solver, t0, t1, dt0, y0, args=args, saveat=saveat, discrete_terminating_event=terminating_event)
'''Plot the results'''
plt.plot(sol.ts, sol.ys[:, 0], label="Prey")
plt.plot(sol.ts, sol.ys[:, 1], label="Predator")
plt.legend()
plt.show()
print(sol.ys[:, 0].size)
print(sol.ts.shape)
"
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the supplied example with diffeqsolve, SaveAt, and DiscreteTerminatingEvent, then trace how saved time steps are represented after the event triggers. Compare the returned sol.ts and sol.ys with the requested pre-event-only behavior; done means no post-event inf values are returned while the terminating event still stops integration.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100