| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2014 - 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_data_on_the_fly_h |
| 17 |
#define dealii_matrix_free_mapping_data_on_the_fly_h |
| 18 |
|
| 19 |
|
| 20 |
#include <deal.II/base/config.h> |
| 21 |
|
| 22 |
#include <deal.II/base/aligned_vector.h> |
| 23 |
#include <deal.II/base/enable_observer_pointer.h> |
| 24 |
#include <deal.II/base/exceptions.h> |
| 25 |
#include <deal.II/base/vectorization.h> |
| 26 |
|
| 27 |
#include <deal.II/fe/fe_nothing.h> |
| 28 |
#include <deal.II/fe/fe_values.h> |
| 29 |
#include <deal.II/fe/mapping_q1.h> |
| 30 |
|
| 31 |
#include <deal.II/matrix_free/mapping_info.h> |
| 32 |
#include <deal.II/matrix_free/shape_info.h> |
| 33 |
|
| 34 |
|
| 35 |
DEAL_II_NAMESPACE_OPEN |
| 36 |
|
| 37 |
|
| 38 |
namespace internal |
| 39 |
{ |
| 40 |
namespace MatrixFreeFunctions |
| 41 |
{ |
| 42 |
/** |
| 43 |
* This class provides evaluated mapping information using standard |
| 44 |
* deal.II information in a form that FEEvaluation and friends can use for |
| 45 |
* vectorized access. Since no vectorization over cells is available with |
| 46 |
* the DoFHandler/Triangulation cell iterators, the interface to |
| 47 |
* FEEvaluation's vectorization model is to use VectorizedArray::size() |
| 48 |
* copies of the same element. This interface is thus primarily useful for |
| 49 |
* evaluating several operators on the same cell, e.g., when assembling |
| 50 |
* cell matrices. |
| 51 |
* |
| 52 |
* As opposed to the Mapping classes in deal.II, this class does not |
| 53 |
* actually provide a boundary description that can be used to evaluate |
| 54 |
* the geometry, but it rather provides the evaluated geometry from a |
| 55 |
* given deal.II mapping (as passed to the constructor of this class) in a |
| 56 |
* form accessible to FEEvaluation. |
| 57 |
*/ |
| 58 |
template <int dim, typename Number> |
| 59 |
class MappingDataOnTheFly |
| 60 |
{ |
| 61 |
public: |
| 62 |
/** |
| 63 |
* Default constructor. |
| 64 |
*/ |
| 65 |
MappingDataOnTheFly() = default; |
| 66 |
|
| 67 |
/** |
| 68 |
* Constructor, similar to FEValues. Since this class only evaluates the |
| 69 |
* geometry, no finite element has to be specified and the simplest |
| 70 |
* element, FE_Nothing, is used internally for the underlying FEValues |
| 71 |
* object. |
| 72 |
*/ |
| 73 |
MappingDataOnTheFly(const Mapping<dim> &mapping, |
| 74 |
const Quadrature<1> &quadrature, |
| 75 |
const UpdateFlags update_flags); |
| 76 |
|
| 77 |
/** |
| 78 |
* Constructor. This constructor is equivalent to the other one except |
| 79 |
* that it makes the object use a $Q_1$ mapping (i.e., an object of type |
| 80 |
* MappingQ(1)) implicitly. |
| 81 |
*/ |
| 82 |
MappingDataOnTheFly(const Quadrature<1> &quadrature, |
| 83 |
const UpdateFlags update_flags); |
| 84 |
|
| 85 |
/** |
| 86 |
* Initialize with the given cell iterator. |
| 87 |
*/ |
| 88 |
void |
| 89 |
reinit(typename dealii::Triangulation<dim>::cell_iterator cell); |
| 90 |
|
| 91 |
/** |
| 92 |
* Return whether reinit() has been called at least once, i.e., a cell |
| 93 |
* has been set. |
| 94 |
*/ |
| 95 |
bool |
| 96 |
is_initialized() const; |
| 97 |
|
| 98 |
/** |
| 99 |
* Return a triangulation iterator to the current cell. |
| 100 |
*/ |
| 101 |
typename dealii::Triangulation<dim>::cell_iterator |
| 102 |
get_cell() const; |
| 103 |
|
| 104 |
/** |
| 105 |
* Return a reference to the underlying FEValues object that evaluates |
| 106 |
* certain quantities (only mapping-related ones like Jacobians or |
| 107 |
* mapped quadrature points are accessible, as no finite element data is |
| 108 |
* actually used). |
| 109 |
*/ |
| 110 |
const dealii::FEValues<dim> & |
| 111 |
get_fe_values() const; |
| 112 |
|
| 113 |
/** |
| 114 |
* Return a reference to the underlying storage field of type |
| 115 |
* MappingInfoStorage of the same format as the data fields in |
| 116 |
* MappingInfo. This ensures compatibility with the precomputed data |
| 117 |
* fields in the MappingInfo class. |
| 118 |
*/ |
| 119 |
const MappingInfoStorage<dim, dim, Number> & |
| 120 |
get_data_storage() const; |
| 121 |
|
| 122 |
/** |
| 123 |
* Return a non-const reference to the underlying storage field |
| 124 |
* of type MappingInfoStorage of the same format as the data fields |
| 125 |
* in MappingInfo. This function can be used to manually fill the |
| 126 |
* data if the default constructor has been called and reinit() was |
| 127 |
* not called for a given cell. |
| 128 |
*/ |
| 129 |
MappingInfoStorage<dim, dim, Number> & |
| 130 |
get_data_storage(); |
| 131 |
|
| 132 |
/** |
| 133 |
* Return a reference to 1d quadrature underlying this object. |
| 134 |
*/ |
| 135 |
const Quadrature<1> & |
| 136 |
get_quadrature() const; |
| 137 |
|
| 138 |
private: |
| 139 |
/** |
| 140 |
* A cell iterator in case we generate the data on the fly to be able to |
| 141 |
* check if we need to re-generate the information stored in this class. |
| 142 |
*/ |
| 143 |
typename dealii::Triangulation<dim>::cell_iterator present_cell; |
| 144 |
|
| 145 |
/** |
| 146 |
* Dummy finite element object necessary for initializing the FEValues |
| 147 |
* object. |
| 148 |
*/ |
| 149 |
std::unique_ptr<FE_Nothing<dim>> fe_dummy; |
| 150 |
|
| 151 |
/** |
| 152 |
* An underlying FEValues object that performs the (scalar) evaluation. |
| 153 |
*/ |
| 154 |
std::unique_ptr<dealii::FEValues<dim>> fe_values; |
| 155 |
|
| 156 |
/** |
| 157 |
* Get 1d quadrature formula to be used for reinitializing shape info. |
| 158 |
*/ |
| 159 |
const Quadrature<1> quadrature_1d; |
| 160 |
|
| 161 |
/** |
| 162 |
* The storage part created for a single cell and held in analogy to |
| 163 |
* MappingInfo. |
| 164 |
*/ |
| 165 |
MappingInfoStorage<dim, dim, Number> mapping_info_storage; |
| 166 |
}; |
| 167 |
|
| 168 |
|
| 169 |
/*-------------------------- Inline functions ---------------------------*/ |
| 170 |
|
| 171 |
template <int dim, typename Number> |
| 172 |
inline MappingDataOnTheFly<dim, Number>::MappingDataOnTheFly( |
| 173 |
const Mapping<dim> &mapping, |
| 174 |
const Quadrature<1> &quadrature, |
| 175 |
const UpdateFlags update_flags) |
| 176 |
: fe_dummy(std::make_unique<FE_Nothing<dim>>()) |
| 177 |
, fe_values(std::make_unique<dealii::FEValues<dim>>( |
| 178 |
mapping, |
| 179 |
*fe_dummy, |
| 180 |
Quadrature<dim>(quadrature), |
| 181 |
MappingInfoStorage<dim, dim, Number>::compute_update_flags( |
| 182 |
update_flags))) |
| 183 |
, quadrature_1d(quadrature) |
| 184 |
{ |
| 185 |
mapping_info_storage.descriptor.resize(1); |
| 186 |
mapping_info_storage.descriptor[0].initialize(quadrature); |
| 187 |
mapping_info_storage.data_index_offsets.resize(1); |
| 188 |
mapping_info_storage.JxW_values.resize(fe_values->n_quadrature_points); |
| 189 |
mapping_info_storage.jacobians[0].resize(fe_values->n_quadrature_points); |
| 190 |
if (update_flags & update_quadrature_points) |
| 191 |
{ |
| 192 |
mapping_info_storage.quadrature_point_offsets.resize(1, 0); |
| 193 |
mapping_info_storage.quadrature_points.resize( |
| 194 |
fe_values->n_quadrature_points); |
| 195 |
} |
| 196 |
if (fe_values->get_update_flags() & update_normal_vectors) |
| 197 |
{ |
| 198 |
mapping_info_storage.normal_vectors.resize( |
| 199 |
fe_values->n_quadrature_points); |
| 200 |
mapping_info_storage.normals_times_jacobians[0].resize( |
| 201 |
fe_values->n_quadrature_points); |
| 202 |
} |
| 203 |
Assert(!(fe_values->get_update_flags() & update_jacobian_grads), |
| 204 |
ExcNotImplemented()); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
template <int dim, typename Number> |
| 210 |
inline MappingDataOnTheFly<dim, Number>::MappingDataOnTheFly( |
| 211 |
const Quadrature<1> &quadrature, |
| 212 |
const UpdateFlags update_flags) |
| 213 |
: MappingDataOnTheFly(::dealii::StaticMappingQ1<dim, dim>::mapping, |
| 214 |
quadrature, |
| 215 |
update_flags) |
| 216 |
{} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
template <int dim, typename Number> |
| 221 |
inline void |
| 222 |
MappingDataOnTheFly<dim, Number>::reinit( |
| 223 |
typename dealii::Triangulation<dim>::cell_iterator cell) |
| 224 |
{ |
| 225 |
if (present_cell == cell) |
| 226 |
return; |
| 227 |
present_cell = cell; |
| 228 |
fe_values->reinit(present_cell); |
| 229 |
for (unsigned int q = 0; q < fe_values->get_quadrature().size(); ++q) |
| 230 |
{ |
| 231 |
if (fe_values->get_update_flags() & update_JxW_values) |
| 232 |
mapping_info_storage.JxW_values[q] = fe_values->JxW(q); |
| 233 |
if (fe_values->get_update_flags() & update_jacobians) |
| 234 |
{ |
| 235 |
Tensor<2, dim> jac = fe_values->jacobian(q); |
| 236 |
jac = invert(transpose(jac)); |
| 237 |
for (unsigned int d = 0; d < dim; ++d) |
| 238 |
for (unsigned int e = 0; e < dim; ++e) |
| 239 |
mapping_info_storage.jacobians[0][q][d][e] = jac[d][e]; |
| 240 |
} |
| 241 |
if (fe_values->get_update_flags() & update_quadrature_points) |
| 242 |
for (unsigned int d = 0; d < dim; ++d) |
| 243 |
mapping_info_storage.quadrature_points[q][d] = |
| 244 |
fe_values->quadrature_point(q)[d]; |
| 245 |
if (fe_values->get_update_flags() & update_normal_vectors) |
| 246 |
{ |
| 247 |
for (unsigned int d = 0; d < dim; ++d) |
| 248 |
mapping_info_storage.normal_vectors[q][d] = |
| 249 |
fe_values->normal_vector(q)[d]; |
| 250 |
mapping_info_storage.normals_times_jacobians[0][q] = |
| 251 |
mapping_info_storage.normal_vectors[q] * |
| 252 |
mapping_info_storage.jacobians[0][q]; |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
template <int dim, typename Number> |
| 260 |
inline bool |
| 261 |
MappingDataOnTheFly<dim, Number>::is_initialized() const |
| 262 |
{ |
| 263 |
return present_cell != |
| 264 |
typename dealii::Triangulation<dim>::cell_iterator(); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
template <int dim, typename Number> |
| 270 |
inline typename dealii::Triangulation<dim>::cell_iterator |
| 271 |
MappingDataOnTheFly<dim, Number>::get_cell() const |
| 272 |
{ |
| 273 |
return fe_values->get_cell(); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
template <int dim, typename Number> |
| 279 |
inline const dealii::FEValues<dim> & |
| 280 |
MappingDataOnTheFly<dim, Number>::get_fe_values() const |
| 281 |
{ |
| 282 |
return *fe_values; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
template <int dim, typename Number> |
| 288 |
inline const MappingInfoStorage<dim, dim, Number> & |
| 289 |
MappingDataOnTheFly<dim, Number>::get_data_storage() const |
| 290 |
{ |
| 291 |
return mapping_info_storage; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
template <int dim, typename Number> |
| 297 |
inline MappingInfoStorage<dim, dim, Number> & |
| 298 |
MappingDataOnTheFly<dim, Number>::get_data_storage() |
| 299 |
{ |
| 300 |
return mapping_info_storage; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
template <int dim, typename Number> |
| 306 |
inline const Quadrature<1> & |
| 307 |
MappingDataOnTheFly<dim, Number>::get_quadrature() const |
| 308 |
{ |
| 309 |
return quadrature_1d; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
} // end of namespace MatrixFreeFunctions |
| 314 |
} // end of namespace internal |
| 315 |
|
| 316 |
|
| 317 |
DEAL_II_NAMESPACE_CLOSE |
| 318 |
|
| 319 |
#endif |