dune-grid-glue  2.7.0
extractor.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 /*
4  * Filename: extractor.hh
5  * Version: 1.0
6  * Created on: Oct 05, 2009
7  * Author: Christian Engwer
8  * ---------------------------------
9  * Project: dune-grid-glue
10  * Description: base class for all grid extractors
11  *
12  */
18 #ifndef DUNE_GRIDGLUE_EXTRACTORS_EXTRACTOR_HH
19 #define DUNE_GRIDGLUE_EXTRACTORS_EXTRACTOR_HH
20 
21 #include <vector>
22 #include <map>
23 #include <algorithm>
24 #include <dune/common/exceptions.hh>
25 #include <dune/common/fvector.hh>
26 #include <dune/common/version.hh>
27 #include <dune/grid/common/geometry.hh>
28 #include <dune/grid/common/grid.hh>
29 #include <dune/grid/common/mcmgmapper.hh>
30 #include <dune/geometry/multilineargeometry.hh>
31 
32 namespace Dune {
33 
34  namespace GridGlue {
35 
42 template<typename GV, int cd>
43 class Extractor
44 {
45 
46 public:
47 
48  static constexpr auto dimworld = GV::dimensionworld;
49  static constexpr auto dim = GV::dimension;
50  static constexpr auto codim = cd;
51 
52  static constexpr int cube_corners = 1 << (dim-codim);
53 
54  typedef GV GridView;
55  typedef typename GridView::Grid Grid;
56 
57  typedef typename GV::Grid::ctype ctype;
58  typedef Dune::FieldVector<ctype, dimworld> Coords;
59  typedef Dune::FieldVector<ctype, dim> LocalCoords;
60 
61  typedef typename GV::Traits::template Codim<dim>::Entity Vertex;
62  typedef typename Vertex::EntitySeed VertexSeed;
63 
64  typedef typename GV::Traits::template Codim<0>::Entity Element;
65  typedef typename Element::EntitySeed ElementSeed;
66 
67  typedef std::vector<unsigned int> VertexVector;
68 
69 #if DUNE_VERSION_NEWER(DUNE_GRID, 2, 6)
70  using CellMapper = MultipleCodimMultipleGeomTypeMapper<GridView>;
71 #else
72  using CellMapper = MultipleCodimMultipleGeomTypeMapper<GridView, MCMGElementLayout>;
73 #endif
74  // typedef typename CellMapper::IndexType IndexType;
75  typedef int IndexType;
76 public:
77 
78  // transformations
79  typedef Dune::MultiLinearGeometry<ctype, dim-codim, dimworld> Geometry;
80  typedef Dune::MultiLinearGeometry<ctype, dim-codim, dim> LocalGeometry;
81 
82 protected:
83  /************************** PRIVATE SUBCLASSES **********************/
84 
89  struct CornerInfo
90  {
91  unsigned int idx : 28;
92  unsigned int num : 4;
93  };
94 
96  {
98  {}
99 
100  CoordinateInfo(unsigned int index_, IndexType vtxindex_)
101  : vtxindex(vtxindex_), index(index_)
102  {}
103 
106 
109 
111  unsigned int index;
112  };
113 
117  struct VertexInfo
118  {
119  VertexInfo(unsigned int idx_, const Vertex& p_) : idx(idx_), p(p_.seed())
120  {}
121  unsigned int idx;
123  };
124 
125 
129  struct ElementInfo
130  {
131  ElementInfo(unsigned int idx_, const Element& p_, unsigned int f_) : idx(idx_), faces(f_), p(p_.seed())
132  {}
133 
135  unsigned int idx : 28;
136 
138  unsigned int faces : 4;
139 
142  };
143 
144 
149  {
151  /*
152  * TODO: move default value of `geometryType_` to member declaration
153  * when removing support for older dune-geometry
154  */
155 #if DUNE_VERSION_NEWER(DUNE_GEOMETRY, 2, 6)
156  : geometryType_(GeometryTypes::simplex(dim-codim))
157  {}
158 #else
159  {
160  geometryType_.makeSimplex(dim-codim);
161  }
162 #endif
163 
164  SubEntityInfo(IndexType parent_, unsigned int num_in_parent_,
165  const Dune::GeometryType& geometryType)
166  : parent(parent_), num_in_parent(num_in_parent_), geometryType_(geometryType)
167  {}
168 
169  unsigned int nCorners() const
170  {
171  return Dune::ReferenceElements<ctype, dim-codim>::general(geometryType_).size(dim-codim);
172  }
173 
176 
178  unsigned int num_in_parent : 3;
179 
181  Dune::GeometryType geometryType_;
182 
189  CornerInfo corners[cube_corners]; // sim = numer of vertices in a simplex
190  };
191 
192 
193  typedef std::map<IndexType, ElementInfo> ElementInfoMap;
194  typedef std::map<IndexType, VertexInfo> VertexInfoMap;
195 
196  /************************** MEMBER VARIABLES ************************/
197 
199  const GridView gv_;
200 
201  /* Geometrical and Topological Information */
202 
204  std::vector<CoordinateInfo> coords_;
205 
207  std::vector<SubEntityInfo> subEntities_;
208 
215 
222 
224 
225 public:
226 
227  /* C O N S T R U C T O R S A N D D E S T R U C T O R S */
228 
233  Extractor(const GV& gv)
234  : gv_(gv)
235 #if DUNE_VERSION_NEWER(DUNE_GRID, 2, 6)
236  , cellMapper_(gv, mcmgElementLayout())
237 #else
238  , cellMapper_(gv)
239 #endif
240  {}
241 
242  /* F U N C T I O N A L I T Y */
243 
247  void clear()
248  {
249  // this is an inofficial way on how to free the memory allocated
250  // by a std::vector
251  {
252  std::vector<CoordinateInfo> dummy;
253  coords_.swap(dummy);
254  }
255  {
256  std::vector<SubEntityInfo> dummy;
257  subEntities_.swap(dummy);
258  }
259 
260  // ...then clear the maps themselves, too
261  vtxInfo_.clear();
262  elmtInfo_.clear();
263  }
264 
265 
266  /* G E T T E R S */
267 
273  void getCoords(std::vector<Dune::FieldVector<ctype, dimworld> >& coords) const
274  {
275  coords.resize(coords_.size());
276  for (unsigned int i = 0; i < coords_.size(); ++i)
277  coords[i] = coords_[i].coord;
278  }
279 
280 
285  unsigned int nCoords() const
286  {
287  return coords_.size();
288  }
289 
291  void getGeometryTypes(std::vector<Dune::GeometryType>& geometryTypes) const
292  {
293  geometryTypes.resize(subEntities_.size());
294  for (size_t i=0; i<subEntities_.size(); i++)
295  geometryTypes[i] = subEntities_[i].geometryType_;
296  }
297 
298 
302  void getFaces(std::vector<VertexVector>& faces) const
303  {
304  faces.resize(subEntities_.size());
305  for (unsigned int i = 0; i < subEntities_.size(); ++i) {
306  faces[i].resize(subEntities_[i].nCorners());
307  for (unsigned int j = 0; j < subEntities_[i].nCorners(); ++j)
308  faces[i][j] = subEntities_[i].corners[j].idx;
309  }
310  }
311 
312 
321  bool faceIndices(const Element& e, int& first, int& count) const
322  {
323  typename ElementInfoMap::const_iterator it =
324  elmtInfo_.find(cellMapper_.map(e));
325  if (it == elmtInfo_.end())
326  {
327  first = -1;
328  count = 0;
329  return false;
330  }
331  // the iterator is valid, fill the out params
332  first = it->second.idx;
333  count = it->second.faces;
334  return true;
335  }
336 
337 
343  int indexInInside(unsigned int index) const
344  {
345  return index < subEntities_.size() ? subEntities_[index].num_in_parent : -1;
346  }
347 
348  // /**
349  // * @brief tests that a given entry in the extraction set does have local couplings
350  // * @todo parallel interface
351  // */
352  // bool contains (unsigned int global, unsigned int & local) const
353  // {
354  // local = global;
355  // return true;
356  // }
357 
361  const GridView & gridView() const
362  {
363  return gv_;
364  }
365 
366  const Grid& grid() const
367  {
368  return gv_.grid();
369  }
370 
377  Element
378  element(unsigned int index) const
379  {
380  if (index >= subEntities_.size())
381  DUNE_THROW(Dune::GridError, "invalid face index");
382  const ElementSeed seed = elmtInfo_.at(subEntities_[index].parent).p;
383  return grid().entity(seed);
384  }
385 
386 #if 1
387 
393  Vertex
394  vertex(unsigned int index) const
395  {
396  if (index >= coords_.size())
397  DUNE_THROW(Dune::GridError, "invalid coordinate index");
398  const VertexSeed seed = vtxInfo_.at(coords_[index].vtxindex).p;
399  return grid().entity(seed);
400  }
401 #endif
402 
404  Geometry geometry(unsigned int index) const;
405 
407  LocalGeometry geometryLocal(unsigned int index) const;
408 
409 };
410 
411 
413 template<typename GV, int cd>
414 typename Extractor<GV,cd>::Geometry Extractor<GV,cd>::geometry(unsigned int index) const
415 {
416  std::vector<Coords> corners(subEntities_[index].nCorners());
417  for (unsigned int i = 0; i < subEntities_[index].nCorners(); ++i)
418  corners[i] = coords_[subEntities_[index].corners[i].idx].coord;
419 
420  return Geometry(subEntities_[index].geometryType_, corners);
421 }
422 
423 
425 template<typename GV, int cd>
427 {
428  std::vector<LocalCoords> corners(subEntities_[index].nCorners());
429 
430  // get face info
431  const SubEntityInfo & face = subEntities_[index];
432  Dune::GeometryType facetype = subEntities_[index].geometryType_;
433 
434  // get reference element
435  const auto elmtseed = elmtInfo_.at(face.parent).p;
436  const auto elmt = grid().entity(elmtseed);
437  const Dune::GeometryType celltype = elmt.type();
438  const auto& re = Dune::ReferenceElements<ctype, dim>::general(celltype);
439  for (unsigned int i = 0; i < subEntities_[index].nCorners(); ++i)
440  corners[i] = re.position(face.corners[i].num,dim);
441 
442  return LocalGeometry(facetype, corners);
443 }
444 
445 } // namespace GridGlue
446 
447 } // namespace Dune
448 
449 #endif // DUNE_GRIDGLUE_EXTRACTORS_EXTRACTOR_HH
Dune::GridGlue::Extractor::Element
GV::Traits::template Codim< 0 >::Entity Element
Definition: extractor.hh:64
Dune::GridGlue::Extractor::codim
static constexpr auto codim
Definition: extractor.hh:50
Dune::GridGlue::Extractor::ElementInfo::p
ElementSeed p
the entity seed for the element
Definition: extractor.hh:141
Dune
Definition: gridglue.hh:35
Dune::GridGlue::Extractor::SubEntityInfo::geometryType_
Dune::GeometryType geometryType_
The GeometryType of the subentity.
Definition: extractor.hh:181
Dune::GridGlue::Extractor::VertexInfo::VertexInfo
VertexInfo(unsigned int idx_, const Vertex &p_)
Definition: extractor.hh:119
Dune::GridGlue::Extractor::ElementInfo::ElementInfo
ElementInfo(unsigned int idx_, const Element &p_, unsigned int f_)
Definition: extractor.hh:131
Dune::GridGlue::Extractor::elmtInfo_
ElementInfoMap elmtInfo_
a map enabling faster access to elements and faces
Definition: extractor.hh:221
Dune::GridGlue::Extractor::getFaces
void getFaces(std::vector< VertexVector > &faces) const
Get the corners of the extracted subentities.
Definition: extractor.hh:302
Dune::GridGlue::Extractor::CoordinateInfo::CoordinateInfo
CoordinateInfo(unsigned int index_, IndexType vtxindex_)
Definition: extractor.hh:100
Dune::GridGlue::Extractor::SubEntityInfo::SubEntityInfo
SubEntityInfo(IndexType parent_, unsigned int num_in_parent_, const Dune::GeometryType &geometryType)
Definition: extractor.hh:164
Dune::GridGlue::Extractor::CoordinateInfo::vtxindex
IndexType vtxindex
the index of the parent element (from index set)
Definition: extractor.hh:105
Dune::GridGlue::Extractor::VertexVector
std::vector< unsigned int > VertexVector
Definition: extractor.hh:67
Dune::GridGlue::Extractor::VertexInfo::idx
unsigned int idx
Definition: extractor.hh:121
Dune::GridGlue::Extractor::ElementInfo::faces
unsigned int faces
the number of extracted faces for this element
Definition: extractor.hh:138
Dune::GridGlue::Extractor::CoordinateInfo::index
unsigned int index
the index of this coordinate (in internal storage scheme) // NEEDED??
Definition: extractor.hh:111
Dune::GridGlue::Extractor::LocalCoords
Dune::FieldVector< ctype, dim > LocalCoords
Definition: extractor.hh:59
Dune::GridGlue::Extractor::coords_
std::vector< CoordinateInfo > coords_
all information about the corner vertices of the extracted
Definition: extractor.hh:204
Dune::GridGlue::Extractor::CoordinateInfo
Definition: extractor.hh:96
Dune::GridGlue::Extractor::cube_corners
static constexpr int cube_corners
Definition: extractor.hh:52
Dune::GridGlue::Extractor::gv_
const GridView gv_
the grid object to extract the surface from
Definition: extractor.hh:199
Dune::GridGlue::Extractor::dim
static constexpr auto dim
Definition: extractor.hh:49
Dune::GridGlue::Extractor::geometry
Geometry geometry(unsigned int index) const
Get world geometry of the extracted face.
Definition: extractor.hh:414
Dune::GridGlue::Extractor::ElementSeed
Element::EntitySeed ElementSeed
Definition: extractor.hh:65
Dune::GridGlue::Extractor::element
Element element(unsigned int index) const
gets the parent element for a given face index, throws an exception if index not valid
Definition: extractor.hh:378
Dune::GridGlue::Extractor::SubEntityInfo
Holds some information about an element's subentity involved in a coupling.
Definition: extractor.hh:149
Dune::GridGlue::Extractor::SubEntityInfo::SubEntityInfo
SubEntityInfo()
Definition: extractor.hh:150
Dune::GridGlue::Extractor::cellMapper_
CellMapper cellMapper_
Definition: extractor.hh:223
Dune::GridGlue::Extractor::SubEntityInfo::nCorners
unsigned int nCorners() const
Definition: extractor.hh:169
Dune::GridGlue::Extractor::SubEntityInfo::num_in_parent
unsigned int num_in_parent
the number of the face in the parent element
Definition: extractor.hh:178
Dune::GridGlue::Extractor::CornerInfo::num
unsigned int num
element corner
Definition: extractor.hh:92
Dune::GridGlue::Extractor::SubEntityInfo::parent
IndexType parent
the index of the parent element (from index set)
Definition: extractor.hh:175
Dune::GridGlue::Extractor::getGeometryTypes
void getGeometryTypes(std::vector< Dune::GeometryType > &geometryTypes) const
Get the list of geometry types.
Definition: extractor.hh:291
Dune::GridGlue::Extractor::ctype
GV::Grid::ctype ctype
Definition: extractor.hh:57
Dune::GridGlue::Extractor::geometryLocal
LocalGeometry geometryLocal(unsigned int index) const
Get geometry of the extracted face in element coordinates.
Definition: extractor.hh:426
Dune::GridGlue::Extractor::IndexType
int IndexType
Definition: extractor.hh:75
Dune::GridGlue::Extractor::CornerInfo
Helpful struct holding one index for the coordinate (vertex) to which it is associated and the elemen...
Definition: extractor.hh:90
Dune::GridGlue::Extractor::ElementInfo::idx
unsigned int idx
the index of this element's first face in the internal list of extracted faces
Definition: extractor.hh:135
Dune::GridGlue::Extractor::nCoords
unsigned int nCoords() const
getter for the count of coordinates
Definition: extractor.hh:285
Dune::GridGlue::Extractor::dimworld
static constexpr auto dimworld
Definition: extractor.hh:48
Dune::GridGlue::Extractor::Coords
Dune::FieldVector< ctype, dimworld > Coords
Definition: extractor.hh:58
Dune::GridGlue::Extractor::getCoords
void getCoords(std::vector< Dune::FieldVector< ctype, dimworld > > &coords) const
getter for the coordinates array
Definition: extractor.hh:273
Dune::GridGlue::Extractor::Vertex
GV::Traits::template Codim< dim >::Entity Vertex
Definition: extractor.hh:61
Dune::GridGlue::Extractor::VertexInfo
simple struct holding a vertex pointer and an index
Definition: extractor.hh:118
Dune::GridGlue::Extractor::gridView
const GridView & gridView() const
give access to the Dune::GridView where this Patch belongs to
Definition: extractor.hh:361
Dune::GridGlue::Extractor
Provides codimension-independent methods for grid extraction.
Definition: extractor.hh:44
Dune::GridGlue::Extractor::clear
void clear()
delete everything build up so far and free the memory
Definition: extractor.hh:247
Dune::GridGlue::Extractor::indexInInside
int indexInInside(unsigned int index) const
gets the number face in the parent element
Definition: extractor.hh:343
Dune::GridGlue::Extractor::GridView
GV GridView
Definition: extractor.hh:54
Dune::GridGlue::Extractor::CoordinateInfo::CoordinateInfo
CoordinateInfo()
Definition: extractor.hh:97
Dune::GridGlue::Extractor::Grid
GridView::Grid Grid
Definition: extractor.hh:55
Dune::GridGlue::Extractor::VertexInfo::p
VertexSeed p
Definition: extractor.hh:122
Dune::GridGlue::Extractor::SubEntityInfo::corners
CornerInfo corners[cube_corners]
the corner indices plus the numbers of the vertices in the parent element
Definition: extractor.hh:189
Dune::GridGlue::Extractor::faceIndices
bool faceIndices(const Element &e, int &first, int &count) const
gets index of first subentity as well as the total number of subentities that were extracted from thi...
Definition: extractor.hh:321
Dune::GridGlue::Extractor< GV, 1 >::CellMapper
MultipleCodimMultipleGeomTypeMapper< GridView, MCMGElementLayout > CellMapper
Definition: extractor.hh:72
Dune::GridGlue::Extractor::vtxInfo_
VertexInfoMap vtxInfo_
a map enabling faster access to vertices and coordinates
Definition: extractor.hh:214
Dune::GridGlue::Extractor::VertexSeed
Vertex::EntitySeed VertexSeed
Definition: extractor.hh:62
Dune::GridGlue::Extractor::ElementInfo
simple struct holding an element seed and an index
Definition: extractor.hh:130
Dune::GridGlue::Extractor::ElementInfoMap
std::map< IndexType, ElementInfo > ElementInfoMap
Definition: extractor.hh:193
Dune::GridGlue::Extractor::Extractor
Extractor(const GV &gv)
Constructor.
Definition: extractor.hh:233
Dune::GridGlue::Extractor::VertexInfoMap
std::map< IndexType, VertexInfo > VertexInfoMap
Definition: extractor.hh:194
Dune::GridGlue::Extractor::vertex
Vertex vertex(unsigned int index) const
gets the vertex for a given coordinate index throws an exception if index not valid
Definition: extractor.hh:394
Dune::GridGlue::Extractor::grid
const Grid & grid() const
Definition: extractor.hh:366
Dune::GridGlue::Extractor::CoordinateInfo::coord
Coords coord
the coordinate
Definition: extractor.hh:108
Dune::GridGlue::Extractor::LocalGeometry
Dune::MultiLinearGeometry< ctype, dim-codim, dim > LocalGeometry
Definition: extractor.hh:80
Dune::GridGlue::Extractor::Geometry
Dune::MultiLinearGeometry< ctype, dim-codim, dimworld > Geometry
Definition: extractor.hh:79
Dune::GridGlue::Extractor::CornerInfo::idx
unsigned int idx
index of the vertex
Definition: extractor.hh:91
Dune::GridGlue::Extractor::subEntities_
std::vector< SubEntityInfo > subEntities_
all information about the extracted subEntities
Definition: extractor.hh:207