#ifndef STOCHASTIC_RUNGE_KUTTA #define STOCHASTIC_RUNGE_KUTTA #include #include #include "InternalDefs.h" #include "Auxiliary.cpp" using namespace std; //Two step, explicit, stochastic Runge-Kutta integration with fixed time step, with mean-square order of accuracy 3/2. //See [Milstein 1995, Numerical Integration of Stochastic Differential Equations, Eq. (3.55) in Theorem 3.3] //Returns false on error //VECTORFIELD_FUNCTOR should implement: // bool operator()(double time, const POINT &p, POINT &velocity, POINT &finalPoint) const, // double D(unsigned int) const, //where D(k) returns the time-independent diffusion coefficient (white noise amplitude = SQRT(2D)) for component k. //finalPoint is a pure return value. If the functor returns false, finalPoint will hold a suggested "last valid point" at which the trajectory is suspected to have left the domain of the vector field. // //POINT should implement: // POINT operator+(POINT, POINT), // POINT operator-(POINT, POINT), // POINT operator*(double,POINT), //satisfying the axioms of a real vector field. Additionally, POINT should implement, // double& operator[](unsigned int k) template bool StochasticRungeKutta( const POINT &startPoint, double maxT, //max integration time double dt, //fixed time step const VECTORFIELD_FUNCTOR &f, unsigned int dimension, //finite system dimension, i.e. number of components, of POINT bool onlySaveFinalPoint, unsigned long maxRecordedPoints, //if small, some intermediate trajectory points are skipped (i.e. not recorded). This might be useful if accurate integration requires a small time step, but would produce to large of a time series. vector &realizedTimes, //if (onlySaveFinalPoint==false), realized time-points, corresponding to realizedPath[]. If (onlySaveFinalPoint==true), will only contain last time. vector &realizedPath, //if (onlySaveFinalPoint==false), traversed points after integration, corresponding to realizedTimes[]. If (onlySaveFinalPoint==true), will only contain last path point. bool &reachedDomainBoundary // (output) returns true if the domain of f() was exited during integration. If that's the case, realizedTimes[] and realizedPath[] will contain that last point. ){ //preliminary error checking if(dt bool StochasticRungeKutta_VN( const POINT &startPoint, double maxT, //max integration time double dt, //fixed time step const VECTORFIELD_FUNCTOR &f, unsigned int dimension, //finite system dimension, i.e. number of components, of POINT bool onlySaveFinalPoint, unsigned long maxRecordedPoints, //if small, some intermediate trajectory points are skipped (i.e. not recorded). This might be useful if accurate integration requires a small time step, but would produce to large of a time series. vector &realizedTimes, //if (onlySaveFinalPoint==false), realized time-points, corresponding to realizedPath[]. If (onlySaveFinalPoint==true), will only contain last time. vector &realizedPath, //if (onlySaveFinalPoint==false), traversed points after integration, corresponding to realizedTimes[]. If (onlySaveFinalPoint==true), will only contain last path point. bool &reachedDomainBoundary // (output) returns true if the domain of f() was exited during integration. If that's the case, realizedTimes[] and realizedPath[] will contain that last point. ){ //preliminary error checking if(dt