Flow123d  release_3.0.0-902-gf72c4b4
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 template<unsigned int dim> 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  Quadrature<dim> &_quadrature,
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);
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);
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, quadrature->size());
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)
337  {
338  ASSERT_LT_DBG(point_no, quadrature->size());
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, quadrature->size());
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)
369  {
370  ASSERT_LT_DBG(point_no, quadrature->size());
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  {
409  return quadrature->size();
410  }
411 
412  /**
413  * @brief Returns the number of shape functions.
414  */
415  inline unsigned int n_dofs()
416  {
417  return fe->n_dofs();
418  }
419 
420 
421  /**
422  * @brief Returns the quadrature in use.
423  */
425  {
426  return quadrature;
427  }
428 
429  /**
430  * @brief Returns the finite element in use.
431  */
432  inline FiniteElement<dim> * get_fe() const
433  {
434  return fe;
435  }
436 
437  /**
438  * @brief Returns the mapping in use.
439  */
441  {
442  return mapping;
443  }
444 
445 protected:
446 
447  /// Precompute finite element data on reference element.
448  FEInternalData *init_fe_data(const Quadrature<dim> *q);
449 
450  /**
451  * @brief Computes the shape function values and gradients on the actual cell
452  * and fills the FEValues structure.
453  *
454  * @param fe_data Precomputed finite element data.
455  */
456  void fill_data(const FEInternalData &fe_data);
457 
458  /// Compute shape functions and gradients on the actual cell for scalar FE.
459  void fill_scalar_data(const FEInternalData &fe_data);
460 
461  /// Compute shape functions and gradients on the actual cell for vectorial FE.
462  void fill_vec_data(const FEInternalData &fe_data);
463 
464  /// Compute shape functions and gradients on the actual cell for vectorial FE.
465  void fill_vec_contravariant_data(const FEInternalData &fe_data);
466 
467  /// Compute shape functions and gradients on the actual cell for Raviart-Thomas FE.
468  void fill_vec_piola_data(const FEInternalData &fe_data);
469 
470  /// Compute shape functions and gradients on the actual cell for tensorial FE.
471  void fill_tensor_data(const FEInternalData &fe_data);
472 
473  /// Compute shape functions and gradients on the actual cell for mixed system of FE.
474  void fill_system_data(const FEInternalData &fe_data);
475 
476 
477  /**
478  * @brief The mapping from the reference cell to the actual cell.
479  */
481 
482  /**
483  * @brief The quadrature rule used to calculate integrals.
484  */
486 
487  /**
488  * @brief The used finite element.
489  */
491 
492  /**
493  * @brief Precomputed mapping data.
494  */
496 
497  /**
498  * @brief Precomputed finite element data.
499  */
501 
502  /**
503  * @brief Data computed by the mapping and finite element.
504  */
506 
507  /// Vector of FEValues for sub-elements of FESystem.
509 
510  /// Number of components of the FE.
511  unsigned int n_components_;
512 
513  /// Auxiliary storage of FEValuesViews accessors.
515 };
516 
517 
518 
519 
520 /**
521  * @brief Calculates finite element data on the actual cell.
522  *
523  * FEValues takes care of the calculation of finite element data on
524  * the actual cell such as values of shape functions at quadrature
525  * points, gradients of shape functions, Jacobians of the mapping
526  * from the reference cell etc.
527  * @param dim Dimension of the reference cell.
528  * @param spacedim Dimension of the Euclidean space where the actual
529  * cell lives.
530  */
531 template<unsigned int dim, unsigned int spacedim>
532 class FEValues : public FEValuesBase<dim,spacedim>
533 {
534 public:
535 
536  /**
537  * @brief Constructor.
538  *
539  * Initializes structures and calculates
540  * cell-independent data.
541  *
542  * @param _mapping The mapping between the reference and actual cell.
543  * @param _quadrature The quadrature rule.
544  * @param _fe The finite element.
545  * @param _flags The update flags.
546  */
548  Quadrature<dim> &_quadrature,
549  FiniteElement<dim> &_fe,
550  UpdateFlags _flags);
551 
552  /**
553  * @brief Update cell-dependent data (gradients, Jacobians etc.)
554  *
555  * @param cell The actual cell.
556  */
557  void reinit(ElementAccessor<3> &cell);
558 
559 
560 };
561 
562 
563 
564 
565 /**
566  * @brief Calculates finite element data on a side.
567  *
568  * FESideValues takes care of the calculation of finite element data on
569  * a side of the actual cell such as values of shape functions at quadrature
570  * points, gradients of shape functions, Jacobians of the mapping
571  * from the reference cell etc.
572  * @param dim Dimension of the reference cell.
573  * @param spacedim Dimension of the Euclidean space where the actual
574  * cell lives.
575  */
576 template<unsigned int dim, unsigned int spacedim>
577 class FESideValues : public FEValuesBase<dim,spacedim>
578 {
579 
580 public:
581 
582  /**
583  * @brief Constructor.
584  *
585  * Initializes structures and calculates
586  * cell-independent data.
587  *
588  * @param _mapping The mapping between the reference and actual cell.
589  * @param _sub_quadrature The quadrature rule on the side.
590  * @param _fe The finite element.
591  * @param flags The update flags.
592  */
594  Quadrature<dim-1> &_sub_quadrature,
595  FiniteElement<dim> &_fe,
596  UpdateFlags flags);
597 
598  /// Destructor.
599  virtual ~FESideValues();
600 
601  /**
602  * @brief Update cell-dependent data (gradients, Jacobians etc.)
603  *
604  * @param cell The actual cell.
605  * @param sid Number of the side of the cell.
606  */
607  void reinit(ElementAccessor<3> &cell,
608  unsigned int sid);
609 
610 
611 
612 private:
613 
614  /**
615  * @brief Quadrature for the integration on the element sides.
616  */
617  const Quadrature<dim-1> *sub_quadrature;
618 
620 
622 
624 
625 };
626 
627 
628 
629 
630 
631 #endif /* FE_VALUES_HH_ */
double JxW(const unsigned int point_no)
Return the product of Jacobian determinant and the quadrature weight at given quadrature point...
Definition: fe_values.hh:336
UpdateFlags
Enum type UpdateFlags indicates which quantities are to be recomputed on each finite element cell...
Definition: update_flags.hh:67
unsigned int n_dofs()
Returns the number of shape functions.
Definition: fe_values.hh:415
std::vector< arma::mat::fixed< dim, spacedim > > inverse_jacobians
Inverse Jacobians at the quadrature points.
Definition: fe_values.hh:122
MappingInternalData * mapping_data
Precomputed mapping data.
Definition: fe_values.hh:495
UpdateFlags update_flags
Flags that indicate which finite element quantities are to be computed.
Definition: fe_values.hh:161
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:505
std::vector< double > JxW_values
Transformed quadrature weights.
Definition: fe_values.hh:107
FEInternalData * fe_data
Precomputed finite element data.
Definition: fe_values.hh:500
FiniteElement< dim > * get_fe() const
Returns the finite element in use.
Definition: fe_values.hh:432
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...
Base class for quadrature rules on simplices in arbitrary dimensions.
Definition: fe_values.hh:35
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:511
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
arma::vec::fixed< spacedim > normal_vector(unsigned int point_no)
Returns the normal vector to a side at given quadrature point.
Definition: fe_values.hh:368
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
virtual ~FEValuesSpaceBase()
Definition: fe_values.hh:178
const Quadrature< dim-1 > * sub_quadrature
Quadrature for the integration on the element sides.
Definition: fe_values.hh:617
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
Quadrature< dim > * quadrature
The quadrature rule used to calculate integrals.
Definition: fe_values.hh:485
Mapping< dim, spacedim > * get_mapping() const
Returns the mapping in use.
Definition: fe_values.hh:440
std::vector< std::vector< arma::mat > > ref_shape_grads
Precomputed gradients of basis functions at the quadrature points.
Definition: fe_values.hh:69
Class FEValuesData holds the arrays of data computed by Mapping and FiniteElement.
Definition: fe_values.hh:86
Quadrature< dim > * get_quadrature() const
Returns the quadrature in use.
Definition: fe_values.hh:424
ViewsCache views_cache_
Auxiliary storage of FEValuesViews accessors.
Definition: fe_values.hh:514
Mapping< dim, spacedim > * mapping
The mapping from the reference cell to the actual cell.
Definition: fe_values.hh:480
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:490
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:532
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:508
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
Calculates finite element data on a side.
Definition: fe_values.hh:577
#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