dune-grid-glue  2.3.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/array.hh>
27 #include <dune/common/version.hh>
28 #include <dune/grid/common/geometry.hh>
29 #include <dune/grid/common/grid.hh>
30 #include <dune/grid/common/mcmgmapper.hh>
31 #if DUNE_VERSION_NEWER(DUNE_GEOMETRY,2,3)
32 #include <dune/geometry/multilineargeometry.hh>
33 #else
34 #include <dune/geometry/genericgeometry/geometry.hh>
35 #endif
36 
37 namespace Dune {
38 
39  namespace GridGlue {
40 
47 template<typename GV, int cd>
48 class Extractor
49 {
50 
51 public:
52 
53  enum {dimworld = GV::dimensionworld};
54  enum {dim = GV::dimension};
55  enum {codim = cd};
56 
57  enum
58  {
60  };
61 
62  typedef GV GridView;
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 
71  typedef typename GV::Traits::template Codim<0>::EntityPointer ElementPtr;
72  typedef typename GV::Traits::template Codim<0>::Entity Element;
73  typedef typename GV::Traits::template Codim<0>::Iterator ElementIter;
74 
75  typedef std::vector<unsigned int> VertexVector;
76 
77  typedef Dune::MultipleCodimMultipleGeomTypeMapper<GridView, MCMGElementLayout> CellMapper;
78  // typedef typename CellMapper::IndexType IndexType;
79  typedef int IndexType;
80 public:
81 
82  // transformations
83 #if DUNE_VERSION_NEWER(DUNE_GEOMETRY,2,3)
84  // We don't need the caching, but the uncached MultiLinearGeometry is not defined at all vertices
85  typedef Dune::CachedMultiLinearGeometry<ctype, dim-codim, dimworld> Geometry;
86  typedef Dune::CachedMultiLinearGeometry<ctype, dim-codim, dim> LocalGeometry;
87 #else
88  typedef Dune::GenericGeometry::BasicGeometry<dim-codim, Dune::GenericGeometry::DefaultGeometryTraits<ctype,dim-codim,dimworld> > Geometry;
89  typedef Dune::GenericGeometry::BasicGeometry<dim-codim, Dune::GenericGeometry::DefaultGeometryTraits<ctype,dim-codim,dim> > LocalGeometry;
90 #endif
91 
92 protected:
93  /************************** PRIVATE SUBCLASSES **********************/
94 
99  struct CornerInfo
100  {
101  unsigned int idx : 28;
102  unsigned int num : 4;
103  };
104 
106  {
108  {}
109 
110  CoordinateInfo(unsigned int index_, IndexType vtxindex_)
111  : vtxindex(vtxindex_), index(index_)
112  {}
113 
115  IndexType vtxindex;
116 
118  Coords coord;
119 
121  unsigned int index;
122  };
123 
127  struct VertexInfo
128  {
129  VertexInfo(unsigned int idx_, VertexPtr p_) : idx(idx_), p(p_)
130  {}
131  unsigned int idx;
132  VertexPtr p;
133  };
134 
135 
139  struct ElementInfo
140  {
141  ElementInfo(unsigned int idx_, ElementPtr p_, unsigned int f_) : idx(idx_), faces(f_), p(p_)
142  {}
143 
145  unsigned int idx : 28;
146 
148  unsigned int faces : 4;
149 
151  ElementPtr p;
152  };
153 
154 
159  {
161  {
162  geometryType_.makeSimplex(dim-codim);
163  }
164 
165  SubEntityInfo(IndexType parent_, unsigned int num_in_parent_,
166  const Dune::GeometryType& geometryType)
167  : parent(parent_), num_in_parent(num_in_parent_), geometryType_(geometryType)
168  {}
169 
170  unsigned int nCorners() const
171  {
172 #if DUNE_VERSION_NEWER(DUNE_GEOMETRY,2,3)
173  return Dune::ReferenceElements<ctype, dim-codim>::general(geometryType_).size(dim-codim);
174 #else
175  return Dune::GenericReferenceElements<ctype, dim-codim>::general(geometryType_).size(dim-codim);
176 #endif
177  }
178 
180  IndexType parent;
181 
183  unsigned int num_in_parent : 3;
184 
186  Dune::GeometryType geometryType_;
187 
194  CornerInfo corners[cube_corners]; // sim = numer of vertices in a simplex
195  };
196 
197 
198  typedef std::map<IndexType, ElementInfo* > ElementInfoMap;
199  typedef std::map<IndexType, VertexInfo* > VertexInfoMap;
200 
201  /************************** MEMBER VARIABLES ************************/
202 
204  const GridView& gv_;
205 
206  /* Geometrical and Topological Information */
207 
209  std::vector<CoordinateInfo> coords_;
210 
212  std::vector<SubEntityInfo> subEntities_;
213 
219  VertexInfoMap vtxInfo_;
220 
226  ElementInfoMap elmtInfo_;
227 
228  CellMapper cellMapper_;
229 
230 public:
231 
232  /* 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 */
233 
238  Extractor(const GV& gv)
239  : gv_(gv), cellMapper_(gv)
240  {}
241 
243  ~Extractor();
244 
245  /* F U N C T I O N A L I T Y */
246 
250  void clear()
251  {
252  // this is an inofficial way on how to free the memory allocated
253  // by a std::vector
254  {
255  std::vector<CoordinateInfo> dummy;
256  coords_.swap(dummy);
257  }
258  {
259  std::vector<SubEntityInfo> dummy;
260  subEntities_.swap(dummy);
261  }
262 
263  // first free all manually allocated vertex/element info items...
264  for (typename VertexInfoMap::iterator it = vtxInfo_.begin();
265  it != vtxInfo_.end(); ++it)
266  if (it->second != NULL)
267  delete it->second;
268  for (typename ElementInfoMap::iterator it = elmtInfo_.begin();
269  it != elmtInfo_.end(); ++it)
270  if (it->second != NULL)
271  delete it->second;
272  // ...then clear the maps themselves, too
273  vtxInfo_.clear();
274  elmtInfo_.clear();
275  }
276 
277 
278  /* G E T T E R S */
279 
285  void getCoords(std::vector<Dune::FieldVector<ctype, dimworld> >& coords) const
286  {
287  coords.resize(coords_.size());
288  for (unsigned int i = 0; i < coords_.size(); ++i)
289  coords[i] = coords_[i].coord;
290  }
291 
292 
297  unsigned int nCoords() const
298  {
299  return coords_.size();
300  }
301 
303  void getGeometryTypes(std::vector<Dune::GeometryType>& geometryTypes) const
304  {
305  geometryTypes.resize(subEntities_.size());
306  for (size_t i=0; i<subEntities_.size(); i++)
307  geometryTypes[i] = subEntities_[i].geometryType_;
308  }
309 
310 
314  void getFaces(std::vector<VertexVector>& faces) const
315  {
316  faces.resize(subEntities_.size());
317  for (unsigned int i = 0; i < subEntities_.size(); ++i) {
318  faces[i].resize(subEntities_[i].nCorners());
319  for (unsigned int j = 0; j < subEntities_[i].nCorners(); ++j)
320  faces[i][j] = subEntities_[i].corners[j].idx;
321  }
322  }
323 
324 
333  bool faceIndices(const Element& e, int& first, int& count) const
334  {
335  typename ElementInfoMap::const_iterator it =
336  elmtInfo_.find(cellMapper_.map(e));
337  if (it == elmtInfo_.end())
338  {
339  first = -1;
340  count = 0;
341  return false;
342  }
343  // the iterator is valid, fill the out params
344  first = it->second->idx;
345  count = it->second->faces;
346  return true;
347  }
348 
349 
355  int indexInInside(unsigned int index) const
356  {
357  return index < subEntities_.size() ? subEntities_[index].num_in_parent : -1;
358  }
359 
360  // /**
361  // * @brief tests that a given entry in the extraction set does have local couplings
362  // * @todo parallel interface
363  // */
364  // bool contains (unsigned int global, unsigned int & local) const
365  // {
366  // local = global;
367  // return true;
368  // }
369 
373  const GridView & gridView() const
374  {
375  return gv_;
376  }
377 
384  const ElementPtr& element(unsigned int index) const
385  {
386  if (index >= subEntities_.size())
387  DUNE_THROW(Dune::GridError, "invalid face index");
388  return (elmtInfo_.find(subEntities_[index].parent))->second->p;
389  }
390 
391 #if 1
392 
398  const VertexPtr& vertex(unsigned int index) const
399  {
400  if (index >= coords_.size())
401  DUNE_THROW(Dune::GridError, "invalid coordinate index");
402  return (vtxInfo_.find(coords_[index].vtxindex))->second->p;
403  }
404 #endif
405 
407  Geometry geometry(unsigned int index) const;
408 
410  LocalGeometry geometryLocal(unsigned int index) const;
411 
412 };
413 
414 
415 template<typename GV, int cd>
417 {
418  clear();
419 }
420 
421 
423 template<typename GV, int cd>
424 typename Extractor<GV,cd>::Geometry Extractor<GV,cd>::geometry(unsigned int index) const
425 {
426  std::vector<Coords> corners(subEntities_[index].nCorners());
427  for (unsigned int i = 0; i < subEntities_[index].nCorners(); ++i)
428  corners[i] = coords_[subEntities_[index].corners[i].idx].coord;
429 
430  return Geometry(subEntities_[index].geometryType_, corners);
431 }
432 
433 
435 template<typename GV, int cd>
437 {
438  std::vector<LocalCoords> corners(subEntities_[index].nCorners());
439 
440  // get face info
441  const SubEntityInfo & face = subEntities_[index];
442  Dune::GeometryType facetype = subEntities_[index].geometryType_;
443 
444  // get reference element
445  Dune::GeometryType celltype = elmtInfo_.find(face.parent)->second->p->type();
446 #if DUNE_VERSION_NEWER(DUNE_GEOMETRY,2,3)
447  const Dune::ReferenceElement<ctype, dim> & re =
448  Dune::ReferenceElements<ctype, dim>::general(celltype);
449 #else
450  const Dune::GenericReferenceElement<ctype, dim> & re =
451  Dune::GenericReferenceElements<ctype, dim>::general(celltype);
452 #endif
453  for (unsigned int i = 0; i < subEntities_[index].nCorners(); ++i)
454  corners[i] = re.position(face.corners[i].num,dim);
455 
456  return LocalGeometry(facetype, corners);
457 }
458 
459 } // namespace GridGlue
460 
461 } // namespace Dune
462 
463 #endif // DUNE_GRIDGLUE_EXTRACTORS_EXTRACTOR_HH
Definition: extractor.hh:105
Coords coord
the coordinate
Definition: extractor.hh:118
unsigned int idx
Definition: extractor.hh:131
GV::Traits::template Codim< 0 >::Iterator ElementIter
Definition: extractor.hh:73
simple struct holding an entity pointer and an index
Definition: extractor.hh:139
void clear()
delete everything build up so far and free the memory
Definition: extractor.hh:250
Dune::GenericGeometry::BasicGeometry< dim-codim, Dune::GenericGeometry::DefaultGeometryTraits< ctype, dim-codim, dimworld > > Geometry
Definition: extractor.hh:88
ElementPtr p
the entity pointer to the element
Definition: extractor.hh:151
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:209
std::vector< SubEntityInfo > subEntities_
all information about the extracted subEntities
Definition: extractor.hh:212
IndexType vtxindex
the index of the parent element (from index set)
Definition: extractor.hh:115
unsigned int nCorners() const
Definition: extractor.hh:170
SubEntityInfo(IndexType parent_, unsigned int num_in_parent_, const Dune::GeometryType &geometryType)
Definition: extractor.hh:165
unsigned int idx
the index of this element's first face in the internal list of extracted faces
Definition: extractor.hh:145
void getGeometryTypes(std::vector< Dune::GeometryType > &geometryTypes) const
Get the list of geometry types.
Definition: extractor.hh:303
Dune::GeometryType geometryType_
The GeometryType of the subentity.
Definition: extractor.hh:186
Definition: extractor.hh:55
Holds some information about an element's subentity involved in a coupling.
Definition: extractor.hh:158
std::map< IndexType, ElementInfo * > ElementInfoMap
Definition: extractor.hh:198
Definition: gridglue.hh:34
unsigned int num
element corner
Definition: extractor.hh:102
unsigned int faces
the number of extracted faces for this element
Definition: extractor.hh:148
Dune::FieldVector< ctype, dim > LocalCoords
Definition: extractor.hh:66
SubEntityInfo()
Definition: extractor.hh:160
simple struct holding a vertex pointer and an index
Definition: extractor.hh:127
CoordinateInfo(unsigned int index_, IndexType vtxindex_)
Definition: extractor.hh:110
Definition: extractor.hh:54
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:333
ElementInfo(unsigned int idx_, ElementPtr p_, unsigned int f_)
Definition: extractor.hh:141
VertexInfo(unsigned int idx_, VertexPtr p_)
Definition: extractor.hh:129
GV::Traits::template Codim< 0 >::EntityPointer ElementPtr
Definition: extractor.hh:71
LocalGeometry geometryLocal(unsigned int index) const
Get geometry of the extracted face in element coordinates.
Definition: extractor.hh:436
unsigned int idx
index of the vertex
Definition: extractor.hh:101
int IndexType
Definition: extractor.hh:79
Dune::GenericGeometry::BasicGeometry< dim-codim, Dune::GenericGeometry::DefaultGeometryTraits< ctype, dim-codim, dim > > LocalGeometry
Definition: extractor.hh:89
Dune::MultipleCodimMultipleGeomTypeMapper< GridView, MCMGElementLayout > CellMapper
Definition: extractor.hh:77
VertexPtr p
Definition: extractor.hh:132
GV GridView
Definition: extractor.hh:62
CornerInfo corners[cube_corners]
the corner indices plus the numbers of the vertices in the parent element
Definition: extractor.hh:194
IndexType parent
the index of the parent element (from index set)
Definition: extractor.hh:180
VertexInfoMap vtxInfo_
a map enabling faster access to vertices and coordinates
Definition: extractor.hh:219
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:99
const VertexPtr & vertex(unsigned int index) const
gets the vertex for a given coordinate index throws an exception if index not valid ...
Definition: extractor.hh:398
CoordinateInfo()
Definition: extractor.hh:107
Definition: extractor.hh:53
const GridView & gridView() const
tests that a given entry in the extraction set does have local couplings
Definition: extractor.hh:373
GV::Traits::template Codim< 0 >::Entity Element
Definition: extractor.hh:72
void getCoords(std::vector< Dune::FieldVector< ctype, dimworld > > &coords) const
getter for the coordinates array
Definition: extractor.hh:285
Geometry geometry(unsigned int index) const
Get world geometry of the extracted face.
Definition: extractor.hh:424
unsigned int nCoords() const
getter for the count of coordinates
Definition: extractor.hh:297
CellMapper cellMapper_
Definition: extractor.hh:228
int indexInInside(unsigned int index) const
gets the number face in the parent element
Definition: extractor.hh:355
std::vector< unsigned int > VertexVector
Definition: extractor.hh:75
GV::Grid::ctype ctype
Definition: extractor.hh:64
Provides codimension-independent methods for grid extraction.
Definition: extractor.hh:48
void getFaces(std::vector< VertexVector > &faces) const
Get the corners of the extracted subentities.
Definition: extractor.hh:314
Definition: extractor.hh:59
const GridView & gv_
the grid object to extract the surface from
Definition: extractor.hh:204
ElementInfoMap elmtInfo_
a map enabling faster access to elements and faces
Definition: extractor.hh:226
unsigned int index
the index of this coordinate (in internal storage scheme) // NEEDED??
Definition: extractor.hh:121
unsigned int num_in_parent
the number of the face in the parent element
Definition: extractor.hh:183
Extractor(const GV &gv)
Constructor.
Definition: extractor.hh:238
~Extractor()
Destructor frees allocated memory.
Definition: extractor.hh:416
Dune::FieldVector< ctype, dimworld > Coords
Definition: extractor.hh:65
const ElementPtr & element(unsigned int index) const
gets the parent element for a given face index, throws an exception if index not valid ...
Definition: extractor.hh:384
std::map< IndexType, VertexInfo * > VertexInfoMap
Definition: extractor.hh:199