FML
Loading...
Searching...
No Matches
FML::SOLVERS::ODESOLVER::ODESolver Class Reference

This is a wrapper around the GSL library to easily solve ODEs and return the data in whatever format you want. More...

#include <ODESolver.h>

Public Member Functions

 ODESolver ()=default
 
 ODESolver (double hstart, double abserr, double relerr)
 
 ODESolver (const ODESolver &rhs)=delete
 
ODESolver & operator= (const ODESolver &rhs)=delete
 
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 solve (const ODEFunction &ode_equation, const DVector &xarr, const DVector &yinitial, const gsl_odeiv2_step_type *stepper=ODESOLVER_FIDUCIAL_STEPPER, const ODEFunctionJacobian &jacobian= *no_jacobian_ptr)
 Solve the ODE defined by the ode_equation.
 
void set_finite_difference_jacobian (bool onoff)
 Implicit steppers without a jacobian: compute it by forward finite differences of the ODE function (relative step 1e-8; n + 2 calls per jacobian) instead of throwing.
 
void set_verbose (bool onoff)
 Show info while solving or not.
 
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)
 
DVector get_final_data () const
 Get the solution at the end point.
 
double get_final_data_by_component (int icomponent) const
 Get the solution for a given component at the end point.
 
DVector2D get_data () const
 Get all the data z_ij = ( y_j(xarr_i) ), i.e. data[ix][icomponent].
 
DVector2D get_data_transpose () const
 Get all the data transposed z_ij = ( y_i(xarr_j) ), i.e. data[icomponent][ix].
 
DVector get_data_by_component (int icomponent) const
 Get the data for a particular component y_i(xarr)
 
DVector get_data_by_xindex (int ix) const
 Get the data at a particular x-index y(xarr_i)
 
DVector2D get_derivative_data () const
 Get all the data for the derivatives z_ij = ( dy_j/dx(xarr_i) ), i.e.
 
DVector get_derivative_data_by_component (int icomponent) const
 Get the data dy_i/dx(xarr) for the derivatives for a particular component.
 

Detailed Description

This is a wrapper around the GSL library to easily solve ODEs and return the data in whatever format you want.

Safe to use within OpenMP threads as long as spline is not created within a thread!

Supplying an x-array the solution will be stored at each of the points in the array (must be monotonic)

Example use: Solve the system dy0/dx=y1, dy1/dx=-y0 with y0 = 2.0 and y1 = -2.0 on the interval [0,1] and store the solution at x = 0.0, 0.25, 0.5 and 1.0


ODEFunction dydx = [&](double x, const double *y, double *dydx){ dydx[0] = y[1]; dydx[1] = -y[0]; return GSL_SUCCESS; }; DVector yini{2.0, -2.0} DVector x_array{0.0, 0.25, 0.5, 1.0}; ODESolver ode; ode.solve(dydx, x_array, yini);

auto solution = ode.get_data();

If the solver throws (or the GSL driver fails) the old data is not valid.

Choices of steppers (fiducial one set below). NB: the fiducial rk2 is robust, but higher order steppers like rkf45 or rk8pd are often much more efficient: for y'' = -y on [0,1000] with the fiducial accuracy rk2 needs ~350000 function calls (error 2e-5) and rk8pd ~15000 (error 2e-7). The implicit steppers rk1imp, rk2imp, rk4imp, bsimp and msbdf require the jacobian (an error is thrown if it is not provided, unless set_finite_difference_jacobian(true) was called: then it is computed by forward finite differences of the ODE function): gsl_odeiv2_step_rk2; gsl_odeiv2_step_rk4; gsl_odeiv2_step_rkf45; gsl_odeiv2_step_rkck; gsl_odeiv2_step_rk8pd; gsl_odeiv2_step_rk2imp; gsl_odeiv2_step_rk4imp; gsl_odeiv2_step_bsimp; gsl_odeiv2_step_rk1imp; gsl_odeiv2_step_msadams; gsl_odeiv2_step_msbdf;

Compile time defines:

USE_MPI : Use MPI (only change is in how errors are handled)

