I am currently using gsl_odeiv2 methods inside my classes to solve differential equations. But because of the well known memberfunction problem I can not define my ode-system inside the class. I am currently using a workaround: I define my ode in a global namespace:
ODE.hpp:
#include "EoS.hpp"
#include <gsl/gsl_math.h>
#include <gsl/gsl_errno.h>
namespace ODEs
{
struct tov_eq_params {EoS *eos;};
int tov_eq(double, const double *, double *, void *);
}
ODE.cpp:
namespace ODEs {
int tov_eq(double r, const double *PM, double *dPdM, void *p) {
struct tov_eq_params * params = (struct tov_eq_params *)p;
EoS *eos = (params->eos);
...
return GSL_SUCCESS
}
}
and use a pointer to an object of a coustom type (class EoS) as parameter. Inside the class that solves the ode I use:
...
struct tov_eq_params comp_tov_params = {(this->star_eos)};
gsl_odeiv2_evolve *comp_tov_evolve = gsl_odeiv2_evolve_alloc(3);
gsl_odeiv2_system comp_tov_system = {tov_eq, NULL, 3,&comp_tov_params};
...
to initalise my system. This works fine but is a bit messy because I need to declare my differential equations in a global namespace.
I know that it is possible to use template wrappers for gsl_functions stackoverflow.com/questions/.../how-to-avoid-static-member-function-when-using-gsl-with-c/... to use them in C++ Classes. I actually use the wrapper described there to define functions for gsl_integration Methods within my classes and it works perfectly and is much cleaner and less code to write. For example: I can use my star_eos Object from above direcly inside the function:
auto dBf = [=](double r)->double{
return 4 * M_PI * gsl_pow_2(r) * (this->star_eos)->nbar(this->P(r)) * sqrt(this->expLambda(r))* 1e54;
};
gsl_function_pp<decltype(dBf)> dBfp(dBf);
gsl_function *dB = static_cast<gsl_function*>(&dBfp);
I tried to write such a template wrapper for the int(double r, const double *PM, double *dPdM, void *p) functions that gsl_odeiv2_system needs but I failed because I am new to C++ and did not fully understand its template/static_cast mechanisms.
Is there someone who uses gsl_odeiv methods and its ode systems with a template wrapper? Or can someone come up with a template similar to the one described above for gsl_functions but for the int(...) ode.
Thinking about how I got it working with the differential equations set in a global namespace I found a solution for my problem. I now have a working wrapper. In a global namespace I have the following:
So I bassically have all my specific information stored in my new class ode_System, which has an int dim to specify the systems dimensions and a pointer so a std::function object. This object represents the mathematica differential equation system.
Inside my class, where I want to solve a differential equation using gsl_odeiv2, I define that system using a lambda function:
The above system represents a coupled system of 3 differential equations. I then declare a ode_System object with the right dimension and set its function pointer df to my defined system. Then I only need a structure with a reference to that system and done: I can use my differential equation defined inside my class with gsl_odeiv2_system:
As far as I can tell this works just as well (or bad) as the implementation I presented in my question. It can use some clean up but in principle this works fine for me.
But if someone knows a better way to do this please feel free to share it!