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

Proposal: Adding additional explicit solvers #222

Open
wants to merge 1 commit 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
56 changes: 55 additions & 1 deletion torchdiffeq/_impl/fixed_grid.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .solvers import FixedGridODESolver
from .rk_common import rk4_alt_step_func
from .rk_common import rk4_step_func, rk4_alt_step_func
from .misc import Perturb


Expand All @@ -11,6 +11,16 @@ def _step_func(self, func, t0, dt, t1, y0):
return dt * f0, f0


class Heun(FixedGridODESolver):
order = 2

def _step_func(self, func, t0, dt, t1, y0):
half_dt = 0.5 * dt
f0 = func(t0, y0, perturb=Perturb.NEXT if self.perturb else Perturb.NONE)
f1 = func(t0 + dt, y0 + dt * f0)
return half_dt * (f0 + f1), f0


class Midpoint(FixedGridODESolver):
order = 2

Expand All @@ -21,9 +31,53 @@ def _step_func(self, func, t0, dt, t1, y0):
return dt * func(t0 + half_dt, y_mid), f0


class Ralston(FixedGridODESolver):
order = 2

def _step_func(self, func, t0, dt, t1, y0):
fourth_dt = 0.25 * dt
double_dt = 2 * dt
f0 = func(t0, y0, perturb=Perturb.NEXT if self.perturb else Perturb.NONE)
f1 = func(t0 + double_dt / 3, y0 + double_dt * f0 / 3)
return fourth_dt * (f0 + 3 * f1), f0


class RK3(FixedGridODESolver):
order = 3

def _step_func(self, func, t0, dt, t1, y0):
half_dt = 0.5 * dt
double_dt = 2 * dt
sixth_dt = (1 / 6) * dt
f0 = func(t0, y0, perturb=Perturb.NEXT if self.perturb else Perturb.NONE)
f1 = func(t0 + half_dt, y0 + half_dt * f0)
f2 = func(t0 + dt, y0 - dt * f0 + double_dt * f1)
return sixth_dt * (f0 + 4 * f1 + f2), f0


class SSPRK3(FixedGridODESolver):
order = 3

def _step_func(self, func, t0, dt, t1, y0):
fourth_dt = 0.25 * dt
sixth_dt = (1 / 6) * dt
f0 = func(t0, y0, perturb=Perturb.NEXT if self.perturb else Perturb.NONE)
f1 = func(t0 + dt, y0 + dt * f0)
f2 = func(t0 + fourth_dt, y0 + fourth_dt * (f0 + f1))
return sixth_dt * (f0 + f1 + 4 * f2), f0


class RK4(FixedGridODESolver):
order = 4

def _step_func(self, func, t0, dt, t1, y0):
f0 = func(t0, y0, perturb=Perturb.NEXT if self.perturb else Perturb.NONE)
return rk4_alt_step_func(func, t0, dt, t1, y0, f0=f0, perturb=self.perturb), f0


class ClassicRK4(FixedGridODESolver):
order = 4

def _step_func(self, func, t0, dt, t1, y0):
f0 = func(t0, y0, perturb=Perturb.NEXT if self.perturb else Perturb.NONE)
return rk4_step_func(func, t0, dt, t1, y0, f0=f0, perturb=self.perturb), f0
7 changes: 6 additions & 1 deletion torchdiffeq/_impl/odeint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .bosh3 import Bosh3Solver
from .adaptive_heun import AdaptiveHeunSolver
from .fehlberg2 import Fehlberg2
from .fixed_grid import Euler, Midpoint, RK4
from .fixed_grid import Euler, Heun, Midpoint, Ralston, RK3, SSPRK3, ClassicRK4, RK4
from .fixed_adams import AdamsBashforth, AdamsBashforthMoulton
from .dopri8 import Dopri8Solver
from .scipy_wrapper import ScipyWrapperODESolver
Expand All @@ -17,7 +17,12 @@
'fehlberg2': Fehlberg2,
'adaptive_heun': AdaptiveHeunSolver,
'euler': Euler,
'heun': Heun,
'midpoint': Midpoint,
'ralston': Ralston,
'rk3': RK3,
'ssprk3': SSPRK3,
'crk4': ClassicRK4,
'rk4': RK4,
'explicit_adams': AdamsBashforth,
'implicit_adams': AdamsBashforthMoulton,
Expand Down