ODESOLVER_THROW_ON_ERROR : Throw std::runtime_error also with USE_MPI (instead of MPI_Abort), so that the caller can catch it (e.g. an MCMC rejecting a point)

ODESOLVER_FIDUCIAL_STEPPER : The choice of stepper

Definition at line 97 of file ODESolver.h.

Constructor & Destructor Documentation

◆ ODESolver() [1/3]

FML::SOLVERS::ODESOLVER::ODESolver::ODESolver ( )
default

◆ ODESolver() [2/3]

FML::SOLVERS::ODESOLVER::ODESolver::ODESolver ( double hstart,
double abserr,
double relerr )

Definition at line 96 of file ODESolver.cpp.

97 : hstart(hstart), abserr(abserr), relerr(relerr) {}

◆ ODESolver() [3/3]

FML::SOLVERS::ODESOLVER::ODESolver::ODESolver ( const ODESolver & rhs)
delete

Member Function Documentation

◆ get_data()

DVector2D FML::SOLVERS::ODESOLVER::ODESolver::get_data ( ) const

Get all the data z_ij = ( y_j(xarr_i) ), i.e. data[ix][icomponent].

Definition at line 230 of file ODESolver.cpp.

230{ return data; }

◆ get_data_by_component()

DVector FML::SOLVERS::ODESOLVER::ODESolver::get_data_by_component ( int icomponent) const

Get the data for a particular component y_i(xarr)

Definition at line 254 of file ODESolver.cpp.

254 {
255 DVector res(num_x_points);
256 for (int ix = 0; ix < num_x_points; ix++) {
257 res[ix] = data[ix][icomponent];
258 }
259 return res;
260 }
FML::SOLVERS::FFTLog::DVector DVector
Definition Main.cpp:4

◆ get_data_by_xindex()

DVector FML::SOLVERS::ODESOLVER::ODESolver::get_data_by_xindex ( int ix) const

Get the data at a particular x-index y(xarr_i)

Definition at line 262 of file ODESolver.cpp.

262 {
263 DVector res(nequations);
264 for (int icomponent = 0; icomponent < nequations; icomponent++) {
265 res[icomponent] = data[ix][icomponent];
266 }
267 return res;
268 }

◆ get_data_transpose()

DVector2D FML::SOLVERS::ODESOLVER::ODESolver::get_data_transpose ( ) const

Get all the data transposed z_ij = ( y_i(xarr_j) ), i.e. data[icomponent][ix].

Definition at line 232 of file ODESolver.cpp.

232 {
233 auto data_transpose = std::vector<DVector>(nequations, DVector(num_x_points));
234 for (int ix = 0; ix < num_x_points; ix++) {
235 for (int icomponent = 0; icomponent < nequations; icomponent++) {
236 data_transpose[icomponent][ix] = data[ix][icomponent];
237 }
238 }
239 return data_transpose;
240 }
std::vector< double > DVector
Definition ODESolver.h:29

◆ get_derivative_data()

DVector2D FML::SOLVERS::ODESOLVER::ODESolver::get_derivative_data ( ) const

Get all the data for the derivatives z_ij = ( dy_j/dx(xarr_i) ), i.e.

data[ix][icomponent]. The derivatives are computed by calling the ODE function at the solution (not finite differences)

Definition at line 270 of file ODESolver.cpp.

270{ return derivative_data; }

◆ get_derivative_data_by_component()

DVector FML::SOLVERS::ODESOLVER::ODESolver::get_derivative_data_by_component ( int icomponent) const

Get the data dy_i/dx(xarr) for the derivatives for a particular component.

Definition at line 272 of file ODESolver.cpp.

272 {
273 DVector res(num_x_points);
274 for (int ix = 0; ix < num_x_points; ix++) {
275 res[ix] = derivative_data[ix][icomponent];
276 }
277 return res;
278 }

◆ get_final_data()

DVector FML::SOLVERS::ODESOLVER::ODESolver::get_final_data ( ) const

Get the solution at the end point.

Definition at line 242 of file ODESolver.cpp.

242 {
243 DVector res(nequations);
244 for (int icomponent = 0; icomponent < nequations; icomponent++) {
245 res[icomponent] = data[num_x_points - 1][icomponent];
246 }
247 return res;
248 }

