Flow123d  master-a7f2151e0
bc_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 bc_mesh.cc
15  * @ingroup mesh
16  * @brief Mesh construction
17  */
18 
19 
20 #include "system/index_types.hh"
21 #include "mesh/bc_mesh.hh"
22 #include "mesh/accessors.hh"
23 #include "mesh/partitioning.hh"
24 #include "mesh/neighbours.h"
25 #include "mesh/range_wrapper.hh"
26 #include "la/distribution.hh"
27 
28 
29 
30 BCMesh::BCMesh(Mesh* parent_mesh)
31 : parent_mesh_(parent_mesh),
32  local_part_(nullptr)
33 {
35  this->init_element_vector(1); // necessary to allocate 1 element due to elements_range() which otherwise fails for empty BCMesh
36  this->init_node_vector(0);
37  this->nodes_ = parent_mesh_->nodes_;
39 }
40 
41 
43 {
44  if (local_part_!=nullptr) delete local_part_;
45 }
46 
48 {
49  vector<LongIdx> loc_el_ids;
50 
51  // loop local parent elements and their sides to find boundary elements
52  for (unsigned int iel = 0; iel<parent_mesh_->get_el_ds()->lsize(); iel++)
53  {
54  auto parent_el = parent_mesh_->element_accessor(parent_mesh_->get_el_4_loc()[iel]);
55  for (unsigned int sid=0; sid<parent_el->n_sides(); sid++)
56  {
57  if (parent_el.side(sid)->is_boundary())
58  {
59  loc_el_ids.push_back(parent_el.side(sid)->cond().element_accessor().idx());
60  }
61  }
62  }
63  this->el_ds = new Distribution(loc_el_ids.size(), PETSC_COMM_WORLD);
64 
65  this->el_4_loc = new LongIdx[loc_el_ids.size()];
66  for (unsigned int i=0; i<loc_el_ids.size(); i++) this->el_4_loc[i] = loc_el_ids[i];
67 
68  this->row_4_el = new LongIdx[n_elements()];
69  vector<LongIdx> row_4_loc_el(n_elements(), 0);
70  for (unsigned int i=0; i<loc_el_ids.size(); i++)
71  row_4_loc_el[loc_el_ids[i]] = i + this->el_ds->begin();
72  MPI_Allreduce(row_4_loc_el.data(), this->row_4_el, n_elements(), MPI_LONG_IDX, MPI_MAX, PETSC_COMM_WORLD);
73 
75 }
76 
77 
79  return parent_mesh_->get_part();
80 }
81 
83  if (local_part_ == nullptr) {
84  local_part_ = new LongIdx[this->n_elements()];
85  unsigned int bc_ele_idx;
86  for (auto ele : parent_mesh_->elements_range())
87  if (ele->boundary_idx_ != NULL)
88  for (unsigned int i=0; i<ele->n_sides(); ++i)
89  if ((int)ele->boundary_idx_[i] != -1) {
90  bc_ele_idx = parent_mesh_->boundary_[ ele->boundary_idx_[i] ].bc_ele_idx_;
91  local_part_[bc_ele_idx] = parent_mesh_->get_local_part()[ele.idx()];
92  }
93  }
94  return local_part_;
95 }
96 
97 
98 std::shared_ptr<EquivalentMeshMap> BCMesh::check_compatible_mesh( Mesh & input_mesh) {
99  return parent_mesh_->check_compatible_mesh(input_mesh);
100 }
101 
102 
103 Boundary BCMesh::boundary(unsigned int) const
104 {
105  ASSERT_PERMANENT( false );
106  return Boundary();
107 }
108 
109 
111 {
112  Neighbour neighbour;
113  EdgeData *edg = nullptr;
114  unsigned int ngh_element_idx;
115  unsigned int last_edge_idx = Mesh::undef_idx;
116 
117  neighbour.mesh_ = this;
118 
120 
121  // pointers to created edges
122  edges.resize(0); // be sure that edges are empty
123 
125  vector<unsigned int> intersection_list; // list of elements in intersection of node element lists
126 
127  // Now we go through all element sides and create edges and neighbours
128  for (auto e : this->elements_range()) {
129  for (unsigned int s=0; s<e->n_sides(); s++)
130  {
131  // skip sides that were already found
132  if (e->edge_idx(s) != Mesh::undef_idx) continue;
133 
134  // Find all elements that share this side.
135  side_nodes.resize(e.side(s)->n_nodes());
136  for (unsigned n=0; n<e.side(s)->n_nodes(); n++) side_nodes[n] = e.side(s)->node(n).idx();
137  intersect_element_lists(side_nodes, intersection_list);
138 
139  bool is_neighbour = find_lower_dim_element(intersection_list, e->dim(), ngh_element_idx);
140 
141  if (is_neighbour) { // edge connects elements of different dimensions
142  // Initialize for the neighbour case.
143  neighbour.elem_idx_ = ngh_element_idx;
144  } else { // edge connects only elements of the same dimension
145  // Initialize for the edge case.
146  last_edge_idx=edges.size();
147  edges.resize(last_edge_idx+1);
148  edg = &( edges.back() );
149  edg->n_sides = 0;
150  edg->side_ = new struct SideIter[ intersection_list.size() ];
151  if (e->dim() > 0)
152  if (intersection_list.size() > max_edge_sides_[e->dim()-1])
153  max_edge_sides_[e->dim()-1] = intersection_list.size();
154 
155  if (intersection_list.size() <= 1) {
156  // outer edge, create boundary object as well
157  edg->n_sides=1;
158  edg->side_[0] = e.side(s);
159  element_vec_[e.idx()].edge_idx_[s] = last_edge_idx;
160 
161  continue; // next side of element e
162  }
163  }
164 
165  // go through the elements connected to the edge or neighbour
166  // setup neigbour or edge
167  for( vector<unsigned int>::iterator isect = intersection_list.begin(); isect!=intersection_list.end(); ++isect) {
168  ElementAccessor<3> elem = this->element_accessor(*isect);
169  for (unsigned int ecs=0; ecs<elem->n_sides(); ecs++) {
170  if (elem->edge_idx(ecs) != Mesh::undef_idx) continue; // ??? This should not happen.
171  SideIter si = elem.side(ecs);
172  if ( same_sides( si, side_nodes) ) {
173  if (is_neighbour) {
174  // create a new edge and neighbour for this side, and element to the edge
175  last_edge_idx=edges.size();
176  edges.resize(last_edge_idx+1);
177  edg = &( edges.back() );
178  edg->n_sides = 1;
179  edg->side_ = new struct SideIter[1];
180  edg->side_[0] = si;
181  element_vec_[elem.idx()].edge_idx_[ecs] = last_edge_idx;
182 
183  neighbour.edge_idx_ = last_edge_idx;
184 
185  vb_neighbours_.push_back(neighbour); // copy neighbour with this edge setting
186  } else {
187  // connect the side to the edge, and side to the edge
188  ASSERT_PTR(edg);
189  edg->side_[ edg->n_sides++ ] = si;
190  ASSERT(last_edge_idx != Mesh::undef_idx);
191  element_vec_[elem.idx()].edge_idx_[ecs] = last_edge_idx;
192  }
193  break; // next element from intersection list
194  }
195  } // search for side of other connected element
196  } // connected elements
197 
198  if (! is_neighbour)
199  ASSERT_EQ( (unsigned int) edg->n_sides, intersection_list.size())(e.input_id())(s).error("Missing edge sides.");
200  } // for element sides
201  } // for elements
202 
203  MessageOut().fmt( "Created {} edges and {} neighbours on boundary mesh.\n", edges.size(), vb_neighbours_.size() );
204 }
MeshBase::nodes_
shared_ptr< Armor::Array< double > > nodes_
Definition: mesh.h:310
MeshBase::region_db_
std::shared_ptr< RegionDB > region_db_
Definition: mesh.h:340
Boundary
Definition: accessors.hh:349
Neighbour::edge_idx_
unsigned int edge_idx_
Index of Edge in Mesh.
Definition: neighbours.h:138
BCMesh::get_local_part
const LongIdx * get_local_part() override
Overwrite Mesh::get_local_part()
Definition: bc_mesh.cc:82
MeshBase::row_4_el
LongIdx * row_4_el
Index set assigning to global element index the local index used in parallel vectors.
Definition: mesh.h:327
neighbours.h
Neighbour::elem_idx_
unsigned int elem_idx_
Index of element in Mesh::element_vec_.
Definition: neighbours.h:137
ASSERT
#define ASSERT(expr)
Definition: asserts.hh:351
MeshBase::get_el_ds
Distribution * get_el_ds() const
Definition: mesh.h:119
Mesh::check_compatible_mesh
virtual std::shared_ptr< EquivalentMeshMap > check_compatible_mesh(Mesh &input_mesh)
Definition: mesh.cc:913
distribution.hh
Support classes for parallel programing.
bc_mesh.hh
MeshBase::el_4_loc
LongIdx * el_4_loc
Index set assigning to local element index its global index.
Definition: mesh.h:329
MPI_MAX
#define MPI_MAX
Definition: mpi.h:197
MeshBase::undef_idx
static const unsigned int undef_idx
Definition: mesh.h:105
std::vector< LongIdx >
ElementAccessor< 3 >
MeshBase::node_ids_
shared_ptr< BidirectionalMap< int > > node_ids_
Maps node ids to indexes into vector node_vec_.
Definition: mesh.h:313
Partitioning
Class for the mesh partitioning. This should provide:
Definition: partitioning.hh:52
EdgeData
Definition: mesh_data.hh:25
Neighbour
Definition: neighbours.h:117
MeshBase::elements_range
Range< ElementAccessor< 3 > > elements_range() const
Returns range of mesh elements.
Definition: mesh.cc:1168
index_types.hh
BCMesh::parent_mesh_
Mesh * parent_mesh_
Pointer to parent (bulk) mesh.
Definition: bc_mesh.hh:74
BCMesh::local_part_
LongIdx * local_part_
Distribution of boundary elements to processors.
Definition: bc_mesh.hh:77
EdgeData::side_
SideIter * side_
Definition: mesh_data.hh:32
MeshBase::same_sides
bool same_sides(const SideIter &si, vector< unsigned int > &side_nodes)
Definition: mesh.cc:606
Distribution
Definition: distribution.hh:50
MeshBase::edges
std::vector< EdgeData > edges
Vector of MH edges, this should not be part of the geometrical mesh.
Definition: mesh.h:299
accessors.hh
Mesh::get_local_part
const LongIdx * get_local_part() override
Definition: mesh.cc:338
ASSERT_PERMANENT
#define ASSERT_PERMANENT(expr)
Allow use shorter versions of macro names if these names is not used with external library.
Definition: asserts.hh:348
MeshBase::vb_neighbours_
vector< Neighbour > vb_neighbours_
Vector of compatible neighbourings.
Definition: mesh.h:302
BCMesh::make_neighbours_and_edges
void make_neighbours_and_edges()
Definition: bc_mesh.cc:110
MeshBase::element_accessor
ElementAccessor< 3 > element_accessor(unsigned int idx) const
Create and return ElementAccessor to element of given idx.
Definition: mesh.cc:866
ASSERT_EQ
#define ASSERT_EQ(a, b)
Definition of comparative assert macro (EQual) only for debug mode.
Definition: asserts.hh:333
BCMesh::init_distribution
void init_distribution()
setup distribution of elements and related vectors
Definition: bc_mesh.cc:47
MPI_LONG_IDX
#define MPI_LONG_IDX
Definition: index_types.hh:30
partitioning.hh
MeshBase::init_node_vector
void init_node_vector(unsigned int size)
Initialize node_vec_, set size.
Definition: mesh.cc:1146
Mesh::get_part
Partitioning * get_part() override
Definition: mesh.cc:334
Element::edge_idx
unsigned int edge_idx(unsigned int edg_idx) const
Return edge_idx of given index.
Definition: elements.h:135
EdgeData::n_sides
unsigned int n_sides
Definition: mesh_data.hh:29
LongIdx
int LongIdx
Define type that represents indices of large arrays (elements, nodes, dofs etc.)
Definition: index_types.hh:24
BCMesh::BCMesh
BCMesh(Mesh *parent_mesh)
Definition: bc_mesh.cc:30
Mesh
Definition: mesh.h:359
MeshBase::intersect_element_lists
void intersect_element_lists(vector< unsigned int > const &nodes_list, vector< unsigned int > &intersection_element_list)
Definition: mesh.cc:552
Element::n_sides
unsigned int n_sides() const
Definition: elements.h:131
MeshBase::el_ds
Distribution * el_ds
Parallel distribution of elements.
Definition: mesh.h:331
MeshBase::init_element_vector
void init_element_vector(unsigned int size)
Initialize element_vec_, set size and reset counters of boundary and bulk elements.
Definition: mesh.cc:1136
MPI_Allreduce
#define MPI_Allreduce(sendbuf, recvbuf, count, datatype, op, comm)
Definition: mpi.h:612
BCMesh::boundary
Boundary boundary(unsigned int) const override
Definition: bc_mesh.cc:103
MeshBase::get_el_4_loc
LongIdx * get_el_4_loc() const
Definition: mesh.h:122
Neighbour::mesh_
MeshBase * mesh_
Pointer to Mesh to which belonged.
Definition: neighbours.h:136
BCMesh::get_part
Partitioning * get_part() override
Overwrite Mesh::get_part()
Definition: bc_mesh.cc:78
MeshBase::side_nodes
vector< vector< vector< unsigned int > > > side_nodes
Definition: mesh.h:252
BCMesh::check_compatible_mesh
std::shared_ptr< EquivalentMeshMap > check_compatible_mesh(Mesh &input_mesh) override
Overwrite Mesh::check_compatible_mesh()
Definition: bc_mesh.cc:98
MeshBase::find_lower_dim_element
bool find_lower_dim_element(vector< unsigned int > &element_list, unsigned int dim, unsigned int &element_idx)
Definition: mesh.cc:584
std::vector::data
T data
Definition: doxy_dummy_defs.hh:7
ElementAccessor::idx
unsigned int idx() const
We need this method after replacing Region by RegionIdx, and movinf RegionDB instance into particular...
Definition: accessors.hh:215
MeshBase::n_elements
unsigned int n_elements() const
Definition: mesh.h:111
Mesh::boundary_
vector< BoundaryData > boundary_
Definition: mesh.h:502
BCMesh::~BCMesh
~BCMesh() override
Destructor.
Definition: bc_mesh.cc:42
MeshBase::create_node_element_lists
void create_node_element_lists()
Definition: mesh.cc:539
ASSERT_PTR
#define ASSERT_PTR(ptr)
Definition of assert macro checking non-null pointer (PTR) only for debug mode.
Definition: asserts.hh:341
ElementAccessor::side
SideIter side(const unsigned int loc_index)
Definition: accessors_impl.hh:131
MeshBase::max_edge_sides_
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:305
MeshBase::element_vec_
vector< Element > element_vec_
Definition: mesh.h:293
MeshBase::n_nodes
unsigned int n_nodes() const
Definition: mesh.h:171
SideIter
Definition: accessors.hh:490
range_wrapper.hh
Implementation of range helper class.
Distribution::begin
unsigned int begin(int proc) const
get starting local index
Definition: distribution.hh:109
MessageOut
#define MessageOut()
Macro defining 'message' record of log.
Definition: logger.hh:275