Flow123d  JS_before_hm-883-gc471082
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 #include <unordered_map>
22 
23 #include "system/system.hh"
24 #include "system/exceptions.hh"
25 #include "system/index_types.hh"
27 #include "input/input_type.hh"
28 #include "input/accessors.hh"
29 #include "system/sys_profiler.hh"
30 #include "la/distribution.hh"
31 
32 #include "mesh/mesh.h"
33 #include "mesh/bc_mesh.hh"
34 #include "mesh/ref_element.hh"
35 #include "mesh/region_set.hh"
36 #include "mesh/range_wrapper.hh"
37 
38 // think about following dependencies
39 #include "mesh/accessors.hh"
40 #include "mesh/node_accessor.hh"
41 #include "mesh/partitioning.hh"
42 #include "mesh/neighbours.h"
43 
44 
45 #include "mesh/bih_tree.hh"
46 #include "mesh/duplicate_nodes.h"
47 
49 
50 
51 
52 //TODO: sources, concentrations, initial condition and similarly boundary conditions should be
53 // instances of a Element valued field
54 // concentrations is in fact reimplemented in transport REMOVE it HERE
55 
56 // After removing non-geometrical things from mesh, this should be part of mash initializing.
57 #include "mesh/region.hh"
58 
59 #define NDEF -1
60 
61 namespace IT = Input::Type;
62 
64  return Input::Type::Selection("Types of search algorithm for finding intersection candidates.")
65  .add_value(Mesh::BIHsearch, "BIHsearch",
66  "Use BIH for finding initial candidates, then continue by prolongation.")
67  .add_value(Mesh::BIHonly, "BIHonly",
68  "Use BIH for finding all candidates.")
69  .add_value(Mesh::BBsearch, "BBsearch",
70  "Use bounding boxes for finding initial candidates, then continue by prolongation.")
71  .close();
72 }
73 
75  return IT::Record("Mesh","Record with mesh related data." )
76  .allow_auto_conversion("mesh_file")
78  "Input file with mesh description.")
80  "List of additional region and region set definitions not contained in the mesh. "
81  "There are three region sets implicitly defined:\n\n"
82  "- ALL (all regions of the mesh)\n"
83  "- .BOUNDARY (all boundary regions)\n"
84  "- BULK (all bulk regions)")
85  .declare_key("partitioning", Partitioning::get_input_type(), IT::Default("\"any_neighboring\""), "Parameters of mesh partitioning algorithms.\n" )
86  .declare_key("print_regions", IT::Bool(), IT::Default("true"), "If true, print table of all used regions.")
87  .declare_key("intersection_search", Mesh::get_input_intersection_variant(),
88  IT::Default("\"BIHsearch\""), "Search algorithm for element intersections.")
89  .declare_key("global_snap_radius", IT::Double(0.0), IT::Default("1E-3"),
90  "Maximal snapping distance from the mesh in various search operations. In particular, it is used "
91  "to find the closest mesh element of an observe point; and in FieldFormula to find closest surface "
92  "element in plan view (Z projection).")
94  "Output file with neighboring data from mesh.")
95  .close();
96 }
97 
98 const unsigned int Mesh::undef_idx;
99 
101 : tree(nullptr),
102  bulk_size_(0),
103  nodes_(3, 1, 0),
104  row_4_el(nullptr),
105  el_4_loc(nullptr),
106  el_ds(nullptr),
107  node_4_loc_(nullptr),
108  node_ds_(nullptr),
109  bc_mesh_(nullptr)
110 
111 {}
112 
113 
114 
116 : tree(nullptr),
117  in_record_(in_record),
118  comm_(com),
119  bulk_size_(0),
120  nodes_(3, 1, 0),
121  row_4_el(nullptr),
122  el_4_loc(nullptr),
123  el_ds(nullptr),
124  node_4_loc_(nullptr),
125  node_ds_(nullptr),
126  bc_mesh_(nullptr)
127 {
128  // set in_record_, if input accessor is empty
129  if (in_record_.is_empty()) {
130  istringstream is("{mesh_file=\"\"}");
131  Input::ReaderToStorage reader;
132  IT::Record &in_rec = const_cast<IT::Record &>(Mesh::get_input_type());
133  in_rec.finish();
134  reader.read_stream(is, in_rec, Input::FileFormat::format_JSON);
136  }
137 
138  int rank;
140  if (rank == 0) {
141  // optionally open raw output file
142  FilePath raw_output_file_path;
143  if (in_record_.opt_val("raw_ngh_output", raw_output_file_path)) {
144  MessageOut() << "Opening raw ngh output: " << raw_output_file_path << "\n";
145  try {
146  raw_output_file_path.open_stream(raw_ngh_output_file);
147  } INPUT_CATCH(FilePath::ExcFileOpen, FilePath::EI_Address_String, (in_record_))
148  }
149 
150  }
151  init();
152 }
153 
155 {
156  return in_record_.val<Mesh::IntersectionSearch>("intersection_search");
157 }
158 
159 
161 {
162 
163  n_insides = NDEF;
164  n_exsides = NDEF;
165  n_sides_ = NDEF;
166 
167  // number of element of particular dimension
168  n_lines = 0;
169  n_triangles = 0;
170  n_tetrahedras = 0;
171 
172  for (int d=0; d<3; d++) max_edge_sides_[d] = 0;
173 
174  // Initialize numbering of nodes on sides.
175  // This is temporary solution, until class Element is templated
176  // by dimension. Then we can replace Mesh::side_nodes by
177  // RefElement<dim>::side_nodes.
178 
179  // indices of side nodes in element node array
180  // Currently this is made ad libitum
181  // with some ordering here we can get sides with correct orientation.
182  // This speedup normal calculation.
183 
184  side_nodes.resize(3); // three side dimensions
185  for(int dim=0; dim < 3; dim++) {
186  side_nodes[dim].resize(dim+2); // number of sides
187  for(int i_side=0; i_side < dim+2; i_side++)
188  side_nodes[dim][i_side].resize(dim+1);
189  }
190 
191  for (unsigned int sid=0; sid<RefElement<1>::n_sides; sid++)
192  for (unsigned int nid=0; nid<RefElement<1>::n_nodes_per_side; nid++)
193  side_nodes[0][sid][nid] = RefElement<1>::interact(Interaction<0,0>(sid))[nid];
194 
195  for (unsigned int sid=0; sid<RefElement<2>::n_sides; sid++)
196  for (unsigned int nid=0; nid<RefElement<2>::n_nodes_per_side; nid++)
197  side_nodes[1][sid][nid] = RefElement<2>::interact(Interaction<0,1>(sid))[nid];
198 
199  for (unsigned int sid=0; sid<RefElement<3>::n_sides; sid++)
200  for (unsigned int nid=0; nid<RefElement<3>::n_nodes_per_side; nid++)
201  side_nodes[2][sid][nid] = RefElement<3>::interact(Interaction<0,2>(sid))[nid];
202 }
203 
204 
206  for(EdgeData &edg : this->edges)
207  if (edg.side_) delete[] edg.side_;
208 
209  for (unsigned int idx=0; idx < bulk_size_; idx++) {
210  Element *ele=&(element_vec_[idx]);
211  if (ele->boundary_idx_) delete[] ele->boundary_idx_;
212  if (ele->neigh_vb) delete[] ele->neigh_vb;
213  }
214 
215  for(unsigned int idx=bulk_size_; idx < element_vec_.size(); idx++) {
216  Element *ele=&(element_vec_[idx]);
217  if (ele->boundary_idx_) delete[] ele->boundary_idx_;
218  }
219 
220  if (row_4_el != nullptr) delete[] row_4_el;
221  if (el_4_loc != nullptr) delete[] el_4_loc;
222  if (el_ds != nullptr) delete el_ds;
223  if (node_4_loc_ != nullptr) delete[] node_4_loc_;
224  if (node_ds_ != nullptr) delete node_ds_;
225  if (bc_mesh_ != nullptr) delete bc_mesh_;
226  if (tree != nullptr) delete tree;
227 }
228 
229 
230 unsigned int Mesh::n_sides() const
231 {
232  if (n_sides_ == NDEF) {
233  n_sides_=0;
234  for (auto ele : this->elements_range()) n_sides_ += ele->n_sides();
235  }
236  return n_sides_;
237 }
238 
239 unsigned int Mesh::n_vb_neighbours() const {
240  return vb_neighbours_.size();
241  }
242 
243 
244 unsigned int Mesh::n_corners() {
245  unsigned int li, count = 0;
246  for (auto ele : this->elements_range()) {
247  for (li=0; li<ele->n_nodes(); li++) {
248  count++;
249  }
250  }
251  return count;
252 }
253 
254 Edge Mesh::edge(uint edge_idx) const
255 {
256  ASSERT_LT_DBG(edge_idx, edges.size());
257  return Edge(this, edge_idx);
258 }
259 
261 {
262  ASSERT_LT_DBG(bc_idx, boundary_.size());
263  return Boundary(&boundary_[bc_idx]);
264 }
265 
267  return part_.get();
268 }
269 
271  return (LongIdx*)this->get_part()->get_loc_part();
272 }
273 
274 
275 //=============================================================================
276 // COUNT ELEMENT TYPES
277 //=============================================================================
278 
280  for (auto elm : this->elements_range())
281  switch (elm->dim()) {
282  case 1:
283  n_lines++;
284  break;
285  case 2:
286  n_triangles++;
287  break;
288  case 3:
289  n_tetrahedras++;
290  break;
291  }
292 }
293 
294 
295 
297 
298  // get dim of the first element in the map, if it exists
299  uint dim_to_check = RegionDB::undefined_dim;
300  std::string reg_name = "UndefinedRegion";
301  if(map.size() > 0){
302  Element &ele = element_vec_[ elem_index(map.begin()->first) ];
303  dim_to_check = ele.dim();
304  reg_name = region_db_.find_id(map.begin()->second).label();
305  }
306 
307  for (auto elem_to_region : map) {
308  Element &ele = element_vec_[ elem_index(elem_to_region.first) ];
309 
310  if( ele.dim() != dim_to_check){
311  xprintf(UsrErr, "User defined region '%s' (id %d) by 'From_Elements' cannot have elements of different dimensions.\n"
312  "Thrown due to: dim %d neq dim %d (ele id %d).\n"
313  "Split elements by dim, create separate regions and then possibly use Union.\n",
314  reg_name.c_str(), elem_to_region.second, dim_to_check, ele.dim(), elem_to_region.first);
315  }
316 
317  ele.region_idx_ = region_db_.get_region( elem_to_region.second, ele.dim() );
319  }
320 }
321 
322 
323 
325  START_TIMER("MESH - setup topology");
326 
328 
329  // check mesh quality
330  for (auto ele : this->elements_range()) {
331  double quality = ele.quality_measure_smooth(ele.side(0));
332  if ( quality< 0.001) WarningOut().fmt("Bad quality (<0.001) of the element {}.\n", ele.idx());
333  }
334 
339 
340  tree = new DuplicateNodes(this);
341 
342  part_ = std::make_shared<Partitioning>(this, in_record_.val<Input::Record>("partitioning") );
343 
344  // create parallel distribution and numbering of elements
345  LongIdx *id_4_old = new LongIdx[n_elements()];
346  int i = 0;
347  for (auto ele : this->elements_range())
348  id_4_old[i++] = ele.idx();
349  part_->id_maps(n_elements(), id_4_old, el_ds, el_4_loc, row_4_el);
350 
351  delete[] id_4_old;
352 
353  this->distribute_nodes();
354 
356 }
357 
358 
359 //
361 {
362 
363  n_insides = 0;
364  n_exsides = 0;
365  for (auto ele : this->elements_range())
366  for(SideIter sde = ele.side(0); sde->side_idx() < ele->n_sides(); ++sde) {
367  if (sde->is_external()) n_exsides++;
368  else n_insides++;
369  }
370 }
371 
372 
373 
375  // for each node we make a list of elements that use this node
376  node_elements_.resize( this->n_nodes() );
377 
378  for (auto ele : this->elements_range())
379  for (unsigned int n=0; n<ele->n_nodes(); n++)
380  node_elements_[ele.node(n).idx()].push_back(ele.idx());
381 
382  for (vector<vector<unsigned int> >::iterator n=node_elements_.begin(); n!=node_elements_.end(); n++)
383  stable_sort(n->begin(), n->end());
384 }
385 
386 
387 void Mesh::intersect_element_lists(vector<unsigned int> const &nodes_list, vector<unsigned int> &intersection_element_list)
388 {
389  if (node_elements_.size() == 0) {
391  }
392 
393  if (nodes_list.size() == 0) {
394  intersection_element_list.clear();
395  } else if (nodes_list.size() == 1) {
396  intersection_element_list = node_elements_[ nodes_list[0] ];
397  } else {
398  vector<unsigned int>::const_iterator it1=nodes_list.begin();
400  intersection_element_list.resize( node_elements_[*it1].size() ); // make enough space
401 
402  it1=set_intersection(
403  node_elements_[*it1].begin(), node_elements_[*it1].end(),
404  node_elements_[*it2].begin(), node_elements_[*it2].end(),
405  intersection_element_list.begin());
406  intersection_element_list.resize(it1-intersection_element_list.begin()); // resize to true size
407 
408  for(;it2<nodes_list.end();++it2) {
409  it1=set_intersection(
410  intersection_element_list.begin(), intersection_element_list.end(),
411  node_elements_[*it2].begin(), node_elements_[*it2].end(),
412  intersection_element_list.begin());
413  intersection_element_list.resize(it1-intersection_element_list.begin()); // resize to true size
414  }
415  }
416 }
417 
418 
419 bool Mesh::find_lower_dim_element( vector<unsigned int> &element_list, unsigned int dim, unsigned int &element_idx) {
420  bool is_neighbour = false;
421 
422  vector<unsigned int>::iterator e_dest=element_list.begin();
423  for( vector<unsigned int>::iterator ele = element_list.begin(); ele!=element_list.end(); ++ele) {
424  if (element_vec_[*ele].dim() == dim) { // keep only indexes of elements of same dimension
425  *e_dest=*ele;
426  ++e_dest;
427  } else if (element_vec_[*ele].dim() == dim-1) { // get only first element of lower dimension
428  if (is_neighbour) xprintf(UsrErr, "Too matching elements id: %d and id: %d in the same mesh.\n",
429  this->elem_index(*ele), this->elem_index(element_idx) );
430 
431  is_neighbour = true;
432  element_idx = *ele;
433  }
434  }
435  element_list.resize( e_dest - element_list.begin());
436  return is_neighbour;
437 }
438 
440  // 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)
441  unsigned int ni=0;
442  while ( ni < si->n_nodes()
443  && find(side_nodes.begin(), side_nodes.end(), si->node(ni).idx() ) != side_nodes.end() ) ni++;
444  return ( ni == si->n_nodes() );
445 }
446 
447 /**
448  * TODO:
449  * - use std::is_any for setting is_neigbour
450  * - possibly make appropriate constructors for Edge and Neighbour
451  * - check side!=-1 when searching neigbouring element
452  * - process boundary elements first, there should be no Neigh, but check it
453  * set Edge and boundary there
454  */
455 
457 {
458  ASSERT(bc_element_tmp_.size()==0)
459  .error("Temporary structure of boundary element data is not empty. Did you call create_boundary_elements?");
460 
461  Neighbour neighbour;
462  EdgeData *edg = nullptr;
463  unsigned int ngh_element_idx;
464  unsigned int last_edge_idx = Mesh::undef_idx;
465 
466  neighbour.mesh_ = this;
467 
469 
470  // pointers to created edges
471  //vector<Edge *> tmp_edges;
472  edges.resize(0); // be sure that edges are empty
473 
475  vector<unsigned int> intersection_list; // list of elements in intersection of node element lists
476 
477  for( unsigned int i=bulk_size_; i<element_vec_.size(); ++i) {
478 
479  ElementAccessor<3> bc_ele = this->element_accessor(i);
480  ASSERT(bc_ele.region().is_boundary());
481  // Find all elements that share this side.
482  side_nodes.resize(bc_ele->n_nodes());
483  for (unsigned n=0; n<bc_ele->n_nodes(); n++) side_nodes[n] = bc_ele->node_idx(n);
484  intersect_element_lists(side_nodes, intersection_list);
485  bool is_neighbour = find_lower_dim_element(intersection_list, bc_ele->dim() +1, ngh_element_idx);
486  if (is_neighbour) {
487  xprintf(UsrErr, "Boundary element (id: %d) match a regular element (id: %d) of lower dimension.\n",
488  bc_ele.idx(), this->elem_index(ngh_element_idx));
489  } else {
490  if (intersection_list.size() == 0) {
491  // no matching dim+1 element found
492  WarningOut().fmt("Lonely boundary element, id: {}, region: {}, dimension {}.\n",
493  bc_ele.idx(), bc_ele.region().id(), bc_ele->dim());
494  continue; // skip the boundary element
495  }
496  last_edge_idx=edges.size();
497  edges.resize(last_edge_idx+1);
498  edg = &( edges.back() );
499  edg->n_sides = 0;
500  edg->side_ = new struct SideIter[ intersection_list.size() ];
501 
502  // common boundary object
503  unsigned int bdr_idx=boundary_.size();
504  boundary_.resize(bdr_idx+1);
505  BoundaryData &bdr=boundary_.back();
506  bdr.bc_ele_idx_ = i;
507  bdr.edge_idx_ = last_edge_idx;
508  bdr.mesh_=this;
509 
510  // for 1d boundaries there can be more then one 1d elements connected to the boundary element
511  // we do not detect this case later in the main search over bulk elements
512  for( vector<unsigned int>::iterator isect = intersection_list.begin(); isect!=intersection_list.end(); ++isect) {
513  ElementAccessor<3> elem = this->element_accessor(*isect);
514  for (unsigned int ecs=0; ecs<elem->n_sides(); ecs++) {
515  SideIter si = elem.side(ecs);
516  if ( same_sides( si, side_nodes) ) {
517  if (elem->edge_idx(ecs) != Mesh::undef_idx) {
518  OLD_ASSERT(elem->boundary_idx_!=nullptr, "Null boundary idx array.\n");
519  int last_bc_ele_idx=this->boundary_[elem->boundary_idx_[ecs]].bc_ele_idx_;
520  int new_bc_ele_idx=i;
521  THROW( ExcDuplicateBoundary()
522  << EI_ElemLast(this->find_elem_id(last_bc_ele_idx))
523  << EI_RegLast(this->element_accessor(last_bc_ele_idx).region().label())
524  << EI_ElemNew(this->find_elem_id(new_bc_ele_idx))
525  << EI_RegNew(this->element_accessor(new_bc_ele_idx).region().label())
526  );
527  }
528  element_vec_[*isect].edge_idx_[ecs] = last_edge_idx;
529  edg->side_[ edg->n_sides++ ] = si;
530 
531  if (elem->boundary_idx_ == NULL) {
532  Element *el = &(element_vec_[*isect]);
533  el->boundary_idx_ = new unsigned int [ el->n_sides() ];
534  std::fill( el->boundary_idx_, el->boundary_idx_ + el->n_sides(), Mesh::undef_idx);
535  }
536  elem->boundary_idx_[ecs] = bdr_idx;
537  break; // next element in intersection list
538  }
539  }
540  }
541 
542  }
543 
544  }
545  // Now we go through all element sides and create edges and neighbours
546  for (auto e : this->elements_range()) {
547  for (unsigned int s=0; s<e->n_sides(); s++)
548  {
549  // skip sides that were already found
550  if (e->edge_idx(s) != Mesh::undef_idx) continue;
551 
552 
553  // Find all elements that share this side.
554  side_nodes.resize(e.side(s)->n_nodes());
555  for (unsigned n=0; n<e.side(s)->n_nodes(); n++) side_nodes[n] = e.side(s)->node(n).idx();
556  intersect_element_lists(side_nodes, intersection_list);
557 
558  bool is_neighbour = find_lower_dim_element(intersection_list, e->dim(), ngh_element_idx);
559 
560  if (is_neighbour) { // edge connects elements of different dimensions
561  // Initialize for the neighbour case.
562  neighbour.elem_idx_ = ngh_element_idx;
563  } else { // edge connects only elements of the same dimension
564  // Initialize for the edge case.
565  last_edge_idx=edges.size();
566  edges.resize(last_edge_idx+1);
567  edg = &( edges.back() );
568  edg->n_sides = 0;
569  edg->side_ = new struct SideIter[ intersection_list.size() ];
570  if (intersection_list.size() > max_edge_sides_[e->dim()-1])
571  max_edge_sides_[e->dim()-1] = intersection_list.size();
572 
573  if (intersection_list.size() == 1) {
574  // outer edge, create boundary object as well
575  Element &elm = element_vec_[e.idx()];
576  edg->n_sides=1;
577  edg->side_[0] = e.side(s);
578  element_vec_[e.idx()].edge_idx_[s] = last_edge_idx;
579 
580  if (e->boundary_idx_ == NULL) {
581  elm.boundary_idx_ = new unsigned int [ e->n_sides() ];
582  std::fill( elm.boundary_idx_, elm.boundary_idx_ + e->n_sides(), Mesh::undef_idx);
583  }
584 
585  unsigned int bdr_idx=boundary_.size()+1; // need for VTK mesh that has no boundary elements
586  // and bulk elements are indexed from 0
587  boundary_.resize(bdr_idx+1);
588  BoundaryData &bdr=boundary_.back();
589  elm.boundary_idx_[s] = bdr_idx;
590 
591  // fill boundary element
592  Element * bc_ele = add_element_to_vector(-bdr_idx);
593  bc_ele->init(e->dim()-1, region_db_.implicit_boundary_region() );
595  for(unsigned int ni = 0; ni< side_nodes.size(); ni++) bc_ele->nodes_[ni] = side_nodes[ni];
596 
597  // fill Boundary object
598  bdr.edge_idx_ = last_edge_idx;
599  bdr.bc_ele_idx_ = elem_index(-bdr_idx);
600  bdr.mesh_=this;
601 
602  continue; // next side of element e
603  }
604  }
605 
606  // go through the elements connected to the edge or neighbour
607  // setup neigbour or edge
608  for( vector<unsigned int>::iterator isect = intersection_list.begin(); isect!=intersection_list.end(); ++isect) {
609  ElementAccessor<3> elem = this->element_accessor(*isect);
610  for (unsigned int ecs=0; ecs<elem->n_sides(); ecs++) {
611  if (elem->edge_idx(ecs) != Mesh::undef_idx) continue; // ??? This should not happen.
612  SideIter si = elem.side(ecs);
613  if ( same_sides( si, side_nodes) ) {
614  if (is_neighbour) {
615  // create a new edge and neighbour for this side, and element to the edge
616  last_edge_idx=edges.size();
617  edges.resize(last_edge_idx+1);
618  edg = &( edges.back() );
619  edg->n_sides = 1;
620  edg->side_ = new struct SideIter[1];
621  edg->side_[0] = si;
622  element_vec_[elem.idx()].edge_idx_[ecs] = last_edge_idx;
623 
624  neighbour.edge_idx_ = last_edge_idx;
625 
626  vb_neighbours_.push_back(neighbour); // copy neighbour with this edge setting
627  } else {
628  // connect the side to the edge, and side to the edge
629  ASSERT_PTR_DBG(edg);
630  edg->side_[ edg->n_sides++ ] = si;
631  ASSERT_DBG(last_edge_idx != Mesh::undef_idx);
632  element_vec_[elem.idx()].edge_idx_[ecs] = last_edge_idx;
633  }
634  break; // next element from intersection list
635  }
636  } // search for side of other connected element
637  } // connected elements
638 
639  if (! is_neighbour)
640  ASSERT_EQ( (unsigned int) edg->n_sides, intersection_list.size())(e.index())(s).error("Missing edge sides.");
641  } // for element sides
642  } // for elements
643 
644  MessageOut().fmt( "Created {} edges and {} neighbours.\n", edges.size(), vb_neighbours_.size() );
645 }
646 
647 
648 
650 {
651  // node numbers is the local index of the node on the last side
652  // this maps the side nodes to the nodes of the reference side(0)
653  std::unordered_map<unsigned int,unsigned int> node_numbers;
654 
655  for (auto edg : edge_range())
656  {
657  unsigned int n_side_nodes = edg.side(0)->n_nodes();
658  // side 0 is reference, so its permutation is 0
659  edg.side(0)->element()->permutation_idx_[edg.side(0)->side_idx()] = 0;
660 
661  if (edg.n_sides() > 1)
662  {
663  // For every node on the reference side(0) give its local idx on the current side.
664  unsigned int permutation[n_side_nodes];
665 
666 
667  node_numbers.clear();
668  for (uint i=0; i<n_side_nodes; i++)
669  node_numbers[edg.side(0)->node(i).idx()] = i;
670 
671  for (uint sid=1; sid<edg.n_sides(); sid++)
672  {
673  for (uint i=0; i<n_side_nodes; i++)
674  permutation[node_numbers[edg.side(sid)->node(i).idx()]] = i;
675 
676  switch (edg.side(0)->dim())
677  {
678  case 0:
679  edg.side(sid)->element()->permutation_idx_[edg.side(sid)->side_idx()] = RefElement<1>::permutation_index(permutation);
680  break;
681  case 1:
682  edg.side(sid)->element()->permutation_idx_[edg.side(sid)->side_idx()] = RefElement<2>::permutation_index(permutation);
683  break;
684  case 2:
685  edg.side(sid)->element()->permutation_idx_[edg.side(sid)->side_idx()] = RefElement<3>::permutation_index(permutation);
686  break;
687  }
688  }
689  }
690  }
691 
692  for (vector<Neighbour>::iterator nb=vb_neighbours_.begin(); nb!=vb_neighbours_.end(); nb++)
693  {
694  // node numbers is the local index of the node on the last side
695  // this maps the side nodes to the nodes of the side(0)
696  unsigned int n_side_nodes = nb->element()->n_nodes();
697  unsigned int permutation[n_side_nodes];
698  node_numbers.clear();
699 
700  // element of lower dimension is reference, so
701  // we calculate permutation for the adjacent side
702  for (unsigned int i=0; i<n_side_nodes; i++)
703  node_numbers[nb->element().node(i).idx()] = i;
704 
705  for (unsigned int i=0; i<n_side_nodes; i++)
706  permutation[node_numbers[nb->side()->node(i).idx()]] = i;
707 
708  switch (nb->side()->dim())
709  {
710  case 0:
711  nb->side()->element()->permutation_idx_[nb->side()->side_idx()] = RefElement<1>::permutation_index(permutation);
712  break;
713  case 1:
714  nb->side()->element()->permutation_idx_[nb->side()->side_idx()] = RefElement<2>::permutation_index(permutation);
715  break;
716  case 2:
717  nb->side()->element()->permutation_idx_[nb->side()->side_idx()] = RefElement<3>::permutation_index(permutation);
718  break;
719  }
720  }
721 }
722 
723 
724 
725 
726 
727 //=============================================================================
728 //
729 //=============================================================================
731 {
732 
733  //MessageOut() << "Element to neighbours of vb2 type... "/*orig verb 5*/;
734 
735  for (vector<Element>::iterator ele = element_vec_.begin(); ele!= element_vec_.begin()+bulk_size_; ++ele)
736  ele->n_neighs_vb_ =0;
737 
738  // count vb neighs per element
739  for (auto & ngh : this->vb_neighbours_) ngh.element()->n_neighs_vb_++;
740 
741  // Allocation of the array per element
742  for (vector<Element>::iterator ele = element_vec_.begin(); ele!= element_vec_.begin()+bulk_size_; ++ele)
743  if( ele->n_neighs_vb() > 0 ) {
744  ele->neigh_vb = new struct Neighbour* [ele->n_neighs_vb()];
745  ele->n_neighs_vb_=0;
746  }
747 
748  // fill
749  ElementAccessor<3> ele;
750  for (auto & ngh : this->vb_neighbours_) {
751  ele = ngh.element();
752  ele->neigh_vb[ ele->n_neighs_vb_++ ] = &ngh;
753  }
754 
755  //MessageOut() << "... O.K.\n"/*orig verb 6*/;
756 }
757 
758 
759 
760 
761 
762 
764  /* Algorithm:
765  *
766  * 1) create BIH tree
767  * 2) for every 1D, find list of candidates
768  * 3) compute intersections for 1d, store it to master_elements
769  *
770  */
771  if (! intersections) {
772  intersections = std::make_shared<MixedMeshIntersections>(this);
773  intersections->compute_intersections();
774  }
775  return *intersections;
776 }
777 
778 
779 
781  return ElementAccessor<3>(this, idx);
782 }
783 
784 
785 
786 NodeAccessor<3> Mesh::node(unsigned int idx) const {
787  return NodeAccessor<3>(this, idx);
788 }
789 
790 
791 
792 void Mesh::elements_id_maps( vector<LongIdx> & bulk_elements_id, vector<LongIdx> & boundary_elements_id) const
793 {
794  if (bulk_elements_id.size() ==0) {
796  LongIdx last_id;
797 
798  bulk_elements_id.resize(n_elements());
799  map_it = bulk_elements_id.begin();
800  last_id = -1;
801  for(unsigned int idx=0; idx < n_elements(); idx++, ++map_it) {
802  LongIdx id = this->find_elem_id(idx);
803  if (last_id >= id) xprintf(UsrErr, "Element IDs in non-increasing order, ID: %d\n", id);
804  last_id=*map_it = id;
805  }
806 
807  boundary_elements_id.resize(n_elements(true));
808  map_it = boundary_elements_id.begin();
809  last_id = -1;
810  for(unsigned int idx=bulk_size_; idx<element_vec_.size(); idx++, ++map_it) {
811  LongIdx id = this->find_elem_id(idx);
812  // We set ID for boundary elements created by the mesh itself to "-1"
813  // this force gmsh reader to skip all remaining entries in boundary_elements_id
814  // and thus report error for any remaining data lines
815  if (id < 0) last_id=*map_it=-1;
816  else {
817  if (last_id >= id) xprintf(UsrErr, "Element IDs in non-increasing order, ID: %d\n", id);
818  last_id=*map_it = id;
819  }
820  }
821  }
822 }
823 
824 
825 bool compare_points(const arma::vec3 &p1, const arma::vec3 &p2) {
826  static const double point_tolerance = 1E-10;
827  return fabs(p1[0]-p2[0]) < point_tolerance
828  && fabs(p1[1]-p2[1]) < point_tolerance
829  && fabs(p1[2]-p2[2]) < point_tolerance;
830 }
831 
832 
833 bool Mesh::check_compatible_mesh( Mesh & mesh, vector<LongIdx> & bulk_elements_id, vector<LongIdx> & boundary_elements_id )
834 {
835  std::vector<unsigned int> node_ids; // allow mapping ids of nodes from source mesh to target mesh
836  std::vector<unsigned int> node_list;
837  std::vector<unsigned int> candidate_list; // returned by intersect_element_lists
838  std::vector<unsigned int> result_list; // list of elements with same dimension as vtk element
839  unsigned int i; // counter over vectors
840 
841  {
842  // iterates over node vector of \p this object
843  // to each node must be found just only one node in target \p mesh
844  // store orders (mapping between source and target meshes) into node_ids vector
845  std::vector<unsigned int> searched_elements; // for BIH tree
846  unsigned int i_node, i_elm_node;
847  const BIHTree &bih_tree=mesh.get_bih_tree();
848 
849  // create nodes of mesh
850  node_ids.resize( this->n_nodes() );
851  i=0;
852  for (auto nod : this->node_range()) {
853  uint found_i_node = Mesh::undef_idx;
854  bih_tree.find_point(*nod, searched_elements);
855 
856  for (std::vector<unsigned int>::iterator it = searched_elements.begin(); it!=searched_elements.end(); it++) {
857  ElementAccessor<3> ele = mesh.element_accessor( *it );
858  for (i_node=0; i_node<ele->n_nodes(); i_node++)
859  {
860  static const double point_tolerance = 1E-10;
861  if ( arma::norm(*ele.node(i_node) - *nod, 1) < point_tolerance) {
862  i_elm_node = ele.node(i_node).idx();
863  if (found_i_node == Mesh::undef_idx) found_i_node = i_elm_node;
864  else if (found_i_node != i_elm_node) {
865  // duplicate nodes in target mesh
866  this->elements_id_maps(bulk_elements_id, boundary_elements_id);
867  return false;
868  }
869  }
870  }
871  }
872  if (found_i_node == Mesh::undef_idx) {
873  // no node found in target mesh
874  this->elements_id_maps(bulk_elements_id, boundary_elements_id);
875  return false;
876  }
877  node_ids[i] = found_i_node;
878  searched_elements.clear();
879  i++;
880  }
881  }
882 
883  {
884  // iterates over bulk elements of \p this object
885  // elements in both meshes must be in ratio 1:1
886  // store orders (mapping between both mesh files) into bulk_elements_id vector
887  bulk_elements_id.clear();
888  bulk_elements_id.resize(this->n_elements());
889  // iterate trough bulk part of element vector, to each element in source mesh must exist only one element in target mesh
890  // fill bulk_elements_id vector
891  i=0;
892  for (auto elm : this->elements_range()) {
893  for (unsigned int j=0; j<elm->n_nodes(); j++) { // iterate trough all nodes of any element
894  node_list.push_back( node_ids[ elm->node_idx(j) ] );
895  }
896  mesh.intersect_element_lists(node_list, candidate_list);
897  for (auto i_elm : candidate_list) {
898  if ( mesh.element_accessor(i_elm)->dim() == elm.dim() ) result_list.push_back( elm.index() );
899  }
900  if (result_list.size() != 1) {
901  // intersect_element_lists must produce one element
902  this->elements_id_maps(bulk_elements_id, boundary_elements_id);
903  return false;
904  }
905  bulk_elements_id[i] = (LongIdx)result_list[0];
906  node_list.clear();
907  result_list.clear();
908  i++;
909  }
910  }
911 
912  {
913  // iterates over boundary elements of \p this object
914  // elements in both meshes must be in ratio 1:1
915  // store orders (mapping between both mesh files) into boundary_elements_id vector
916  auto bc_mesh = this->get_bc_mesh();
917  boundary_elements_id.clear();
918  boundary_elements_id.resize(bc_mesh->n_elements());
919  // iterate trough boundary part of element vector, to each element in source mesh must exist only one element in target mesh
920  // fill boundary_elements_id vector
921  i=0;
922  for (auto elm : bc_mesh->elements_range()) {
923  for (unsigned int j=0; j<elm->n_nodes(); j++) { // iterate trough all nodes of any element
924  node_list.push_back( node_ids[ elm->node_idx(j) ] );
925  }
926  mesh.get_bc_mesh()->intersect_element_lists(node_list, candidate_list);
927  for (auto i_elm : candidate_list) {
928  if ( mesh.get_bc_mesh()->element_accessor(i_elm)->dim() == elm.dim() ) result_list.push_back( elm.index() );
929  }
930  if (result_list.size() != 1) {
931  // intersect_element_lists must produce one element
932  this->elements_id_maps(bulk_elements_id, boundary_elements_id);
933  return false;
934  }
935  boundary_elements_id[i] = (LongIdx)result_list[0];
936  node_list.clear();
937  result_list.clear();
938  i++;
939  }
940  }
941 
942  return true;
943 }
944 
946 {
948  it != region_list.end();
949  ++it) {
950  // constructor has side effect in the mesh - create new region or set and store them to Mesh::region_db_
951  (*it).factory< RegionSetBase, const Input::Record &, Mesh * >(*it, this);
952  }
953 }
954 
956 {
958  region_db_.el_to_reg_map_.clear();
959  region_db_.close();
961 
962  if ( in_record_.val<bool>("print_regions") ) {
963  stringstream ss;
965  MessageOut() << ss.str();
966  }
967 }
968 
969 
971  START_TIMER("Mesh::compute_element_boxes");
973 
974  // make element boxes
975  unsigned int i=0;
976  boxes.resize(this->n_elements());
977  for (auto element : this->elements_range()) {
978  boxes[i] = element.bounding_box();
979  i++;
980  }
981 
982  return boxes;
983 }
984 
986  if (! this->bih_tree_) {
987  bih_tree_ = std::make_shared<BIHTree>();
988  bih_tree_->add_boxes( this->get_element_boxes() );
989  bih_tree_->construct();
990  }
991  return *bih_tree_;
992 }
993 
994 double Mesh::global_snap_radius() const {
995  return in_record_.val<double>("global_snap_radius");
996 }
997 
998 void Mesh::add_physical_name(unsigned int dim, unsigned int id, std::string name) {
999  region_db_.add_region(id, name, dim, "$PhysicalNames");
1000 }
1001 
1002 
1003 void Mesh::add_node(unsigned int node_id, arma::vec3 coords) {
1004 
1005  nodes_.append(coords);
1006  node_ids_.add_item(node_id);
1007 }
1008 
1009 
1010 void Mesh::add_element(unsigned int elm_id, unsigned int dim, unsigned int region_id, unsigned int partition_id,
1011  std::vector<unsigned int> node_ids) {
1012  RegionIdx region_idx = region_db_.get_region( region_id, dim );
1013  if ( !region_idx.is_valid() ) {
1014  region_idx = region_db_.add_region( region_id, region_db_.create_label_from_id(region_id), dim, "$Element" );
1015  }
1016  region_db_.mark_used_region(region_idx.idx());
1017 
1018  if (region_idx.is_boundary()) {
1019  bc_element_tmp_.push_back( ElementTmpData(elm_id, dim, region_idx, partition_id, node_ids) );
1020  } else {
1021  if(dim == 0 ) {
1022  WarningOut().fmt("Bulk elements of zero size(dim=0) are not supported. Element ID: {}.\n", elm_id);
1023  }
1024  else {
1025  Element *ele = add_element_to_vector(elm_id);
1026  bulk_size_++;
1027  this->init_element(ele, elm_id, dim, region_idx, partition_id, node_ids);
1028  }
1029  }
1030 }
1031 
1032 
1033 void Mesh::init_element(Element *ele, unsigned int elm_id, unsigned int dim, RegionIdx region_idx, unsigned int partition_id,
1034  std::vector<unsigned int> node_ids) {
1035  ele->init(dim, region_idx);
1036  ele->pid_ = partition_id;
1037 
1038  for (unsigned int ni=0; ni<ele->n_nodes(); ni++) {
1039  ele->nodes_[ni] = this->node_index(node_ids[ni]);
1040  }
1041 
1042  // check that tetrahedron element is numbered correctly and is not degenerated
1043  if(ele->dim() == 3)
1044  {
1045  ElementAccessor<3> ea = this->element_accessor( this->elem_index(elm_id) );
1046  double jac = ea.tetrahedron_jacobian();
1047  if( ! (jac > 0) ) {
1048  WarningOut().fmt("Tetrahedron element with id {} has wrong numbering or is degenerated (Jacobian = {}).",elm_id, jac);
1049  }
1050  }
1051 }
1052 
1053 
1055  if (node_elements_.size() == 0) {
1056  this->create_node_element_lists();
1057  }
1058  return node_elements_;
1059 }
1060 
1061 
1062 void Mesh::init_element_vector(unsigned int size) {
1063  element_vec_.clear();
1064  element_ids_.clear();
1065  element_vec_.reserve(size);
1066  element_ids_.reserve(size);
1067  bc_element_tmp_.clear();
1068  bc_element_tmp_.reserve(size);
1069  bulk_size_ = 0;
1071 }
1072 
1073 
1074 void Mesh::init_node_vector(unsigned int size) {
1075  nodes_.reinit(size);
1076  node_ids_.clear();
1077  node_ids_.reserve(size);
1078 }
1079 
1080 
1082  element_vec_.push_back( Element() );
1083  Element * elem = &element_vec_.back(); //[element_vec_.size()-1];
1084  element_ids_.add_item((unsigned int)(id));
1085  return elem;
1086 }
1087 
1089  auto bgn_it = make_iter<ElementAccessor<3>>( ElementAccessor<3>(this, 0) );
1090  auto end_it = make_iter<ElementAccessor<3>>( ElementAccessor<3>(this, bulk_size_) );
1091  return Range<ElementAccessor<3>>(bgn_it, end_it);
1092 }
1093 
1095  auto bgn_it = make_iter<NodeAccessor<3>>( NodeAccessor<3>(this, 0) );
1096  auto end_it = make_iter<NodeAccessor<3>>( NodeAccessor<3>(this, n_nodes()) );
1097  return Range<NodeAccessor<3>>(bgn_it, end_it);
1098 }
1099 
1101  auto bgn_it = make_iter<Edge>( Edge(this, 0) );
1102  auto end_it = make_iter<Edge>( Edge(this, edges.size()) );
1103  return Range<Edge>(bgn_it, end_it);
1104 }
1105 
1106 inline void Mesh::check_element_size(unsigned int elem_idx) const
1107 {
1108  ASSERT(elem_idx < element_vec_.size())(elem_idx)(element_vec_.size()).error("Index of element is out of bound of element vector!");
1109 }
1110 
1111 /*
1112  * Output of internal flow data.
1113  */
1115 {
1116  START_TIMER("Mesh::output_internal_ngh_data");
1117 
1118  if (! raw_ngh_output_file.is_open()) return;
1119 
1120  // header
1121  raw_ngh_output_file << "// fields:\n//ele_id n_sides ns_side_neighbors[n] neighbors[n*ns] n_vb_neighbors vb_neighbors[n_vb]\n";
1123 
1124  int cit = 0;
1125 
1126  // map from higher dim elements to its lower dim neighbors, using gmsh IDs: ele->id()
1127  unsigned int undefined_ele_id = -1;
1129  for (auto ele : this->elements_range()) {
1130  if(ele->n_neighs_vb() > 0){
1131  for (unsigned int i = 0; i < ele->n_neighs_vb(); i++){
1132  ElementAccessor<3> higher_ele = ele->neigh_vb[i]->side()->element();
1133 
1134  auto search = neigh_vb_map.find(higher_ele.idx());
1135  if(search != neigh_vb_map.end()){
1136  // if found, add id to correct local side idx
1137  search->second[ele->neigh_vb[i]->side()->side_idx()] = ele.idx();
1138  }
1139  else{
1140  // if not found, create new vector, each side can have one vb neighbour
1141  std::vector<unsigned int> higher_ele_side_ngh(higher_ele->n_sides(), undefined_ele_id);
1142  higher_ele_side_ngh[ele->neigh_vb[i]->side()->side_idx()] = ele.idx();
1143  neigh_vb_map[higher_ele.idx()] = higher_ele_side_ngh;
1144  }
1145  }
1146  }
1147  }
1148 
1149  for (auto ele : this->elements_range()) {
1150  raw_ngh_output_file << ele.idx() << " ";
1151  raw_ngh_output_file << ele->n_sides() << " ";
1152 
1153  auto search_neigh = neigh_vb_map.end();
1154  for (unsigned int i = 0; i < ele->n_sides(); i++) {
1155  unsigned int n_side_neighs = ele.side(i)->edge().n_sides()-1; //n_sides - the current one
1156  // check vb neighbors (lower dimension)
1157  if(n_side_neighs == 0){
1158  //update search
1159  if(search_neigh == neigh_vb_map.end())
1160  search_neigh = neigh_vb_map.find(ele.idx());
1161 
1162  if(search_neigh != neigh_vb_map.end())
1163  if(search_neigh->second[i] != undefined_ele_id)
1164  n_side_neighs = 1;
1165  }
1166  raw_ngh_output_file << n_side_neighs << " ";
1167  }
1168 
1169  for (unsigned int i = 0; i < ele->n_sides(); i++) {
1170  Edge edge = ele.side(i)->edge();
1171  if(edge.n_sides() > 1){
1172  for (uint j = 0; j < edge.n_sides(); j++) {
1173  if(edge.side(j) != ele.side(i))
1174  raw_ngh_output_file << edge.side(j)->element().idx() << " ";
1175  }
1176  }
1177  //check vb neighbour
1178  else if(search_neigh != neigh_vb_map.end()
1179  && search_neigh->second[i] != undefined_ele_id){
1180  raw_ngh_output_file << search_neigh->second[i] << " ";
1181  }
1182  }
1183 
1184  // list higher dim neighbours
1185  raw_ngh_output_file << ele->n_neighs_vb() << " ";
1186  for (unsigned int i = 0; i < ele->n_neighs_vb(); i++)
1187  raw_ngh_output_file << ele->neigh_vb[i]->side()->element().idx() << " ";
1188 
1189  raw_ngh_output_file << endl;
1190  cit ++;
1191  }
1192  raw_ngh_output_file << "$EndFlowField\n" << endl;
1193 }
1194 
1195 
1197  // Copy boundary elements in temporary storage to the second part of the element vector
1198  for(ElementTmpData &e_data : bc_element_tmp_) {
1199  Element *ele = add_element_to_vector(e_data.elm_id);
1200  this->init_element(ele, e_data.elm_id, e_data.dim, e_data.region_idx,
1201  e_data.partition_id, e_data.node_ids);
1202  }
1203  // release memory
1204  vector<ElementTmpData>().swap(bc_element_tmp_);
1205 }
1206 
1207 
1208 void Mesh::permute_tetrahedron(unsigned int elm_idx, std::vector<unsigned int> permutation_vec)
1209 {
1210  ASSERT_LT_DBG(elm_idx, element_vec_.size());
1211  ASSERT_EQ_DBG(permutation_vec.size(), 4);
1212 
1213  std::array<unsigned int, 4> tmp_nodes;
1214  Element &elem = element_vec_[elm_idx];
1215  ASSERT_EQ_DBG(elem.dim(), 3);
1216 
1217  for(unsigned int i=0; i<elem.n_nodes(); i++)
1218  {
1219  tmp_nodes[i] = elem.nodes_[permutation_vec[i]];
1220  }
1221  elem.nodes_ = tmp_nodes;
1222 }
1223 
1224 
1225 void Mesh::permute_triangle(unsigned int elm_idx, std::vector<unsigned int> permutation_vec)
1226 {
1227  ASSERT_LT_DBG(elm_idx, element_vec_.size());
1228  ASSERT_EQ_DBG(permutation_vec.size(), 3);
1229 
1230  std::array<unsigned int, 4> tmp_nodes;
1231  Element &elem = element_vec_[elm_idx];
1232  ASSERT_EQ_DBG(elem.dim(), 2);
1233 
1234  for(unsigned int i=0; i<elem.n_nodes(); i++)
1235  {
1236  tmp_nodes[i] = elem.nodes_[permutation_vec[i]];
1237  }
1238  elem.nodes_ = tmp_nodes;
1239 }
1240 
1241 
1243  if (bc_mesh_ == nullptr) bc_mesh_ = new BCMesh(this);
1244  return bc_mesh_;
1245 }
1246 
1247 
1249  ASSERT_PTR(el_4_loc).error("Array 'el_4_loc' is not initialized. Did you call Partitioning::id_maps?\n");
1250 
1251  unsigned int i_proc, i_node, i_ghost_node, elm_node;
1252  unsigned int my_proc = el_ds->myp();
1253  unsigned int n_proc = el_ds->np();
1254 
1255  // distribute nodes between processes, every node is assigned to minimal process of elements that own node
1256  // fill node_proc vector with same values on all processes
1257  std::vector<unsigned int> node_proc( this->n_nodes(), n_proc );
1258  std::vector<bool> local_node_flag( this->n_nodes(), false );
1259 
1260  for ( auto elm : this->elements_range() ) {
1261  i_proc = elm.proc();
1262  for (elm_node=0; elm_node<elm->n_nodes(); elm_node++) {
1263  i_node = elm->node_idx(elm_node);
1264  if (i_proc == my_proc) local_node_flag[i_node] = true;
1265  if (i_proc < node_proc[i_node]) node_proc[i_node] = i_proc;
1266  }
1267  }
1268 
1269  unsigned int n_own_nodes=0, n_local_nodes=0; // number of own and ghost nodes
1270  for(uint i_proc : node_proc) if (i_proc == my_proc) n_own_nodes++;
1271  for(uint loc_flag : local_node_flag) if (loc_flag) n_local_nodes++;
1272 
1273  //DebugOut() << print_var(n_own_nodes) << print_var(n_local_nodes) << this->n_nodes();
1274  // create and fill node_4_loc_ (mapping local to global indexes)
1275  node_4_loc_ = new LongIdx [ n_local_nodes ];
1276  i_node=0;
1277  i_ghost_node = n_own_nodes;
1278  for (unsigned int i=0; i<this->n_nodes(); ++i) {
1279  if (local_node_flag[i]) {
1280  if (node_proc[i]==my_proc)
1281  node_4_loc_[i_node++] = i;
1282  else
1283  node_4_loc_[i_ghost_node++] = i;
1284  }
1285  }
1286 
1287  // Construct node distribution object, set number of local nodes (own+ghost)
1288  node_ds_ = new Distribution(n_own_nodes, PETSC_COMM_WORLD);
1289  node_ds_->get_lsizes_array(); // need to initialize lsizes data member
1291 
1292 }
1293 
1294 //-----------------------------------------------------------------------------
1295 // vim: set cindent:
int n_triangles
Definition: mesh.h:267
Distribution * el_ds
Parallel distribution of elements.
Definition: mesh.h:553
Class for the mesh partitioning. This should provide:
Definition: partitioning.hh:52
Iterator< ValueType > begin() const
void read_stream(istream &in, const Type::TypeBase &root_type, FileFormat format)
This method actually reads the given stream in.
unsigned int n_sides() const
Returns number of sides aligned with the edge.
Definition: accessors.hh:300
void output_internal_ngh_data()
Output of neighboring data into raw output.
Definition: mesh.cc:1114
const Element * element() const
Definition: accessors.hh:160
unsigned int n_nodes() const
Definition: elements.h:126
std::array< unsigned int, 4 > nodes_
indices to element&#39;s nodes
Definition: elements.h:111
vector< Element > element_vec_
Definition: mesh.h:505
BidirectionalMap< int > node_ids_
Maps node ids to indexes into vector node_vec_.
Definition: mesh.h:525
vector< vector< unsigned int > > const & node_elements()
Definition: mesh.cc:1054
#define ASSERT_EQ_DBG(a, b)
Definition of comparative assert macro (EQual) only for debug mode.
Definition: asserts.hh:332
Accessor to input data conforming to declared Array.
Definition: accessors.hh:567
void reserve(unsigned int init_size=0)
Reset data of map, reserve space for given size.
unsigned int n_local_nodes_
Hold number of local nodes (own + ghost), value is equal with size of node_4_loc array.
Definition: mesh.h:559
unsigned int * boundary_idx_
Definition: elements.h:80
void permute_tetrahedron(unsigned int elm_idx, std::vector< unsigned int > permutation_vec)
Permute nodes of 3D elements of given elm_idx.
Definition: mesh.cc:1208
bool is_boundary() const
Returns true if it is a Boundary region and false if it is a Bulk region.
Definition: region.hh:74
unsigned int uint
unsigned int bc_ele_idx_
Definition: mesh_data.hh:53
friend class Element
Definition: mesh.h:534
void count_side_types()
Definition: mesh.cc:360
int MPI_Comm
Definition: mpi.h:141
BCMesh * bc_mesh_
Boundary mesh, object is created only if it&#39;s necessary.
Definition: mesh.h:561
unsigned int side_idx() const
Returns local index of the side on the element.
Definition: accessors.hh:415
void check_and_finish()
Definition: mesh.cc:955
MapElementIDToRegionID el_to_reg_map_
Definition: region.hh:571
Reader for (slightly) modified input files.
void init_element_vector(unsigned int size)
Initialize element_vec_, set size and reset counters of boundary and bulk elements.
Definition: mesh.cc:1062
virtual const LongIdx * get_local_part()
Definition: mesh.cc:270
MixedMeshIntersections & mixed_intersections()
Definition: mesh.cc:763
Class Input::Type::Default specifies default value of keys of a Input::Type::Record.
Definition: type_record.hh:61
BidirectionalMap< int > element_ids_
Maps element ids to indexes into vector element_vec_.
Definition: mesh.h:517
Class for declaration of the input of type Bool.
Definition: type_base.hh:459
Edge edge() const
Returns pointer to the edge connected to the side.
void append(const ArmaMat< Type, nr, nc > &item)
Definition: armor.hh:726
void add_node(unsigned int node_id, arma::vec3 coords)
Add new node of given id and coordinates to mesh.
Definition: mesh.cc:1003
LongIdx * node_4_loc_
Index set assigning to local node index its global index.
Definition: mesh.h:555
#define MessageOut()
Macro defining &#39;message&#39; record of log.
Definition: logger.hh:255
DuplicateNodes * tree
Definition: mesh.h:255
unsigned int elem_idx_
Index of element in Mesh::element_vec_.
Definition: neighbours.h:137
int n_lines
Definition: mesh.h:266
static const Input::Type::Record & get_input_type()
Definition: partitioning.cc:49
ElementAccessor< 3 > element_accessor(unsigned int idx) const override
Overwrite Mesh::element_accessor()
Definition: bc_mesh.cc:88
static const unsigned int undef_idx
Definition: mesh.h:104
std::vector< BoundingBox > get_element_boxes()
Compute bounding boxes of elements contained in mesh.
Definition: mesh.cc:970
void init()
Definition: mesh.cc:160
ofstream raw_ngh_output_file
Definition: mesh.h:563
std::string format(CStringRef format_str, ArgList args)
Definition: format.h:3141
#define INPUT_CATCH(ExceptionType, AddressEITag, input_accessor)
Definition: accessors.hh:64
void create_node_element_lists()
Definition: mesh.cc:374
static unsigned int permutation_index(unsigned int p[n_nodes_per_side])
Definition: ref_element.cc:552
BCMesh * get_bc_mesh()
Create boundary mesh if doesn&#39;t exist and return it.
Definition: mesh.cc:1242
static const Input::Type::Record & get_input_type()
Definition: mesh.cc:74
int elem_index(int elem_id) const
For element of given elem_id returns index in element_vec_ or (-1) if element doesn&#39;t exist...
Definition: mesh.h:346
static Default obligatory()
The factory function to make an empty default value which is obligatory.
Definition: type_record.hh:110
std::shared_ptr< MixedMeshIntersections > intersections
Definition: mesh.h:247
double global_snap_radius() const
Maximal distance of observe point from Mesh relative to its size.
Definition: mesh.cc:994
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:469
Definition: mesh.h:78
unsigned int n_local_nodes() const
Definition: mesh.h:169
std::vector< EdgeData > edges
Vector of MH edges, this should not be part of the geometrical mesh.
Definition: mesh.h:528
Input::Record in_record_
Definition: mesh.h:493
std::string create_label_from_id(unsigned int id) const
Definition: region.cc:338
vector< vector< vector< unsigned int > > > side_nodes
Definition: mesh.h:280
SideIter side(const unsigned int loc_index)
int n_tetrahedras
Definition: mesh.h:268
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:347
virtual unsigned int n_nodes() const
Definition: mesh.h:126
vector< BoundaryData > boundary_
Definition: mesh.h:239
Boundary boundary(uint edge_idx) const
Definition: mesh.cc:260
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:998
int node_index(int node_id) const
For node of given node_id returns index in element_vec_ or (-1) if node doesn&#39;t exist.
Definition: mesh.h:358
ElementAccessor< 3 > element() const
Returns iterator to the element of the side.
virtual Partitioning * get_part()
Definition: mesh.cc:266
NodeAccessor< 3 > node(unsigned int ni) const
Definition: accessors.hh:200
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:304
Class for declaration of inputs sequences.
Definition: type_base.hh:346
virtual ElementAccessor< 3 > element_accessor(unsigned int idx) const
Create and return ElementAccessor to element of given idx.
Definition: mesh.cc:780
int n_exsides
Definition: mesh.h:263
void init(unsigned int dim, RegionIdx reg)
Definition: elements.cc:52
IteratorBase end() const
const unsigned int * get_lsizes_array()
get local sizes array
Armor::Array< double > nodes_
Definition: mesh.h:522
Region get_region(unsigned int id, unsigned int dim)
Definition: region.cc:151
unsigned int dim() const
Definition: elements.h:121
static Default optional()
The factory function to make an empty default value which is optional.
Definition: type_record.hh:124
#define OLD_ASSERT(...)
Definition: global_defs.h:131
unsigned int n_vb_neighbours() const
Definition: mesh.cc:239
ElementAccessor< 3 > element()
Definition: neighbours.h:161
void open_stream(Stream &stream) const
Definition: file_path.cc:211
virtual bool check_compatible_mesh(Mesh &mesh, vector< LongIdx > &bulk_elements_id, vector< LongIdx > &boundary_elements_id)
Definition: mesh.cc:833
bool opt_val(const string &key, Ret &value) const
unsigned int edge_idx_
Index of Edge in Mesh.
Definition: neighbours.h:138
void read_regions_from_input(Input::Array region_list)
Definition: mesh.cc:945
unsigned int boundary_loaded_size_
Count of boundary elements loaded from mesh file.
Definition: mesh.h:514
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:133
bool same_sides(const SideIter &si, vector< unsigned int > &side_nodes)
Definition: mesh.cc:439
Class for declaration of the input data that are floating point numbers.
Definition: type_base.hh:541
int find_elem_id(unsigned int pos) const
Return element id (in GMSH file) of element of given position in element vector.
Definition: mesh.h:352
unsigned int add_item(T val)
Add new item at the end position of map.
Mesh * mesh_
Definition: mesh_data.hh:54
unsigned int node_idx(unsigned int ni) const
Return index (in Mesh::node_vec) of ni-th node.
Definition: elements.h:72
void check_element_size(unsigned int elem_idx) const
Check if given index is in element_vec_.
Definition: mesh.cc:1106
void setup_topology()
Definition: mesh.cc:324
IntersectionSearch get_intersection_search()
Getter for input type selection for intersection search algorithm.
Definition: mesh.cc:154
Neighbour ** neigh_vb
Definition: elements.h:84
Region find_id(unsigned int id, unsigned int dim) const
Definition: region.cc:181
LongIdx * el_4_loc
Index set assigning to local element index its global index.
Definition: mesh.h:551
Range helper class.
friend class BCMesh
Definition: mesh.h:537
T get_root_interface() const
Returns the root accessor.
unsigned int edge_idx(unsigned int edg_idx) const
Return edge_idx of given index.
Definition: elements.h:136
friend class RegionSetBase
Definition: mesh.h:533
static const Input::Type::Selection & get_input_intersection_variant()
The definition of input record for selection of variant of file format.
Definition: mesh.cc:63
static FileName input()
The factory function for declaring type FileName for input files.
Definition: type_base.cc:526
SideIter side()
Definition: neighbours.h:145
Accessor to the data with type Type::Record.
Definition: accessors.hh:292
unsigned int edge_idx_
Definition: mesh_data.hh:49
#define NDEF
Definition: mesh.cc:59
const Ret val(const string &key) const
unsigned int n_sides() const
Definition: elements.h:132
#define xprintf(...)
Definition: system.hh:93
#define START_TIMER(tag)
Starts a timer with specified tag.
void reinit(uint size)
Definition: armor.hh:688
Class for O(log N) lookup for intersections with a set of bounding boxes.
Definition: bih_tree.hh:38
Selection & add_value(const int value, const std::string &key, const std::string &description="", TypeBase::attribute_map attributes=TypeBase::attribute_map())
Adds one new value with name given by key to the Selection.
unsigned int n_corners()
Definition: mesh.cc:244
LongIdx * row_4_el
Index set assigning to global element index the local index used in parallel vectors.
Definition: mesh.h:549
BoundingBox bounding_box() const
Definition: accessors.hh:208
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:503
void close()
Definition: region.cc:250
bool is_empty() const
Definition: accessors.hh:366
Region implicit_boundary_region()
Definition: region.cc:76
virtual Range< ElementAccessor< 3 > > elements_range() const
Returns range of bulk elements.
Definition: mesh.cc:1088
Element * add_element_to_vector(int id)
Adds element to mesh data structures (element_vec_, element_ids_), returns pointer to this element...
Definition: mesh.cc:1081
NodeAccessor< 3 > node(unsigned int idx) const
Create and return NodeAccessor to node of given idx.
Definition: mesh.cc:786
unsigned int np() const
get num of processors
unsigned int n_sides() const
Definition: mesh.cc:230
std::shared_ptr< BIHTree > bih_tree_
Definition: mesh.h:487
void swap(nlohmann::json &j1, nlohmann::json &j2) noexcept(is_nothrow_move_constructible< nlohmann::json >::value andis_nothrow_move_assignable< nlohmann::json >::value)
exchanges the values of two JSON objects
Definition: json.hpp:8688
vector< vector< unsigned int > > node_elements_
For each node the vector contains a list of elements that use this node.
Definition: mesh.h:343
NodeAccessor< 3 > node(unsigned int i) const
Returns node for given local index i on the side.
friend class Boundary
Definition: mesh.h:536
Accessor to the polymorphic input data of a type given by an AbstracRecord object.
Definition: accessors.hh:459
Region region() const
Definition: accessors.hh:165
void elements_id_maps(vector< LongIdx > &bulk_elements_id, vector< LongIdx > &boundary_elements_id) const
Definition: mesh.cc:792
void count_element_types()
Definition: mesh.cc:279
bool compare_points(const arma::vec3 &p1, const arma::vec3 &p2)
Definition: mesh.cc:825
const BIHTree & get_bih_tree()
Getter for BIH. Creates and compute BIH at first call.
Definition: mesh.cc:985
void create_boundary_elements()
Create boundary elements from data of temporary structure, this method MUST be call after read mesh f...
Definition: mesh.cc:1196
#define MPI_Comm_rank
Definition: mpi.h:236
void check_regions()
Definition: region.cc:462
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
unsigned int myp() const
get my processor
Definition: system.hh:65
void mark_used_region(unsigned int idx)
Definition: region.cc:236
Support classes for parallel programing.
Region add_region(unsigned int id, const std::string &label, unsigned int dim, const std::string &address="implicit")
Definition: region.cc:86
vector< Neighbour > vb_neighbours_
Definition: mesh.h:260
int LongIdx
Define type that represents indices of large arrays (elements, nodes, dofs etc.)
Definition: index_types.hh:24
Distribution * node_ds_
Parallel distribution of nodes. Depends on elements distribution.
Definition: mesh.h:557
FinishStatus finish(FinishStatus finish_type=FinishStatus::regular_) override
Finish declaration of the Record type.
Definition: type_record.cc:243
virtual unsigned int n_elements(bool boundary=false) const
Returns count of boundary or bulk elements.
Definition: mesh.h:337
void distribute_nodes()
Fill array node_4_loc_ and create object node_ds_ according to element distribution.
Definition: mesh.cc:1248
void find_point(const Space< 3 >::Point &point, std::vector< unsigned int > &result_list, bool full_list=false) const
Definition: bih_tree.cc:287
int n_sides_
Definition: mesh.h:264
RegionIdx region_idx_
Definition: elements.h:107
Class represents boundary part of mesh.
Definition: bc_mesh.hh:35
#define ASSERT_PTR(ptr)
Definition of assert macro checking non-null pointer (PTR)
Definition: asserts.hh:336
const Selection & close() const
Close the Selection, no more values can be added.
RegionDB region_db_
Definition: mesh.h:478
MPI_Comm comm_
Definition: mesh.h:498
#define ASSERT_DBG(expr)
bool find_lower_dim_element(vector< unsigned int > &element_list, unsigned int dim, unsigned int &element_idx)
Definition: mesh.cc:419
int pid_
Id # of mesh partition.
Definition: elements.h:89
int n_insides
Definition: mesh.h:262
void print_region_table(std::ostream &stream) const
Definition: region.cc:410
void intersect_element_lists(vector< unsigned int > const &nodes_list, vector< unsigned int > &intersection_element_list)
Definition: mesh.cc:387
void modify_element_ids(const RegionDB::MapElementIDToRegionID &map)
Definition: mesh.cc:296
#define WarningOut()
Macro defining &#39;warning&#39; record of log.
Definition: logger.hh:258
#define MPI_COMM_WORLD
Definition: mpi.h:123
void make_edge_permutations()
Definition: mesh.cc:649
double tetrahedron_jacobian() const
Class RefElement defines numbering of vertices, sides, calculation of normal vectors etc...
static Input::Type::Abstract & get_input_type()
Definition: region_set.cc:25
Range< NodeAccessor< 3 > > node_range() const
Returns range of nodes.
Definition: mesh.cc:1094
SideIter * side_
Definition: mesh_data.hh:32
Edge edge(uint edge_idx) const
Definition: mesh.cc:254
const LongIdx * get_loc_part() const
Definition: partitioning.cc:85
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:1010
Record type proxy class.
Definition: type_record.hh:182
static const unsigned int undefined_dim
Definition: region.hh:332
bool is_valid() const
Returns false if the region has undefined/invalid value.
Definition: region.hh:78
IntersectionSearch
Types of search algorithm for finding intersection candidates.
Definition: mesh.h:93
void element_to_neigh_vb()
Definition: mesh.cc:730
void init_element(Element *ele, unsigned int elm_id, unsigned int dim, RegionIdx region_idx, unsigned int partition_id, std::vector< unsigned int > node_ids)
Initialize element.
Definition: mesh.cc:1033
friend class Edge
Definition: mesh.h:531
Mesh()
Definition: mesh.cc:100
static FileName output()
The factory function for declaring type FileName for input files.
Definition: type_base.cc:533
unsigned int n_neighs_vb_
of neighbours, V-B type (comp.)
Definition: elements.h:91
void permute_triangle(unsigned int elm_idx, std::vector< unsigned int > permutation_vec)
Permute nodes of 2D elements of given elm_idx.
Definition: mesh.cc:1225
#define ASSERT_PTR_DBG(ptr)
Definition of assert macro checking non-null pointer (PTR) only for debug mode.
Definition: asserts.hh:340
unsigned int idx() const
Return local idx of element in boundary / bulk part of element vector.
Definition: accessors.hh:181
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
SideIter side(const unsigned int i) const
Gets side iterator of the i -th side.
void clear()
Clear the content. Do not release memory.
Range< Edge > edge_range() const
Returns range of edges.
Definition: mesh.cc:1100
Template for classes storing finite set of named values.
Implementation of range helper class.
unsigned int bulk_size_
Count of bulk elements.
Definition: mesh.h:511
std::shared_ptr< Partitioning > part_
Definition: mesh.h:482
void make_neighbours_and_edges()
Definition: mesh.cc:456
vector< ElementTmpData > bc_element_tmp_
Hold data of boundary elements during reading mesh (allow to preserve correct order during reading of...
Definition: mesh.h:508
Mesh * mesh_
Pointer to Mesh to which belonged.
Definition: neighbours.h:136
#define ASSERT_EQ(a, b)
Definition of comparative assert macro (EQual)
Definition: asserts.hh:328
void init_node_vector(unsigned int size)
Initialize node_vec_, set size.
Definition: mesh.cc:1074
unsigned int id() const
Returns id of the region (using RegionDB)
Definition: region.cc:38
Main class for computation of intersection of meshes of combined dimensions.
unsigned int n_nodes() const
Returns number of nodes of the side.
Definition: accessors.hh:407
virtual ~Mesh()
Destructor.
Definition: mesh.cc:205
unsigned int idx() const
Returns a global index of the region.
Definition: region.hh:82
#define ASSERT_LT_DBG(a, b)
Definition of comparative assert macro (Less Than) only for debug mode.
Definition: asserts.hh:300
unsigned int n_sides
Definition: mesh_data.hh:29