dofvector.hh

00001 #ifndef DUNE_ALBERTA_DOFVECTOR_HH
00002 #define DUNE_ALBERTA_DOFVECTOR_HH
00003 
00004 #include <cstdlib>
00005 #include <limits>
00006 
00007 #include <dune/grid/albertagrid/misc.hh>
00008 #include <dune/grid/albertagrid/elementinfo.hh>
00009 #include <dune/grid/albertagrid/refinement.hh>
00010 
00011 #if HAVE_ALBERTA
00012 
00013 namespace Dune
00014 {
00015 
00016   namespace Alberta
00017   {
00018 
00019     // External Forward Declarations
00020     // -----------------------------
00021 
00022     template< int dim >
00023     class MeshPointer;
00024 
00025 
00026 
00027     // DofVectorProvider
00028     // -----------------
00029 
00030     template< class Dof >
00031     struct DofVectorProvider;
00032 
00033     template<>
00034     struct DofVectorProvider< int >
00035     {
00036       typedef ALBERTA DOF_INT_VEC DofVector;
00037 
00038       static DofVector *get ( const DofSpace *dofSpace, const std::string &name )
00039       {
00040         return ALBERTA get_dof_int_vec( name.c_str(), dofSpace );
00041       }
00042 
00043       static void free ( DofVector *dofVector )
00044       {
00045         ALBERTA free_dof_int_vec( dofVector );
00046       }
00047 
00048       static DofVector *read ( const std::string &filename, Mesh *mesh, DofSpace *dofSpace )
00049       {
00050         return ALBERTA read_dof_int_vec_xdr( filename.c_str(), mesh, dofSpace );
00051       }
00052 
00053       static bool write ( const DofVector *dofVector, const std::string &filename )
00054       {
00055         int success = ALBERTA write_dof_int_vec_xdr( dofVector, filename.c_str() );
00056         return (success == 0);
00057       }
00058     };
00059 
00060     template<>
00061     struct DofVectorProvider< signed char >
00062     {
00063       typedef ALBERTA DOF_SCHAR_VEC DofVector;
00064 
00065       static DofVector *get ( const DofSpace *dofSpace, const std::string &name )
00066       {
00067         return ALBERTA get_dof_schar_vec( name.c_str(), dofSpace );
00068       }
00069 
00070       static void free ( DofVector *dofVector )
00071       {
00072         ALBERTA free_dof_schar_vec( dofVector );
00073       }
00074 
00075       static DofVector *read ( const std::string &filename, Mesh *mesh, DofSpace *dofSpace )
00076       {
00077         return ALBERTA read_dof_schar_vec_xdr( filename.c_str(), mesh, dofSpace );
00078       }
00079 
00080       static bool write ( const DofVector *dofVector, const std::string &filename )
00081       {
00082         int success = ALBERTA write_dof_schar_vec_xdr( dofVector, filename.c_str() );
00083         return (success == 0);
00084       }
00085     };
00086 
00087     template<>
00088     struct DofVectorProvider< unsigned char >
00089     {
00090       typedef ALBERTA DOF_UCHAR_VEC DofVector;
00091 
00092       static DofVector *get ( const DofSpace *dofSpace, const std::string &name )
00093       {
00094         return ALBERTA get_dof_uchar_vec( name.c_str(), dofSpace );
00095       }
00096 
00097       static void free ( DofVector *dofVector )
00098       {
00099         ALBERTA free_dof_uchar_vec( dofVector );
00100       }
00101 
00102       static DofVector *read ( const std::string &filename, Mesh *mesh, DofSpace *dofSpace )
00103       {
00104         return ALBERTA read_dof_uchar_vec_xdr( filename.c_str(), mesh, dofSpace );
00105       }
00106 
00107       static bool write ( const DofVector *dofVector, const std::string &filename )
00108       {
00109         int success = ALBERTA write_dof_uchar_vec_xdr( dofVector, filename.c_str() );
00110         return (success == 0);
00111       }
00112     };
00113 
00114     template<>
00115     struct DofVectorProvider< Real >
00116     {
00117       typedef ALBERTA DOF_REAL_VEC DofVector;
00118 
00119       static DofVector *get ( const DofSpace *dofSpace, const std::string &name )
00120       {
00121         return ALBERTA get_dof_real_vec( name.c_str(), dofSpace );
00122       }
00123 
00124       static void free ( DofVector *dofVector )
00125       {
00126         ALBERTA free_dof_real_vec( dofVector );
00127       }
00128 
00129       static DofVector *read ( const std::string &filename, Mesh *mesh, DofSpace *dofSpace )
00130       {
00131         return ALBERTA read_dof_real_vec_xdr( filename.c_str(), mesh, dofSpace );
00132       }
00133 
00134       static bool write ( const DofVector *dofVector, const std::string &filename )
00135       {
00136         int success = ALBERTA write_dof_real_vec_xdr( dofVector, filename.c_str() );
00137         return (success == 0);
00138       }
00139     };
00140 
00141     template<>
00142     struct DofVectorProvider< GlobalVector >
00143     {
00144       typedef ALBERTA DOF_REAL_D_VEC DofVector;
00145 
00146       static DofVector *get ( const DofSpace *dofSpace, const std::string &name )
00147       {
00148         return ALBERTA get_dof_real_d_vec( name.c_str(), dofSpace );
00149       }
00150 
00151       static void free ( DofVector *dofVector )
00152       {
00153         ALBERTA free_dof_real_d_vec( dofVector );
00154       }
00155 
00156       static DofVector *read ( const std::string &filename, Mesh *mesh, DofSpace *dofSpace )
00157       {
00158         return ALBERTA read_dof_real_d_vec_xdr( filename.c_str(), mesh, dofSpace );
00159       }
00160 
00161       static bool write ( const DofVector *dofVector, const std::string &filename )
00162       {
00163         int success = ALBERTA write_dof_real_d_vec_xdr( dofVector, filename.c_str() );
00164         return (success == 0);
00165       }
00166     };
00167 
00168 
00169 
00170     // DofVectorPointer
00171     // ----------------
00172 
00173     template< class Dof >
00174     class DofVectorPointer
00175     {
00176       typedef DofVectorPointer< Dof > This;
00177 
00178       typedef Alberta::DofVectorProvider< Dof > DofVectorProvider;
00179 
00180     public:
00181       typedef typename DofVectorProvider::DofVector DofVector;
00182 
00183       static const bool supportsAdaptationData = (DUNE_ALBERTA_VERSION >= 0x201);
00184 
00185     private:
00186       DofVector *dofVector_;
00187 
00188     public:
00189       DofVectorPointer ()
00190       : dofVector_( NULL )
00191       {}
00192 
00193       explicit DofVectorPointer ( const DofSpace *dofSpace,
00194                                   const std::string &name = "" )
00195       : dofVector_ ( DofVectorProvider::get( dofSpace, name ) )
00196       {}
00197 
00198       explicit DofVectorPointer ( DofVector *dofVector )
00199       : dofVector_( dofVector )
00200       {}
00201 
00202       operator DofVector * () const
00203       {
00204         return dofVector_;
00205       }
00206 
00207       operator Dof * () const
00208       {
00209         Dof *ptr = NULL;
00210         GET_DOF_VEC( ptr, dofVector_ );
00211         return ptr;
00212       }
00213 
00214       bool operator! () const
00215       {
00216         return (dofVector_ == NULL);
00217       }
00218 
00219       const DofSpace *dofSpace () const
00220       {
00221         return dofVector_->fe_space;
00222       }
00223       
00224       std::string name () const
00225       {
00226         if( dofVector_ != NULL )
00227           return dofVector_->name;
00228         else
00229           return std::string();
00230       }
00231 
00232       void create ( const DofSpace *dofSpace, const std::string &name = "" )
00233       {
00234         release();
00235         dofVector_ = DofVectorProvider::get( dofSpace, name );
00236       }
00237 
00238       template< int dim >
00239       void read ( const std::string &filename, const MeshPointer< dim > &meshPointer )
00240       {
00241         release();
00242         dofVector_ = DofVectorProvider::read( filename, meshPointer, NULL );
00243       }
00244 
00245       bool write ( const std::string &filename ) const
00246       {
00247         return DofVectorProvider::write( dofVector_, filename );
00248       }
00249 
00250       void release ()
00251       {
00252         if( dofVector_ != NULL )
00253         {
00254           DofVectorProvider::free( dofVector_ );
00255           dofVector_ = NULL;
00256         }
00257       }
00258 
00259       template< class Functor >
00260       void forEach ( Functor &functor ) const
00261       {
00262         Dof *array = (Dof *)(*this);
00263         FOR_ALL_DOFS( dofSpace()->admin, functor( array[ dof ] ) );
00264       }
00265 
00266       void initialize ( const Dof &value )
00267       {
00268         Dof *array = (Dof *)(*this);
00269         FOR_ALL_DOFS( dofSpace()->admin, array[ dof ] = value );
00270       }
00271 
00272       template< class AdaptationData >
00273       AdaptationData *getAdaptationData () const
00274       {
00275         assert( dofVector_ != NULL );
00276 #if DUNE_ALBERTA_VERSION >= 0x201
00277         assert( dofVector_->user_data != NULL );
00278         return static_cast< AdaptationData * >( dofVector_->user_data );
00279 #else
00280         return 0;
00281 #endif
00282       }
00283 
00284       template< class AdaptationData >
00285       void setAdaptationData ( AdaptationData *adaptationData )
00286       {
00287         assert( dofVector_ != NULL );
00288 #if DUNE_ALBERTA_VERSION >= 0x201
00289         dofVector_->user_data = adaptationData;
00290 #endif // #if DUNE_ALBERTA_VERSION >= 0x201
00291       }
00292 
00293       template< class Interpolation >
00294       void setupInterpolation ()
00295       {
00296         assert( dofVector_ != NULL );
00297         dofVector_->refine_interpol = &refineInterpolate< Interpolation >;
00298       }
00299 
00300       template< class Restriction >
00301       void setupRestriction ()
00302       {
00303         assert( dofVector_ != NULL );
00304         dofVector_->coarse_restrict = &coarsenRestrict< Restriction >;
00305       }
00306 
00307     private:
00308       template< class Interpolation >
00309       static void refineInterpolate ( DofVector *dofVector, RC_LIST_EL *list, int n )
00310       {
00311         const This dofVectorPointer( dofVector );
00312         const Patch< Interpolation::dimension > patch( list, n );
00313         Interpolation::interpolateVector( dofVectorPointer, patch );
00314       }
00315 
00316       template< class Restriction >
00317       static void coarsenRestrict ( DofVector *dofVector, RC_LIST_EL *list, int n )
00318       {
00319         const This dofVectorPointer( dofVector );
00320         const Patch< Restriction::dimension > patch( list, n );
00321         Restriction::restrictVector( dofVectorPointer, patch );
00322       }
00323     };
00324 
00325 
00326 
00327     // Auxilliary Functions
00328     // --------------------
00329 
00330     inline void abs ( const DofVectorPointer< int > &dofVector )
00331     {
00332       assert( !dofVector == false );
00333       int *array = (int *)dofVector;
00334       FOR_ALL_DOFS( dofVector.dofSpace()->admin,
00335                     array[ dof ] = std::abs( array[ dof ] ) );
00336     }
00337 
00338 
00339     inline int max ( const DofVectorPointer< int > &dofVector )
00340     {
00341       assert( !dofVector == false );
00342       int *array = (int *)dofVector;
00343       int result = std::numeric_limits< int >::min();
00344       FOR_ALL_DOFS( dofVector.dofSpace()->admin,
00345                     result = std::max( result, array[ dof ] ) );
00346       return result;
00347     }
00348 
00349 
00350     inline int min ( const DofVectorPointer< int > &dofVector )
00351     {
00352       assert( !dofVector == false );
00353       int *array = (int *)dofVector;
00354       int result = std::numeric_limits< int >::max();
00355       FOR_ALL_DOFS( dofVector.dofSpace()->admin,
00356                     result = std::min( result, array[ dof ] ) );
00357       return result;
00358     }
00359 
00360   }
00361 
00362 }
00363 
00364 #endif // #if HAVE_ALBERTA
00365 
00366 #endif

Generated on Sun Nov 15 22:28:39 2009 for dune-grid by  doxygen 1.5.6