root/old_code/2015linfty/iodof.h

/* [<][>][^][v][top][bottom][index][help] */
   1 /*! \file iodof.h
   2 
   3     \brief Input and output for finite element function
   4 
   5      Revision history:
   6 
   7           Revised by Christian Power dd.mm.yyyy
   8           Originally written by Christian Power
   9                (power22c@gmail.com) 17. Dezember 2015
  10 
  11      The class defined in this header gives an interface for input and/or output of 
  12      nodal values from a dune finite elemente function.
  13 
  14      Created by Christian Power on 17.12.2015
  15      Copyright (c) 2015 Christian Power.  All rights reserved.
  16  */
  17 
  18 #ifndef IODOF_H
  19 #define IODOF_H 
  20 
  21 #include <string>
  22 #include <fstream>
  23 
  24 enum class IO_direction {write, read};
  25 
  26 class IO_dune_fem{
  27 public:
  28   IO_dune_fem(const std::string& filename_series_prefix,
  29               const IO_direction write_or_read,
  30               const size_t starting_no = 0);
  31   template<typename DiscreteFunction>
  32   void operator()(const DiscreteFunction& fe);  
  33 private:
  34   std::string filename;
  35   IO_direction io_dir;
  36   size_t f_no;
  37 };
  38 
  39 //======================================================================
  40 // All implementation details
  41 
  42 //----------------------------------------------------------------------
  43 // Implementation details for IO_dune_fem
  44 IO_dune_fem::IO_dune_fem(const std::string& filename_series_prefix,
  45                          const std::string& input_dir,
  46                          const std::string& output_dir,
  47                          const size_t starting_no)
  48   : filename {filename_series_prefix}, io_dir {write_or_read}, f_no {starting_no}
  49     {
  50       switch(io_dir){
  51       case IO_direction::write: case IO_direction::read:
  52         break;
  53       default:
  54         throw std::runtime_error 
  55         {"Error in the constructor of IO_dune_fem.  "
  56             "Encountered unknown IO_direction type."};
  57       }
  58     }
  59 
  60 template<typename DiscreteFunction>
  61 void IO_dune_fem::operator()(const DiscreteFunction& fe){
  62   auto err_handling = [](const std::string& msg){
  63     std::string full_msg {"Error in IO_dune_fem::operator().\n"};
  64     full_msg += msg;
  65     throw std::runtime_error {full_msg};
  66   };
  67   
  68   const std::string curr_file {filename + std::to_string(f_no)};
  69   
  70   switch(io_dir){
  71   case IO_direction::write:
  72     std::ofstream ofs {curr_file};
  73     if(!ofs) err_handling("Could not open file: " + curr_file);
  74     for(auto it = fe.dbegin(); it != fe.dend(); ++it)
  75       ofs << *it << std::endl;
  76     break;
  77   case IO_direction::read:
  78     std::ifstream ifs {curr_file};
  79     if(!ifs) err_handling("Could not open file: " + curr_file);
  80     for(auto it = fe.dbegein(); it != fe.dend(); ++it)
  81       ifs >> *it;
  82     break;
  83   default:
  84     err_handling("Impossible error in the switch statement.");
  85     break;
  86   }
  87 }
  88 
  89 #endif // IODOF_H
  90 
  91 /*! Log:
  92  */

/* [<][>][^][v][top][bottom][index][help] */