Flow123d  jenkins-Flow123d-windows32-release-multijob-51
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  mesh->region_db_.add_region(id, name, dim);
239  }
240 
241  } catch (bad_lexical_cast &) {
242  xprintf(UsrErr, "Wrong format of number, %s.\n", tok.position_msg().c_str());
243  }
244 }
245 
246 
247 // Is assumed to be called just after tok.skip_to("..")
248 // reads the header from the tokenizer @p tok and return it as the second parameter
250  using namespace boost;
251  try {
252  // string tags
253  tok.next_line(false);
254  unsigned int n_str = lexical_cast<unsigned int>(*tok); ++tok;
255  head.field_name="";
256  head.interpolation_scheme = "";
257  if (n_str > 0) {
258  tok.next_line(); n_str--;
259  head.field_name= *tok; ++tok; // unquoted by tokenizer if needed
260  }
261  if (n_str > 0) {
262  tok.next_line(); n_str--;
263  head.interpolation_scheme = *tok; ++tok;
264  }
265  for(;n_str>0;n_str--) tok.next_line(false); // skip possible remaining tags
266 
267  //real tags
268  tok.next_line();
269  unsigned int n_real = lexical_cast<unsigned int>(*tok); ++tok;
270  head.time=0.0;
271  if (n_real>0) {
272  tok.next_line(); n_real--;
273  head.time=lexical_cast<double>(*tok); ++tok;
274  }
275  for(;n_real>0;n_real--) tok.next_line(false);
276 
277  // int tags
278  tok.next_line();
279  unsigned int n_int = lexical_cast<unsigned int>(*tok); ++tok;
280  head.time_index=0;
281  head.n_components=1;
282  head.n_entities=0;
283  head.partition_index=0;
284  if (n_int>0) {
285  tok.next_line(); n_int--;
286  head.time_index=lexical_cast<unsigned int>(*tok); ++tok;
287  }
288  if (n_int>0) {
289  tok.next_line(); n_int--;
290  head.n_components=lexical_cast<unsigned int>(*tok); ++tok;
291  }
292  if (n_int>0) {
293  tok.next_line(); n_int--;
294  head.n_entities=lexical_cast<unsigned int>(*tok); ++tok;
295  }
296  for(;n_int>0;n_int--) tok.next_line(false);
297  } catch (bad_lexical_cast &) {
298  xprintf(UsrErr, "Wrong format of the $ElementData header, %s.\n", tok.position_msg().c_str());
299  }
300 }
301 
302 
303 
305  double *data, std::vector<int> const & el_ids)
306 {
307 
308  using namespace boost;
309 
310  unsigned int id, idx, n_read;
312  double * data_ptr;
313 
314  while ( last_header.time <= search_header.time*(1.0 + 2.0*numeric_limits<double>::epsilon()) ) {
315  // @p data buffer is not actual anymore
316 
317  if (last_header.actual) {
318  // read @p data buffer as we have correct header with already passed time
319  // we assume that @p data buffer is big enough
320 
321  n_read = 0;
322  id_iter=el_ids.begin();
323  unsigned int i_row;
324  for (i_row = 0; i_row < last_header.n_entities; ++i_row)
325  try {
326  tok_.next_line();
327  id = lexical_cast<unsigned int>(*tok_); ++tok_;
328  while (id_iter != el_ids.end() && *id_iter < (int)id) {
329  ++id_iter; // skip initialization of some rows in data if ID is missing
330  }
331  if (id_iter == el_ids.end()) {
332  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",
333  tok_.f_name().c_str(), search_header.field_name.c_str(), last_header.time, id);
334  break;
335  }
336  // save data from the line if ID was found
337  if (*id_iter == (int)id) {
338  idx = id_iter - el_ids.begin();
339  data_ptr = data + idx * search_header.n_components;
340  for (unsigned int i_col =0; i_col < search_header.n_components; ++i_col, ++data_ptr) {
341  *(data_ptr) = lexical_cast<double>(*tok_); ++tok_;
342  }
343  n_read++;
344  }
345  // skip the line if ID on the line < actual ID in the map el_ids
346  } catch (bad_lexical_cast &) {
347  xprintf(UsrErr, "Wrong format of $ElementData line, %s.\n", tok_.position_msg().c_str());
348  }
349  // possibly skip remaining lines after break
350  while (i_row < last_header.n_entities) tok_.next_line(false), ++i_row;
351 
352  xprintf(Msg, "time: %f; %d entities of field %s read.\n",
353  last_header.time, n_read, last_header.field_name.c_str());
354 
355  search_header.actual = true; // use input header to indicate modification of @p data buffer
356  }
357 
358  // find next the data section of corresponding field name
360  while (! tok_.eof() && last_header.field_name != search_header.field_name) {
361  if ( tok_.skip_to("$ElementData") )
363  }
364 
365  if (tok_.eof()) {
366  if (! last_header.actual) {
367  // first call of the method, no data read
368  xprintf(UsrErr, "In file '%s', missing '$ElementData' section for field '%s'.\n",
369  tok_.f_name().c_str(), search_header.field_name.c_str());
370  return;
371  } else {
372  // mark data as actual until inf
373  last_header.time=numeric_limits<double>::infinity(); //
374  return;
375  }
376  } else {
377  // check that the header is valid, try to correct
378  if (last_header.n_components != search_header.n_components) {
379  xprintf(Warn, "In file '%s', '$ElementData' section for field '%s', time: %f.\nWrong number of components: %d, using %d instead.\n",
380  tok_.f_name().c_str(), search_header.field_name.c_str(), last_header.time, last_header.n_components, search_header.n_components);
381  last_header.n_components=search_header.n_components;
382  }
383  if (last_header.n_entities != search_header.n_entities) {
384  xprintf(Warn, "In file '%s', '$ElementData' section for field '%s', time: %f.\nWrong number of entities: %d, using %d instead.\n",
385  tok_.f_name().c_str(), search_header.field_name.c_str(), last_header.time, last_header.n_entities, search_header.n_entities);
386  // last_header.n_entities=search_header.n_entities;
387  }
388 
389  }
390  last_header.actual=true;
391 
392  } // time loop
393 }
394 
std::string field_name
#define FOR_ELEMENT_NODES(i, j)
Definition: elements.h:150
void read_nodes(Tokenizer &in, Mesh *)
bool is_boundary() const
Returns true if it is a Boundary region and false if it is a Bulk region.
Definition: region.hh:63
Definition: system.hh:72
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:479
Node ** node
Definition: elements.h:90
FullIter add_item(int id)
Definition: sys_vector.hh:369
GmshMeshReader(const FilePath &file_name)
FullIter find_id(const int id)
Definition: sys_vector.hh:444
void read_elements(Tokenizer &in, Mesh *, const RegionDB::MapElementIDToRegionID *el_to_reg_map=NULL)
unsigned int size() const
Returns size of the container. This is independent of the allocated space.
Definition: sys_vector.hh:401
unsigned int n_entities
Number of rows.
#define ASSERT(...)
Definition: global_defs.h:121
Definition: system.hh:72
#define xprintf(...)
Definition: system.hh:100
#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
Region add_region(unsigned int id, const std::string &label, unsigned int dim)
Definition: region.cc:97
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:32
Definition: system.hh:72
const double epsilon
Definition: mathfce.h:6
unsigned int n_all_input_elements_
Number of elements read from input.
Definition: mesh.h:317
void read_mesh(Mesh *mesh, const RegionDB::MapElementIDToRegionID *el_to_reg_map=NULL)
RegionDB region_db_
Definition: mesh.h:329
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:397
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