dune-pdelab  2.6.0-rc1
pk1d.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 
4 // Pk in one dimension with k as runtime variable
5 
6 #ifndef DUNE_PDELAB_FINITEELEMENT_PK1D_HH
7 #define DUNE_PDELAB_FINITEELEMENT_PK1D_HH
8 
9 #include <numeric>
10 #include <vector>
11 
12 #include <dune/common/fmatrix.hh>
13 #include <dune/geometry/type.hh>
14 
15 #include<dune/localfunctions/common/localbasis.hh>
16 #include<dune/localfunctions/common/localkey.hh>
17 #include<dune/localfunctions/common/localfiniteelementtraits.hh>
18 
19 namespace Dune {
20 
29  template<class D, class R>
31  {
33  class Pk1dLocalBasis
34  {
35  Dune::GeometryType gt; // store geometry type for the basis
36  std::size_t k; // polynomial degree
37  std::size_t n; // the number of basis functions
38  std::vector<R> s; // Lagrange points on the reference interval
39  public:
40  typedef Dune::LocalBasisTraits<D,1,Dune::FieldVector<D,1>,R,1,Dune::FieldVector<R,1>,Dune::FieldMatrix<R,1,1>, 1> Traits;
41 
43  Pk1dLocalBasis (std::size_t k_) : gt(Dune::GeometryType::cube,1), k(k_), n(k_+1), s(n)
44  {
45  for (std::size_t i=0; i<=k; i++) s[i] = (1.0*i)/k;
46  }
47 
49  std::size_t size () const { return n; }
50 
52  inline void evaluateFunction (const typename Traits::DomainType& in,
53  std::vector<typename Traits::RangeType>& out) const {
54  out.resize(n);
55  for (std::size_t i=0; i<=k; i++)
56  {
57  out[i] = 1.0;
58  for (std::size_t j=0; j<=k; j++)
59  if (i!=j) out[i] *= (in[0]-s[j])/(s[i]-s[j]);
60  }
61  }
62 
64  inline void
65  evaluateJacobian (const typename Traits::DomainType& in,
66  std::vector<typename Traits::JacobianType>& out) const {
67  out.resize(n);
68  for (std::size_t i=0; i<=k; i++) // derivative of basis function i
69  {
70  out[i][0][0] = 0.0;
71  R factor = 1.0;
72  R denominator = 1.0;
73  for (std::size_t j=0; j<=k; j++)
74  {
75  if (j==i) continue; // treat factor (x-s_j)
76  denominator *= s[i]-s[j];
77  R a=1.0; // product of remaining factors (might be empty)
78  for (std::size_t l=j+1; l<=k; l++)
79  {
80  if (l==i) continue;
81  a *= in[0]-s[l];
82  }
83  out[i][0][0] += factor*a;
84  factor *= in[0]-s[j];
85  }
86  out[i][0][0] /= denominator;
87  }
88  }
89 
91  void partial(const std::array<unsigned int, Traits::dimDomain>& DUNE_UNUSED(order),
92  const typename Traits::DomainType& DUNE_UNUSED(in),
93  std::vector<typename Traits::RangeType>& DUNE_UNUSED(out)) const {
94  auto totalOrder = std::accumulate(order.begin(), order.end(), 0);
95  if (totalOrder == 0) {
96  evaluateFunction(in, out);
97  } else {
98  DUNE_THROW(NotImplemented, "Desired derivative order is not implemented");
99  }
100  }
101 
103  unsigned int order () const {
104  return k;
105  }
106 
108  Dune::GeometryType type () const { return gt; }
109  };
110 
112  class Pk1dLocalCoefficients
113  {
114  public:
115  Pk1dLocalCoefficients (std::size_t k_) : k(k_), n(k_+1), li(k_+1) {
116  li[0] = Dune::LocalKey(0,1,0);
117  for (int i=1; i<int(k); i++) li[i] = Dune::LocalKey(0,0,i-1);
118  li[k] = Dune::LocalKey(1,1,0);
119  }
120 
122  std::size_t size () const { return n; }
123 
125  const Dune::LocalKey& localKey (int i) const {
126  return li[i];
127  }
128 
129  private:
130  std::size_t k; // polynomial degree
131  std::size_t n; // the number of basis functions
132  std::vector<Dune::LocalKey> li; // assignment of basis function to subentities
133  };
134 
136  template<typename LB>
137  class Pk1dLocalInterpolation
138  {
139  public:
140  Pk1dLocalInterpolation (std::size_t k_) : k(k_), n(k_+1) {}
141 
143  template<typename F, typename C>
144  void interpolate (const F& f, std::vector<C>& out) const
145  {
146  out.resize(n);
147  typename LB::Traits::DomainType x;
148  typename LB::Traits::RangeType y;
149  for (int i=0; i<=int(k); i++)
150  {
151  x[0] = (1.0*i)/k; // the point to evaluate
152  f.evaluate(x,y);
153  out[i] = y[0];
154  }
155  }
156  private:
157  std::size_t k; // polynomial degree
158  std::size_t n; // the number of basis functions
159  };
160 
161  Dune::GeometryType gt;
162  Pk1dLocalBasis basis;
163  Pk1dLocalCoefficients coefficients;
164  Pk1dLocalInterpolation<Pk1dLocalBasis> interpolation;
165 
166  public:
167  typedef Dune::LocalFiniteElementTraits<Pk1dLocalBasis,
168  Pk1dLocalCoefficients,
169  Pk1dLocalInterpolation<Pk1dLocalBasis> > Traits;
170 
171  Pk1dLocalFiniteElement (std::size_t k)
172  : gt(Dune::GeometryType::cube,1), basis(k), coefficients(k), interpolation(k)
173  {}
174 
175  const typename Traits::LocalBasisType& localBasis () const
176  {
177  return basis;
178  }
179 
180  const typename Traits::LocalCoefficientsType& localCoefficients () const
181  {
182  return coefficients;
183  }
184 
185  const typename Traits::LocalInterpolationType& localInterpolation () const
186  {
187  return interpolation;
188  }
189 
192  std::size_t size() const
193  {
194  return basis.size();
195  }
196 
197  Dune::GeometryType type () const { return gt; }
198 
200  return new Pk1dLocalFiniteElement(*this);
201  }
202  };
203 }
204 
205 #endif // DUNE_PDELAB_FINITEELEMENT_PK1D_HH
Define the Pk Lagrange basis functions in 1d on the reference interval.
Definition: pk1d.hh:30
std::size_t size() const
Definition: pk1d.hh:192
const Traits::LocalCoefficientsType & localCoefficients() const
Definition: pk1d.hh:180
Pk1dLocalFiniteElement(std::size_t k)
Definition: pk1d.hh:171
Dune::LocalFiniteElementTraits< Pk1dLocalBasis, Pk1dLocalCoefficients, Pk1dLocalInterpolation< Pk1dLocalBasis > > Traits
Definition: pk1d.hh:169
For backward compatibility – Do not use this!
Definition: adaptivity.hh:28
const Traits::LocalInterpolationType & localInterpolation() const
Definition: pk1d.hh:185
Dune::GeometryType type() const
Definition: pk1d.hh:197
const std::string s
Definition: function.hh:830
Pk1dLocalFiniteElement * clone() const
Definition: pk1d.hh:199
const Traits::LocalBasisType & localBasis() const
Definition: pk1d.hh:175
void interpolate(const F &f, const GFS &gfs, XG &xg)
interpolation from a given grid function
Definition: interpolate.hh:204