13 void ODESolver::throw_error(std::string errormessage)
const {
14#if defined(USE_MPI) && !defined(ODESOLVER_THROW_ON_ERROR)
15 std::cout << errormessage << std::flush;
16 MPI_Abort(MPI_COMM_WORLD, 1);
19 throw std::runtime_error(errormessage);
29 [[maybe_unused]]
const double *
y,
30 [[maybe_unused]]
double * dfdy,
31 [[maybe_unused]]
double * dfdt) {
return GSL_SUCCESS; };
35 struct ODEParamWrapper {
42 : deriv(_deriv), jacobian(_jacobian) {}
43 auto get_deriv() ->
const ODEFunction & {
return deriv; }
47 int ode_wrapper(
double x,
const double *
y,
double * dydx,
void * param) {
48 auto * p =
static_cast<struct ODEParamWrapper *
>(param);
49 return (p->get_deriv())(x,
y, dydx);
52 int jacobian_wrapper(
double t,
const double *
y,
double * dfdy,
double * dfdt,
void * param) {
53 auto * p =
static_cast<struct ODEParamWrapper *
>(param);
54 return (p->get_jacobian())(t,
y, dfdy, dfdt);
60 struct FiniteDifferenceJacobian {
65 FiniteDifferenceJacobian(
const ODEFunction & d,
size_t _n,
double dir)
66 : deriv(d), n(_n), direction(dir), ytmp(_n), f0(_n), f1(_n) {}
67 int operator()(
double t,
const double *
y,
double * dfdy,
double * dfdt) {
68 int status = deriv(t,
y, f0.data());
69 if (status != GSL_SUCCESS)
71 ytmp.assign(
y,
y + n);
72 for (
size_t j = 0; j < n; j++) {
73 const double h = 1e-8 * std::max(std::fabs(
y[j]), 1e-8);
75 status = deriv(t, ytmp.data(), f1.data());
76 if (status != GSL_SUCCESS)
78 for (
size_t i = 0; i < n; i++)
79 dfdy[i * n + j] = (f1[i] - f0[i]) /
h;
82 const double ht = -direction * 1e-8 * std::max(std::fabs(t), 1.0);
83 status = deriv(t + ht,
y, f1.data());
84 if (status != GSL_SUCCESS)
86 for (
size_t i = 0; i < n; i++)
87 dfdt[i] = (f1[i] - f0[i]) / ht;
97 : hstart(hstart), abserr(abserr), relerr(relerr) {}
102 const gsl_odeiv2_step_type * stepper,
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);
113 struct ODEParamWrapper equations(ode_equation, fdjac);
117 struct ODEParamWrapper equations(ode_equation, jacobian);
131 const gsl_odeiv2_step_type * stepper,
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);
140 if (nequations < 1) {
141 std::string errormessage =
"[ODESolver::solve] The yinitial is empty\n";
142 throw_error(errormessage);
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}) {
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);
161 double sign = xarr[1] > xarr[0] ? 1.0 : -1.0;
164 gsl_odeiv2_system ode_system = {ode_equation, jacobian, size_t(yinitial.size()), parameters};
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();
175 ode_equation(x,
y.data(), dydx.data(), parameters);
178 data = std::vector<DVector>(xarr.size(), yinitial);
179 derivative_data = std::vector<DVector>(xarr.size(), dydx);
182 std::cout <<
"ODESolver step " << std::setw(5) << 0 <<
" / " << num_x_points - 1 <<
" x: ["
183 << std::setw(10) << x <<
"] ";
185 for (
auto & ycomp :
y) {
186 std::cout <<
" " << std::setw(10) << ycomp <<
" ";
188 std::cout <<
"]" << std::endl;
192 for (
int i = 1; i < num_x_points; i++) {
193 const double xnew = xarr[i];
196 std::cout <<
"ODESolver step " << std::setw(5) << i <<
" / " << num_x_points - 1 <<
" x: ["
197 << std::setw(10) << 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) +
208 throw_error(errormessage);
213 for (
auto & ycomp :
y) {
214 std::cout <<
" " << std::setw(10) << ycomp <<
" ";
216 std::cout <<
"]" << std::endl;
220 ode_equation(x,
y.data(), dydx.data(), parameters);
224 derivative_data[i] = dydx;
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];
239 return data_transpose;
244 for (
int icomponent = 0; icomponent < nequations; icomponent++) {
245 res[icomponent] = data[num_x_points - 1][icomponent];
251 return data[num_x_points - 1][icomponent];
256 for (
int ix = 0; ix < num_x_points; ix++) {
257 res[ix] = data[ix][icomponent];
264 for (
int icomponent = 0; icomponent < nequations; icomponent++) {
265 res[icomponent] = data[ix][icomponent];
274 for (
int ix = 0; ix < num_x_points; ix++) {
275 res[ix] = derivative_data[ix][icomponent];
FML::SOLVERS::FFTLog::DVector DVector
FML::SOLVERS::ODESOLVER::ODEFunction ODEFunction
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.
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].
std::vector< double > DVector
std::vector< DVector > DVector2D
int ode_wrapper(double x, const double *y, double *dydx, void *param)
ODEFunctionJacobian no_jacobian
int(*)(double, const double[], double[], double[], void *) ODEFunctionPointerJacobian
ODEFunctionJacobian * no_jacobian_ptr
int(*)(double, const double[], double[], void *) ODEFunctionPointer
std::function< int(double, const double *, double *, double *)> ODEFunctionJacobian
std::function< int(double, const double *, double *)> ODEFunction
int jacobian_wrapper(double t, const double *y, double *dfdy, double *dfdt, void *param)