Hermes2D  2.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  std::vector<std::string> varlist;
155 
156  int dummy_int;
157  double dummy_dbl;
158 
159  std::ifstream inFile(mesh_file_.c_str());
160  std::string line, word, temp_word, next_word;
161 
162  int counter(0);
163  bool isVert(false), isElt(false), isBdy(false), isCurv(false), isRef(false), isVar(false);
164 
165  while (std::getline(inFile,line))
166  {
167  // Remove all comments, unnecessary blank spaces, commas and paranthesis
168  strip(line);
169 
170  if(line.find_first_not_of("\t ") != line.npos)
171  {
172  std::istringstream stream(line);
173  stream >> word;
174 
175  if(*word.rbegin() == '=')
176  {
177  word.erase(word.size() - 1);
178 
179  if(word == "vertices")
180  {
181  isVert = true;
182  isElt = false; isBdy = false; isCurv = false; isRef = false; isVar = false;
183  counter = -1;
184  }
185  else if(word == "elements")
186  {
187  isElt = true;
188  isVert = false; isBdy = false; isCurv = false; isRef = false; isVar = false;
189  counter = -1;
190  }
191  else if(word == "boundaries")
192  {
193  isBdy = true;
194  isVert = false; isElt = false; isCurv = false; isRef = false; isVar = false;
195  counter = -1;
196  }
197  else if(word == "curves")
198  {
199  isCurv = true;
200  isVert = false; isElt = false; isBdy = false; isRef = false; isVar = false;
201  counter = -1;
202  }
203  else if(word == "refinements")
204  {
205  isRef = true;
206  isVert = false; isElt = false; isBdy = false; isCurv = false; isVar = false;
207  counter = -1;
208  }
209  else
210  {
211  isVar = true;
212  isVert = false; isElt = false; isBdy = false; isCurv = false; isRef = false;
213  counter = -1;
214 
215  temp_word = restore(word);
216  }
217  }
218 
219  if(counter == -1)
220  counter = 0;
221  else
222  {
223  if(isVert)
224  {
225  std::istringstream istr(word);
226 
227  if(!(istr >> dummy_dbl))
228  x_vertex.push_back(atof(vars_[restore(word)][0].c_str()));
229  else
230  x_vertex.push_back(atof(word.c_str()));
231 
232  ++counter;
233  }
234  if(isElt)
235  {
236  std::istringstream istr(word);
237 
238  if(!(istr >> dummy_int))
239  en1.push_back(atoi(vars_[restore(word)][0].c_str()));
240  else
241  en1.push_back(atoi(word.c_str()));
242 
243  ++counter;
244  }
245  else if(isBdy)
246  {
247  std::istringstream istr(word);
248 
249  if(!(istr >> dummy_dbl))
250  bdy_first.push_back(atof(vars_[restore(word)][0].c_str()));
251  else
252  bdy_first.push_back(atof(word.c_str()));
253 
254  ++counter;
255  }
256  else if(isCurv)
257  {
258  std::istringstream istr(word);
259 
260  if(!(istr >> dummy_int))
261  curv_first.push_back(atoi(vars_[restore(word)][0].c_str()));
262  else
263  curv_first.push_back(atoi(word.c_str()));
264 
265  ++counter;
266  }
267  else if(isRef)
268  {
269  std::istringstream istr(word);
270 
271  if(!(istr >> dummy_int))
272  ref_elt.push_back(atoi(vars_[restore(word)][0].c_str()));
273  else
274  ref_elt.push_back(atoi(word.c_str()));
275 
276  ++counter;
277  }
278  else if(isVar)
279  {
280  vars_[temp_word].push_back(restore(word));
281  ++counter;
282  }
283  }
284 
285  if(isVert)
286  {
287  while (stream >> word)
288  {
289  std::istringstream istr(word);
290 
291  if(counter%2 == 0)
292  {
293  if(!(istr >> dummy_dbl))
294  x_vertex.push_back(atof(vars_[restore(word)][0].c_str()));
295  else
296  x_vertex.push_back(atof(word.c_str()));
297  }
298  else
299  {
300  if(!(istr >> dummy_dbl))
301  y_vertex.push_back(atof(vars_[restore(word)][0].c_str()));
302  else
303  y_vertex.push_back(atof(word.c_str()));
304  }
305 
306  ++counter;
307  }
308  }
309  else if(isElt)
310  {
311  while (stream >> word)
312  {
313  std::istringstream istr(word);
314 
315  if(counter%5 == 0)
316  {
317  if(!(istr >> dummy_int))
318  en1.push_back(atoi(vars_[restore(word)][0].c_str()));
319  else
320  en1.push_back(atoi(word.c_str()));
321  }
322  else if(counter%5 == 1)
323  {
324  if(!(istr >> dummy_int))
325  en2.push_back(atoi(vars_[restore(word)][0].c_str()));
326  else
327  en2.push_back(atoi(word.c_str()));
328  }
329  else if(counter%5 == 2)
330  {
331  if(!(istr >> dummy_int))
332  en3.push_back(atoi(vars_[restore(word)][0].c_str()));
333  else
334  en3.push_back(atoi(word.c_str()));
335  }
336  else if(counter%5 == 3)
337  {
338  if(!(istr >> dummy_int))
339  {
340  en4.push_back(-1);
341  e_mtl.push_back(restore(word));
342 
343  ++counter;
344  }
345  else
346  en4.push_back(atoi(word.c_str()));
347  }
348  else
349  e_mtl.push_back(restore(word));
350 
351  ++counter;
352  }
353  }
354  else if(isBdy)
355  {
356  while (stream >> word)
357  {
358  std::istringstream istr(word);
359 
360  if(counter%3 == 0)
361  {
362  if(!(istr >> dummy_int))
363  bdy_first.push_back(atoi(vars_[restore(word)][0].c_str()));
364  else
365  bdy_first.push_back(atoi(word.c_str()));
366  }
367  else if(counter%3 == 1)
368  {
369  if(!(istr >> dummy_int))
370  bdy_second.push_back(atoi(vars_[restore(word)][0].c_str()));
371  else
372  bdy_second.push_back(atoi(word.c_str()));
373  }
374  else
375  bdy_type.push_back(restore(word));
376 
377  ++counter;
378  }
379  }
380  else if(isCurv)
381  {
382  while (stream >> word)
383  {
384  std::istringstream istr(word);
385 
386  if(counter%5 == 0)
387  {
388  if(!(istr >> dummy_int))
389  curv_first.push_back(atoi(vars_[restore(word)][0].c_str()));
390  else
391  curv_first.push_back(atoi(word.c_str()));
392  }
393  else if(counter%5 == 1)
394  {
395  if(!(istr >> dummy_int))
396  curv_second.push_back(atoi(vars_[restore(word)][0].c_str()));
397  else
398  curv_second.push_back(atoi(word.c_str()));
399  }
400  else if(counter%5 == 2)
401  {
402  if(istr >> dummy_dbl)
403  {
404  curv_third.push_back(atof(word.c_str()));
405  }
406  else
407  {
408  curv_third.push_back(atof(vars_[restore(word)][0].c_str()));
409  }
410 
411  stream >> next_word;
412 
413  if(next_word == "")
414  {
415  curv_nurbs.push_back(false);
416 
417  curv_inner_pts.push_back("none");
418  curv_knots.push_back("none");
419 
420  counter += 2;
421  }
422  else
423  {
424  curv_nurbs.push_back(true);
425  }
426  }
427  else if(counter%5 == 3)
428  {
429  curv_inner_pts.push_back(restore(next_word));
430  curv_knots.push_back(restore(word));
431  ++counter;
432  }
433 
434  ++counter;
435  }
436  }
437  else if(isRef)
438  {
439  while (stream >> word)
440  {
441  std::istringstream istr(word);
442 
443  if(counter%2 == 0)
444  {
445  if(!(istr >> dummy_int))
446  ref_elt.push_back(atoi(vars_[restore(word)][0].c_str()));
447  else
448  ref_elt.push_back(atoi(word.c_str()));
449  }
450  else
451  {
452  if(!(istr >> dummy_int))
453  ref_type.push_back(atoi(vars_[restore(word)][0].c_str()));
454  else
455  ref_type.push_back(atoi(word.c_str()));
456  }
457 
458  ++counter;
459  }
460  }
461  else if(isVar)
462  {
463  while (stream >> word)
464  {
465  vars_[temp_word].push_back(restore(word));
466  ++counter;
467  }
468  }
469  }
470  }
471 
472  assert(x_vertex.size() == y_vertex.size());
473  n_vert = x_vertex.size();
474 
475  assert(en1.size() == en2.size());
476  assert(en2.size() == en3.size());
477  assert(en3.size() == en4.size());
478  assert(en4.size() == e_mtl.size());
479  n_el = en1.size();
480 
481  assert(bdy_first.size() == bdy_second.size());
482  assert(bdy_first.size() == bdy_type.size());
483  n_bdy = bdy_first.size();
484 
485  assert(curv_first.size() == curv_second.size());
486 
487  n_curv = curv_first.size();
488 
489  assert(ref_elt.size() == ref_type.size());
490  n_ref = ref_elt.size();
491  }
492  }
493 }