Flow123d  JS_before_hm-1712-g4d968e368
accessors_impl.hh
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 accessors_impl.hh
15  * @brief Implementation of the inline functions of the mesh accessors.
16  */
17 
18 /*******************************************************************************
19  * ElementAccessor IMPLEMENTATION
20  *******************************************************************************/
21 /**
22  * Default invalid accessor.
23  */
24 template <int spacedim> inline
26 : mesh_(nullptr)
27 {}
28 
29 /**
30  * Regional accessor.
31  */
32 template <int spacedim> inline
34 : dim_(undefined_dim_),
35  mesh_(mesh),
36  r_idx_(r_idx)
37 {}
38 
39 /**
40  * Element accessor.
41  */
42 template <int spacedim> inline
43 ElementAccessor<spacedim>::ElementAccessor(const Mesh *mesh, unsigned int idx)
44 : mesh_(mesh),
45  boundary_(idx>=mesh->n_elements()),
46  element_idx_(idx),
47  r_idx_(element()->region_idx())
48 {
49  dim_=element()->dim();
50 }
51 
52 template <int spacedim> inline
54  ASSERT(!is_regional()).error("Do not call inc() for regional accessor!");
55  element_idx_++;
56  r_idx_ = element()->region_idx();
57  dim_=element()->dim();
58  boundary_ = (element_idx_>=mesh_->n_elements());
59 }
60 
61 template <int spacedim> inline
62 vector<arma::vec3> ElementAccessor<spacedim>::vertex_list() const {
63  vector<arma::vec3> vertices(element()->n_nodes());
64  for(unsigned int i=0; i<element()->n_nodes(); i++) vertices[i]=*node(i);
65  return vertices;
66 }
67 
68 template <int spacedim> inline
70 {
71  ASSERT(dim() == 3)(dim()).error("Cannot provide Jacobian for dimension other than 3.");
72  return arma::dot( arma::cross(*( node(1) ) - *( node(0) ),
73  *( node(2) ) - *( node(0) )),
74  *( node(3) ) - *( node(0) )
75  );
76 }
77 
78 /**
79  * SET THE "METRICS" FIELD IN STRUCT ELEMENT
80  */
81 template <int spacedim> inline
83  switch (dim()) {
84  case 0:
85  return 1.0;
86  break;
87  case 1:
88  return arma::norm(*( node(1) ) - *( node(0) ) , 2);
89  break;
90  case 2:
91  return
92  arma::norm(
93  arma::cross(*( node(1) ) - *( node(0) ), *( node(2) ) - *( node(0) )),
94  2
95  ) / 2.0 ;
96  break;
97  case 3:
98  return fabs(
99  arma::dot(
100  arma::cross(*( node(1) ) - *( node(0) ), *( node(2) ) - *( node(0) )),
101  *( node(3) ) - *( node(0) ) )
102  ) / 6.0;
103  break;
104  }
105  return 1.0;
106 }
107 
108 /**
109  * SET THE "CENTRE[]" FIELD IN STRUCT ELEMENT
110  */
111 template <int spacedim> inline
112 arma::vec::fixed<spacedim> ElementAccessor<spacedim>::centre() const {
113  ASSERT(is_valid()).error("Invalid element accessor.");
114  if (is_regional() ) return arma::vec::fixed<spacedim>();
115 
116  arma::vec::fixed<spacedim> centre;
117  centre.zeros();
118 
119  for (unsigned int li=0; li<element()->n_nodes(); li++) {
120  centre += *node( li );
121  }
122  centre /= (double) element()->n_nodes();
123  return centre;
124 }
125 
126 
127 template <int spacedim> inline
129  if (dim_==3) {
130  double sum_faces=0;
131  double face[4];
132  for(unsigned int i=0; i<4; i++) sum_faces+=( face[i]=side(i)->measure());
133 
134  double sum_pairs=0;
135  for(unsigned int i=0;i<3;i++)
136  for(unsigned int j=i+1;j<4;j++) {
137  unsigned int i_line = RefElement<3>::line_between_faces(i,j);
138  arma::vec line = *node(RefElement<3>::interact(Interaction<0,1>(i_line))[1]) - *node(RefElement<3>::interact(Interaction<0,1>(i_line))[0]);
139  sum_pairs += face[i]*face[j]*arma::dot(line, line);
140  }
141  double regular = (2.0*sqrt(2.0/3.0)/9.0); // regular tetrahedron
142  return fabs( measure()
143  * pow( sum_faces/sum_pairs, 3.0/4.0))/ regular;
144 
145  }
146  if (dim_==2) {
147  return fabs(
148  measure()/
149  pow(
150  arma::norm(*node(1) - *node(0), 2)
151  *arma::norm(*node(2) - *node(1), 2)
152  *arma::norm(*node(0) - *node(2), 2)
153  , 2.0/3.0)
154  ) / ( sqrt(3.0) / 4.0 ); // regular triangle
155  }
156  return 1.0;
157 }
158 
159 template <int spacedim> inline
160 SideIter ElementAccessor<spacedim>::side(const unsigned int loc_index) {
161  return SideIter( Side(mesh_, element_idx_, loc_index) );
162 }
163 
164 template <int spacedim> inline
165 const SideIter ElementAccessor<spacedim>::side(const unsigned int loc_index) const {
166  return SideIter( Side(mesh_, element_idx_, loc_index) );
167 }
168 
169 
170 
171 /*******************************************************************************
172  * Edge IMPLEMENTATION
173  *******************************************************************************/
174 
175 inline Edge::Edge()
176 : mesh_(nullptr),
177  edge_idx_(Mesh::undef_idx)
178 {}
179 
180 inline Edge::Edge(const Mesh *mesh, unsigned int edge_idx)
181 : mesh_(mesh),
182  edge_idx_(edge_idx)
183 {}
184 
185 inline const EdgeData* Edge::edge_data() const
186 {
187  ASSERT_DBG(is_valid());
189  return &mesh_->edges[edge_idx_];
190 }
191 
192 inline SideIter Edge::side(const unsigned int i) const {
193  return edge_data()->side_[i];
194 }
195 
196 
197 
198 
199 /*******************************************************************************
200  * Side IMPLEMENTATION
201  *******************************************************************************/
202 
203 inline Side::Side()
204 : mesh_(NULL), elem_idx_(0), side_idx_(0)
205 {}
206 
207 inline Side::Side(const Mesh * mesh, unsigned int elem_idx, unsigned int set_lnum)
208 : mesh_(mesh), elem_idx_(elem_idx), side_idx_(set_lnum)
209 {
211 }
212 
213 inline unsigned int Side::dim() const {
214  return element()->dim()-1;
215  }
216 
217 // returns true for all sides either on boundary or connected to vb neigboring
218 inline bool Side::is_external() const {
219  return edge().n_sides() == 1;
220 }
221 
222 // returns true for all sides either on boundary or connected to vb neigboring
223 inline bool Side::is_boundary() const {
224  return is_external() && cond_idx() != Mesh::undef_idx;
225 }
226 
227 inline NodeAccessor<3> Side::node(unsigned int i) const {
228  int i_n = mesh_->side_nodes[dim()][side_idx_][i];
229 
230  return element().node( i_n );
231 }
232 
234  ASSERT( is_valid() ).error("Wrong use of uninitialized accessor.\n");
235  return mesh_->element_accessor( elem_idx_ );
236 }
237 
238 inline unsigned int Side::edge_idx() const {
239  return element()->edge_idx(side_idx_);
240 }
241 
242 inline Edge Side::edge() const {
243  return mesh_->edge(edge_idx());
244 }
245 
246 inline Boundary Side::cond() const {
247  return mesh_->boundary(cond_idx());
248 }
249 
250 inline unsigned int Side::cond_idx() const {
251  if (element()->boundary_idx_ == nullptr) return Mesh::undef_idx;
252  else return element()->boundary_idx_[side_idx_];
253 }
254 
255 
256 
257 /*******************************************************************************
258  * Boundary IMPLEMENTATION
259  *******************************************************************************/
261 : boundary_data_(nullptr)
262 {}
263 
264 inline Boundary::Boundary(BoundaryData* boundary_data)
265 : boundary_data_(boundary_data)
266 {}
267 
269 {
270  ASSERT_DBG(is_valid());
272 }
273 
275 {
276  ASSERT_DBG(is_valid());
278 }
279 
281 {
282  return element_accessor().region();
283 }
284 
286 {
287  ASSERT_DBG(is_valid());
289 }
Side::edge
Edge edge() const
Returns pointer to the edge connected to the side.
Definition: accessors_impl.hh:242
Side::side_idx_
unsigned int side_idx_
Local # of side in element (to remove it, we heve to remove calc_side_rhs)
Definition: accessors.hh:453
Boundary
Definition: accessors.hh:320
ElementAccessor::dim_
unsigned int dim_
Dimension of reference element.
Definition: accessors.hh:244
Armor::vec
ArmaVec< double, N > vec
Definition: armor.hh:885
Side::elem_idx_
unsigned int elem_idx_
Index of element in Mesh::element_vec_.
Definition: accessors.hh:452
Side::is_external
bool is_external() const
Returns true for all sides either on boundary or connected to vb neigboring.
Definition: accessors_impl.hh:218
ElementAccessor::tetrahedron_jacobian
double tetrahedron_jacobian() const
Definition: accessors_impl.hh:69
Edge::mesh_
const Mesh * mesh_
Pointer to the mesh owning the node.
Definition: accessors.hh:305
RefElement
Definition: ref_element.hh:163
Mesh::undef_idx
static const unsigned int undef_idx
Definition: mesh.h:121
Side::Side
Side()
Default invalid side accessor constructor.
Definition: accessors_impl.hh:203
ASSERT
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library.
Definition: asserts.hh:347
Element::dim
unsigned int dim() const
Definition: elements.h:121
Side::mesh_
const Mesh * mesh_
Pointer to Mesh to which belonged.
Definition: accessors.hh:451
Edge::n_sides
unsigned int n_sides() const
Returns number of sides aligned with the edge.
Definition: accessors.hh:300
BoundaryData::edge_idx_
unsigned int edge_idx_
Definition: mesh_data.hh:49
Side::is_boundary
bool is_boundary() const
Returns true for side on the boundary.
Definition: accessors_impl.hh:223
Mesh::element_vec_
vector< Element > element_vec_
Definition: mesh.h:567
ASSERT_DBG
#define ASSERT_DBG(expr)
Definition: include_fadbad.hh:28
Side::dim
unsigned int dim() const
Returns dimension of the side, that is dimension of the element minus one.
Definition: accessors_impl.hh:213
ElementAccessor< 3 >
Side::elem_idx
unsigned int elem_idx() const
Returns index of element in Mesh::element_vec_.
Definition: accessors.hh:419
BoundaryData
Definition: mesh_data.hh:40
Edge::edge_data
const EdgeData * edge_data() const
Getter for edge data from mesh.
Definition: accessors_impl.hh:185
Mesh::boundary
Boundary boundary(uint edge_idx) const
Definition: mesh.cc:250
EdgeData
Definition: mesh_data.hh:25
Side::cond
Boundary cond() const
Definition: accessors_impl.hh:246
Edge::is_valid
bool is_valid() const
Definition: accessors.hh:279
ElementAccessor::element
const Element * element() const
Definition: accessors.hh:160
Edge::Edge
Edge()
Default invalid edge accessor constructor.
Definition: accessors_impl.hh:175
BoundaryData::bc_ele_idx_
unsigned int bc_ele_idx_
Definition: mesh_data.hh:53
ElementAccessor::quality_measure_smooth
double quality_measure_smooth() const
Definition: accessors_impl.hh:128
Element
Definition: elements.h:39
EdgeData::side_
SideIter * side_
Definition: mesh_data.hh:32
Region
Definition: region.hh:146
Side::edge_idx
unsigned int edge_idx() const
Returns global index of the edge connected to the side.
Definition: accessors_impl.hh:238
Side::cond_idx
unsigned int cond_idx() const
Returns global index of the prescribed boundary condition.
Definition: accessors_impl.hh:250
Edge::side
SideIter side(const unsigned int i) const
Gets side iterator of the i -th side.
Definition: accessors_impl.hh:192
ElementAccessor::inc
void inc()
Incremental function of the Element iterator.
Definition: accessors_impl.hh:53
Boundary::is_valid
bool is_valid() const
Definition: accessors.hh:331
Mesh::side_nodes
vector< vector< vector< unsigned int > > > side_nodes
Definition: mesh.h:316
Side
Definition: accessors.hh:361
Side::element
ElementAccessor< 3 > element() const
Returns iterator to the element of the side.
Definition: accessors_impl.hh:233
ElementAccessor::vertex_list
vector< arma::vec3 > vertex_list() const
Return list of element vertices.
Definition: accessors_impl.hh:62
Boundary::element_accessor
ElementAccessor< 3 > element_accessor()
Definition: accessors_impl.hh:274
ElementAccessor::node
NodeAccessor< 3 > node(unsigned int ni) const
Definition: accessors.hh:200
Mesh::check_element_size
void check_element_size(unsigned int elem_idx) const
Check if given index is in element_vec_.
Definition: mesh.cc:1333
Interaction
Definition: ref_element.hh:157
Boundary::element
Element * element()
Definition: accessors_impl.hh:285
Element::edge_idx
unsigned int edge_idx(unsigned int edg_idx) const
Return edge_idx of given index.
Definition: elements.h:136
Mesh
Definition: mesh.h:77
ElementAccessor::ElementAccessor
ElementAccessor()
Default invalid accessor.
Definition: accessors_impl.hh:25
Mesh::edges
std::vector< EdgeData > edges
Vector of MH edges, this should not be part of the geometrical mesh.
Definition: mesh.h:590
Edge
Definition: accessors.hh:265
RefElement::line_between_faces
static unsigned int line_between_faces(unsigned int f1, unsigned int f2)
RegionIdx
Definition: region.hh:67
ElementAccessor::region
Region region() const
Definition: accessors.hh:165
Boundary::boundary_data_
BoundaryData * boundary_data_
Definition: accessors.hh:351
NodeAccessor
Definition: mesh.h:55
ASSERT_LT_DBG
#define ASSERT_LT_DBG(a, b)
Definition of comparative assert macro (Less Than) only for debug mode.
Definition: asserts.hh:300
Boundary::Boundary
Boundary()
Definition: accessors_impl.hh:260
Side::is_valid
bool is_valid() const
Returns true if the side has assigned element.
Definition: accessors.hh:423
Side::node
NodeAccessor< 3 > node(unsigned int i) const
Returns node for given local index i on the side.
Definition: accessors_impl.hh:227
ElementAccessor::side
SideIter side(const unsigned int loc_index)
Definition: accessors_impl.hh:160
Mesh::element_accessor
virtual ElementAccessor< 3 > element_accessor(unsigned int idx) const
Create and return ElementAccessor to element of given idx.
Definition: mesh.cc:908
BoundaryData::mesh_
Mesh * mesh_
Definition: mesh_data.hh:54
Edge::edge_idx_
unsigned int edge_idx_
Index into Mesh::edges vector.
Definition: accessors.hh:307
Boundary::region
Region region()
Definition: accessors_impl.hh:280
SideIter
Definition: accessors.hh:461
Element::boundary_idx_
unsigned int * boundary_idx_
Definition: elements.h:80
Mesh::edge
Edge edge(uint edge_idx) const
Definition: mesh.cc:244
Boundary::edge
Edge edge()
Definition: accessors_impl.hh:268
ElementAccessor::measure
double measure() const
Computes the measure of the element.
Definition: accessors_impl.hh:82
ElementAccessor::centre
arma::vec::fixed< spacedim > centre() const
Computes the barycenter.
Definition: accessors_impl.hh:112