dune-grid-glue  2.4-git
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 #if DUNE_VERSION_NEWER(DUNE_GEOMETRY,2,3)
31 #include <dune/geometry/multilineargeometry.hh>
32 #else
33 #include <dune/geometry/genericgeometry/geometry.hh>
34 #endif
35 
36 namespace Dune {
37 
38  namespace GridGlue {
39 
46 template<typename GV, int cd>
47 class Extractor
48 {
49 
50 public:
51 
52  enum {dimworld = GV::dimensionworld};
53  enum {dim = GV::dimension};
54  enum {codim = cd};
55 
56  enum
57  {
59  };
60 
61  typedef GV GridView;
62  typedef typename GridView::Grid Grid;
63 
64  typedef typename GV::Grid::ctype ctype;
65  typedef Dune::FieldVector<ctype, dimworld> Coords;
66  typedef Dune::FieldVector<ctype, dim> LocalCoords;
67 
68  typedef typename GV::Traits::template Codim<dim>::EntityPointer VertexPtr;
69  typedef typename GV::Traits::template Codim<dim>::Entity Vertex;
70  typedef typename Vertex::EntitySeed VertexSeed;
71 
72  typedef typename GV::Traits::template Codim<0>::EntityPointer ElementPtr;
73  typedef typename GV::Traits::template Codim<0>::Entity Element;
74  typedef typename Element::EntitySeed ElementSeed;
75  typedef typename GV::Traits::template Codim<0>::Iterator ElementIter;
76 
77  typedef std::vector<unsigned int> VertexVector;
78 
79  typedef Dune::MultipleCodimMultipleGeomTypeMapper<GridView, MCMGElementLayout> CellMapper;
80  // typedef typename CellMapper::IndexType IndexType;
81  typedef int IndexType;
82 public:
83 
84  // transformations
85 #if DUNE_VERSION_NEWER(DUNE_GEOMETRY,2,3)
86  // We don't need the caching, but the uncached MultiLinearGeometry is not defined at all vertices
87  typedef Dune::CachedMultiLinearGeometry<ctype, dim-codim, dimworld> Geometry;
88  typedef Dune::CachedMultiLinearGeometry<ctype, dim-codim, dim> LocalGeometry;
89 #else
90  typedef Dune::GenericGeometry::BasicGeometry<dim-codim, Dune::GenericGeometry::DefaultGeometryTraits<ctype,dim-codim,dimworld> > Geometry;
91  typedef Dune::GenericGeometry::BasicGeometry<dim-codim, Dune::GenericGeometry::DefaultGeometryTraits<ctype,dim-codim,dim> > LocalGeometry;
92 #endif
93 
94 protected:
95  /************************** PRIVATE SUBCLASSES **********************/
96 
101  struct CornerInfo
102  {
103  unsigned int idx : 28;
104  unsigned int num : 4;
105  };
106 
108  {
110  {}
111 
112  CoordinateInfo(unsigned int index_, IndexType vtxindex_)
113  : vtxindex(vtxindex_), index(index_)
114  {}
115 
117  IndexType vtxindex;
118 
120  Coords coord;
121 
123  unsigned int index;
124  };
125 
129  struct VertexInfo
130  {
131  VertexInfo(unsigned int idx_, const Vertex& p_) : idx(idx_), p(p_.seed())
132  {}
133  unsigned int idx;
134  VertexSeed p;
135  };
136 
137 
141  struct ElementInfo
142  {
143  ElementInfo(unsigned int idx_, const Element& p_, unsigned int f_) : idx(idx_), faces(f_), p(p_.seed())
144  {}
145 
147  unsigned int idx : 28;
148 
150  unsigned int faces : 4;
151 
153  ElementSeed p;
154  };
155 
156 
161  {
163  {
164  geometryType_.makeSimplex(dim-codim);
165  }
166 
167  SubEntityInfo(IndexType parent_, unsigned int num_in_parent_,
168  const Dune::GeometryType& geometryType)
169  : parent(parent_), num_in_parent(num_in_parent_), geometryType_(geometryType)
170  {}
171 
172  unsigned int nCorners() const
173  {
174 #if DUNE_VERSION_NEWER(DUNE_GEOMETRY,2,3)
175  return Dune::ReferenceElements<ctype, dim-codim>::general(geometryType_).size(dim-codim);
176 #else
177  return Dune::GenericReferenceElements<ctype, dim-codim>::general(geometryType_).size(dim-codim);
178 #endif
179  }
180 
182  IndexType parent;
183 
185  unsigned int num_in_parent : 3;
186 
188  Dune::GeometryType geometryType_;
189 
196  CornerInfo corners[cube_corners]; // sim = numer of vertices in a simplex
197  };
198 
199 
200  typedef std::map<IndexType, ElementInfo* > ElementInfoMap;
201  typedef std::map<IndexType, VertexInfo* > VertexInfoMap;
202 
203  /************************** MEMBER VARIABLES ************************/
204 
206  const GridView gv_;
207 
208  /* Geometrical and Topological Information */
209 
211  std::vector<CoordinateInfo> coords_;
212 
214  std::vector<SubEntityInfo> subEntities_;
215 
221  VertexInfoMap vtxInfo_;
222 
228  ElementInfoMap elmtInfo_;
229 
230  CellMapper cellMapper_;
231 
232 public:
233 
234  /* 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 */
235 
240  Extractor(const GV& gv)
241  : gv_(gv), cellMapper_(gv)
242  {}
243 
245  ~Extractor();
246 
247  /* F U N C T I O N A L I T Y */
248 
252  void clear()
253  {
254  // this is an inofficial way on how to free the memory allocated
255  // by a std::vector
256  {
257  std::vector<CoordinateInfo> dummy;
258  coords_.swap(dummy);
259  }
260  {
261  std::vector<SubEntityInfo> dummy;
262  subEntities_.swap(dummy);
263  }
264 
265  // first free all manually allocated vertex/element info items...
266  for (typename VertexInfoMap::iterator it = vtxInfo_.begin();
267  it != vtxInfo_.end(); ++it)
268  if (it->second != NULL)
269  delete it->second;
270  for (typename ElementInfoMap::iterator it = elmtInfo_.begin();
271  it != elmtInfo_.end(); ++it)
272  if (it->second != NULL)
273  delete it->second;
274  // ...then clear the maps themselves, too
275  vtxInfo_.clear();
276  elmtInfo_.clear();
277  }
278 
279 
280  /* G E T T E R S */
281 
287  void getCoords(std::vector<Dune::FieldVector<ctype, dimworld> >& coords) const
288  {
289  coords.resize(coords_.size());
290  for (unsigned int i = 0; i < coords_.size(); ++i)
291  coords[i] = coords_[i].coord;
292  }
293 
294 
299  unsigned int nCoords() const
300  {
301  return coords_.size();
302  }
303 
305  void getGeometryTypes(std::vector<Dune::GeometryType>& geometryTypes) const
306  {
307  geometryTypes.resize(subEntities_.size());
308  for (size_t i=0; i<subEntities_.size(); i++)
309  geometryTypes[i] = subEntities_[i].geometryType_;
310  }
311 
312 
316  void getFaces(std::vector<VertexVector>& faces) const
317  {
318  faces.resize(subEntities_.size());
319  for (unsigned int i = 0; i < subEntities_.size(); ++i) {
320  faces[i].resize(subEntities_[i].nCorners());
321  for (unsigned int j = 0; j < subEntities_[i].nCorners(); ++j)
322  faces[i][j] = subEntities_[i].corners[j].idx;
323  }
324  }
325 
326 
335  bool faceIndices(const Element& e, int& first, int& count) const
336  {
337  typename ElementInfoMap::const_iterator it =
338  elmtInfo_.find(cellMapper_.map(e));
339  if (it == elmtInfo_.end())
340  {
341  first = -1;
342  count = 0;
343  return false;
344  }
345  // the iterator is valid, fill the out params
346  first = it->second->idx;
347  count = it->second->faces;
348  return true;
349  }
350 
351 
357  int indexInInside(unsigned int index) const
358  {
359  return index < subEntities_.size() ? subEntities_[index].num_in_parent : -1;
360  }
361 
362  // /**
363  // * @brief tests that a given entry in the extraction set does have local couplings
364  // * @todo parallel interface
365  // */
366  // bool contains (unsigned int global, unsigned int & local) const
367  // {
368  // local = global;
369  // return true;
370  // }
371 
375  const GridView & gridView() const
376  {
377  return gv_;
378  }
379 
380  const Grid& grid() const
381  {
382  return gv_.grid();
383  }
384 
391 #if DUNE_VERSION_NEWER(DUNE_GRID, 2, 4) || DOXYGEN
392  Element
393 #else
394  ElementPtr
395 #endif
396  element(unsigned int index) const
397  {
398  if (index >= subEntities_.size())
399  DUNE_THROW(Dune::GridError, "invalid face index");
400  const ElementSeed seed = (elmtInfo_.find(subEntities_[index].parent))->second->p;
401 #if DUNE_VERSION_NEWER(DUNE_GRID, 2, 4)
402  return grid().entity(seed);
403 #else
404  return grid().entityPointer(seed);
405 #endif
406  }
407 
408 #if 1
409 
415 #if DUNE_VERSION_NEWER(DUNE_GRID, 2, 4) || DOXYGEN
416  Vertex
417 #else
418  VertexPtr
419 #endif
420  vertex(unsigned int index) const
421  {
422  if (index >= coords_.size())
423  DUNE_THROW(Dune::GridError, "invalid coordinate index");
424  const VertexSeed seed = (vtxInfo_.find(coords_[index].vtxindex))->second->p;
425 #if DUNE_VERSION_NEWER(DUNE_GRID, 2, 4)
426  return grid().entity(seed);
427 #else
428  return grid().entityPointer(seed);
429 #endif
430  }
431 #endif
432 
434  Geometry geometry(unsigned int index) const;
435 
437  LocalGeometry geometryLocal(unsigned int index) const;
438 
439 };
440 
441 
442 template<typename GV, int cd>
444 {
445  clear();
446 }
447 
448 
450 template<typename GV, int cd>
451 typename Extractor<GV,cd>::Geometry Extractor<GV,cd>::geometry(unsigned int index) const
452 {
453  std::vector<Coords> corners(subEntities_[index].nCorners());
454  for (unsigned int i = 0; i < subEntities_[index].nCorners(); ++i)
455  corners[i] = coords_[subEntities_[index].corners[i].idx].coord;
456 
457  return Geometry(subEntities_[index].geometryType_, corners);
458 }
459 
460 
462 template<typename GV, int cd>
464 {
465  std::vector<LocalCoords> corners(subEntities_[index].nCorners());
466 
467  // get face info
468  const SubEntityInfo & face = subEntities_[index];
469  Dune::GeometryType facetype = subEntities_[index].geometryType_;
470 
471  // get reference element
472  const auto elmtseed = elmtInfo_.find(face.parent)->second->p;
473 #if DUNE_VERSION_NEWER(DUNE_GRID, 2, 4)
474  const auto elmt = grid().entity(elmtseed);
475 #else
476  const auto elmtPtr = grid().entityPointer(elmtseed);
477  const auto& elmt = *elmtPtr;
478 #endif
479  const Dune::GeometryType celltype = elmt.type();
480 #if DUNE_VERSION_NEWER(DUNE_GEOMETRY,2,3)
481  const Dune::ReferenceElement<ctype, dim> & re =
482  Dune::ReferenceElements<ctype, dim>::general(celltype);
483 #else
484  const Dune::GenericReferenceElement<ctype, dim> & re =
485  Dune::GenericReferenceElements<ctype, dim>::general(celltype);
486 #endif
487  for (unsigned int i = 0; i < subEntities_[index].nCorners(); ++i)
488  corners[i] = re.position(face.corners[i].num,dim);
489 
490  return LocalGeometry(facetype, corners);
491 }
492 
493 } // namespace GridGlue
494 
495 } // namespace Dune
496 
497 #endif // DUNE_GRIDGLUE_EXTRACTORS_EXTRACTOR_HH
Definition: extractor.hh:107
const GridView gv_
the grid object to extract the surface from
Definition: extractor.hh:206
VertexInfo(unsigned int idx_, const Vertex &p_)
Definition: extractor.hh:131
Coords coord
the coordinate
Definition: extractor.hh:120
ElementInfo(unsigned int idx_, const Element &p_, unsigned int f_)
Definition: extractor.hh:143
GV::Traits::template Codim< 0 >::Iterator ElementIter
Definition: extractor.hh:75
simple struct holding an element seed and an index
Definition: extractor.hh:141
Element::EntitySeed ElementSeed
Definition: extractor.hh:74
void clear()
delete everything build up so far and free the memory
Definition: extractor.hh:252
Dune::GenericGeometry::BasicGeometry< dim-codim, Dune::GenericGeometry::DefaultGeometryTraits< ctype, dim-codim, dimworld > > Geometry
Definition: extractor.hh:90
GV::Traits::template Codim< dim >::Entity Vertex
Definition: extractor.hh:69
std::vector< CoordinateInfo > coords_
all information about the corner vertices of the extracted
Definition: extractor.hh:211
std::vector< SubEntityInfo > subEntities_
all information about the extracted subEntities
Definition: extractor.hh:214
IndexType vtxindex
the index of the parent element (from index set)
Definition: extractor.hh:117
Dune::GeometryType geometryType_
The GeometryType of the subentity.
Definition: extractor.hh:188
unsigned int idx
the index of this element's first face in the internal list of extracted faces
Definition: extractor.hh:147
void getGeometryTypes(std::vector< Dune::GeometryType > &geometryTypes) const
Get the list of geometry types.
Definition: extractor.hh:305
unsigned int num_in_parent
the number of the face in the parent element
Definition: extractor.hh:185
simple struct holding a vertex pointer and an index
Definition: extractor.hh:129
Definition: extractor.hh:54
std::map< IndexType, ElementInfo * > ElementInfoMap
Definition: extractor.hh:200
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:396
unsigned int num
element corner
Definition: extractor.hh:104
unsigned int faces
the number of extracted faces for this element
Definition: extractor.hh:150
Dune::FieldVector< ctype, dim > LocalCoords
Definition: extractor.hh:66
const Grid & grid() const
Definition: extractor.hh:380
VertexSeed p
Definition: extractor.hh:134
SubEntityInfo()
Definition: extractor.hh:162
unsigned int nCorners() const
Definition: extractor.hh:172
Holds some information about an element's subentity involved in a coupling.
Definition: extractor.hh:160
CoordinateInfo(unsigned int index_, IndexType vtxindex_)
Definition: extractor.hh:112
ElementSeed p
the entity seed for the element
Definition: extractor.hh:153
Definition: extractor.hh:53
IndexType parent
the index of the parent element (from index set)
Definition: extractor.hh:182
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:335
GV::Traits::template Codim< 0 >::EntityPointer ElementPtr
Definition: extractor.hh:72
LocalGeometry geometryLocal(unsigned int index) const
Get geometry of the extracted face in element coordinates.
Definition: extractor.hh:463
unsigned int idx
index of the vertex
Definition: extractor.hh:103
Vertex::EntitySeed VertexSeed
Definition: extractor.hh:70
int IndexType
Definition: extractor.hh:81
Dune::GenericGeometry::BasicGeometry< dim-codim, Dune::GenericGeometry::DefaultGeometryTraits< ctype, dim-codim, dim > > LocalGeometry
Definition: extractor.hh:91
Dune::MultipleCodimMultipleGeomTypeMapper< GridView, MCMGElementLayout > CellMapper
Definition: extractor.hh:79
GV GridView
Definition: extractor.hh:61
VertexInfoMap vtxInfo_
a map enabling faster access to vertices and coordinates
Definition: extractor.hh:221
GV::Traits::template Codim< dim >::EntityPointer VertexPtr
Definition: extractor.hh:68
Helpful struct holding one index for the coordinate (vertex) to which it is associated and the elemen...
Definition: extractor.hh:101
Definition: gridglue.hh:33
CoordinateInfo()
Definition: extractor.hh:109
Definition: extractor.hh:52
const GridView & gridView() const
tests that a given entry in the extraction set does have local couplings
Definition: extractor.hh:375
GV::Traits::template Codim< 0 >::Entity Element
Definition: extractor.hh:73
void getCoords(std::vector< Dune::FieldVector< ctype, dimworld > > &coords) const
getter for the coordinates array
Definition: extractor.hh:287
Geometry geometry(unsigned int index) const
Get world geometry of the extracted face.
Definition: extractor.hh:451
unsigned int nCoords() const
getter for the count of coordinates
Definition: extractor.hh:299
GridView::Grid Grid
Definition: extractor.hh:62
CellMapper cellMapper_
Definition: extractor.hh:230
SubEntityInfo(IndexType parent_, unsigned int num_in_parent_, const Dune::GeometryType &geometryType)
Definition: extractor.hh:167
Vertex vertex(unsigned int index) const
gets the vertex for a given coordinate index throws an exception if index not valid ...
Definition: extractor.hh:420
int indexInInside(unsigned int index) const
gets the number face in the parent element
Definition: extractor.hh:357
unsigned int idx
Definition: extractor.hh:133
std::vector< unsigned int > VertexVector
Definition: extractor.hh:77
GV::Grid::ctype ctype
Definition: extractor.hh:64
Provides codimension-independent methods for grid extraction.
Definition: extractor.hh:47
void getFaces(std::vector< VertexVector > &faces) const
Get the corners of the extracted subentities.
Definition: extractor.hh:316
CornerInfo corners[cube_corners]
the corner indices plus the numbers of the vertices in the parent element
Definition: extractor.hh:196
Definition: extractor.hh:58
ElementInfoMap elmtInfo_
a map enabling faster access to elements and faces
Definition: extractor.hh:228
unsigned int index
the index of this coordinate (in internal storage scheme) // NEEDED??
Definition: extractor.hh:123
Extractor(const GV &gv)
Constructor.
Definition: extractor.hh:240
~Extractor()
Destructor frees allocated memory.
Definition: extractor.hh:443
Dune::FieldVector< ctype, dimworld > Coords
Definition: extractor.hh:65
std::map< IndexType, VertexInfo * > VertexInfoMap
Definition: extractor.hh:201