dune-grid-glue  2.5.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  enum {dimworld = GV::dimensionworld};
49  enum {dim = GV::dimension};
50  enum {codim = cd};
51 
52  enum
53  {
55  };
56 
57  typedef GV GridView;
58  typedef typename GridView::Grid Grid;
59 
60  typedef typename GV::Grid::ctype ctype;
61  typedef Dune::FieldVector<ctype, dimworld> Coords;
62  typedef Dune::FieldVector<ctype, dim> LocalCoords;
63 
64  typedef typename GV::Traits::template Codim<dim>::Entity Vertex;
65  typedef typename Vertex::EntitySeed VertexSeed;
66 
67  typedef typename GV::Traits::template Codim<0>::Entity Element;
68  typedef typename Element::EntitySeed ElementSeed;
69  typedef typename GV::Traits::template Codim<0>::Iterator ElementIter;
70 
71  typedef std::vector<unsigned int> VertexVector;
72 
73 #if DUNE_VERSION_NEWER(DUNE_GRID, 2, 6)
74  using CellMapper = MultipleCodimMultipleGeomTypeMapper<GridView>;
75 #else
76  using CellMapper = MultipleCodimMultipleGeomTypeMapper<GridView, MCMGElementLayout>;
77 #endif
78  // typedef typename CellMapper::IndexType IndexType;
79  typedef int IndexType;
80 public:
81 
82  // transformations
83  typedef Dune::MultiLinearGeometry<ctype, dim-codim, dimworld> Geometry;
84  typedef Dune::MultiLinearGeometry<ctype, dim-codim, dim> LocalGeometry;
85 
86 protected:
87  /************************** PRIVATE SUBCLASSES **********************/
88 
93  struct CornerInfo
94  {
95  unsigned int idx : 28;
96  unsigned int num : 4;
97  };
98 
100  {
102  {}
103 
104  CoordinateInfo(unsigned int index_, IndexType vtxindex_)
105  : vtxindex(vtxindex_), index(index_)
106  {}
107 
109  IndexType vtxindex;
110 
112  Coords coord;
113 
115  unsigned int index;
116  };
117 
121  struct VertexInfo
122  {
123  VertexInfo(unsigned int idx_, const Vertex& p_) : idx(idx_), p(p_.seed())
124  {}
125  unsigned int idx;
126  VertexSeed p;
127  };
128 
129 
133  struct ElementInfo
134  {
135  ElementInfo(unsigned int idx_, const Element& p_, unsigned int f_) : idx(idx_), faces(f_), p(p_.seed())
136  {}
137 
139  unsigned int idx : 28;
140 
142  unsigned int faces : 4;
143 
145  ElementSeed p;
146  };
147 
148 
153  {
155  {
156  geometryType_.makeSimplex(dim-codim);
157  }
158 
159  SubEntityInfo(IndexType parent_, unsigned int num_in_parent_,
160  const Dune::GeometryType& geometryType)
161  : parent(parent_), num_in_parent(num_in_parent_), geometryType_(geometryType)
162  {}
163 
164  unsigned int nCorners() const
165  {
166  return Dune::ReferenceElements<ctype, dim-codim>::general(geometryType_).size(dim-codim);
167  }
168 
170  IndexType parent;
171 
173  unsigned int num_in_parent : 3;
174 
176  Dune::GeometryType geometryType_;
177 
184  CornerInfo corners[cube_corners]; // sim = numer of vertices in a simplex
185  };
186 
187 
188  typedef std::map<IndexType, ElementInfo* > ElementInfoMap;
189  typedef std::map<IndexType, VertexInfo* > VertexInfoMap;
190 
191  /************************** MEMBER VARIABLES ************************/
192 
194  const GridView gv_;
195 
196  /* Geometrical and Topological Information */
197 
199  std::vector<CoordinateInfo> coords_;
200 
202  std::vector<SubEntityInfo> subEntities_;
203 
209  VertexInfoMap vtxInfo_;
210 
216  ElementInfoMap elmtInfo_;
217 
219 
220 public:
221 
222  /* 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 */
223 
228  Extractor(const GV& gv)
229  : gv_(gv)
230 #if DUNE_VERSION_NEWER(DUNE_GRID, 2, 6)
231  , cellMapper_(gv, mcmgElementLayout())
232 #else
233  , cellMapper_(gv)
234 #endif
235  {}
236 
238  ~Extractor();
239 
240  /* F U N C T I O N A L I T Y */
241 
245  void clear()
246  {
247  // this is an inofficial way on how to free the memory allocated
248  // by a std::vector
249  {
250  std::vector<CoordinateInfo> dummy;
251  coords_.swap(dummy);
252  }
253  {
254  std::vector<SubEntityInfo> dummy;
255  subEntities_.swap(dummy);
256  }
257 
258  // first free all manually allocated vertex/element info items...
259  for (typename VertexInfoMap::iterator it = vtxInfo_.begin();
260  it != vtxInfo_.end(); ++it)
261  if (it->second != NULL)
262  delete it->second;
263  for (typename ElementInfoMap::iterator it = elmtInfo_.begin();
264  it != elmtInfo_.end(); ++it)
265  if (it->second != NULL)
266  delete it->second;
267  // ...then clear the maps themselves, too
268  vtxInfo_.clear();
269  elmtInfo_.clear();
270  }
271 
272 
273  /* G E T T E R S */
274 
280  void getCoords(std::vector<Dune::FieldVector<ctype, dimworld> >& coords) const
281  {
282  coords.resize(coords_.size());
283  for (unsigned int i = 0; i < coords_.size(); ++i)
284  coords[i] = coords_[i].coord;
285  }
286 
287 
292  unsigned int nCoords() const
293  {
294  return coords_.size();
295  }
296 
298  void getGeometryTypes(std::vector<Dune::GeometryType>& geometryTypes) const
299  {
300  geometryTypes.resize(subEntities_.size());
301  for (size_t i=0; i<subEntities_.size(); i++)
302  geometryTypes[i] = subEntities_[i].geometryType_;
303  }
304 
305 
309  void getFaces(std::vector<VertexVector>& faces) const
310  {
311  faces.resize(subEntities_.size());
312  for (unsigned int i = 0; i < subEntities_.size(); ++i) {
313  faces[i].resize(subEntities_[i].nCorners());
314  for (unsigned int j = 0; j < subEntities_[i].nCorners(); ++j)
315  faces[i][j] = subEntities_[i].corners[j].idx;
316  }
317  }
318 
319 
328  bool faceIndices(const Element& e, int& first, int& count) const
329  {
330  typename ElementInfoMap::const_iterator it =
331  elmtInfo_.find(cellMapper_.map(e));
332  if (it == elmtInfo_.end())
333  {
334  first = -1;
335  count = 0;
336  return false;
337  }
338  // the iterator is valid, fill the out params
339  first = it->second->idx;
340  count = it->second->faces;
341  return true;
342  }
343 
344 
350  int indexInInside(unsigned int index) const
351  {
352  return index < subEntities_.size() ? subEntities_[index].num_in_parent : -1;
353  }
354 
355  // /**
356  // * @brief tests that a given entry in the extraction set does have local couplings
357  // * @todo parallel interface
358  // */
359  // bool contains (unsigned int global, unsigned int & local) const
360  // {
361  // local = global;
362  // return true;
363  // }
364 
368  const GridView & gridView() const
369  {
370  return gv_;
371  }
372 
373  const Grid& grid() const
374  {
375  return gv_.grid();
376  }
377 
384  Element
385  element(unsigned int index) const
386  {
387  if (index >= subEntities_.size())
388  DUNE_THROW(Dune::GridError, "invalid face index");
389  const ElementSeed seed = (elmtInfo_.find(subEntities_[index].parent))->second->p;
390  return grid().entity(seed);
391  }
392 
393 #if 1
394 
400  Vertex
401  vertex(unsigned int index) const
402  {
403  if (index >= coords_.size())
404  DUNE_THROW(Dune::GridError, "invalid coordinate index");
405  const VertexSeed seed = (vtxInfo_.find(coords_[index].vtxindex))->second->p;
406  return grid().entity(seed);
407  }
408 #endif
409 
411  Geometry geometry(unsigned int index) const;
412 
414  LocalGeometry geometryLocal(unsigned int index) const;
415 
416 };
417 
418 
419 template<typename GV, int cd>
421 {
422  clear();
423 }
424 
425 
427 template<typename GV, int cd>
428 typename Extractor<GV,cd>::Geometry Extractor<GV,cd>::geometry(unsigned int index) const
429 {
430  std::vector<Coords> corners(subEntities_[index].nCorners());
431  for (unsigned int i = 0; i < subEntities_[index].nCorners(); ++i)
432  corners[i] = coords_[subEntities_[index].corners[i].idx].coord;
433 
434  return Geometry(subEntities_[index].geometryType_, corners);
435 }
436 
437 
439 template<typename GV, int cd>
441 {
442  std::vector<LocalCoords> corners(subEntities_[index].nCorners());
443 
444  // get face info
445  const SubEntityInfo & face = subEntities_[index];
446  Dune::GeometryType facetype = subEntities_[index].geometryType_;
447 
448  // get reference element
449  const auto elmtseed = elmtInfo_.find(face.parent)->second->p;
450  const auto elmt = grid().entity(elmtseed);
451  const Dune::GeometryType celltype = elmt.type();
452  const Dune::ReferenceElement<ctype, dim> & re =
453  Dune::ReferenceElements<ctype, dim>::general(celltype);
454  for (unsigned int i = 0; i < subEntities_[index].nCorners(); ++i)
455  corners[i] = re.position(face.corners[i].num,dim);
456 
457  return LocalGeometry(facetype, corners);
458 }
459 
460 } // namespace GridGlue
461 
462 } // namespace Dune
463 
464 #endif // DUNE_GRIDGLUE_EXTRACTORS_EXTRACTOR_HH
simple struct holding an element seed and an index
Definition: extractor.hh:133
Dune::MultiLinearGeometry< ctype, dim-codim, dim > LocalGeometry
Definition: extractor.hh:84
GV::Grid::ctype ctype
Definition: extractor.hh:60
~Extractor()
Destructor frees allocated memory.
Definition: extractor.hh:420
std::vector< CoordinateInfo > coords_
all information about the corner vertices of the extracted
Definition: extractor.hh:199
void getCoords(std::vector< Dune::FieldVector< ctype, dimworld > > &coords) const
getter for the coordinates array
Definition: extractor.hh:280
Provides codimension-independent methods for grid extraction.
Definition: extractor.hh:43
unsigned int idx
the index of this element&#39;s first face in the internal list of extracted faces
Definition: extractor.hh:139
GridView::Grid Grid
Definition: extractor.hh:58
std::map< IndexType, VertexInfo *> VertexInfoMap
Definition: extractor.hh:189
Definition: extractor.hh:54
Definition: extractor.hh:48
simple struct holding a vertex pointer and an index
Definition: extractor.hh:121
LocalGeometry geometryLocal(unsigned int index) const
Get geometry of the extracted face in element coordinates.
Definition: extractor.hh:440
Dune::MultiLinearGeometry< ctype, dim-codim, dimworld > Geometry
Definition: extractor.hh:83
Definition: gridglue.hh:31
Element::EntitySeed ElementSeed
Definition: extractor.hh:68
CoordinateInfo(unsigned int index_, IndexType vtxindex_)
Definition: extractor.hh:104
const GridView & gridView() const
tests that a given entry in the extraction set does have local couplings
Definition: extractor.hh:368
unsigned int nCorners() const
Definition: extractor.hh:164
int IndexType
Definition: extractor.hh:79
const GridView gv_
the grid object to extract the surface from
Definition: extractor.hh:194
Extractor(const GV &gv)
Constructor.
Definition: extractor.hh:228
Dune::FieldVector< ctype, dim > LocalCoords
Definition: extractor.hh:62
MultipleCodimMultipleGeomTypeMapper< GridView, MCMGElementLayout > CellMapper
Definition: extractor.hh:76
SubEntityInfo()
Definition: extractor.hh:154
Dune::GeometryType geometryType_
The GeometryType of the subentity.
Definition: extractor.hh:176
CoordinateInfo()
Definition: extractor.hh:101
GV::Traits::template Codim< 0 >::Iterator ElementIter
Definition: extractor.hh:69
const Grid & grid() const
Definition: extractor.hh:373
VertexInfoMap vtxInfo_
a map enabling faster access to vertices and coordinates
Definition: extractor.hh:209
Vertex vertex(unsigned int index) const
gets the vertex for a given coordinate index throws an exception if index not valid ...
Definition: extractor.hh:401
unsigned int index
the index of this coordinate (in internal storage scheme) // NEEDED??
Definition: extractor.hh:115
GV::Traits::template Codim< dim >::Entity Vertex
Definition: extractor.hh:64
GV::Traits::template Codim< 0 >::Entity Element
Definition: extractor.hh:67
Helpful struct holding one index for the coordinate (vertex) to which it is associated and the elemen...
Definition: extractor.hh:93
IndexType vtxindex
the index of the parent element (from index set)
Definition: extractor.hh:109
unsigned int idx
Definition: extractor.hh:125
Geometry geometry(unsigned int index) const
Get world geometry of the extracted face.
Definition: extractor.hh:428
unsigned int num_in_parent
the number of the face in the parent element
Definition: extractor.hh:173
int indexInInside(unsigned int index) const
gets the number face in the parent element
Definition: extractor.hh:350
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:385
Definition: extractor.hh:50
unsigned int num
element corner
Definition: extractor.hh:96
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:328
GV GridView
Definition: extractor.hh:57
std::vector< SubEntityInfo > subEntities_
all information about the extracted subEntities
Definition: extractor.hh:202
Holds some information about an element&#39;s subentity involved in a coupling.
Definition: extractor.hh:152
CellMapper cellMapper_
Definition: extractor.hh:218
unsigned int faces
the number of extracted faces for this element
Definition: extractor.hh:142
Definition: extractor.hh:49
VertexSeed p
Definition: extractor.hh:126
unsigned int idx
index of the vertex
Definition: extractor.hh:95
unsigned int nCoords() const
getter for the count of coordinates
Definition: extractor.hh:292
Coords coord
the coordinate
Definition: extractor.hh:112
SubEntityInfo(IndexType parent_, unsigned int num_in_parent_, const Dune::GeometryType &geometryType)
Definition: extractor.hh:159
void getFaces(std::vector< VertexVector > &faces) const
Get the corners of the extracted subentities.
Definition: extractor.hh:309
ElementInfoMap elmtInfo_
a map enabling faster access to elements and faces
Definition: extractor.hh:216
Dune::FieldVector< ctype, dimworld > Coords
Definition: extractor.hh:61
ElementSeed p
the entity seed for the element
Definition: extractor.hh:145
IndexType parent
the index of the parent element (from index set)
Definition: extractor.hh:170
VertexInfo(unsigned int idx_, const Vertex &p_)
Definition: extractor.hh:123
void getGeometryTypes(std::vector< Dune::GeometryType > &geometryTypes) const
Get the list of geometry types.
Definition: extractor.hh:298
void clear()
delete everything build up so far and free the memory
Definition: extractor.hh:245
std::map< IndexType, ElementInfo *> ElementInfoMap
Definition: extractor.hh:188
Vertex::EntitySeed VertexSeed
Definition: extractor.hh:65
ElementInfo(unsigned int idx_, const Element &p_, unsigned int f_)
Definition: extractor.hh:135
std::vector< unsigned int > VertexVector
Definition: extractor.hh:71