将dopri5与odeint boost库一起使用

Using dopri5 with odeint boost library

本文关键字:一起 boost dopri5 odeint      更新时间:2023-10-16

方程组

嗨。我想在时间上把这些方程从0发展到10^16,初始条件x(0)=10^8和y(0)=0.5。由于方程对分母中x的依赖性,我认为使用带runge_kutta_dopri5的odeint是一个很好的选择,因为它具有自适应阶跃控制。问题是,我不知道如何在实践中做到这一点,因为我在c++和odeint方面几乎没有经验。我搜索了很多关于使用odeint的信息,但这些例子对我没有帮助。此外,我想在x达到零时停止计算,我看到了这个https://stackoverflow.com/questions/33334073/stop-integration-in-odeint-with-stiff-ode

根据例子,我写这篇文章到目前为止没有运气

#include <iostream>
#include <vector>
#include <cmath>
#include <boost/array.hpp>
#include <boost/numeric/odeint.hpp>
using namespace std;
using namespace boost::numeric::odeint;
const double b = 43.0e17;
typedef boost::array< double , 2 > state_type;
void binary(const state_type &x , state_type &dxdt , double t )
{
 dxdt[0] = -b*(64.0/5)*(1 + (73.0/24)*pow(x[1],2) 
   +  37.0/96)*pow(x[1],4) )/pow(x[0],3)*pow(1-pow(x[1],2),7.0/2);
 dxdt[1] = -b*(304.0/96)*x[1]*(1 + (121.0/304)*pow(x[1],2))
 /pow(x[0],4)*pow((1 - pow(x[1],2)),5.0/2);
 }
 void write_binary( const state_type &x , const double t )
{
cout << t << 't' << x[0] << 't' << x[1] << 't' << x[2] << endl;
}
 //I dont know what this does but the examples used it
struct streaming_observer
{
 std::ostream& m_out;
streaming_observer( std::ostream &out ) : m_out( out ) { }
template< class State , class Time >
void operator()( const State &x , Time t ) const
{
    m_out << t;
    for( size_t i=0 ; i<x.size() ; ++i ) m_out << "t" << x[i] ;
    m_out << "n";
 }
};
 //This was a first try with a given stepper but i want to replace it
 int main(int argc, char **argv)
  {
state_type x = { 20.871e8 , 0.5  }; // initial conditions
integrate( binary , x , 0.0 , 1000.0 , 0.1 , write_binary );
}

当我编译它运行时,我得到了这个错误

内部程序错误-断言(i<N)在常量T&boost::array::operator[]/usr/include/boost/array.hpp(129):超出范围中止(堆芯转储)

我怎样才能得到这份工作?

write_binary函数写入数组边界并导致断言。x[2]无效。