◆ get_final_data_by_component()

double FML::SOLVERS::ODESOLVER::ODESolver::get_final_data_by_component ( int icomponent) const

Get the solution for a given component at the end point.

Definition at line 250 of file ODESolver.cpp.

250 {
251 return data[num_x_points - 1][icomponent];
252 }

◆ operator=()

ODESolver & FML::SOLVERS::ODESOLVER::ODESolver::operator= ( const ODESolver & rhs)
delete

◆ set_accuracy()

void FML::SOLVERS::ODESOLVER::ODESolver::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)

Definition at line 280 of file ODESolver.cpp.

280 {
281 hstart = h;
282 abserr = a;
283 relerr = r;
284 }
const double h
Definition Main.cpp:66

◆ set_finite_difference_jacobian()

void FML::SOLVERS::ODESOLVER::ODESolver::set_finite_difference_jacobian ( bool onoff)

Implicit steppers without a jacobian: compute it by forward finite differences of the ODE function (relative step 1e-8; n + 2 calls per jacobian) instead of throwing.

Definition at line 121 of file ODESolver.cpp.

121{ finite_difference_jacobian = onoff; }

◆ set_verbose()

void FML::SOLVERS::ODESOLVER::ODESolver::set_verbose ( bool onoff)

Show info while solving or not.

Definition at line 228 of file ODESolver.cpp.

228{ verbose = onoff; }

◆ solve() [1/2]

void FML::SOLVERS::ODESOLVER::ODESolver::solve ( const ODEFunction & ode_equation,
const DVector & xarr,
const DVector & yinitial,
const gsl_odeiv2_step_type * stepper = ODESOLVER_FIDUCIAL_STEPPER,
const ODEFunctionJacobian & jacobian = *no_jacobian_ptr )

Solve the ODE defined by the ode_equation.

We solve it from the first to the last point in the xarr and store the value of the solution in all points in xarr. The initial value is in yinitial and we have the option to choose a stepper and provide the jacobian if the stepper requires that

Definition at line 99 of file ODESolver.cpp.

103 {
104 // If no jacobian is provided pass nullptr to GSL (the dummy no_jacobian does not set dfdy, dfdt
105 // so implicit steppers would use uninitialized memory). We then throw if the stepper needs it,
106 // unless the jacobian is to be computed by finite differences
107 const bool has_jacobian = (&jacobian != no_jacobian_ptr);
108 if (!has_jacobian && finite_difference_jacobian && xarr.size() >= 2 && !yinitial.empty()) {
109 FiniteDifferenceJacobian fd(ode_equation, yinitial.size(), xarr[1] > xarr[0] ? 1.0 : -1.0);
110 const ODEFunctionJacobian fdjac = [&fd](double t, const double * y, double * dfdy, double * dfdt) {
111 return fd(t, y, dfdy, dfdt);
112 };
113 struct ODEParamWrapper equations(ode_equation, fdjac);
114 solve(ode_wrapper, &equations, xarr, yinitial, stepper, jacobian_wrapper);
115 return;
116 }
117 struct ODEParamWrapper equations(ode_equation, jacobian);
118 solve(ode_wrapper, &equations, xarr, yinitial, stepper, has_jacobian ? jacobian_wrapper : nullptr);
119 }
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)
int ode_wrapper(double x, const double *y, double *dydx, void *param)
Definition ODESolver.cpp:47
ODEFunctionJacobian * no_jacobian_ptr
Definition ODESolver.cpp:33
int jacobian_wrapper(double t, const double *y, double *dfdy, double *dfdt, void *param)
Definition ODESolver.cpp:52
return y
Definition Global.h:365

◆ solve() [2/2]

void FML::SOLVERS::ODESOLVER::ODESolver::solve ( ODEFunctionPointer ode_equation,
void * parameters,
const DVector & xarr,
const DVector & yinitial,
const gsl_odeiv2_step_type * stepper = ODESOLVER_FIDUCIAL_STEPPER,
ODEFunctionPointerJacobian jacobian = nullptr )

