Flow123d  release_2.2.0-36-g163dc99
accessors.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.hh
15  * @brief
16  */
17 
18 #ifndef ACCESSORS_HH_
19 #define ACCESSORS_HH_
20 
21 #include "mesh/bounding_box.hh"
22 #include "mesh/mesh_types.hh"
23 #include "mesh/region.hh"
24 #include "mesh/elements.h"
25 #include "mesh/mesh.h"
26 #include <armadillo>
27 
28 /**
29  * Element accessor templated just by dimension of the embedding space, used by Fields.
30  * This should allow algorithms over elements where dimension of particular element is runtime parameter.
31  *
32  * This class suites as interface of Fields to the mesh elements, in particular this accessor knows directly
33  * the region, and also can be used as an accessor that works on the whole region if used by Fields that do not depend on
34  * particular elements as FieldConstant, FiledFormula, and FieldPython.
35  *
36  * TODO:
37  * - make this kind of accessor subclass of FieldCommonBase or at least move it into src/fields
38  * since it has functionality particular for Fields
39  *
40  * Ideas:
41  * need function to calculate intersection (object) of two ElementAccessors, but this definitely should be templated by
42  * dimension of the ref. element (or rather shape of ref. element), here we can have case dispatch
43  *
44  */
45 template <int spacedim>
46 class ElementAccessor {
47 public:
48  /**
49  * Default invalid accessor.
50  */
52  : mesh_(NULL)
53  {}
54 
55  /**
56  * Regional accessor.
57  */
58  ElementAccessor(const Mesh *mesh, RegionIdx r_idx)
59  : dim_(undefined_dim_), mesh_(mesh), r_idx_(r_idx)
60  {}
61 
62  /**
63  * Element accessor.
64  */
65  ElementAccessor(const Mesh *mesh, unsigned int idx, bool boundary)
66  : mesh_(mesh), boundary_(boundary), element_idx_(idx), r_idx_(element()->region_idx())
67  {
68  dim_=element()->dim();
69  }
70 
71  inline bool is_regional() const {
72  return dim_ == undefined_dim_;
73  }
74 
75  inline bool is_elemental() const {
76  return ( is_valid() && ! is_regional() );
77  }
78 
79  inline bool is_valid() const {
80  return mesh_ != NULL;
81  }
82 
83  inline unsigned int dim() const
84  { return dim_; }
85 
86  inline const Element * element() const {
87  if (boundary_) return (Element *)(mesh_->bc_elements(element_idx_)) ;
88  else return (Element *)(mesh_->element(element_idx_)) ;
89  }
90 
91  inline arma::vec::fixed<spacedim> centre() const {
92  OLD_ASSERT(is_valid(), "Invalid element accessor.");
93  if (is_regional() ) return arma::vec::fixed<spacedim>();
94  else return element()->centre();
95  }
96 
97 
98  inline Region region() const
99  { return Region( r_idx_, mesh_->region_db()); }
100 
101  inline RegionIdx region_idx() const
102  { return r_idx_; }
103 
104  /// We need this method after replacing Region by RegionIdx, and movinf RegionDB instance into particular mesh
105  //inline unsigned int region_id() const {
106  // return region().id();
107  //}
108 
109  inline bool is_boundary() const {
110  return boundary_;
111  }
112 
113  inline unsigned int idx() const {
114  return element_idx_;
115  }
116 private:
117  /**
118  * When dim_ == undefined_dim_ ; the value of element_idx_ is invalid.
119  * Is used for ElementAccessors for whole region
120  */
121  static const unsigned int undefined_dim_ = 100;
122 
123  /// Dimension of reference element.
124  unsigned int dim_;
125 
126  /// Pointer to the mesh owning the element.
127  const Mesh *mesh_;
128  /// True if the element is boundary, i.e. stored in Mesh::bc_elements, bulk elements are stored in Mesh::element
129  bool boundary_;
130 
131  /// Index into Mesh::bc_elements or Mesh::element array.
132  unsigned int element_idx_;
133 
134  /// Region index.
136 };
137 
138 
139 
140 
141 /******************************************************************* implementations
142  *
143  *
144  */
145 /*
146 template<int spacedim>
147 const BoundingBox &ElementAccessor<spacedim>::bounding_box() {
148  return box_;
149 }
150 */
151 
152 #endif /* ACCESSORS_HH_ */
const Element * element() const
Definition: accessors.hh:86
unsigned int dim_
Dimension of reference element.
Definition: accessors.hh:124
bool is_boundary() const
We need this method after replacing Region by RegionIdx, and movinf RegionDB instance into particular...
Definition: accessors.hh:109
Definition: mesh.h:97
bool is_valid() const
Definition: accessors.hh:79
const Mesh * mesh_
Pointer to the mesh owning the element.
Definition: accessors.hh:127
const RegionDB & region_db() const
Definition: mesh.h:155
unsigned int element_idx_
Index into Mesh::bc_elements or Mesh::element array.
Definition: accessors.hh:132
unsigned int dim() const
#define OLD_ASSERT(...)
Definition: global_defs.h:131
static const unsigned int undefined_dim_
Definition: accessors.hh:121
bool is_elemental() const
Definition: accessors.hh:75
ElementVector bc_elements
Definition: mesh.h:236
bool boundary_
True if the element is boundary, i.e. stored in Mesh::bc_elements, bulk elements are stored in Mesh::...
Definition: accessors.hh:129
Region region() const
Definition: accessors.hh:98
RegionIdx r_idx_
Region index.
Definition: accessors.hh:135
ElementAccessor(const Mesh *mesh, unsigned int idx, bool boundary)
Definition: accessors.hh:65
arma::vec3 centre() const
Definition: elements.cc:120
RegionIdx region_idx() const
Definition: accessors.hh:101
arma::vec::fixed< spacedim > centre() const
Definition: accessors.hh:91
ElementAccessor(const Mesh *mesh, RegionIdx r_idx)
Definition: accessors.hh:58
unsigned int dim() const
Definition: accessors.hh:83
unsigned int idx() const
Definition: accessors.hh:113
ElementVector element
Vector of elements of the mesh.
Definition: mesh.h:228
bool is_regional() const
Definition: accessors.hh:71