dune-grid-glue  2.5-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/grid/common/geometry.hh>
27 #include <dune/grid/common/grid.hh>
28 #include <dune/grid/common/mcmgmapper.hh>
29 #include <dune/geometry/multilineargeometry.hh>
30 
31 namespace Dune {
32 
33  namespace GridGlue {
34 
41 template<typename GV, int cd>
42 class Extractor
43 {
44 
45 public:
46 
47  enum {dimworld = GV::dimensionworld};
48  enum {dim = GV::dimension};
49  enum {codim = cd};
50 
51  enum
52  {
54  };
55 
56  typedef GV GridView;
57  typedef typename GridView::Grid Grid;
58 
59  typedef typename GV::Grid::ctype ctype;
60  typedef Dune::FieldVector<ctype, dimworld> Coords;
61  typedef Dune::FieldVector<ctype, dim> LocalCoords;
62 
63  typedef typename GV::Traits::template Codim<dim>::Entity Vertex;
64  typedef typename Vertex::EntitySeed VertexSeed;
65 
66  typedef typename GV::Traits::template Codim<0>::Entity Element;
67  typedef typename Element::EntitySeed ElementSeed;
68  typedef typename GV::Traits::template Codim<0>::Iterator ElementIter;
69 
70  typedef std::vector<unsigned int> VertexVector;
71 
72  typedef Dune::MultipleCodimMultipleGeomTypeMapper<GridView, MCMGElementLayout> CellMapper;
73  // typedef typename CellMapper::IndexType IndexType;
74  typedef int IndexType;
75 public:
76 
77  // transformations
78  typedef Dune::MultiLinearGeometry<ctype, dim-codim, dimworld> Geometry;
79  typedef Dune::MultiLinearGeometry<ctype, dim-codim, dim> LocalGeometry;
80 
81 protected:
82  /************************** PRIVATE SUBCLASSES **********************/
83 
88  struct CornerInfo
89  {
90  unsigned int idx : 28;
91  unsigned int num : 4;
92  };
93 
95  {
97  {}
98 
99  CoordinateInfo(unsigned int index_, IndexType vtxindex_)
100  : vtxindex(vtxindex_), index(index_)
101  {}
102 
104  IndexType vtxindex;
105 
107  Coords coord;
108 
110  unsigned int index;
111  };
112 
116  struct VertexInfo
117  {
118  VertexInfo(unsigned int idx_, const Vertex& p_) : idx(idx_), p(p_.seed())
119  {}
120  unsigned int idx;
121  VertexSeed p;
122  };
123 
124 
128  struct ElementInfo
129  {
130  ElementInfo(unsigned int idx_, const Element& p_, unsigned int f_) : idx(idx_), faces(f_), p(p_.seed())
131  {}
132 
134  unsigned int idx : 28;
135 
137  unsigned int faces : 4;
138 
140  ElementSeed p;
141  };
142 
143 
148  {
150  {
151  geometryType_.makeSimplex(dim-codim);
152  }
153 
154  SubEntityInfo(IndexType parent_, unsigned int num_in_parent_,
155  const Dune::GeometryType& geometryType)
156  : parent(parent_), num_in_parent(num_in_parent_), geometryType_(geometryType)
157  {}
158 
159  unsigned int nCorners() const
160  {
161  return Dune::ReferenceElements<ctype, dim-codim>::general(geometryType_).size(dim-codim);
162  }
163 
165  IndexType parent;
166 
168  unsigned int num_in_parent : 3;
169 
171  Dune::GeometryType geometryType_;
172 
179  CornerInfo corners[cube_corners]; // sim = numer of vertices in a simplex
180  };
181 
182 
183  typedef std::map<IndexType, ElementInfo* > ElementInfoMap;
184  typedef std::map<IndexType, VertexInfo* > VertexInfoMap;
185 
186  /************************** MEMBER VARIABLES ************************/
187 
189  const GridView gv_;
190 
191  /* Geometrical and Topological Information */
192 
194  std::vector<CoordinateInfo> coords_;
195 
197  std::vector<SubEntityInfo> subEntities_;
198 
204  VertexInfoMap vtxInfo_;
205 
211  ElementInfoMap elmtInfo_;
212 
213  CellMapper cellMapper_;
214 
215 public:
216 
217  /* 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 */
218 
223  Extractor(const GV& gv)
224  : gv_(gv), cellMapper_(gv)
225  {}
226 
228  ~Extractor();
229 
230  /* F U N C T I O N A L I T Y */
231 
235  void clear()
236  {
237  // this is an inofficial way on how to free the memory allocated
238  // by a std::vector
239  {
240  std::vector<CoordinateInfo> dummy;
241  coords_.swap(dummy);
242  }
243  {
244  std::vector<SubEntityInfo> dummy;
245  subEntities_.swap(dummy);
246  }
247 
248  // first free all manually allocated vertex/element info items...
249  for (typename VertexInfoMap::iterator it = vtxInfo_.begin();
250  it != vtxInfo_.end(); ++it)
251  if (it->second != NULL)
252  delete it->second;
253  for (typename ElementInfoMap::iterator it = elmtInfo_.begin();
254  it != elmtInfo_.end(); ++it)
255  if (it->second != NULL)
256  delete it->second;
257  // ...then clear the maps themselves, too
258  vtxInfo_.clear();
259  elmtInfo_.clear();
260  }
261 
262 
263  /* G E T T E R S */
264 
270  void getCoords(std::vector<Dune::FieldVector<ctype, dimworld> >& coords) const
271  {
272  coords.resize(coords_.size());
273  for (unsigned int i = 0; i < coords_.size(); ++i)
274  coords[i] = coords_[i].coord;
275  }
276 
277 
282  unsigned int nCoords() const
283  {
284  return coords_.size();
285  }
286 
288  void getGeometryTypes(std::vector<Dune::GeometryType>& geometryTypes) const
289  {
290  geometryTypes.resize(subEntities_.size());
291  for (size_t i=0; i<subEntities_.size(); i++)
292  geometryTypes[i] = subEntities_[i].geometryType_;
293  }
294 
295 
299  void getFaces(std::vector<VertexVector>& faces) const
300  {
301  faces.resize(subEntities_.size());
302  for (unsigned int i = 0; i < subEntities_.size(); ++i) {
303  faces[i].resize(subEntities_[i].nCorners());
304  for (unsigned int j = 0; j < subEntities_[i].nCorners(); ++j)
305  faces[i][j] = subEntities_[i].corners[j].idx;
306  }
307  }
308 
309 
318  bool faceIndices(const Element& e, int& first, int& count) const
319  {
320  typename ElementInfoMap::const_iterator it =
321  elmtInfo_.find(cellMapper_.map(e));
322  if (it == elmtInfo_.end())
323  {
324  first = -1;
325  count = 0;
326  return false;
327  }
328  // the iterator is valid, fill the out params
329  first = it->second->idx;
330  count = it->second->faces;
331  return true;
332  }
333 
334 
340  int indexInInside(unsigned int index) const
341  {
342  return index < subEntities_.size() ? subEntities_[index].num_in_parent : -1;
343  }
344 
345  // /**
346  // * @brief tests that a given entry in the extraction set does have local couplings
347  // * @todo parallel interface
348  // */
349  // bool contains (unsigned int global, unsigned int & local) const
350  // {
351  // local = global;
352  // return true;
353  // }
354 
358  const GridView & gridView() const
359  {
360  return gv_;
361  }
362 
363  const Grid& grid() const
364  {
365  return gv_.grid();
366  }
367 
374  Element
375  element(unsigned int index) const
376  {
377  if (index >= subEntities_.size())
378  DUNE_THROW(Dune::GridError, "invalid face index");
379  const ElementSeed seed = (elmtInfo_.find(subEntities_[index].parent))->second->p;
380  return grid().entity(seed);
381  }
382 
383 #if 1
384 
390  Vertex
391  vertex(unsigned int index) const
392  {
393  if (index >= coords_.size())
394  DUNE_THROW(Dune::GridError, "invalid coordinate index");
395  const VertexSeed seed = (vtxInfo_.find(coords_[index].vtxindex))->second->p;
396  return grid().entity(seed);
397  }
398 #endif
399 
401  Geometry geometry(unsigned int index) const;
402 
404  LocalGeometry geometryLocal(unsigned int index) const;
405 
406 };
407 
408 
409 template<typename GV, int cd>
411 {
412  clear();
413 }
414 
415 
417 template<typename GV, int cd>
418 typename Extractor<GV,cd>::Geometry Extractor<GV,cd>::geometry(unsigned int index) const
419 {
420  std::vector<Coords> corners(subEntities_[index].nCorners());
421  for (unsigned int i = 0; i < subEntities_[index].nCorners(); ++i)
422  corners[i] = coords_[subEntities_[index].corners[i].idx].coord;
423 
424  return Geometry(subEntities_[index].geometryType_, corners);
425 }
426 
427 
429 template<typename GV, int cd>
431 {
432  std::vector<LocalCoords> corners(subEntities_[index].nCorners());
433 
434  // get face info
435  const SubEntityInfo & face = subEntities_[index];
436  Dune::GeometryType facetype = subEntities_[index].geometryType_;
437 
438  // get reference element
439  const auto elmtseed = elmtInfo_.find(face.parent)->second->p;
440  const auto elmt = grid().entity(elmtseed);
441  const Dune::GeometryType celltype = elmt.type();
442  const Dune::ReferenceElement<ctype, dim> & re =
443  Dune::ReferenceElements<ctype, dim>::general(celltype);
444  for (unsigned int i = 0; i < subEntities_[index].nCorners(); ++i)
445  corners[i] = re.position(face.corners[i].num,dim);
446 
447  return LocalGeometry(facetype, corners);
448 }
449 
450 } // namespace GridGlue
451 
452 } // namespace Dune
453 
454 #endif // DUNE_GRIDGLUE_EXTRACTORS_EXTRACTOR_HH
std::map< IndexType, VertexInfo *> VertexInfoMap
Definition: extractor.hh:184
void clear()
delete everything build up so far and free the memory
Definition: extractor.hh:235
simple struct holding an element seed and an index
Definition: extractor.hh:128
Helpful struct holding one index for the coordinate (vertex) to which it is associated and the elemen...
Definition: extractor.hh:88
unsigned int num
element corner
Definition: extractor.hh:91
Vertex::EntitySeed VertexSeed
Definition: extractor.hh:64
Definition: extractor.hh:53
int indexInInside(unsigned int index) const
gets the number face in the parent element
Definition: extractor.hh:340
void getFaces(std::vector< VertexVector > &faces) const
Get the corners of the extracted subentities.
Definition: extractor.hh:299
GV::Traits::template Codim< dim >::Entity Vertex
Definition: extractor.hh:63
std::vector< CoordinateInfo > coords_
all information about the corner vertices of the extracted
Definition: extractor.hh:194
std::vector< SubEntityInfo > subEntities_
all information about the extracted subEntities
Definition: extractor.hh:197
VertexInfoMap vtxInfo_
a map enabling faster access to vertices and coordinates
Definition: extractor.hh:204
Coords coord
the coordinate
Definition: extractor.hh:107
Element::EntitySeed ElementSeed
Definition: extractor.hh:67
VertexInfo(unsigned int idx_, const Vertex &p_)
Definition: extractor.hh:118
ElementInfo(unsigned int idx_, const Element &p_, unsigned int f_)
Definition: extractor.hh:130
std::map< IndexType, ElementInfo *> ElementInfoMap
Definition: extractor.hh:183
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:375
unsigned int nCorners() const
Definition: extractor.hh:159
simple struct holding a vertex pointer and an index
Definition: extractor.hh:116
GV::Grid::ctype ctype
Definition: extractor.hh:59
Provides codimension-independent methods for grid extraction.
Definition: extractor.hh:42
void getGeometryTypes(std::vector< Dune::GeometryType > &geometryTypes) const
Get the list of geometry types.
Definition: extractor.hh:288
unsigned int idx
Definition: extractor.hh:120
const GridView & gridView() const
tests that a given entry in the extraction set does have local couplings
Definition: extractor.hh:358
ElementInfoMap elmtInfo_
a map enabling faster access to elements and faces
Definition: extractor.hh:211
Dune::FieldVector< ctype, dim > LocalCoords
Definition: extractor.hh:61
const Grid & grid() const
Definition: extractor.hh:363
Holds some information about an element&#39;s subentity involved in a coupling.
Definition: extractor.hh:147
Dune::MultiLinearGeometry< ctype, dim-codim, dim > LocalGeometry
Definition: extractor.hh:79
LocalGeometry geometryLocal(unsigned int index) const
Get geometry of the extracted face in element coordinates.
Definition: extractor.hh:430
CellMapper cellMapper_
Definition: extractor.hh:213
Geometry geometry(unsigned int index) const
Get world geometry of the extracted face.
Definition: extractor.hh:418
IndexType parent
the index of the parent element (from index set)
Definition: extractor.hh:165
void getCoords(std::vector< Dune::FieldVector< ctype, dimworld > > &coords) const
getter for the coordinates array
Definition: extractor.hh:270
Dune::MultiLinearGeometry< ctype, dim-codim, dimworld > Geometry
Definition: extractor.hh:78
Definition: extractor.hh:47
SubEntityInfo(IndexType parent_, unsigned int num_in_parent_, const Dune::GeometryType &geometryType)
Definition: extractor.hh:154
unsigned int nCoords() const
getter for the count of coordinates
Definition: extractor.hh:282
GV::Traits::template Codim< 0 >::Iterator ElementIter
Definition: extractor.hh:68
const GridView gv_
the grid object to extract the surface from
Definition: extractor.hh:189
int IndexType
Definition: extractor.hh:74
SubEntityInfo()
Definition: extractor.hh:149
ElementSeed p
the entity seed for the element
Definition: extractor.hh:140
GV::Traits::template Codim< 0 >::Entity Element
Definition: extractor.hh:66
Definition: extractor.hh:49
VertexSeed p
Definition: extractor.hh:121
Dune::GeometryType geometryType_
The GeometryType of the subentity.
Definition: extractor.hh:171
Dune::MultipleCodimMultipleGeomTypeMapper< GridView, MCMGElementLayout > CellMapper
Definition: extractor.hh:72
GV GridView
Definition: extractor.hh:56
unsigned int index
the index of this coordinate (in internal storage scheme) // NEEDED??
Definition: extractor.hh:110
Vertex vertex(unsigned int index) const
gets the vertex for a given coordinate index throws an exception if index not valid ...
Definition: extractor.hh:391
std::vector< unsigned int > VertexVector
Definition: extractor.hh:70
GridView::Grid Grid
Definition: extractor.hh:57
Definition: extractor.hh:48
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:318
unsigned int idx
index of the vertex
Definition: extractor.hh:90
CoordinateInfo(unsigned int index_, IndexType vtxindex_)
Definition: extractor.hh:99
IndexType vtxindex
the index of the parent element (from index set)
Definition: extractor.hh:104
Extractor(const GV &gv)
Constructor.
Definition: extractor.hh:223
~Extractor()
Destructor frees allocated memory.
Definition: extractor.hh:410
CoordinateInfo()
Definition: extractor.hh:96
Definition: gridglue.hh:30
Dune::FieldVector< ctype, dimworld > Coords
Definition: extractor.hh:60