Flow123d  last_with_con_2.0.0-4-g42e6930
mesh.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 mesh.cc
15  * @ingroup mesh
16  * @brief Mesh construction
17  */
18 
19 #include <unistd.h>
20 #include <set>
21 
22 
23 #include "system/system.hh"
25 #include "input/input_type.hh"
26 #include "system/sys_profiler.hh"
27 #include "la/distribution.hh"
28 
29 #include <boost/tokenizer.hpp>
30 #include "boost/lexical_cast.hpp"
31 #include <boost/make_shared.hpp>
32 
33 #include "mesh/mesh.h"
34 #include "mesh/ref_element.hh"
35 
36 // think about following dependencies
37 #include "mesh/boundaries.h"
38 #include "mesh/accessors.hh"
39 #include "mesh/partitioning.hh"
40 
41 #include "mesh/bih_tree.hh"
42 
43 
44 //TODO: sources, concentrations, initial condition and similarly boundary conditions should be
45 // instances of a Element valued field
46 // concentrations is in fact reimplemented in transport REMOVE it HERE
47 
48 // After removing non-geometrical things from mesh, this should be part of mash initializing.
49 #include "mesh/msh_gmshreader.h"
50 #include "mesh/region.hh"
51 
52 #define NDEF -1
53 
54 namespace IT = Input::Type;
55 
56 
58  return IT::Record("Mesh","Record with mesh related data." )
59  .allow_auto_conversion("mesh_file")
61  "Input file with mesh description.")
63  "List of additional region and region set definitions not contained in the mesh.\n"
64  "There are three region sets implicitly defined:\n\n"
65  " - ALL (all regions of the mesh)\n - .BOUNDARY (all boundary regions)\n - and BULK (all bulk regions)")
66  .declare_key("partitioning", Partitioning::get_input_type(), IT::Default("\"any_neighboring\""), "Parameters of mesh partitioning algorithms.\n" )
67  .declare_key("print_regions", IT::Bool(), IT::Default("false"), "If true, print table of all used regions.")
68  .close();
69 }
70 
71 
72 
73 const unsigned int Mesh::undef_idx;
74 
75 Mesh::Mesh(const std::string &input_str, MPI_Comm comm)
76 :comm_(comm),
77  row_4_el(nullptr),
78  el_4_loc(nullptr),
79  el_ds(nullptr)
80 {
81 
84 
86 }
87 
88 
89 
91 : in_record_(in_record),
92  comm_(com),
93  row_4_el(nullptr),
94  el_4_loc(nullptr),
95  el_ds(nullptr)
96 {
98 }
99 
100 
101 
103 {
104 
105  n_insides = NDEF;
106  n_exsides = NDEF;
107  n_sides_ = NDEF;
108 
109  // number of element of particular dimension
110  n_lines = 0;
111  n_triangles = 0;
112  n_tetrahedras = 0;
113 
114  for (int d=0; d<3; d++) max_edge_sides_[d] = 0;
115 
116  // Initialize numbering of nodes on sides.
117  // This is temporary solution, until class Element is templated
118  // by dimension. Then we can replace Mesh::side_nodes by
119  // RefElement<dim>::side_nodes.
120 
121  // indices of side nodes in element node array
122  // Currently this is made ad libitum
123  // with some ordering here we can get sides with correct orientation.
124  // This speedup normal calculation.
125 
126  side_nodes.resize(3); // three side dimensions
127  for(int i=0; i < 3; i++) {
128  side_nodes[i].resize(i+2); // number of sides
129  for(int j=0; j < i+2; j++)
130  side_nodes[i][j].resize(i+1);
131  }
132 
133  for (unsigned int sid=0; sid<RefElement<1>::n_sides; sid++)
134  for (unsigned int nid=0; nid<RefElement<1>::n_nodes_per_side; nid++)
135  side_nodes[0][sid][nid] = RefElement<1>::side_nodes[sid][nid];
136 
137  for (unsigned int sid=0; sid<RefElement<2>::n_sides; sid++)
138  for (unsigned int nid=0; nid<RefElement<2>::n_nodes_per_side; nid++)
139  side_nodes[1][sid][nid] = RefElement<2>::side_nodes[sid][nid];
140 
141  for (unsigned int sid=0; sid<RefElement<3>::n_sides; sid++)
142  for (unsigned int nid=0; nid<RefElement<3>::n_nodes_per_side; nid++)
143  side_nodes[2][sid][nid] = RefElement<3>::side_nodes[sid][nid];
144 }
145 
146 
148  for(Edge &edg : this->edges)
149  if (edg.side_) delete[] edg.side_;
150 
151  FOR_ELEMENTS( this, ele ) {
152  if (ele->node) delete[] ele->node;
153  if (ele->edge_idx_) delete[] ele->edge_idx_;
154  if (ele->permutation_idx_) delete[] ele->permutation_idx_;
155  if (ele->boundary_idx_) delete[] ele->boundary_idx_;
156  }
157 
158  for(unsigned int idx=0; idx < this->bc_elements.size(); idx++) {
159  Element *ele=&(bc_elements[idx]);
160  if (ele->node) delete[] ele->node;
161  if (ele->edge_idx_) delete[] ele->edge_idx_;
162  if (ele->permutation_idx_) delete[] ele->permutation_idx_;
163  if (ele->boundary_idx_) delete[] ele->boundary_idx_;
164  }
165 
166  if (row_4_el != nullptr) delete[] row_4_el;
167  if (el_4_loc != nullptr) delete[] el_4_loc;
168  if (el_ds != nullptr) delete el_ds;
169 }
170 
171 
172 unsigned int Mesh::n_sides()
173 {
174  if (n_sides_ == NDEF) {
175  n_sides_=0;
176  FOR_ELEMENTS(this, ele) n_sides_ += ele->n_sides();
177  }
178  return n_sides_;
179 }
180 
181 unsigned int Mesh::n_corners() {
182  unsigned int li, count = 0;
183  FOR_ELEMENTS(this, ele) {
184  FOR_ELEMENT_NODES(ele, li) {
185  count++;
186  }
187  }
188  return count;
189 }
190 
192  return part_.get();
193 }
194 
195 
196 //=============================================================================
197 // COUNT ELEMENT TYPES
198 //=============================================================================
199 
201  FOR_ELEMENTS(this, elm)
202  switch (elm->dim()) {
203  case 1:
204  n_lines++;
205  break;
206  case 2:
207  n_triangles++;
208  break;
209  case 3:
210  n_tetrahedras++;
211  break;
212  }
213 }
214 
215 
216 void Mesh::read_gmsh_from_stream(istream &in) {
217 
218  START_TIMER("Reading mesh - from_stream");
219 
220  GmshMeshReader reader(in);
221  reader.read_physical_names(this);
222  reader.read_mesh(this);
223  setup_topology();
224  //close region_db_.
225  region_db_.close();
226 }
227 
228 
229 
231  START_TIMER("Reading mesh - init_from_input");
232 
233  Input::Array region_list;
234  // read raw mesh, add regions from GMSH file
235  GmshMeshReader reader( in_record_.val<FilePath>("mesh_file") );
236  reader.read_physical_names(this);
237  // create regions from input
238  if (in_record_.opt_val("regions", region_list)) {
239  this->read_regions_from_input(region_list);
240  }
241  reader.read_mesh(this);
242  // possibly add implicit_boundary region.
243  setup_topology();
244  // finish mesh initialization
245  this->check_and_finish();
246 }
247 
248 
249 
250 
252  for (auto elem_to_region : map) {
253  ElementIter ele = this->element.find_id(elem_to_region.first);
254  ele->region_idx_ = region_db_.get_region( elem_to_region.second, ele->dim() );
256  }
257 }
258 
259 
260 
262  START_TIMER("MESH - setup topology");
263 
265 
266  // check mesh quality
267  FOR_ELEMENTS(this, ele)
268  if (ele->quality_measure_smooth() < 0.001) WarningOut().fmt("Bad quality (<0.001) of the element {}.\n", ele.id());
269 
274 
275  part_ = std::make_shared<Partitioning>(this, in_record_.val<Input::Record>("partitioning") );
276 
277  // create parallel distribution and numbering of elements
278  int *id_4_old = new int[element.size()];
279  int i = 0;
280  FOR_ELEMENTS(this, ele)
281  id_4_old[i++] = ele.index();
282  part_->id_maps(element.size(), id_4_old, el_ds, el_4_loc, row_4_el);
283 
284  delete[] id_4_old;
285 }
286 
287 
288 //
290 {
291 
292  n_insides = 0;
293  n_exsides = 0;
294  FOR_SIDES(this, sde ) {
295  if (sde->is_external()) n_exsides++;
296  else n_insides++;
297  }
298 }
299 
300 
301 
303  // for each node we make a list of elements that use this node
304  node_elements.resize(node_vector.size());
305 
306  FOR_ELEMENTS( this, e )
307  for (unsigned int n=0; n<e->n_nodes(); n++)
308  node_elements[node_vector.index(e->node[n])].push_back(e->index());
309 
310  for (vector<vector<unsigned int> >::iterator n=node_elements.begin(); n!=node_elements.end(); n++)
311  stable_sort(n->begin(), n->end());
312 }
313 
314 
315 void Mesh::intersect_element_lists(vector<unsigned int> const &nodes_list, vector<unsigned int> &intersection_element_list)
316 {
317  if (nodes_list.size() == 0) {
318  intersection_element_list.clear();
319  } else if (nodes_list.size() == 1) {
320  intersection_element_list = node_elements[ nodes_list[0] ];
321  } else {
322  vector<unsigned int>::const_iterator it1=nodes_list.begin();
324  intersection_element_list.resize( node_elements[*it1].size() ); // make enough space
325 
326  it1=set_intersection(
327  node_elements[*it1].begin(), node_elements[*it1].end(),
328  node_elements[*it2].begin(), node_elements[*it2].end(),
329  intersection_element_list.begin());
330  intersection_element_list.resize(it1-intersection_element_list.begin()); // resize to true size
331 
332  for(;it2<nodes_list.end();++it2) {
333  it1=set_intersection(
334  intersection_element_list.begin(), intersection_element_list.end(),
335  node_elements[*it2].begin(), node_elements[*it2].end(),
336  intersection_element_list.begin());
337  intersection_element_list.resize(it1-intersection_element_list.begin()); // resize to true size
338  }
339  }
340 }
341 
342 
344  unsigned int dim, unsigned int &element_idx) {
345  bool is_neighbour = false;
346 
347  vector<unsigned int>::iterator e_dest=element_list.begin();
348  for( vector<unsigned int>::iterator ele = element_list.begin(); ele!=element_list.end(); ++ele)
349  if (elements[*ele].dim() == dim) { // keep only indexes of elements of same dimension
350  *e_dest=*ele;
351  ++e_dest;
352  } else if (elements[*ele].dim() == dim-1) { // get only first element of lower dimension
353  if (is_neighbour) xprintf(UsrErr, "Too matching elements id: %d and id: %d in the same mesh.\n",
354  elements(*ele).id(), elements(element_idx).id() );
355 
356  is_neighbour = true;
357  element_idx = *ele;
358  }
359  element_list.resize( e_dest - element_list.begin());
360  return is_neighbour;
361 }
362 
364  // check if nodes lists match (this is slow and will be faster only when we convert whole mesh into hierarchical design like in deal.ii)
365  unsigned int ni=0;
366  while ( ni < si->n_nodes()
367  && find(side_nodes.begin(), side_nodes.end(), node_vector.index( si->node(ni) ) ) != side_nodes.end() ) ni++;
368  return ( ni == si->n_nodes() );
369 }
370 
371 /**
372  * TODO:
373  * - use std::is_any for setting is_neigbour
374  * - possibly make appropriate constructors for Edge and Neighbour
375  * - check side!=-1 when searching neigbouring element
376  * - process bc_elements first, there should be no Neigh, but check it
377  * set Edge and boundary there
378  */
379 
381 {
382  Neighbour neighbour;
383  Edge *edg;
384  unsigned int ngh_element_idx, last_edge_idx;
385 
387 
388  // pointers to created edges
389  //vector<Edge *> tmp_edges;
390  edges.resize(0); // be sure that edges are empty
391 
393  vector<unsigned int> intersection_list; // list of elements in intersection of node element lists
394 
395  for( ElementFullIter bc_ele = bc_elements.begin(); bc_ele != bc_elements.end(); ++bc_ele) {
396  // Find all elements that share this side.
397  side_nodes.resize(bc_ele->n_nodes());
398  for (unsigned n=0; n<bc_ele->n_nodes(); n++) side_nodes[n] = node_vector.index(bc_ele->node[n]);
399  intersect_element_lists(side_nodes, intersection_list);
400  bool is_neighbour = find_lower_dim_element(element, intersection_list, bc_ele->dim() +1, ngh_element_idx);
401  if (is_neighbour) {
402  xprintf(UsrErr, "Boundary element (id: %d) match a regular element (id: %d) of lower dimension.\n",
403  bc_ele.id(), element(ngh_element_idx).id());
404  } else {
405  if (intersection_list.size() == 0) {
406  // no matching dim+1 element found
407  WarningOut().fmt("Lonely boundary element, id: {}, region: {}, dimension {}.\n",
408  bc_ele.id(), bc_ele->region().id(), bc_ele->dim());
409  continue; // skip the boundary element
410  }
411  last_edge_idx=edges.size();
412  edges.resize(last_edge_idx+1);
413  edg = &( edges.back() );
414  edg->n_sides = 0;
415  edg->side_ = new struct SideIter[ intersection_list.size() ];
416 
417  // common boundary object
418  unsigned int bdr_idx=boundary_.size();
419  boundary_.resize(bdr_idx+1);
420  Boundary &bdr=boundary_.back();
421  bdr.bc_ele_idx_ = bc_ele.index();
422  bdr.edge_idx_ = last_edge_idx;
423  bdr.mesh_=this;
424 
425  // for 1d boundaries there can be more then one 1d elements connected to the boundary element
426  // we do not detect this case later in the main search over bulk elements
427  for( vector<unsigned int>::iterator isect = intersection_list.begin(); isect!=intersection_list.end(); ++isect) {
428  Element *elem = &(element[*isect]);
429  for (unsigned int ecs=0; ecs<elem->n_sides(); ecs++) {
430  SideIter si = elem->side(ecs);
431  if ( same_sides( si, side_nodes) ) {
432  if (elem->edge_idx_[ecs] != Mesh::undef_idx) {
433  OLD_ASSERT(elem->boundary_idx_!=nullptr, "Null boundary idx array.\n");
434  int last_bc_ele_idx=this->boundary_[elem->boundary_idx_[ecs]].bc_ele_idx_;
435  int new_bc_ele_idx=bc_ele.index();
436  THROW( ExcDuplicateBoundary()
437  << EI_ElemLast(this->bc_elements.get_id(last_bc_ele_idx))
438  << EI_RegLast(this->bc_elements[last_bc_ele_idx].region().label())
439  << EI_ElemNew(this->bc_elements.get_id(new_bc_ele_idx))
440  << EI_RegNew(this->bc_elements[new_bc_ele_idx].region().label())
441  );
442  }
443  elem->edge_idx_[ecs] = last_edge_idx;
444  edg->side_[ edg->n_sides++ ] = si;
445 
446  if (elem->boundary_idx_ == NULL) {
447  elem->boundary_idx_ = new unsigned int [ elem->n_sides() ];
448  std::fill( elem->boundary_idx_, elem->boundary_idx_ + elem->n_sides(), Mesh::undef_idx);
449  }
450  elem->boundary_idx_[ecs] = bdr_idx;
451  break; // next element in intersection list
452  }
453  }
454  }
455 
456  }
457 
458  }
459  // Now we go through all element sides and create edges and neighbours
460  FOR_ELEMENTS( this, e )
461  {
462  for (unsigned int s=0; s<e->n_sides(); s++)
463  {
464  // skip sides that were already found
465  if (e->edge_idx_[s] != Mesh::undef_idx) continue;
466 
467 
468  // Find all elements that share this side.
469  side_nodes.resize(e->side(s)->n_nodes());
470  for (unsigned n=0; n<e->side(s)->n_nodes(); n++) side_nodes[n] = node_vector.index(e->side(s)->node(n));
471  intersect_element_lists(side_nodes, intersection_list);
472 
473  bool is_neighbour = find_lower_dim_element(element, intersection_list, e->dim(), ngh_element_idx);
474 
475  if (is_neighbour) { // edge connects elements of different dimensions
476  neighbour.element_ = &(element[ngh_element_idx]);
477  } else { // edge connects only elements of the same dimension
478  // Allocate the array of sides.
479  last_edge_idx=edges.size();
480  edges.resize(last_edge_idx+1);
481  edg = &( edges.back() );
482  edg->n_sides = 0;
483  edg->side_ = new struct SideIter[ intersection_list.size() ];
484  if (intersection_list.size() > max_edge_sides_[e->dim()-1])
485  max_edge_sides_[e->dim()-1] = intersection_list.size();
486 
487  if (intersection_list.size() == 1) { // outer edge, create boundary object as well
488  edg->n_sides=1;
489  edg->side_[0] = e->side(s);
490  e->edge_idx_[s] = last_edge_idx;
491 
492  if (e->boundary_idx_ == NULL) {
493  e->boundary_idx_ = new unsigned int [ e->n_sides() ];
494  std::fill( e->boundary_idx_, e->boundary_idx_ + e->n_sides(), Mesh::undef_idx);
495  }
496 
497  unsigned int bdr_idx=boundary_.size();
498  boundary_.resize(bdr_idx+1);
499  Boundary &bdr=boundary_.back();
500  e->boundary_idx_[s] = bdr_idx;
501 
502  // fill boundary element
503  ElementFullIter bc_ele = bc_elements.add_item( -bdr_idx ); // use negative bcd index as ID,
504  bc_ele->init(e->dim()-1, this, region_db_.implicit_boundary_region() );
505  region_db_.mark_used_region( bc_ele->region_idx_.idx() );
506  for(unsigned int ni = 0; ni< side_nodes.size(); ni++) bc_ele->node[ni] = &( node_vector[side_nodes[ni]] );
507 
508  // fill Boundary object
509  bdr.edge_idx_ = last_edge_idx;
510  bdr.bc_ele_idx_ = bc_ele.index();
511  bdr.mesh_=this;
512 
513  continue; // next side of element e
514  }
515  }
516 
517  // go through the elements connected to the edge or neighbour
518  for( vector<unsigned int>::iterator isect = intersection_list.begin(); isect!=intersection_list.end(); ++isect) {
519  Element *elem = &(element[*isect]);
520  for (unsigned int ecs=0; ecs<elem->n_sides(); ecs++) {
521  if (elem->edge_idx_[ecs] != Mesh::undef_idx) continue;
522  SideIter si = elem->side(ecs);
523  if ( same_sides( si, side_nodes) ) {
524  if (is_neighbour) {
525  // create a new edge and neighbour for this side, and element to the edge
526  last_edge_idx=edges.size();
527  edges.resize(last_edge_idx+1);
528  edg = &( edges.back() );
529  edg->n_sides = 1;
530  edg->side_ = new struct SideIter[1];
531  edg->side_[0] = si;
532  elem->edge_idx_[ecs] = last_edge_idx;
533 
534  neighbour.edge_idx_ = last_edge_idx;
535 
536  vb_neighbours_.push_back(neighbour); // copy neighbour with this edge setting
537  } else {
538  // connect the side to the edge, and side to the edge
539  edg->side_[ edg->n_sides++ ] = si;
540  elem->edge_idx_[ecs] = last_edge_idx;
541  }
542  break; // next element from intersection list
543  }
544  } // search for side of other connected element
545  } // connected elements
546  OLD_ASSERT( is_neighbour || ( (unsigned int) edg->n_sides ) == intersection_list.size(), "Some connected sides were not found.\n");
547  } // for element sides
548  } // for elements
549 
550  MessageOut().fmt( "Created {} edges and {} neighbours.\n", edges.size(), vb_neighbours_.size() );
551 }
552 
553 
554 
556 {
557  for (EdgeVector::iterator edg=edges.begin(); edg!=edges.end(); edg++)
558  {
559  // side 0 is reference, so its permutation is 0
560  edg->side(0)->element()->permutation_idx_[edg->side(0)->el_idx()] = 0;
561 
562  if (edg->n_sides > 1)
563  {
564  map<const Node*,unsigned int> node_numbers;
565  unsigned int permutation[edg->side(0)->n_nodes()];
566 
567  for (unsigned int i=0; i<edg->side(0)->n_nodes(); i++)
568  node_numbers[edg->side(0)->node(i)] = i;
569 
570  for (int sid=1; sid<edg->n_sides; sid++)
571  {
572  for (unsigned int i=0; i<edg->side(0)->n_nodes(); i++)
573  permutation[node_numbers[edg->side(sid)->node(i)]] = i;
574 
575  switch (edg->side(0)->dim())
576  {
577  case 0:
578  edg->side(sid)->element()->permutation_idx_[edg->side(sid)->el_idx()] = RefElement<1>::permutation_index(permutation);
579  break;
580  case 1:
581  edg->side(sid)->element()->permutation_idx_[edg->side(sid)->el_idx()] = RefElement<2>::permutation_index(permutation);
582  break;
583  case 2:
584  edg->side(sid)->element()->permutation_idx_[edg->side(sid)->el_idx()] = RefElement<3>::permutation_index(permutation);
585  break;
586  }
587  }
588  }
589  }
590 
591  for (vector<Neighbour>::iterator nb=vb_neighbours_.begin(); nb!=vb_neighbours_.end(); nb++)
592  {
593  map<const Node*,unsigned int> node_numbers;
594  unsigned int permutation[nb->element()->n_nodes()];
595 
596  // element of lower dimension is reference, so
597  // we calculate permutation for the adjacent side
598  for (unsigned int i=0; i<nb->element()->n_nodes(); i++)
599  node_numbers[nb->element()->node[i]] = i;
600 
601  for (unsigned int i=0; i<nb->side()->n_nodes(); i++)
602  permutation[node_numbers[nb->side()->node(i)]] = i;
603 
604  switch (nb->side()->dim())
605  {
606  case 0:
607  nb->side()->element()->permutation_idx_[nb->side()->el_idx()] = RefElement<1>::permutation_index(permutation);
608  break;
609  case 1:
610  nb->side()->element()->permutation_idx_[nb->side()->el_idx()] = RefElement<2>::permutation_index(permutation);
611  break;
612  case 2:
613  nb->side()->element()->permutation_idx_[nb->side()->el_idx()] = RefElement<3>::permutation_index(permutation);
614  break;
615  }
616  }
617 }
618 
619 
620 
621 
622 
623 //=============================================================================
624 //
625 //=============================================================================
627 {
628 
629  //MessageOut() << "Element to neighbours of vb2 type... "/*orig verb 5*/;
630 
631  FOR_ELEMENTS(this,ele) ele->n_neighs_vb =0;
632 
633  // count vb neighs per element
634  FOR_NEIGHBOURS(this, ngh ) ngh->element_->n_neighs_vb++;
635 
636  // Allocation of the array per element
637  FOR_ELEMENTS(this, ele )
638  if( ele->n_neighs_vb > 0 ) {
639  ele->neigh_vb = new struct Neighbour* [ele->n_neighs_vb];
640  ele->n_neighs_vb=0;
641  }
642 
643  // fill
644  ElementIter ele;
645  FOR_NEIGHBOURS(this, ngh ) {
646  ele = ngh->element();
647  ele->neigh_vb[ ele->n_neighs_vb++ ] = &( *ngh );
648  }
649 
650  //MessageOut() << "... O.K.\n"/*orig verb 6*/;
651 }
652 
653 
654 
655 
659 
660 
662  /* Algorithm:
663  *
664  * 1) create BIH tree
665  * 2) for every 1D, find list of candidates
666  * 3) compute intersections for 1d, store it to master_elements
667  *
668  */
669  const BIHTree &bih_tree =get_bih_tree();
670  master_elements.resize(n_elements());
671 
672  for(unsigned int i_ele=0; i_ele<n_elements(); i_ele++) {
673  Element &ele = this->element[i_ele];
674 
675  if (ele.dim() == 1) {
676  vector<unsigned int> candidate_list;
677  bih_tree.find_bounding_box(ele.bounding_box(), candidate_list);
678 
679  //for(unsigned int i_elm=0; i_elm<n_elements(); i_elm++) {
680  for(unsigned int i_elm : candidate_list) {
681  ElementFullIter elm = this->element( i_elm );
682  if (elm->dim() == 2) {
683  IntersectionLocal *intersection;
684  GetIntersection( TAbscissa(ele), TTriangle(*elm), intersection);
685  if (intersection && intersection->get_type() == IntersectionLocal::line) {
686 
687  master_elements[i_ele].push_back( intersections.size() );
688  intersections.push_back( Intersection(this->element(i_ele), elm, intersection) );
689  }
690  }
691 
692  }
693  }
694  }
695 
696 }
697 
698 
699 
700 ElementAccessor<3> Mesh::element_accessor(unsigned int idx, bool boundary) {
701  return ElementAccessor<3>(this, idx, boundary);
702 }
703 
704 
705 
706 vector<int> const & Mesh::elements_id_maps( bool boundary_domain) const
707 {
708  if (bulk_elements_id_.size() ==0) {
710  int last_id;
711 
712  bulk_elements_id_.resize(n_elements());
713  map_it = bulk_elements_id_.begin();
714  last_id = -1;
715  for(unsigned int idx=0; idx < element.size(); idx++, ++map_it) {
716  int id = element.get_id(idx);
717  if (last_id >= id) xprintf(UsrErr, "Element IDs in non-increasing order, ID: %d\n", id);
718  last_id=*map_it = id;
719  }
720 
722  map_it = boundary_elements_id_.begin();
723  last_id = -1;
724  for(unsigned int idx=0; idx < bc_elements.size(); idx++, ++map_it) {
725  int id = bc_elements.get_id(idx);
726  // We set ID for boundary elements created by the mesh itself to "-1"
727  // this force gmsh reader to skip all remaining entries in boundary_elements_id_
728  // and thus report error for any remaining data lines
729  if (id < 0) last_id=*map_it=-1;
730  else {
731  if (last_id >= id) xprintf(UsrErr, "Element IDs in non-increasing order, ID: %d\n", id);
732  last_id=*map_it = id;
733  }
734  }
735  }
736 
737  if (boundary_domain) return boundary_elements_id_;
738  return bulk_elements_id_;
739 }
740 
742 {
744  it != region_list.end();
745  ++it) {
746  // constructor has side effect in the mesh - create new region or set and store them to Mesh::region_db_
747  (*it).factory< RegionSetBase, const Input::Record &, Mesh * >(*it, this);
748  }
749 }
750 
752 {
754  region_db_.el_to_reg_map_.clear();
755  region_db_.close();
757 
758  if ( in_record_.val<bool>("print_regions") ) {
760  }
761 }
762 
763 
765  if (! this->bih_tree_)
766  bih_tree_ = std::make_shared<BIHTree>(this);
767  return *bih_tree_;
768 }
769 
770 //-----------------------------------------------------------------------------
771 // vim: set cindent:
int n_triangles
Definition: mesh.h:258
Distribution * el_ds
Parallel distribution of elements.
Definition: mesh.h:397
Class for the mesh partitioning. This should provide:
Definition: partitioning.hh:39
Iterator< ValueType > begin() const
Mesh(const std::string &input_str="{mesh_file=\"\"}", MPI_Comm com=MPI_COMM_WORLD)
Definition: mesh.cc:75
#define FOR_ELEMENT_NODES(i, j)
Definition: elements.h:188
vector< vector< unsigned int > > node_elements
Definition: mesh.h:280
Accessor to input data conforming to declared Array.
Definition: accessors.hh:552
unsigned int * boundary_idx_
Definition: elements.h:83
int get_id(const T *it) const
Definition: sys_vector.hh:458
void count_side_types()
Definition: mesh.cc:289
int MPI_Comm
Definition: mpi.h:141
void make_intersec_elements()
Definition: mesh.cc:661
void check_and_finish()
Definition: mesh.cc:751
MapElementIDToRegionID el_to_reg_map_
Definition: region.hh:570
Reader for (slightly) modified input files.
Class Input::Type::Default specifies default value of keys of a Input::Type::Record.
Definition: type_record.hh:50
#define FOR_ELEMENTS(_mesh_, __i)
Definition: mesh.h:410
Class for declaration of the input of type Bool.
Definition: type_base.hh:434
#define MessageOut()
Macro defining &#39;message&#39; record of log.
Definition: logger.hh:231
unsigned int * permutation_idx_
Definition: elements.h:92
int n_lines
Definition: mesh.h:257
static const Input::Type::Record & get_input_type()
Definition: partitioning.cc:45
static const unsigned int undef_idx
Definition: mesh.h:108
void create_node_element_lists()
Definition: mesh.cc:302
static unsigned int permutation_index(unsigned int p[n_nodes_per_side])
Definition: ref_element.cc:346
static const Input::Type::Record & get_input_type()
Definition: mesh.cc:57
static Default obligatory()
The factory function to make an empty default value which is obligatory.
Definition: type_record.hh:99
unsigned int max_edge_sides_[3]
Maximal number of sides per one edge in the actual mesh (set in make_neighbours_and_edges()).
Definition: mesh.h:360
Definition: mesh.h:95
int index() const
Definition: sys_vector.hh:78
Input::Record in_record_
Definition: mesh.h:379
Node ** node
Definition: elements.h:79
FullIter add_item(int id)
Definition: sys_vector.hh:359
int n_sides
Definition: edges.h:36
vector< vector< vector< unsigned int > > > side_nodes
Definition: mesh.h:270
FullIter find_id(const int id)
Definition: sys_vector.hh:434
Definition: edges.h:26
int n_tetrahedras
Definition: mesh.h:259
ElementAccessor< 3 > element_accessor(unsigned int idx, bool boundary=false)
Definition: mesh.cc:700
vector< Boundary > boundary_
Definition: mesh.h:225
Partitioning * get_part()
Definition: mesh.cc:191
unsigned int n_sides()
Definition: mesh.cc:172
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:286
Class for declaration of inputs sequences.
Definition: type_base.hh:321
int * el_4_loc
Index set assigning to local element index its global index.
Definition: mesh.h:395
int * row_4_el
Index set assigning to global element index the local index used in parallel vectors.
Definition: mesh.h:393
int n_exsides
Definition: mesh.h:254
IteratorBase end() const
Region get_region(unsigned int id, unsigned int dim)
Definition: region.cc:150
unsigned int dim() const
unsigned int size() const
Returns size of the container. This is independent of the allocated space.
Definition: sys_vector.hh:391
static Default optional()
The factory function to make an empty default value which is optional.
Definition: type_record.hh:113
#define OLD_ASSERT(...)
Definition: global_defs.h:131
bool opt_val(const string &key, Ret &value) const
unsigned int edge_idx_
Definition: neighbours.h:135
unsigned int * edge_idx_
Definition: elements.h:82
void read_regions_from_input(Input::Array region_list)
Definition: mesh.cc:741
virtual Record & allow_auto_conversion(const string &from_key)
Allows shorter input of the Record providing only value of the from_key given as the parameter...
Definition: type_record.cc:132
unsigned int n_elements() const
Definition: mesh.h:138
bool same_sides(const SideIter &si, vector< unsigned int > &side_nodes)
Definition: mesh.cc:363
static FileName input()
The factory function for declaring type FileName for input files.
Definition: type_base.hh:606
std::vector< T >::iterator iterator
Definition: sys_vector.hh:215
vector< int > const & elements_id_maps(bool boundary_domain) const
Definition: mesh.cc:706
void setup_topology()
Definition: mesh.cc:261
Neighbour ** neigh_vb
Definition: elements.h:166
T get_root_interface() const
Returns the root accessor.
friend class RegionSetBase
Definition: mesh.h:387
Accessor to the data with type Type::Record.
Definition: accessors.hh:277
#define NDEF
Definition: mesh.cc:52
const Ret val(const string &key) const
unsigned int n_sides() const
#define xprintf(...)
Definition: system.hh:87
#define START_TIMER(tag)
Starts a timer with specified tag.
Class for O(log N) lookup for intersections with a set of bounding boxes.
Definition: bih_tree.hh:36
unsigned int n_corners()
Definition: mesh.cc:181
unsigned int index(const T *pointer) const
Definition: sys_vector.hh:373
#define FOR_NEIGHBOURS(_mesh_, it)
Definition: mesh.h:461
Record & declare_key(const string &key, std::shared_ptr< TypeBase > type, const Default &default_value, const string &description, TypeBase::attribute_map key_attributes=TypeBase::attribute_map())
Declares a new key of the Record.
Definition: type_record.cc:468
ElementVector bc_elements
Definition: mesh.h:229
void close()
Definition: region.cc:249
SideIter side(const unsigned int loc_index)
FullIter begin()
Definition: sys_vector.hh:383
Region implicit_boundary_region()
Definition: region.cc:75
std::shared_ptr< BIHTree > bih_tree_
Definition: mesh.h:375
unsigned int edge_idx_
Definition: boundaries.h:75
void read_mesh(Mesh *mesh)
Accessor to the polymorphic input data of a type given by an AbstracRecord object.
Definition: accessors.hh:444
vector< int > boundary_elements_id_
Definition: mesh.h:355
unsigned int bc_ele_idx_
Definition: boundaries.h:76
void find_bounding_box(const BoundingBox &boundingBox, std::vector< unsigned int > &result_list) const
Definition: bih_tree.cc:193
void count_element_types()
Definition: mesh.cc:200
const BIHTree & get_bih_tree()
Definition: mesh.cc:764
void check_regions()
Definition: region.cc:461
Dedicated class for storing path to input and output files.
Definition: file_path.hh:48
Definition: system.hh:59
void read_gmsh_from_stream(istream &in)
Definition: mesh.cc:216
void mark_used_region(unsigned int idx)
Definition: region.cc:235
void print_region_table(ostream &stream) const
Definition: region.cc:409
Support classes for parallel programing.
vector< vector< unsigned int > > master_elements
Definition: mesh.h:246
vector< Neighbour > vb_neighbours_
Definition: mesh.h:251
int n_sides_
Definition: mesh.h:255
RegionIdx region_idx_
Definition: elements.h:174
void read_physical_names(Mesh *mesh)
RegionDB region_db_
Definition: mesh.h:366
std::vector< Edge > edges
Vector of MH edges, this should not be part of the geometrical mesh.
Definition: mesh.h:232
vector< Intersection > intersections
Definition: mesh.h:240
MPI_Comm comm_
Definition: mesh.h:384
unsigned int n_neighs_vb
Definition: elements.h:164
int n_insides
Definition: mesh.h:253
void intersect_element_lists(vector< unsigned int > const &nodes_list, vector< unsigned int > &intersection_element_list)
Definition: mesh.cc:315
void modify_element_ids(const RegionDB::MapElementIDToRegionID &map)
Definition: mesh.cc:251
#define WarningOut()
Macro defining &#39;warning&#39; record of log.
Definition: logger.hh:234
void make_edge_permutations()
Definition: mesh.cc:555
Class RefElement defines numbering of vertices, sides, calculation of normal vectors etc...
static Input::Type::Abstract & get_input_type()
Definition: region_set.cc:25
Mesh * mesh_
Definition: boundaries.h:77
unsigned int n_nodes() const
Definition: mesh.h:134
Record type proxy class.
Definition: type_record.hh:171
ElementIter element_
Definition: neighbours.h:136
SideIter * side_
Definition: edges.h:37
void element_to_neigh_vb()
Definition: mesh.cc:626
const Node * node(unsigned int i) const
Definition: side_impl.hh:46
void reinit(Input::Record in_record)
Definition: mesh.cc:102
FullIter end()
Returns FullFullIterer of the fictions past the end element.
Definition: sys_vector.hh:387
#define FOR_SIDES(_mesh_, it)
Definition: mesh.h:454
void init_from_input()
Definition: mesh.cc:230
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:45
void GetIntersection(const TBisector &, const TBisector &, TPosition &, double &, double &)
bool find_lower_dim_element(ElementVector &elements, vector< unsigned int > &element_list, unsigned int dim, unsigned int &element_idx)
Definition: mesh.cc:343
vector< int > bulk_elements_id_
Definition: mesh.h:355
IntersectionType get_type() const
std::shared_ptr< Partitioning > part_
Definition: mesh.h:370
void make_neighbours_and_edges()
Definition: mesh.cc:380
NodeVector node_vector
Vector of nodes of the mesh.
Definition: mesh.h:219
ElementVector element
Vector of elements of the mesh.
Definition: mesh.h:221
unsigned int n_nodes() const
Definition: side_impl.hh:33
~Mesh()
Destructor.
Definition: mesh.cc:147
unsigned int idx() const
Returns a global index of the region.
Definition: region.hh:81
BoundingBox bounding_box()
Definition: elements.h:102