dune-localfunctions  2.2.1
interpolationhelper.hh
Go to the documentation of this file.
1 #ifndef GENERIC_INTERPOLATIONHELPER_HH
2 #define GENERIC_INTERPOLATIONHELPER_HH
3 
4 #include <vector>
5 
6 #include <dune/common/fvector.hh>
8 
9 namespace Dune
10 {
11  // A small helper class to avoid having to
12  // write the interpolation twice (once for function
13  // and once for a basis)
14  template< class F, unsigned int dimension >
16  {
17  template <class Func,class Container, bool type>
18  struct Helper;
19  };
20  template <class F,unsigned int d>
21  template <class Func,class Vector>
22  struct InterpolationHelper<F,d>::Helper<Func,Vector,true>
23  // Func is of Function type
24  {
25  typedef std::vector< Dune::FieldVector<F,d> > Result;
26  Helper(const Func& func, Vector &vec)
27  : func_(func),
28  vec_(vec),
29  tmp_(1)
30  {}
31  const typename Vector::value_type &operator()(unsigned int row,unsigned int col)
32  {
33  return vec_[row];
34  }
35  template <class Fy>
36  void set(unsigned int row,unsigned int col,
37  const Fy &val)
38  {
39  assert(col==0);
40  assert(row<vec_.size());
41  field_cast( val, vec_[row] );
42  }
43  template <class Fy>
44  void add(unsigned int row,unsigned int col,
45  const Fy &val)
46  {
47  assert(col==0);
48  assert(row<vec_.size());
49  vec_[row] += field_cast<typename Vector::value_type>(val);
50  }
51  template <class DomainVector>
52  const Result &evaluate(const DomainVector &x) const
53  {
54  typename Func::DomainType xx ;
55  typename Func::RangeType ff ;
56  field_cast(x,xx);
57  func_.evaluate(xx,ff);
58  field_cast(ff, tmp_[0] );
59  return tmp_;
60  }
61  unsigned int size() const
62  {
63  return 1;
64  }
65  const Func &func_;
66  Vector &vec_;
67  mutable Result tmp_;
68  };
69  template <class F,unsigned int d>
70  template <class Basis,class Matrix>
71  struct InterpolationHelper<F,d>::Helper<Basis,Matrix,false>
72  // Func is of Basis type
73  {
74  typedef std::vector< Dune::FieldVector<F,d> > Result;
75  Helper(const Basis& basis, Matrix &matrix)
76  : basis_(basis),
77  matrix_(matrix),
78  tmp_(basis.size()) {
79  }
80  const F &operator()(unsigned int row,unsigned int col) const
81  {
82  return matrix_(row,col);
83  }
84  F &operator()(unsigned int row,unsigned int col)
85  {
86  return matrix_(row,col);
87  }
88  template <class Fy>
89  void set(unsigned int row,unsigned int col,
90  const Fy &val)
91  {
92  assert(col<matrix_.cols());
93  assert(row<matrix_.rows());
94  field_cast(val,matrix_(row,col));
95  }
96  template <class Fy>
97  void add(unsigned int row,unsigned int col,
98  const Fy &val)
99  {
100  assert(col<matrix_.cols());
101  assert(row<matrix_.rows());
102  matrix_(row,col) += val;
103  }
104  template <class DomainVector>
105  const Result &evaluate(const DomainVector &x) const
106  {
107  basis_.template evaluate<0>(x,tmp_);
108  return tmp_;
109  }
110  unsigned int size() const
111  {
112  return basis_.size();
113  }
114 
115  const Basis &basis_;
116  Matrix &matrix_;
117  mutable Result tmp_;
118  };
119 }
120 #endif // GENERIC_INTERPOLATIONHELPER_HH
121