dune-grid  2.1.1
alu2dinclude.hh
Go to the documentation of this file.
00001 #ifndef DUNE_ALU2DGRID_INCLUDE_HH
00002 #define DUNE_ALU2DGRID_INCLUDE_HH
00003 
00004 #define ALU2DGRID_COMPATIBILITY_LEVEL 2
00005 
00006 #include <alugrid_2d.h>
00007 
00009 // compile imp.cc into lib (1 yes, 0 no)
00010 // if you change this, you'll get what you deserve
00012 #define COMPILE_ALU2DGRID_LIB 0
00013 
00014 #if COMPILE_ALU2DGRID_LIB 
00015   #define COMPILE_ALU2DGRID_INLINE 0
00016 #else
00017   #define COMPILE_ALU2DGRID_INLINE 1
00018 #endif
00019 
00020 #if COMPILE_ALU2DGRID_INLINE
00021 #define alu2d_inline inline 
00022 #else
00023 #define alu2d_inline
00024 #endif
00025 
00026 
00027 // fix for ALUGrid 1.22, where ALUGRID_VERTEX_PROJECTION is defined only in alugrid_serial.h
00028 #ifndef ALUGRID_VERTEX_PROJECTION
00029 #include <alugrid_serial.h>
00030 #endif
00031 
00032 #define ALU2DSPACE ALU2DSPACENAME ::
00033 
00034 #ifdef ALUGRID_SURFACE_2D
00035 #define ALU2DSPACENAME ALU2DGrid
00036 #define ALU2DDIMWORLD(dimw,eltype) < dimw,(eltype == ALU2DSPACE triangle ? 3 : 4) >
00037 #else
00038 #define ALU2DSPACENAME ALUGridSpace
00039 #define ALU2DDIMWORLD(dimw,eltype)
00040 #endif
00041 
00042 // use the ALU3dGrid Parallel detection
00043 #include <dune/grid/alugrid/common/checkparallel.hh>
00044 //#define ALU2DGRID_PARALLEL ALU3DGRID_PARALLEL
00045 #define ALU2DGRID_PARALLEL 0
00046 
00047 #include <dune/common/collectivecommunication.hh>
00048 
00049 #if ALU2DGRID_PARALLEL
00050 //#include "communicator.hh"
00051 #warning "Using ALU2dGrid in parallel"
00052 #endif
00053 
00054 
00055 namespace ALU2DSPACENAME
00056 {
00057 
00058   enum ElementType { triangle, quadrilateral, mixed };
00059 
00060 }
00061 
00062 
00063 namespace Dune
00064 {
00065   
00066   typedef double alu2d_ctype;
00067 
00068 
00069 
00070   // ALU2dImplInterface
00071   // ------------------
00072 
00073   template< int dim, int dimw, ALU2DSPACE ElementType eltype >
00074   struct ALU2dImplInterface;
00075 
00076   template< int dimw, ALU2DSPACE ElementType eltype >
00077   struct ALU2dImplInterface< 0, dimw, eltype >
00078   {
00079 #ifdef ALUGRID_SURFACE_2D
00080     typedef typename ALU2DSPACE Hmesh_basic ALU2DDIMWORLD(dimw,eltype) ::vertex_t Type;
00081 #else
00082     typedef ALU2DSPACE Vertex Type;
00083 #endif
00084   };
00085 
00086   template< int dimw, ALU2DSPACE ElementType eltype >
00087   struct ALU2dImplInterface< 1, dimw, eltype >
00088   {
00089     typedef typename ALU2DSPACE Hmesh_basic ALU2DDIMWORLD(dimw,eltype) ::helement_t Type;
00090   };
00091 
00092   template< int dimw, ALU2DSPACE ElementType eltype >
00093   struct ALU2dImplInterface< 2, dimw, eltype >
00094   {
00095     typedef typename ALU2DSPACE Hmesh_basic ALU2DDIMWORLD(dimw,eltype) ::helement_t Type;
00096   };
00097 
00098 
00099 
00100   // ALU2dImplTraits
00101   // ---------------
00102 
00103   template< int dimw, ALU2DSPACE ElementType eltype >
00104   struct ALU2dImplTraits
00105   {
00106     template< int cdim >
00107     struct Codim
00108     {
00109       typedef typename ALU2dImplInterface< 2-cdim, dimw, eltype >::Type InterfaceType;
00110     };
00111 
00112     typedef ALU2DSPACE Hmesh ALU2DDIMWORLD(dimw,eltype) HmeshType;
00113     typedef ALU2DSPACE Thinelement ALU2DDIMWORLD(dimw,eltype) ThinelementType;
00114     typedef ALU2DSPACE Element ALU2DDIMWORLD(dimw,eltype) ElementType;
00115     typedef typename HmeshType::helement_t HElementType;
00116     typedef typename HmeshType::hbndel_t HBndElType;
00117     typedef ALU2DSPACE Bndel_periodic ALU2DDIMWORLD(dimw,eltype) PeriodicBndElType;
00118   };
00119 
00120 
00121 
00122   // ALU2dGridMarkerVector
00123   // ---------------------
00124 
00125   class ALU2dGridMarkerVector 
00126   {
00127     typedef std::vector< int > VectorType;
00128   public:
00129     ALU2dGridMarkerVector() : up2Date_(false) {}
00130    
00131     bool up2Date() const { return up2Date_; }
00132 
00133     void unsetUp2Date() { up2Date_ = false; }
00134     
00135     bool isOnElement(int elementIndex, int idx, int codim) const 
00136     {
00137       return marker_[codim-1][idx] == elementIndex; 
00138     }
00139     
00140     template <class GridType> 
00141     void update (const GridType & grid, int level ) 
00142     {
00143       enum { dim = GridType::dimension };
00144       static const int dimworld = GridType::dimensionworld;
00145       static const ALU2DSPACE ElementType eltype = GridType::elementType;
00146 
00147       typedef typename ALU2dImplTraits< dimworld, eltype >::template Codim<0>::InterfaceType ElementType;
00148       typedef typename ALU2dImplTraits< dimworld, eltype >::template Codim<2>::InterfaceType VertexType;
00149       typedef ALU2DSPACE Listwalkptr< ElementType > IteratorType;
00150 
00151       // resize 
00152       for(int i=0; i<2; ++i) 
00153       {
00154         int s = grid.hierSetSize(i+1);
00155         if((int) marker_[i].size() < s ) marker_[i].resize(s);
00156 
00157         size_t markerSize = marker_[i].size();
00158         // reset marker vector to default value 
00159         for(size_t k=0; k<markerSize; ++k) marker_[i][k] = -1;
00160       }
00161 
00162       IteratorType iter(grid.myGrid(), level);
00163 
00164       for(iter->first(); !iter->done(); iter->next())
00165       {
00166         ElementType & elem = iter->getitem();
00167         int elIdx = elem.getIndex();
00168 
00169         // if element is not valid, go to next 
00170 #if ALU2DGRID_PARALLEL 
00171         if( ! grid.rankManager().isValid( elIdx , All_Partition ) ) continue;
00172 #endif
00173         for(int i=0; i<elem.numvertices(); ++i) 
00174         {
00175           enum { vxCodim = 1 };
00176           int vxIdx = elem.getVertex(i)->getIndex();
00177           if( marker_[vxCodim][vxIdx] < 0) marker_[vxCodim][vxIdx] = elIdx;
00178           
00179           enum { edgeCodim = 0 };
00180           int edgeIdx = elem.edge_idx(i);
00181           if( marker_[edgeCodim][edgeIdx] < 0) marker_[edgeCodim][edgeIdx] = elIdx;
00182         }
00183       }
00184       up2Date_ = true;
00185     }
00186 
00187   private:
00188     VectorType marker_[2];
00189 
00190     bool up2Date_;
00191   };
00192 
00193   class ALU2dGridLeafMarkerVector 
00194   {
00195     typedef std::vector< int > VectorType;
00196   public:
00197     ALU2dGridLeafMarkerVector() : up2Date_(false) {}
00198    
00199     bool up2Date() const { return up2Date_; }
00200 
00201     void unsetUp2Date() { up2Date_ = false; }
00202     
00203     // return true, if edge is visited on given element 
00204     bool isOnElement(int elementIndex, int idx, int codim) const 
00205     {
00206       assert( up2Date_ );
00207       // this marker only works for codim 1, i.e. edges  
00208       assert( codim == 1 );
00209       return marker_[idx] == elementIndex; 
00210     }
00211     
00212     // this is for the LeafIterator
00213     template <class GridType> 
00214     void update (const GridType & grid) 
00215     {
00216       static const int dimworld = GridType::dimensionworld;
00217       static const ALU2DSPACE ElementType eltype = GridType::elementType;
00218 
00219       typedef typename ALU2dImplTraits< dimworld, eltype >::template Codim<0>::InterfaceType ElementType;
00220       typedef typename ALU2dImplTraits< dimworld, eltype >::template Codim<2>::InterfaceType VertexType;
00221       typedef ALU2DSPACE Listwalkptr< ElementType > IteratorType;
00222 
00223       // resize edge marker 
00224       {
00225         int s = grid.hierSetSize(1);
00226         if((int) marker_.size() < s ) marker_.resize(s);
00227 
00228         size_t markerSize = marker_.size();
00229         // reset marker vector to default value 
00230         for(size_t k=0; k<markerSize; ++k) marker_[k] = -1;
00231       }
00232 
00233       // resize vertex levels 
00234       {
00235         int s = grid.hierSetSize(2);
00236         if((int) vertexLevels_.size() < s ) vertexLevels_.resize(s);
00237 
00238         // initialize with -1 
00239         size_t vxSize = vertexLevels_.size();
00240         for(size_t k=0; k<vxSize; ++k) vertexLevels_[k] = -1;
00241       }
00242 
00243       enum { dim = GridType::dimension };
00244       IteratorType iter(grid.myGrid());
00245 
00246       for(iter->first(); !iter->done(); iter->next())
00247       {
00248         ElementType & elem = iter->getitem();
00249         int elIdx = elem.getIndex();
00250 
00251 #if ALU2DGRID_PARALLEL 
00252         // is element is not valid, go to next 
00253         if( ! grid.rankManager().isValid( elIdx , All_Partition ) ) continue;
00254 #endif
00255         int level = elem.level();
00256 
00257         for(int i=0; i<elem.numvertices(); ++i) 
00258         {
00259           int vxIdx = elem.getVertex(i)->getIndex();
00260 
00261           // set max level to vertices, see Grid docu paper
00262           if(level > vertexLevels_[vxIdx]) vertexLevels_[vxIdx] = level;    
00263           
00264           int edgeIdx = elem.edge_idx(i);
00265           if( marker_[edgeIdx] < 0) marker_[edgeIdx] = elIdx;
00266         }
00267       }     
00268       up2Date_ = true;
00269     }
00270 
00272     int levelOfVertex(const int vxIdx) const 
00273     {
00274       assert( up2Date_ );
00275       assert( vxIdx >= 0 && vxIdx < (int) vertexLevels_.size());
00276       // if this assertion is thrown, the level has not been initialized 
00277       assert( vertexLevels_[vxIdx] >= 0 );
00278       return vertexLevels_[vxIdx];
00279     }
00280 
00282     bool isValidVertex(const int vxIdx) const 
00283     {
00284       assert( up2Date_ );
00285       assert( vxIdx >= 0 && vxIdx < (int) vertexLevels_.size());
00286       return (vertexLevels_[vxIdx] >= 0);
00287     }
00288 
00289   private:
00290     VectorType marker_;
00291     VectorType vertexLevels_;
00292 
00293     bool up2Date_;
00294   };
00295 
00296   // dummy object stream class 
00297   class ALU2dGridObjectStream
00298   {
00299     public:
00300       class EOFException {} ;
00301       template <class T>
00302       void readObject (T &) {}
00303       void readObject (int) {}
00304       void readObject (double) {}
00305       template <class T>
00306       void writeObject (T &) {}
00307       void writeObject (int) {}
00308       void writeObject (double) {}
00309      
00310       template <class T>
00311       void read (T &) const {}
00312       template <class T> 
00313       void write (const T &) {}
00314   };  
00315       
00316 } //end namespace Dune
00317 #endif