Reference documentation for deal.II version 9.0.0
polynomial_space.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2002 - 2017 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_polynomial_space_h
17 #define dealii_polynomial_space_h
18 
19 
20 #include <deal.II/base/config.h>
21 #include <deal.II/base/exceptions.h>
22 #include <deal.II/base/tensor.h>
23 #include <deal.II/base/point.h>
24 #include <deal.II/base/polynomial.h>
25 #include <deal.II/base/smartpointer.h>
26 
27 #include <vector>
28 
29 DEAL_II_NAMESPACE_OPEN
30 
96 template <int dim>
98 {
99 public:
104  static const unsigned int dimension = dim;
105 
113  template <class Pol>
114  PolynomialSpace (const std::vector<Pol> &pols);
115 
119  template <class StreamType>
120  void output_indices(StreamType &out) const;
121 
126  void set_numbering(const std::vector<unsigned int> &renumber);
127 
141  void compute (const Point<dim> &unit_point,
142  std::vector<double> &values,
143  std::vector<Tensor<1,dim> > &grads,
144  std::vector<Tensor<2,dim> > &grad_grads,
145  std::vector<Tensor<3,dim> > &third_derivatives,
146  std::vector<Tensor<4,dim> > &fourth_derivatives) const;
147 
154  double compute_value (const unsigned int i,
155  const Point<dim> &p) const;
156 
165  template <int order>
166  Tensor<order,dim> compute_derivative (const unsigned int i,
167  const Point<dim> &p) const;
168 
175  Tensor<1,dim> compute_grad (const unsigned int i,
176  const Point<dim> &p) const;
177 
184  Tensor<2,dim> compute_grad_grad (const unsigned int i,
185  const Point<dim> &p) const;
186 
193  unsigned int n () const;
194 
201  unsigned int degree () const;
202 
210  static unsigned int compute_n_pols (const unsigned int n);
211 
212 protected:
213 
220  void compute_index (const unsigned int n,
221  unsigned int (&index)[dim>0?dim:1]) const;
222 
223 private:
227  const std::vector<Polynomials::Polynomial<double> > polynomials;
228 
232  const unsigned int n_pols;
233 
237  std::vector<unsigned int> index_map;
238 
242  std::vector<unsigned int> index_map_inverse;
243 };
244 
245 
246 /* -------------- declaration of explicit specializations --- */
247 
248 template <>
249 void PolynomialSpace<1>::compute_index(const unsigned int n,
250  unsigned int (&index)[1]) const;
251 template <>
252 void PolynomialSpace<2>::compute_index(const unsigned int n,
253  unsigned int (&index)[2]) const;
254 template <>
255 void PolynomialSpace<3>::compute_index(const unsigned int n,
256  unsigned int (&index)[3]) const;
257 
258 
259 
260 /* -------------- inline and template functions ------------- */
261 
262 template <int dim>
263 template <class Pol>
264 PolynomialSpace<dim>::PolynomialSpace (const std::vector<Pol> &pols)
265  :
266  polynomials (pols.begin(), pols.end()),
267  n_pols (compute_n_pols(polynomials.size())),
268  index_map(n_pols),
269  index_map_inverse(n_pols)
270 {
271  // per default set this index map
272  // to identity. This map can be
273  // changed by the user through the
274  // set_numbering function
275  for (unsigned int i=0; i<n_pols; ++i)
276  {
277  index_map[i]=i;
278  index_map_inverse[i]=i;
279  }
280 }
281 
282 
283 template <int dim>
284 inline
285 unsigned int
287 {
288  return n_pols;
289 }
290 
291 
292 
293 template <int dim>
294 inline
295 unsigned int
297 {
298  return polynomials.size();
299 }
300 
301 
302 template <int dim>
303 template <class StreamType>
304 void
306 {
307  unsigned int ix[dim];
308  for (unsigned int i=0; i<n_pols; ++i)
309  {
310  compute_index(i,ix);
311  out << i << "\t";
312  for (unsigned int d=0; d<dim; ++d)
313  out << ix[d] << " ";
314  out << std::endl;
315  }
316 }
317 
318 template <int dim>
319 template <int order>
322  const Point<dim> &p) const
323 {
324  unsigned int indices[dim];
325  compute_index (i, indices);
326 
327  double v [dim][order+1];
328  {
329  std::vector<double> tmp (order+1);
330  for (unsigned int d=0; d<dim; ++d)
331  {
332  polynomials[indices[d]].value (p(d), tmp);
333  for (unsigned int j=0; j<order+1; ++j)
334  v[d][j] = tmp[j];
335  }
336  }
337 
338  Tensor<order,dim> derivative;
339  switch (order)
340  {
341  case 1:
342  {
343  Tensor<1,dim> &derivative_1 = *reinterpret_cast<Tensor<1,dim>*>(&derivative);
344  for (unsigned int d=0; d<dim; ++d)
345  {
346  derivative_1[d] = 1.;
347  for (unsigned int x=0; x<dim; ++x)
348  {
349  unsigned int x_order=0;
350  if (d==x) ++x_order;
351 
352  derivative_1[d] *= v[x][x_order];
353  }
354  }
355 
356  return derivative;
357  }
358  case 2:
359  {
360  Tensor<2,dim> &derivative_2 = *reinterpret_cast<Tensor<2,dim>*>(&derivative);
361  for (unsigned int d1=0; d1<dim; ++d1)
362  for (unsigned int d2=0; d2<dim; ++d2)
363  {
364  derivative_2[d1][d2] = 1.;
365  for (unsigned int x=0; x<dim; ++x)
366  {
367  unsigned int x_order=0;
368  if (d1==x) ++x_order;
369  if (d2==x) ++x_order;
370 
371  derivative_2[d1][d2] *= v[x][x_order];
372  }
373  }
374 
375  return derivative;
376  }
377  case 3:
378  {
379  Tensor<3,dim> &derivative_3 = *reinterpret_cast<Tensor<3,dim>*>(&derivative);
380  for (unsigned int d1=0; d1<dim; ++d1)
381  for (unsigned int d2=0; d2<dim; ++d2)
382  for (unsigned int d3=0; d3<dim; ++d3)
383  {
384  derivative_3[d1][d2][d3] = 1.;
385  for (unsigned int x=0; x<dim; ++x)
386  {
387  unsigned int x_order=0;
388  if (d1==x) ++x_order;
389  if (d2==x) ++x_order;
390  if (d3==x) ++x_order;
391 
392  derivative_3[d1][d2][d3] *= v[x][x_order];
393  }
394  }
395 
396  return derivative;
397  }
398  case 4:
399  {
400  Tensor<4,dim> &derivative_4 = *reinterpret_cast<Tensor<4,dim>*>(&derivative);
401  for (unsigned int d1=0; d1<dim; ++d1)
402  for (unsigned int d2=0; d2<dim; ++d2)
403  for (unsigned int d3=0; d3<dim; ++d3)
404  for (unsigned int d4=0; d4<dim; ++d4)
405  {
406  derivative_4[d1][d2][d3][d4] = 1.;
407  for (unsigned int x=0; x<dim; ++x)
408  {
409  unsigned int x_order=0;
410  if (d1==x) ++x_order;
411  if (d2==x) ++x_order;
412  if (d3==x) ++x_order;
413  if (d4==x) ++x_order;
414 
415  derivative_4[d1][d2][d3][d4] *= v[x][x_order];
416  }
417  }
418 
419  return derivative;
420  }
421  default:
422  {
423  Assert (false, ExcNotImplemented());
424  return derivative;
425  }
426  }
427 
428 }
429 
430 
431 DEAL_II_NAMESPACE_CLOSE
432 
433 #endif
void set_numbering(const std::vector< unsigned int > &renumber)
static const unsigned int dimension
std::vector< unsigned int > index_map_inverse
#define Assert(cond, exc)
Definition: exceptions.h:1142
void compute(const Point< dim > &unit_point, std::vector< double > &values, std::vector< Tensor< 1, dim > > &grads, std::vector< Tensor< 2, dim > > &grad_grads, std::vector< Tensor< 3, dim > > &third_derivatives, std::vector< Tensor< 4, dim > > &fourth_derivatives) const
unsigned int n() const
const unsigned int n_pols
Tensor< order, dim > compute_derivative(const unsigned int i, const Point< dim > &p) const
void output_indices(StreamType &out) const
Tensor< 2, dim > compute_grad_grad(const unsigned int i, const Point< dim > &p) const
double compute_value(const unsigned int i, const Point< dim > &p) const
static unsigned int compute_n_pols(const unsigned int n)
Tensor< 1, dim > compute_grad(const unsigned int i, const Point< dim > &p) const
unsigned int degree() const
PolynomialSpace(const std::vector< Pol > &pols)
static ::ExceptionBase & ExcNotImplemented()
std::vector< unsigned int > index_map
const std::vector< Polynomials::Polynomial< double > > polynomials
void compute_index(const unsigned int n, unsigned int(&index)[dim >0?dim:1]) const