【问题标题】:odeint streaming observer and related questionsodeint 流观察者和相关问题
【发布时间】:2012-08-27 21:54:10
【问题描述】:

我有一个由 4 个耦合方程组成的系统要求解,还有一个参数 Gamma[i] 要迭代。由于我对 C++ 很陌生,所以我的代码非常初级。如果它在某些部分看起来复杂而优雅,那只是因为我改编了 odeint 作者的代码。 :)

这个问题与(http://stackoverflow.com/questions/12060111/using-odeint-function-definition/12066958#comment16253600_12066958)有关,但不完全相同。请不要删除这个。 :(

代码行之间插入了问题。

#include <iostream>
#include <iterator>
#include <algorithm>
#include <boost/numeric/odeint.hpp>
#include <cmath>
#include <vector>
#include <fstream>
#include <iomanip>

using namespace std;
using namespace boost::numeric::odeint;
class NLI_class {
private:
    double gamma;
public:
 NLI_class (double r) : gamma(r) {} 

 void operator()( vector<double> &u , vector<double> &du , double z ) {
            du[0] = u[0]*u[1]*cos(u[3]); //u1
            du[1] = -u[0]*u[0]*cos(u[3]); //u2
            du[2] = gamma * (2/(u[0]*u[0]) - 1/(u[1]*u[1])); //theta
            du[3] = gamma * (1.0/(u[0]*u[0])); //phi1
            du[4] = gamma * (1.0/(u[1]*u[1])); //phi2;

}
};

问题 #1:

在我的原始程序中,我有这样的东西将输出通过管道传输到 csv 文件:

 inline void save(vector<double>& v, string filename)
  {
ofstream output(filename);
for(int i=0;i<v.size();++i){ 
    output << setprecision(64) << v[i] << endl;
}
   }

如何调整 streaming_observer 来完成我的 save() 的工作?基本上,我想为每次迭代生成 .csv 文件。在这一点上,我正在以丑陋的方式进行操作,即编译所有内容,打开 Windows 命令提示符,然后将 exe 输出通过管道传输到文本文件。这会生成一个包含所有迭代的大文件。

分析大量迭代变得非常痛苦。

struct streaming_observer {

 std::ostream &m_out;
 streaming_observer( std::ostream &out ) : m_out( out ) {}

 void operator()( const vector<double> &x , double t ) const
 {
      m_out << t;
      for( size_t i=0 ; i < x.size() ; ++i )
          m_out << "\t" << x[i];
      m_out << "\n";
 }
};




    int main(){

vector<double> x( 5 );
vector<double> Gamma;
vector<double>delta;
const double  pi=acos(-1.0); 
short delta_n=5;
const double delta_step=(2*pi)/delta_n;
const double dz = 0.01;
const double  zeta = 3.0;
const double  theta_initial=0.0;
const double  u20=tanh(zeta); 
const double  u10=sqrt(1.0-(u20*u20)); 

double d=0.0;
double G=0.0;

for(int i=0;i<=delta_n;i++){
    //When i=0, the d=0.0 and G=0.0 are pushed into the vector.
    delta.push_back(d);  
    Gamma.push_back(G);
    // Compute delta and Gamma
    d=d+delta_step; 
    G=-u10*u10*u20*sin(theta_initial+d);
}

save(delta,"delta.csv");
save(Gamma,"Gamma.csv");

问题#2: 我在这里得到的结果与我使用简单的显式欧拉方法得到的结果不一致。因此,我想查看 RK4 系数(最好将它们转储到文件中)或中间步骤。我怎样才能得到这些信息?

//Numeric Integration
    for (unsigned i = 0; i < Gamma.size(); ++i) {
        x[0] = u10;
        x[1] = u20;
        x[2] = 0.0;
        x[3] = 0.0;
        x[4] = 0.0;

        NLI_class nli_obj(Gamma[i]);
        integrate_const( runge_kutta4< vector<double > >(), nli_obj, x , 0.0 , 3.0 , dz,streaming_observer( std::cout ) );
}
    }

感谢所有帮助过的人!

编辑: 有什么方法可以得到运行错误估计?请注意,u[0]*u[0]+u[1]*u[1]=1。

【问题讨论】:

    标签: numerical-methods


    【解决方案1】:

    问题 #1:

    我不明白你需要什么样的输出。但是如果你想在每次迭代后写出结果,你可以像这样实现一个输出观察器:

    struct output_observer
    {
        string filename_;
        size_t count_;
        output_observer( const string &filename ) : filename_( filename ) , count_( 0 ) { }
        void operator()( const state_type &x , time_type dt )
        {
            char fn[512] = "";
            sprintf( fn , "%s_%04lu.csv" , filename_.c_str() , count_ );
            ofstream fout( fn );
            for( size_t i=0 ; i<x.size() ; ++i ) fout << x[i] << "\n";
            ++count_;
        }
    };
    

    你可以简单地应用这个观察者

    integrate_const( runge_kutta4< vector<double > >() , nli_obj , x ,
        0.0 , 3.0 , dz , output_observer( "filename" ) );
    

    这是想要的功能吗?

    问题 #2:

    看不到runge_kutta4的中间e步骤。这些系数是经典 Runge-Kutta 方法的标准系数:http://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods

    问题 #3:

    odeint 有几个错误步进器,它们估计在一个步骤中产生的错误。例如,您可以使用 Runge_Kutta Cash Karp 算法;

    runge_kutta_cash_karp54< state_type > rk;
    state_type xerr;
    rk.do_step( nli_obj , x , t , xerr );
    

    进行一步并估计错误并将错误结果写入xerr。

    【讨论】:

    • sprintf(fn, "%s_%04lu.csv", count)中是否应该是count_;如果是 count_,我会收到访问冲突错误。如果没有,则似乎没有定义计数。对不起,伙计,我是个菜鸟。
    • 你初始化状态类型正确吗?它是一个向量,需要手动定义大小(在构造函数期间或稍后通过 push_back() 的 resize())。
    • 是的,我做到了。编译器抱怨“无法确定函数模板 std::count 的实例”。
    • 确切的错误是 "\main.cpp(63): error C2664: 'sprintf' : cannot convert parameter 3 from 'iterator_traits<_iter>::difference_type (__cdecl *)(_InIt,_InIt,const _Ty &)' to '...' 1> 上下文不允许消除重载函数的歧义”
    • 好吧,这是我的错误。文件名字符串丢失,但现在已修复。抱歉,我没有编译流媒体示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    相关资源
    最近更新 更多