|
FML
|
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. | |
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);
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.
|
default |
| FML::SOLVERS::ODESOLVER::ODESolver::ODESolver | ( | double | hstart, |
| double | abserr, | ||
| double | relerr ) |
Definition at line 96 of file ODESolver.cpp.
|
delete |
| 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.
| 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.
| DVector FML::SOLVERS::ODESOLVER::ODESolver::get_data_by_xindex | ( | int | ix | ) | const |
| 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.
| 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.
| DVector FML::SOLVERS::ODESOLVER::ODESolver::get_derivative_data_by_component | ( | int | icomponent | ) | const |
| DVector FML::SOLVERS::ODESOLVER::ODESolver::get_final_data | ( | ) | const |
| 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.
| 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.
| 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.
| void FML::SOLVERS::ODESOLVER::ODESolver::set_verbose | ( | bool | onoff | ) |
Show info while solving or not.
Definition at line 228 of file ODESolver.cpp.
| 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.
| 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.