2016-05-17 113 views
0

system of equations使用dopri5與odeint增強庫

嗨。我想在時間上從零到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; long unsigned int N = 2ul; boost :: array :: const_reference = const double &;提高::數組:: SIZE_TYPE =長的無符號整數]: /usr/include/boost/array.hpp(129):超出範圍 中止(核心轉儲)

我怎樣才能得到這個工作?

+0

你的狀態變量只有兩個x [0]和x [1]出現在'state_type x = {20.871e8,0.5};'中。 – CroCo

回答

1

write_binary函數寫入數組邊界並導致斷言。 x[2]無效。