libpspm
rkck45.h
Go to the documentation of this file.
1 #ifndef PSPM_ODE_RKCK45_H
2 #define PSPM_ODE_RKCK45_H_
3 
4 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5  *
6  * Code for RK Solvers
7  * Original version obtained from:
8  * https://www.physics.rutgers.edu/grad/509/src_numerics/ODE/1/ode1.cc
9  *
10  * v2
11  * Modified sligtly by Jaideep Joshi (30/5/2020)
12  * - Cleaned up indentation
13  * - Better class names
14  * - remove requirement of stop time
15  * - Add "step_to" function
16  * - move all containers to main class for ease of resizing
17  *
18  * v3
19  * - uses GSL style step change algorithm
20  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
21 
22 #include <iostream>
23 #include <iomanip>
24 #include <cmath>
25 #include <vector>
26 #include <cassert>
27 
28 
29 
30 //using namespace std;
31 
32 typedef std::vector<double> container;
33 
34 //template <class functor, class container>
35 //void Euler(double x, double h, container& y, functor& derivs){
36  //container fk(y.size());
37  //derivs(x, y, fk);
38  //for (int i=0; i<y.size(); i++) y[i] += h*fk[i];
39 //}
40 
47 class RKCK45{
48  private:
49  static constexpr double SAFETY = 0.9;
50  static constexpr double PGROW = -0.2;
51  static constexpr double PSHRNK = -0.25;
52  static constexpr double ERRCON = 1.89e-4;
53  double a_y = 1;
54  double a_dydt = 0;
55  container yscal; // scaling factors
56  double ht; // current time-step
57  double eps_rel, eps_abs; // Accuracy we check at each step
58  double xt, t_stop; // start and stop time
59  int nok=0, nbad=0; // number of bad and good steps
60  container dydx; // temporary current derivatives
61  int nfe = 0;
62 
64 
65  int sys_size = 0;
66 
67  public:
68  // Constructor takes the following arguments:
69  // 1) t_start -- start time
70  // 2) t_stop -- end_time
71  // 3) size -- number of equations
72  // 4) accuracy -- desired accuracy
73  // 5) h1 -- trial size of the first step
74  RKCK45(double t_start_, double accuracy, double h1);
75 
76  // Resize the container
77  void resize(int new_size);
78 
79  // The main function which takes next RK step with predefined accuracy
80  // returns bool: true if time becomes larger of equal to t_stop, otherwise false
81  // arguments:
82  // 1) x -- current time (independent variable), which will be changes for a step when successful
83  // 2) y -- current solution (dependent variable)
84  // 3) derivs -- function which evaluates derivatives
85  template <class functor>
86  void Step(double& x, container& y, functor& derivs, double hmax=1e20, double hmin=1e-6);
87 
88  //template <class functor>
89  //void Step_RK4(double &x, double h, container& y, functor& derivs);
90 
91  template <class functor, class AfterStep>
92  void Step_to(double t_stop, double& x, container& y, functor& derivs, AfterStep &after_step);
93 
94  double h(){return ht;}
95  double size(){return sys_size;}
96  int get_fn_evals(){return nfe;}
97 
98  private:
99  template <class functor>
100  void RKTry(container& y, container& dydx, double& x, double h, container& yout, container& yerr, functor& derivs);
101 
102  template <class functor>
103  void RKStep(container& y, container& dydx, double& x, double htry, double& hdid, double& hnext, functor& derivs);
104 };
105 
106 
107 #include "../src/rkck45.tpp"
108 
109 
110 #endif
111 
112 
113 
int nok
Definition: rkck45.h:59
double size()
Definition: rkck45.h:95
int get_fn_evals()
Definition: rkck45.h:96
double a_dydt
Definition: rkck45.h:54
static constexpr double PGROW
Definition: rkck45.h:50
container yt
Definition: rkck45.h:63
container k3
Definition: rkck45.h:63
double eps_rel
Definition: rkck45.h:57
int nbad
Definition: rkck45.h:59
container yscal
Definition: rkck45.h:55
void RKStep(container &y, container &dydx, double &x, double htry, double &hdid, double &hnext, functor &derivs)
Definition: rkck45.tpp:11
double h()
Definition: rkck45.h:94
int nfe
Definition: rkck45.h:61
double ht
Definition: rkck45.h:56
container k4
Definition: rkck45.h:63
void resize(int new_size)
Definition: rkck45.cpp:10
RKCK45(double t_start_, double accuracy, double h1)
Definition: rkck45.cpp:5
static constexpr double PSHRNK
Definition: rkck45.h:51
double eps_abs
Definition: rkck45.h:57
static constexpr double SAFETY
Definition: rkck45.h:49
void Step_to(double t_stop, double &x, container &y, functor &derivs, AfterStep &after_step)
Definition: rkck45.tpp:149
double xt
Definition: rkck45.h:58
container k1
Definition: rkck45.h:63
double a_y
Definition: rkck45.h:53
container k2
Definition: rkck45.h:63
Definition: rkck45.h:47
void Step(double &x, container &y, functor &derivs, double hmax=1e20, double hmin=1e-6)
Definition: rkck45.tpp:103
void RKTry(container &y, container &dydx, double &x, double h, container &yout, container &yerr, functor &derivs)
Definition: rkck45.tpp:43
container k5
Definition: rkck45.h:63
double t_stop
Definition: rkck45.h:58
static constexpr double ERRCON
Definition: rkck45.h:52
container dydx
Definition: rkck45.h:60
std::vector< double > container
Definition: rkck45.h:32
int sys_size
Definition: rkck45.h:65