在 C++ 中调用 ODE 求解器 (odeint) class
Calling ODE solver (odeint) within c++ class
我需要求解使用 odeint 库设置的 ODE 方程组。 "main.cpp" 文件只是调用计算(后面会扩展,这里简化版保持代码干净)
main.cpp
#include "calc.h"
int main()
{
calc c;
c.startC();
return 0;
}
这里是计算头,starC();已设置 public。
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;
}
此操作以 "Initializing of ODE solver" 级别的错误结束:
'((calc*)this)->calc::solveO' cannot be used as a member pointer,
since it is of type '<unresolved overloaded function type>'
这个调用有什么问题:
this->*solveO
在 "integrate_const" 中调用 solveO 的正确方法是什么?
编辑:
感谢 @tobi303 和 @rbelli 的帮助,它现在可以工作了。这里我post总结,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
中使用 class 的任何成员,您可以简单地使其成为一个自由函数:
void solveO( const vector_type &y , vector_type &ODE , const double t )
{
dy[0] = y[1];
dy[1] = -y[0] - g*y[1];
}
我需要求解使用 odeint 库设置的 ODE 方程组。 "main.cpp" 文件只是调用计算(后面会扩展,这里简化版保持代码干净)
main.cpp
#include "calc.h"
int main()
{
calc c;
c.startC();
return 0;
}
这里是计算头,starC();已设置 public。
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;
}
此操作以 "Initializing of ODE solver" 级别的错误结束:
'((calc*)this)->calc::solveO' cannot be used as a member pointer,
since it is of type '<unresolved overloaded function type>'
这个调用有什么问题:
this->*solveO
在 "integrate_const" 中调用 solveO 的正确方法是什么?
编辑: 感谢 @tobi303 和 @rbelli 的帮助,它现在可以工作了。这里我post总结,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
中使用 class 的任何成员,您可以简单地使其成为一个自由函数:
void solveO( const vector_type &y , vector_type &ODE , const double t )
{
dy[0] = y[1];
dy[1] = -y[0] - g*y[1];
}