dune-grid-glue  2.3.0
codim0extractor.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: codim0extractor.hh
5  * Version: 1.0
6  * Created on: Jun 23, 2009
7  * Author: Oliver Sander, Christian Engwer
8  * ---------------------------------
9  * Project: dune-grid-glue
10  * Description: base class for grid extractors extracting surface grids
11  *
12  */
18 #ifndef DUNE_GRIDGLUE_EXTRACTORS_CODIM0EXTRACTOR_HH
19 #define DUNE_GRIDGLUE_EXTRACTORS_CODIM0EXTRACTOR_HH
20 
21 #include <deque>
22 
23 #include <dune/grid/common/mcmgmapper.hh>
24 
25 #include "extractor.hh"
26 #include "extractorpredicate.hh"
27 
28 namespace Dune {
29 
30  namespace GridGlue {
31 
32 template<typename GV>
33 class Codim0Extractor : public Extractor<GV,0>
34 {
35 
36 public:
37 
38  /* E X P O R T E D T Y P E S A N D C O N S T A N T S */
40  typedef typename Extractor<GV,0>::ctype ctype;
44 
45  typedef typename GV::Traits::template Codim<dim>::EntityPointer VertexPtr;
46  typedef typename GV::Traits::template Codim<0>::EntityPointer ElementPtr;
47 
48  static const Dune::PartitionIteratorType PType = Dune::Interior_Partition;
49  typedef typename GV::Traits::template Codim<0>::template Partition<PType>::Iterator ElementIter;
50 
51  // import typedefs from base class
57 
63  Codim0Extractor(const GV& gv, const ExtractorPredicate<GV,0>& descr)
64  : Extractor<GV,0>(gv), positiveNormalDirection_(false)
65  {
66  std::cout << "This is Codim0Extractor on a <"
67  << GV::dimension << "," << GV::dimensionworld << "> grid!" << std::endl;
68  update(descr);
69  }
70 
72  const bool & positiveNormalDirection() const { return positiveNormalDirection_; }
73 
74 protected:
76 private:
77  void update(const ExtractorPredicate<GV,0>& descr);
78 };
79 
80 
81 template<typename GV>
83 {
84  // In this first pass iterate over all entities of codim 0.
85  // Get its corner vertices, find resp. store them together with their associated index,
86  // and remember the indices of the corners.
87 
88  // free everything there is in this object
89  this->clear();
90 
91  // several counter for consecutive indexing are needed
92  size_t element_index = 0;
93  size_t vertex_index = 0;
94 
95  // a temporary container where newly acquired face
96  // information can be stored at first
97  std::deque<SubEntityInfo> temp_faces;
98 
99  // iterate over all codim 0 elements on the grid
100  for (ElementIter elit = this->gv_.template begin<0, PType>();
101  elit != this->gv_.template end<0, PType>(); ++elit)
102  {
103  ElementPtr eptr(elit);
104  IndexType eindex = this->cellMapper_.map(*elit);
105 
106  // only do sth. if this element is "interesting"
107  // implicit cast is done automatically
108  if (descr.contains(eptr,0))
109  {
110  // add an entry to the element info map, the index will be set properly later
111  this->elmtInfo_[eindex] = new ElementInfo(element_index, eptr, 1);
112 
113  int numCorners = elit->template count<dim>();
114  unsigned int vertex_indices[numCorners]; // index in global vector
115  unsigned int vertex_numbers[numCorners]; // index in parent entity
116 
117  // try for each of the faces vertices whether it is already inserted or not
118  for (int i = 0; i < numCorners; ++i)
119  {
120  vertex_numbers[i] = i;
121 
122  // get the vertex pointer and the index from the index set
123  VertexPtr vptr(elit->template subEntity<dim>(vertex_numbers[i]));
124  IndexType vindex = this->gv_.indexSet().template index<dim>(*vptr);
125 
126  // if the vertex is not yet inserted in the vertex info map
127  // it is a new one -> it will be inserted now!
128  typename VertexInfoMap::iterator vimit = this->vtxInfo_.find(vindex);
129  if (vimit == this->vtxInfo_.end())
130  {
131  // insert into the map
132  this->vtxInfo_[vindex] = new VertexInfo(vertex_index, vptr);
133  // remember this vertex' index
134  vertex_indices[i] = vertex_index;
135  // increase the current index
136  vertex_index++;
137  }
138  else
139  {
140  // only remember the vertex' index
141  vertex_indices[i] = vimit->second->idx;
142  }
143  }
144 
145  // flip cell if necessary
146  {
147  switch (int(dim))
148  {
149  case 0 :
150  break;
151  case 1 :
152  {
153  // The following test only works if the zero-th coordinate is the
154  // one that defines the orientation. A sufficient condition for
155  // this is dimworld == 1
156  /* assert(dimworld==1); */
157  bool elementNormalDirection =
158  (elit->geometry().corner(1)[0] < elit->geometry().corner(0)[0]);
159  if ( positiveNormalDirection_ != elementNormalDirection )
160  {
161  std::swap(vertex_indices[0], vertex_indices[1]);
162  std::swap(vertex_numbers[0], vertex_numbers[1]);
163  }
164  break;
165  }
166  case 2 :
167  {
168  Dune::FieldVector<ctype, dimworld>
169  v0 = elit->geometry().corner(1),
170  v1 = elit->geometry().corner(2);
171  v0 -= elit->geometry().corner(0);
172  v1 -= elit->geometry().corner(0);
173  ctype normal_sign = v0[0]*v1[1] - v0[1]*v1[0];
174  bool elementNormalDirection = (normal_sign < 0);
175  if ( positiveNormalDirection_ != elementNormalDirection )
176  {
177  std::cout << "swap\n";
178  if (elit->type().isCube())
179  {
180  for (int i = 0; i < (1<<dim); i+=2)
181  {
182  // swap i and i+1
183  std::swap(vertex_indices[i], vertex_indices[i+1]);
184  std::swap(vertex_numbers[i], vertex_numbers[i+1]);
185  }
186  } else if (elit->type().isSimplex()) {
187  std::swap(vertex_indices[0], vertex_indices[1]);
188  std::swap(vertex_numbers[0], vertex_numbers[1]);
189  } else {
190  DUNE_THROW(Dune::Exception, "Unexpected Geometrytype");
191  }
192  }
193  break;
194  }
195  }
196  }
197 
198  // add a new face to the temporary collection
199  temp_faces.push_back(SubEntityInfo(eindex,0,elit->type()));
200  element_index++;
201  for (int i=0; i<numCorners; i++) {
202  temp_faces.back().corners[i].idx = vertex_indices[i];
203  // remember the vertices' numbers in parent element's vertices
204  temp_faces.back().corners[i].num = vertex_numbers[i];
205  }
206 
207  }
208  } // end loop over elements
209 
210  // allocate the array for the face specific information...
211  this->subEntities_.resize(element_index);
212  // ...and fill in the data from the temporary containers
213  copy(temp_faces.begin(), temp_faces.end(), this->subEntities_.begin());
214 
215  // now first write the array with the coordinates...
216  this->coords_.resize(this->vtxInfo_.size());
217  typename VertexInfoMap::const_iterator it1 = this->vtxInfo_.begin();
218  for (; it1 != this->vtxInfo_.end(); ++it1)
219  {
220  // get a pointer to the associated info object
221  CoordinateInfo* current = &this->coords_[it1->second->idx];
222  // store this coordinates index // NEEDED?
223  current->index = it1->second->idx;
224  // store the vertex' index for the index2vertex mapping
225  current->vtxindex = it1->first;
226  // store the vertex' coordinates under the associated index
227  // in the coordinates array
228  current->coord = it1->second->p->geometry().corner(0);
229  }
230 
231 }
232 
233 } // namespace GridGlue
234 
235 } // namespace Dune
236 
237 #endif // DUNE_GRIDGLUE_EXTRACTORS_CODIM0EXTRACTOR_HH
GV::Traits::template Codim< dim >::EntityPointer VertexPtr
Definition: codim0extractor.hh:45
static const Dune::PartitionIteratorType PType
Definition: codim0extractor.hh:48
Codim0Extractor(const GV &gv, const ExtractorPredicate< GV, 0 > &descr)
Constructor.
Definition: codim0extractor.hh:63
Extractor< GV, 0 >::VertexInfo VertexInfo
Definition: codim0extractor.hh:54
Definition: codim0extractor.hh:33
const bool & positiveNormalDirection() const
Definition: codim0extractor.hh:72
bool & positiveNormalDirection()
Definition: codim0extractor.hh:71
Definition: gridglue.hh:34
Extractor< GV, 0 >::CoordinateInfo CoordinateInfo
Definition: codim0extractor.hh:55
Extractor< GV, 0 >::ctype ctype
Definition: codim0extractor.hh:40
Extractor< GV, 0 >::SubEntityInfo SubEntityInfo
Definition: codim0extractor.hh:52
Extractor< GV, 0 >::IndexType IndexType
Definition: codim0extractor.hh:43
Base class for subentity-selecting predicates.
Definition: extractorpredicate.hh:30
Extractor< GV, 0 >::ElementInfo ElementInfo
Definition: codim0extractor.hh:53
GV::Traits::template Codim< 0 >::EntityPointer ElementPtr
Definition: codim0extractor.hh:46
GV::Traits::template Codim< 0 >::template Partition< PType >::Iterator ElementIter
Definition: codim0extractor.hh:49
extractor base class
Extractor< GV, 0 >::VertexInfoMap VertexInfoMap
Definition: codim0extractor.hh:56
Provides codimension-independent methods for grid extraction.
Definition: extractor.hh:48
virtual bool contains(const typename GV::Traits::template Codim< 0 >::EntityPointer &element, unsigned int subentity) const =0
Return true if a subentity should be extracted.
Base class for predicates selecting the part of a grid to be extracted.
bool positiveNormalDirection_
Definition: codim0extractor.hh:75