dune-localfunctions  2.1.1
interpolationhelper.hh
Go to the documentation of this file.
00001 #ifndef GENERIC_INTERPOLATIONHELPER_HH
00002 #define GENERIC_INTERPOLATIONHELPER_HH
00003 
00004 #include <vector>
00005 
00006 #include <dune/common/fvector.hh>
00007 #include <dune/localfunctions/utility/field.hh>
00008 
00009 namespace Dune 
00010 {
00011   // A small helper class to avoid having to 
00012   // write the interpolation twice (once for function
00013   // and once for a basis)
00014   template< class F, unsigned int dimension >
00015   struct InterpolationHelper
00016   {
00017     template <class Func,class Container, bool type>
00018     struct Helper;
00019   };
00020   template <class F,unsigned int d>
00021   template <class Func,class Vector>
00022   struct InterpolationHelper<F,d>::Helper<Func,Vector,true>
00023   // Func is of Function type
00024   {
00025     typedef std::vector< Dune::FieldVector<F,d> > Result;
00026     Helper(const Func& func, Vector &vec) 
00027     : func_(func),
00028       vec_(vec),
00029       tmp_(1)
00030     {}
00031     const typename Vector::value_type &operator()(unsigned int row,unsigned int col)
00032     {
00033       return vec_[row];
00034     }
00035     template <class Fy>
00036     void set(unsigned int row,unsigned int col,
00037              const Fy &val)
00038     {
00039       assert(col==0);
00040       assert(row<vec_.size());
00041       field_cast( val, vec_[row] );
00042     }
00043     template <class Fy>
00044     void add(unsigned int row,unsigned int col,
00045              const Fy &val)
00046     {
00047       assert(col==0);
00048       assert(row<vec_.size());
00049       vec_[row] += field_cast<typename Vector::value_type>(val);
00050     }
00051     template <class DomainVector>
00052     const Result &evaluate(const DomainVector &x) const
00053     {
00054       typename Func::DomainType xx ;
00055       typename Func::RangeType ff ;
00056       field_cast(x,xx);
00057       func_.evaluate(xx,ff);
00058       field_cast(ff, tmp_[0] );
00059       return tmp_;
00060     }
00061     unsigned int size() const
00062     {
00063       return 1;
00064     }
00065     const Func &func_;
00066     Vector &vec_;
00067     mutable Result tmp_;
00068   };
00069   template <class F,unsigned int d>
00070   template <class Basis,class Matrix>
00071   struct InterpolationHelper<F,d>::Helper<Basis,Matrix,false>
00072   // Func is of Basis type
00073   {
00074     typedef std::vector< Dune::FieldVector<F,d> > Result;
00075     Helper(const Basis& basis, Matrix &matrix) 
00076     : basis_(basis), 
00077       matrix_(matrix),
00078       tmp_(basis.size()) {
00079     }
00080     const F &operator()(unsigned int row,unsigned int col) const
00081     {
00082       return matrix_(row,col);
00083     }
00084     F &operator()(unsigned int row,unsigned int col)
00085     {
00086       return matrix_(row,col);
00087     }
00088     template <class Fy>
00089     void set(unsigned int row,unsigned int col,
00090              const Fy &val)
00091     {
00092       assert(col<matrix_.cols());
00093       assert(row<matrix_.rows());
00094       field_cast(val,matrix_(row,col));
00095     }
00096     template <class Fy>
00097     void add(unsigned int row,unsigned int col,
00098              const Fy &val)
00099     {
00100       assert(col<matrix_.cols());
00101       assert(row<matrix_.rows());
00102       matrix_(row,col) += val;
00103     }
00104     template <class DomainVector>
00105     const Result &evaluate(const DomainVector &x) const
00106     {
00107       basis_.template evaluate<0>(x,tmp_);
00108       return tmp_;
00109     }
00110     unsigned int size() const
00111     {
00112       return basis_.size();
00113     }
00114 
00115     const Basis &basis_;
00116     Matrix &matrix_;
00117     mutable Result tmp_;
00118   };
00119 }
00120 #endif // GENERIC_INTERPOLATIONHELPER_HH
00121