dune-foamgrid  2.8-git
foamgridelements.hh
Go to the documentation of this file.
1 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set ts=8 sw=4 et sts=4:
3 #ifndef DUNE_FOAMGRID_ELEMENTS_HH
4 #define DUNE_FOAMGRID_ELEMENTS_HH
5 
6 #include <memory>
7 #include <functional>
8 
9 #include <dune/common/fmatrix.hh>
10 
13 
14 namespace Dune {
15 
17  template <int dimgrid, int dimworld, class ctype>
18  class FoamGridEntityImp<dimgrid, dimgrid, dimworld, ctype> {};
19 
21  template <int dimworld, class ctype>
22  class FoamGridEntityImp<1, 1, dimworld, ctype>
23  : public FoamGridEntityBase
24  {
26  enum {dimgrid = 1};
27 
28  public:
29 
31  enum MarkState { DO_NOTHING , COARSEN , REFINE, IS_COARSENED };
32 
35  int level, unsigned int id)
36  : FoamGridEntityBase(level,id),
37  vertex_{{v0, v1}},
38  facet_{{v0, v1}},
39  sons_{{nullptr, nullptr}},
40  nSons_(0), father_(nullptr),
41  refinementIndex_(-1), markState_(DO_NOTHING), isNew_(false),
42  coarseningBlocked_(false),
43  growthInsertionIndex_(-1)
44  {}
45 
48  int level, unsigned int id,
49  FoamGridEntityImp* father)
50 
51  : FoamGridEntityBase(level,id),
52  vertex_{{v0, v1}},
53  facet_{{v0, v1}},
54  sons_{{nullptr, nullptr}},
55  nSons_(0), father_(father),
56  refinementIndex_(-1), markState_(DO_NOTHING), isNew_(false),
57  coarseningBlocked_(false),
58  growthInsertionIndex_(-1)
59  {}
60 
61  FoamGridEntityImp(int level, unsigned int id)
62  : FoamGridEntityBase(level, id),
63  vertex_{{nullptr, nullptr}},
64  facet_{{nullptr, nullptr}},
65  sons_{{nullptr, nullptr}},
66  nSons_(0),
67  father_(nullptr),
68  refinementIndex_(-1),
69  markState_(DO_NOTHING), isNew_(false),
70  coarseningBlocked_(false),
71  growthInsertionIndex_(-1)
72  {}
73 
75  bool isLeaf() const {
76  return sons_[0]==nullptr &&
77  sons_[1]==nullptr;
78  }
79 
81  unsigned int nSons() const {
82  return nSons_;
83  }
84 
85  bool mightVanish() const
86  {
87  return markState_==COARSEN;
88  }
89 
90  bool isNew() const
91  {
92  return isNew_;
93  }
94 
95  GeometryType type() const {
96  return Dune::GeometryTypes::simplex(1);
97  }
98 
99  bool hasFather() const
100  {
101  return father_!=nullptr;
102  }
103 
105  int corners() const {
106  return 2;
107  }
108 
109  FieldVector<ctype, dimworld> corner(int i) const {
110  assert(i < this->corners());
111  assert(int(vertex_.size())==this->corners());
112  assert(vertex_[i]!=nullptr);
113  return vertex_[i]->pos_;
114  }
115 
116  PartitionType partitionType() const {
117  return InteriorEntity;
118  }
119 
125  FieldVector<ctype, 1> globalToLocal(const FieldVector<ctype, dimworld>& coord) const
126  {
127  const auto diff = vertex_[1]->pos_ - vertex_[0]->pos_;
128  const ctype eps = diff.two_norm()*std::numeric_limits<ctype>::epsilon();
129 
130  using std::abs;
131  for (std::size_t dimIdx = 0; dimIdx < dimworld; ++dimIdx)
132  if (abs(diff[dimIdx]) > eps)
133  return (coord[dimIdx] - vertex_[0]->pos_[dimIdx]) / diff[dimIdx];
134 
135  DUNE_THROW(Dune::GridError, "Global to local mapping failed because element is degenerated.");
136  }
137 
140  int subLevelIndex (int i, unsigned int codim) const {
141  assert(0<=codim && codim<=1);
142  switch (codim) {
143  case 0:
144  return this->levelIndex_;
145  case 1:
146  return vertex_[i]->levelIndex_;
147  }
148  DUNE_THROW(GridError, "Non-existing codimension requested!");
149  }
150 
153  int subLeafIndex (int i,unsigned int codim) const {
154  assert(0<=codim && codim<=1);
155  switch (codim) {
156  case 0:
157  return this->leafIndex_;
158  case 1:
159  return vertex_[i]->leafIndex_;
160  }
161  DUNE_THROW(GridError, "Non-existing codimension requested!");
162  }
163 
164  std::array<FoamGridEntityImp<0, dimgrid, dimworld, ctype>*, 2> vertex_;
165 
166  std::array<FoamGridEntityImp<dimgrid-1, dimgrid, dimworld, ctype>*, 2> facet_;
167 
169  std::array<FoamGridEntityImp<dimgrid, dimgrid, dimworld, ctype>*, 2> sons_;
170 
172  unsigned int nSons_;
173 
176 
178 
180 
181  bool isNew_;
182 
184  std::function<FieldVector<ctype, dimworld>(FieldVector<ctype, dimgrid>)> elementParametrization_;
185 
189 
196  };
197 
199  template <int dimworld, class ctype>
200  class FoamGridEntityImp<2, 2, dimworld, ctype>
201  : public FoamGridEntityBase
202  {
204  enum {dimgrid = 2};
205 
206  public:
207 
209  enum MarkState { DO_NOTHING , COARSEN , REFINE, IS_COARSENED };
210 
211  FoamGridEntityImp(int level, unsigned int id)
212  : FoamGridEntityBase(level,id),
213  refinementIndex_(-1),
214  nSons_(0),
215  sons_{{nullptr, nullptr, nullptr, nullptr}},
216  facet_{{nullptr, nullptr, nullptr}},
217  vertex_{{nullptr, nullptr, nullptr}},
218  markState_(DO_NOTHING), isNew_(false),
219  father_{nullptr},
220  coarseningBlocked_(false),
221  growthInsertionIndex_(-1)
222  {}
223 
224 
225  int corners() const {
226  return 3;
227  }
228 
229  GeometryType type() const {
230  return GeometryTypes::simplex(2);
231  }
232 
233 
234  bool hasFather() const
235  {
236  return father_!=nullptr;
237  }
238 
239  bool mightVanish() const
240  {
241  return markState_==COARSEN;
242  }
243 
244  bool isLeaf() const {
245  return sons_[0] == nullptr &&
246  sons_[1] == nullptr &&
247  sons_[2] == nullptr &&
248  sons_[3] == nullptr;
249  }
250 
251  bool isNew() const
252  {
253  return isNew_;
254  }
255 
257  unsigned int nSons() const {
258  return nSons_;
259  }
260 
266  FieldVector<ctype,2> globalToLocal(const FieldVector<ctype, dimworld>& coord) const
267  {
268  // If we set up the overdetermined system matrix we have
269  // A[i][0]=vertex_[1].pos_[i]-vertex_[0].pos_[i];
270  // A[i][1]=vertex_[2].pos_[i]-vertex_[0].pos_[i];
271  // t[i]=coord[i]-vertex_[0].pos_[i];
272  //
273  // to determine the local coordinates we solve
274  // A'A x= A' t
275  //
276 
277  FieldMatrix<ctype,2,2> mat; // A'A
278  // mat_{ij}=\sum_k A_{ki}A_{kj}
279  mat=0;
280  for(std::size_t i=0; i <dimworld; ++i)
281  {
282  mat[0][0]+=(vertex_[1]->pos_[i]-vertex_[0]->pos_[i])*(vertex_[1]->pos_[i]-vertex_[0]->pos_[i]);
283  }
284  for(std::size_t i=0; i <dimworld; ++i)
285  {
286  mat[1][0]+=(vertex_[2]->pos_[i]-vertex_[0]->pos_[i])*(vertex_[1]->pos_[i]-vertex_[0]->pos_[i]);
287  }
288  mat[0][1]=mat[1][0];
289  for(std::size_t i=0; i <dimworld; ++i)
290  {
291  mat[1][1]+=(vertex_[2]->pos_[i]-vertex_[0]->pos_[i])*(vertex_[2]->pos_[i]-vertex_[0]->pos_[i]);
292  }
293 
294  FieldVector<ctype, 2> b, x;
295  b=0;
296  for(std::size_t i=0; i <dimworld; ++i)
297  {
298  b[0]+=(vertex_[1]->pos_[i]-vertex_[0]->pos_[i])*(coord[i]-vertex_[0]->pos_[i]);
299  b[1]+=(vertex_[2]->pos_[i]-vertex_[0]->pos_[i])*(coord[i]-vertex_[0]->pos_[i]);
300  }
301  mat.solve(x, b);
302 #ifndef NDEBUG
303  FieldVector<ctype, dimworld> test(vertex_[0]->pos_);
304  test.axpy(x[0], vertex_[1]->pos_);
305  test.axpy(-x[0], vertex_[0]->pos_);
306  test.axpy(x[1], vertex_[2]->pos_);
307  test.axpy(-x[1], vertex_[0]->pos_);
308  assert((test-coord).two_norm()< std::numeric_limits<ctype>::epsilon()*8);
309 #endif
310  return x;
311  }
312 
315  int subLevelIndex (int i, unsigned int codim) const {
316  assert(0<=codim && codim<=2);
317  switch (codim) {
318  case 0:
319  return this->levelIndex_;
320  case 1:
321  return facet_[i]->levelIndex_;
322  case 2:
323  return vertex_[i]->levelIndex_;
324  }
325  DUNE_THROW(GridError, "Non-existing codimension requested!");
326  }
327 
330  int subLeafIndex (int i,unsigned int codim) const {
331  assert(0<=codim && codim<=2);
332  switch (codim) {
333  case 0:
334  return this->leafIndex_;
335  case 1:
336  return facet_[i]->leafIndex_;
337  case 2:
338  return vertex_[i]->leafIndex_;
339  }
340  DUNE_THROW(GridError, "Non-existing codimension requested!");
341  }
342 
351 
352  unsigned int nSons_;
353 
354  std::array<FoamGridEntityImp<dimgrid, dimgrid, dimworld, ctype>*, 4> sons_;
355 
356  std::array<FoamGridEntityImp<dimgrid-1, dimgrid, dimworld, ctype>*, 3> facet_;
357 
358  std::array<FoamGridEntityImp<0, dimgrid, dimworld, ctype>*, 3> vertex_;
359 
362 
364  bool isNew_;
365 
367 
369  std::function<FieldVector<ctype, dimworld>(FieldVector<ctype, dimgrid>)> elementParametrization_;
370 
374 
381  };
382 }
383 
384 #endif
Definition: dgffoam.cc:6
Element specialization of FoamGridEntityImp. Element is a grid entity of topological codimension 0 an...
Definition: foamgridelements.hh:18
std::array< FoamGridEntityImp< dimgrid, dimgrid, dimworld, ctype > *, 2 > sons_
links to refinements of this edge
Definition: foamgridelements.hh:169
bool isLeaf() const
Definition: foamgridelements.hh:75
GeometryType type() const
Definition: foamgridelements.hh:95
int corners() const
Number of corners (==2)
Definition: foamgridelements.hh:105
unsigned int nSons_
The number of refined edges (0 or 2).
Definition: foamgridelements.hh:172
int growthInsertionIndex_
If this element was created in a growth step this will be the index of insertion So if this is the fi...
Definition: foamgridelements.hh:195
std::array< FoamGridEntityImp< 0, dimgrid, dimworld, ctype > *, 2 > vertex_
Definition: foamgridelements.hh:164
bool hasFather() const
Definition: foamgridelements.hh:99
MarkState markState_
Definition: foamgridelements.hh:179
bool coarseningBlocked_
This flag is set by postGrow() if the element looses its right to coarsen because it contains a bifur...
Definition: foamgridelements.hh:188
MarkState
The different ways to mark an element for grid changes.
Definition: foamgridelements.hh:31
@ COARSEN
Definition: foamgridelements.hh:31
std::function< FieldVector< ctype, dimworld >FieldVector< ctype, dimgrid >)> elementParametrization_
The element parametrization.
Definition: foamgridelements.hh:184
std::array< FoamGridEntityImp< dimgrid-1, dimgrid, dimworld, ctype > *, 2 > facet_
Definition: foamgridelements.hh:166
FoamGridEntityImp(FoamGridEntityImp< 0, dimgrid, dimworld, ctype > *v0, FoamGridEntityImp< 0, dimgrid, dimworld, ctype > *v1, int level, unsigned int id)
Definition: foamgridelements.hh:33
int refinementIndex_
Definition: foamgridelements.hh:177
FoamGridEntityImp(int level, unsigned int id)
Definition: foamgridelements.hh:61
bool mightVanish() const
Definition: foamgridelements.hh:85
FoamGridEntityImp(FoamGridEntityImp< 0, dimgrid, dimworld, ctype > *v0, FoamGridEntityImp< 0, dimgrid, dimworld, ctype > *v1, int level, unsigned int id, FoamGridEntityImp *father)
Definition: foamgridelements.hh:46
unsigned int nSons() const
Definition: foamgridelements.hh:81
int subLeafIndex(int i, unsigned int codim) const
Return leaf index of sub entity with codim = cc and local number i.
Definition: foamgridelements.hh:153
bool isNew() const
Definition: foamgridelements.hh:90
FoamGridEntityImp< dimgrid, dimgrid, dimworld, ctype > * father_
Pointer to father element.
Definition: foamgridelements.hh:175
bool isNew_
Definition: foamgridelements.hh:181
PartitionType partitionType() const
Definition: foamgridelements.hh:116
FieldVector< ctype, 1 > globalToLocal(const FieldVector< ctype, dimworld > &coord) const
Compute local cordinates from global ones.
Definition: foamgridelements.hh:125
int subLevelIndex(int i, unsigned int codim) const
Return level index of sub entity with codim = cc and local number i.
Definition: foamgridelements.hh:140
FieldVector< ctype, dimworld > corner(int i) const
Definition: foamgridelements.hh:109
bool isLeaf() const
Definition: foamgridelements.hh:244
unsigned int nSons_
Definition: foamgridelements.hh:352
GeometryType type() const
Definition: foamgridelements.hh:229
std::array< FoamGridEntityImp< 0, dimgrid, dimworld, ctype > *, 3 > vertex_
Definition: foamgridelements.hh:358
MarkState markState_
Stores requests for refinement and coarsening.
Definition: foamgridelements.hh:361
int subLevelIndex(int i, unsigned int codim) const
Return level index of sub entity with codim = cc and local number i.
Definition: foamgridelements.hh:315
bool isNew_
This flag is set by adapt() if this element has been newly created.
Definition: foamgridelements.hh:364
bool mightVanish() const
Definition: foamgridelements.hh:239
FoamGridEntityImp< dimgrid, dimgrid,dimworld, ctype > * father_
Definition: foamgridelements.hh:366
FieldVector< ctype, 2 > globalToLocal(const FieldVector< ctype, dimworld > &coord) const
Compute local cordinates from global ones.
Definition: foamgridelements.hh:266
std::array< FoamGridEntityImp< dimgrid-1, dimgrid, dimworld, ctype > *, 3 > facet_
Definition: foamgridelements.hh:356
int corners() const
Definition: foamgridelements.hh:225
int subLeafIndex(int i, unsigned int codim) const
Return leaf index of sub entity with codim = cc and local number i.
Definition: foamgridelements.hh:330
bool hasFather() const
Definition: foamgridelements.hh:234
bool isNew() const
Definition: foamgridelements.hh:251
std::function< FieldVector< ctype, dimworld >FieldVector< ctype, dimgrid >)> elementParametrization_
The element parametrization.
Definition: foamgridelements.hh:369
FoamGridEntityImp(int level, unsigned int id)
Definition: foamgridelements.hh:211
unsigned int nSons() const
Definition: foamgridelements.hh:257
int refinementIndex_
index of the refined element in the father
Definition: foamgridelements.hh:350
int growthInsertionIndex_
If this element was created in a growth step this will be the index of insertion So if this is the fi...
Definition: foamgridelements.hh:380
bool coarseningBlocked_
This flag is set by postGrow() if the element looses its right to coarsen because it contains a bifur...
Definition: foamgridelements.hh:373
MarkState
The different ways to mark an element for grid changes.
Definition: foamgridelements.hh:209
@ COARSEN
Definition: foamgridelements.hh:209
std::array< FoamGridEntityImp< dimgrid, dimgrid, dimworld, ctype > *, 4 > sons_
Definition: foamgridelements.hh:354
Base class for FoamGrid entity implementation classes.
Definition: foamgridvertex.hh:16
The actual entity implementation.
Definition: foamgridvertex.hh:47
Vertex specialization of FoamGridEntityImp.
Definition: foamgridvertex.hh:53