Flow123d  JS_before_hm-2137-g84767e588
msh_gmshreader.cc
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.cc
15  * @ingroup mesh
16  * @brief
17  * @author dalibor
18  */
19 
20 #include <istream>
21 #include <string>
22 #include <limits>
23 
24 #include "msh_gmshreader.h"
26 
27 #include "system/system.hh"
28 #include "system/tokenizer.hh"
29 #include "boost/lexical_cast.hpp"
30 
31 #include "mesh/mesh.h"
32 #include "mesh/bc_mesh.hh"
33 
34 
35 
36 using namespace std;
37 
38 
40 : BaseMeshReader(file_name)
41 {
42  tok_.set_comment_pattern( "#");
43  data_section_name_ = "$ElementData";
44  has_compatible_mesh_ = false;
45  can_have_components_ = true;
47 }
48 
49 
50 
51 GmshMeshReader::~GmshMeshReader() // Tokenizer close the file automatically
52 {}
53 
54 
55 
57  using namespace boost;
58  unsigned int n_nodes;
59  MessageOut() << "- Reading nodes...";
60  tok_.set_position( Tokenizer::Position() );
61 
62  if (! tok_.skip_to("$Nodes")) THROW(ExcMissingSection() << EI_Section("$Nodes") << EI_GMSHFile(tok_.f_name()) );
63  try {
64  tok_.next_line(false);
65  n_nodes = lexical_cast<unsigned int> (*tok_);
66  mesh->init_node_vector( n_nodes );
67  if (n_nodes == 0) THROW( ExcZeroNodes() << EI_Position(tok_.position_msg()) );
68  ++tok_; // end of line
69 
70  for (unsigned int i = 0; i < n_nodes; ++i) {
71  tok_.next_line();
72 
73  unsigned int id = lexical_cast<unsigned int> (*tok_); ++tok_; // node id
74  arma::vec3 coords; // node coordinates
75  coords(0) = lexical_cast<double> (*tok_); ++tok_;
76  coords(1) = lexical_cast<double> (*tok_); ++tok_;
77  coords(2) = lexical_cast<double> (*tok_); ++tok_;
78  ++tok_; // skip mesh size parameter
79 
80  mesh->add_node(id, coords);
81  }
82 
83  } catch (bad_lexical_cast &) {
84  THROW(ExcWrongFormat() << EI_Type("number") << EI_TokenizerMsg(tok_.position_msg()) << EI_MeshFile(tok_.f_name()) );
85  }
86  MessageOut().fmt("... {} nodes read. \n", n_nodes);
87 }
88 
89 
91  using namespace boost;
92  MessageOut() << "- Reading elements...";
93 
94  if (! tok_.skip_to("$Elements")) THROW(ExcMissingSection() << EI_Section("$Elements") << EI_GMSHFile(tok_.f_name()) );
95  try {
96  tok_.next_line(false);
97  unsigned int n_elements = lexical_cast<unsigned int> (*tok_);
98  if (n_elements == 0) THROW( ExcZeroElements() << EI_Position(tok_.position_msg()) );
99  ++tok_; // end of line
100 
101  std::vector<unsigned int> node_ids; //node_ids of elements
102  node_ids.resize(4); // maximal count of nodes
103 
104  mesh->init_element_vector(n_elements);
105 
106  for (unsigned int i = 0; i < n_elements; ++i) {
107  tok_.next_line();
108  unsigned int id = lexical_cast<unsigned int>(*tok_); ++tok_;
109 
110  //get element type: supported:
111  // 1 Line (2 nodes)
112  // 2 Triangle (3 nodes)
113  // 4 Tetrahedron (4 nodes)
114  // 15 Point (1 node)
115  unsigned int type = lexical_cast<unsigned int>(*tok_); ++tok_;
116  unsigned int dim;
117  switch (type) {
118  case 1:
119  dim = 1;
120  break;
121  case 2:
122  dim = 2;
123  break;
124  case 4:
125  dim = 3;
126  break;
127  case 15:
128  dim = 0;
129  break;
130  default:
131  dim = 0;
132  THROW(ExcUnsupportedType() << EI_ElementId(id) << EI_ElementType(type) << EI_GMSHFile(tok_.f_name()) );
133  break;
134  }
135 
136  //get number of tags (at least 2)
137  unsigned int n_tags = lexical_cast<unsigned int>(*tok_);
138  if (n_tags < 2) THROW( ExcTooManyElementTags() << EI_ElementId(id) << EI_Position(tok_.position_msg()) );
139  ++tok_;
140 
141  //get tags 1 and 2
142  unsigned int region_id = lexical_cast<unsigned int>(*tok_); ++tok_; // region_id
143  lexical_cast<unsigned int>(*tok_); ++tok_; // GMSH region number, we do not store this
144  //get remaining tags
145  unsigned int partition_id = 0;
146  if (n_tags > 2) { partition_id = lexical_cast<unsigned int>(*tok_); ++tok_; } // save partition number from the new GMSH format
147  for (unsigned int ti = 3; ti < n_tags; ti++) ++tok_; //skip remaining tags
148 
149  for (unsigned int ni=0; ni<dim+1; ++ni) { // read node ids
150  node_ids[ni] = lexical_cast<unsigned int>(*tok_);
151  ++tok_;
152  }
153  mesh->add_element(id, dim, region_id, partition_id, node_ids);
154  }
155 
156  } catch (bad_lexical_cast &) {
157  THROW(ExcWrongFormat() << EI_Type("number") << EI_TokenizerMsg(tok_.position_msg()) << EI_MeshFile(tok_.f_name()) );
158  }
159 
160  MessageOut().fmt("... {} bulk elements, {} boundary elements. \n", mesh->n_elements(), mesh->bc_mesh()->n_elements());
161 }
162 
163 
164 
166  ASSERT(mesh).error("Argument mesh is NULL.\n");
167 
168  using namespace boost;
169 
170  if (! tok_.skip_to("$PhysicalNames", "$Nodes") ) return;
171  try {
172  tok_.next_line(false);
173  unsigned int n_physicals = lexical_cast<unsigned int> (*tok_);
174  ++tok_; // end of line
175 
176  for (unsigned int i = 0; i < n_physicals; ++i) {
177  tok_.next_line();
178  // format of one line:
179  // dim physical-id physical-name
180 
181  unsigned int dim = lexical_cast<unsigned int>(*tok_); ++tok_;
182  unsigned int id = lexical_cast<unsigned int>(*tok_); ++tok_;
183  string name = *tok_; ++tok_;
184  mesh->add_physical_name( dim, id, name );
185  }
186 
187  } catch (bad_lexical_cast &) {
188  THROW(ExcWrongFormat() << EI_Type("number") << EI_TokenizerMsg(tok_.position_msg()) << EI_MeshFile(tok_.f_name()) );
189  }
190 
191 }
192 
193 
194 // Is assumed to be called just after tok.skip_to("..")
195 // reads the header from the tokenizer @p tok and return it as the second parameter
197  using namespace boost;
198  try {
199  // string tags
200  tok_.next_line(false);
201  unsigned int n_str = lexical_cast<unsigned int>(*tok_); ++tok_;
202  head.field_name="";
203  head.interpolation_scheme = "";
204  if (n_str > 0) {
205  tok_.next_line(); n_str--;
206  head.field_name= *tok_; ++tok_; // unquoted by tokenizer if needed
207  }
208  if (n_str > 0) {
209  tok_.next_line(); n_str--;
210  head.interpolation_scheme = *tok_; ++tok_;
211  }
212  for(;n_str>0;n_str--) tok_.next_line(false); // skip possible remaining tags
213 
214  //real tags
215  tok_.next_line();
216  unsigned int n_real = lexical_cast<unsigned int>(*tok_); ++tok_;
217  head.time=0.0;
218  if (n_real>0) {
219  tok_.next_line(); n_real--;
220  head.time=lexical_cast<double>(*tok_); ++tok_;
221  }
222  for(;n_real>0;n_real--) tok_.next_line(false);
223 
224  // int tags
225  tok_.next_line();
226  unsigned int n_int = lexical_cast<unsigned int>(*tok_); ++tok_;
227  head.time_index=0;
228  head.n_components=1;
229  head.n_entities=0;
230  head.partition_index=0;
231  if (n_int>0) {
232  tok_.next_line(); n_int--;
233  head.time_index=lexical_cast<unsigned int>(*tok_); ++tok_;
234  }
235  if (n_int>0) {
236  tok_.next_line(); n_int--;
237  head.n_components=lexical_cast<unsigned int>(*tok_); ++tok_;
238  }
239  if (n_int>0) {
240  tok_.next_line(); n_int--;
241  head.n_entities=lexical_cast<unsigned int>(*tok_); ++tok_;
242  }
243  for(;n_int>0;n_int--) tok_.next_line(false);
244  head.position = tok_.get_position();
245  head.discretization = OutputTime::DiscreteSpace::ELEM_DATA;
246  } catch (bad_lexical_cast &) {
247  THROW(ExcWrongFormat() << EI_Type("$ElementData header") << EI_TokenizerMsg(tok_.position_msg()) << EI_MeshFile(tok_.f_name()) );
248  }
249 }
250 
251 
252 
253 void GmshMeshReader::read_element_data(ElementDataCacheBase &data_cache, MeshDataHeader actual_header, unsigned int n_components,
254  bool boundary_domain) {
255  unsigned int id, i_row;
256  unsigned int n_read = 0;
257  std::vector<int> const & el_ids = this->get_element_vector(boundary_domain);
258  vector<int>::const_iterator id_iter = el_ids.begin();
259 
260  // read @p data buffer as we have correct header with already passed time
261  // we assume that @p data buffer is big enough
262  tok_.set_position(actual_header.position);
263 
264  // read data
265  for (i_row = 0; i_row < actual_header.n_entities; ++i_row)
266  try {
267  tok_.next_line();
268  id = boost::lexical_cast<unsigned int>(*tok_); ++tok_;
269  //skip_element = false;
270  while (id_iter != el_ids.end() && *id_iter < (int)id) {
271  ++id_iter; // skip initialization of some rows in data if ID is missing
272  }
273  if (id_iter == el_ids.end()) {
274  WarningOut().fmt("In file '{}', '$ElementData' section for field '{}', time: {}.\nData ID {} not found or is not in order. Skipping rest of data.\n",
275  tok_.f_name(), actual_header.field_name, actual_header.time, id);
276  break;
277  }
278  // save data from the line if ID was found
279  if (*id_iter == (int)id) {
280  data_cache.read_ascii_data(tok_, n_components, (id_iter - el_ids.begin()) );
281  n_read++;
282  }
283  // skip the line if ID on the line < actual ID in the map el_ids
284  } catch (boost::bad_lexical_cast &) {
285  THROW(ExcWrongFormat() << EI_Type("$ElementData line") << EI_TokenizerMsg(tok_.position_msg())
286  << EI_MeshFile(tok_.f_name()) );
287  }
288  // possibly skip remaining lines after break
289  while (i_row < actual_header.n_entities) tok_.next_line(false), ++i_row;
290 
291  LogOut().fmt("time: {}; {} entities of field {} read.\n",
292  actual_header.time, n_read, actual_header.field_name);
293 }
294 
295 
296 
298 {
299  header_table_.clear();
300  MeshDataHeader header;
301  while ( !tok_.eof() ) {
302  if ( tok_.skip_to("$ElementData") ) {
303  read_data_header(header);
304  HeaderTable::iterator it = header_table_.find(header.field_name);
305 
306  if (it == header_table_.end()) { // field doesn't exists, insert new vector to map
308  vec.push_back(header);
309  header_table_[header.field_name]=vec;
310  } else if ( header.time <= it->second.back().time ) { // time is in wrong order. can't be add
311  WarningOut().fmt("Wrong time order: field '{}', time '{}', file '{}'. Skipping this '$ElementData' section.\n",
312  header.field_name, header.time, tok_.f_name() );
313  } else { // add new time step
314  it->second.push_back(header);
315  }
316  }
317  }
318 
319  tok_.set_position( Tokenizer::Position() );
320 }
321 
322 
323 
325 {
326  // check discretization, only type element_data or undefined is supported
327  if (header_query.discretization != OutputTime::DiscreteSpace::ELEM_DATA) {
328  if (header_query.discretization != OutputTime::DiscreteSpace::UNDEFINED && header_query.discretization != OutputTime::DiscreteSpace::NATIVE_DATA) {
329  WarningOut().fmt(
330  "Unsupported discretization for field '{}', time: {} and GMSH format.\nType 'ELEM_DATA' of discretization will be used.\n",
331  header_query.field_name, header_query.time);
332  }
333  header_query.discretization = OutputTime::DiscreteSpace::ELEM_DATA;
334  }
335 
336  HeaderTable::iterator table_it = header_table_.find(header_query.field_name);
337 
338  if (table_it == header_table_.end()) {
339  // no data found
340  THROW( ExcFieldNameNotFound() << EI_FieldName(header_query.field_name) << EI_MeshFile(tok_.f_name()));
341  }
342 
343  auto comp = [](double t, const MeshDataHeader &a) {
344  return t * (1.0 + 2.0*numeric_limits<double>::epsilon()) < a.time;
345  };
346 
347  std::vector<MeshDataHeader>::iterator headers_it = std::upper_bound(table_it->second.begin(),
348  table_it->second.end(),
349  header_query.time,
350  comp);
351 
352  if (headers_it == table_it->second.begin()) {
353  THROW( ExcFieldNameNotFound() << EI_FieldName(header_query.field_name)
354  << EI_MeshFile(tok_.f_name()) << EI_Time(header_query.time));
355  }
356 
357  --headers_it;
358  actual_header_ = *headers_it;
359  return actual_header_;
360 }
BaseMeshReader::actual_header_
MeshDataHeader actual_header_
Header of actual loaded data.
Definition: msh_basereader.hh:257
BaseMeshReader::tok_
Tokenizer tok_
Tokenizer used for reading ASCII file format.
Definition: msh_basereader.hh:250
Armor::vec
ArmaVec< double, N > vec
Definition: armor.hh:885
ASSERT
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library.
Definition: asserts.hh:347
element_data_cache_base.hh
GmshMeshReader::find_header
MeshDataHeader & find_header(HeaderQuery &header_query) override
Definition: msh_gmshreader.cc:324
GmshMeshReader::make_header_table
void make_header_table() override
Definition: msh_gmshreader.cc:297
bc_mesh.hh
GmshMeshReader::read_physical_names
void read_physical_names(Mesh *mesh) override
Definition: msh_gmshreader.cc:165
FilePath
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
THROW
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
std::vector< unsigned int >
boost
Definition: finite_state_filter.hpp:34
system.hh
arma::vec3
Definition: doxy_dummy_defs.hh:17
BaseMeshReader::MeshDataHeader::interpolation_scheme
std::string interpolation_scheme
Currently d'ont used.
Definition: msh_basereader.hh:123
BaseMeshReader::MeshDataHeader::n_components
unsigned int n_components
Number of values on one row.
Definition: msh_basereader.hh:129
msh_gmshreader.h
BaseMeshReader::get_element_vector
const std::vector< int > & get_element_vector(bool boundary_domain)
Definition: msh_basereader.cc:82
GmshMeshReader::read_element_data
void read_element_data(ElementDataCacheBase &data_cache, MeshDataHeader actual_header, unsigned int n_components, bool boundary_domain) override
Definition: msh_gmshreader.cc:253
BaseMeshReader::MeshDataHeader::discretization
OutputTime::DiscreteSpace discretization
Flag marks input discretization of data of VTK file.
Definition: msh_basereader.hh:139
Mesh::bc_mesh
BCMesh * bc_mesh() const override
Implement MeshBase::bc_mesh(), getter of boundary mesh.
Definition: mesh.h:559
BaseMeshReader::HeaderQuery::field_name
std::string field_name
Name of field.
Definition: msh_basereader.hh:135
BaseMeshReader
Definition: msh_basereader.hh:58
LogOut
#define LogOut()
Macro defining 'log' record of log.
Definition: logger.hh:281
BaseMeshReader::MeshDataHeader::field_name
std::string field_name
Name of field.
Definition: msh_basereader.hh:121
GmshMeshReader::read_nodes
void read_nodes(Mesh *mesh)
Definition: msh_gmshreader.cc:56
BaseMeshReader::can_have_components_
bool can_have_components_
Definition: msh_basereader.hh:264
ElementDataCacheBase
Definition: element_data_cache_base.hh:33
mesh.h
Mesh::add_element
void add_element(unsigned int elm_id, unsigned int dim, unsigned int region_id, unsigned int partition_id, std::vector< unsigned int > node_ids)
Add new element of given id to mesh.
Definition: mesh.cc:1088
Input::Type
Definition: balance.hh:41
BaseMeshReader::MeshDataHeader
Definition: msh_basereader.hh:100
MeshBase::init_node_vector
void init_node_vector(unsigned int size)
Initialize node_vec_, set size.
Definition: mesh.cc:1146
GmshMeshReader::~GmshMeshReader
virtual ~GmshMeshReader()
Definition: msh_gmshreader.cc:51
ElementDataCacheBase::read_ascii_data
virtual void read_ascii_data(Tokenizer &tok, unsigned int n_components, unsigned int i_row)=0
Mesh
Definition: mesh.h:355
MeshBase::init_element_vector
void init_element_vector(unsigned int size)
Initialize element_vec_, set size and reset counters of boundary and bulk elements.
Definition: mesh.cc:1136
BaseMeshReader::HeaderQuery::discretization
OutputTime::DiscreteSpace discretization
Flag determinate type of discrete of Field (typically is used for native data of VTK)
Definition: msh_basereader.hh:139
WarningOut
#define WarningOut()
Macro defining 'warning' record of log.
Definition: logger.hh:278
BaseMeshReader::MeshDataHeader::partition_index
unsigned int partition_index
Currently d'ont used.
Definition: msh_basereader.hh:133
std
Definition: doxy_dummy_defs.hh:5
BaseMeshReader::MeshDataHeader::position
Tokenizer::Position position
Position of data in mesh file.
Definition: msh_basereader.hh:135
Mesh::add_node
void add_node(unsigned int node_id, arma::vec3 coords)
Add new node of given id and coordinates to mesh.
Definition: mesh.cc:1080
BaseMeshReader::MeshDataHeader::n_entities
unsigned int n_entities
Number of rows.
Definition: msh_basereader.hh:131
BaseMeshReader::MeshDataHeader::time_index
unsigned int time_index
Currently d'ont used.
Definition: msh_basereader.hh:127
GmshMeshReader::header_table_
HeaderTable header_table_
Table with data of ElementData headers.
Definition: msh_gmshreader.h:116
MeshBase::n_elements
unsigned int n_elements() const
Definition: mesh.h:111
BaseMeshReader::data_section_name_
std::string data_section_name_
Store name of field data section specify for type of mesh file.
Definition: msh_basereader.hh:244
GmshMeshReader::read_data_header
void read_data_header(MeshDataHeader &head)
Definition: msh_gmshreader.cc:196
BaseMeshReader::MeshDataHeader::time
double time
Time of field data (used only for GMSH reader)
Definition: msh_basereader.hh:125
BaseMeshReader::HeaderQuery::time
double time
Time of field data (used only for GMSH and PVD reader)
Definition: msh_basereader.hh:138
BaseMeshReader::has_compatible_mesh_
bool has_compatible_mesh_
Definition: msh_basereader.hh:241
BaseMeshReader::HeaderQuery
Definition: msh_basereader.hh:132
Mesh::add_physical_name
void add_physical_name(unsigned int dim, unsigned int id, std::string name)
Add new node of given id and coordinates to mesh.
Definition: mesh.cc:1075
tokenizer.hh
MessageOut
#define MessageOut()
Macro defining 'message' record of log.
Definition: logger.hh:275
GmshMeshReader::GmshMeshReader
GmshMeshReader(const FilePath &file_name)
Definition: msh_gmshreader.cc:39
GmshMeshReader::read_elements
void read_elements(Mesh *mesh)
Definition: msh_gmshreader.cc:90