Definition at line 127 of file ODESolver.cpp.

132 {
133 // Store the number of equations and size of x-array
134 nequations = int(yinitial.size());
135 num_x_points = int(xarr.size());
136 if (num_x_points < 2) {
137 std::string errormessage = "[ODESolver::solve] The xarray needs to have at least size 2\n";
138 throw_error(errormessage);
139 }
140 if (nequations < 1) {
141 std::string errormessage = "[ODESolver::solve] The yinitial is empty\n";
142 throw_error(errormessage);
143 }
144
145 // Implicit steppers need the jacobian (GSL would call a null function pointer)
146 if (jacobian == nullptr) {
147 for (auto st : {gsl_odeiv2_step_rk1imp,
148 gsl_odeiv2_step_rk2imp,
149 gsl_odeiv2_step_rk4imp,
150 gsl_odeiv2_step_bsimp,
151 gsl_odeiv2_step_msbdf}) {
152 if (stepper == st) {
153 std::string errormessage = "[ODESolver::solve] The stepper " + std::string(stepper->name) +
154 " requires the jacobian, but none was provided\n";
155 throw_error(errormessage);
156 }
157 }
158 }
159
160 // Are we integrating forward or backward?
161 double sign = xarr[1] > xarr[0] ? 1.0 : -1.0;
162
163 // Set up the ODE system
164 gsl_odeiv2_system ode_system = {ode_equation, jacobian, size_t(yinitial.size()), parameters};
165 // The driver is freed also if an exception is thrown (e.g. by the users ODE function)
166 std::unique_ptr<gsl_odeiv2_driver, decltype(&gsl_odeiv2_driver_free)> ode_driver_ptr(
167 gsl_odeiv2_driver_alloc_y_new(&ode_system, stepper, std::abs(hstart) * sign, abserr, relerr),
168 &gsl_odeiv2_driver_free);
169 gsl_odeiv2_driver * ode_driver = ode_driver_ptr.get();
170
171 // Initialize with the initial condition
172 double x = xarr[0];
173 DVector y(yinitial);
174 DVector dydx(nequations);
175 ode_equation(x, y.data(), dydx.data(), parameters);
176
177 // Allocate memory for the the results: data[i][j] = y_j(x_i)
178 data = std::vector<DVector>(xarr.size(), yinitial);
179 derivative_data = std::vector<DVector>(xarr.size(), dydx);
180
181 if (verbose) {
182 std::cout << "ODESolver step " << std::setw(5) << 0 << " / " << num_x_points - 1 << " x: ["
183 << std::setw(10) << x << "] ";
184 std::cout << "y: [";
185 for (auto & ycomp : y) {
186 std::cout << " " << std::setw(10) << ycomp << " ";
187 }
188 std::cout << "]" << std::endl;
189 }
190
191 // Solve it step by step...
192 for (int i = 1; i < num_x_points; i++) {
193 const double xnew = xarr[i];
194
195 if (verbose) {
196 std::cout << "ODESolver step " << std::setw(5) << i << " / " << num_x_points - 1 << " x: ["
197 << std::setw(10) << xnew << "] ";
198 }
199
200 // Integrate from x -> xnew
201 const int status = gsl_odeiv2_driver_apply(ode_driver, &x, xnew, y.data());
202 if (status != GSL_SUCCESS) {
203 ode_driver_ptr.reset();
204 std::string errormessage =
205 "[ODESolver::solve] GSL gsl_odeiv2_driver_apply returned non-successful at x = " +
206 std::to_string(x) + " (target " + std::to_string(xnew) + "): " + gsl_strerror(status) +
207 "\n";
208 throw_error(errormessage);
209 }
210
211 if (verbose) {
212 std::cout << "y: [";
213 for (auto & ycomp : y) {
214 std::cout << " " << std::setw(10) << ycomp << " ";
215 }
216 std::cout << "]" << std::endl;
217 }
218
219 // Store the derivative
220 ode_equation(x, y.data(), dydx.data(), parameters);
221
222 // Copy over result
223 data[i] = y;
224 derivative_data[i] = dydx;
225 }
226 }

The documentation for this class was generated from the following files: