Class RungeKutta
- All Implemented Interfaces:
DModel, CLOProvider
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 Model
Model.HasDE, Model.HasIBS -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final doubleCash-Karp coefficient for the second stage time.private static final doubleCash-Karp coefficient for the third stage time.private static final doubleCash-Karp coefficient for the fourth stage time.private static final doubleCash-Karp coefficient for the fifth stage time.private static final doubleCash-Karp coefficient for the sixth stage time.private static final doubleThe 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[]Third stage derivative vector.private double[]Fourth stage derivative vector.private double[]Fifth stage derivative vector.private double[]Sixth stage derivative vector.(package private) booleantrueif the adaptive step sizes should be used.private static final doubleCash-Karp coefficient for the second stage derivative.private static final doubleCash-Karp coefficient for the third stage derivative.private static final doubleCash-Karp coefficient for the third stage derivative.private static final doubleCash-Karp coefficient for the fourth stage derivative.private static final doubleCash-Karp coefficient for the fourth stage derivative.private static final doubleCash-Karp coefficient for the fourth stage derivative.private static final doubleCash-Karp coefficient for the fifth stage derivative.private static final doubleCash-Karp coefficient for the fifth stage derivative.private static final doubleCash-Karp coefficient for the fifth stage derivative.private static final doubleCash-Karp coefficient for the fifth stage derivative.private static final doubleCash-Karp coefficient for the sixth stage derivative.private static final doubleCash-Karp coefficient for the sixth stage derivative.private static final doubleCash-Karp coefficient for the sixth stage derivative.private static final doubleCash-Karp coefficient for the sixth stage derivative.private static final doubleCash-Karp coefficient for the sixth stage derivative.private static final doubleCash-Karp fifth-order output coefficient.private static final doubleCash-Karp fifth-order output coefficient.private static final doubleCash-Karp fifth-order output coefficient.private static final doubleCash-Karp fifth-order output coefficient.private static final doubleCash-Karp error estimate coefficient.private static final doubleCash-Karp error estimate coefficient.private static final doubleCash-Karp error estimate coefficient.private static final doubleCash-Karp error estimate coefficient.private static final doubleCash-Karp error estimate coefficient.(package private) booleantrueto resort to the traditional Euler's method with fixed step size.private static final doubleThe maximum error in a single step.private double[]Temporary derivative vector.private static final doubleThe exponent for increasing the time step if error outside margin.private static final doubleThe exponent for decreasing the time step if error outside margin.private static final doubleThe 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[]Temporary state vector.Fields inherited from class ODE
accuracy, cloAdjustedDynamics, cloDEAccuracy, cloDEdt, cloInit, cloTimeReversed, dependents, dstate, dt, dtTaken, dtTry, dyt, effRate, forward, ft, idxSpecies, initType, invFitRange, isAdjustedDynamics, isDensity, monoStop, mutation, names, nDim, staticfit, y0, yout, ytFields inherited from class Model
cloSamples, cloTimeRelax, cloTimeStep, cloTimeStop, connect, converged, engine, fixData, isMultispecies, isRelaxing, logger, markers, mode, nSamples, nSpecies, nStatisticsFailed, nStatisticsSamples, rng, species, statisticsSampleNew, time, timeRelax, timeStep, timeStop, type -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancheck()Check consistency of parameters and adjust if necessary (and possible).protected doubledeStep(double step) Attempts a numerical integration step of sizestepwith step size adjustment.booleanGets whether adaptive step sizes are used.voidreset()Milestone: Reset this modelprivate booleanrkck(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].voidsetAutoDt(boolean autoDt) Sets whether adaptive step sizes should be used.voidunload()Milestone: Unload this model and free resources (if applicable).Methods inherited from class ODE
checkConvergence, collectCLO, computeEffRates, encodeFitness, encodeState, encodeTraits, getAccuracy, getDerivatives, getDt, getFitnessData, getFitnessHistogramData, getInitialTraits, getInitialTraits, getMaxFitness, getMaxScore, getMeanFitness, getMeanFitness, getMeanTraits, getMeanTraits, getMinFitness, getMinScore, getMonoScore, getNMean, getNMean, getOccupiedDensity, getStatus, getStatus, getTimeStep, getTraitData, init, isDensity, isMonomorphic, isTimeReversed, load, next, normalizeState, parse, permitsTimeReversal, restoreFitness, restoreState, restoreTraits, setAccuracy, setAdjustedDynamics, setDt, setInitialTraits, setInitialTraits, setTimeReversed, update, updateBest, updateBestResponse, updateEcology, updateImitate, updateImitateBetter, updateProportional, updateThermalMethods inherited from class Model
debugStep, finishRelaxation, getCounter, getFitnessNameAt, getFixationData, getLogger, getMarkers, getMeanFitnessAt, getMeanName, getMeanNames, getMeanTraitAt, getMode, getNextHalt, getNSamples, getNSpecies, getNStatisticsFailed, getNStatisticsSamples, getScoreNameAt, getSpecies, getTime, getTimeRelax, getTimeStop, getTraitNameAt, getType, getUpdates, hasConverged, initStatisticsFailed, initStatisticsSample, isConnected, isContinuous, isDE, isIBS, isODE, isPDE, isRelaxing, isSDE, permitsDebugStep, permitsMode, permitsSampleStatistics, permitsUpdateStatistics, readStatisticsSample, relax, requestMode, resetState, resetStatisticsSample, setMode, setNSamples, setTimeRelax, setTimeStep, setTimeStop, startRelaxationMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface CLOProvider
adjustCLO
-
Field Details
-
autoDt
boolean autoDttrueif the adaptive step sizes should be used. Iffalsethe traditional Euler's method with fixed step size is used.- See Also:
-
doEuler
boolean doEulertrueto resort to the traditional Euler's method with fixed step size. This happens ifautoDt==falseor in multi-species modules with mixed frequency and density dynamics.- 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 valueERRCONequals(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[] ytmpTemporary state vector. -
ftmp
private double[] ftmpTemporary derivative vector. -
A2
private static final double A2Cash-Karp coefficient for the second stage time.- See Also:
-
A3
private static final double A3Cash-Karp coefficient for the third stage time.- See Also:
-
A4
private static final double A4Cash-Karp coefficient for the fourth stage time.- See Also:
-
A5
private static final double A5Cash-Karp coefficient for the fifth stage time.- See Also:
-
A6
private static final double A6Cash-Karp coefficient for the sixth stage time.- See Also:
-
B21
private static final double B21Cash-Karp coefficient for the second stage derivative.- See Also:
-
B31
private static final double B31Cash-Karp coefficient for the third stage derivative.- See Also:
-
B32
private static final double B32Cash-Karp coefficient for the third stage derivative.- See Also:
-
B41
private static final double B41Cash-Karp coefficient for the fourth stage derivative.- See Also:
-
B42
private static final double B42Cash-Karp coefficient for the fourth stage derivative.- See Also:
-
B43
private static final double B43Cash-Karp coefficient for the fourth stage derivative.- See Also:
-
B51
private static final double B51Cash-Karp coefficient for the fifth stage derivative.- See Also:
-
B52
private static final double B52Cash-Karp coefficient for the fifth stage derivative.- See Also:
-
B53
private static final double B53Cash-Karp coefficient for the fifth stage derivative.- See Also:
-
B54
private static final double B54Cash-Karp coefficient for the fifth stage derivative.- See Also:
-
B61
private static final double B61Cash-Karp coefficient for the sixth stage derivative.- See Also:
-
B62
private static final double B62Cash-Karp coefficient for the sixth stage derivative.- See Also:
-
B63
private static final double B63Cash-Karp coefficient for the sixth stage derivative.- See Also:
-
B64
private static final double B64Cash-Karp coefficient for the sixth stage derivative.- See Also:
-
B65
private static final double B65Cash-Karp coefficient for the sixth stage derivative.- See Also:
-
C1
private static final double C1Cash-Karp fifth-order output coefficient.- See Also:
-
C3
private static final double C3Cash-Karp fifth-order output coefficient.- See Also:
-
C4
private static final double C4Cash-Karp fifth-order output coefficient.- See Also:
-
C6
private static final double C6Cash-Karp fifth-order output coefficient.- See Also:
-
DC5
private static final double DC5Cash-Karp error estimate coefficient.- See Also:
-
DC1
private static final double DC1Cash-Karp error estimate coefficient.- See Also:
-
DC3
private static final double DC3Cash-Karp error estimate coefficient.- See Also:
-
DC4
private static final double DC4Cash-Karp error estimate coefficient.- See Also:
-
DC6
private static final double DC6Cash-Karp error estimate coefficient.- See Also:
-
ak2
private double[] ak2Temporary variables for intermediate results required to implement the fifth-order Cash-Karp Runge-Kutta method. -
ak3
private double[] ak3Third stage derivative vector. -
ak4
private double[] ak4Fourth stage derivative vector. -
ak5
private double[] ak5Fifth stage derivative vector. -
ak6
private double[] ak6Sixth stage derivative vector.
-
-
Constructor Details
-
RungeKutta
Constructs a new model for the numerical integration of the system of ordinary differential equations representing the dynamics specified by theModulemoduleusing theEvoLudopacemakerengineto 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
-
reset
-
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 tofalsethe 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:
trueif using adaptive step sizes
-
check
public boolean check()Description copied from class:ModelCheck 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 sizestepwith step size adjustment. Starts with an Euler step but if the result yields:- negative densities/frequencies
- frequencies exceeding 1.0
0.0or1.0, respectively, and the effective step size is adjusted and recorded inODE.dtTaken.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:
trueif step successful
-