dune-grid-glue  2.9
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 // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-GPL-2.0-only-with-dune-grid-glue-exception
5 /*
6  * Filename: extractor.hh
7  * Version: 1.0
8  * Created on: Oct 05, 2009
9  * Author: Christian Engwer
10  * ---------------------------------
11  * Project: dune-grid-glue
12  * Description: base class for all grid extractors
13  *
14  */
20 #ifndef DUNE_GRIDGLUE_EXTRACTORS_EXTRACTOR_HH
21 #define DUNE_GRIDGLUE_EXTRACTORS_EXTRACTOR_HH
22 
23 #include <vector>
24 #include <map>
25 #include <algorithm>
26 #include <dune/common/exceptions.hh>
27 #include <dune/common/fvector.hh>
28 #include <dune/common/version.hh>
29 #include <dune/grid/common/geometry.hh>
30 #include <dune/grid/common/grid.hh>
31 #include <dune/grid/common/mcmgmapper.hh>
32 #include <dune/geometry/multilineargeometry.hh>
33 
34 namespace Dune {
35 
36  namespace GridGlue {
37 
44 template<typename GV, int cd>
45 class Extractor
46 {
47 
48 public:
49 
50  static constexpr auto dimworld = GV::dimensionworld;
51  static constexpr auto dim = GV::dimension;
52  static constexpr auto codim = cd;
53 
54  static constexpr int cube_corners = 1 << (dim-codim);
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 
69  typedef std::vector<unsigned int> VertexVector;
70 
71 #if DUNE_VERSION_NEWER(DUNE_GRID, 2, 6)
72  using CellMapper = MultipleCodimMultipleGeomTypeMapper<GridView>;
73 #else
74  using CellMapper = MultipleCodimMultipleGeomTypeMapper<GridView, MCMGElementLayout>;
75 #endif
76  // typedef typename CellMapper::IndexType IndexType;
77  typedef int IndexType;
78 public:
79 
80  // transformations
81  typedef Dune::MultiLinearGeometry<ctype, dim-codim, dimworld> Geometry;
82  typedef Dune::MultiLinearGeometry<ctype, dim-codim, dim> LocalGeometry;
83 
84 protected:
85  /************************** PRIVATE SUBCLASSES **********************/
86 
91  struct CornerInfo
92  {
93  unsigned int idx : 28;
94  unsigned int num : 4;
95  };
96 
98  {
100  {}
101 
102  CoordinateInfo(unsigned int index_, IndexType vtxindex_)
103  : vtxindex(vtxindex_), index(index_)
104  {}
105 
108 
111 
113  unsigned int index;
114  };
115 
119  struct VertexInfo
120  {
121  VertexInfo(unsigned int idx_, const Vertex& p_) : idx(idx_), p(p_.seed())
122  {}
123  unsigned int idx;
125  };
126 
127 
131  struct ElementInfo
132  {
133  ElementInfo(unsigned int idx_, const Element& p_, unsigned int f_) : idx(idx_), faces(f_), p(p_.seed())
134  {}
135 
137  unsigned int idx : 28;
138 
140  unsigned int faces : 4;
141 
144  };
145 
146 
151  {
153  /*
154  * TODO: move default value of `geometryType_` to member declaration
155  * when removing support for older dune-geometry
156  */
157 #if DUNE_VERSION_NEWER(DUNE_GEOMETRY, 2, 6)
158  : geometryType_(GeometryTypes::simplex(dim-codim))
159  {}
160 #else
161  {
162  geometryType_.makeSimplex(dim-codim);
163  }
164 #endif
165 
166  SubEntityInfo(IndexType parent_, unsigned int num_in_parent_,
167  const Dune::GeometryType& geometryType)
168  : parent(parent_), num_in_parent(num_in_parent_), geometryType_(geometryType)
169  {}
170 
171  unsigned int nCorners() const
172  {
173  return Dune::ReferenceElements<ctype, dim-codim>::general(geometryType_).size(dim-codim);
174  }
175 
178 
180  unsigned int num_in_parent : 3;
181 
183  Dune::GeometryType geometryType_;
184 
191  CornerInfo corners[cube_corners]; // sim = numer of vertices in a simplex
192  };
193 
194 
195  typedef std::map<IndexType, ElementInfo> ElementInfoMap;
196  typedef std::map<IndexType, VertexInfo> VertexInfoMap;
197 
198  /************************** MEMBER VARIABLES ************************/
199 
201  const GridView gv_;
202 
203  /* Geometrical and Topological Information */
204 
206  std::vector<CoordinateInfo> coords_;
207 
209  std::vector<SubEntityInfo> subEntities_;
210 
217 
224 
226 
227 public:
228 
229  /* 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 */
230 
235  Extractor(const GV& gv)
236  : gv_(gv)
237 #if DUNE_VERSION_NEWER(DUNE_GRID, 2, 6)
238  , cellMapper_(gv, mcmgElementLayout())
239 #else
240  , cellMapper_(gv)
241 #endif
242  {}
243 
244  /* F U N C T I O N A L I T Y */
245 
249  void clear()
250  {
251  // this is an inofficial way on how to free the memory allocated
252  // by a std::vector
253  {
254  std::vector<CoordinateInfo> dummy;
255  coords_.swap(dummy);
256  }
257  {
258  std::vector<SubEntityInfo> dummy;
259  subEntities_.swap(dummy);
260  }
261 
262  // ...then clear the maps themselves, too
263  vtxInfo_.clear();
264  elmtInfo_.clear();
265  }
266 
267 
268  /* G E T T E R S */
269 
275  void getCoords(std::vector<Dune::FieldVector<ctype, dimworld> >& coords) const
276  {
277  coords.resize(coords_.size());
278  for (unsigned int i = 0; i < coords_.size(); ++i)
279  coords[i] = coords_[i].coord;
280  }
281 
282 
287  unsigned int nCoords() const
288  {
289  return coords_.size();
290  }
291 
293  void getGeometryTypes(std::vector<Dune::GeometryType>& geometryTypes) const
294  {
295  geometryTypes.resize(subEntities_.size());
296  for (size_t i=0; i<subEntities_.size(); i++)
297  geometryTypes[i] = subEntities_[i].geometryType_;
298  }
299 
300 
304  void getFaces(std::vector<VertexVector>& faces) const
305  {
306  faces.resize(subEntities_.size());
307  for (unsigned int i = 0; i < subEntities_.size(); ++i) {
308  faces[i].resize(subEntities_[i].nCorners());
309  for (unsigned int j = 0; j < subEntities_[i].nCorners(); ++j)
310  faces[i][j] = subEntities_[i].corners[j].idx;
311  }
312  }
313 
314 
323  bool faceIndices(const Element& e, int& first, int& count) const
324  {
325  typename ElementInfoMap::const_iterator it =
326  elmtInfo_.find(cellMapper_.map(e));
327  if (it == elmtInfo_.end())
328  {
329  first = -1;
330  count = 0;
331  return false;
332  }
333  // the iterator is valid, fill the out params
334  first = it->second.idx;
335  count = it->second.faces;
336  return true;
337  }
338 
339 
345  int indexInInside(unsigned int index) const
346  {
347  return index < subEntities_.size() ? subEntities_[index].num_in_parent : -1;
348  }
349 
350  // /**
351  // * @brief tests that a given entry in the extraction set does have local couplings
352  // * @todo parallel interface
353  // */
354  // bool contains (unsigned int global, unsigned int & local) const
355  // {
356  // local = global;
357  // return true;
358  // }
359 
363  const GridView & gridView() const
364  {
365  return gv_;
366  }
367 
368  const Grid& grid() const
369  {
370  return gv_.grid();
371  }
372 
379  Element
380  element(unsigned int index) const
381  {
382  if (index >= subEntities_.size())
383  DUNE_THROW(Dune::GridError, "invalid face index");
384  const ElementSeed seed = elmtInfo_.at(subEntities_[index].parent).p;
385  return grid().entity(seed);
386  }
387 
388 #if 1
395  Vertex
396  vertex(unsigned int index) const
397  {
398  if (index >= coords_.size())
399  DUNE_THROW(Dune::GridError, "invalid coordinate index");
400  const VertexSeed seed = vtxInfo_.at(coords_[index].vtxindex).p;
401  return grid().entity(seed);
402  }
403 #endif
404 
406  Geometry geometry(unsigned int index) const;
407 
409  LocalGeometry geometryLocal(unsigned int index) const;
410 
411 };
412 
413 
415 template<typename GV, int cd>
416 typename Extractor<GV,cd>::Geometry Extractor<GV,cd>::geometry(unsigned int index) const
417 {
418  std::vector<Coords> corners(subEntities_[index].nCorners());
419  for (unsigned int i = 0; i < subEntities_[index].nCorners(); ++i)
420  corners[i] = coords_[subEntities_[index].corners[i].idx].coord;
421 
422  return Geometry(subEntities_[index].geometryType_, corners);
423 }
424 
425 
427 template<typename GV, int cd>
429 {
430  std::vector<LocalCoords> corners(subEntities_[index].nCorners());
431 
432  // get face info
433  const SubEntityInfo & face = subEntities_[index];
434  Dune::GeometryType facetype = subEntities_[index].geometryType_;
435 
436  // get reference element
437  const auto elmtseed = elmtInfo_.at(face.parent).p;
438  const auto elmt = grid().entity(elmtseed);
439  const Dune::GeometryType celltype = elmt.type();
440  const auto& re = Dune::ReferenceElements<ctype, dim>::general(celltype);
441  for (unsigned int i = 0; i < subEntities_[index].nCorners(); ++i)
442  corners[i] = re.position(face.corners[i].num,dim);
443 
444  return LocalGeometry(facetype, corners);
445 }
446 
447 } // namespace GridGlue
448 
449 } // namespace Dune
450 
451 #endif // DUNE_GRIDGLUE_EXTRACTORS_EXTRACTOR_HH
Definition: gridglue.hh:37
Provides codimension-independent methods for grid extraction.
Definition: extractor.hh:46
GV::Traits::template Codim< dim >::Entity Vertex
Definition: extractor.hh:63
VertexInfoMap vtxInfo_
a map enabling faster access to vertices and coordinates
Definition: extractor.hh:216
MultipleCodimMultipleGeomTypeMapper< GridView, MCMGElementLayout > CellMapper
Definition: extractor.hh:74
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:380
int indexInInside(unsigned int index) const
gets the number face in the parent element
Definition: extractor.hh:345
Dune::FieldVector< ctype, dim > LocalCoords
Definition: extractor.hh:61
LocalGeometry geometryLocal(unsigned int index) const
Get geometry of the extracted face in element coordinates.
Definition: extractor.hh:428
static constexpr auto dimworld
Definition: extractor.hh:50
const GridView & gridView() const
give access to the Dune::GridView where this Patch belongs to
Definition: extractor.hh:363
Dune::MultiLinearGeometry< ctype, dim-codim, dimworld > Geometry
Definition: extractor.hh:81
int IndexType
Definition: extractor.hh:77
std::vector< CoordinateInfo > coords_
all information about the corner vertices of the extracted
Definition: extractor.hh:206
GV::Traits::template Codim< 0 >::Entity Element
Definition: extractor.hh:66
void getFaces(std::vector< VertexVector > &faces) const
Get the corners of the extracted subentities.
Definition: extractor.hh:304
std::vector< unsigned int > VertexVector
Definition: extractor.hh:69
const GridView gv_
the grid object to extract the surface from
Definition: extractor.hh:201
std::vector< SubEntityInfo > subEntities_
all information about the extracted subEntities
Definition: extractor.hh:209
static constexpr int cube_corners
Definition: extractor.hh:54
Vertex vertex(unsigned int index) const
gets the vertex for a given coordinate index throws an exception if index not valid
Definition: extractor.hh:396
static constexpr auto codim
Definition: extractor.hh:52
Dune::FieldVector< ctype, dimworld > Coords
Definition: extractor.hh:60
void getGeometryTypes(std::vector< Dune::GeometryType > &geometryTypes) const
Get the list of geometry types.
Definition: extractor.hh:293
std::map< IndexType, VertexInfo > VertexInfoMap
Definition: extractor.hh:196
GridView::Grid Grid
Definition: extractor.hh:57
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:323
Geometry geometry(unsigned int index) const
Get world geometry of the extracted face.
Definition: extractor.hh:416
GV GridView
Definition: extractor.hh:56
unsigned int nCoords() const
getter for the count of coordinates
Definition: extractor.hh:287
GV::Grid::ctype ctype
Definition: extractor.hh:59
std::map< IndexType, ElementInfo > ElementInfoMap
Definition: extractor.hh:195
Extractor(const GV &gv)
Constructor.
Definition: extractor.hh:235
void getCoords(std::vector< Dune::FieldVector< ctype, dimworld > > &coords) const
getter for the coordinates array
Definition: extractor.hh:275
CellMapper cellMapper_
Definition: extractor.hh:225
Element::EntitySeed ElementSeed
Definition: extractor.hh:67
const Grid & grid() const
Definition: extractor.hh:368
Vertex::EntitySeed VertexSeed
Definition: extractor.hh:64
ElementInfoMap elmtInfo_
a map enabling faster access to elements and faces
Definition: extractor.hh:223
void clear()
delete everything build up so far and free the memory
Definition: extractor.hh:249
static constexpr auto dim
Definition: extractor.hh:51
Dune::MultiLinearGeometry< ctype, dim-codim, dim > LocalGeometry
Definition: extractor.hh:82
Helpful struct holding one index for the coordinate (vertex) to which it is associated and the elemen...
Definition: extractor.hh:92
unsigned int idx
index of the vertex
Definition: extractor.hh:93
unsigned int num
element corner
Definition: extractor.hh:94
CoordinateInfo(unsigned int index_, IndexType vtxindex_)
Definition: extractor.hh:102
unsigned int index
the index of this coordinate (in internal storage scheme) // NEEDED??
Definition: extractor.hh:113
CoordinateInfo()
Definition: extractor.hh:99
Coords coord
the coordinate
Definition: extractor.hh:110
IndexType vtxindex
the index of the parent element (from index set)
Definition: extractor.hh:107
simple struct holding a vertex pointer and an index
Definition: extractor.hh:120
unsigned int idx
Definition: extractor.hh:123
VertexInfo(unsigned int idx_, const Vertex &p_)
Definition: extractor.hh:121
VertexSeed p
Definition: extractor.hh:124
simple struct holding an element seed and an index
Definition: extractor.hh:132
unsigned int idx
the index of this element's first face in the internal list of extracted faces
Definition: extractor.hh:137
unsigned int faces
the number of extracted faces for this element
Definition: extractor.hh:140
ElementInfo(unsigned int idx_, const Element &p_, unsigned int f_)
Definition: extractor.hh:133
ElementSeed p
the entity seed for the element
Definition: extractor.hh:143
Holds some information about an element's subentity involved in a coupling.
Definition: extractor.hh:151
unsigned int nCorners() const
Definition: extractor.hh:171
CornerInfo corners[cube_corners]
the corner indices plus the numbers of the vertices in the parent element
Definition: extractor.hh:191
IndexType parent
the index of the parent element (from index set)
Definition: extractor.hh:177
SubEntityInfo(IndexType parent_, unsigned int num_in_parent_, const Dune::GeometryType &geometryType)
Definition: extractor.hh:166
unsigned int num_in_parent
the number of the face in the parent element
Definition: extractor.hh:180
Dune::GeometryType geometryType_
The GeometryType of the subentity.
Definition: extractor.hh:183
SubEntityInfo()
Definition: extractor.hh:152