| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2012 - 2024 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
|
| 16 |
#ifndef dealii_matrix_free_mapping_info_h |
| 17 |
#define dealii_matrix_free_mapping_info_h |
| 18 |
|
| 19 |
|
| 20 |
#include <deal.II/base/config.h> |
| 21 |
|
| 22 |
#include <deal.II/base/aligned_vector.h> |
| 23 |
#include <deal.II/base/exceptions.h> |
| 24 |
#include <deal.II/base/vectorization.h> |
| 25 |
|
| 26 |
#include <deal.II/fe/fe.h> |
| 27 |
#include <deal.II/fe/mapping.h> |
| 28 |
|
| 29 |
#include <deal.II/grid/reference_cell.h> |
| 30 |
|
| 31 |
#include <deal.II/hp/mapping_collection.h> |
| 32 |
#include <deal.II/hp/q_collection.h> |
| 33 |
|
| 34 |
#include <deal.II/matrix_free/face_info.h> |
| 35 |
#include <deal.II/matrix_free/mapping_info_storage.h> |
| 36 |
|
| 37 |
#include <memory> |
| 38 |
|
| 39 |
|
| 40 |
DEAL_II_NAMESPACE_OPEN |
| 41 |
|
| 42 |
|
| 43 |
namespace internal |
| 44 |
{ |
| 45 |
namespace MatrixFreeFunctions |
| 46 |
{ |
| 47 |
/** |
| 48 |
* The class that stores all geometry-dependent data related with cell |
| 49 |
* interiors for use in the matrix-free class. |
| 50 |
* |
| 51 |
* @ingroup matrixfree |
| 52 |
*/ |
| 53 |
template <int dim, typename Number, typename VectorizedArrayType> |
| 54 |
struct MappingInfo |
| 55 |
{ |
| 56 |
/** |
| 57 |
* Compute the information in the given cells and faces. The cells are |
| 58 |
* specified by the level and the index within the level (as given by |
| 59 |
* CellIterator::level() and CellIterator::index(), in order to allow |
| 60 |
* for different kinds of iterators, e.g. standard DoFHandler, |
| 61 |
* multigrid, etc.) on a fixed Triangulation. In addition, a mapping |
| 62 |
* and several 1d quadrature formulas are given. |
| 63 |
*/ |
| 64 |
void |
| 65 |
initialize( |
| 66 |
const dealii::Triangulation<dim> &tria, |
| 67 |
const std::vector<std::pair<unsigned int, unsigned int>> &cells, |
| 68 |
const FaceInfo<VectorizedArrayType::size()> &face_info, |
| 69 |
const std::vector<unsigned int> &active_fe_index, |
| 70 |
const std::shared_ptr<dealii::hp::MappingCollection<dim>> &mapping, |
| 71 |
const std::vector<dealii::hp::QCollection<dim>> &quad, |
| 72 |
const UpdateFlags update_flags_cells, |
| 73 |
const UpdateFlags update_flags_boundary_faces, |
| 74 |
const UpdateFlags update_flags_inner_faces, |
| 75 |
const UpdateFlags update_flags_faces_by_cells, |
| 76 |
const bool piola_transform); |
| 77 |
|
| 78 |
/** |
| 79 |
* Update the information in the given cells and faces that is the |
| 80 |
* result of a change in the given `mapping` class, keeping the cells, |
| 81 |
* quadrature formulas and other unknowns unchanged. This call is only |
| 82 |
* valid if MappingInfo::initialize() has been called before. |
| 83 |
*/ |
| 84 |
void |
| 85 |
update_mapping( |
| 86 |
const dealii::Triangulation<dim> &tria, |
| 87 |
const std::vector<std::pair<unsigned int, unsigned int>> &cells, |
| 88 |
const FaceInfo<VectorizedArrayType::size()> &face_info, |
| 89 |
const std::vector<unsigned int> &active_fe_index, |
| 90 |
const std::shared_ptr<dealii::hp::MappingCollection<dim>> &mapping); |
| 91 |
|
| 92 |
/** |
| 93 |
* Return the type of a given cell as detected during initialization. |
| 94 |
*/ |
| 95 |
GeometryType |
| 96 |
get_cell_type(const unsigned int cell_chunk_no) const; |
| 97 |
|
| 98 |
/** |
| 99 |
* Clear all data fields in this class. |
| 100 |
*/ |
| 101 |
void |
| 102 |
clear(); |
| 103 |
|
| 104 |
/** |
| 105 |
* Return the memory consumption of this class in bytes. |
| 106 |
*/ |
| 107 |
std::size_t |
| 108 |
memory_consumption() const; |
| 109 |
|
| 110 |
/** |
| 111 |
* Prints a detailed summary of memory consumption in the different |
| 112 |
* structures of this class to the given output stream. |
| 113 |
*/ |
| 114 |
template <typename StreamType> |
| 115 |
void |
| 116 |
print_memory_consumption(StreamType &out, |
| 117 |
const TaskInfo &task_info) const; |
| 118 |
|
| 119 |
/** |
| 120 |
* The given update flags for computing the geometry on the cells. |
| 121 |
*/ |
| 122 |
UpdateFlags update_flags_cells; |
| 123 |
|
| 124 |
/** |
| 125 |
* The given update flags for computing the geometry on the boundary |
| 126 |
* faces. |
| 127 |
*/ |
| 128 |
UpdateFlags update_flags_boundary_faces; |
| 129 |
|
| 130 |
/** |
| 131 |
* The given update flags for computing the geometry on the interior |
| 132 |
* faces. |
| 133 |
*/ |
| 134 |
UpdateFlags update_flags_inner_faces; |
| 135 |
|
| 136 |
/** |
| 137 |
* The given update flags for computing the geometry on the faces for |
| 138 |
* cell-centric loops. |
| 139 |
*/ |
| 140 |
UpdateFlags update_flags_faces_by_cells; |
| 141 |
|
| 142 |
/** |
| 143 |
* Stores whether a cell is Cartesian (cell type 0), has constant |
| 144 |
* transform data (Jacobians) (cell type 1), or is general (cell type |
| 145 |
* 3). Type 2 is only used for faces and no cells are assigned this |
| 146 |
* value. |
| 147 |
*/ |
| 148 |
std::vector<GeometryType> cell_type; |
| 149 |
|
| 150 |
/** |
| 151 |
* Stores whether a face (and both cells adjacent to the face) is |
| 152 |
* Cartesian (face type 0), whether it represents an affine situation |
| 153 |
* (face type 1), whether it is a flat face where the normal vector is |
| 154 |
* the same throughout the face (face type 2), or is general (face type |
| 155 |
* 3). |
| 156 |
*/ |
| 157 |
std::vector<GeometryType> face_type; |
| 158 |
|
| 159 |
/** |
| 160 |
* Store whether the face geometry for the face_data_by_cells data |
| 161 |
* structure is represented as Cartesian (cell type 0), with constant |
| 162 |
* transform data (Jacobians) (cell type 1), or a general type (cell |
| 163 |
* type 3). Note that both the interior and exterior agree on the type |
| 164 |
* of the data structure, using the more general of the two. |
| 165 |
*/ |
| 166 |
std::vector<std::array<GeometryType, GeometryInfo<dim>::faces_per_cell>> |
| 167 |
faces_by_cells_type; |
| 168 |
|
| 169 |
/** |
| 170 |
* The data cache for the cells. |
| 171 |
*/ |
| 172 |
std::vector<MappingInfoStorage<dim, dim, VectorizedArrayType>> cell_data; |
| 173 |
|
| 174 |
/** |
| 175 |
* The data cache for the faces. |
| 176 |
*/ |
| 177 |
std::vector<MappingInfoStorage<dim - 1, dim, VectorizedArrayType>> |
| 178 |
face_data; |
| 179 |
|
| 180 |
/** |
| 181 |
* The data cache for the face-associated-with-cell topology, following |
| 182 |
* the @p cell_type variable for the cell types. |
| 183 |
*/ |
| 184 |
std::vector<MappingInfoStorage<dim - 1, dim, VectorizedArrayType>> |
| 185 |
face_data_by_cells; |
| 186 |
|
| 187 |
/** |
| 188 |
* The pointer to the underlying hp::MappingCollection object. |
| 189 |
*/ |
| 190 |
std::shared_ptr<dealii::hp::MappingCollection<dim>> mapping_collection; |
| 191 |
|
| 192 |
/** |
| 193 |
* The pointer to the first entry of mapping_collection. |
| 194 |
*/ |
| 195 |
ObserverPointer<const Mapping<dim>> mapping; |
| 196 |
|
| 197 |
/** |
| 198 |
* Reference-cell type related to each quadrature and active quadrature |
| 199 |
* index. |
| 200 |
*/ |
| 201 |
std::vector<std::vector<ReferenceCell>> reference_cell_types; |
| 202 |
|
| 203 |
/** |
| 204 |
* Internal function to compute the geometry for the case the mapping is |
| 205 |
* a MappingQ and a single quadrature formula per slot (non-hp-case) is |
| 206 |
* used. This method computes all data from the underlying cell |
| 207 |
* quadrature points using the fast operator evaluation techniques from |
| 208 |
* the matrix-free framework itself, i.e., it uses a polynomial |
| 209 |
* description of the cell geometry (that is computed in a first step) |
| 210 |
* and then computes all Jacobians and normal vectors based on this |
| 211 |
* information. This optimized approach is much faster than going |
| 212 |
* through FEValues and FEFaceValues, especially when several different |
| 213 |
* quadrature formulas are involved, and consumes less memory. |
| 214 |
* |
| 215 |
* @param tria The triangulation to be used for setup |
| 216 |
* |
| 217 |
* @param cells The actual cells of the triangulation to be worked on, |
| 218 |
* given as a tuple of the level and index within the level as used in |
| 219 |
* the main initialization of the class |
| 220 |
* |
| 221 |
* @param face_info The description of the connectivity from faces to |
| 222 |
* cells as filled in the MatrixFree class |
| 223 |
*/ |
| 224 |
void |
| 225 |
compute_mapping_q( |
| 226 |
const dealii::Triangulation<dim> &tria, |
| 227 |
const std::vector<std::pair<unsigned int, unsigned int>> &cells, |
| 228 |
const FaceInfo<VectorizedArrayType::size()> &face_info); |
| 229 |
|
| 230 |
/** |
| 231 |
* Computes the information in the given cells, called within |
| 232 |
* initialize. |
| 233 |
*/ |
| 234 |
void |
| 235 |
initialize_cells( |
| 236 |
const dealii::Triangulation<dim> &tria, |
| 237 |
const std::vector<std::pair<unsigned int, unsigned int>> &cells, |
| 238 |
const std::vector<unsigned int> &active_fe_index, |
| 239 |
const dealii::hp::MappingCollection<dim> &mapping); |
| 240 |
|
| 241 |
/** |
| 242 |
* Computes the information in the given faces, called within |
| 243 |
* initialize. |
| 244 |
*/ |
| 245 |
void |
| 246 |
initialize_faces( |
| 247 |
const dealii::Triangulation<dim> &tria, |
| 248 |
const std::vector<std::pair<unsigned int, unsigned int>> &cells, |
| 249 |
const std::vector<FaceToCellTopology<VectorizedArrayType::size()>> |
| 250 |
&faces, |
| 251 |
const std::vector<unsigned int> &active_fe_index, |
| 252 |
const dealii::hp::MappingCollection<dim> &mapping); |
| 253 |
|
| 254 |
/** |
| 255 |
* Computes the information in the given faces, called within |
| 256 |
* initialize. |
| 257 |
*/ |
| 258 |
void |
| 259 |
initialize_faces_by_cells( |
| 260 |
const dealii::Triangulation<dim> &tria, |
| 261 |
const std::vector<std::pair<unsigned int, unsigned int>> &cells, |
| 262 |
const FaceInfo<VectorizedArrayType::size()> &face_info, |
| 263 |
const dealii::hp::MappingCollection<dim> &mapping); |
| 264 |
}; |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
/** |
| 269 |
* A helper class to extract either cell or face data from mapping info |
| 270 |
* for use in FEEvaluationBase. |
| 271 |
*/ |
| 272 |
template <int, typename, bool, typename> |
| 273 |
struct MappingInfoCellsOrFaces; |
| 274 |
|
| 275 |
template <int dim, typename Number, typename VectorizedArrayType> |
| 276 |
struct MappingInfoCellsOrFaces<dim, Number, false, VectorizedArrayType> |
| 277 |
{ |
| 278 |
static const MappingInfoStorage<dim, dim, VectorizedArrayType> & |
| 279 |
get(const MappingInfo<dim, Number, VectorizedArrayType> &mapping_info, |
| 280 |
const unsigned int quad_no) |
| 281 |
{ |
| 282 |
AssertIndexRange(quad_no, mapping_info.cell_data.size()); |
| 283 |
return mapping_info.cell_data[quad_no]; |
| 284 |
} |
| 285 |
}; |
| 286 |
|
| 287 |
template <int dim, typename Number, typename VectorizedArrayType> |
| 288 |
struct MappingInfoCellsOrFaces<dim, Number, true, VectorizedArrayType> |
| 289 |
{ |
| 290 |
static const MappingInfoStorage<dim - 1, dim, VectorizedArrayType> & |
| 291 |
get(const MappingInfo<dim, Number, VectorizedArrayType> &mapping_info, |
| 292 |
const unsigned int quad_no) |
| 293 |
{ |
| 294 |
AssertIndexRange(quad_no, mapping_info.face_data.size()); |
| 295 |
return mapping_info.face_data[quad_no]; |
| 296 |
} |
| 297 |
}; |
| 298 |
|
| 299 |
|
| 300 |
/* ------------------- inline functions ----------------------------- */ |
| 301 |
|
| 302 |
template <int dim, typename Number, typename VectorizedArrayType> |
| 303 |
inline GeometryType |
| 304 |
MappingInfo<dim, Number, VectorizedArrayType>::get_cell_type( |
| 305 |
const unsigned int cell_no) const |
| 306 |
{ |
| 307 |
AssertIndexRange(cell_no, cell_type.size()); |
| 308 |
return cell_type[cell_no]; |
| 309 |
} |
| 310 |
|
| 311 |
} // end of namespace MatrixFreeFunctions |
| 312 |
} // end of namespace internal |
| 313 |
|
| 314 |
DEAL_II_NAMESPACE_CLOSE |
| 315 |
|
| 316 |
#endif |