passed parameters to boost odeint in C++

有些话、适合烂在心里 提交于 2019-12-10 17:53:49

问题


This answer is helpful, but I would like to know how to pass multiple parameters of different types to the ODE model, perhaps in a struct. For my immediate use case, I need to be able to pass one std::array<double, 6>, two std::vector<std::vector<double>> and two two double scalars for a total of four parameters to be passed. In the linked example, as well as in harmonic_oscillator.cpp, there is only a single double passed parameter. Thanks.

Here's an example of the struct I would need passed to the ODE force model and used within the rate equations.

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

回答1:


I believe I've come up with a struct solution that works, but am not sure if it has any variable/memory scope no-no's. Here's an example:

#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);

}


来源:https://stackoverflow.com/questions/40773530/passed-parameters-to-boost-odeint-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!