Package org.evoludo.simulator.models
Class RungeKutta
- All Implemented Interfaces:
Discrete
,CLOProvider
- Direct Known Subclasses:
ATBT.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
-
Nested Class Summary
Nested classes/interfaces inherited from class ODE
ODE.HasDE, ODE.HasODE, ODE.InitType
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final double
More magic numbers from Numerical Recipes in C.private static final double
More magic numbers from Numerical Recipes in C.private static final double
More magic numbers from Numerical Recipes in C.private static final double
More magic numbers from Numerical Recipes in C.private static final double
More magic numbers from Numerical Recipes in C.private static final double
The accuracy required for the integration.private double[]
Temporary variables for intermediate results required to implement the fifth-order Cash-Karp Runge-Kutta method.private double[]
Temporary variables for intermediate results required to implement the fifth-order Cash-Karp Runge-Kutta method.private double[]
Temporary variables for intermediate results required to implement the fifth-order Cash-Karp Runge-Kutta method.private double[]
Temporary variables for intermediate results required to implement the fifth-order Cash-Karp Runge-Kutta method.private double[]
Temporary variables for intermediate results required to implement the fifth-order Cash-Karp Runge-Kutta method.(package private) boolean
true
if the adaptive step sizes should be used.private static final double
private static final double
private static final double
private static final double
private static final double
private static final double
private static final double
private static final double
private static final double
private static final double
private static final double
private static final double
private static final double
private static final double
private static final double
private static final double
private static final double
private static final double
private static final double
private static final double
private static final double
private static final double
private static final double
private static final double
(package private) boolean
true
to resort to the traditional Euler's method with fixed step size.private static final double
The maximum error in a single step.private double[]
Helper variables and temporary storage for errors, states and fitness when calculating derivatives for different steps.(package private) boolean
true
for frequency dynamics.private static final double
The exponent for increasing the time step if error outside margin.private static final double
The exponent for decreasing the time step if error outside margin.private static final double
The safety margin for adjusting time steps.private double[]
Helper variables and temporary storage for errors, states and fitness when calculating derivatives for different steps.private double[]
Helper variables and temporary storage for errors, states and fitness when calculating derivatives for different steps.Fields inherited from class ODE
accuracy, cloAdjustedDynamics, cloDEAccuracy, cloDEdt, cloInit, cloTimeReversed, dependents, dstate, dt, dtTaken, dtTry, dyt, forward, ft, idxSpecies, initType, invFitRange, isAdjustedDynamics, monoStop, mutation, names, nDim, rates, staticfit, y0, yout, yt
Fields inherited from class Model
cloSamples, cloTimeRelax, cloTimeStep, cloTimeStop, connect, converged, engine, fixData, isMultispecies, isRelaxing, logger, mode, nSamples, nSpecies, nStatisticsFailed, nStatisticsSamples, rng, species, statisticsSampleNew, time, timeRelax, timeStep, timeStop, type
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
check()
Check consistency of parameters and adjust if necessary (and possible).protected double
deStep
(double step) Attempts a numerical integration step of sizestep
.boolean
Gets whether adaptive step sizes are used.void
reset()
Milestone: Reset this modelprivate 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].void
setAutoDt
(boolean autoDt) Sets whether adaptive step sizes should be used.void
unload()
Milestone: Unload this model and free resources (if applicable).Methods inherited from class ODE
checkConvergence, collectCLO, encodeFitness, encodeState, encodeStrategies, getAccuracy, getDerivatives, getDt, getFitnessData, getFitnessHistogramData, getInitialTraits, getInitialTraits, getMaxFitness, getMaxScore, getMeanFitness, getMeanFitness, getMeanTraits, getMeanTraits, getMinFitness, getMinScore, getMonoScore, getNMean, getNMean, getStatus, getTraitData, init, isDensity, isMonomorphic, isTimeReversed, load, next, normalizeState, parse, permitsTimeReversal, restoreFitness, restoreState, restoreStrategies, setAccuracy, setAdjustedDynamics, setDt, setInitialTraits, setInitialTraits, setTimeReversed, update, updateBest, updateBestResponse, updateEcology, updateImitate, updateImitateBetter, updateProportional, updateThermal
Methods inherited from class Model
debugStep, getCounter, getFitnessNameAt, getFixationData, getLogger, getMeanFitnessAt, getMeanName, getMeanNames, getMeanTraitAt, getMode, getNextHalt, getNSamples, getNSpecies, getNStatisticsFailed, getNStatisticsSamples, getScoreNameAt, getSpecies, getTime, getTimeRelax, getTimeStep, getTimeStop, getTraitNameAt, getType, hasConverged, initStatisticsFailed, initStatisticsSample, isConnected, isContinuous, isDE, isIBS, isODE, isPDE, isRelaxing, isSDE, isType, permitsDebugStep, permitsMode, permitsSampleStatistics, permitsUpdateStatistics, readStatisticsSample, relax, requestMode, resetStatisticsSample, setMode, setNSamples, setTimeRelax, setTimeStep, setTimeStop, useScheduling
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface CLOProvider
adjustCLO
-
Field Details
-
autoDt
boolean autoDttrue
if the adaptive step sizes should be used. Iffalse
the traditional Euler's method with fixed step size is used.- See Also:
-
doEuler
boolean doEulertrue
to resort to the traditional Euler's method with fixed step size. This happens ifautoDt==false
or in multi-species modules with mixed frequency and density dynamics.- See Also:
-
isReplicator
boolean isReplicatortrue
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 SAFETYThe safety margin for adjusting time steps. Magic number from Numerical Recipes in C.- See Also:
-
PGROW
private static final double PGROWThe exponent for increasing the time step if error outside margin. Magic number from Numerical Recipes in C.- See Also:
-
PSHRNK
private static final double PSHRNKThe exponent for decreasing the time step if error outside margin. Magic number from Numerical Recipes in C.- See Also:
-
ERRCON
private static final double ERRCONThe maximum error in a single step. The valueERRCON
equals(5/SAFETY)^(1/PGROW)
. Magic number from Numerical Recipes in C.- See Also:
-
ACCURACY
private static final double ACCURACYThe accuracy required for the integration. Magic number from Numerical Recipes in C.- See Also:
-
yerr
private double[] yerrHelper variables and temporary storage for errors, states and fitness when calculating derivatives for different steps. -
ytmp
private double[] ytmpHelper variables and temporary storage for errors, states and fitness when calculating derivatives for different steps. -
ftmp
private double[] ftmpHelper variables and temporary storage for errors, states and fitness when calculating derivatives for different steps. -
a2
private static final double a2More magic numbers from Numerical Recipes in C.- See Also:
-
a3
private static final double a3More magic numbers from Numerical Recipes in C.- See Also:
-
a4
private static final double a4More magic numbers from Numerical Recipes in C.- See Also:
-
a5
private static final double a5More magic numbers from Numerical Recipes in C.- See Also:
-
a6
private static final double a6More 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[] ak2Temporary variables for intermediate results required to implement the fifth-order Cash-Karp Runge-Kutta method. -
ak3
private double[] ak3Temporary variables for intermediate results required to implement the fifth-order Cash-Karp Runge-Kutta method. -
ak4
private double[] ak4Temporary variables for intermediate results required to implement the fifth-order Cash-Karp Runge-Kutta method. -
ak5
private double[] ak5Temporary variables for intermediate results required to implement the fifth-order Cash-Karp Runge-Kutta method. -
ak6
private double[] ak6Temporary variables for intermediate results required to implement the fifth-order Cash-Karp Runge-Kutta method.
-
-
Constructor Details
-
RungeKutta
Constructs a new model for the numerical integration of the system of ordinary differential equations representing the dynamics specified by theModule
module
using theEvoLudo
pacemakerengine
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). -
reset
public void reset()Description copied from class:Model
Milestone: Reset this model -
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 tofalse
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 throughlogger
. 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. -
deStep
protected double deStep(double step) Attempts a numerical integration step of sizestep
. 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
-
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:
-