Flow123d  jenkins-Flow123d-windows-release-multijob-285
msh_gmshreader.h
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2007 Technical University of Liberec. All rights reserved.
4  *
5  * Please make a following refer to Flow123d on your project site if you use the program for any purpose,
6  * especially for academic research:
7  * Flow123d, Research Centre: Advanced Remedial Technologies, Technical University of Liberec, Czech Republic
8  *
9  * This program is free software; you can redistribute it and/or modify it under the terms
10  * of the GNU General Public License version 3 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along with this program; if not,
17  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 021110-1307, USA.
18  *
19  *
20  * $Id$
21  * $Revision$
22  * $LastChangedBy$
23  * $LastChangedDate$
24  *
25  *
26  * @file msh_gmshreader.h
27  * @author dalibor
28  *
29  * @date October 3, 2010, 11:23 AM
30  */
31 
32 #ifndef _GMSHMESHREADER_H
33 #define _GMSHMESHREADER_H
34 
35 #include <string>
36 #include <istream>
37 #include <vector>
38 #include <map>
39 
40 
41 #include "system/tokenizer.hh"
42 #include "mesh/region.hh"
44 #include "input/accessors.hh"
45 
46 class Mesh;
47 class FilePath;
48 
49 
50 
51 /***********************************
52  * Structure to store the information from a header of \\$ElementData section.
53  *
54  * Format of GMSH ASCII data sections
55  *
56  number-of-string-tags (== 2)
57  field_name
58  interpolation_scheme_name
59  number-of-real-tags (==1)
60  time_of_dataset
61  number-of-integer-tags
62  time_step_index (starting from zero)
63  number_of_field_components (1, 3, or 9 - i.e. 3d scalar, vector or tensor data)
64  number_of entities (nodes or elements)
65  partition_index (0 == no partition, not clear if GMSH support reading different partition from different files)
66  elm-number value ...
67 *
68 */
69 
71  /// True if the stream position is just after the header.
72  /// False either before first header is found or at EOF.
73  bool actual;
74  std::string field_name;
75  /// Currently ont used
76  std::string interpolation_scheme;
77  double time;
78  /// Currently ont used
79  unsigned int time_index;
80  /// Number of values on one row
81  unsigned int n_components;
82  /// Number of rows
83  unsigned int n_entities;
84  /// ?? Currently ont used
85  unsigned int partition_index;
86  /// Position of data in mesh file
87  Tokenizer::Position position;
88 };
89 
90 
92 public:
93  TYPEDEF_ERR_INFO(EI_FieldName, std::string);
94  TYPEDEF_ERR_INFO(EI_GMSHFile, std::string);
95  TYPEDEF_ERR_INFO(EI_Time, double);
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_GMSHFile::qval);
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  * Optional map el_to_reg_map can be used to override region of some elements provided by GMSH file.
129  * Input of the mesh allows changing regions within the input CON file.
130  */
131  void read_mesh(Mesh* mesh, const RegionDB::MapElementIDToRegionID *el_to_reg_map=NULL);
132 
133  /**
134  * Reads ElementData sections of opened GMSH file. The file is serached for the \\$ElementData section with header
135  * that match the given @p search_header (same field_name, time of the next section is the first greater then
136  * that given in the @p search_header). If such section has not been yet read, we read the data section into
137  * raw buffer @p data. The map @p id_to_idx is used to convert IDs that marks individual input rows/entities into
138  * indexes to the raw buffer. The buffer must have size at least @p search_header.n_components * @p search_header.n_entities.
139  * Indexes in the map must be smaller then @p search_header.n_entities.
140  * If the @p data buffer is updated we set search_header.actual to true.
141  *
142  * Possible optimizations:
143  * If the map ID lookup seem slow, we may assume that IDs are in increasing order, use simple array of IDs instead of map
144  * and just check that they comes in in correct order.
145  */
146  template<typename T>
148  std::vector<int> const & el_ids, unsigned int component_idx);
149 
150 private:
151  /**
152  * Read section '$PhysicalNames' of the GMSH file and save the physical sections as regions in the RegionDB.
153  *
154  * Region Labels starting with '!' are treated as boundary regions. Elements of these regions are used just to
155  * assign regions to the boundary and are not used in actual FEM computations.
156  */
157  void read_physical_names(Tokenizer &in, Mesh * mesh);
158 
159  /**
160  * private method for reading of nodes
161  */
162  void read_nodes(Tokenizer &in, Mesh*);
163  /**
164  * Method for reading of elements.
165  * Optional map el_to_reg_map can be used to override region of some elements provided by GMSH file.
166  * Input of the mesh allows changing regions within the input CON file.
167  *
168  */
169  void read_elements(Tokenizer &in, Mesh*, const RegionDB::MapElementIDToRegionID *el_to_reg_map=NULL);
170  /**
171  * Reads the header from the tokenizer @p tok and return it as the second parameter.
172  */
173  void read_data_header(Tokenizer &tok, GMSH_DataHeader &head);
174  /**
175  * Reads table of ElementData headers from the tokenizer file.
176  */
177  void make_header_table();
178  /**
179  * Finds GMSH data header for ElementData given by time and field_name and return it as the first parameter.
180  */
181  GMSH_DataHeader & find_header(double time, std::string field_name);
182 
183 
184  /// Tokenizer used for reading ASCII GMSH file format.
185  Tokenizer tok_;
186  /// Table with data of ElementData headers
187  HeaderTable header_table_;
188  /// Cache with last read element data
190 };
191 
192 #endif /* _GMSHMESHREADER_H */
193 
194 
std::string field_name
std::shared_ptr< std::vector< T > > ComponentDataPtr
Tokenizer::Position position
Position of data in mesh file.
void read_nodes(Tokenizer &in, Mesh *)
void read_data_header(Tokenizer &tok, GMSH_DataHeader &head)
void read_physical_names(Tokenizer &in, Mesh *mesh)
Definition: mesh.h:109
GmshMeshReader(const FilePath &file_name)
void read_elements(Tokenizer &in, Mesh *, const RegionDB::MapElementIDToRegionID *el_to_reg_map=NULL)
ElementDataCacheBase * current_cache_
Cache with last read element data.
GMSH_DataHeader & find_header(double time, std::string field_name)
unsigned int n_entities
Number of rows.
void make_header_table()
ElementDataCache< T >::ComponentDataPtr get_element_data(GMSH_DataHeader &search_header, std::vector< int > const &el_ids, unsigned int component_idx)
DECLARE_INPUT_EXCEPTION(ExcFieldNameNotFound,<< "No data for field: "<< EI_FieldName::qval<< " and time: "<< EI_Time::val<< " in the input file: "<< EI_GMSHFile::qval)
unsigned int n_components
Number of values on one row.
Dedicated class for storing path to input and output files.
Definition: file_path.hh:32
HeaderTable header_table_
Table with data of ElementData headers.
void read_mesh(Mesh *mesh, const RegionDB::MapElementIDToRegionID *el_to_reg_map=NULL)
TYPEDEF_ERR_INFO(EI_FieldName, std::string)
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