c++ - 传递参数以在 C++ 中 boost odeint

标签 c++ boost parameters odeint integrator

This答案很有帮助,但我想知道如何将不同类型的多个参数传递给 ODE 模型,也许是在一个结构中。对于我的直接用例,我需要能够通过一个 std::array<double, 6> , 两个 std::vector<std::vector<double>>和两个两个double总共要传递四个参数的标量。在链接示例以及 harmonic_oscillator.cpp 中, 只有一个 double传递的参数。谢谢。

这是我需要传递给 ODE 力模型并在速率方程中使用的结构示例。

struct T
{
    std::array<double, 6> IC;
    double S;
    double M;
    std::vector<std::vector<double>> C;
    std::vector<std::vector<double>> WT;
};

最佳答案

我相信我已经想出了一个可行的结构解决方案,但不确定它是否有任何变量/内存范围禁忌。这是一个例子:

#include <vector>
#include <boost/numeric/odeint.hpp>

// define structure
struct T
{
    std::array<double, 6> IC;
    double                S;
};

// force model
class harm_osc
{
    struct T T1;

public:
    harm_osc(struct T G) : T1(G) {}

    void operator() ( const std::vector< double > &x , std::vector< double > &dxdt , const double /* t */ )
    {
        dxdt[0] = x[1];
        dxdt[1] = -x[0] - T1.IC[0]*x[1] + T1.S;
    }
};

// print integrated state solution
void write_solution( const std::vector< double > &x , const double t )
{
    printf("%-6.2f %-6.2f %-6.2f\n", t, x[0], x[1]);
}

// problem setup
int main()
{

    std::vector< double > x(2);
    x[0] = 1.0;
    x[1] = 0.0;

    struct T T2;

    T2.IC = {0.15, 0.15, 0.15, 0.15, 0.15, 0.15};
    T2.S  = 0.0;

    harm_osc ho(T2);
    boost::numeric::odeint::integrate(ho, x, 0.0, 10.0, 0.1, write_solution);

}

关于c++ - 传递参数以在 C++ 中 boost odeint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40773530/

相关文章:

parameters - 团队城市 : project parameters inheritance issue?

C++ 等价于整数的#define

c++ - 将 C++ 类实例暴露给 python 嵌入式解释器

javascript - 如何将函数名称作为参数传递,然后再引用该函数?

c++ - Boost MSM编译 boost

c++ - std::vector 在 move 元素时做额外的操作

c++ - 模板模板参数推演指南

c++ - 确保动态分配的矩阵是正方形的方法?

c++ - glibc 版本早于 gcc 版本和 -Wl,-rpath 不起作用

c++ - boost::asio 读取处理程序类型要求未满足