Hermes2D  3.0
mesh_data.cpp
1 // This file is part of Hermes2D.
2 //
3 // Hermes2D is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // Hermes2D is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Hermes2D. If not, see <http://www.gnu.org/licenses/>.
15 
16 # include "mesh_data.h"
17 
18 namespace Hermes
19 {
20  namespace Hermes2D
21  {
22  MeshData::MeshData(const std::string &mesh_file) : mesh_file_(mesh_file)
23  {
24  }
25 
27 
28  MeshData::MeshData(const MeshData &m) : mesh_file_(m.mesh_file_), n_vert(m.n_vert), n_el(m.n_el), n_bdy(m.n_bdy), n_curv(m.n_curv), n_ref(m.n_ref)
29  {
30  vars_ = m.vars_;
31 
32  x_vertex = m.x_vertex;
33  y_vertex = m.y_vertex;
34 
35  en1 = m.en1; en2 = m.en2; en3 = m.en3; en4 = m.en4;
36  e_mtl = m.e_mtl;
37 
39  bdy_type = m.bdy_type;
40 
46 
47  ref_elt = m.ref_elt;
48  ref_type = m.ref_type;
49  }
50 
52  {
53  assert(&m != this);
54 
55  mesh_file_ = m.mesh_file_;
56  vars_ = m.vars_;
57 
58  n_vert = m.n_vert;
59  n_el = m.n_el;
60  n_bdy = m.n_bdy;
61  n_curv = m.n_curv;
62  n_ref = m.n_ref;
63 
64  x_vertex = m.x_vertex;
65  y_vertex = m.y_vertex;
66 
67  en1 = m.en1; en2 = m.en2; en3 = m.en3; en4 = m.en4;
68  e_mtl = m.e_mtl;
69 
71  bdy_type = m.bdy_type;
72 
78 
79  ref_elt = m.ref_elt;
80  ref_type = m.ref_type;
81 
82  return *this;
83  }
84 
85  void MeshData::strip(std::string& str)
86  {
87  std::string temp;
88 
89  // Remove comments
90  if (str.find('#') != str.npos)
91  str.erase(str.find('#'));
92 
93  // Remove brackets, commas and unnecessary tab spaces
94  for (size_t i = 0; i < str.length(); i++)
95  {
96  //if(str[i] != ' ' && str[i] != '\t' && str[i] != '[' && str[i] != ']' && str[i] != '{' && str[i] != '}' && str[i] != '"')
97  if (str[i] != '\t' && str[i] != '[' && str[i] != ']' && str[i] != '{' && str[i] != '}')
98  {
99  if (str[i] == ',' || str[i] == ';')
100  temp.append("\t");
101  else if (str[i] == '=')
102  temp.append("=\t");
103  else
104  temp.append(1, str[i]);
105  }
106  }
107 
108  str.assign(temp);
109  temp.clear();
110 
111  // Remove leading whitespaces
112  str.erase(0, str.find_first_not_of("\t "));
113 
114  // Remove trailing whitespaces
115  if (str.find_last_of("\t ") == (str.size() - 1))
116  str.erase(str.find_last_not_of("\t ") + 1);
117 
118  // Remove unnecessary blank spaces
119  for (size_t i = 0; i < str.length(); i++)
120  {
121  if (str[i] == ' ')
122  {
123  if (str[i + 1] != ' ' && str[i + 1] != '\t' && str[i + 1] != '=' && str[i - 1] != ' ' && str[i - 1] != '\t')
124  temp.append(1, ';'); // Meaningful blank spaces are temporarily replaced with ';'
125  }
126  else
127  temp.append(1, str[i]);
128  }
129 
130  str.assign(temp);
131  }
132 
133  std::string MeshData::restore(std::string &str)
134  {
135  std::string temp;
136 
137  for (size_t i = 0; i < str.length(); i++)
138  {
139  if (str[i] != '"')
140  {
141  if (str[i] == ';')
142  temp.append(1, ' ');
143  else
144  temp.append(1, str[i]);
145  }
146  }
147 
148  str.assign(temp);
149  return temp;
150  }
151 
153  {
154  int dummy_int;
155  double dummy_dbl;
156 
157  std::ifstream inFile(mesh_file_.c_str());
158  std::string line, word, temp_word, next_word;
159 
160  int counter(0);
161  bool isVert(false), isElt(false), isBdy(false), isCurv(false), isRef(false), isVar(false);
162 
163  while (std::getline(inFile, line))
164  {
165  // Remove all comments, unnecessary blank spaces, commas and paranthesis
166  strip(line);
167 
168  if (line.find_first_not_of("\t ") != line.npos)
169  {
170  std::istringstream stream(line);
171  stream >> word;
172 
173  if (*word.rbegin() == '=')
174  {
175  word.erase(word.size() - 1);
176 
177  if (word == "vertices")
178  {
179  isVert = true;
180  isElt = false; isBdy = false; isCurv = false; isRef = false; isVar = false;
181  counter = -1;
182  }
183  else if (word == "elements")
184  {
185  isElt = true;
186  isVert = false; isBdy = false; isCurv = false; isRef = false; isVar = false;
187  counter = -1;
188  }
189  else if (word == "boundaries")
190  {
191  isBdy = true;
192  isVert = false; isElt = false; isCurv = false; isRef = false; isVar = false;
193  counter = -1;
194  }
195  else if (word == "curves")
196  {
197  isCurv = true;
198  isVert = false; isElt = false; isBdy = false; isRef = false; isVar = false;
199  counter = -1;
200  }
201  else if (word == "refinements")
202  {
203  isRef = true;
204  isVert = false; isElt = false; isBdy = false; isCurv = false; isVar = false;
205  counter = -1;
206  }
207  else
208  {
209  isVar = true;
210  isVert = false; isElt = false; isBdy = false; isCurv = false; isRef = false;
211  counter = -1;
212 
213  temp_word = restore(word);
214  }
215  }
216 
217  if (counter == -1)
218  counter = 0;
219  else
220  {
221  if (isVert)
222  {
223  std::istringstream istr(word);
224 
225  if (!(istr >> dummy_dbl))
226  x_vertex.push_back(atof(vars_[restore(word)][0].c_str()));
227  else
228  x_vertex.push_back(atof(word.c_str()));
229 
230  ++counter;
231  }
232  if (isElt)
233  {
234  std::istringstream istr(word);
235 
236  if (!(istr >> dummy_int))
237  en1.push_back(atoi(vars_[restore(word)][0].c_str()));
238  else
239  en1.push_back(atoi(word.c_str()));
240 
241  ++counter;
242  }
243  else if (isBdy)
244  {
245  std::istringstream istr(word);
246 
247  if (!(istr >> dummy_dbl))
248  bdy_first.push_back(atof(vars_[restore(word)][0].c_str()));
249  else
250  bdy_first.push_back(atof(word.c_str()));
251 
252  ++counter;
253  }
254  else if (isCurv)
255  {
256  std::istringstream istr(word);
257 
258  if (!(istr >> dummy_int))
259  curv_first.push_back(atoi(vars_[restore(word)][0].c_str()));
260  else
261  curv_first.push_back(atoi(word.c_str()));
262 
263  ++counter;
264  }
265  else if (isRef)
266  {
267  std::istringstream istr(word);
268 
269  if (!(istr >> dummy_int))
270  ref_elt.push_back(atoi(vars_[restore(word)][0].c_str()));
271  else
272  ref_elt.push_back(atoi(word.c_str()));
273 
274  ++counter;
275  }
276  else if (isVar)
277  {
278  vars_[temp_word].push_back(restore(word));
279  ++counter;
280  }
281  }
282 
283  if (isVert)
284  {
285  while (stream >> word)
286  {
287  std::istringstream istr(word);
288 
289  if (counter % 2 == 0)
290  {
291  if (!(istr >> dummy_dbl))
292  x_vertex.push_back(atof(vars_[restore(word)][0].c_str()));
293  else
294  x_vertex.push_back(atof(word.c_str()));
295  }
296  else
297  {
298  if (!(istr >> dummy_dbl))
299  y_vertex.push_back(atof(vars_[restore(word)][0].c_str()));
300  else
301  y_vertex.push_back(atof(word.c_str()));
302  }
303 
304  ++counter;
305  }
306  }
307  else if (isElt)
308  {
309  while (stream >> word)
310  {
311  std::istringstream istr(word);
312 
313  if (counter % 5 == 0)
314  {
315  if (!(istr >> dummy_int))
316  en1.push_back(atoi(vars_[restore(word)][0].c_str()));
317  else
318  en1.push_back(atoi(word.c_str()));
319  }
320  else if (counter % 5 == 1)
321  {
322  if (!(istr >> dummy_int))
323  en2.push_back(atoi(vars_[restore(word)][0].c_str()));
324  else
325  en2.push_back(atoi(word.c_str()));
326  }
327  else if (counter % 5 == 2)
328  {
329  if (!(istr >> dummy_int))
330  en3.push_back(atoi(vars_[restore(word)][0].c_str()));
331  else
332  en3.push_back(atoi(word.c_str()));
333  }
334  else if (counter % 5 == 3)
335  {
336  if (!(istr >> dummy_int))
337  {
338  en4.push_back(-1);
339  e_mtl.push_back(restore(word));
340 
341  ++counter;
342  }
343  else
344  en4.push_back(atoi(word.c_str()));
345  }
346  else
347  e_mtl.push_back(restore(word));
348 
349  ++counter;
350  }
351  }
352  else if (isBdy)
353  {
354  while (stream >> word)
355  {
356  std::istringstream istr(word);
357 
358  if (counter % 3 == 0)
359  {
360  if (!(istr >> dummy_int))
361  bdy_first.push_back(atoi(vars_[restore(word)][0].c_str()));
362  else
363  bdy_first.push_back(atoi(word.c_str()));
364  }
365  else if (counter % 3 == 1)
366  {
367  if (!(istr >> dummy_int))
368  bdy_second.push_back(atoi(vars_[restore(word)][0].c_str()));
369  else
370  bdy_second.push_back(atoi(word.c_str()));
371  }
372  else
373  bdy_type.push_back(restore(word));
374 
375  ++counter;
376  }
377  }
378  else if (isCurv)
379  {
380  while (stream >> word)
381  {
382  std::istringstream istr(word);
383 
384  if (counter % 5 == 0)
385  {
386  if (!(istr >> dummy_int))
387  curv_first.push_back(atoi(vars_[restore(word)][0].c_str()));
388  else
389  curv_first.push_back(atoi(word.c_str()));
390  }
391  else if (counter % 5 == 1)
392  {
393  if (!(istr >> dummy_int))
394  curv_second.push_back(atoi(vars_[restore(word)][0].c_str()));
395  else
396  curv_second.push_back(atoi(word.c_str()));
397  }
398  else if (counter % 5 == 2)
399  {
400  if (istr >> dummy_dbl)
401  {
402  curv_third.push_back(atof(word.c_str()));
403  }
404  else
405  {
406  curv_third.push_back(atof(vars_[restore(word)][0].c_str()));
407  }
408 
409  stream >> next_word;
410 
411  if (next_word == "")
412  {
413  curv_nurbs.push_back(false);
414 
415  curv_inner_pts.push_back("none");
416  curv_knots.push_back("none");
417 
418  counter += 2;
419  }
420  else
421  {
422  curv_nurbs.push_back(true);
423  }
424  }
425  else if (counter % 5 == 3)
426  {
427  curv_inner_pts.push_back(restore(next_word));
428  curv_knots.push_back(restore(word));
429  ++counter;
430  }
431 
432  ++counter;
433  }
434  }
435  else if (isRef)
436  {
437  while (stream >> word)
438  {
439  std::istringstream istr(word);
440 
441  if (counter % 2 == 0)
442  {
443  if (!(istr >> dummy_int))
444  ref_elt.push_back(atoi(vars_[restore(word)][0].c_str()));
445  else
446  ref_elt.push_back(atoi(word.c_str()));
447  }
448  else
449  {
450  if (!(istr >> dummy_int))
451  ref_type.push_back(atoi(vars_[restore(word)][0].c_str()));
452  else
453  ref_type.push_back(atoi(word.c_str()));
454  }
455 
456  ++counter;
457  }
458  }
459  else if (isVar)
460  {
461  while (stream >> word)
462  {
463  vars_[temp_word].push_back(restore(word));
464  ++counter;
465  }
466  }
467  }
468  }
469 
470  assert(x_vertex.size() == y_vertex.size());
471  n_vert = x_vertex.size();
472 
473  assert(en1.size() == en2.size());
474  assert(en2.size() == en3.size());
475  assert(en3.size() == en4.size());
476  assert(en4.size() == e_mtl.size());
477  n_el = en1.size();
478 
479  assert(bdy_first.size() == bdy_second.size());
480  assert(bdy_first.size() == bdy_type.size());
481  n_bdy = bdy_first.size();
482 
483  assert(curv_first.size() == curv_second.size());
484 
485  n_curv = curv_first.size();
486 
487  assert(ref_elt.size() == ref_type.size());
488  n_ref = ref_elt.size();
489  }
490  }
491 }
std::vector< std::string > e_mtl
Element markers – single word strings.
Definition: mesh_data.h:82
void parse_mesh(void)
This function parses a given input mesh file line by line and extracts the necessary information into...
Definition: mesh_data.cpp:152
Definition: adapt.h:24
std::vector< int > ref_elt
List of elements to be refined.
Definition: mesh_data.h:107
int n_ref
Number of elements with specified refinements.
Definition: mesh_data.h:65
std::map< std::string, std::vector< std::string > > vars_
Map for storing variables in input mesh file.
Definition: mesh_data.h:54
MeshData & operator=(const MeshData &m)
MeshData Assignment Operator.
Definition: mesh_data.cpp:51
std::vector< int > curv_second
Second node of a curved edge.
Definition: mesh_data.h:94
int n_vert
Number of vertices.
Definition: mesh_data.h:57
std::vector< bool > curv_nurbs
Nurbs Indicator. True if curve is modeled with NURBS. False if it is a circular arc.
Definition: mesh_data.h:104
int n_el
Number of elements.
Definition: mesh_data.h:59
std::vector< std::string > bdy_type
Boundary name.
Definition: mesh_data.h:89
Class to stored 2d mesh parameters. The MeshData class organizes all the necessary data structures re...
Definition: mesh_data.h:40
std::vector< int > en4
Nodes with local node number 4. Only for quadrilateral elements. For triangular elements it is set to...
Definition: mesh_data.h:79
int n_curv
Number of curved edges (including NURBS curves)
Definition: mesh_data.h:63
std::vector< int > en3
Nodes with local node number 3.
Definition: mesh_data.h:77
std::vector< int > en2
Nodes with local node number 2.
Definition: mesh_data.h:75
std::vector< int > ref_type
List of element refinement type.
Definition: mesh_data.h:109
MeshData(const std::string &mesh_file)
MeshData Constructor.
Definition: mesh_data.cpp:22
int n_bdy
Number of boundary edges.
Definition: mesh_data.h:61
std::vector< std::string > curv_inner_pts
Name of the list of the control points and weights of a NURBS curve. Set to "none" for a circular arc...
Definition: mesh_data.h:100
std::vector< std::string > curv_knots
Name of the list of knot vectors of a NURBS curve. Set to "none" for a circular arc.
Definition: mesh_data.h:102
std::vector< int > bdy_first
First node of a boundary edge.
Definition: mesh_data.h:85
std::vector< int > en1
Nodes with local node number 1.
Definition: mesh_data.h:73
std::vector< double > y_vertex
y-coordinate of the vertices
Definition: mesh_data.h:70
std::vector< double > x_vertex
x-coordinate of the vertices
Definition: mesh_data.h:68
::xsd::cxx::tree::string< char, simple_type > string
C++ type corresponding to the string XML Schema built-in type.
std::vector< int > bdy_second
Second node of a boundary edge.
Definition: mesh_data.h:87
~MeshData()
MeshData Destructor.
Definition: mesh_data.cpp:26
std::vector< int > curv_first
First node of a curved edge.
Definition: mesh_data.h:92
std::vector< double > curv_third
Third entry of a curve specification. Angle for a circular arc and degree for a NURBS curve...
Definition: mesh_data.h:97