嗨。我想及时将这些方程从零演化为 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/