1 // --------------------------------------------------------------------- 2 // 3 // Copyright (C) 2006 - 2020 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.md at 12 // the top level directory of deal.II. 13 // 14 // --------------------------------------------------------------------- 15 16 17 /** 18 * @defgroup UpdateFlags The interplay of UpdateFlags, Mapping, and FiniteElement in FEValues 19 * 20 * <h2>Introduction</h2> 21 * 22 * In order to compute local contributions of an individual cell to the global 23 * matrix and right hand side, we usually employ two techniques: 24 * - First, the integral is transformed from the actual cell $K$ to the 25 * unit/reference cell $\hat K$. For example, for the Laplace equation, we 26 * transform 27 * @f[ 28 * A^K_{ij} = \int_K \nabla \varphi_i(\bf x) \cdot \nabla \varphi_j(\bf x) \; dx 29 * @f] 30 * into 31 * @f[ 32 * A^K_{ij} = 33 * \int_{\hat K} 34 * \left[ J^{-1}(\hat{\bf x}) \hat \nabla \varphi_i(\hat{\bf x}) \right] 35 * \cdot 36 * \left[ J^{-1}(\hat{\bf x}) \hat \nabla \varphi_j(\hat{\bf x}) \right] 37 * \; 38 * |\textrm{det}\; J(\hat{\bf x})| 39 * \;\; 40 * d\hat x, 41 * @f] 42 * where a hat indicates reference coordinates, and $J(\hat{\bf 43 * x}_q)$ is the Jacobian 44 * $\frac{\partial \bf F_K(\hat{\bf x})}{\partial\bf \hat x}$ of the mapping 45 * $\bf x = \bf F_K(\hat{\bf x})$. 46 * - Second, this integral is then approximated through quadrature. This yields 47 * the formula 48 * @f[ 49 * A^K_{ij} = \sum_{q}J^{-1}\left[(\hat{\bf x}_q) \hat \nabla \varphi_i(\hat{\bf x}_q)\right] \cdot 50 * \left[J^{-1}(\hat{\bf x}_q) \hat \nabla \varphi_j(\hat{\bf x}_q)\right]\ |\textrm{det}\ J(\hat{\bf x}_q)| 51 * w_q, 52 * @f] 53 * where $q$ indicates the index of the quadrature point, $\hat{\bf x}_q$ its 54 * location on the reference cell, and $w_q$ its weight. 55 * 56 * In order to evaluate such an expression in an application code, we 57 * have to access three different kinds of objects: a quadrature 58 * object that describes locations $\hat{\bf x}_q$ and weights $w_q$ of 59 * quadrature points on the reference cell; a finite element object that 60 * describes the gradients $\hat\nabla \varphi_i(\hat{\bf x}_q)$ of shape 61 * functions on the unit cell; and a mapping object that provides the 62 * Jacobian as well as its determinant. Dealing with all these 63 * objects would be cumbersome and error prone. 64 * 65 * On the other hand, these three kinds of objects almost always appear together, 66 * and it is in fact very rare for deal.II application codes to do anything with 67 * quadrature, finite element, or mapping objects besides using them together. 68 * For this reason, deal.II uses the FEValues abstraction 69 * combining information on the shape functions, the geometry of the actual mesh 70 * cell and a quadrature rule on a reference cell. Upon construction it takes one 71 * object of each of the three mentioned categories. Later, it can be 72 * "re-initialized" for a concrete grid cell and then provides mapped quadrature 73 * points and weights, mapped shape function values and derivatives as well as 74 * some properties of the transformation from the reference cell to the actual 75 * mesh cell. 76 * 77 * Since computation of any of these values is potentially expensive (for 78 * example when using high order mappings with high order elements), the 79 * FEValues class only computes what it is explicitly asked for. To this 80 * end, it takes a list of flags of type UpdateFlags at construction time 81 * specifying which quantities should be updated each time a cell is 82 * visited. In the case above, you want the gradients of the shape functions 83 * on the real cell, which is encoded by the flag <code>update_gradients</code>, 84 * as well as the product of the determinant of the Jacobian times the 85 * quadrature weight, which is mnemonically encoded using the 86 * term <code>JxW</code> and encoded in the flag <code>update_JxW_values</code>. 87 * Because these flags are represented by single bits in integer numbers, 88 * producing a <i>set of flags</i> amounts to setting multiple bits 89 * in an integer, which is facilitated using the operation 90 * <code>update_gradients | update_JxW_values</code> (in other words, and 91 * maybe slightly confusingly so, the operation @"this operation <i>and</i> that 92 * operation@" is represented by the expression @"single-bit-in-an-integer-for-this-operation 93 * <i>binary-or</i> single-bit-in-an-integer-for-that-operation@"). To 94 * make operations cheaper, FEValues and the mapping and finite element objects 95 * it depends on really only compute those pieces of information that you 96 * have specified in the update flags (plus some information necessary to 97 * compute what has been specified, see below), and not everything that 98 * could possibly be computed on a cell. This optimization makes it much 99 * cheaper to iterate over cells for assembly, but it also means that one 100 * should take care to provide the minimal set of flags possible. 101 * 102 * In addition, once you pass a set of flags for what you want, the functions 103 * filling the data fields of FEValues are able to distinguish between 104 * values that have to be recomputed on each cell (for example mapped 105 * gradients) and quantities that do not change from cell to cell (for 106 * example the values of shape functions of the usual $Q_p$ 107 * finite elements at the same quadrature points on different cells; this 108 * property does not hold for the shape functions of Raviart-Thomas 109 * elements, however, which must be rotated with the local cell). 110 * This allows further optimization of the computations underlying assembly. 111 * 112 * 113 * <h2> Tracking dependencies </h2> 114 * 115 * Let's say you want to compute the Laplace matrix as shown above. In that 116 * case, you need to specify the <code>update_gradients</code> flag 117 * (for $\nabla\varphi_i(\bf x_q)$) and the <code>update_JxW_values</code> 118 * flag (for computing $|\textrm{det}\; J(\bf x_q)|w_q$). Internally, however, 119 * the finite element requires the computation of the inverse of the full 120 * Jacobian matrix, $J^{-1}(\bf x_q)$ (and not just the determinant of the matrix), 121 * and to compute the inverse of the Jacobian, it is also necessary to compute 122 * the Jacobian matrix first. 123 * 124 * Since these are requirements that are not important to the user, it 125 * is not necessary to specify this in user code. Rather, given a set 126 * of update flags, the FEValues object first asks the finite element 127 * object what information it needs to compute in order to satisfy the 128 * user's request provided in the update flags. The finite element 129 * object may therefore add other flags to the update flags (e.g., in 130 * the example above, an FE_Q object would add 131 * <code>update_covariant_transformation</code> to the list, since 132 * that is the necessary transformation from 133 * $\hat\nabla\hat\varphi_i(\hat{\bf x}_q)$ to $\nabla\varphi_i(\bf 134 * x_q)$). With these updated flags, FEValues then asks the mapping 135 * whether it also wants to add more flags to the list to satisfy the 136 * needs of both the user and the finite element object, by calling 137 * Mapping::requires_update_flags(). (This procedure of first asking 138 * the finite element and then the mapping does not have to be 139 * iterated because mappings never require information computed by the 140 * finite element classes, while finite element classes typically need 141 * information computed by mappings.) Using this final list, the 142 * FEValues object then asks both the finite element object and 143 * mapping object to create temporary structures into which they can 144 * store some temporary information that can be computed once and for 145 * all, and these flags will be used when re-computing data on each 146 * cell we will visit later on. 147 * 148 * 149 * <h2>Update once or each</h2> 150 * 151 * As outlined above, we have now determined the final set of things that are 152 * necessary to satisfy a user's desired pieces of information as conveyed by 153 * the update flags they provided. This information will then typically be queried 154 * on every cell the user code visits in a subsequent integration loop. 155 * 156 * Given that many of the things mappings or finite element classes compute are 157 * potentially expensive, FEValues employs a system whereby mappings and finite 158 * element objects are encouraged to pre-compute information that can be computed 159 * once without reference to a concrete cell, and make use of this when asked 160 * to visit a particular cell of the mesh. An example is that the values of 161 * the shape functions of the common FE_Q element are defined on the reference 162 * cell, and the values on the actual cell are simply exactly the values on 163 * the reference cell -- there is consequently no need to evaluate shape functions 164 * on every cell, but it is sufficient to do this once at the beginning, store 165 * the values somewhere, and when visiting a concrete cell simply copying these 166 * values from their temporary location to the output structure. (Note, however, 167 * that this is specific to the FE_Q element: this is not the case if we used 168 * a FE_RaviartThomas element, since there, 169 * computing the values of the shape functions on a cell involves knowing the 170 * Jacobian of the mapping which depends on the geometry of the cell we visit; 171 * thus, for this element, simply copying pre-computed information is not 172 * sufficient to evaluate the values of shape functions on a particular cell.) 173 * 174 * To accommodate this structure, both mappings and finite element classes 175 * may internally split the update flags into two sets commonly referenced as 176 * <code>update_once</code> and <code>update_each</code> (though these names 177 * do not appear in any public interfaces). The former contains 178 * all those pieces of information that can be pre-computed once at the 179 * time the FEValues object starts to interact with a mapping or 180 * finite element, whereas the latter contains those flags corresponding to 181 * things that need to be computed on every cell. For example, if 182 * <code>update_flags=update_values</code>, then the FE_Q class will 183 * set <code>update_once=update_values</code> and 184 * <code>update_each=0</code>, whereas the Raviart-Thomas element will 185 * do it the other way around. 186 * 187 * These sets of flags are intended to be mutually exclusive. There is, 188 * on the other hand, nothing that ever provides this decomposition to 189 * anything outside the mapping or finite element classes -- it is a purely 190 * internal decomposition. 191 * 192 * 193 * <h2>Generation of the actual data</h2> 194 * 195 * As outlined above, data is computed at two different times: once at 196 * the beginning on the reference cell, and once whenever we move to an 197 * actual cell. The functions involved in each of these steps are 198 * discussed next: 199 * 200 * 201 * <h3>Initialization</h3> 202 * 203 * Computing data on the reference cell before we even visit the first 204 * real cell is a two-step process. First, the constructor of FEValues, 205 * FEFaceValues and FESubfaceValues, respectively, need to allow the 206 * Mapping and FiniteElement objects to set up internal data 207 * structures. These structures are internal in the following sense: the 208 * FEValues object asks the finite element and mapping objects to create 209 * an object of type FiniteElement::InternalDataBase and 210 * Mapping::InternalDataBase each; the actual finite element and mapping 211 * class may in fact create objects of a derived type if they wish to 212 * store some data beyond what these base classes already provide. The 213 * functions involved in this are 214 * <ul> 215 * <li>Mapping::get_data() 216 * <li>Mapping::get_face_data() 217 * <li>Mapping::get_subface_data() 218 * <li>FiniteElement::get_data() 219 * <li>FiniteElement::get_face_data() 220 * <li>FiniteElement::get_subface_data() 221 * </ul> 222 * 223 * The FEValues object then takes over ownership of these objects and will 224 * destroy them at the end of the FEValues object's lifetime. After this, 225 * the FEValues object asks the FiniteElement and Mapping objects to fill 226 * these InternalDataBase objects with the data that pertains to what 227 * can and needs to be computed on the reference cell. This is done in these 228 * functions: 229 * <ul> 230 * <li>FEValues::initialize() 231 * <li>FEFaceValues::initialize() 232 * <li>FESubfaceValues::initialize() 233 * </ul> 234 * 235 * 236 * <h3>Reinitialization for a mesh cell</h3> 237 * 238 * Once initialization is over and we call FEValues::reinit, FEFaceValues::reinit 239 * or FESubfaceValues::reinit to move to a concrete cell or face, we need 240 * to calculate the "update_each" kinds of data. This is done in the following 241 * functions: 242 * <ul> 243 * <li>FEValues::reinit() calls Mapping::fill_fe_values(), then FiniteElement::fill_fe_values() 244 * <li>FEFaceValues::reinit() calls Mapping::fill_fe_face_values(), then FiniteElement::fill_fe_face_values() 245 * <li>FESubfaceValues::reinit() calls Mapping::fill_fe_subface_values(), 246 * then FiniteElement::fill_fe_subface_values() 247 * </ul> 248 * 249 * This is where the actual data fields for FEValues, stored in 250 * internal::FEValues::MappingRelatedData and 251 * internal::FEValues::FiniteElementRelatedData objects, are 252 * computed. These functions call the function in Mapping first, such 253 * that all the mapping data required by the finite element is 254 * available. Then, the FiniteElement function is called. 255 * 256 * @ingroup feall 257 */ 258