Flow123d  release_3.0.0-1193-g9220a69
fe_values.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file fe_values.hh
15  * @brief Class FEValues calculates finite element data on the actual
16  * cells such as shape function values, gradients, Jacobian of
17  * the mapping from the reference cell etc.
18  * @author Jan Stebel
19  */
20 
21 #ifndef FE_VALUES_HH_
22 #define FE_VALUES_HH_
23 
24 #include <string.h> // for memcpy
25 #include <algorithm> // for swap
26 #include <new> // for operator new[]
27 #include <string> // for operator<<
28 #include <vector> // for vector
29 #include "fem/fe_values_views.hh" // for FEValuesViews
30 #include "mesh/ref_element.hh" // for RefElement
31 #include "mesh/accessors.hh"
32 #include "fem/update_flags.hh" // for UpdateFlags
33 
34 class DOFHandlerBase;
35 class Quadrature;
36 template<unsigned int dim> class FiniteElement;
37 template<unsigned int dim, unsigned int spacedim> class FEValuesBase;
38 template<unsigned int dim, unsigned int spacedim> class Mapping;
39 
40 struct MappingInternalData;
41 
42 
43 
44 /**
45  * @brief Structure for storing the precomputed finite element data.
46  */
48 {
49 public:
50 
51  FEInternalData(unsigned int np, unsigned int nd);
52 
53  /**
54  * @brief Precomputed values of basis functions at the quadrature points.
55  *
56  * Dimensions: (no. of quadrature points)
57  * x (no. of dofs)
58  * x (no. of components in ref. cell)
59  */
61 
62  /**
63  * @brief Precomputed gradients of basis functions at the quadrature points.
64  *
65  * Dimensions: (no. of quadrature points)
66  * x (no. of dofs)
67  * x ((dim of. ref. cell)x(no. of components in ref. cell))
68  */
70 
71  /// Number of quadrature points.
72  unsigned int n_points;
73 
74  /// Number of dofs (shape functions).
75  unsigned int n_dofs;
76 };
77 
78 
79 
80 
81 /**
82  * @brief Class FEValuesData holds the arrays of data computed by
83  * Mapping and FiniteElement.
84  */
85 template<unsigned int dim, unsigned int spacedim>
87 {
88 public:
89 
90  /**
91  * @brief Resize the data arrays.
92  * @param size Number of quadrature points.
93  * @param flags Update flags to be stores.
94  * @param n_comp Number of components of shape values.
95  */
96  void allocate(unsigned int size, UpdateFlags flags, unsigned n_comp);
97 
98 
99 
100  /**
101  * @brief Transformed quadrature weights.
102  *
103  * Values at quadrature points of the product of the Jacobian
104  * determinant of the mapping and the weight at the particular
105  * quadrature point.
106  */
108 
109  /**
110  * @brief Jacobians of the mapping at the quadrature points.
111  */
113 
114  /**
115  * @brief Determinants of Jacobians at quadrature points.
116  */
118 
119  /**
120  * @brief Inverse Jacobians at the quadrature points.
121  */
123 
124  /**
125  * @brief Coordinates of quadrature points in the actual cell coordinate system.
126  */
128 
129  /**
130  * @brief Shape functions evaluated at the quadrature points.
131  */
133 
134  /**
135  * @brief Gradients of shape functions evaluated at the quadrature points.
136  *
137  * Each row of the matrix contains the gradient of one shape function.
138  */
140 
141 // /**
142 // * @brief Shape functions (for vectorial finite elements) evaluated at
143 // * quadrature points.
144 // */
145 // std::vector<std::vector<arma::vec::fixed<spacedim> > > shape_vectors;
146 //
147 // /**
148 // * @brief Gradients of shape functions (for vectorial finite elements).
149 // */
150 // std::vector<std::vector<arma::mat::fixed<spacedim,spacedim> > > shape_grad_vectors;
151 
152  /**
153  * @brief Normal vectors to the element at the quadrature points lying
154  * on a side.
155  */
157 
158  /**
159  * @brief Flags that indicate which finite element quantities are to be computed.
160  */
162 
163  /**
164  * @brief Iterator to the last reinit-ed cell.
165  */
167 
168 };
169 
170 
171 /**
172  * @brief Abstract base class with certain methods independent of the template parameter @p dim.
173  */
174 template<unsigned int spacedim>
176 {
177 public:
178  virtual ~FEValuesSpaceBase() {}
179  /**
180  * @brief Return the value of the @p function_no-th shape function at
181  * the @p point_no-th quadrature point.
182  *
183  * @param function_no Number of the shape function.
184  * @param point_no Number of the quadrature point.
185  */
186  virtual double shape_value(const unsigned int function_no, const unsigned int point_no) = 0;
187 
188  /**
189  * @brief Return the gradient of the @p function_no-th shape function at
190  * the @p point_no-th quadrature point.
191  *
192  * @param function_no Number of the shape function.
193  * @param point_no Number of the quadrature point.
194  */
195  virtual arma::vec::fixed<spacedim> shape_grad(const unsigned int function_no, const unsigned int point_no) = 0;
196 
197  /**
198  * @brief Return the product of Jacobian determinant and the quadrature
199  * weight at given quadrature point.
200  *
201  * @param point_no Number of the quadrature point.
202  */
203  virtual double JxW(const unsigned int point_no) = 0;
204 
205  /**
206  * @brief Returns the normal vector to a side at given quadrature point.
207  *
208  * @param point_no Number of the quadrature point.
209  */
210  virtual arma::vec::fixed<spacedim> normal_vector(unsigned int point_no) = 0;
211 
212  /**
213  * @brief Returns the number of shape functions.
214  */
215  virtual unsigned int n_dofs() = 0;
216 
217 };
218 
219 /**
220  * @brief Base class for FEValues and FESideValues
221  */
222 template<unsigned int dim, unsigned int spacedim>
223 class FEValuesBase : public FEValuesSpaceBase<spacedim>
224 {
225 private:
226 
227  // internal structure that stores all possible views
228  // for scalar and vector-valued components of the FE
229  struct ViewsCache {
233 
234  void initialize(FEValuesBase &fv);
235  };
236 
237 public:
238 
239  /**
240  * @brief Default constructor
241  */
242  FEValuesBase();
243 
244 
245  /**
246  * Correct deallocation of objects created by 'initialize' methods.
247  */
248  virtual ~FEValuesBase();
249 
250 
251  /**
252  * @brief Allocates space for computed data.
253  *
254  * @param _mapping The mapping between reference and actual cell.
255  * @param _quadrature The quadrature rule.
256  * @param _fe The finite element.
257  * @param flags The update flags.
258  */
259  void allocate(Mapping<dim,spacedim> &_mapping,
260  unsigned int n_points,
261  FiniteElement<dim> &_fe,
262  UpdateFlags flags);
263 
264  /**
265  * @brief Determine quantities to be recomputed on each cell.
266  *
267  * @param flags Flags that indicate what has to be recomputed.
268  */
269  UpdateFlags update_each(UpdateFlags flags);
270 
271  /**
272  * @brief Return the value of the @p function_no-th shape function at
273  * the @p point_no-th quadrature point.
274  *
275  * @param function_no Number of the shape function.
276  * @param point_no Number of the quadrature point.
277  */
278  double shape_value(const unsigned int function_no, const unsigned int point_no) override;
279 
280 
281  /**
282  * @brief Return the gradient of the @p function_no-th shape function at
283  * the @p point_no-th quadrature point.
284  *
285  * @param function_no Number of the shape function.
286  * @param point_no Number of the quadrature point.
287  */
288  arma::vec::fixed<spacedim> shape_grad(const unsigned int function_no, const unsigned int point_no) override;
289 
290  /**
291  * @brief Return the value of the @p function_no-th shape function at
292  * the @p point_no-th quadrature point.
293  *
294  * For vectorial finite elements.
295  *
296  * @param function_no Number of the shape function.
297  * @param point_no Number of the quadrature point.
298  */
299  double shape_value_component(const unsigned int function_no,
300  const unsigned int point_no,
301  const unsigned int comp) const;
302 
303  /**
304  * @brief Return the gradient of the @p function_no-th shape function at
305  * the @p point_no-th quadrature point.
306  *
307  * For vectorial finite elements.
308  *
309  * @param function_no Number of the shape function.
310  * @param point_no Number of the quadrature point.
311  */
312  arma::vec::fixed<spacedim> shape_grad_component(const unsigned int function_no,
313  const unsigned int point_no,
314  const unsigned int comp) const;
315 
316  /**
317  * @brief Return the relative volume change of the cell (Jacobian determinant).
318  *
319  * If dim==spacedim then the sign may be negative, otherwise the
320  * result is a positive number.
321  *
322  * @param point_no Number of the quadrature point.
323  */
324  inline double determinant(const unsigned int point_no)
325  {
326  ASSERT_LT_DBG(point_no, n_points_);
327  return data.determinants[point_no];
328  }
329 
330  /**
331  * @brief Return the product of Jacobian determinant and the quadrature
332  * weight at given quadrature point.
333  *
334  * @param point_no Number of the quadrature point.
335  */
336  inline double JxW(const unsigned int point_no) override
337  {
338  ASSERT_LT_DBG(point_no, n_points_);
339  return data.JxW_values[point_no];
340  }
341 
342  /**
343  * @brief Return coordinates of the quadrature point in the actual cell system.
344  *
345  * @param point_no Number of the quadrature point.
346  */
347  inline arma::vec::fixed<spacedim> point(const unsigned int point_no)
348  {
349  ASSERT_LT_DBG(point_no, n_points_);
350  return data.points[point_no];
351  }
352 
353  /**
354  * @brief Return coordinates of all quadrature points in the actual cell system.
355  *
356  */
358  {
359  return data.points;
360  }
361 
362 
363  /**
364  * @brief Returns the normal vector to a side at given quadrature point.
365  *
366  * @param point_no Number of the quadrature point.
367  */
368  inline arma::vec::fixed<spacedim> normal_vector(unsigned int point_no) override
369  {
370  ASSERT_LT_DBG(point_no, n_points_);
371  return data.normal_vectors[point_no];
372  }
373 
374  /**
375  * @brief Accessor to scalar values of multicomponent FE.
376  * @param i Index of scalar component.
377  */
379  {
380  ASSERT_LT_DBG(i, views_cache_.scalars.size());
381  return views_cache_.scalars[i];
382  }
383 
384  /**
385  * @brief Accessor to vector values of multicomponent FE.
386  * @param i Index of first vector component.
387  */
389  {
390  ASSERT_LT_DBG(i, views_cache_.vectors.size());
391  return views_cache_.vectors[i];
392  }
393 
394  /**
395  * @brief Accessor to tensor values of multicomponent FE.
396  * @param i Index of first tensor component.
397  */
399  {
400  ASSERT_LT_DBG(i, views_cache_.tensors.size());
401  return views_cache_.tensors[i];
402  }
403 
404  /**
405  * @brief Returns the number of quadrature points.
406  */
407  inline unsigned int n_points()
408  { return n_points_; }
409 
410  /**
411  * @brief Returns the number of shape functions.
412  */
413  inline unsigned int n_dofs() override
414  {
415  return fe->n_dofs();
416  }
417 
418 
419  /**
420  * @brief Returns the quadrature in use.
421  */
422  virtual const Quadrature * get_quadrature() const = 0;
423 
424  /**
425  * @brief Returns the finite element in use.
426  */
427  inline FiniteElement<dim> * get_fe() const
428  {
429  return fe;
430  }
431 
432  /**
433  * @brief Returns the mapping in use.
434  */
436  {
437  return mapping;
438  }
439 
440 protected:
441 
442  /// Precompute finite element data on reference element.
443  FEInternalData *init_fe_data(const Quadrature *q);
444 
445  /**
446  * @brief Computes the shape function values and gradients on the actual cell
447  * and fills the FEValues structure.
448  *
449  * @param fe_data Precomputed finite element data.
450  */
451  void fill_data(const FEInternalData &fe_data);
452 
453  /// Compute shape functions and gradients on the actual cell for scalar FE.
454  void fill_scalar_data(const FEInternalData &fe_data);
455 
456  /// Compute shape functions and gradients on the actual cell for vectorial FE.
457  void fill_vec_data(const FEInternalData &fe_data);
458 
459  /// Compute shape functions and gradients on the actual cell for vectorial FE.
460  void fill_vec_contravariant_data(const FEInternalData &fe_data);
461 
462  /// Compute shape functions and gradients on the actual cell for Raviart-Thomas FE.
463  void fill_vec_piola_data(const FEInternalData &fe_data);
464 
465  /// Compute shape functions and gradients on the actual cell for tensorial FE.
466  void fill_tensor_data(const FEInternalData &fe_data);
467 
468  /// Compute shape functions and gradients on the actual cell for mixed system of FE.
469  void fill_system_data(const FEInternalData &fe_data);
470 
471 
472  /**
473  * @brief The mapping from the reference cell to the actual cell.
474  */
476 
477  /** @brief Number of integration points. */
478  unsigned int n_points_;
479 
480  /**
481  * @brief The used finite element.
482  */
484 
485  /**
486  * @brief Precomputed mapping data.
487  */
489 
490  /**
491  * @brief Precomputed finite element data.
492  */
494 
495  /**
496  * @brief Data computed by the mapping and finite element.
497  */
499 
500  /// Vector of FEValues for sub-elements of FESystem.
502 
503  /// Number of components of the FE.
504  unsigned int n_components_;
505 
506  /// Auxiliary storage of FEValuesViews accessors.
508 };
509 
510 
511 
512 
513 /**
514  * @brief Calculates finite element data on the actual cell.
515  *
516  * FEValues takes care of the calculation of finite element data on
517  * the actual cell such as values of shape functions at quadrature
518  * points, gradients of shape functions, Jacobians of the mapping
519  * from the reference cell etc.
520  * @param dim Dimension of the reference cell.
521  * @param spacedim Dimension of the Euclidean space where the actual
522  * cell lives.
523  */
524 template<unsigned int dim, unsigned int spacedim>
525 class FEValues : public FEValuesBase<dim,spacedim>
526 {
527 public:
528 
529  /**
530  * @brief Constructor.
531  *
532  * Initializes structures and calculates
533  * cell-independent data.
534  *
535  * @param _mapping The mapping between the reference and actual cell.
536  * @param _quadrature The quadrature rule.
537  * @param _fe The finite element.
538  * @param _flags The update flags.
539  */
541  Quadrature &_quadrature,
542  FiniteElement<dim> &_fe,
543  UpdateFlags _flags);
544 
545  /**
546  * @brief Update cell-dependent data (gradients, Jacobians etc.)
547  *
548  * @param cell The actual cell.
549  */
550  void reinit(ElementAccessor<3> &cell);
551 
552  const Quadrature *get_quadrature() const override
553  { return quadrature; }
554 
555 
556 
557 private:
558 
559  /**
560  * @brief The quadrature rule used to calculate integrals.
561  */
563 
564 
565 };
566 
567 
568 
569 
570 /**
571  * @brief Calculates finite element data on a side.
572  *
573  * FESideValues takes care of the calculation of finite element data on
574  * a side of the actual cell such as values of shape functions at quadrature
575  * points, gradients of shape functions, Jacobians of the mapping
576  * from the reference cell etc.
577  * @param dim Dimension of the reference cell.
578  * @param spacedim Dimension of the Euclidean space where the actual
579  * cell lives.
580  */
581 template<unsigned int dim, unsigned int spacedim>
582 class FESideValues : public FEValuesBase<dim,spacedim>
583 {
584 
585 public:
586 
587  /**
588  * @brief Constructor.
589  *
590  * Initializes structures and calculates
591  * cell-independent data.
592  *
593  * @param _mapping The mapping between the reference and actual cell.
594  * @param _sub_quadrature The quadrature rule on the side.
595  * @param _fe The finite element.
596  * @param flags The update flags.
597  */
599  Quadrature &_sub_quadrature,
600  FiniteElement<dim> &_fe,
601  UpdateFlags flags);
602 
603  /// Destructor.
604  virtual ~FESideValues();
605 
606  /**
607  * @brief Update cell-dependent data (gradients, Jacobians etc.)
608  *
609  * @param cell The actual cell.
610  * @param sid Number of the side of the cell.
611  */
612  void reinit(ElementAccessor<3> &cell,
613  unsigned int sid);
614 
615  const Quadrature *get_quadrature() const override
616  { return &side_quadrature[side_idx_][side_perm_]; }
617 
618 
619 private:
620 
621  /**
622  * @brief Quadrature for the integration on the element sides.
623  */
625 
627 
629 
631 
632  /// Current side on which FESideValues was recomputed.
633  unsigned int side_idx_;
634 
635  /// Permutation index of current side.
636  unsigned int side_perm_;
637 
638 };
639 
640 
641 
642 
643 
644 #endif /* FE_VALUES_HH_ */
UpdateFlags
Enum type UpdateFlags indicates which quantities are to be recomputed on each finite element cell...
Definition: update_flags.hh:67
const Quadrature * get_quadrature() const override
Returns the quadrature in use.
Definition: fe_values.hh:615
std::vector< arma::mat::fixed< dim, spacedim > > inverse_jacobians
Inverse Jacobians at the quadrature points.
Definition: fe_values.hh:122
std::vector< std::vector< Quadrature > > side_quadrature
Definition: fe_values.hh:626
MappingInternalData * mapping_data
Precomputed mapping data.
Definition: fe_values.hh:488
UpdateFlags update_flags
Flags that indicate which finite element quantities are to be computed.
Definition: fe_values.hh:161
unsigned int side_perm_
Permutation index of current side.
Definition: fe_values.hh:636
const FEValuesViews::Tensor< dim, spacedim > & tensor_view(unsigned int i) const
Accessor to tensor values of multicomponent FE.
Definition: fe_values.hh:398
FEValuesData< dim, spacedim > data
Data computed by the mapping and finite element.
Definition: fe_values.hh:498
std::vector< double > JxW_values
Transformed quadrature weights.
Definition: fe_values.hh:107
FEInternalData * fe_data
Precomputed finite element data.
Definition: fe_values.hh:493
FiniteElement< dim > * get_fe() const
Returns the finite element in use.
Definition: fe_values.hh:427
vector< FEValuesViews::Tensor< dim, spacedim > > tensors
Definition: fe_values.hh:232
unsigned int n_points()
Returns the number of quadrature points.
Definition: fe_values.hh:407
Enum type UpdateFlags indicates which quantities are to be recomputed on each finite element cell...
unsigned int side_idx_
Current side on which FESideValues was recomputed.
Definition: fe_values.hh:633
Base class for quadrature rules on simplices in arbitrary dimensions.
Definition: quadrature.hh:48
vector< FEValuesViews::Vector< dim, spacedim > > vectors
Definition: fe_values.hh:231
unsigned int n_components_
Number of components of the FE.
Definition: fe_values.hh:504
std::vector< arma::vec::fixed< spacedim > > normal_vectors
Shape functions (for vectorial finite elements) evaluated at quadrature points.
Definition: fe_values.hh:156
vector< FEValuesViews::Scalar< dim, spacedim > > scalars
Definition: fe_values.hh:230
Quadrature * quadrature
The quadrature rule used to calculate integrals.
Definition: fe_values.hh:562
std::vector< arma::mat::fixed< spacedim, dim > > jacobians
Jacobians of the mapping at the quadrature points.
Definition: fe_values.hh:112
std::vector< arma::vec::fixed< spacedim > > points
Coordinates of quadrature points in the actual cell coordinate system.
Definition: fe_values.hh:127
Abstract class for the mapping between reference and actual cell.
Definition: fe_values.hh:38
double JxW(const unsigned int point_no) override
Return the product of Jacobian determinant and the quadrature weight at given quadrature point...
Definition: fe_values.hh:336
virtual ~FEValuesSpaceBase()
Definition: fe_values.hh:178
const Quadrature * get_quadrature() const override
Returns the quadrature in use.
Definition: fe_values.hh:552
const FEValuesViews::Vector< dim, spacedim > & vector_view(unsigned int i) const
Accessor to vector values of multicomponent FE.
Definition: fe_values.hh:388
FEInternalData(unsigned int np, unsigned int nd)
Definition: fe_values.cc:39
arma::vec::fixed< spacedim > normal_vector(unsigned int point_no) override
Returns the normal vector to a side at given quadrature point.
Definition: fe_values.hh:368
Mapping< dim, spacedim > * get_mapping() const
Returns the mapping in use.
Definition: fe_values.hh:435
std::vector< std::vector< arma::mat > > ref_shape_grads
Precomputed gradients of basis functions at the quadrature points.
Definition: fe_values.hh:69
unsigned int n_points_
Number of integration points.
Definition: fe_values.hh:478
Class FEValuesData holds the arrays of data computed by Mapping and FiniteElement.
Definition: fe_values.hh:86
ViewsCache views_cache_
Auxiliary storage of FEValuesViews accessors.
Definition: fe_values.hh:507
const Quadrature * sub_quadrature
Quadrature for the integration on the element sides.
Definition: fe_values.hh:624
Mapping< dim, spacedim > * mapping
The mapping from the reference cell to the actual cell.
Definition: fe_values.hh:475
std::vector< std::vector< double > > shape_values
Shape functions evaluated at the quadrature points.
Definition: fe_values.hh:132
FiniteElement< dim > * fe
The used finite element.
Definition: fe_values.hh:483
ElementAccessor< 3 > * present_cell
Iterator to the last reinit-ed cell.
Definition: fe_values.hh:166
std::vector< double > determinants
Determinants of Jacobians at quadrature points.
Definition: fe_values.hh:117
Calculates finite element data on the actual cell.
Definition: fe_values.hh:525
unsigned int n_points
Number of quadrature points.
Definition: fe_values.hh:72
vector< arma::vec::fixed< spacedim > > & point_list()
Return coordinates of all quadrature points in the actual cell system.
Definition: fe_values.hh:357
Class RefElement defines numbering of vertices, sides, calculation of normal vectors etc...
std::vector< std::shared_ptr< FEValuesBase< dim, spacedim > > > fe_values_vec
Vector of FEValues for sub-elements of FESystem.
Definition: fe_values.hh:501
Structure for storing the precomputed finite element data.
Definition: fe_values.hh:47
std::vector< std::vector< arma::vec > > ref_shape_values
Precomputed values of basis functions at the quadrature points.
Definition: fe_values.hh:60
Abstract class for the description of a general finite element on a reference simplex in dim dimensio...
Base class for FEValues and FESideValues.
Definition: fe_values.hh:37
Abstract base class with certain methods independent of the template parameter dim.
Definition: fe_values.hh:175
std::vector< std::vector< arma::vec::fixed< spacedim > > > shape_gradients
Gradients of shape functions evaluated at the quadrature points.
Definition: fe_values.hh:139
unsigned int n_dofs
Number of dofs (shape functions).
Definition: fe_values.hh:75
Mapping data that can be precomputed on the actual cell.
Definition: mapping.hh:99
const FEValuesViews::Scalar< dim, spacedim > & scalar_view(unsigned int i) const
Accessor to scalar values of multicomponent FE.
Definition: fe_values.hh:378
double determinant(const unsigned int point_no)
Return the relative volume change of the cell (Jacobian determinant).
Definition: fe_values.hh:324
unsigned int n_dofs() override
Returns the number of shape functions.
Definition: fe_values.hh:413
Calculates finite element data on a side.
Definition: fe_values.hh:582
#define ASSERT_LT_DBG(a, b)
Definition of comparative assert macro (Less Than) only for debug mode.
Definition: asserts.hh:299
arma::vec::fixed< spacedim > point(const unsigned int point_no)
Return coordinates of the quadrature point in the actual cell system.
Definition: fe_values.hh:347