Paul Aner
Paul Aner

Reputation: 493

Using Boost/odeint with class (calling integrate from outside the class with ODE-function inside the class)

I have written a lot of code that got VERY messy over the last couple of weeks/months and I wanted to clean up, by putting most of it into classes. However, I can't figure out, how to call an ODE-function, that is inside a class with Boost/integrate from outside the class. I'm not sure, if this is the cleanest/best practise, but I tried something like this:

class ODEclass
{
public:
    // Lots of stuff here (initialization, methods, properties, parameters)
    void odefun(std::vector<double> x, std::vector<double>& dxdt, const double t)
    {
        // ...
    }
}

And then calling somewhere outside the class:

integrate_adaptive(make_controlled<runge_kutta_cash_karp54<std::vector<double>>>(errTol[0], errTol[1]), odefun, Data, 0.0, something, something);

With odefun as ODEclass::odefun, ODEinstance.odefun, ODEinstance->odefun and every other way I could think of, but nothing works. Putting a void odefun(...) outside the class works perfectly fine, but that would go against my current cleaning urge.

I found answers here on Stackoverflow that would use std::bind, but those do odeint-calls form inside that class and don't seem to work in my case.

So how can I pass an ODE-function to odeint that is inside a class? Or is there a better (clean) way to do this? I hope, I put all the necessary information in here. If something is missing, please let me know.

Upvotes: 0

Views: 437

Answers (1)

sehe
sehe

Reputation: 392999

Calling a non-static member function requires an instance of the class.

The simplest fix is to mark the odefun as static. This will work unless you wanted to access the internals. Assuming that the correct signature for odefun is actually void(std::vector<double>, std::vector<double>&, const double) or compatible:

ODEclass instance;
auto odefun = std::bind(&ODEclass::odefun, &instance, _1, _2, _3);

Upvotes: 1

Related Questions