Flow123d  release_2.2.0-914-gf1a3a4f
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 <string>
24 #include <vector>
25 #include <istream>
26 #include <memory>
27 
28 #include "io/element_data_cache.hh"
29 #include "io/output_time.hh"
30 #include "mesh/mesh.h"
31 #include "input/accessors.hh"
32 #include "system/system.hh"
33 #include "system/tokenizer.hh"
34 
35 
36 
37 /// Types of VTK data (value 'undefined' for empty value)
38 enum DataType {
40 };
41 
42 
43 
44 /**
45  * Abstract parent of mesh readers.
46  *
47  * Supported are:
48  * - GMSH reader (class GmshMeshReader)
49  * - VTK reader (class VtkMeshReader)
50  */
52 public:
53  TYPEDEF_ERR_INFO(EI_FieldName, std::string);
54  TYPEDEF_ERR_INFO(EI_Time, double);
55  TYPEDEF_ERR_INFO(EI_MeshFile, std::string);
56  TYPEDEF_ERR_INFO(EI_Type, std::string);
57  TYPEDEF_ERR_INFO(EI_TokenizerMsg, std::string);
58  TYPEDEF_ERR_INFO(EI_FileExtension, std::string);
59  DECLARE_INPUT_EXCEPTION(ExcFieldNameNotFound,
60  << "No data for field: "<< EI_FieldName::qval
61  << " and time: "<< EI_Time::val
62  << " in the input file: "<< EI_MeshFile::qval);
63  DECLARE_INPUT_EXCEPTION(ExcMissingFieldDiscretization,
64  << "Missing data type specification for field: "<< EI_FieldName::qval
65  << " and time: "<< EI_Time::val
66  << " in the input file: "<< EI_MeshFile::qval
67  << "\nPlease, add value of key 'input_discretization'.");
68  DECLARE_EXCEPTION(ExcWrongFormat,
69  << "Wrong format of " << EI_Type::val << ", " << EI_TokenizerMsg::val << "\n"
70  << "in the input file: " << EI_MeshFile::qval);
71  DECLARE_EXCEPTION(ExcWrongExtension,
72  << "Unsupported extension " << EI_FileExtension::qval << " of the input file: " << EI_MeshFile::qval);
73 
74 
75  /***********************************
76  * Structure to store the information from a header of \\$ElementData (GMSH file) or DataArray (VTK file) section.
77  *
78  * Format of GMSH ASCII data sections
79  *
80  number-of-string-tags (== 2)
81  field_name
82  interpolation_scheme_name
83  number-of-real-tags (==1)
84  time_of_dataset
85  number-of-integer-tags
86  time_step_index (starting from zero)
87  number_of_field_components (1, 3, or 9 - i.e. 3d scalar, vector or tensor data)
88  number_of entities (nodes or elements)
89  partition_index (0 == no partition, not clear if GMSH support reading different partition from different files)
90  elm-number value ...
91  *
92  */
93  struct MeshDataHeader {
94  /// Set field_name value to empty string, that signs invalid header (using after reading data)
95  void reset() { field_name = ""; }
96  /// Name of field
97  std::string field_name;
98  /// Currently d'ont used
99  std::string interpolation_scheme;
100  /// Time of field data (used only for GMSH reader)
101  double time;
102  /// Currently d'ont used
103  unsigned int time_index;
104  /// Number of values on one row
105  unsigned int n_components;
106  /// Number of rows
107  unsigned int n_entities;
108  /// Currently d'ont used
109  unsigned int partition_index;
110  /// Position of data in mesh file
111  Tokenizer::Position position;
112  /// Type of data (used only for VTK reader)
114  /// Flag marks input discretization of data of VTK file
116  /// Hash of DOF handler object (only for native data of VTK file)
117  std::size_t dof_handler_hash;
118  };
119 
120  /**
121  * Store base data that allows search mesh data header.
122  *
123  * Data members discretization and dof_handler_hash are specific for VTK files.
124  */
125  struct HeaderQuery {
126  /// Constructor
127  HeaderQuery(std::string name, double t, OutputTime::DiscreteSpace disc, std::size_t hash=0)
128  : field_name(name), time(t), discretization(disc), dof_handler_hash(hash) {};
129 
130  std::string field_name; ///< Name of field
131  double time; ///< Time of field data (used only for GMSH and PVD reader)
132  OutputTime::DiscreteSpace discretization; ///< Flag determinate type of discrete of Field (typically is used for native data of VTK)
133  std::size_t dof_handler_hash; ///< Hash of DOF handler object
134  };
135 
136  /// Constructor
137  BaseMeshReader(const FilePath &file_name);
138 
139  /**
140  * This static method gets file path object of reader,
141  * dispatch to correct constructor and initialize appropriate function object from the input.
142  * Returns shared pointer to BaseMeshReader.
143  */
144  static std::shared_ptr< BaseMeshReader > reader_factory(const FilePath &file_name);
145 
146  /**
147  * This static method gets accessor to record with function input,
148  * dispatch to correct constructor and initialize appropriate function object from the input.
149  * Returns pointer to Mesh.
150  */
151  static Mesh * mesh_factory(const Input::Record &input_mesh_rec);
152 
153  /**
154  * Reads @p raw data of mesh (only nodes and elements) from the GMSH or VTKfile.
155  * Input of the mesh allows changing regions within the input file.
156  *
157  */
158  void read_raw_mesh(Mesh * mesh);
159 
160  /**
161  * Read regions from the mesh file and save the physical sections as regions in the RegionDB.
162  */
163  virtual void read_physical_names(Mesh * mesh)=0;
164 
165  /**
166  * Reads ElementData sections of opened mesh file. Method must be call after \p set_data_header method. If such section
167  * has not been yet read, we read the data section into raw buffer @p data. The buffer must have size at least
168  * @p n_components * @p n_entities. Indexes in the map must be smaller then @p n_entities.
169  *
170  * Possible optimizations:
171  * If the map ID lookup seem slow, we may assume that IDs are in increasing order, use simple array of IDs instead of map
172  * and just check that they comes in in correct order.
173  *
174  * @param n_entities count of entities (elements)
175  * @param n_components count of components (size of returned data is given by n_entities*n_components)
176  * @param boundary_domain flag determines that data is read for boundary or bulk elements
177  * @param component_idx component index of MultiField
178  */
179  template<typename T>
181  bool boundary_domain, unsigned int component_idx);
182 
183  /**
184  * Check if nodes and elements of reader mesh is compatible with \p mesh.
185  */
186  virtual void check_compatible_mesh(Mesh &mesh)=0;
187 
188  /**
189  * Returns vector of boundary or bulk element ids by parameter boundary_domain
190  */
191  std::vector<int> const & get_element_vector(bool boundary_domain);
192 
193  /**
194  * Find data header for time and field given by header_query.
195  */
196  virtual MeshDataHeader & find_header(HeaderQuery &header_query)=0;
197 
198 protected:
199  typedef std::shared_ptr<ElementDataCacheBase> ElementDataPtr;
201 
202  /// Constructor
203  BaseMeshReader(const FilePath &file_name, std::shared_ptr<ElementDataFieldMap> element_data_values);
204 
205  /**
206  * private method for reading of nodes
207  */
208  virtual void read_nodes(Mesh * mesh)=0;
209 
210  /**
211  * Method for reading of elements.
212  */
213  virtual void read_elements(Mesh * mesh)=0;
214 
215  /**
216  * Reads table of data headers specific for each descendants.
217  */
218  virtual void make_header_table()=0;
219 
220  /**
221  * Read element data to data cache
222  */
223  virtual void read_element_data(ElementDataCacheBase &data_cache, MeshDataHeader actual_header, unsigned int n_components,
224  bool boundary_domain)=0;
225 
226  /**
227  * Flag stores that check of compatible mesh was performed.
228  *
229  * This flag has effect only for VTK reader.
230  */
232 
233  /// Store name of field data section specify for type of mesh file.
234  std::string data_section_name_;
235 
236  /// Cache with last read element data
237  std::shared_ptr<ElementDataFieldMap> element_data_values_;
238 
239  /// Tokenizer used for reading ASCII file format.
240  Tokenizer tok_;
241 
242  /// Vector of both bulk and boundary IDs. Bulk elements come first, then boundary elements, but only the portion that appears
243  /// in input mesh file and has ID assigned.
245 
246  /// Header of actual loaded data.
248 };
249 
250 #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:99
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.
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.
vector< IdxInt > boundary_elements_id_
virtual void read_element_data(ElementDataCacheBase &data_cache, MeshDataHeader actual_header, unsigned int n_components, bool boundary_domain)=0
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)
vector< IdxInt > bulk_elements_id_
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)