Class RungeKutta

Object
Model
ODE
RungeKutta
All Implemented Interfaces:
Discrete, CLOProvider
Direct Known Subclasses:
ATBT.ODE

public class RungeKutta extends ODE
Implementation of Runge-Kutta method with adaptive step size for the numerical integration of systems of differential equations. Adapted from numerical recipes in C.
Author:
Christoph Hauert
  • Field Details

    • autoDt

      boolean autoDt
      true if the adaptive step sizes should be used. If false the traditional Euler's method with fixed step size is used.
      See Also:
    • doEuler

      boolean doEuler
      true to resort to the traditional Euler's method with fixed step size. This happens if autoDt==false or in multi-species modules with mixed frequency and density dynamics.
      See Also:
    • isReplicator

      boolean isReplicator
      true for frequency dynamics. This ensures proper normalization of the states. In mutli-species modules this requires that all modules are frequency based.
      See Also:
    • SAFETY

      private static final double SAFETY
      The safety margin for adjusting time steps. Magic number from Numerical Recipes in C.
      See Also:
    • PGROW

      private static final double PGROW
      The exponent for increasing the time step if error outside margin. Magic number from Numerical Recipes in C.
      See Also:
    • PSHRNK

      private static final double PSHRNK
      The exponent for decreasing the time step if error outside margin. Magic number from Numerical Recipes in C.
      See Also:
    • ERRCON

      private static final double ERRCON
      The maximum error in a single step. The value ERRCON equals (5/SAFETY)^(1/PGROW). Magic number from Numerical Recipes in C.
      See Also:
    • ACCURACY

      private static final double ACCURACY
      The accuracy required for the integration. Magic number from Numerical Recipes in C.
      See Also:
    • yerr

      private double[] yerr
      Helper variables and temporary storage for errors, states and fitness when calculating derivatives for different steps.
    • ytmp

      private double[] ytmp
      Helper variables and temporary storage for errors, states and fitness when calculating derivatives for different steps.
    • ftmp

      private double[] ftmp
      Helper variables and temporary storage for errors, states and fitness when calculating derivatives for different steps.
    • a2

      private static final double a2
      More magic numbers from Numerical Recipes in C.
      See Also:
    • a3

      private static final double a3
      More magic numbers from Numerical Recipes in C.
      See Also:
    • a4

      private static final double a4
      More magic numbers from Numerical Recipes in C.
      See Also:
    • a5

      private static final double a5
      More magic numbers from Numerical Recipes in C.
      See Also:
    • a6

      private static final double a6
      More magic numbers from Numerical Recipes in C.
      See Also:
    • b21

      private static final double b21
      See Also:
    • b31

      private static final double b31
      See Also:
    • b32

      private static final double b32
      See Also:
    • b41

      private static final double b41
      See Also:
    • b42

      private static final double b42
      See Also:
    • b43

      private static final double b43
      See Also:
    • b51

      private static final double b51
      See Also:
    • b52

      private static final double b52
      See Also:
    • b53

      private static final double b53
      See Also:
    • b54

      private static final double b54
      See Also:
    • b61

      private static final double b61
      See Also:
    • b62

      private static final double b62
      See Also:
    • b63

      private static final double b63
      See Also:
    • b64

      private static final double b64
      See Also:
    • b65

      private static final double b65
      See Also:
    • c1

      private static final double c1
      See Also:
    • c3

      private static final double c3
      See Also:
    • c4

      private static final double c4
      See Also:
    • c6

      private static final double c6
      See Also:
    • dc5

      private static final double dc5
      See Also:
    • dc1

      private static final double dc1
      See Also:
    • dc3

      private static final double dc3
      See Also:
    • dc4

      private static final double dc4
      See Also:
    • dc6

      private static final double dc6
      See Also:
    • ak2

      private double[] ak2
      Temporary variables for intermediate results required to implement the fifth-order Cash-Karp Runge-Kutta method.
    • ak3

      private double[] ak3
      Temporary variables for intermediate results required to implement the fifth-order Cash-Karp Runge-Kutta method.
    • ak4

      private double[] ak4
      Temporary variables for intermediate results required to implement the fifth-order Cash-Karp Runge-Kutta method.
    • ak5

      private double[] ak5
      Temporary variables for intermediate results required to implement the fifth-order Cash-Karp Runge-Kutta method.
    • ak6

      private double[] ak6
      Temporary variables for intermediate results required to implement the fifth-order Cash-Karp Runge-Kutta method.
  • Constructor Details

    • RungeKutta

      public RungeKutta(EvoLudo engine)
      Constructs a new model for the numerical integration of the system of ordinary differential equations representing the dynamics specified by the Module module using the EvoLudo pacemaker engine to control the numerical evaluations. The integrator implements the fifth order Runge-Kutta method with adaptive step size.
      Parameters:
      engine - the pacemaker for running the model
  • Method Details

    • unload

      public void unload()
      Description copied from class: Model
      Milestone: Unload this model and free resources (if applicable).
      Overrides:
      unload in class ODE
      See Also:
    • reset

      public void reset()
      Description copied from class: Model
      Milestone: Reset this model
      Overrides:
      reset in class ODE
      See Also:
    • setAutoDt

      public void setAutoDt(boolean autoDt)
      Sets whether adaptive step sizes should be used. This is required for this implementation of the fifth order Cash-Karp Runge-Kutta method. If set to false the integrator defaults to the Euler method with fixed increments.
      Parameters:
      autoDt - the flag to indicate whether adaptive step sizes should be used.
      See Also:
    • getAutoDt

      public boolean getAutoDt()
      Gets whether adaptive step sizes are used. If not the integrator defaults to the Euler method with fixed increments.
      Returns:
      true if using adaptive step sizes
    • check

      public boolean check()
      Description copied from class: Model
      Check consistency of parameters and adjust if necessary (and possible). All issues and modifications should be reported through logger. Some parameters can be adjusted while the model remains active or even while running, whereas others require a reset. An example of the former category is in general simple adjustments of payoffs, while an example of the latter category is a change of the population structure.
      Overrides:
      check in class ODE
      Returns:
      true if reset required
      See Also:
    • deStep

      protected double deStep(double step)
      Attempts a numerical integration step of size step. The baseline are steps of fixed size following Euler's method.

      Implementation Notes:

      Fifth-order Runge-Kutta step with monitoring of local truncation error to ensure accuracy and adjust step size. Input are the dependent variable vector y[1..n] and its derivative dydx[1..n] at the starting value of the independent variable x. Also input are the step size to be attempted htry, the required accuracy eps, and the vector yscal[1..n] against which the error is scaled. On output, y and x are replaced by their new values, hdid is the step size that was actually accomplished, and hnext is the estimated next step size. derivs is the user-supplied routine that computes the right-hand side derivatives.

      If you desire constant fractional errors, plug a pointer to y into the pointer to yscal calling slot (no need to copy the values into a different array). If you desire constant absolute errors relative to some maximum values, set the elements of yscal equal to those maximum values. A useful “trick” for getting constant fractional errors except “very” near zero crossings is to set yscal[i] equal to |y[i]| + |h × dydx[i]|. (The routine odeint, below, does this.)

      Copied from Numerical Recipes in C, chapter 16.2, p.718f

      Overrides:
      deStep in class ODE
      Parameters:
      step - the time step to attempt
      Returns:
      squared distance between this state and previous one, (yt-yout)2.
    • rkck

      private boolean rkck(double h)
      Given values for n variables y[1..n] and their derivatives dydx[1..n] known at x, use the fifth-order Cash-Karp Runge-Kutta method to advance the solution over an interval h and return the incremented variables as yout[1..n]. Also return an estimate of the local truncation error in yout using the embedded fourth-order method. The user supplies the routine derivs(x,y,dydx), which returns derivatives dydx at x.

      Copied from Numerical Recipes in C, chapter 16.2, p.719

      Implementation Notes:

      For replicator dynamics all intermediate steps must remain normalized, i.e. sum ytmp = 1, otherwise reject. Currently this requires that in multi-species modules either all or none are frequency based.
      Parameters:
      h - the step to try to take
      Returns:
      true if step successful
      See Also: