c++ - boost::odeint 在成员类中调用

标签 c++ class boost odeint

这是我一直在从事的个人项目,我无法弄清楚这里发生了什么(只是学习 C++)。我找到了非常相似问题的答案,但我似乎无法执行该解决方案。这是我的代码,删除了一些不重要的位:

#include <iostream>
#include <cmath>
#include <complex>
#include <boost/array.hpp>
#include <boost/numeric/odeint.hpp>
#include <gsl/gsl_roots.h>

class Riemann
{
public:
    // constructor
    Riemann(double leftP, double rightP, double leftrho, double rightrho, \
        double leftvx, double rightvx, double leftvy, double rightvy, double gam);

    double PL,PR,rhoL,rhoR,vxL,vxR,vyL,vyR,gamma;

    // function prototypes
    double shockvelocity(double Pg, int sign);
    double rarefactionvelocity(double Pg, int sign);
    void RfODE(const boost::array<double,6> &vrhovt, \
        boost::array<double,6> &dvrhovtdp, double t);

//  ~Riemann();
};

Riemann::Riemann(double leftP, double rightP, double leftrho, double rightrho, \
        double leftvx, double rightvx, double leftvy, double rightvy, double gam){
    // constructs Riemann public variables
}

double Riemann::shockvelocity(double Pg,int sign){
    // calculate a shock velocity, not important here...
}



void Riemann::RfODE(const boost::array<double,6> &vrhovt, \
    boost::array<double,6> &dvrhovtdp, double t){
    // calculates the ODE I want to solve

}

double Riemann::rarefactionvelocity(double Pg, int sign){
    double dpsize=0.00001;
    double P,rho,vx,vy,vtest;
    //
    boost::array<double,6> vrhovt = {vx,rho,vy,double(sign),P,gamma}; // initial conditions
    boost::numeric::odeint::integrate(std::bind(&Riemann::RfODE,std::ref(*this),std::placeholders::_1, 
        std::placeholders::_2, std::placeholders::_3),vrhovt,P,Pg,dpsize);
    std::cout<<"vRarefaction="<<vrhovt[0]<<std::endl;
    return vrhovt[0];
}

double FRiemann(double Pg, void* Riemannvalues){
    Riemann* Rvals = (Riemann*)Riemannvalues;
    // calls on Riemann::rarefactionvelocity at some point
}


int main(){
    double PL= 1000.0;
    double PR= 0.01;
    double rhoL= 1.0;
    double rhoR= 1.0;
    double vxL= 0.0;
    double vxR= 0.0;
    double vyL= 0.0;
    double vyR= 0.0;
    double gam = 5.0/3.0;

    // calls FRiemann to get a root

}

发生的事情是代码正在运行,调用 Riemann::rarefactionvelocity 就好了,但是由于某种原因 RfODE 从未执行过(例如,此函数中的打印语句从未执行过)并且返回的 vrhovt[0] 的值为当然是它开始的值,vx。也没有编译器错误(使用 gcc 4.8.1 和 -std=c++11 和 -O2 标记)这很奇怪,因为我已经自己测试了稀疏特定函数(在 Riemann 类之外)并且它们工作——问题似乎是他们在这个类(class)。不过,考虑到黎曼求解器的工作原理,我有理由从这些函数中创建一个类,并且真的很想找到一种方法来完成这项工作,而无需进行大量重写和更改类结构。

非常感谢任何帮助!谢谢你! :)

最佳答案

P 可能未正确初始化。至少我没有在你的代码中看到它。 P 需要小于 PG 否则您已经落后于集成的终点。

此外,不要使用绑定(bind),而是使用 lambda。我认为绑定(bind)在 C++11/C++14 中已经过时了。绑定(bind)可能无法获得正确的引用。

double Riemann::rarefactionvelocity(double Pg, int sign)
{
    // ...

    // not tested
    using namspace boost::numeric::odeint;
    integrate( [this](auto const& x, auto &dxdt ,auto t ) {
        this->RfODE(x, dt, t); } ,vrhovt,P,Pg,dpsize);
}

关于c++ - boost::odeint 在成员类中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36817706/

相关文章:

c++ - 如果 (QString.contains()) 不能正常工作,使用 temp bool 变量更正

c++ - 十进制奇偶校验

c++ - Cpp - 检查 key 是否存在于 boost bimap 中

C++ boost 正则表达式替换为条件

Python:当旧对象与新对象相等时复制对象

c++ - 如何使用显式强制转换来抑制此警告?

java - 为什么 Java 必须抛出并发修改异常?

c++ - 如何在不使用 std::initializer_list 的情况下定义初始化列表构造函数?

c# - 将 WPF 控件类作为模板类

python - 运算符可以作为 python 中的类方法重载吗?