使用boost ::数字::里面的类odeint(Using boost::numeric::ode

2019-10-16 19:41发布

对于模拟,我使用boost ::数字:: odeint但我有一个问题。 我使用了我的一个类的方法内部集成的功能,我有“不匹配功能呼叫整合”的错误。 为了更清楚,这里是我的代码的压缩版本:

#include "MotionGeneration.h"
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>

typedef boost::array< double , 10 > state_type;

MotionGeneration::MotionGeneration(some_data) {
     //My constructor.
     //some_data assignment.
}

MotionGeneration::~MotionGeneration() {

}

double MotionGeneration::getError(double time) {
   //error calculation.
}

void MotionGeneration::execute(){
    state_type init_conf = { 0, -1.5708, 0, 0, 0, -1.5708, 0, -1.5708, 0, 0.5};
    boost::numeric::odeint::integrate(motionScheme, init_conf, 0.0, 1.0, 0.05, plot);
}

void MotionGeneration::motionScheme(const state_type &q, state_type &q_dot, double t){
     //Some long code goes here. Also I have some calls to some methods of this class. for example:
     double err = getError(t);          
}

void MotionGeneration::plot(const state_type &q , const double t){
    //some data pfintf here.
}

请注意,所有的我的方法是静态的,并且事实上,我不能用静态方法。 当我生成项目,我有以下错误:

error: no matching function for call to `integrate(<unknown type>, state_type&, double, double, double, <unknown type>)'

我认为这是具有系统功能的一类方法的问题,但我不知道如何处理这种情况。

Answer 1:

odeint需要一个operator()( const state_type &x , state_type &dxdt , double dt )

在你的情况,MotionGenerator没有这个操作,但可以绑定方法motionScheme

#include <functional>
namespace pl = std::placeholders;

// ...

// not tested
void MotionGeneration::execute()
{
    state_type init_conf = { 0, -1.5708, 0, 0, 0, -1.5708, 0, -1.5708, 0, 0.5};
    boost::numeric::odeint::integrate(
        std::bind(&MotionGenerator::motionScheme, *this , pl::_1 , pl::_2 , pl::_3 ) , init_conf, 0.0, 1.0, 0.05, plot);
}

```

但是,这将是很容易的方法重新命名motionSchemeoperator()并简单地传递*this整合。

编辑:您还可以使用std::ref ,以避免您的MotionGenerator的实例的副本:

void MotionGeneration::execute()
{
    state_type init_conf = { 0, -1.5708, 0, 0, 0, -1.5708, 0, -1.5708, 0, 0.5};
    boost::numeric::odeint::integrate(
        std::bind(&MotionGenerator::motionScheme, std::ref(*this) , pl::_1 , pl::_2 , pl::_3 ) , init_conf, 0.0, 1.0, 0.05, plot);
}


文章来源: Using boost::numeric::odeint inside the class
标签: c++ oop boost ode