Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to handle conditional input to the func #258

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions torchdiffeq/_impl/odeint.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
}


def odeint(func, y0, t, *, rtol=1e-7, atol=1e-9, method=None, options=None, event_fn=None):
def odeint(func, y0, t, *, rtol=1e-7, atol=1e-9, method=None, options=None, event_fn=None, condition=None):
"""Integrate a system of ordinary differential equations.

Solves the initial value problem for a non-stiff system of first order ODEs:
Expand Down Expand Up @@ -77,7 +77,7 @@ def odeint(func, y0, t, *, rtol=1e-7, atol=1e-9, method=None, options=None, even
solver = SOLVERS[method](func=func, y0=y0, rtol=rtol, atol=atol, **options)

if event_fn is None:
solution = solver.integrate(t)
solution = solver.integrate(t, condition)
else:
event_t, solution = solver.integrate_until_event(t[0], event_fn)
event_t = event_t.to(t)
Expand Down
7 changes: 6 additions & 1 deletion torchdiffeq/_impl/solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def _grid_constructor(func, y0, t):
def _step_func(self, func, t0, dt, t1, y0):
pass

def integrate(self, t):
def integrate(self, t, condition=None):
time_grid = self.grid_constructor(self.func, self.y0, t)
assert time_grid[0] == t[0] and time_grid[-1] == t[-1]

Expand All @@ -108,11 +108,16 @@ def integrate(self, t):

j = 1
y0 = self.y0
if condition is not None:
y0_condition = y0.clone()
for t0, t1 in zip(time_grid[:-1], time_grid[1:]):
dt = t1 - t0
self.func.callback_step(t0, y0, dt)
dy, f0 = self._step_func(self.func, t0, dt, t1, y0)
y1 = y0 + dy
if condition is not None:
#replace all channels of y1 except ch 0 with y0_condition
y1[:,condition:,:,:] = y0_condition[:,condition:,:,:]

while j < len(t) and t1 >= t[j]:
if self.interp == "linear":
Expand Down