FML
Loading...
Searching...
No Matches
ODESolver.h
Go to the documentation of this file.
1#ifndef ODESOLVER_HEADER
2#define ODESOLVER_HEADER
3#include <gsl/gsl_errno.h>
4#include <gsl/gsl_integration.h>
5#include <gsl/gsl_odeiv2.h>
6
7#include <functional>
8#include <iomanip>
9#include <iostream>
10#include <vector>
11
12#ifdef USE_MPI
13#include <mpi.h>
14#endif
15
16namespace FML {
17
18 /// This nanespace contains various solvers
19 namespace SOLVERS {
20
21 /// This namespace contains solvers for (coupled) ordinary differential equations.
22 namespace ODESOLVER {
23
24 // The fiducial stepper if not provided by the user
25#ifndef ODESOLVER_FIDUCIAL_STEPPER
26#define ODESOLVER_FIDUCIAL_STEPPER gsl_odeiv2_step_rk2
27#endif
28
29 using DVector = std::vector<double>;
30 using DVector2D = std::vector<DVector>;
31 using ODEFunctionPointer = int (*)(double, const double[], double[], void *);
32 using ODEFunctionPointerJacobian = int (*)(double, const double[], double[], double[], void *);
33 using ODEFunction = std::function<int(double, const double *, double *)>;
34 using ODEFunctionJacobian = std::function<int(double, const double *, double *, double *)>;
35
37
38 //===================================================
39 ///
40 /// This is a wrapper around the GSL library to easily
41 /// solve ODEs and return the data in whatever format
42 /// you want. Safe to use within OpenMP threads as
43 /// long as spline is not created within a thread!
44 ///
45 /// Supplying an x-array the solution will be stored at
46 /// each of the points in the array (must be monotonic)
47 ///
48 /// Example use:
49 /// Solve the system dy0/dx=y1, dy1/dx=-y0 with
50 /// y0 = 2.0 and y1 = -2.0 on the interval [0,1]
51 /// and store the solution at x = 0.0, 0.25, 0.5 and 1.0
52 ///
53 ///---------------------------------------------------
54 /// ODEFunction dydx = [&](double x, const double *y, double *dydx){
55 /// dydx[0] = y[1];
56 /// dydx[1] = -y[0];
57 /// return GSL_SUCCESS;
58 /// };
59 /// DVector yini{2.0, -2.0}
60 /// DVector x_array{0.0, 0.25, 0.5, 1.0};
61 /// ODESolver ode;
62 /// ode.solve(dydx, x_array, yini);
63 /// auto solution = ode.get_data();
64 ///---------------------------------------------------
65 ///
66 /// If the solver throws (or the GSL driver fails) the old data is not valid.
67 ///
68 /// Choices of steppers (fiducial one set below). NB: the fiducial rk2 is robust, but higher order steppers
69 /// like rkf45 or rk8pd are often much more efficient: for y'' = -y on [0,1000] with the fiducial accuracy
70 /// rk2 needs ~350000 function calls (error 2e-5) and rk8pd ~15000 (error 2e-7).
71 /// The implicit steppers rk1imp, rk2imp, rk4imp, bsimp and msbdf require the jacobian
72 /// (an error is thrown if it is not provided, unless set_finite_difference_jacobian(true) was called:
73 /// then it is computed by forward finite differences of the ODE function):
74 /// gsl_odeiv2_step_rk2;
75 /// gsl_odeiv2_step_rk4;
76 /// gsl_odeiv2_step_rkf45;
77 /// gsl_odeiv2_step_rkck;
78 /// gsl_odeiv2_step_rk8pd;
79 /// gsl_odeiv2_step_rk2imp;
80 /// gsl_odeiv2_step_rk4imp;
81 /// gsl_odeiv2_step_bsimp;
82 /// gsl_odeiv2_step_rk1imp;
83 /// gsl_odeiv2_step_msadams;
84 /// gsl_odeiv2_step_msbdf;
85 ///
86 /// Compile time defines:
87 ///
88 /// USE_MPI : Use MPI (only change is in how errors are handled)
89 ///
90 /// ODESOLVER_THROW_ON_ERROR : Throw std::runtime_error also with USE_MPI (instead of MPI_Abort),
91 /// so that the caller can catch it (e.g. an MCMC rejecting a point)
92 ///
93 /// ODESOLVER_FIDUCIAL_STEPPER : The choice of stepper
94 ///
95 //===================================================
96
97 class ODESolver {
98 private:
99 // Fiducial accuracy parameters
100 double hstart = 1e-3;
101 double abserr = 1e-7;
102 double relerr = 1e-7;
103
104 bool verbose = false;
105 bool finite_difference_jacobian = false;
106
107 int nequations = 1;
108 int num_x_points = 0;
109
110 std::vector<DVector> data{};
111 std::vector<DVector> derivative_data{};
112
113 void throw_error(std::string errormessage) const;
114
115 public:
116 ODESolver() = default;
117 ODESolver(double hstart, double abserr, double relerr);
118 ODESolver(const ODESolver & rhs) = delete;
119 ODESolver & operator=(const ODESolver & rhs) = delete;
120
121 void solve(ODEFunctionPointer ode_equation,
122 void * parameters,
123 const DVector & xarr,
124 const DVector & yinitial,
125 const gsl_odeiv2_step_type * stepper = ODESOLVER_FIDUCIAL_STEPPER,
126 ODEFunctionPointerJacobian jacobian = nullptr);
127
128 /// Solve the ODE defined by the ode_equation. We solve it from the first to the last point in the xarr
129 /// and store the value of the solution in all points in xarr. The initial value is in yinitial and
130 /// we have the option to choose a stepper and provide the jacobian if the stepper requires that
131 void solve(const ODEFunction & ode_equation,
132 const DVector & xarr,
133 const DVector & yinitial,
134 const gsl_odeiv2_step_type * stepper = ODESOLVER_FIDUCIAL_STEPPER,
135 const ODEFunctionJacobian & jacobian = *no_jacobian_ptr);
136
137 /// Implicit steppers without a jacobian: compute it by forward finite differences of the ODE
138 /// function (relative step 1e-8; n + 2 calls per jacobian) instead of throwing
139 void set_finite_difference_jacobian(bool onoff);
140
141 /// Show info while solving or not
142 void set_verbose(bool onoff);
143 /// Set the accuracy parameters (first guess for the step-size, the absolute and relative error)
144 void set_accuracy(const double hstart, const double abserr, const double relerr);
145
146 /// Get the solution at the end point
147 DVector get_final_data() const;
148 /// Get the solution for a given component at the end point
149 double get_final_data_by_component(int icomponent) const;
150
151 /// Get all the data z_ij = ( y_j(xarr_i) ), i.e. data[ix][icomponent]
152 DVector2D get_data() const;
153
154 /// Get all the data transposed z_ij = ( y_i(xarr_j) ), i.e. data[icomponent][ix]
156
157 /// Get the data for a particular component y_i(xarr)
158 DVector get_data_by_component(int icomponent) const;
159
160 /// Get the data at a particular x-index y(xarr_i)
161 DVector get_data_by_xindex(int ix) const;
162
163 /// Get all the data for the derivatives z_ij = ( dy_j/dx(xarr_i) ), i.e. data[ix][icomponent]. The
164 /// derivatives are computed by calling the ODE function at the solution (not finite differences)
166
167 /// Get the data dy_i/dx(xarr) for the derivatives for a particular component
168 DVector get_derivative_data_by_component(int icomponent) const;
169 };
170 } // namespace ODESOLVER
171 } // namespace SOLVERS
172} // namespace FML
173#endif
This is a wrapper around the GSL library to easily solve ODEs and return the data in whatever format ...
Definition ODESolver.h:97
DVector get_data_by_xindex(int ix) const
Get the data at a particular x-index y(xarr_i)
void set_finite_difference_jacobian(bool onoff)
Implicit steppers without a jacobian: compute it by forward finite differences of the ODE function (r...
DVector2D get_data() const
Get all the data z_ij = ( y_j(xarr_i) ), i.e. data[ix][icomponent].
double get_final_data_by_component(int icomponent) const
Get the solution for a given component at the end point.
DVector2D get_derivative_data() const
Get all the data for the derivatives z_ij = ( dy_j/dx(xarr_i) ), i.e.
ODESolver & operator=(const ODESolver &rhs)=delete
DVector get_derivative_data_by_component(int icomponent) const
Get the data dy_i/dx(xarr) for the derivatives for a particular component.
DVector get_data_by_component(int icomponent) const
Get the data for a particular component y_i(xarr)
DVector get_final_data() const
Get the solution at the end point.
void set_verbose(bool onoff)
Show info while solving or not.
void solve(ODEFunctionPointer ode_equation, void *parameters, const DVector &xarr, const DVector &yinitial, const gsl_odeiv2_step_type *stepper=ODESOLVER_FIDUCIAL_STEPPER, ODEFunctionPointerJacobian jacobian=nullptr)
void set_accuracy(const double hstart, const double abserr, const double relerr)
Set the accuracy parameters (first guess for the step-size, the absolute and relative error)
DVector2D get_data_transpose() const
Get all the data transposed z_ij = ( y_i(xarr_j) ), i.e. data[icomponent][ix].
ODESolver(const ODESolver &rhs)=delete
std::vector< double > DVector
Definition ODESolver.h:29
std::vector< DVector > DVector2D
Definition ODESolver.h:30
int(*)(double, const double[], double[], double[], void *) ODEFunctionPointerJacobian
Definition ODESolver.h:32
ODEFunctionJacobian * no_jacobian_ptr
Definition ODESolver.cpp:33
int(*)(double, const double[], double[], void *) ODEFunctionPointer
Definition ODESolver.h:31
std::function< int(double, const double *, double *, double *)> ODEFunctionJacobian
Definition ODESolver.h:34
std::function< int(double, const double *, double *)> ODEFunction
Definition ODESolver.h:33