在C 类中调用ODE求解器(ODEINT)

Calling ODE solver (odeint) within c++ class

本文关键字:ODEINT ODE 调用      更新时间:2023-10-16

我需要求解使用Odeint库设置的ODE方程。" main.cpp"文件仅为调用计算(以后将延长,此处简单版本可以保持代码清洁)

main.cpp

#include "calc.h"
int main()
{
    calc c;
    c.startC();
    return 0;
}

这是计算标头starc();公开。 calc.h

#ifndef MAIN_H
#define MAIN_H
#include <iostream>
#include <boost/numeric/odeint.hpp>
#include <math.h>
using namespace std;
using namespace boost::numeric::odeint;
typedef std::vector< double > vector_type;
class calc
{
public:
    int main();
    void startC();
private:
    void solveODE(const vector_type &y , vector_type &ODE , const double t );
    void printR();
};
#endif // MAIN_H

这是主要计算部分。同样在这里发生错误: calc.cpp

include <iostream>
#include <boost/numeric/odeint.hpp>
#include <math.h>
#include "calc.h"
using namespace std;
using namespace boost::numeric::odeint;
const double g = 0.15;

void calc::solveO( const vector_type &y , vector_type &ODE , const double t )
{
    dy[0] =  y[1];
    dy[1] = -y[0] - g*y[1];
}
void calc::printR(const vector_type &y, const double t)
{
    cout << t << endl;
    cout << y[0] << endl;
}
void calc::startC()
{
    const double dt = 0.1;
    typedef runge_kutta_dopri5<vector_type> stepper_type;
    vector_type y(2);
    //Initial conditins
    y[0]=   1.0;
    y[1]=   0.0;

    //Initializing of ODE solver
    integrate_const(make_dense_output<stepper_type>( 1E-12, 1E-6 ), 
                    this->*solveO, y, 0.0, 1.0, dt , printResults); //ERROR HERE
}
int calc::main()
{
    return 0;
}

此操作以:

的" ode solver初始化"级别以错误结尾
'((calc*)this)->calc::solveO' cannot be used as a member pointer,
since it is of type '<unresolved overloaded function type>'

此调用有什么问题:

this->*solveO

,在" integnate_const"内调用solveo的正确方法是什么?

编辑:多亏了 @tobi303和@rbelli的帮助,现在可以使用。在这里,我发布了总结,TL:DR解释:

最简单的解决方案是使solveo和printr自由功能并致电:

integrate_const(make_dense_output<stepper_type>( 1E-12, 1E-6 ), solveO, y, 0.0, 1.0, dt , printR);

如果integrate_const采用函数指针,则无法传递成员函数指针。成员函数指针与函数指针不同,因为它们隐含地需要this作为参数。

由于您不使用solveO中的任何类成员,因此可以简单地使其成为免费功能:

void solveO( const vector_type &y , vector_type &ODE , const double t )
{
    dy[0] =  y[1];
    dy[1] = -y[0] - g*y[1];
}