Flow123d  release_3.0.0-903-ge56fc7d
msh_basereader.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file msh_basereader.hh
15  * @brief
16  * @author dalibor
17  */
18 
19 #ifndef MSH_BASE_READER_HH
20 #define MSH_BASE_READER_HH
21 
22 
23 #include <boost/exception/info.hpp> // for error_info::~error_info<Tag, T>
24 #include <map> // for map, map<>::value_compare
25 #include <memory> // for shared_ptr
26 #include <string> // for string
27 #include <vector> // for vector
28 #include "input/accessors.hh" // for Record
29 #include "input/input_exception.hh" // for DECLARE_INPUT_EXCEPTION, Exception
30 #include "io/element_data_cache.hh" // for ElementDataCache
31 #include "io/output_time.hh"
32 #include "mesh/partitioning.hh"
33 #include "mesh/long_idx.hh"
34 #include "system/exceptions.hh" // for ExcStream, operator<<, EI, TYPED...
35 #include "system/file_path.hh" // for FilePath
36 #include "system/tokenizer.hh" // for Tokenizer, Tokenizer::Position
37 
39 class Mesh;
40 class ReaderCache;
41 
42 
43 
44 /// Types of VTK data (value 'undefined' for empty value)
45 enum DataType {
47 };
48 
49 
50 
51 /**
52  * Abstract parent of mesh readers.
53  *
54  * Supported are:
55  * - GMSH reader (class GmshMeshReader)
56  * - VTK reader (class VtkMeshReader)
57  */
59 public:
60  TYPEDEF_ERR_INFO(EI_FieldName, std::string);
61  TYPEDEF_ERR_INFO(EI_Time, double);
62  TYPEDEF_ERR_INFO(EI_MeshFile, std::string);
63  TYPEDEF_ERR_INFO(EI_Type, std::string);
64  TYPEDEF_ERR_INFO(EI_TokenizerMsg, std::string);
65  TYPEDEF_ERR_INFO(EI_FileExtension, std::string);
66  DECLARE_INPUT_EXCEPTION(ExcFieldNameNotFound,
67  << "No data for field: "<< EI_FieldName::qval
68  << " and time: "<< EI_Time::val
69  << " in the input file: "<< EI_MeshFile::qval);
70  DECLARE_INPUT_EXCEPTION(ExcMissingFieldDiscretization,
71  << "Missing data type specification for field: "<< EI_FieldName::qval
72  << " and time: "<< EI_Time::val
73  << " in the input file: "<< EI_MeshFile::qval
74  << "\nPlease, add value of key 'input_discretization'.");
75  DECLARE_EXCEPTION(ExcWrongFormat,
76  << "Wrong format of " << EI_Type::val << ", " << EI_TokenizerMsg::val << "\n"
77  << "in the input file: " << EI_MeshFile::qval);
78  DECLARE_EXCEPTION(ExcWrongExtension,
79  << "Unsupported extension " << EI_FileExtension::qval << " of the input file: " << EI_MeshFile::qval);
80 
81 
82  /***********************************
83  * Structure to store the information from a header of \\$ElementData (GMSH file) or DataArray (VTK file) section.
84  *
85  * Format of GMSH ASCII data sections
86  *
87  number-of-string-tags (== 2)
88  field_name
89  interpolation_scheme_name
90  number-of-real-tags (==1)
91  time_of_dataset
92  number-of-integer-tags
93  time_step_index (starting from zero)
94  number_of_field_components (1, 3, or 9 - i.e. 3d scalar, vector or tensor data)
95  number_of entities (nodes or elements)
96  partition_index (0 == no partition, not clear if GMSH support reading different partition from different files)
97  elm-number value ...
98  *
99  */
100  struct MeshDataHeader {
101  /// Set field_name value to empty string, that signs invalid header (using after reading data)
102  void reset() { field_name = ""; }
103  /// Name of field
104  std::string field_name;
105  /// Currently d'ont used
106  std::string interpolation_scheme;
107  /// Time of field data (used only for GMSH reader)
108  double time;
109  /// Currently d'ont used
110  unsigned int time_index;
111  /// Number of values on one row
112  unsigned int n_components;
113  /// Number of rows
114  unsigned int n_entities;
115  /// Currently d'ont used
116  unsigned int partition_index;
117  /// Position of data in mesh file
118  Tokenizer::Position position;
119  /// Type of data (used only for VTK reader)
121  /// Flag marks input discretization of data of VTK file
123  /// Hash of DOF handler object (only for native data of VTK file)
124  std::size_t dof_handler_hash;
125  };
126 
127  /**
128  * Store base data that allows search mesh data header.
129  *
130  * Data members discretization and dof_handler_hash are specific for VTK files.
131  */
132  struct HeaderQuery {
133  /// Constructor
134  HeaderQuery(std::string name, double t, OutputTime::DiscreteSpace disc, std::size_t hash=0)
135  : field_name(name), time(t), discretization(disc), dof_handler_hash(hash) {};
136 
137  std::string field_name; ///< Name of field
138  double time; ///< Time of field data (used only for GMSH and PVD reader)
139  OutputTime::DiscreteSpace discretization; ///< Flag determinate type of discrete of Field (typically is used for native data of VTK)
140  std::size_t dof_handler_hash; ///< Hash of DOF handler object
141  };
142 
143  /// Constructor
144  BaseMeshReader(const FilePath &file_name);
145 
146  /**
147  * This static method gets file path object of reader,
148  * dispatch to correct constructor and initialize appropriate function object from the input.
149  * Returns shared pointer to BaseMeshReader.
150  */
151  static std::shared_ptr< BaseMeshReader > reader_factory(const FilePath &file_name);
152 
153  /**
154  * This static method gets accessor to record with function input,
155  * dispatch to correct constructor and initialize appropriate function object from the input.
156  * Returns pointer to Mesh.
157  */
158  static Mesh * mesh_factory(const Input::Record &input_mesh_rec);
159 
160  /**
161  * Reads @p raw data of mesh (only nodes and elements) from the GMSH or VTKfile.
162  * Input of the mesh allows changing regions within the input file.
163  *
164  */
165  void read_raw_mesh(Mesh * mesh);
166 
167  /**
168  * Read regions from the mesh file and save the physical sections as regions in the RegionDB.
169  */
170  virtual void read_physical_names(Mesh * mesh)=0;
171 
172  /**
173  * Reads ElementData sections of opened mesh file. Method must be call after \p set_data_header method. If such section
174  * has not been yet read, we read the data section into raw buffer @p data. The buffer must have size at least
175  * @p n_components * @p n_entities. Indexes in the map must be smaller then @p n_entities.
176  *
177  * Possible optimizations:
178  * If the map ID lookup seem slow, we may assume that IDs are in increasing order, use simple array of IDs instead of map
179  * and just check that they comes in in correct order.
180  *
181  * @param n_entities count of entities (elements)
182  * @param n_components count of components (size of returned data is given by n_entities*n_components)
183  * @param boundary_domain flag determines that data is read for boundary or bulk elements
184  * @param component_idx component index of MultiField
185  */
186  template<typename T>
188  bool boundary_domain, unsigned int component_idx);
189 
190  /**
191  * Check if nodes and elements of reader mesh is compatible with \p mesh.
192  *
193  * OBSOLETE method - will be replace with Mesh::check_compatible_mesh after merge fields!
194  */
195  virtual void check_compatible_mesh(Mesh &mesh)=0;
196 
197  /**
198  * Returns vector of boundary or bulk element ids by parameter boundary_domain
199  */
200  std::vector<int> const & get_element_vector(bool boundary_domain);
201 
202  /**
203  * Find data header for time and field given by header_query.
204  */
205  virtual MeshDataHeader & find_header(HeaderQuery &header_query)=0;
206 
207  /**
208  * Scale and check data stored in ElementDataCache of given field.
209  */
210  CheckResult scale_and_check_limits(string field_name, double coef, double default_val,
211  double lower_bound = -std::numeric_limits<double>::max(), double upper_bound = std::numeric_limits<double>::max());
212 
213 
214 protected:
215  typedef std::shared_ptr<ElementDataCacheBase> ElementDataPtr;
217 
218  /// Constructor
219  BaseMeshReader(const FilePath &file_name, std::shared_ptr<ElementDataFieldMap> element_data_values);
220 
221  /**
222  * private method for reading of nodes
223  */
224  virtual void read_nodes(Mesh * mesh)=0;
225 
226  /**
227  * Method for reading of elements.
228  */
229  virtual void read_elements(Mesh * mesh)=0;
230 
231  /**
232  * Reads table of data headers specific for each descendants.
233  */
234  virtual void make_header_table()=0;
235 
236  /**
237  * Read element data to data cache
238  */
239  virtual void read_element_data(ElementDataCacheBase &data_cache, MeshDataHeader actual_header, unsigned int n_components,
240  bool boundary_domain)=0;
241 
242  /**
243  * Flag stores that check of compatible mesh was performed.
244  *
245  * This flag has effect only for VTK reader.
246  */
248 
249  /// Store name of field data section specify for type of mesh file.
250  std::string data_section_name_;
251 
252  /// Cache with last read element data
253  std::shared_ptr<ElementDataFieldMap> element_data_values_;
254 
255  /// Tokenizer used for reading ASCII file format.
256  Tokenizer tok_;
257 
258  /// Vector of both bulk and boundary IDs. Bulk elements come first, then boundary elements, but only the portion that appears
259  /// in input mesh file and has ID assigned.
261 
262  /// Header of actual loaded data.
264 
265  friend class ReaderCache;
266 };
267 
268 #endif /* MSH_BASE_READER_HH */
HeaderQuery(std::string name, double t, OutputTime::DiscreteSpace disc, std::size_t hash=0)
Constructor.
std::shared_ptr< std::vector< T > > ComponentDataPtr
DataType type
Type of data (used only for VTK reader)
std::string interpolation_scheme
Currently d&#39;ont used.
std::map< string, ElementDataPtr > ElementDataFieldMap
TYPEDEF_ERR_INFO(EI_FieldName, std::string)
std::shared_ptr< ElementDataFieldMap > element_data_values_
Cache with last read element data.
BaseMeshReader(const FilePath &file_name)
Constructor.
std::shared_ptr< ElementDataCacheBase > ElementDataPtr
std::string field_name
Name of field.
Definition: mesh.h:80
std::string data_section_name_
Store name of field data section specify for type of mesh file.
unsigned int partition_index
Currently d&#39;ont used.
virtual MeshDataHeader & find_header(HeaderQuery &header_query)=0
unsigned int time_index
Currently d&#39;ont used.
std::size_t dof_handler_hash
Hash of DOF handler object.
CheckResult scale_and_check_limits(string field_name, double coef, double default_val, double lower_bound=-std::numeric_limits< double >::max(), double upper_bound=std::numeric_limits< double >::max())
unsigned int n_entities
Number of rows.
virtual void read_nodes(Mesh *mesh)=0
static Mesh * mesh_factory(const Input::Record &input_mesh_rec)
Accessor to the data with type Type::Record.
Definition: accessors.hh:292
void read_raw_mesh(Mesh *mesh)
virtual void read_elements(Mesh *mesh)=0
OutputTime::DiscreteSpace discretization
Flag marks input discretization of data of VTK file.
virtual void read_element_data(ElementDataCacheBase &data_cache, MeshDataHeader actual_header, unsigned int n_components, bool boundary_domain)=0
CheckResult
Return type of method that checked data stored in ElementDataCache (NaN values, limits) ...
void reset()
Set field_name value to empty string, that signs invalid header (using after reading data) ...
std::string field_name
Name of field.
virtual void check_compatible_mesh(Mesh &mesh)=0
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
virtual void read_physical_names(Mesh *mesh)=0
Tokenizer::Position position
Position of data in mesh file.
OutputTime::DiscreteSpace discretization
Flag determinate type of discrete of Field (typically is used for native data of VTK) ...
double time
Time of field data (used only for GMSH reader)
unsigned int n_components
Number of values on one row.
double time
Time of field data (used only for GMSH and PVD reader)
MeshDataHeader actual_header_
Header of actual loaded data.
DECLARE_INPUT_EXCEPTION(ExcFieldNameNotFound,<< "No data for field: "<< EI_FieldName::qval<< " and time: "<< EI_Time::val<< " in the input file: "<< EI_MeshFile::qval)
DECLARE_EXCEPTION(ExcWrongFormat,<< "Wrong format of "<< EI_Type::val<< ", "<< EI_TokenizerMsg::val<< "\n"<< "in the input file: "<< EI_MeshFile::qval)
ElementDataCache< T >::ComponentDataPtr get_element_data(unsigned int n_entities, unsigned int n_components, bool boundary_domain, unsigned int component_idx)
DataType
Types of VTK data (value &#39;undefined&#39; for empty value)
static std::shared_ptr< BaseMeshReader > reader_factory(const FilePath &file_name)
Tokenizer tok_
Tokenizer used for reading ASCII file format.
virtual void make_header_table()=0
std::size_t dof_handler_hash
Hash of DOF handler object (only for native data of VTK file)
std::vector< int > const & get_element_vector(bool boundary_domain)
vector< LongIdx > boundary_elements_id_
vector< LongIdx > bulk_elements_id_