c++ - 将 dopri5 与 odeint boost 库一起使用

标签 c++ odeint

system of equations

嗨。我想及时将这些方程从零演化为 10^16 和初始条件 x(0)=10^8 和 y(0)=0.5。由于方程对分母中 x 的依赖性,我认为将 odeint 与 runge_kutta_dopri5 结合使用是一个不错的选择,因为自适应步长控制。问题是我不知道如何在实践中做到这一点,因为我在 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) 在 const T& boost::array::operator[](boost::array::size_type) const [with T = double; 中失败;长无符号整数 N = 2ul; boost::array::const_reference = const double&; boost::array::size_type = long unsigned int]: /usr/include/boost/array.hpp(129): 超出范围 中止(核心转储)

我怎样才能完成这项工作?

最佳答案

write_binary 函数覆盖数组边界并引发断言。 x[2] 无效。

关于c++ - 将 dopri5 与 odeint boost 库一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37287542/

相关文章:

C++:如何从 WLAN_BSS_ENTRY 打印 dot11Bssid?

c++ - 返回具有动态内存的对象

c++ - 将 int** 转换为 "pointer to a two-dimensional array of integers with fixed number of elements per column"

python - Scipy优化错误

c++ - 在 C++ 中及时求解 ODE 系统

c++ - 为什么新版本的 odeint 会失败?

c++ - 如何使用 odeint 的标签系统为各种步进器类型做特定的工作

python - 如何使用 solve_ivp 通过精确点?

C++:如何获取在同一类的不同函数中定义的元素?

c++ - std::set比较器