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