我在使用受控错误步进器和复杂状态类型的odeint库时遇到问题。我修改了带有复杂stuart landau方程的示例中的代码,使其包含自适应积分器。现在的代码如下所示:
#include <iostream>
#include <complex>
#include <boost/array.hpp>
#include <boost/numeric/odeint.hpp>
using namespace std;
using namespace boost::numeric::odeint;
//[ stuart_landau_system_function
typedef complex< double > state_type;
struct stuart_landau
{
double m_eta;
double m_alpha;
stuart_landau( double eta = 1.0 , double alpha = 1.0 )
: m_eta( eta ) , m_alpha( alpha ) { }
void operator()( const state_type &x , state_type &dxdt , double t ) const
{
const complex< double > I( 0.0 , 1.0 );
dxdt = ( 1.0 + m_eta * I ) * x - ( 1.0 + m_alpha * I ) * norm( x ) * x;
}
};
//]
struct streaming_observer
{
std::ostream& m_out;
streaming_observer( std::ostream &out ) : m_out( out ) { }
template< class State >
void operator()( const State &x , double t ) const
{
m_out.precision(10);
m_out << t;
m_out << "\t" << x.real() << "\t" << x.imag() ;
m_out << "\n";
}
};
int main( int argc , char **argv )
{
//[ stuart_landau_integration
state_type x = complex< double >( 1.0 , 0.0 );
bulirsch_stoer< state_type > stepper( 1E-12 , 1E-12 , 1 , 1 );
const double dt = 0.1;
//]
integrate_adaptive( stepper , stuart_landau( 2.0 , 1.0 ) , x , 0.0 , 10.0 , dt , streaming_observer( cout ) );
return 0;
}
但是,如果我将步进器的定义更改为
bulirsch_stoer< state_type, complex< double > > stepper( 1E-12 , 1E-12 , 1 , 1 );
编译失败。
我的问题是:受控错误步进器是否不支持复杂数据类型?如果是这样,那么有解决该问题的方法。或者,是否可以为复杂的数据类型定义自己的 vector 代数?
最佳答案
odeint库确实支持复杂的数据类型,包括受控步进器。
例如,要获得4/5阶Dormand-Prince方法的受控步进器,可以执行以下操作:
runge_kutta_dopri5< state_type > stepper;
auto c_stepper = make_controlled(1.E-12, 1.E-12, stepper);
integrate_adaptive(c_stepper, stuart_landau( 2.0 , 1.0 ) , x , 0.0 , 10.0 , dt , streaming_observer( cout ) );
其中
state_type
类型定义为typedef complex< double > state_type;
但是,在odeint中实现的Bulirsch-Stoer algorithm本质上已经是受控的步进器。因此,在这种情况下,无法应用
make_controlled
或其他策略来创建受控步进器。 odeint库中没有这样的模板,因为这没有意义。您发布的第一个代码已经集成了ODE,复杂的值和受控的步进器。目前尚不清楚您要通过将步进器更改为
bulirsch_stoer< complex<double>, complex<double> > stepper( 1E-12 , 1E-12 , 1 , 1 );