root/src/io_dgf.cpp

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

DEFINITIONS

This source file includes following definitions.
  1. write
  2. write
  3. read
  4. read
  5. no_of_nodes
  6. create_simplices
  7. create_stringSimplices
  8. get_precision
  9. calculate_dof
  10. create_vertices
  11. create_stringVertices

   1 /*! \file io_dgf.cpp
   2 
   3     \brief <Program Name>
   4 
   5      Revision history:
   6 
   7           Revised by Christian Power dd.mm.yyyy
   8           Originally written by Christian Power
   9                (power22c@gmail.com) March 2016
  10 
  11      Implementation details for io_dgf.h
  12      Created by Christian Power on 08.03.2016
  13      
  14      Copyright (c) 2016 Christian Power. All rights reserved.
  15  */
  16 
  17 #include <fstream>
  18 #include <sstream>
  19 #include <vector>
  20 #include "io_dgf.h"
  21 #include "grid_GridAndTime_impl.h"
  22 
  23 using namespace std;
  24 
  25 using Esfem::Io::Dgf::Handler;
  26 using Simplex = Handler::Simplex;
  27 using Simplices = Handler::Simplices;
  28 using Vertex = Handler::Vertex;
  29 using Vertices = Handler::Vertices;
  30 using Lines = Handler::Lines;
  31 using Vec_FEfun = Esfem::Grid::Vec_FEfun;
  32 using Scal_FEfun = Esfem::Grid::Scal_FEfun;
  33 
  34 static constexpr int dim = 3;
  35 
  36 // ----------------------------------------------------------------------
  37 // Handler implementation
  38 
  39 struct Handler::Data{
  40   int digit_precision {8};
  41   const size_t scal_dim_dof;
  42   const size_t vec_dim_dof;
  43   const Simplices simplices;
  44   Data(const string&);
  45 
  46   // --------------------------------------------------
  47   // Helper function
  48   // void to_dgfFile(const Vertices&, std::ostream&);
  49   // string from(const Vertex&);
  50 };
  51 
  52 // ------------------------------------------------------------
  53 // Handler::Data Implementation
  54 
  55 Handler::Data::Data(const string& fname)
  56   : digit_precision {static_cast<int>(get_precision(fname))},
  57     scal_dim_dof {calculate_dof(fname)},
  58     vec_dim_dof {dim * scal_dim_dof},
  59     simplices {create_simplices(fname)}
  60 {}
  61 // void Handler::Data::to_dgfFile(const Vertices& v, std::ostream& os){
  62 //   for(const auto& vertex : v)
  63 //     os << from(vertex, digit_precision) << endl;
  64 // }
  65 // string Handler::Data::from(const Vertex& v){
  66 //   std::ostringstream oss {};
  67 //   oss << scientific;
  68 //   for(const auto& d : v)
  69 //     if( !(oss << setprecision(digit_precision) << d << ' ') )
  70 //       throw runtime_error {"Esfem::Io::Dgf::Handler::Data::from()."};
  71 //   return oss.str();
  72 // }
  73 
  74 // ------------------------------------------------------------
  75 // Handler member functions
  76 
  77 Handler::Handler(const std::string& dgf_filename)
  78 try : d_ptr {make_unique<Data>(dgf_filename)}
  79 {}
  80  catch(const exception&){
  81    throw_with_nested
  82      (runtime_error {"Error in constructor of Esfem::Io::Handler."});
  83  }
  84  catch(...){
  85    throw runtime_error {"Unknown error in constructor of Esfem::Io::Handler."};
  86  }
  87 
  88 Handler::~Handler() = default;
  89 
  90 void Handler::write(const std::string& out_filename, const Scal_FEfun& fef) const try{
  91   if(fef.size() != no_of_nodes()) throw logic_error
  92     {"Finit element function has a bad degree of freedom dimension."};
  93   
  94   ofstream dgf_file {out_filename};
  95   if(!dgf_file) throw runtime_error {"Could not open file " + out_filename};
  96 
  97   dgf_file << "DGF" << std::endl;
  98   const auto vertices = Esfem::Io::Dgf::to_vertices(fef, 1); 
  99   dgf_file << "VERTEX" << std::endl;
 100   to_dgfFile(vertices, dgf_file, d_ptr -> digit_precision);
 101   dgf_file << "#" << '\n'
 102            << "SIMPLEX" << std::endl;
 103   Esfem::Io::Dgf::to_dgfFile(d_ptr -> simplices, dgf_file, d_ptr -> digit_precision);
 104   dgf_file << "#" << std::endl;
 105   dgf_file << "GRIDPARAMETER\n" 
 106            << "tolerance " <<  pow(10,(-1) * d_ptr -> digit_precision)
 107            << "\n#\n#" << std::endl;
 108  }
 109  catch(...){
 110    throw_with_nested (runtime_error {"Error in Handler::write()."});   
 111  }
 112 
 113 void Handler::write(const std::string& out_filename, const Vec_FEfun& vfef) const try{
 114   if(vfef.size() != d_ptr -> vec_dim_dof) throw logic_error
 115     {"Finit element function has a bad degree of freedom dimension."};
 116   
 117   ofstream dgf_file {out_filename};
 118   if(!dgf_file) throw runtime_error {"Could not open file " + out_filename};
 119 
 120   dgf_file << "DGF" << std::endl;
 121   const auto vertices = Esfem::Io::Dgf::to_vertices(vfef); 
 122   dgf_file << "VERTEX" << std::endl;
 123   to_dgfFile(vertices, dgf_file, d_ptr -> digit_precision);
 124   dgf_file << "#" << '\n'
 125            << "SIMPLEX" << std::endl;
 126   to_dgfFile(d_ptr -> simplices, dgf_file, d_ptr -> digit_precision);
 127   dgf_file << "#" << std::endl;
 128   dgf_file << "GRIDPARAMETER\n" 
 129            << "tolerance " <<  pow(10, (-1) * d_ptr -> digit_precision)
 130            << "\n#\n#" << std::endl;
 131  }
 132  catch(...){
 133    throw_with_nested (runtime_error {"Error in Handler::write()."});
 134  }
 135 
 136 void Handler::read(const string& in_filename, Vec_FEfun& vfef) const try{
 137   const auto vertices = create_vertices(in_filename);
 138 
 139   // We assume that vertices[i] always has the same size.
 140   const auto dim_of_vertices = vertices.size() * vertices.front().size();
 141   const auto dim_of_fef = vfef.size();
 142   
 143   // if(dim_of_fef != dim_of_vertices) throw logic_error
 144   //   {"Input file is incompatible with finite element function."};
 145   
 146   vfef << vertices;  
 147  }
 148  catch(const exception&){
 149    throw_with_nested
 150      (runtime_error {"Error in Handler::read()."});
 151  }
 152  catch(...){
 153    throw runtime_error {"Unknown error in Handler::write()."};
 154  }
 155 
 156 void Handler::read(const string& in_filename, Scal_FEfun& fef) const try{
 157   const auto vertices = create_vertices(in_filename);
 158 
 159   // We assume that vertices[i] always has the same size.
 160   const auto dim_of_vertices = vertices.size() * vertices.front().size();
 161   const auto dim_of_fef = fef.size();
 162 
 163   // std::clog << "dim_of_vertices: " << dim_of_vertices << '\n'
 164   //        << "dim_of_fef: " << dim_of_fef << std::endl;
 165 
 166   if(dim_of_fef != dim_of_vertices) throw logic_error
 167     {"Input file is incompatible with finite element function."};
 168   
 169   fef << vertices;  
 170  }
 171  catch(const exception&){
 172    throw_with_nested
 173      (runtime_error {"Error in Handler::read()."});
 174  }
 175  catch(...){
 176    throw runtime_error {"Unknown error in Handler::write()."};
 177  }
 178 
 179 
 180 size_t Handler::no_of_nodes() const noexcept{
 181   return d_ptr -> scal_dim_dof;
 182 }
 183 
 184 // ----------------------------------------------------------------------
 185 // Constructor helper functions
 186 
 187 Simplices Esfem::Io::Dgf::create_simplices(const std::string& filename) try{
 188   Simplices rv {};
 189   const auto simplices_list = create_stringSimplices(filename);
 190   rv.reserve(simplices_list.size());
 191   for(const auto& line : simplices_list)
 192     rv.emplace_back(to<Simplex>(line));
 193   return rv;
 194  }
 195  catch(const exception&){
 196    throw_with_nested
 197      (runtime_error {"Error in create_simplices()."});
 198  }
 199 Lines Esfem::Io::Dgf::create_stringSimplices(const string& filename) try{
 200   const auto lines = Esfem::Impl::dgfFile_to_vec(filename);
 201   auto first = Esfem::Io::Dgf::find(cbegin(lines), cend(lines), "SIMPLEX");
 202   auto last = Esfem::Io::Dgf::close_list(first, cend(lines), "#");
 203   return {first, last};
 204  }
 205  catch(const exception&){
 206    throw_with_nested
 207      (runtime_error {"Error in create_stringSimplices()."});
 208  }
 209 size_t Esfem::Io::Dgf::get_precision(const string& dgf_file){
 210   const auto vertices_list = create_stringVertices(dgf_file);
 211   return Esfem::Impl::get_relevant_precision(vertices_list);
 212 }
 213 size_t Esfem::Io::Dgf::calculate_dof(const string& dgf_file){
 214   const auto lines = Esfem::Impl::dgfFile_to_vec(dgf_file);
 215   auto first = Esfem::Io::Dgf::find(cbegin(lines), cend(lines), "VERTEX");
 216   auto last = Esfem::Io::Dgf::close_list(first, cend(lines), "#");
 217   return distance(first, last);
 218 }
 219 Vertices Esfem::Io::Dgf::create_vertices(const string& filename) try{
 220   Vertices rv {};
 221   const auto vertices_list = create_stringVertices(filename);
 222   rv.reserve(vertices_list.size());
 223   for(const auto& line : vertices_list)
 224     rv.emplace_back(to<Vertex>(line));
 225   return rv;  
 226  }
 227  catch(const exception&){
 228    throw_with_nested
 229      (runtime_error {"Error in Esfem::Io::Dgf::create_vertices()."});
 230  }
 231  catch(...){
 232    throw runtime_error {"Unknown error in Esfem::Io::Dgf::create_vertices()."};
 233  }
 234 Lines Esfem::Io::Dgf::create_stringVertices(const string& filename){
 235   const auto lines = Esfem::Impl::dgfFile_to_vec(filename);
 236   auto first = Esfem::Io::Dgf::find(cbegin(lines), cend(lines), "VERTEX");
 237   auto last = Esfem::Io::Dgf::close_list(first, cend(lines), "#");
 238   return {first, last};
 239 }
 240 
 241 /*! Log:
 242  */

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