Flow123d  release_2.1.0-87-gfbc1563
msh_gmshreader.h
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_gmshreader.h
15  * @brief
16  * @author dalibor
17  */
18 
19 #ifndef _GMSHMESHREADER_H
20 #define _GMSHMESHREADER_H
21 
22 #include <string>
23 #include <istream>
24 #include <vector>
25 #include <map>
26 
27 
28 #include "system/tokenizer.hh"
29 #include "mesh/region.hh"
31 #include "input/input_exception.hh"
32 
33 class Mesh;
34 class FilePath;
35 
36 
37 
38 /***********************************
39  * Structure to store the information from a header of \\$ElementData section.
40  *
41  * Format of GMSH ASCII data sections
42  *
43  number-of-string-tags (== 2)
44  field_name
45  interpolation_scheme_name
46  number-of-real-tags (==1)
47  time_of_dataset
48  number-of-integer-tags
49  time_step_index (starting from zero)
50  number_of_field_components (1, 3, or 9 - i.e. 3d scalar, vector or tensor data)
51  number_of entities (nodes or elements)
52  partition_index (0 == no partition, not clear if GMSH support reading different partition from different files)
53  elm-number value ...
54 *
55 */
56 
58  /// True if the stream position is just after the header.
59  /// False either before first header is found or at EOF.
60  bool actual;
61  std::string field_name;
62  /// Currently ont used
63  std::string interpolation_scheme;
64  double time;
65  /// Currently ont used
66  unsigned int time_index;
67  /// Number of values on one row
68  unsigned int n_components;
69  /// Number of rows
70  unsigned int n_entities;
71  /// ?? Currently ont used
72  unsigned int partition_index;
73  /// Position of data in mesh file
74  Tokenizer::Position position;
75 };
76 
77 
79 public:
80  TYPEDEF_ERR_INFO(EI_FieldName, std::string);
81  TYPEDEF_ERR_INFO(EI_GMSHFile, std::string);
82  TYPEDEF_ERR_INFO(EI_Time, double);
83  TYPEDEF_ERR_INFO(EI_Type, std::string);
84  TYPEDEF_ERR_INFO(EI_TokenizerMsg, std::string);
85  TYPEDEF_ERR_INFO(EI_Section, std::string);
86  TYPEDEF_ERR_INFO(EI_ElementId, int);
87  TYPEDEF_ERR_INFO(EI_ElementType, int);
88  DECLARE_INPUT_EXCEPTION(ExcFieldNameNotFound,
89  << "No data for field: "<< EI_FieldName::qval
90  << " and time: "<< EI_Time::val
91  << " in the input file: "<< EI_GMSHFile::qval);
92  DECLARE_EXCEPTION(ExcWrongFormat,
93  << "Wrong format of " << EI_Type::val << ", " << EI_TokenizerMsg::val << "\n"
94  << "in the input file: " << EI_GMSHFile::qval);
95  DECLARE_EXCEPTION(ExcMissingSection,
96  << "Missing section " << EI_Section::qval << " in the GMSH input file: " << EI_GMSHFile::qval);
97  DECLARE_EXCEPTION(ExcUnsupportedType,
98  << "Element " << EI_ElementId::val << "in the GMSH input file " << EI_GMSHFile::qval
99  << " is of the unsupported type " << EI_ElementType::val );
100 
101  /**
102  * Map of ElementData sections in GMSH file.
103  *
104  * For each field_name contains vector of GMSH_DataHeader.
105  * Headers are sorted by time in ascending order.
106  */
108 
109  /**
110  * Construct the GMSH format reader from given filename.
111  * This opens the file for reading.
112  */
113  GmshMeshReader(const FilePath &file_name);
114  /**
115  * Construct the GMSH format reader from given input stream.
116  * The input stream should be correctly opened. To get correct information about
117  * line numbers there should be no previous reading from the stream.
118  */
119  GmshMeshReader(std::istream &in);
120 
121  /**
122  * Destructor close the file if opened.
123  */
124  ~GmshMeshReader();
125 
126  /**
127  * Reads @p mesh from the GMSH file.
128  * Input of the mesh allows changing regions within the input CON file.
129  */
130  void read_mesh(Mesh* mesh);
131 
132  /**
133  * Read section '$PhysicalNames' of the GMSH file and save the physical sections as regions in the RegionDB.
134  *
135  * Region Labels starting with '!' are treated as boundary regions. Elements of these regions are used just to
136  * assign regions to the boundary and are not used in actual FEM computations.
137  */
138  void read_physical_names(Mesh * mesh);
139 
140  /**
141  * Reads ElementData sections of opened GMSH file. The file is serached for the \\$ElementData section with header
142  * that match the given @p search_header (same field_name, time of the next section is the first greater then
143  * that given in the @p search_header). If such section has not been yet read, we read the data section into
144  * raw buffer @p data. The map @p id_to_idx is used to convert IDs that marks individual input rows/entities into
145  * indexes to the raw buffer. The buffer must have size at least @p search_header.n_components * @p search_header.n_entities.
146  * Indexes in the map must be smaller then @p search_header.n_entities.
147  * If the @p data buffer is updated we set search_header.actual to true.
148  *
149  * Possible optimizations:
150  * If the map ID lookup seem slow, we may assume that IDs are in increasing order, use simple array of IDs instead of map
151  * and just check that they comes in in correct order.
152  */
153  template<typename T>
154  typename ElementDataCache<T>::ComponentDataPtr get_element_data( GMSH_DataHeader &search_header,
155  std::vector<int> const & el_ids, unsigned int component_idx);
156 
157 private:
158  /**
159  * private method for reading of nodes
160  */
161  void read_nodes(Mesh*);
162  /**
163  * Method for reading of elements.
164  * Input of the mesh allows changing regions within the input CON file.
165  */
166  void read_elements(Mesh*);
167  /**
168  * Reads the header from the tokenizer @p tok and return it as the second parameter.
169  */
170  void read_data_header(GMSH_DataHeader &head);
171  /**
172  * Reads table of ElementData headers from the tokenizer file.
173  */
174  void make_header_table();
175  /**
176  * Finds GMSH data header for ElementData given by time and field_name and return it as the first parameter.
177  */
178  GMSH_DataHeader & find_header(double time, std::string field_name);
179 
180 
181  /// Tokenizer used for reading ASCII GMSH file format.
182  Tokenizer tok_;
183  /// Table with data of ElementData headers
184  HeaderTable header_table_;
185  /// Cache with last read element data
187 };
188 
189 #endif /* _GMSHMESHREADER_H */
190 
191 
std::string field_name
std::shared_ptr< std::vector< T > > ComponentDataPtr
Tokenizer::Position position
Position of data in mesh file.
#define DECLARE_EXCEPTION(ExcName, Format)
Macro for simple definition of exceptions.
Definition: exceptions.hh:150
Definition: mesh.h:95
ElementDataCacheBase * current_cache_
Cache with last read element data.
unsigned int n_entities
Number of rows.
#define TYPEDEF_ERR_INFO(EI_Type, Type)
Macro to simplify declaration of error_info types.
Definition: exceptions.hh:186
unsigned int n_components
Number of values on one row.
Dedicated class for storing path to input and output files.
Definition: file_path.hh:48
#define DECLARE_INPUT_EXCEPTION(ExcName, Format)
Macro for simple definition of input exceptions.
HeaderTable header_table_
Table with data of ElementData headers.
std::string interpolation_scheme
Currently ont used.
unsigned int time_index
Currently ont used.
unsigned int partition_index
?? Currently ont used
Tokenizer tok_
Tokenizer used for reading ASCII GMSH file format.
std::map< std::string, std::vector< GMSH_DataHeader > > HeaderTable