src/numericalnim/ode

Source   Edit  

ODEs

This module implements routines for solving ODEs numerically.

The recommended methods to use are: tsit54, dopri54, vern65. All of these are adaptive high-order methods that are both accurate and fast. Settings like tolerances and step size can be provided in a options object, see newODEoptions.

Example:

import src/numericalnim/ode
import numericalnim
# dy = -y
proc dy(t: float, y: float, ctx: NumContext[float, float]): float =
    -y

let y0 = 1.0
let tspan = linspace(0.0, 10.0, 100)
let (tSol, ySol) = solveODE(dy, y0, tspan, integrator="tsit54")

Types

IntegratorProc[T] = proc (f: ODEProc[T]; t: float; y, FSAL: T; dt: float;
                          options: ODEoptions; ctx: NumContext[T, float]): (T,
    T, float, float)
Source   Edit  
ODEoptions = object
  dt*: float
  dtMax*: float
  dtMin*: float
  tStart*: float
  absTol*: float
  relTol*: float
  scaleMax*: float
  scaleMin*: float
Source   Edit  
ODEProc[T] = proc (t: float; y: T; ctx: NumContext[T, float]): T
Source   Edit  

Consts

adaptiveODE = ["rk21", "bs32", "dopri54", "tsit54", "vern65"]
Source   Edit  
allODE = ["heun2", "ralston2", "kutta3", "heun3", "ralston3", "ssprk3",
          "ralston4", "kutta4", "rk4", "rk21", "bs32", "dopri54", "tsit54",
          "vern65"]
Source   Edit  
fixedODE = ["heun2", "ralston2", "kutta3", "heun3", "ralston3", "ssprk3",
            "ralston4", "kutta4", "rk4"]
Source   Edit  

Procs

proc newODEoptions(dt: float = 0.0001; absTol: float = 0.0001;
                   relTol: float = 0.0001; dtMax: float = 0.01;
                   dtMin: float = 0.0001; scaleMax: float = 4.0;
                   scaleMin: float = 0.1; tStart: float = 0.0): ODEoptions {.
    ...raises: [ValueError], tags: [], forbids: [].}

Create a new ODEoptions object.

Input:

  • dt: The time step to use in fixed timestep integrators.
  • absTol: The absolute error tolerance to use in adaptive timestep integrators.
  • relTol: The relative error tolerance to use in adaptive timestep integrators.
  • dtMax: The maximum timestep allowed in adaptive timestep integrators.
  • dtMin: The minimum timestep allowed in adaptive timestep integrators.
  • scaleMax: The maximum increase factor for the time step dt between two steps.
  • ScaleMin: The minimum factor for the time step dt between two steps.
  • tStart: The time to start the ODE-solver at. The time the initial conditions are supplied at.

Returns:

  • ODEoptions object with the supplied parameters.
Source   Edit  
proc solveODE[T](f: ODEProc[T]; y0: T; tspan: openArray[float];
                 options: ODEoptions = DEFAULT_ODEoptions;
                 ctx: NumContext[T, float] = nil; integrator = "dopri54"): (
    seq[float], seq[T])

Solve an ODE initial value problem.

Input:

  • f: the ODE function y' = f(t, y).
  • y0: Initial value.
  • tspan: Seq of t values that y will be returned at.
  • options: ODEoptions object with ODE parameters.
  • ctx: A context variable that can be accessed and modified in f. It is a ref type so IT IS MUTABLE. It can be used to save extra information during the solving for example, or to pass in big Tensors.
  • integrator: String with the integrator to use. Choices: "dopri54", "tsit54", "vern65", "rk4", "rk21", "bs32", "heun2", "ralston2", "kutta3", "heun3", "ralston3", "ssprk3", "ralston4", "kutta4"

Returns:

  • A tuple containing a seq of t-values and a seq of y-values (t, y).
Source   Edit