抱歉,如果这是一个简单的问题-但是是否有“最佳实践”来对 odeint 中状态变量的演变进行下采样?
下面,我复制了一个很好的示例,用于构建“观察者”来记录本文中提供的状态变量 ( http://www.codeproject.com/Articles/268589/odeint-v2-Solving-ordinary-differential-equations )
struct streaming_observer
{
std::ostream &m_out;
streaming_observer( std::ostream &out ) : m_out( out ) {}
void operator()( const state_type &x , double t ) const
{
m_out << t;
for( size_t i=0 ; i < x.size() ; ++i )
m_out << "\t" << x[i];
m_out << "\n";
}
};
// ...
integrate_const( runge_kutta4< state_type >() , lorenz , x , 0.0 , 10.0 , dt , streaming_observer( std::cout ) );
您如何将观察者更改为仅每 10 步记录一次状态(例如)。我想知道是否有比放入 if 语句更优雅的解决方案:
struct streaming_observer
{
std::ostream &m_out;
int count;
streaming_observer( std::ostream &out ) : m_out( out ) {count = 10;}
void operator()( const state_type &x , double t ) const
{
if( count == 10 ) {
count = 1;
m_out << t;
for( size_t i=0 ; i < x.size() ; ++i )
m_out << "\t" << x[i];
m_out << "\n";
}
else {
count++;
}
}
};