dune-fem  2.4.1-rc
checkgeomaffinity.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_CHECKGEOMETRYAFFINITY_HH
2 #define DUNE_FEM_CHECKGEOMETRYAFFINITY_HH
3 
4 #include <dune/common/fvector.hh>
5 #include <dune/grid/common/gridenums.hh>
6 
9 
10 namespace Dune
11 {
12 
13  namespace Fem
14  {
15 
20  template <class QuadratureType>
23  {
24  // return true if geometry is affine
25  template< class EntityType, class ElementGeometryType >
26  static bool checkGeometry( const EntityType& entity,
27  const ElementGeometryType& geo,
28  const int quadOrd )
29  {
30  // if method tells that geometry is not affine
31  // then check it carefully
32  if( ! geo.affine() )
33  {
34  // get quadrature of desired order
35  QuadratureType volQuad( entity, quadOrd );
36  const int nop = volQuad.nop();
37 
38  // check all integration elements against the first
39  const double oldIntel = geo.integrationElement( volQuad.point(0) );
40  for(int l=1; l<nop; ++l)
41  {
42  const double intel = geo.integrationElement( volQuad.point(l) );
43  if( std::abs( oldIntel - intel ) > 1e-12 )
44  return false;
45  }
46  }
47  return true ;
48  }
49 
50  // return true if geometry is affine
51  template< class EntityType >
52  static bool checkGeometry( const EntityType& entity, const int quadOrd )
53  {
54  return checkGeometry( entity, entity.geometry(), quadOrd );
55  }
56 
58  template <class IteratorType>
59  static inline bool checkAffinity(const IteratorType& begin,
60  const IteratorType& endit,
61  const int quadOrd)
62  {
63  for(IteratorType it = begin; it != endit; ++it)
64  {
65  if( ! checkGeometry( *it, quadOrd ) ) return false ;
66  }
67  return true;
68  }
69 
71  template <class GridPartType, class Vector >
72  static inline void checkElementAffinity(const GridPartType& gridPart,
73  const int quadOrd,
74  Vector& affineGeomtryVec )
75  {
76  typedef typename GridPartType :: template Codim< 0 > :: IteratorType IteratorType;
77  typedef typename GridPartType :: template Codim< 0 > :: EntityType EntityType;
78  const IteratorType endit = gridPart.template end<0> ();
79  affineGeomtryVec.resize( gridPart.indexSet().size( 0 ) );
80  for(IteratorType it = gridPart.template begin<0>(); it != endit; ++it)
81  {
82  const EntityType& entity = *it ;
83  const int index = gridPart.indexSet().index( entity );
84  affineGeomtryVec[ index ] = checkGeometry( entity, quadOrd );
85  }
86 
87  //for( size_t i=0; i<affineGeomtryVec.size(); ++ i)
88  // std::cout << "geo is " << affineGeomtryVec[ i ] << std::endl;
89  }
90  };
91 
93  template <class GridPartType>
95  {
96  typedef typename GridPartType :: GridType GridType ;
97  protected:
99  template <class IndexSetType>
100  static inline bool doCheck(const GridType& grid, const IndexSetType& indexSet)
101  {
102  typedef typename GridType :: template Codim<0> :: LevelIterator MacroIteratorType;
103  typedef typename GridType :: template Codim<0> :: Entity EntityType;
104  typedef typename GridType :: template Codim<0> :: Geometry Geometry;
105 
106  typedef typename GridType :: template Partition< All_Partition > :: LevelGridView MacroGridView ;
107 
108  // get macro grid view
109  MacroGridView macroView = grid.levelGridView( 0 );
110 
111  const MacroIteratorType endit = macroView.template end<0> ();
112  MacroIteratorType it = macroView.template begin<0> ();
113 
114  // empty grids are considerd as cartesian
115  if( it == endit ) return true;
116 
117  typedef AllGeomTypes< IndexSetType, GridType> GeometryInformationType;
118  GeometryInformationType geoInfo( indexSet );
119 
120  // if we have more than one geometry Type return false
121  if( geoInfo.geomTypes(0).size() > 1 ) return false;
122 
123  // if this type is not cube return false
124  if( ! geoInfo.geomTypes(0)[0].isCube() ) return false;
125 
126  typedef typename GridType :: ctype ctype;
127  enum { dimension = GridType :: dimension };
128  enum { dimworld = GridType :: dimensionworld };
129 
130  // grid width
131  FieldVector<ctype, dimension> h(0);
132  FieldVector<ctype, dimworld> enBary;
133  FieldVector<ctype, dimension-1> mid(0.5);
134 
135  const int map[3] = {1, 2, 4};
136  {
137  const Geometry geo = it->geometry();
138  if ( ! geo.type().isCube() ) return false;
139 
140  // calculate grid with
141  for(int i=0; i<dimension; ++i)
142  {
143  h[i] = (geo.corner( 0 ) - geo.corner( map[i] )).two_norm();
144  }
145  }
146 
147  // loop over all macro elements
148  for(MacroIteratorType it = macroView.template begin<0> ();
149  it != endit; ++it)
150  {
151  const EntityType& en = *it;
152  const Geometry geo = en.geometry();
153 
154  const FieldVector<ctype, dimworld> enBary =
155  geo.global( geoInfo.localCenter( geo.type() ));
156 
157  typedef typename MacroGridView :: IntersectionIterator IntersectionIteratorType;
158 
159  // if geometry is not cube, it's not a cartesian grid
160  if ( ! geo.type().isCube() ) return false;
161 
162  for(int i=0; i<dimension; ++i)
163  {
164  const ctype w = (geo.corner(0) - geo.corner( map[i] )).two_norm();
165  if( std::abs( h[i] - w ) > 1e-15 ) return false;
166  }
167 
168  IntersectionIteratorType endnit = macroView.iend( en );
169  for(IntersectionIteratorType nit = macroView.ibegin( en );
170  nit != endnit; ++nit)
171  {
172  const typename IntersectionIteratorType::Intersection& inter=*nit;
173  if( ! checkIntersection(inter) ) return false;
174 
175  if( inter.neighbor() )
176  {
177  EntityType nb = make_entity( inter.outside() );
178  Geometry nbGeo = nb.geometry();
179 
180  FieldVector<ctype, dimworld> diff = nbGeo.global( geoInfo.localCenter( nbGeo.type() ));
181  diff -= enBary;
182  assert( diff.two_norm() > 1e-15 );
183  diff /= diff.two_norm();
184 
185  // scalar product should be 1 or -1
186  if( std::abs(std::abs((diff * inter.unitOuterNormal(mid))) - 1.0) > 1e-12 ) return false;
187  }
188  }
189  }
190  return true;
191  }
192 
193  template <class ctype, int dim>
195  {
196  const FieldVector<ctype,dim-1> mid_;
197  enum { numberOfNormals = 2 * dim };
198  FieldVector<ctype,dim> refNormal_[numberOfNormals];
199  public:
200  ReferenceNormals () : mid_(0.5)
201  {
202  for(int i=0; i<numberOfNormals; ++i)
203  {
204  // get i-th reference normal
205  FieldVector<ctype,dim>& refNormal = refNormal_[i];
206  // reset normal
207  refNormal = 0;
208  // set one component
209  int comp = ((int) i/2);
210  refNormal[comp] = ((i % 2) == 0) ? -1 : 1;
211  }
212  }
213 
214  const FieldVector<ctype,dim>& referenceNormal(const int i) const
215  {
216  assert( i >= 0 && i< numberOfNormals );
217  return refNormal_[i];
218  }
219 
220  const FieldVector<ctype,dim-1>& faceMidPoint() const
221  {
222  return mid_;
223  }
224  };
225 
226  public:
227  // check that element is provided following the DUNE reference cube
228  template <class IntersectionType>
229  static bool checkIntersection(const IntersectionType& nit)
230  {
231  if ( ! nit.type().isCube() ) return false;
232 
233  typedef typename IntersectionType :: Entity EntityType;
234  typedef typename EntityType :: Geometry :: ctype ctype;
235  enum { dimworld = EntityType :: Geometry :: coorddimension };
236 
237  // get reference normals
238  static const ReferenceNormals<ctype,dimworld> normals;
239 
240  // get current normal
241  FieldVector<ctype,dimworld> unitNormal = nit.unitOuterNormal(normals.faceMidPoint());
242  unitNormal -= normals.referenceNormal( nit.indexInInside() );
243 
244  // if normals are not equal grid is not cartesian
245  if( unitNormal.infinity_norm() > 1e-10 ) return false;
246 
247  return true;
248  }
249 
251  static inline bool check(const GridPartType& gridPart)
252  {
253  bool cartesian = doCheck( gridPart.grid() , gridPart.indexSet() );
254  int val = (cartesian) ? 1 : 0;
255  // take global minimum
256  val = gridPart.comm().min( val );
257  return (val == 1) ? true : false;
258  }
259  };
260 
262 
263  } // namespace Fem
264 
265 } // namespace Dune
266 
267 #endif // #ifndef DUNE_FEM_CHECKGEOMETRYAFFINITY_HH
const FieldVector< ctype, dim-1 > & faceMidPoint() const
Definition: checkgeomaffinity.hh:220
static bool checkIntersection(const IntersectionType &nit)
Definition: checkgeomaffinity.hh:229
const FieldVector< ctype, dim > & referenceNormal(const int i) const
Definition: checkgeomaffinity.hh:214
ReferenceNormals()
Definition: checkgeomaffinity.hh:200
static bool checkGeometry(const EntityType &entity, const int quadOrd)
Definition: checkgeomaffinity.hh:52
Helper class to check whether grid is structured or not.
Definition: checkgeomaffinity.hh:94
static bool checkGeometry(const EntityType &entity, const ElementGeometryType &geo, const int quadOrd)
Definition: checkgeomaffinity.hh:26
GridPartType::GridType GridType
Definition: checkgeomaffinity.hh:96
Double abs(const Double &a)
Definition: double.hh:860
Dune::EntityPointer< Grid, Implementation >::Entity make_entity(const Dune::EntityPointer< Grid, Implementation > &entityPointer)
Definition: compatibility.hh:23
static bool check(const GridPartType &gridPart)
check whether all the is grid is a cartesian grid
Definition: checkgeomaffinity.hh:251
static bool doCheck(const GridType &grid, const IndexSetType &indexSet)
check whether this is a cartesian grid or not
Definition: checkgeomaffinity.hh:100
Definition: coordinate.hh:4
static void checkElementAffinity(const GridPartType &gridPart, const int quadOrd, Vector &affineGeomtryVec)
check whether all geometry mappings are affine
Definition: checkgeomaffinity.hh:72
Helper class to check affinity of the grid&#39;s geometries.
Definition: checkgeomaffinity.hh:22
default implementation uses method geomTypes of given index set. Used in DiscreteFunctionSpaces.
Definition: allgeomtypes.hh:89
static bool checkAffinity(const IteratorType &begin, const IteratorType &endit, const int quadOrd)
check whether all geometry mappings are affine
Definition: checkgeomaffinity.hh:59
Definition: checkgeomaffinity.hh:194