c++ - odeint (c++) - 下采样观察

标签 c++ numeric odeint

抱歉,如果这是一个简单的问题 - 但是否存在对 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++;
      }
   }
};

最佳答案

我遇到了同样的问题,并且完全按照你的方式解决了它。但是,您也可以考虑使用带步长控制的步进器,然后将 integrate_const 与 dt 一起使用,以便在所需的时间间隔调用观察器。当您使用带步长控制的步进器时(甚至更好:像 dopri5 这样的密集输出),integrate_const 会根据您的误差容限调整步长,但随后确保观察器在时间 t0 + n*dt 被调用。

关于c++ - odeint (c++) - 下采样观察,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20360972/

相关文章:

c++ - 如何检查和处理非常接近于零的数字

android - 如何清除 Android 中的数字 editText?

c++ - odeint的runge_kutta4与Matlab的ode45的比较

python - 微分方程的解在 boost::odeint 和 scipy.integrate 中完全不同

c++ - 确定一组点是在正方形内部还是外部

c++ - 为特定目录中除其子目录外的所有 C++ 文件生成标签文件

c++ - 我如何在 Gecode 中使用评价函数?

c++ - Valgrind 导致长 double 的数字问题

c++ - -std=c++11 在 g++ 命令行中的位置

c++11 - 使用自定义张量数据结构的 boost::odeint 输出错误