Flow123d  3.9.0-9663d1cde
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 
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 "system/index_types.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  DECLARE_EXCEPTION(ExcWrongComponentsCount,
81  << "Wrong number of components of field " << EI_FieldName::qval
82  << " at time " << EI_Time::val
83  << " in the input file: " << EI_MeshFile::qval);
84 
85 
86  /***********************************
87  * Structure to store the information from a header of \\$ElementData (GMSH file) or DataArray (VTK file) section.
88  *
89  * Format of GMSH ASCII data sections
90  *
91  number-of-string-tags (== 2)
92  field_name
93  interpolation_scheme_name
94  number-of-real-tags (==1)
95  time_of_dataset
96  number-of-integer-tags
97  time_step_index (starting from zero)
98  number_of_field_components (1, 3, or 9 - i.e. 3d scalar, vector or tensor data)
99  number_of entities (nodes or elements)
100  partition_index (0 == no partition, not clear if GMSH support reading different partition from different files)
101  elm-number value ...
102  *
103  */
104  struct MeshDataHeader {
105  /// Set field_name value to empty string, that signs invalid header (using after reading data)
106  void reset() { field_name = ""; }
107  /// Name of field
108  std::string field_name;
109  /// Currently d'ont used
110  std::string interpolation_scheme;
111  /// Time of field data (used only for GMSH reader)
112  double time;
113  /// Currently d'ont used
114  unsigned int time_index;
115  /// Number of values on one row
116  unsigned int n_components;
117  /// Number of rows
118  unsigned int n_entities;
119  /// Currently d'ont used
120  unsigned int partition_index;
121  /// Position of data in mesh file
122  Tokenizer::Position position;
123  /// Type of data (used only for VTK reader)
124  DataType type;
125  /// Flag marks input discretization of data of VTK file
127  /// Hash of DOF handler object (only for native data of VTK file)
128  std::size_t dof_handler_hash;
129  };
130 
131  /**
132  * Store base data that allows search mesh data header.
133  *
134  * Data members discretization and dof_handler_hash are specific for VTK files.
135  */
136  struct HeaderQuery {
137  /// Constructor
138  HeaderQuery(std::string name, double t, OutputTime::DiscreteSpace disc, std::size_t hash=0)
139  : field_name(name), time(t), discretization(disc), dof_handler_hash(hash) {};
140 
141  std::string field_name; ///< Name of field
142  double time; ///< Time of field data (used only for GMSH and PVD reader)
143  OutputTime::DiscreteSpace discretization; ///< Flag determinate type of discrete of Field (typically is used for native data of VTK)
144  std::size_t dof_handler_hash; ///< Hash of DOF handler object
145  };
146 
147  /// Constructor
148  BaseMeshReader(const FilePath &file_name);
149 
150  /**
151  * This static method gets file path object of reader,
152  * dispatch to correct constructor and initialize appropriate function object from the input.
153  * Returns shared pointer to BaseMeshReader.
154  */
155  static std::shared_ptr< BaseMeshReader > reader_factory(const FilePath &file_name);
156 
157  /**
158  * This static method gets accessor to record with function input,
159  * dispatch to correct constructor and initialize appropriate function object from the input.
160  * Allow to make optimization of elements and nodes order if flag optimize_mesh is set
161  * Returns pointer to Mesh.
162  */
163  static Mesh * mesh_factory(const Input::Record &input_mesh_rec);
164 
165  /**
166  * Reads @p raw data of mesh (only nodes and elements) from the GMSH or VTKfile.
167  * Input of the mesh allows changing regions within the input file.
168  *
169  */
170  void read_raw_mesh(Mesh * mesh);
171 
172  /**
173  * Read regions from the mesh file and save the physical sections as regions in the RegionDB.
174  */
175  virtual void read_physical_names(Mesh * mesh)=0;
176 
177  /**
178  * Reads ElementData sections of opened mesh file. Method must be call after \p set_data_header method. If such section
179  * has not been yet read, we read the data section into raw buffer @p data. The buffer must have size at least
180  * @p n_components * @p n_entities. Indexes in the map must be smaller then @p n_entities.
181  *
182  * Possible optimizations:
183  * If the map ID lookup seem slow, we may assume that IDs are in increasing order, use simple array of IDs instead of map
184  * and just check that they comes in in correct order.
185  *
186  * @param n_entities count of entities (elements)
187  * @param n_components count of components (size of returned data is given by n_entities*n_components)
188  * @param boundary_domain flag determines that data is read for boundary or bulk elements
189  * @param component_idx component index of MultiField; 0 for single component fields.
190  */
191  template<typename T>
193  MeshDataHeader header, unsigned int expected_n_entities,
194  unsigned int expected_n_components, bool boundary_domain);
195 
196  /**
197  * Set ID vectors from a different mesh.
198  * Must be set in order to determine for which IDs the GMSH reader should read the data.
199  * Could possibly read just a subset.
200  */
201  void set_element_ids(const Mesh &mesh);
202 
203  /**
204  * Returns vector of boundary or bulk element IDs to read.
205  * Used by GMSH reader only.
206  */
207  std::vector<int> const & get_element_ids(bool boundary_domain);
208 
209  /**
210  * Find data header for time and field given by header_query.
211  */
212  virtual MeshDataHeader & find_header(HeaderQuery &header_query)=0;
213 
214  /**
215  * Scale and check data stored in ElementDataCache of given field.
216  */
217  CheckResult scale_and_check_limits(string field_name, double coef, double default_val,
218  double lower_bound = -std::numeric_limits<double>::max(), double upper_bound = std::numeric_limits<double>::max());
219 
220 
221 protected:
222  typedef std::shared_ptr<ElementDataCacheBase> ElementDataPtr;
224 
225  /// Constructor
226  BaseMeshReader(const FilePath &file_name, std::shared_ptr<ElementDataFieldMap> element_data_values);
227 
228 
229  /**
230  * private method for reading of nodes
231  */
232  virtual void read_nodes(Mesh * mesh)=0;
233 
234  /**
235  * Method for reading of elements.
236  */
237  virtual void read_elements(Mesh * mesh)=0;
238 
239  /**
240  * Reads table of data headers specific for each descendants.
241  */
242  virtual void make_header_table()=0;
243 
244  /**
245  * Read element data to data cache
246  */
247  virtual void read_element_data(ElementDataCacheBase &data_cache, MeshDataHeader header,
248  bool boundary_domain)=0;
249 
250  /**
251  * Flag stores that check of compatible mesh was performed.
252  *
253  * This flag has effect only for VTK reader.
254  */
256 
257  /// Store name of field data section specify for type of mesh file.
258  std::string data_section_name_;
259 
260  /// Cache with last read element data
261  std::shared_ptr<ElementDataFieldMap> element_data_values_;
262 
263  /// Tokenizer used for reading ASCII file format.
264  Tokenizer tok_;
265 
266  /// Vector of both bulk and boundary IDs. Bulk elements come first, then boundary elements, but only the portion that appears
267  /// in input mesh file and has ID assigned.
268  /// If set through set_element_ids, the GMSH reader only reads given IDs and check that all IDs are read.
270 
271  friend class ReaderCache;
272 };
273 
274 #endif /* MSH_BASE_READER_HH */
BaseMeshReader::TYPEDEF_ERR_INFO
TYPEDEF_ERR_INFO(EI_FieldName, std::string)
BaseMeshReader::tok_
Tokenizer tok_
Tokenizer used for reading ASCII file format.
Definition: msh_basereader.hh:264
element_data_cache.hh
BaseMeshReader::scale_and_check_limits
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())
Definition: msh_basereader.cc:132
BaseMeshReader::boundary_elements_id_
vector< LongIdx > boundary_elements_id_
Definition: msh_basereader.hh:269
BaseMeshReader::MeshDataHeader::reset
void reset()
Set field_name value to empty string, that signs invalid header (using after reading data)
Definition: msh_basereader.hh:123
uint8
@ uint8
Definition: msh_basereader.hh:46
BaseMeshReader::reader_factory
static std::shared_ptr< BaseMeshReader > reader_factory(const FilePath &file_name)
Definition: msh_basereader.cc:38
BaseMeshReader::find_header
virtual MeshDataHeader & find_header(HeaderQuery &header_query)=0
undefined
@ undefined
Definition: msh_basereader.hh:46
uint64
@ uint64
Definition: msh_basereader.hh:46
file_path.hh
FilePath
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
BaseMeshReader::read_nodes
virtual void read_nodes(Mesh *mesh)=0
BaseMeshReader::set_element_ids
void set_element_ids(const Mesh &mesh)
Definition: msh_basereader.cc:81
BaseMeshReader::MeshDataHeader::type
DataType type
Type of data (used only for VTK reader)
Definition: msh_basereader.hh:141
std::vector< int >
BaseMeshReader::HeaderQuery::dof_handler_hash
std::size_t dof_handler_hash
Hash of DOF handler object.
Definition: msh_basereader.hh:144
BaseMeshReader::MeshDataHeader::interpolation_scheme
std::string interpolation_scheme
Currently d'ont used.
Definition: msh_basereader.hh:127
BaseMeshReader::ElementDataPtr
std::shared_ptr< ElementDataCacheBase > ElementDataPtr
Definition: msh_basereader.hh:222
BaseMeshReader::MeshDataHeader::n_components
unsigned int n_components
Number of values on one row.
Definition: msh_basereader.hh:133
index_types.hh
BaseMeshReader::element_data_values_
std::shared_ptr< ElementDataFieldMap > element_data_values_
Cache with last read element data.
Definition: msh_basereader.hh:261
exceptions.hh
int8
@ int8
Definition: msh_basereader.hh:46
BaseMeshReader::MeshDataHeader::discretization
OutputTime::DiscreteSpace discretization
Flag marks input discretization of data of VTK file.
Definition: msh_basereader.hh:143
BaseMeshReader::mesh_factory
static Mesh * mesh_factory(const Input::Record &input_mesh_rec)
Definition: msh_basereader.cc:52
BaseMeshReader::HeaderQuery::field_name
std::string field_name
Name of field.
Definition: msh_basereader.hh:139
BaseMeshReader
Definition: msh_basereader.hh:58
BaseMeshReader::DECLARE_INPUT_EXCEPTION
DECLARE_INPUT_EXCEPTION(ExcFieldNameNotFound,<< "No data for field: "<< EI_FieldName::qval<< " and time: "<< EI_Time::val<< " in the input file: "<< EI_MeshFile::qval)
BaseMeshReader::make_header_table
virtual void make_header_table()=0
Input::Record
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
int32
@ int32
Definition: msh_basereader.hh:46
BaseMeshReader::MeshDataHeader::field_name
std::string field_name
Name of field.
Definition: msh_basereader.hh:125
uint16
@ uint16
Definition: msh_basereader.hh:46
accessors.hh
CheckResult
CheckResult
Return type of method that checked data stored in ElementDataCache (NaN values, limits)
Definition: element_data_cache.hh:36
output_time.hh
BaseMeshReader::get_element_ids
const std::vector< int > & get_element_ids(bool boundary_domain)
Definition: msh_basereader.cc:88
BaseMeshReader::read_physical_names
virtual void read_physical_names(Mesh *mesh)=0
BaseMeshReader::BaseMeshReader
BaseMeshReader(const FilePath &file_name)
Constructor.
Definition: msh_basereader.cc:28
int16
@ int16
Definition: msh_basereader.hh:46
ElementDataCacheBase
Definition: element_data_cache_base.hh:33
input_exception.hh
std::map
Definition: doxy_dummy_defs.hh:11
BaseMeshReader::HeaderQuery::HeaderQuery
HeaderQuery(std::string name, double t, OutputTime::DiscreteSpace disc, std::size_t hash=0)
Constructor.
Definition: msh_basereader.hh:138
BaseMeshReader::ElementDataFieldMap
std::map< string, ElementDataPtr > ElementDataFieldMap
Definition: msh_basereader.hh:223
BaseMeshReader::MeshDataHeader::dof_handler_hash
std::size_t dof_handler_hash
Hash of DOF handler object (only for native data of VTK file)
Definition: msh_basereader.hh:145
partitioning.hh
BaseMeshReader::MeshDataHeader
Definition: msh_basereader.hh:104
int64
@ int64
Definition: msh_basereader.hh:46
uint32
@ uint32
Definition: msh_basereader.hh:46
Mesh
Definition: mesh.h:361
BaseMeshReader::read_elements
virtual void read_elements(Mesh *mesh)=0
ElementDataCache::CacheData
std::shared_ptr< std::vector< T > > CacheData
Definition: element_data_cache.hh:52
BaseMeshReader::HeaderQuery::discretization
OutputTime::DiscreteSpace discretization
Flag determinate type of discrete of Field (typically is used for native data of VTK)
Definition: msh_basereader.hh:143
BaseMeshReader::MeshDataHeader::partition_index
unsigned int partition_index
Currently d'ont used.
Definition: msh_basereader.hh:137
BaseMeshReader::MeshDataHeader::position
Tokenizer::Position position
Position of data in mesh file.
Definition: msh_basereader.hh:139
float64
@ float64
Definition: msh_basereader.hh:46
BaseMeshReader::read_raw_mesh
void read_raw_mesh(Mesh *mesh)
Definition: msh_basereader.cc:74
OutputTime::DiscreteSpace
DiscreteSpace
Definition: output_time.hh:108
BaseMeshReader::MeshDataHeader::n_entities
unsigned int n_entities
Number of rows.
Definition: msh_basereader.hh:135
BaseMeshReader::MeshDataHeader::time_index
unsigned int time_index
Currently d'ont used.
Definition: msh_basereader.hh:131
BaseMeshReader::get_element_data
ElementDataCache< T >::CacheData get_element_data(MeshDataHeader header, unsigned int expected_n_entities, unsigned int expected_n_components, bool boundary_domain)
Definition: msh_basereader.cc:95
BaseMeshReader::data_section_name_
std::string data_section_name_
Store name of field data section specify for type of mesh file.
Definition: msh_basereader.hh:258
BaseMeshReader::MeshDataHeader::time
double time
Time of field data (used only for GMSH reader)
Definition: msh_basereader.hh:129
BaseMeshReader::DECLARE_EXCEPTION
DECLARE_EXCEPTION(ExcWrongFormat,<< "Wrong format of "<< EI_Type::val<< ", "<< EI_TokenizerMsg::val<< "\n"<< "in the input file: "<< EI_MeshFile::qval)
BaseMeshReader::HeaderQuery::time
double time
Time of field data (used only for GMSH and PVD reader)
Definition: msh_basereader.hh:142
BaseMeshReader::read_element_data
virtual void read_element_data(ElementDataCacheBase &data_cache, MeshDataHeader header, bool boundary_domain)=0
BaseMeshReader::has_compatible_mesh_
bool has_compatible_mesh_
Definition: msh_basereader.hh:255
float32
@ float32
Definition: msh_basereader.hh:46
BaseMeshReader::HeaderQuery
Definition: msh_basereader.hh:136
ReaderCache
Definition: reader_cache.hh:37
BaseMeshReader::bulk_elements_id_
vector< LongIdx > bulk_elements_id_
Definition: msh_basereader.hh:269
DataType
DataType
Types of VTK data (value 'undefined' for empty value)
Definition: msh_basereader.hh:45
tokenizer.hh