ode-toolbox

ODE integration tools
git clone https://git.0xfab.ch/ode-toolbox.git
Log | Files | Refs | README | LICENSE

GnuplotDump.h (3038B)


      1 /* File:   GnuplotDump.h */
      2 /* Date:   Tue Mar  1 21:07:49 2016 */
      3 /* Author: Fabian Wermelinger */
      4 /* Tag:    Gnuplot Dumper */
      5 /* Copyright 2016 ETH Zurich. All Rights Reserved. */
      6 #ifndef GNUPLOTDUMP_H_2VNNTIUB
      7 #define GNUPLOTDUMP_H_2VNNTIUB
      8 
      9 #include <cstdlib>
     10 #include <fstream>
     11 #include <iostream>
     12 #include <string>
     13 
     14 #ifdef _FLOAT_PRECISION_
     15 using Real = float;
     16 #else
     17 using Real = double;
     18 #endif
     19 
     20 #ifdef _GNUPLOT_DOUBLE_
     21 using GPfloat = double;
     22 #else
     23 using GPfloat = float;
     24 #endif
     25 
     26 class GnuplotDump
     27 {
     28 private:
     29     const int m_ftype;
     30     std::string m_fname;
     31     std::ofstream m_file;
     32     size_t m_N;
     33 
     34     void _writeASCII(const size_t step, const Real t, const Real dt, const Real * const src, const size_t N)
     35     {
     36         if (m_file.good())
     37         {
     38             m_file << step << '\t' << t << '\t' << dt;
     39             for (size_t i = 0; i < N; ++i)
     40                 m_file << '\t' << src[i];
     41             m_file << std::endl;
     42             m_N = N+3;
     43         }
     44     }
     45 
     46     void _writeBIN(const size_t step, const GPfloat t, const GPfloat dt, const Real * const src, size_t N)
     47     {
     48         if (1024 < N)
     49         {
     50             std::cerr << "GnuplotDump: WARNING -- Max. Column width for BIN dump is 1024" << std::endl;
     51             N = 1024;
     52         }
     53         GPfloat buf[1024+3];
     54 
     55         buf[0] = static_cast<GPfloat>(step);
     56         buf[1] = t;
     57         buf[2] = dt;
     58         for (size_t i = 0; i < N; ++i)
     59             buf[i+3] = static_cast<GPfloat>(src[i]);
     60 
     61         if (m_file.good())
     62         {
     63             m_file.write((const char*)(&buf[0]), (N+3)*sizeof(GPfloat));
     64             m_N = N+3;
     65         }
     66     }
     67 
     68     void _writeGPscript() const
     69     {
     70         std::ofstream out(m_fname+".gp");
     71         if (m_ftype == ASCII)
     72             out << "plot '" << m_fname+".dat" << "' using 1:2 with lines" << std::endl;
     73         else
     74         {
     75             out << "plot '" << m_fname + ".bin"
     76                 << "' binary format='%" << m_N;
     77             if (sizeof(GPfloat) == 4)
     78                 out << "float";
     79             else
     80                 out << "double";
     81             out << "' using 1:2 with lines" << std::endl;
     82         }
     83         out.close();
     84     }
     85 
     86 public:
     87     GnuplotDump(const std::string fname="gnuplot", const int type=BIN, const size_t pos=0) : m_ftype(type), m_fname(fname)
     88     {
     89         if (m_ftype == ASCII)
     90         {
     91             m_file.open(m_fname+".dat", std::ios_base::out|std::ios_base::app);
     92             m_file.setf(std::ios::scientific, std::ios::floatfield);
     93         }
     94         else
     95             m_file.open(m_fname+".bin", std::ios_base::out|std::ios_base::binary|std::ios_base::app);
     96 
     97         if (pos > 0) m_file.seekp(pos);
     98     }
     99     ~GnuplotDump()
    100     {
    101         m_file.close();
    102         _writeGPscript();
    103     }
    104 
    105     enum {ASCII, BIN};
    106 
    107     void write(const size_t step, const Real t, const Real dt, const Real * const src, const size_t N)
    108     {
    109         if (m_ftype == ASCII)
    110             _writeASCII(step, t, dt, src, N);
    111         else
    112             _writeBIN(step, t, dt, src, N);
    113     }
    114 };
    115 
    116 #endif /* GNUPLOTDUMP_H_2VNNTIUB */