Flow123d  jenkins-Flow123d-windows32-release-multijob-28
msh_gmshreader.cc
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  * @file
26  * @ingroup mesh
27  * @brief
28  * @author dalibor
29  *
30  * @date Created on October 3, 2010, 11:32 AM
31  */
32 
33 #include <istream>
34 #include <string>
35 #include <limits>
36 
37 #include "msh_gmshreader.h"
38 
39 #include "system/system.hh"
40 #include "system/sys_profiler.hh"
41 #include "system/tokenizer.hh"
42 #include "boost/lexical_cast.hpp"
43 
44 #include "mesh/mesh.h"
45 #include "mesh/nodes.hh"
46 
47 
48 using namespace std;
49 
50 
52 : tok_(file_name)
53 {
54  tok_.set_comment_pattern( "#");
55  last_header.time=-numeric_limits<double>::infinity();
56  last_header.actual=false;
57 }
58 
59 
60 
62 : tok_(in)
63 {
64  tok_.set_comment_pattern( "#");
65  last_header.time=-numeric_limits<double>::infinity();
66  last_header.actual=false;
67 }
68 
69 
70 
71 GmshMeshReader::~GmshMeshReader() // Tokenizer close the file automatically
72 {}
73 
74 
75 
77  START_TIMER("GMSHReader - read mesh");
78 
79  ASSERT( mesh , "Argument mesh is NULL.\n");
81  read_nodes(tok_, mesh);
82  read_elements(tok_, mesh, el_to_reg_map);
83 }
84 
85 
86 
87 void GmshMeshReader::read_nodes(Tokenizer &tok, Mesh* mesh) {
88  using namespace boost;
89  xprintf(Msg, "- Reading nodes...");
90 
91  if (! tok.skip_to("$Nodes")) xprintf(UsrErr,"Missing section '$Nodes' in the GMSH input file: %s\n",tok.f_name().c_str());
92  try {
93  tok.next_line(false);
94  unsigned int n_nodes = lexical_cast<unsigned int> (*tok);;
95  INPUT_CHECK( n_nodes > 0, "Zero number of nodes, %s.\n", tok.position_msg().c_str() );
96  ++tok; // end of line
97 
98  mesh->node_vector.reserve(n_nodes);
99  for (unsigned int i = 0; i < n_nodes; ++i) {
100  tok.next_line();
101 
102  unsigned int id = lexical_cast<unsigned int> (*tok); ++tok;
103  NodeFullIter node = mesh->node_vector.add_item(id);
104 
105  node->point()(0)=lexical_cast<double> (*tok); ++tok;
106  node->point()(1)=lexical_cast<double> (*tok); ++tok;
107  node->point()(2)=lexical_cast<double> (*tok); ++tok;
108  ++tok; // skip mesh size parameter
109  }
110 
111  } catch (bad_lexical_cast &) {
112  xprintf(UsrErr, "Wrong format of number, %s.\n", tok.position_msg().c_str());
113  }
114  xprintf(Msg, " %d nodes read. \n", mesh->node_vector.size());
115 }
116 
117 
118 
119 void GmshMeshReader::read_elements(Tokenizer &tok, Mesh * mesh, const RegionDB::MapElementIDToRegionID *el_to_reg_map) {
120  using namespace boost;
121  xprintf(Msg, "- Reading elements...");
122 
123  if (! tok.skip_to("$Elements")) xprintf(UsrErr,"Missing section '$Elements' in the GMSH input file: %s\n",tok.f_name().c_str());
124  try {
125  tok.next_line(false);
126  unsigned int n_elements = lexical_cast<unsigned int> (*tok);
127  INPUT_CHECK( n_elements > 0, "Zero number of elements, %s.\n", tok.position_msg().c_str());
128  ++tok; // end of line
129 
130  mesh->element.reserve(n_elements);
131 
132  for (unsigned int i = 0; i < n_elements; ++i) {
133  tok.next_line();
134 
135  unsigned int id = lexical_cast<unsigned int>(*tok); ++tok;
136 
137 
138  //get element type: supported:
139  // 1 Line (2 nodes)
140  // 2 Triangle (3 nodes)
141  // 4 Tetrahedron (4 nodes)
142  // 15 Point (1 node)
143  unsigned int type = lexical_cast<unsigned int>(*tok); ++tok;
144  unsigned int dim;
145  switch (type) {
146  case 1:
147  dim = 1;
148  break;
149  case 2:
150  dim = 2;
151  break;
152  case 4:
153  dim = 3;
154  break;
155  case 15:
156  dim = 0;
157  break;
158  default:
159  dim = 0;
160  xprintf(UsrErr, "Element %d is of the unsupported type %d\n", id, type);
161  break;
162  }
163 
164  //get number of tags (at least 2)
165  unsigned int n_tags = lexical_cast<unsigned int>(*tok);
166  INPUT_CHECK(n_tags >= 2, "At least two element tags have to be defined for element with id=%d, %s.\n",
167  id, tok.position_msg().c_str());
168  ++tok;
169 
170  //get tags 1 and 2
171  unsigned int region_id = lexical_cast<unsigned int>(*tok); ++tok;
172  lexical_cast<unsigned int>(*tok); ++tok; // GMSH region number, we do not store this
173  //get remaining tags
174  unsigned int partition_id=0;
175  if (n_tags > 2) { partition_id = lexical_cast<unsigned int>(*tok); ++tok; } // save partition number from the new GMSH format
176  for (unsigned int ti = 3; ti < n_tags; ti++) ++tok; //skip remaining tags
177 
178  // allocate element arrays TODO: should be in mesh class
179  Element *ele=nullptr;
180  // possibly modify region id
181  if (el_to_reg_map) {
182  RegionDB::MapElementIDToRegionID::const_iterator it = el_to_reg_map->find(id);
183  if (it != el_to_reg_map->end()) region_id = it->second;
184  }
185  RegionIdx region_idx = mesh->region_db_.add_region( region_id, dim );
186 
187  if (region_idx.is_boundary()) {
188  ele = mesh->bc_elements.add_item(id);
189  } else {
190  if(dim == 0 )
191  xprintf(Warn, "Bulk elements of zero size(dim=0) are not supported. Mesh file: %s, Element ID: %d.\n", tok.f_name().c_str() ,id);
192  else
193  ele = mesh->element.add_item(id);
194  }
195  ele->init(dim, mesh, region_idx);
196  ele->pid=partition_id;
197 
198  unsigned int ni;
199  FOR_ELEMENT_NODES(ele, ni) {
200  unsigned int node_id = lexical_cast<unsigned int>(*tok);
201  NodeIter node = mesh->node_vector.find_id( node_id );
202  INPUT_CHECK( node!=mesh->node_vector.end() ,
203  "Unknown node id %d in specification of element with id=%d, %s.\n",
204  node_id, id, tok.position_msg().c_str());
205  ele->node[ni] = node;
206  ++tok;
207  }
208  }
209 
210  } catch (bad_lexical_cast &) {
211  xprintf(UsrErr, "Wrong format of number, %s.\n", tok.position_msg().c_str());
212  }
213 
214  mesh->n_all_input_elements_=mesh->element.size() + mesh->bc_elements.size();
215  xprintf(Msg, " %d bulk elements, %d boundary elements. \n", mesh->element.size(), mesh->bc_elements.size());
216 }
217 
218 
219 
220 void GmshMeshReader::read_physical_names(Tokenizer &tok, Mesh * mesh) {
221  using namespace boost;
222 
223  if (! tok.skip_to("$PhysicalNames", "$Nodes") ) return;
224  try {
225  tok.next_line(false);
226  unsigned int n_physicals = lexical_cast<unsigned int> (*tok);
227  ++tok; // end of line
228 
229  for (unsigned int i = 0; i < n_physicals; ++i) {
230  tok.next_line();
231  // format of one line:
232  // dim physical-id physical-name
233 
234  unsigned int dim = lexical_cast<unsigned int>(*tok); ++tok;
235  unsigned int id = lexical_cast<unsigned int>(*tok); ++tok;
236  string name = *tok; ++tok;
237 
238  bool boundary = ( name.size() != 0 && name[0] == '.' );
239  mesh->region_db_.add_region(id, name, dim, boundary);
240  }
241 
242  } catch (bad_lexical_cast &) {
243  xprintf(UsrErr, "Wrong format of number, %s.\n", tok.position_msg().c_str());
244  }
245 }
246 
247 
248 // Is assumed to be called just after tok.skip_to("..")
249 // reads the header from the tokenizer @p tok and return it as the second parameter
251  using namespace boost;
252  try {
253  // string tags
254  tok.next_line(false);
255  unsigned int n_str = lexical_cast<unsigned int>(*tok); ++tok;
256  head.field_name="";
257  head.interpolation_scheme = "";
258  if (n_str > 0) {
259  tok.next_line(); n_str--;
260  head.field_name= *tok; ++tok; // unquoted by tokenizer if needed
261  }
262  if (n_str > 0) {
263  tok.next_line(); n_str--;
264  head.interpolation_scheme = *tok; ++tok;
265  }
266  for(;n_str>0;n_str--) tok.next_line(false); // skip possible remaining tags
267 
268  //real tags
269  tok.next_line();
270  unsigned int n_real = lexical_cast<unsigned int>(*tok); ++tok;
271  head.time=0.0;
272  if (n_real>0) {
273  tok.next_line(); n_real--;
274  head.time=lexical_cast<double>(*tok); ++tok;
275  }
276  for(;n_real>0;n_real--) tok.next_line(false);
277 
278  // int tags
279  tok.next_line();
280  unsigned int n_int = lexical_cast<unsigned int>(*tok); ++tok;
281  head.time_index=0;
282  head.n_components=1;
283  head.n_entities=0;
284  head.partition_index=0;
285  if (n_int>0) {
286  tok.next_line(); n_int--;
287  head.time_index=lexical_cast<unsigned int>(*tok); ++tok;
288  }
289  if (n_int>0) {
290  tok.next_line(); n_int--;
291  head.n_components=lexical_cast<unsigned int>(*tok); ++tok;
292  }
293  if (n_int>0) {
294  tok.next_line(); n_int--;
295  head.n_entities=lexical_cast<unsigned int>(*tok); ++tok;
296  }
297  for(;n_int>0;n_int--) tok.next_line(false);
298  } catch (bad_lexical_cast &) {
299  xprintf(UsrErr, "Wrong format of the $ElementData header, %s.\n", tok.position_msg().c_str());
300  }
301 }
302 
303 
304 
306  double *data, std::vector<int> const & el_ids)
307 {
308 
309  using namespace boost;
310 
311  unsigned int id, idx, n_read;
313  double * data_ptr;
314 
315  while ( last_header.time <= search_header.time*(1.0 + 2.0*numeric_limits<double>::epsilon()) ) {
316  // @p data buffer is not actual anymore
317 
318  if (last_header.actual) {
319  // read @p data buffer as we have correct header with already passed time
320  // we assume that @p data buffer is big enough
321 
322  n_read = 0;
323  id_iter=el_ids.begin();
324  unsigned int i_row;
325  for (i_row = 0; i_row < last_header.n_entities; ++i_row)
326  try {
327  tok_.next_line();
328 // DBGMSG("data line: %d %d '%s'\n", i_row, last_header.n_entities, tok_.line().c_str());
329  id = lexical_cast<unsigned int>(*tok_); ++tok_;
330  while (id_iter != el_ids.end() && *id_iter < (int)id) {
331 // DBGMSG("get id: %u %d\n", id, *id_iter);
332  ++id_iter; // skip initialization of some rows in data if ID is missing
333  }
334  if (id_iter == el_ids.end()) {
335  xprintf(Warn,"In file '%s', '$ElementData' section for field '%s', time: %f.\nData ID %d not found or is not in order. Skipping rest of data.\n",
336  tok_.f_name().c_str(), search_header.field_name.c_str(), last_header.time, id);
337  break;
338  }
339  // save data from the line if ID was found
340  if (*id_iter == (int)id) {
341  idx = id_iter - el_ids.begin();
342  data_ptr = data + idx * search_header.n_components;
343  for (unsigned int i_col =0; i_col < search_header.n_components; ++i_col, ++data_ptr) {
344  *(data_ptr) = lexical_cast<double>(*tok_); ++tok_;
345  }
346  n_read++;
347  }
348  // skip the line if ID on the line < actual ID in the map el_ids
349  } catch (bad_lexical_cast &) {
350  xprintf(UsrErr, "Wrong format of $ElementData line, %s.\n", tok_.position_msg().c_str());
351  }
352  // possibly skip remaining lines after break
353  while (i_row < last_header.n_entities) tok_.next_line(false), ++i_row;
354 
355  xprintf(Msg, "time: %f; %d entities of field %s read.\n",
356  last_header.time, n_read, last_header.field_name.c_str());
357 
358  search_header.actual = true; // use input header to indicate modification of @p data buffer
359  }
360 
361  // find next the data section of corresponding field name
363  while (! tok_.eof() && last_header.field_name != search_header.field_name) {
364  if ( tok_.skip_to("$ElementData") )
366  }
367 
368  if (tok_.eof()) {
369  if (! last_header.actual) {
370  // first call of the method, no data read
371  xprintf(UsrErr, "In file '%s', missing '$ElementData' section for field '%s'.\n",
372  tok_.f_name().c_str(), search_header.field_name.c_str());
373  return;
374  } else {
375  // mark data as actual until inf
376  last_header.time=numeric_limits<double>::infinity(); //
377  return;
378  }
379  } else {
380  // check that the header is valid, try to correct
381  if (last_header.n_components != search_header.n_components) {
382  xprintf(Warn, "In file '%s', '$ElementData' section for field '%s', time: %f.\nWrong number of components: %d, using %d instead.\n",
383  tok_.f_name().c_str(), search_header.field_name.c_str(), last_header.time, last_header.n_components, search_header.n_components);
384  last_header.n_components=search_header.n_components;
385  }
386  if (last_header.n_entities != search_header.n_entities) {
387  xprintf(Warn, "In file '%s', '$ElementData' section for field '%s', time: %f.\nWrong number of entities: %d, using %d instead.\n",
388  tok_.f_name().c_str(), search_header.field_name.c_str(), last_header.time, last_header.n_entities, search_header.n_entities);
389  // last_header.n_entities=search_header.n_entities;
390  }
391 
392  }
393  last_header.actual=true;
394 
395  } // time loop
396 }
397 
std::string field_name
#define FOR_ELEMENT_NODES(i, j)
Definition: elements.h:150
void read_nodes(Tokenizer &in, Mesh *)
bool is_boundary() const
Allow implicit conversion from Region. We loose information about input ID, label, dim stored in database.
Definition: region.hh:66
Definition: system.hh:75
Definition: nodes.hh:44
Nodes of a mesh.
int pid
Definition: elements.h:87
void read_data_header(Tokenizer &tok, GMSH_DataHeader &head)
void read_physical_names(Tokenizer &in, Mesh *mesh)
???
Definition: mesh.h:108
void reserve(unsigned int size)
Reallocates the container space.
Definition: sys_vector.hh:490
Node ** node
Definition: elements.h:90
FullIter add_item(int id)
Definition: sys_vector.hh:379
GmshMeshReader(const FilePath &file_name)
FullIter find_id(const int id)
Definition: sys_vector.hh:455
void read_elements(Tokenizer &in, Mesh *, const RegionDB::MapElementIDToRegionID *el_to_reg_map=NULL)
Region add_region(unsigned int id, const std::string &label, unsigned int dim, bool boundary)
Definition: region.cc:107
unsigned int size() const
Returns size of the container. This is independent of the allocated space.
Definition: sys_vector.hh:412
unsigned int n_entities
Number of rows.
#define ASSERT(...)
Definition: global_defs.h:120
Definition: system.hh:75
#define xprintf(...)
Definition: system.hh:104
#define START_TIMER(tag)
Starts a timer with specified tag.
void read_element_data(GMSH_DataHeader &search_header, double *data, std::vector< int > const &el_ids)
ElementVector bc_elements
Definition: mesh.h:206
unsigned int n_components
Number of values on one row.
#define INPUT_CHECK(i,...)
Debugging macros.
Definition: global_defs.h:61
void init(unsigned int dim, Mesh *mesh_in, RegionIdx reg)
Definition: elements.cc:72
Dedicated class for storing path to input and output files.
Definition: file_path.hh:34
Definition: system.hh:75
const double epsilon
Definition: mathfce.h:6
unsigned int n_all_input_elements_
Number of elements read from input.
Definition: mesh.h:319
void read_mesh(Mesh *mesh, const RegionDB::MapElementIDToRegionID *el_to_reg_map=NULL)
RegionDB region_db_
Definition: mesh.h:331
GMSH_DataHeader last_header
Last read header of ElementData section.
std::string interpolation_scheme
Currently ont used.
unsigned int time_index
Currently ont used.
FullIter end()
Returns FullFullIterer of the fictions past the end element.
Definition: sys_vector.hh:408
unsigned int partition_index
?? Currently ont used
Tokenizer tok_
Tokenizer used for reading ASCII GMSH file format.
NodeVector node_vector
Vector of nodes of the mesh.
Definition: mesh.h:196
ElementVector element
Vector of elements of the mesh.
Definition: mesh.h:198