首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将参数传递给C++中的boost odeint

将参数传递给C++中的boost odeint
EN

Stack Overflow用户
提问于 2016-11-24 04:21:56
回答 1查看 569关注 0票数 3

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

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

代码语言:javascript
复制
struct T
{
    std::array<double, 6> IC;
    double S;
    double M;
    std::vector<std::vector<double>> C;
    std::vector<std::vector<double>> WT;
};
EN

回答 1

Stack Overflow用户

发布于 2016-11-24 06:07:53

我相信我已经想出了一个有效的结构解决方案,但不确定它是否有任何变量/内存作用域的禁忌。

代码语言:javascript
复制
#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);

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40773530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档