root/include/io_dgf.h

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

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. find
  2. close_list
  3. to_dgfFile
  4. to
  5. from
  6. to_vertices
  7. to_vertices

   1 /*! \file io_dgf.h
   2     \author Christian Power
   3     \date 22. March 2016
   4 
   5     \brief Provides class Io::Dgf::Handler to communicate
   6            between dgf files and finite element functions
   7 
   8     Revision history
   9     --------------------------------------------------
  10 
  11           Revised by Christian Power March 2016
  12           Originally written by Christian Power
  13                (power22c@gmail.com) March 2016
  14 
  15      Idea
  16      --------------------------------------------------
  17 
  18      Providing routines to input resp. output nodal values of a scalar valued
  19      finite element function to files according to the dune grid format.
  20 
  21          Created by Christian Power on 17.03.2016
  22          Copyright (c) 2016 Christian Power.  All rights reserved.
  23 */
  24 
  25 #ifndef IO_DGF_H
  26 #define IO_DGF_H 
  27 
  28 #include <sstream>
  29 #include <iterator>
  30 #include <iomanip>
  31 #include "esfem_fwd.h"
  32 
  33 #include <iostream>
  34 
  35 namespace Esfem{
  36   namespace Io{
  37     namespace Dgf{
  38       class Handler{
  39       public:
  40         Handler(const std::string& dgf_filename);
  41         ~Handler();
  42       
  43         void write(const std::string& out_filename, const Grid::Scal_FEfun&) const;
  44         void write(const std::string& out_filename, const Grid::Vec_FEfun&) const;
  45 
  46         void read(const std::string& in_filename, Grid::Vec_FEfun&) const;
  47         void read(const std::string& in_filename, Grid::Scal_FEfun&) const;
  48         std::size_t no_of_nodes() const noexcept;
  49         
  50         using Vertex = std::vector<double>;
  51         using Vertices = std::vector<Vertex>;
  52         using Simplex = std::vector<short>;
  53         using Simplices = std::vector<Simplex>;
  54         using Lines = std::vector<std::string>;
  55       private:
  56         struct Data;
  57         std::unique_ptr<Data> d_ptr;
  58       };
  59 
  60       // ----------------------------------------------------------------------
  61       // Constructor helper functions
  62 
  63       Handler::Simplices create_simplices(const std::string& filename);
  64       Handler::Lines create_stringSimplices(const std::string& filename);
  65 
  66       std::size_t get_precision(const std::string& filename);
  67       std::size_t calculate_dof(const std::string& dgf_file);
  68       
  69       Handler::Vertices create_vertices(const std::string& filename);
  70       Handler::Lines create_stringVertices(const std::string& filename);
  71       
  72       // ----------------------------------------------------------------------
  73       // Helper functions for dgf files
  74 
  75       // ------------------------------------------------------------
  76       // Navigation 
  77 
  78       template<typename It>
  79       It find(It first, It last, const std::string& keyword);
  80       template<typename It>
  81       It close_list(It first, It last, const std::string& keyword);
  82 
  83       // ------------------------------------------------------------
  84       // Read and write
  85 
  86       template<typename FEfun>
  87       FEfun& operator<<(FEfun&, const Handler::Vertices&);
  88       
  89       template<typename CC>     // We assume that CC::value_type is a container.
  90       void to_dgfFile(const CC&, std::ostream&, const std::size_t precision);
  91       
  92       template<typename C>
  93       C to(const std::string&);
  94       template<typename C>
  95       std::string from(const C&, const std::size_t precision);
  96 
  97       template<typename C>
  98       Handler::Vertices to_vertices(const C&, const int dim = 3);
  99       template<typename It>
 100       Handler::Vertices
 101       to_vertices(It first, It last, const int dim, const std::size_t size);
 102 
 103       // ----------------------------------------------------------------------
 104       // template implementation
 105 
 106       template<typename It>
 107       It find(It first, It last, const std::string& keyword){
 108         It rv = std::find(first, last, keyword);
 109         if(rv == last) throw std::runtime_error
 110           {"Could not find \"" + keyword + "\"."};
 111         ++rv;
 112         return rv;
 113       }    
 114       template<typename It>
 115       It close_list(It first, It last, const std::string& keyword){
 116         It rv = std::find(first, last, keyword);
 117         if(rv == last) throw std::runtime_error
 118           {"Could not find closing \"" + keyword + "\"."};
 119         return rv;
 120       }
 121       template<typename CC>
 122       void to_dgfFile(const CC& ccontainer, std::ostream& os,
 123                       const std::size_t precision){
 124         for(const auto& container : ccontainer)
 125           os << from(container, precision) << std::endl;
 126       }
 127       template<typename C>
 128       C to(const std::string& s){
 129         using Number = typename C::value_type;
 130         std::istringstream iss {s};
 131         C rv {};
 132         for(Number n {0}; iss >> n; ) rv.push_back(n);
 133         return rv;
 134       }
 135       template<typename C>
 136       std::string from(const C& container, const std::size_t precision){
 137         std::ostringstream oss {};
 138         oss << std::scientific;
 139         for(const auto it : container)
 140           if( !(oss << std::setprecision(precision) << it << ' ') )
 141             throw std::runtime_error {"Esfem::Io::Dgf::from()."};
 142         return oss.str();
 143       }
 144       template<typename C>
 145       Handler::Vertices to_vertices(const C& container, const int dim){
 146         using std::begin;
 147         using std::end;
 148         const auto size = container.size();
 149         if(size % dim) throw std::logic_error
 150           {"Bad degree of freedom dimension."};
 151         return to_vertices(begin(container), end(container), dim, size);
 152       }
 153       template<typename It>
 154       Handler::Vertices to_vertices(It first, It last,
 155                                     const int dim, const std::size_t size) try{
 156         using Vertex = Handler::Vertices::value_type;
 157         Handler::Vertices rv {};
 158         rv.reserve(size);
 159         while(first != last){
 160           Vertex v(dim);
 161           for(auto& it : v){
 162             it = *first;
 163             ++first;
 164           }
 165           rv.emplace_back(std::move(v));
 166         }
 167         return rv;
 168       }
 169       catch(const std::exception&){
 170         std::throw_with_nested(std::runtime_error {"Esfem::Io::Dgf::to_vertices()."});
 171       }
 172 
 173       template<typename FEfun>
 174       FEfun& operator<<(FEfun& fef, const Handler::Vertices& v){
 175         using std::begin;
 176         using std::end;
 177         auto o_it = begin(fef);
 178         auto first = cbegin(v);
 179         auto last = cend(v);
 180         while(first != last){
 181           const auto vertex = *first;
 182           for(const auto d : vertex){
 183             *o_it = d;
 184             ++o_it;
 185           }
 186           ++first;
 187         }
 188         if(o_it != end(fef)) throw std::logic_error
 189           {"Esfem::Io::Dgf::operator<<().  Dof to small"};
 190         return fef;
 191       }
 192     }   // namespace Dgf
 193   }     // namespace Io
 194 }       // namespace Esfem
 195 
 196 #endif // IO_DGF_H

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