3#ifndef DUNE_ALBERTA_ELEMENTINFO_HH
4#define DUNE_ALBERTA_ELEMENTINFO_HH
15#include <dune/grid/albertagrid/geometrycache.hh>
16#include <dune/grid/albertagrid/macroelement.hh>
32 struct BasicNodeProjection;
48 typedef Instance *InstancePtr;
51 static const int dimension = dim;
53 static const int numVertices = NumSubEntities< dimension, dimension >::value;
54 static const int numFaces = NumSubEntities< dimension, 1 >::value;
56 typedef Alberta::MacroElement< dimension > MacroElement;
57 typedef Alberta::MeshPointer< dimension > MeshPointer;
58 typedef Alberta::FillFlags< dimension > FillFlags;
60 static const int maxNeighbors = N_NEIGH_MAX;
62 static const int maxLevelNeighbors = Library< dimWorld >::maxLevelNeighbors;
64#if !DUNE_ALBERTA_CACHE_COORDINATES
65 typedef GeometryCacheProxy< dim > GeometryCache;
71 explicit ElementInfo (
const InstancePtr &instance );
75 ElementInfo (
const MeshPointer &mesh,
const MacroElement ¯oElement,
76 typename FillFlags::Flags fillFlags = FillFlags::standard );
77 ElementInfo (
const MeshPointer &mesh,
const Seed &seed,
78 typename FillFlags::Flags fillFlags = FillFlags::standard );
79 ElementInfo (
const ElementInfo &other );
80 ElementInfo ( ElementInfo&& other );
84 ElementInfo &operator= (
const ElementInfo &other );
85 ElementInfo &operator= ( ElementInfo &&other );
87 explicit operator bool ()
const {
return (instance_ != null()); }
89 bool operator== (
const ElementInfo &other )
const;
90 bool operator!= (
const ElementInfo &other )
const;
92 const MacroElement ¯oElement ()
const;
93 ElementInfo father ()
const;
94 int indexInFather ()
const;
95 ElementInfo child (
int i )
const;
100 MeshPointer mesh ()
const;
102 bool mightVanish ()
const;
109 int getMark ()
const;
110 void setMark (
int refCount )
const;
112 bool hasLeafNeighbor (
const int face )
const;
113 ElementInfo leafNeighbor (
const int face )
const;
125 int levelNeighbors (
const int face, ElementInfo (&neighbor)[ maxLevelNeighbors ],
int (&faceInNeighbor)[ maxLevelNeighbors ] )
const;
127 template<
int codim >
128 int twist (
int subEntity )
const;
129 int twistInNeighbor (
int face )
const;
130 bool isBoundary (
int face )
const;
131 int boundaryId (
int face )
const;
132 AffineTransformation *transformation (
int face )
const;
133 BasicNodeProjection *boundaryProjection (
int face )
const;
135 bool hasCoordinates ()
const;
136 const GlobalVector &coordinate (
int vertex )
const;
137#if !DUNE_ALBERTA_CACHE_COORDINATES
138 GeometryCache geometryCache ()
const
140 return GeometryCache( instance_->geometryCache, instance_->elInfo );
144 template<
class Functor >
145 void hierarchicTraverse ( Functor &functor )
const;
147 template<
class Functor >
148 void leafTraverse ( Functor &functor )
const;
150 const Element *element ()
const;
151 const Element *neighbor (
int face )
const;
152 Element *el ()
const;
153 ALBERTA EL_INFO &elInfo ()
const;
156 createFake (
const MeshPointer &mesh,
157 const Element *element,
int level,
int type = 0 );
158 static ElementInfo createFake (
const ALBERTA EL_INFO &elInfo );
161 static bool isLeaf ( Element *element );
162 static bool mightVanish ( Element *element,
int depth );
164 static void fill ( Mesh *mesh,
const ALBERTA MACRO_EL *mel, ALBERTA EL_INFO &elInfo );
165 static void fill (
int ichild,
const ALBERTA EL_INFO &parentInfo, ALBERTA EL_INFO &elInfo );
167 void addReference ()
const;
168 void removeReference ()
const;
170 static InstancePtr null ();
171 static Stack &stack ();
173 InstancePtr instance_;
182 struct ElementInfo< dim >::Instance
184 ALBERTA EL_INFO elInfo;
185 unsigned int refCount;
187 InstancePtr &parent ()
195#if !DUNE_ALBERTA_CACHE_COORDINATES
197 Alberta::GeometryCache< dim > geometryCache;
207 class ElementInfo< dim >::Stack
216 InstancePtr allocate ();
217 void release ( InstancePtr &p );
228 struct ElementInfo< dim >::Library
230 typedef Alberta::ElementInfo< dim > ElementInfo;
232 static const int maxLevelNeighbors = (1 << (dim-1));
235 leafNeighbor (
const ElementInfo &element,
const int face, ElementInfo &neighbor );
238 levelNeighbors (
const ElementInfo &element,
const int face,
239 ElementInfo (&neighbor)[ maxLevelNeighbors ],
int (&faceInNeighbor)[ maxLevelNeighbors ] );
243 macroNeighbor (
const ElementInfo &element,
const int face, ElementInfo &neighbor );
252 struct ElementInfo< dim >::Seed
255 : macroIndex_( -1 ), level_( 0 ), path_( 0 )
258 Seed (
const int macroIndex,
const int level,
const unsigned long path )
259 : macroIndex_( macroIndex ), level_( level ), path_( path )
264 return (macroIndex() == other.macroIndex()) && (level() == other.level()) && (path() == other.path());
267 bool operator< (
const Seed &other )
const
269 const bool ml = (macroIndex() < other.macroIndex());
270 const bool me = (macroIndex() == other.macroIndex());
271 const bool ll = (level() < other.level());
272 const bool le = (level() == other.level());
273 const bool pl = (path() < other.path());
274 return ml | (me & (ll | (
le & pl)));
277 bool operator!= (
const Seed &other )
const {
return !(*
this == other); }
278 bool operator<= (
const Seed &other )
const {
return !(other < *
this); }
279 bool operator> (
const Seed &other )
const {
return (other < *
this); }
280 bool operator>= (
const Seed &other )
const {
return !(*
this < other); }
282 bool isValid ( )
const {
return macroIndex_ != -1; }
284 int macroIndex ()
const {
return macroIndex_; }
285 int level ()
const {
return level_; }
286 unsigned long path ()
const {
return path_; }
300 inline ElementInfo< dim >::ElementInfo (
const InstancePtr &instance )
301 : instance_( instance )
308 inline ElementInfo< dim >::ElementInfo ()
309 : instance_( null() )
316 inline ElementInfo< dim >
317 ::ElementInfo (
const MeshPointer &mesh,
const MacroElement ¯oElement,
318 typename FillFlags::Flags fillFlags )
320 instance_ = stack().allocate();
321 instance_->parent() = null();
322 ++(instance_->parent()->refCount);
326 elInfo().fill_flag = fillFlags;
329 for(
int k = 0; k < maxNeighbors; ++k )
330 elInfo().opp_vertex[ k ] = -1;
332 fill( mesh, ¯oElement, elInfo() );
337 inline ElementInfo< dim >
338 ::ElementInfo (
const MeshPointer &mesh,
const Seed &seed,
339 typename FillFlags::Flags fillFlags )
341 instance_ = stack().allocate();
342 instance_->parent() = null();
343 ++(instance_->parent()->refCount);
348 elInfo().fill_flag = fillFlags;
351 for(
int k = 0; k < maxNeighbors; ++k )
352 elInfo().opp_vertex[ k ] = -1;
354 fill( mesh, ((Mesh *)mesh)->macro_els + seed.macroIndex(), elInfo() );
357 unsigned long path = seed.path();
358 for(
int i = 0; i < seed.level(); ++i )
360 InstancePtr child = stack().allocate();
361 child->parent() = instance_;
364 for(
int k = 0; k < maxNeighbors; ++k )
365 child->elInfo.opp_vertex[ k ] = -2;
367 fill( path & 1, elInfo(), child->elInfo );
375 assert( this->seed() == seed );
380 inline ElementInfo< dim >::ElementInfo (
const ElementInfo &other )
381 : instance_( other.instance_ )
387 inline ElementInfo< dim >::ElementInfo ( ElementInfo &&other )
391 swap( instance_, other.instance_ );
395 inline ElementInfo< dim >::~ElementInfo ()
402 inline ElementInfo< dim > &
403 ElementInfo< dim >::operator= (
const ElementInfo< dim > &other )
405 other.addReference();
407 instance_ = other.instance_;
412 inline ElementInfo< dim > &
413 ElementInfo< dim >::operator= ( ElementInfo< dim > &&other )
416 swap( instance_, other.instance_ );
424 return (instance_->elInfo.el == other.instance_->elInfo.el);
432 return (instance_->elInfo.el != other.instance_->elInfo.el);
437 inline const typename ElementInfo< dim >::MacroElement &
438 ElementInfo< dim >::macroElement ()
const
441 assert( elInfo().macro_el != NULL );
442 return static_cast< const MacroElement &
>( *(elInfo().macro_el) );
447 inline ElementInfo< dim > ElementInfo< dim >::father ()
const
450 return ElementInfo< dim >( instance_->parent() );
455 inline int ElementInfo< dim >::indexInFather ()
const
457 const Element *element = elInfo().el;
458 const Element *father = elInfo().parent->el;
459 assert( father != NULL );
461 const int index = (father->child[ 0 ] == element ? 0 : 1);
462 assert( father->child[ index ] == element );
468 inline ElementInfo< dim > ElementInfo< dim >::child (
int i )
const
472 InstancePtr child = stack().allocate();
473 child->parent() = instance_;
477 for(
int k = 0; k < maxNeighbors; ++k )
478 child->elInfo.opp_vertex[ k ] = -2;
480 fill( i, elInfo(), child->elInfo );
481 return ElementInfo< dim >( child );
486 inline bool ElementInfo< dim >::isLeaf ()
const
488 assert( !(*
this) ==
false );
489 return isLeaf( el() );
494 inline typename ElementInfo< dim >::Seed ElementInfo< dim >::seed ()
const
499 unsigned long path = 0;
500 for( InstancePtr p = instance_; p->parent() != null(); p = p->parent() )
502 const Element *element = p->elInfo.el;
503 const Element *father = p->parent()->elInfo.el;
504 const unsigned long child =
static_cast< unsigned long >( father->child[ 1 ] == element );
505 path = (path << 1) | child;
509 if( level != elInfo().level )
510 DUNE_THROW( NotImplemented,
"Seed for fake elements not implemented." );
512 return Seed( macroElement().index, level, path );
517 inline typename ElementInfo< dim >::MeshPointer ElementInfo< dim >::mesh ()
const
519 return MeshPointer( elInfo().mesh );
524 inline bool ElementInfo< dim >::mightVanish ()
const
526 return mightVanish( el(), 0 );
531 inline int ElementInfo< dim >::level ()
const
533 return elInfo().level;
538 inline int ElementInfo< dim >::type ()
const
545 inline int ElementInfo< 3 >::type ()
const
547 return instance_->elInfo.el_type;
552 inline int ElementInfo< dim >::getMark ()
const
559 inline void ElementInfo< dim >::setMark (
int refCount )
const
562 assert( (refCount >= -128) && (refCount < 127) );
563 el()->mark = refCount;
568 inline bool ElementInfo< dim >::hasLeafNeighbor (
const int face )
const
571 assert( (face >= 0) && (face < maxNeighbors) );
573 assert( (elInfo().fill_flag & FillFlags::boundaryId) != 0 );
574 const int macroFace = elInfo().macro_wall[ face ];
576 return (macroElement().neighbor( macroFace ) != NULL);
583 inline ElementInfo< dim > ElementInfo< dim >::leafNeighbor (
const int face )
const
585 assert( (face >= 0) && (face < numFaces) );
586 ElementInfo neighbor;
587 Library< dimWorld >::leafNeighbor( *
this, face, neighbor );
593 inline int ElementInfo< dim >
594 ::levelNeighbors (
const int face, ElementInfo (&neighbor)[ maxLevelNeighbors ],
int (&faceInNeighbor)[ maxLevelNeighbors ] )
const
596 assert( (face >= 0) && (face < numFaces) );
597 return Library< dimWorld >::levelNeighbors( *
this, face, neighbor, faceInNeighbor );
602 template<
int codim >
603 inline int ElementInfo< dim >::twist (
int subEntity )
const
605 return Twist< dim, dim-codim >::twist( element(), subEntity );
610 inline int ElementInfo< dim >::twistInNeighbor (
const int face )
const
612 assert( neighbor( face ) != NULL );
613 return Twist< dim, dim-1 >::twist( neighbor( face ), elInfo().opp_vertex[ face ] );
618 inline bool ElementInfo< dim >::isBoundary (
int face )
const
621 assert( (face >= 0) && (face < maxNeighbors) );
623 assert( (elInfo().fill_flag & FillFlags::boundaryId) != 0 );
624 const int macroFace = elInfo().macro_wall[ face ];
626 return macroElement().isBoundary( macroFace );
633 inline int ElementInfo< dim >::boundaryId (
int face )
const
636 assert( (face >= 0) && (face < N_WALLS_MAX) );
638 assert( (elInfo().fill_flag & FillFlags::boundaryId) != 0 );
639 const int macroFace = elInfo().macro_wall[ face ];
640 const int id = macroElement().boundaryId( macroFace );
648 inline AffineTransformation *
649 ElementInfo< dim >::transformation (
int face )
const
652 assert( (face >= 0) && (face < N_WALLS_MAX) );
654 assert( (elInfo().fill_flag & FillFlags::boundaryId) != 0 );
655 const int macroFace = elInfo().macro_wall[ face ];
656 return (macroFace < 0 ? NULL : macroElement().wall_trafo[ macroFace ]);
661 inline BasicNodeProjection *
662 ElementInfo< dim >::boundaryProjection (
int face )
const
665 assert( (face >= 0) && (face < N_WALLS_MAX) );
667 assert( (elInfo().fill_flag & FillFlags::boundaryId) != 0 );
668 const int macroFace = elInfo().macro_wall[ face ];
670 return static_cast< BasicNodeProjection *
>( macroElement().projection[ macroFace+1 ] );
677 inline bool ElementInfo< dim >::hasCoordinates ()
const
679 return ((elInfo().fill_flag & FillFlags::coords) != 0);
683 inline const GlobalVector &ElementInfo< dim >::coordinate (
int vertex )
const
685 assert( hasCoordinates() );
687 return elInfo().coord[
vertex ];
692 template<
class Functor >
693 inline void ElementInfo< dim >::hierarchicTraverse ( Functor &functor )
const
698 child( 0 ).hierarchicTraverse( functor );
699 child( 1 ).hierarchicTraverse( functor );
705 template<
class Functor >
706 inline void ElementInfo< dim >::leafTraverse ( Functor &functor )
const
710 child( 0 ).leafTraverse( functor );
711 child( 1 ).leafTraverse( functor );
719 inline const Element *ElementInfo< dim >::element ()
const
726 inline const Element *ElementInfo< dim >::neighbor (
int face )
const
728 assert( (face >= 0) && (face < numFaces) );
729 assert( (elInfo().fill_flag & FillFlags::neighbor) != 0 );
730 return elInfo().neigh[ face ];
735 inline Element *ElementInfo< dim >::el ()
const
742 inline ALBERTA EL_INFO &ElementInfo< dim >::elInfo ()
const
744 return (instance_->elInfo);
749 inline ElementInfo< dim >
750 ElementInfo< dim >::createFake (
const MeshPointer &mesh,
751 const Element *element,
int level,
int type )
753 InstancePtr instance = stack().allocate();
754 instance->parent() = null();
755 ++(instance->parent()->refCount);
757 instance->elInfo.mesh = mesh;
758 instance->elInfo.macro_el = NULL;
759 instance->elInfo.el =
const_cast< Element *
>( element );
760 instance->elInfo.parent = NULL;
761 instance->elInfo.fill_flag = FillFlags::nothing;
762 instance->elInfo.level = level;
763 instance->elInfo.el_type = type;
765 return ElementInfo< dim >( instance );
770 inline ElementInfo< dim >
771 ElementInfo< dim >::createFake (
const ALBERTA EL_INFO &elInfo )
773 InstancePtr instance = stack().allocate();
774 instance->parent() = null();
775 ++(instance->parent()->refCount);
777 instance->elInfo = elInfo;
778 return ElementInfo< dim >( instance );
783 inline bool ElementInfo< dim >::isLeaf ( Element *element )
785 return IS_LEAF_EL( element );
790 inline bool ElementInfo< dim >::mightVanish ( Alberta::Element *element,
int depth )
792 if( isLeaf( element ) )
793 return (element->mark < depth);
795 return (mightVanish( element->child[ 0 ], depth-1 ) && mightVanish( element->child[ 1 ], depth-1 ));
800 inline void ElementInfo< dim >
801 ::fill ( Mesh *mesh,
const ALBERTA MACRO_EL *mel, ALBERTA EL_INFO &elInfo )
803 ALBERTA fill_macro_info( mesh, mel, &elInfo );
807 inline void ElementInfo< dim >
808 ::fill (
int ichild,
const ALBERTA EL_INFO &parentInfo, ALBERTA EL_INFO &elInfo )
810 ALBERTA fill_elinfo( ichild, FILL_ANY, &parentInfo, &elInfo );
815 inline void ElementInfo< dim >::addReference ()
const
817 ++(instance_->refCount);
822 inline void ElementInfo< dim >::removeReference ()
const
828 for( InstancePtr instance = instance_; --(instance->refCount) == 0; )
830 const InstancePtr parent = instance->parent();
831 stack().release( instance );
838 inline typename ElementInfo< dim >::InstancePtr
839 ElementInfo< dim >::null ()
841 return stack().null();
846 inline typename ElementInfo< dim >::Stack &
847 ElementInfo< dim >::stack ()
859 inline ElementInfo< dim >::Stack::Stack ()
862 null_.elInfo.el = NULL;
869 inline ElementInfo< dim >::Stack::~Stack ()
873 InstancePtr p = top_;
881 inline typename ElementInfo< dim >::InstancePtr
882 ElementInfo< dim >::Stack::allocate ()
884 InstancePtr p = top_;
895 inline void ElementInfo< dim >::Stack::release ( InstancePtr &p )
897 assert( (p != null()) && (p->refCount == 0) );
904 inline typename ElementInfo< dim >::InstancePtr
905 ElementInfo< dim >::Stack::null ()
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
bool le(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first lesser or equal second
Definition: float_cmp.cc:168
constexpr GeometryType vertex
GeometryType representing a vertex.
Definition: type.hh:727
EnableIfInterOperable< T1, T2, bool >::type operator<(const RandomAccessIteratorFacade< T1, V1, R1, D > &lhs, const RandomAccessIteratorFacade< T2, V2, R2, D > &rhs)
Comparison operator.
Definition: iteratorfacades.hh:629
EnableIfInterOperable< T1, T2, bool >::type operator>(const RandomAccessIteratorFacade< T1, V1, R1, D > &lhs, const RandomAccessIteratorFacade< T2, V2, R2, D > &rhs)
Comparison operator.
Definition: iteratorfacades.hh:675
EnableIfInterOperable< T1, T2, bool >::type operator<=(const RandomAccessIteratorFacade< T1, V1, R1, D > &lhs, const RandomAccessIteratorFacade< T2, V2, R2, D > &rhs)
Comparison operator.
Definition: iteratorfacades.hh:652
EnableIfInterOperable< T1, T2, bool >::type operator==(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for equality.
Definition: iteratorfacades.hh:233
EnableIfInterOperable< T1, T2, bool >::type operator>=(const RandomAccessIteratorFacade< T1, V1, R1, D > &lhs, const RandomAccessIteratorFacade< T2, V2, R2, D > &rhs)
Comparison operator.
Definition: iteratorfacades.hh:697
EnableIfInterOperable< T1, T2, bool >::type operator!=(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for inequality.
Definition: iteratorfacades.hh:255
Dune namespace.
Definition: alignedallocator.hh:10