dune-pdelab  2.6.0-rc1
qkdglobatto.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 #ifndef DUNE_PDELAB_FINITEELEMENT_QKDGLOBATTO_HH
5 #define DUNE_PDELAB_FINITEELEMENT_QKDGLOBATTO_HH
6 
7 #include <numeric>
8 
9 #include <dune/localfunctions/common/localbasis.hh>
10 #include <dune/localfunctions/common/localkey.hh>
11 #include <dune/localfunctions/common/localfiniteelementtraits.hh>
12 #include <dune/localfunctions/common/localtoglobaladaptors.hh>
13 
14 namespace Dune
15 {
16  namespace QkStuff
17  {
18 
20  template<class D, class R, int k>
22  {
23  R xi_gl[k+1];
24  R w_gl[k+1];
25 
26  public:
28  {
29  int matched_order=-1;
30  int matched_size=-1;
31  for (int order=1; order<=40; order++)
32  {
33  const Dune::QuadratureRule<D,1>& rule = Dune::QuadratureRules<D,1>::rule(Dune::GeometryType::cube,order,Dune::QuadratureType::GaussLobatto);
34  if (rule.size()==k+1)
35  {
36  matched_order = order;
37  matched_size = rule.size();
38  //std::cout << "GL: input order=" << order << " delivered=" << rule.order() << " size=" << rule.size()<< std::endl;
39  break;
40  }
41  }
42  if (matched_order<0) DUNE_THROW(Dune::Exception,"could not find Gauss Lobatto rule of appropriate size");
43  const Dune::QuadratureRule<D,1>& rule = Dune::QuadratureRules<D,1>::rule(Dune::GeometryType::cube,matched_order,Dune::QuadratureType::GaussLobatto);
44  size_t count=0;
45  for (typename Dune::QuadratureRule<D,1>::const_iterator it=rule.begin(); it!=rule.end(); ++it)
46  {
47  size_t group=count/2;
48  size_t member=count%2;
49  size_t newj;
50  if (member==1) newj=group; else newj=k-group;
51  xi_gl[newj] = it->position()[0];
52  w_gl[newj] = it->weight();
53  count++;
54  }
55  for (int j=0; j<matched_size/2; j++)
56  if (xi_gl[j]>0.5)
57  {
58  R temp=xi_gl[j];
59  xi_gl[j] = xi_gl[k-j];
60  xi_gl[k-j] = temp;
61  temp=w_gl[j];
62  w_gl[j] = w_gl[k-j];
63  w_gl[k-j] = temp;
64  }
65  // for (int i=0; i<k+1; i++)
66  // std::cout << "i=" << i
67  // << " xi=" << xi_gl[i]
68  // << " w=" << w_gl[i]
69  // << std::endl;
70  }
71 
72  // ith Lagrange polynomial of degree k in one dimension
73  R p (int i, D x) const
74  {
75  R result(1.0);
76  for (int j=0; j<=k; j++)
77  if (j!=i) result *= (x-xi_gl[j])/(xi_gl[i]-xi_gl[j]);
78  return result;
79  }
80 
81  // derivative of ith Lagrange polynomial of degree k in one dimension
82  R dp (int i, D x) const
83  {
84  R result(0.0);
85 
86  for (int j=0; j<=k; j++)
87  if (j!=i)
88  {
89  R prod( 1.0/(xi_gl[i]-xi_gl[j]) );
90  for (int l=0; l<=k; l++)
91  if (l!=i && l!=j) prod *= (x-xi_gl[l])/(xi_gl[i]-xi_gl[l]);
92  result += prod;
93  }
94  return result;
95  }
96 
97  // get ith Lagrange point
98  R x (int i) const
99  {
100  return xi_gl[i];
101  }
102 
103  // get weight of ith Lagrange point
104  R w (int i) const
105  {
106  return w_gl[i];
107  }
108  };
109 
122  template<class D, class R, int k, int d>
124  {
125  enum{ n = QkSize<k,d>::value };
127 
128  public:
129  typedef LocalBasisTraits<D,d,Dune::FieldVector<D,d>,R,1,Dune::FieldVector<R,1>,Dune::FieldMatrix<R,1,d> > Traits;
130 
132  unsigned int size () const
133  {
134  return QkSize<k,d>::value;
135  }
136 
138  inline void evaluateFunction (const typename Traits::DomainType& in,
139  std::vector<typename Traits::RangeType>& out) const
140  {
141  out.resize(size());
142  for (size_t i=0; i<size(); i++)
143  {
144  // convert index i to multiindex
145  Dune::FieldVector<int,d> alpha(multiindex<k,d>(i));
146 
147  // initialize product
148  out[i] = 1.0;
149 
150  // dimension by dimension
151  for (int j=0; j<d; j++)
152  out[i] *= poly.p(alpha[j],in[j]);
153  }
154  }
155 
157  inline void
158  evaluateJacobian (const typename Traits::DomainType& in, // position
159  std::vector<typename Traits::JacobianType>& out) const // return value
160  {
161  out.resize(size());
162 
163  // Loop over all shape functions
164  for (size_t i=0; i<size(); i++)
165  {
166  // convert index i to multiindex
167  Dune::FieldVector<int,d> alpha(multiindex<k,d>(i));
168 
169  // Loop over all coordinate directions
170  for (int j=0; j<d; j++)
171  {
172  // Initialize: the overall expression is a product
173  // if j-th bit of i is set to -1, else 1
174  out[i][0][j] = poly.dp(alpha[j],in[j]);
175 
176  // rest of the product
177  for (int l=0; l<d; l++)
178  if (l!=j)
179  out[i][0][j] *= poly.p(alpha[l],in[l]);
180  }
181  }
182  }
183 
185  void partial(const std::array<unsigned int, Traits::dimDomain>& DUNE_UNUSED(order),
186  const typename Traits::DomainType& DUNE_UNUSED(in),
187  std::vector<typename Traits::RangeType>& DUNE_UNUSED(out)) const {
188  auto totalOrder = std::accumulate(order.begin(), order.end(), 0);
189  if (totalOrder == 0) {
190  evaluateFunction(in, out);
191  } else {
192  DUNE_THROW(NotImplemented, "Desired derivative order is not implemented");
193  }
194  }
195 
197  unsigned int order () const
198  {
199  return k;
200  }
201  };
202 
204  template<int k, int d, class LB>
206  {
208 
209  public:
210 
212  template<typename F, typename C>
213  void interpolate (const F& f, std::vector<C>& out) const
214  {
215  typename LB::Traits::DomainType x;
216  typename LB::Traits::RangeType y;
217 
218  out.resize(QkSize<k,d>::value);
219 
220  for (int i=0; i<QkSize<k,d>::value; i++)
221  {
222  // convert index i to multiindex
223  Dune::FieldVector<int,d> alpha(multiindex<k,d>(i));
224 
225  // Generate coordinate of the i-th Lagrange point
226  for (int j=0; j<d; j++)
227  x[j] = poly.x(alpha[j]);
228 
229  f.evaluate(x,y); out[i] = y;
230  }
231  }
232  };
233 
235  template<int d, class LB>
237  {
238  public:
240  template<typename F, typename C>
241  void interpolate (const F& f, std::vector<C>& out) const
242  {
243  typename LB::Traits::DomainType x(0.5);
244  typename LB::Traits::RangeType y;
245  f.evaluate(x,y);
246  out.resize(1);
247  out[0] = y;
248  }
249  };
250 
251  }
252 
255  template<class D, class R, int k, int d>
257  {
261 
262  public:
263  // static number of basis functions
265 
268  typedef LocalFiniteElementTraits<LocalBasis,LocalCoefficients,LocalInterpolation> Traits;
269 
273  : gt(GeometryTypes::cube(d))
274  {}
275 
278  const typename Traits::LocalBasisType& localBasis () const
279  {
280  return basis;
281  }
282 
285  const typename Traits::LocalCoefficientsType& localCoefficients () const
286  {
287  return coefficients;
288  }
289 
292  const typename Traits::LocalInterpolationType& localInterpolation () const
293  {
294  return interpolation;
295  }
296 
299  std::size_t size() const
300  {
301  return basis.size();
302  }
303 
306  GeometryType type () const
307  {
308  return gt;
309  }
310 
312  {
313  return new QkDGGLLocalFiniteElement(*this);
314  }
315 
316  private:
317  LocalBasis basis;
318  LocalCoefficients coefficients;
319  LocalInterpolation interpolation;
320  GeometryType gt;
321  };
322 
324 
329  template<class Geometry, class RF, int k>
331  public ScalarLocalToGlobalFiniteElementAdaptorFactory<
332  QkDGGLLocalFiniteElement<
333  typename Geometry::ctype, RF, k, Geometry::mydimension
334  >,
335  Geometry
336  >
337  {
338  typedef QkDGGLLocalFiniteElement<
339  typename Geometry::ctype, RF, k, Geometry::mydimension
340  > LFE;
341  typedef ScalarLocalToGlobalFiniteElementAdaptorFactory<LFE, Geometry> Base;
342 
343  static const LFE lfe;
344 
345  public:
347  QkDGGLFiniteElementFactory() : Base(lfe) {}
348  };
349 
350  template<class Geometry, class RF, int k>
351  const typename QkDGGLFiniteElementFactory<Geometry, RF, k>::LFE
352  QkDGGLFiniteElementFactory<Geometry, RF, k>::lfe;
353 
354 }
355 
356 #endif // DUNE_PDELAB_FINITEELEMENT_QKDGLOBATTO_HH
Definition: qkdglobatto.hh:256
static const unsigned int value
Definition: gridfunctionspace/tags.hh:139
Lagrange shape functions of order k on the reference cube.
Definition: qkdglobatto.hh:123
const Traits::LocalCoefficientsType & localCoefficients() const
Definition: qkdglobatto.hh:285
LocalBasisTraits< D, d, Dune::FieldVector< D, d >, R, 1, Dune::FieldVector< R, 1 >, Dune::FieldMatrix< R, 1, d > > Traits
Definition: qkdglobatto.hh:129
QkDGGLFiniteElementFactory()
default constructor
Definition: qkdglobatto.hh:347
R x(int i) const
Definition: qkdglobatto.hh:98
LocalFiniteElementTraits< LocalBasis, LocalCoefficients, LocalInterpolation > Traits
Definition: qkdglobatto.hh:268
const Traits::LocalBasisType & localBasis() const
Definition: qkdglobatto.hh:278
Layout map for Q1 elements.
Definition: qkdglagrange.hh:229
For backward compatibility – Do not use this!
Definition: adaptivity.hh:28
void evaluateJacobian(const typename Traits::DomainType &in, std::vector< typename Traits::JacobianType > &out) const
Evaluate Jacobian of all shape functions.
Definition: qkdglobatto.hh:158
std::size_t size() const
Definition: qkdglobatto.hh:299
R dp(int i, D x) const
Definition: qkdglobatto.hh:82
void partial(const std::array< unsigned int, Traits::dimDomain > &DUNE_UNUSED(order), const typename Traits::DomainType &DUNE_UNUSED(in), std::vector< typename Traits::RangeType > &DUNE_UNUSED(out)) const
Evaluate partial derivative of all shape functions.
Definition: qkdglobatto.hh:185
unsigned int size() const
number of shape functions
Definition: qkdglobatto.hh:132
Definition: qkdglobatto.hh:264
GaussLobattoLagrangePolynomials()
Definition: qkdglobatto.hh:27
R p(int i, D x) const
Definition: qkdglobatto.hh:73
unsigned int order() const
Polynomial order of the shape functions.
Definition: qkdglobatto.hh:197
R w(int i) const
Definition: qkdglobatto.hh:104
Lagrange polynomials at Gauss-Lobatto points.
Definition: qkdglobatto.hh:21
Factory for global-valued QkDG elements.
Definition: qkdglobatto.hh:330
GeometryType type() const
Definition: qkdglobatto.hh:306
const Traits::LocalInterpolationType & localInterpolation() const
Definition: qkdglobatto.hh:292
void interpolate(const F &f, std::vector< C > &out) const
Local interpolation of a function.
Definition: qkdglobatto.hh:241
void evaluateFunction(const typename Traits::DomainType &in, std::vector< typename Traits::RangeType > &out) const
Evaluate all shape functions.
Definition: qkdglobatto.hh:138
Definition: qkdglagrange.hh:23
Definition: qkdglobatto.hh:205
QkDGGLLocalFiniteElement()
Definition: qkdglobatto.hh:272
QkDGGLLocalFiniteElement * clone() const
Definition: qkdglobatto.hh:311
void interpolate(const F &f, std::vector< C > &out) const
Local interpolation of a function.
Definition: qkdglobatto.hh:213