| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2012 - 2025 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 |
#ifndef dealii_matrix_free_shape_info_templates_h |
| 16 |
#define dealii_matrix_free_shape_info_templates_h |
| 17 |
|
| 18 |
|
| 19 |
#include <deal.II/base/config.h> |
| 20 |
|
| 21 |
#include <deal.II/base/memory_consumption.h> |
| 22 |
#include <deal.II/base/polynomial.h> |
| 23 |
#include <deal.II/base/polynomials_piecewise.h> |
| 24 |
#include <deal.II/base/qprojector.h> |
| 25 |
#include <deal.II/base/tensor_product_polynomials.h> |
| 26 |
#include <deal.II/base/utilities.h> |
| 27 |
|
| 28 |
#include <deal.II/fe/fe.h> |
| 29 |
#include <deal.II/fe/fe_dgp.h> |
| 30 |
#include <deal.II/fe/fe_dgq.h> |
| 31 |
#include <deal.II/fe/fe_nothing.h> |
| 32 |
#include <deal.II/fe/fe_poly.h> |
| 33 |
#include <deal.II/fe/fe_pyramid_p.h> |
| 34 |
#include <deal.II/fe/fe_q.h> |
| 35 |
#include <deal.II/fe/fe_q_dg0.h> |
| 36 |
#include <deal.II/fe/fe_q_iso_q1.h> |
| 37 |
#include <deal.II/fe/fe_raviart_thomas.h> |
| 38 |
#include <deal.II/fe/fe_simplex_p.h> |
| 39 |
#include <deal.II/fe/fe_simplex_p_bubbles.h> |
| 40 |
#include <deal.II/fe/fe_tools.h> |
| 41 |
#include <deal.II/fe/fe_wedge_p.h> |
| 42 |
|
| 43 |
#include <deal.II/grid/reference_cell.h> |
| 44 |
|
| 45 |
#include <deal.II/lac/householder.h> |
| 46 |
|
| 47 |
#include <deal.II/matrix_free/shape_info.h> |
| 48 |
#include <deal.II/matrix_free/util.h> |
| 49 |
|
| 50 |
|
| 51 |
DEAL_II_NAMESPACE_OPEN |
| 52 |
|
| 53 |
|
| 54 |
namespace internal |
| 55 |
{ |
| 56 |
namespace MatrixFreeFunctions |
| 57 |
{ |
| 58 |
template <typename Number> |
| 59 |
UnivariateShapeData<Number>::UnivariateShapeData() |
| 60 |
: element_type(tensor_general) |
| 61 |
, fe_degree(0) |
| 62 |
, n_q_points_1d(0) |
| 63 |
, nodal_at_cell_boundaries(false) |
| 64 |
{} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
template <typename Number> |
| 69 |
Number |
| 70 |
get_first_array_element(const Number a) |
| 71 |
{ |
| 72 |
return a; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
template <typename Number, std::size_t width> |
| 78 |
Number |
| 79 |
get_first_array_element(const VectorizedArray<Number, width> a) |
| 80 |
{ |
| 81 |
return a[0]; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
template <int dim, int spacedim> |
| 87 |
void |
| 88 |
get_element_type_specific_information( |
| 89 |
const FiniteElement<dim, spacedim> &fe_in, |
| 90 |
const FiniteElement<dim, spacedim> &fe, |
| 91 |
const unsigned int base_element_number, |
| 92 |
ElementType &element_type, |
| 93 |
std::vector<unsigned int> &scalar_lexicographic, |
| 94 |
std::vector<unsigned int> &lexicographic_numbering) |
| 95 |
{ |
| 96 |
element_type = tensor_general; |
| 97 |
|
| 98 |
const auto fe_poly = dynamic_cast<const FE_Poly<dim, spacedim> *>(&fe); |
| 99 |
|
| 100 |
if (dynamic_cast<const FE_SimplexPoly<dim, spacedim> *>(&fe) != nullptr || |
| 101 |
dynamic_cast<const FE_WedgePoly<dim, spacedim> *>(&fe) != nullptr || |
| 102 |
dynamic_cast<const FE_PyramidPoly<dim, spacedim> *>(&fe) != nullptr) |
| 103 |
{ |
| 104 |
scalar_lexicographic.resize(fe.n_dofs_per_cell()); |
| 105 |
for (unsigned int i = 0; i < scalar_lexicographic.size(); ++i) |
| 106 |
scalar_lexicographic[i] = i; |
| 107 |
element_type = tensor_none; |
| 108 |
} |
| 109 |
else if (fe_poly != nullptr && |
| 110 |
(dynamic_cast<const TensorProductPolynomials<dim> *>( |
| 111 |
&fe_poly->get_poly_space()) != nullptr || |
| 112 |
dynamic_cast<const TensorProductPolynomials< |
| 113 |
dim, |
| 114 |
Polynomials::PiecewisePolynomial<double>> *>( |
| 115 |
&fe_poly->get_poly_space()) != nullptr)) |
| 116 |
scalar_lexicographic = fe_poly->get_poly_space_numbering_inverse(); |
| 117 |
else if (const auto fe_dgp = |
| 118 |
dynamic_cast<const FE_DGP<dim, spacedim> *>(&fe)) |
| 119 |
{ |
| 120 |
scalar_lexicographic.resize(fe_dgp->n_dofs_per_cell()); |
| 121 |
for (unsigned int i = 0; i < fe_dgp->n_dofs_per_cell(); ++i) |
| 122 |
scalar_lexicographic[i] = i; |
| 123 |
element_type = truncated_tensor; |
| 124 |
} |
| 125 |
else if (const auto fe_q_dg0 = |
| 126 |
dynamic_cast<const FE_Q_DG0<dim, spacedim> *>(&fe)) |
| 127 |
{ |
| 128 |
scalar_lexicographic = fe_q_dg0->get_poly_space_numbering_inverse(); |
| 129 |
element_type = tensor_symmetric_plus_dg0; |
| 130 |
} |
| 131 |
else if (fe.n_dofs_per_cell() == 0) |
| 132 |
{ |
| 133 |
// FE_Nothing case -> nothing to do here |
| 134 |
} |
| 135 |
else |
| 136 |
DEAL_II_NOT_IMPLEMENTED(); |
| 137 |
|
| 138 |
// Finally store the renumbering into the respective field |
| 139 |
if (fe_in.n_components() == 1) |
| 140 |
lexicographic_numbering = scalar_lexicographic; |
| 141 |
else |
| 142 |
{ |
| 143 |
// have more than one component, get the inverse permutation, invert |
| 144 |
// it, sort the components one by one, and invert back |
| 145 |
std::vector<unsigned int> scalar_inv = |
| 146 |
Utilities::invert_permutation(scalar_lexicographic); |
| 147 |
std::vector<unsigned int> lexicographic( |
| 148 |
fe_in.n_dofs_per_cell(), numbers::invalid_unsigned_int); |
| 149 |
unsigned int components_before = 0; |
| 150 |
for (unsigned int e = 0; e < base_element_number; ++e) |
| 151 |
components_before += fe_in.element_multiplicity(e); |
| 152 |
for (unsigned int comp = 0; |
| 153 |
comp < fe_in.element_multiplicity(base_element_number); |
| 154 |
++comp) |
| 155 |
for (unsigned int i = 0; i < scalar_inv.size(); ++i) |
| 156 |
lexicographic[fe_in.component_to_system_index( |
| 157 |
comp + components_before, i)] = |
| 158 |
scalar_inv.size() * comp + scalar_inv[i]; |
| 159 |
|
| 160 |
// invert numbering again. Need to do it manually because we might |
| 161 |
// have undefined blocks |
| 162 |
lexicographic_numbering.resize(fe_in.element_multiplicity( |
| 163 |
base_element_number) * |
| 164 |
fe.n_dofs_per_cell(), |
| 165 |
numbers::invalid_unsigned_int); |
| 166 |
for (unsigned int i = 0; i < lexicographic.size(); ++i) |
| 167 |
if (lexicographic[i] != numbers::invalid_unsigned_int) |
| 168 |
{ |
| 169 |
AssertIndexRange(lexicographic[i], |
| 170 |
lexicographic_numbering.size()); |
| 171 |
lexicographic_numbering[lexicographic[i]] = i; |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
template <int dim_to, int dim, int spacedim> |
| 179 |
std::unique_ptr<FiniteElement<dim_to, dim_to>> |
| 180 |
create_fe(const FiniteElement<dim, spacedim> &fe) |
| 181 |
{ |
| 182 |
std::string fe_name = fe.get_name(); |
| 183 |
|
| 184 |
Assert( |
| 185 |
fe_name.find("FESystem") == std::string::npos, |
| 186 |
ExcMessage( |
| 187 |
"This function can not accept FESystem but only base elements.")); |
| 188 |
|
| 189 |
{ |
| 190 |
const std::size_t template_starts = fe_name.find_first_of('<'); |
| 191 |
Assert(fe_name[template_starts + 1] == |
| 192 |
(dim == 1 ? '1' : (dim == 2 ? '2' : '3')), |
| 193 |
ExcInternalError()); |
| 194 |
fe_name[template_starts + 1] = std::to_string(dim_to)[0]; |
| 195 |
} |
| 196 |
return FETools::get_fe_by_name<dim_to, dim_to>(fe_name); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
// ----------------- actual ShapeInfo implementation -------------------- |
| 201 |
|
| 202 |
template <typename Number> |
| 203 |
ShapeInfo<Number>::ShapeInfo() |
| 204 |
: element_type(tensor_general) |
| 205 |
, n_dimensions(0) |
| 206 |
, n_components(0) |
| 207 |
, n_q_points(0) |
| 208 |
, dofs_per_component_on_cell(0) |
| 209 |
, n_q_points_face(0) |
| 210 |
, dofs_per_component_on_face(0) |
| 211 |
{} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
template <typename Number> |
| 216 |
template <int dim, int spacedim, int dim_q> |
| 217 |
inline ShapeInfo<Number>::ShapeInfo( |
| 218 |
const Quadrature<dim_q> &quad, |
| 219 |
const FiniteElement<dim, spacedim> &fe_in, |
| 220 |
const unsigned int base_element_number) |
| 221 |
: element_type(tensor_general) |
| 222 |
, n_dimensions(0) |
| 223 |
, n_components(0) |
| 224 |
, n_q_points(0) |
| 225 |
, dofs_per_component_on_cell(0) |
| 226 |
, n_q_points_face(0) |
| 227 |
, dofs_per_component_on_face(0) |
| 228 |
{ |
| 229 |
reinit(quad, fe_in, base_element_number); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
template <typename Number> |
| 235 |
template <int dim, int spacedim, int dim_q> |
| 236 |
void |
| 237 |
ShapeInfo<Number>::reinit(const Quadrature<dim_q> &quad_in, |
| 238 |
const FiniteElement<dim, spacedim> &fe_in, |
| 239 |
const unsigned int base_element_number) |
| 240 |
{ |
| 241 |
// ShapeInfo for RT elements. Here, data is of size 2 instead of 1. |
| 242 |
// data[0] is univariate_shape_data in normal direction and |
| 243 |
// data[1] is univariate_shape_data in tangential direction |
| 244 |
// |
| 245 |
if (dynamic_cast<const FE_RaviartThomasNodal<dim> *>( |
| 246 |
&fe_in.base_element(base_element_number))) |
| 247 |
{ |
| 248 |
element_type = tensor_raviart_thomas; |
| 249 |
|
| 250 |
const auto quad = quad_in.get_tensor_basis()[0]; |
| 251 |
|
| 252 |
const FiniteElement<dim, spacedim> &fe = |
| 253 |
fe_in.base_element(base_element_number); |
| 254 |
n_dimensions = dim; |
| 255 |
n_components = fe_in.n_components(); |
| 256 |
|
| 257 |
data.resize(2); |
| 258 |
const unsigned int n_q_points_1d = quad.size(); |
| 259 |
|
| 260 |
n_q_points = Utilities::fixed_power<dim>(n_q_points_1d); |
| 261 |
n_q_points_face = Utilities::fixed_power<dim - 1>(n_q_points_1d); |
| 262 |
|
| 263 |
dofs_per_component_on_cell = fe_in.n_dofs_per_cell() / n_components; |
| 264 |
|
| 265 |
// NOTE dofs_per_component_on_face is in tangential direction! |
| 266 |
dofs_per_component_on_face = |
| 267 |
fe_in.n_dofs_per_face() + Utilities::pow(fe_in.degree, dim - 2); |
| 268 |
const unsigned int dofs_per_face_normal = fe_in.n_dofs_per_face(); |
| 269 |
|
| 270 |
lexicographic_numbering = |
| 271 |
FE_RaviartThomas<dim>::get_lexicographic_numbering(fe_in.degree - |
| 272 |
1); |
| 273 |
|
| 274 |
// To get the right shape_values of the RT element |
| 275 |
std::vector<unsigned int> lex_normal, lex_tangent; |
| 276 |
for (unsigned int i = 0; i < fe.degree; ++i) |
| 277 |
lex_tangent.push_back(i); |
| 278 |
|
| 279 |
lex_normal.push_back(0); |
| 280 |
for (unsigned int i = dofs_per_face_normal * 2 * dim; |
| 281 |
i < dofs_per_face_normal * 2 * dim + fe.degree - 1; |
| 282 |
++i) |
| 283 |
lex_normal.push_back(i); |
| 284 |
lex_normal.push_back(dofs_per_face_normal); |
| 285 |
|
| 286 |
// 'direction' distinguishes between normal and tangential direction |
| 287 |
for (unsigned int direction = 0; direction < 2; ++direction) |
| 288 |
{ |
| 289 |
data[direction].element_type = tensor_raviart_thomas; |
| 290 |
data[direction].quadrature = quad; |
| 291 |
data[direction].n_q_points_1d = n_q_points_1d; |
| 292 |
data[direction].fe_degree = fe.degree - direction; |
| 293 |
const std::vector<unsigned int> &lexicographic = |
| 294 |
direction == 0 ? lex_normal : lex_tangent; |
| 295 |
|
| 296 |
data[direction].evaluate_shape_functions(fe, |
| 297 |
quad, |
| 298 |
lexicographic, |
| 299 |
direction); |
| 300 |
data[direction].evaluate_collocation_space(fe, |
| 301 |
quad, |
| 302 |
lexicographic, |
| 303 |
direction); |
| 304 |
data[direction].check_and_set_shapes_symmetric(); |
| 305 |
} |
| 306 |
|
| 307 |
if (dim == 3) |
| 308 |
face_orientations_quad = compute_orientation_table(n_q_points_1d); |
| 309 |
|
| 310 |
return; |
| 311 |
} |
| 312 |
else if (quad_in.is_tensor_product() == false || |
| 313 |
dynamic_cast<const FE_SimplexPoly<dim, spacedim> *>( |
| 314 |
&fe_in.base_element(base_element_number)) != nullptr || |
| 315 |
dynamic_cast<const FE_WedgePoly<dim, spacedim> *>( |
| 316 |
&fe_in.base_element(base_element_number)) != nullptr || |
| 317 |
dynamic_cast<const FE_PyramidPoly<dim, spacedim> *>( |
| 318 |
&fe_in.base_element(base_element_number)) != nullptr) |
| 319 |
{ |
| 320 |
// specialization for arbitrary finite elements and quadrature rules |
| 321 |
// as needed in the context, e.g., of simplices |
| 322 |
|
| 323 |
AssertDimension(dim, dim_q); |
| 324 |
|
| 325 |
const auto quad = Quadrature<dim>(quad_in); |
| 326 |
const auto &fe = fe_in.base_element(base_element_number); |
| 327 |
n_dimensions = dim; |
| 328 |
n_components = fe_in.n_components(); |
| 329 |
n_q_points = quad.size(); |
| 330 |
dofs_per_component_on_cell = fe.n_dofs_per_cell(); |
| 331 |
n_q_points_face = 0; // not implemented yet |
| 332 |
dofs_per_component_on_face = 0; // |
| 333 |
|
| 334 |
Assert(fe.n_components() == 1, |
| 335 |
ExcMessage( |
| 336 |
"FEEvaluation only works for scalar finite elements.")); |
| 337 |
|
| 338 |
data.resize(1); |
| 339 |
UnivariateShapeData<Number> &univariate_shape_data = data.front(); |
| 340 |
data_access.reinit(n_dimensions, n_components); |
| 341 |
data_access.fill(&univariate_shape_data); |
| 342 |
|
| 343 |
// note: we cannot write `univariate_shape_data.quadrature = quad`, |
| 344 |
// since the quadrature rule within UnivariateShapeData expects |
| 345 |
// a 1d quadrature rule. However, in this case we are not able to |
| 346 |
// define that rule anyway so other code cannot use this information. |
| 347 |
|
| 348 |
univariate_shape_data.fe_degree = fe.degree; |
| 349 |
univariate_shape_data.n_q_points_1d = quad.size(); |
| 350 |
|
| 351 |
if ((fe.n_dofs_per_cell() == 0) || (quad.empty())) |
| 352 |
return; |
| 353 |
|
| 354 |
// grant write access to common univariate shape data |
| 355 |
auto &shape_values = univariate_shape_data.shape_values; |
| 356 |
auto &shape_values_face = univariate_shape_data.shape_values_face; |
| 357 |
auto &shape_gradients = univariate_shape_data.shape_gradients; |
| 358 |
auto &shape_gradients_face = |
| 359 |
univariate_shape_data.shape_gradients_face; |
| 360 |
|
| 361 |
const unsigned int n_dofs = fe.n_dofs_per_cell(); |
| 362 |
|
| 363 |
const unsigned int array_size = n_dofs * n_q_points; |
| 364 |
|
| 365 |
shape_values.resize_fast(array_size); |
| 366 |
shape_gradients.resize_fast(array_size * dim); |
| 367 |
|
| 368 |
for (unsigned int i = 0; i < n_dofs; ++i) |
| 369 |
for (unsigned int q = 0; q < n_q_points; ++q) |
| 370 |
{ |
| 371 |
shape_values[i * n_q_points + q] = |
| 372 |
fe.shape_value(i, quad.point(q)); |
| 373 |
|
| 374 |
const auto grad = fe.shape_grad(i, quad.point(q)); |
| 375 |
|
| 376 |
for (unsigned int d = 0; d < dim; ++d) |
| 377 |
shape_gradients[i * dim * n_q_points + q * dim + d] = grad[d]; |
| 378 |
} |
| 379 |
|
| 380 |
{ |
| 381 |
const auto reference_cell = fe.reference_cell(); |
| 382 |
|
| 383 |
const auto temp = get_face_quadrature_collection(quad, false); |
| 384 |
const auto &quad_face = temp.second; |
| 385 |
|
| 386 |
if (reference_cell != temp.first) |
| 387 |
{ |
| 388 |
// TODO: this might happen if the quadrature rule and the |
| 389 |
// FE do not match |
| 390 |
this->n_q_points_face = 0; |
| 391 |
} |
| 392 |
else |
| 393 |
{ |
| 394 |
this->n_q_points_face = quad_face[0].size(); |
| 395 |
|
| 396 |
const unsigned int n_faces = temp.first.n_faces(); |
| 397 |
|
| 398 |
n_q_points_faces.resize(n_faces); |
| 399 |
for (unsigned int i = 0; i < n_faces; ++i) |
| 400 |
n_q_points_faces[i] = |
| 401 |
quad_face[quad_face.size() == 1 ? 0 : i].size(); |
| 402 |
|
| 403 |
unsigned int n_q_points_face_max = 0; |
| 404 |
|
| 405 |
for (unsigned int i = 0; i < quad_face.size(); ++i) |
| 406 |
n_q_points_face_max = |
| 407 |
std::max(n_q_points_face_max, quad_face[i].size()); |
| 408 |
|
| 409 |
unsigned int n_max_vertices = 0; |
| 410 |
|
| 411 |
for (unsigned int face_no = 0; face_no < n_faces; ++face_no) |
| 412 |
n_max_vertices = std::max( |
| 413 |
n_max_vertices, |
| 414 |
reference_cell.face_reference_cell(face_no).n_vertices()); |
| 415 |
|
| 416 |
const auto projected_quad_face = |
| 417 |
QProjector<dim>::project_to_all_faces(reference_cell, |
| 418 |
quad_face); |
| 419 |
|
| 420 |
const unsigned int n_max_face_orientations = |
| 421 |
dim == 2 ? 2 : (2 * n_max_vertices); |
| 422 |
|
| 423 |
shape_values_face.reinit({n_faces, |
| 424 |
n_max_face_orientations, |
| 425 |
n_dofs * n_q_points_face_max}); |
| 426 |
|
| 427 |
shape_gradients_face.reinit( |
| 428 |
{n_faces, |
| 429 |
n_max_face_orientations, |
| 430 |
dim * n_dofs * n_q_points_face_max}); |
| 431 |
|
| 432 |
for (unsigned int f = 0; f < n_faces; ++f) |
| 433 |
{ |
| 434 |
const unsigned int n_face_orientations = |
| 435 |
reference_cell.n_face_orientations(f); |
| 436 |
|
| 437 |
const unsigned int n_q_points_face = |
| 438 |
quad_face[quad_face.size() == 1 ? 0 : f].size(); |
| 439 |
|
| 440 |
for (types::geometric_orientation orientation = 0; |
| 441 |
orientation < n_face_orientations; |
| 442 |
++orientation) |
| 443 |
{ |
| 444 |
const auto offset = |
| 445 |
QProjector<dim>::DataSetDescriptor::face( |
| 446 |
reference_cell, f, orientation, quad_face); |
| 447 |
|
| 448 |
for (unsigned int i = 0; i < n_dofs; ++i) |
| 449 |
for (unsigned int q = 0; q < n_q_points_face; ++q) |
| 450 |
{ |
| 451 |
const auto &point = |
| 452 |
projected_quad_face.point(q + offset); |
| 453 |
|
| 454 |
shape_values_face(f, |
| 455 |
orientation, |
| 456 |
i * n_q_points_face + q) = |
| 457 |
fe.shape_value(i, point); |
| 458 |
|
| 459 |
const auto grad = fe.shape_grad(i, point); |
| 460 |
|
| 461 |
for (unsigned int d = 0; d < dim; ++d) |
| 462 |
shape_gradients_face(f, |
| 463 |
orientation, |
| 464 |
i * dim * n_q_points_face + |
| 465 |
q * dim + d) = grad[d]; |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
// TODO: also fill shape_hessians, inverse_shape_values, |
| 473 |
// shape_data_on_face, quadrature_data_on_face, |
| 474 |
// values_within_subface, gradients_within_subface, |
| 475 |
// hessians_within_subface |
| 476 |
|
| 477 |
// note: shape_gradients_collocation, shape_hessians_collocation, |
| 478 |
// shape_values_eo, shape_gradients_eo, shape_hessians_eo, |
| 479 |
// shape_gradients_collocation_eo, shape_hessians_collocation_eo, |
| 480 |
// inverse_shape_values_eo cannot be filled |
| 481 |
|
| 482 |
std::vector<unsigned int> scalar_lexicographic; |
| 483 |
get_element_type_specific_information(fe_in, |
| 484 |
fe, |
| 485 |
base_element_number, |
| 486 |
element_type, |
| 487 |
scalar_lexicographic, |
| 488 |
lexicographic_numbering); |
| 489 |
|
| 490 |
univariate_shape_data.element_type = this->element_type; |
| 491 |
|
| 492 |
univariate_shape_data.nodal_at_cell_boundaries = true; |
| 493 |
|
| 494 |
const ReferenceCell reference_cell = fe.reference_cell(); |
| 495 |
if (reference_cell.is_simplex()) |
| 496 |
{ |
| 497 |
if (dim == 2) |
| 498 |
dofs_per_component_on_face = fe.degree + 1; |
| 499 |
else |
| 500 |
dofs_per_component_on_face = |
| 501 |
(fe.degree + 1) * (fe.degree + 2) / 2; |
| 502 |
|
| 503 |
face_to_cell_index_nodal.reinit(reference_cell.n_faces(), |
| 504 |
dofs_per_component_on_face); |
| 505 |
|
| 506 |
|
| 507 |
for (unsigned int face = 0; face < reference_cell.n_faces(); |
| 508 |
++face) |
| 509 |
{ |
| 510 |
// first get info from reference cell, i.e. the linear case |
| 511 |
unsigned int d = 0; |
| 512 |
for (; d < dim; ++d) |
| 513 |
face_to_cell_index_nodal[face][d] = |
| 514 |
reference_cell.face_to_cell_vertices( |
| 515 |
face, d, numbers::default_geometric_orientation); |
| 516 |
|
| 517 |
// now fill the rest of the indices, start with the lines |
| 518 |
if (fe.degree == 2) |
| 519 |
for (; d < dofs_per_component_on_face; ++d) |
| 520 |
face_to_cell_index_nodal[face][d] = |
| 521 |
reference_cell.n_vertices() + |
| 522 |
reference_cell.face_to_cell_lines( |
| 523 |
face, |
| 524 |
d - dim, |
| 525 |
numbers::default_geometric_orientation); |
| 526 |
|
| 527 |
// in the cubic case it is more complicated as more DoFs are |
| 528 |
// on the lines |
| 529 |
else if (fe.degree == 3) |
| 530 |
{ |
| 531 |
for (unsigned int line = 0; |
| 532 |
d < dofs_per_component_on_face - 1; |
| 533 |
++line, d += 2) |
| 534 |
{ |
| 535 |
const unsigned int face_to_cell_lines = |
| 536 |
reference_cell.face_to_cell_lines( |
| 537 |
face, |
| 538 |
line, |
| 539 |
numbers::default_geometric_orientation); |
| 540 |
// check the direction of the line |
| 541 |
// is it 0 -> 1 or 1 -> 0 |
| 542 |
// as DoFs on the line are ordered differently |
| 543 |
if (reference_cell.line_to_cell_vertices( |
| 544 |
face_to_cell_lines, 0) == |
| 545 |
reference_cell.face_to_cell_vertices( |
| 546 |
face, |
| 547 |
line, |
| 548 |
numbers::default_geometric_orientation)) |
| 549 |
{ |
| 550 |
face_to_cell_index_nodal[face][d] = |
| 551 |
reference_cell.n_vertices() + |
| 552 |
2 * face_to_cell_lines; |
| 553 |
face_to_cell_index_nodal[face][d + 1] = |
| 554 |
reference_cell.n_vertices() + |
| 555 |
2 * face_to_cell_lines + 1; |
| 556 |
} |
| 557 |
else |
| 558 |
{ |
| 559 |
face_to_cell_index_nodal[face][d + 1] = |
| 560 |
reference_cell.n_vertices() + |
| 561 |
2 * face_to_cell_lines; |
| 562 |
face_to_cell_index_nodal[face][d] = |
| 563 |
reference_cell.n_vertices() + |
| 564 |
2 * face_to_cell_lines + 1; |
| 565 |
} |
| 566 |
} |
| 567 |
// in 3D we also need the DoFs on the quads |
| 568 |
if (dim == 3) |
| 569 |
{ |
| 570 |
face_to_cell_index_nodal |
| 571 |
[face][dofs_per_component_on_face - 1] = |
| 572 |
reference_cell.n_vertices() + |
| 573 |
2 * reference_cell.n_lines() + face; |
| 574 |
} |
| 575 |
} |
| 576 |
else if (fe.degree > 3) |
| 577 |
DEAL_II_NOT_IMPLEMENTED(); |
| 578 |
} |
| 579 |
} |
| 580 |
// TODO: set up face_to_cell_index_nodal, face_to_cell_index_hermite, |
| 581 |
// face_orientations |
| 582 |
|
| 583 |
return; |
| 584 |
} |
| 585 |
|
| 586 |
const auto quad = quad_in.get_tensor_basis()[0]; |
| 587 |
|
| 588 |
const FiniteElement<dim, spacedim> &fe = |
| 589 |
fe_in.base_element(base_element_number); |
| 590 |
n_dimensions = dim; |
| 591 |
n_components = fe_in.n_components(); |
| 592 |
|
| 593 |
Assert(fe.n_components() == 1, |
| 594 |
ExcMessage("FEEvaluation only works for scalar finite elements.")); |
| 595 |
|
| 596 |
// assuming isotropy of dimensions and components |
| 597 |
data.resize(1); |
| 598 |
UnivariateShapeData<Number> &univariate_shape_data = data.front(); |
| 599 |
data_access.reinit(n_dimensions, n_components); |
| 600 |
data_access.fill(&univariate_shape_data); |
| 601 |
univariate_shape_data.quadrature = quad; |
| 602 |
univariate_shape_data.fe_degree = fe.degree; |
| 603 |
univariate_shape_data.n_q_points_1d = quad.size(); |
| 604 |
|
| 605 |
if ((fe.n_dofs_per_cell() == 0) || (quad.empty())) |
| 606 |
return; |
| 607 |
|
| 608 |
const unsigned int fe_degree = fe.degree; |
| 609 |
const unsigned int n_q_points_1d = quad.size(); |
| 610 |
const unsigned int n_dofs_1d = |
| 611 |
std::min(fe.n_dofs_per_cell(), fe_degree + 1); |
| 612 |
|
| 613 |
// renumber (this is necessary for FE_Q, for example, since there the |
| 614 |
// vertex DoFs come first, which is incompatible with the lexicographic |
| 615 |
// ordering necessary to apply tensor products efficiently) |
| 616 |
std::vector<unsigned int> scalar_lexicographic; |
| 617 |
Assert(fe.n_components() == 1, ExcMessage("Expected a scalar element")); |
| 618 |
|
| 619 |
get_element_type_specific_information(fe_in, |
| 620 |
fe, |
| 621 |
base_element_number, |
| 622 |
element_type, |
| 623 |
scalar_lexicographic, |
| 624 |
lexicographic_numbering); |
| 625 |
|
| 626 |
n_q_points = Utilities::fixed_power<dim>(n_q_points_1d); |
| 627 |
n_q_points_face = |
| 628 |
(dim > 1 ? Utilities::fixed_power<dim - 1>(n_q_points_1d) : 1); |
| 629 |
dofs_per_component_on_cell = fe.n_dofs_per_cell(); |
| 630 |
dofs_per_component_on_face = |
| 631 |
(dim > 1 ? Utilities::fixed_power<dim - 1>(fe_degree + 1) : 1); |
| 632 |
|
| 633 |
univariate_shape_data.evaluate_shape_functions(fe, |
| 634 |
quad, |
| 635 |
scalar_lexicographic, |
| 636 |
0); |
| 637 |
|
| 638 |
if (dim > 1 && (dynamic_cast<const FE_Q<dim, spacedim> *>(&fe) || |
| 639 |
dynamic_cast<const FE_Q_iso_Q1<dim, spacedim> *>(&fe))) |
| 640 |
{ |
| 641 |
auto &subface_interpolation_matrix_0 = |
| 642 |
univariate_shape_data.subface_interpolation_matrices[0]; |
| 643 |
auto &subface_interpolation_matrix_1 = |
| 644 |
univariate_shape_data.subface_interpolation_matrices[1]; |
| 645 |
auto &subface_interpolation_matrix_scalar_0 = |
| 646 |
univariate_shape_data.subface_interpolation_matrices_scalar[0]; |
| 647 |
auto &subface_interpolation_matrix_scalar_1 = |
| 648 |
univariate_shape_data.subface_interpolation_matrices_scalar[1]; |
| 649 |
|
| 650 |
const unsigned int nn = fe_degree + 1; |
| 651 |
subface_interpolation_matrix_0.resize(nn * nn); |
| 652 |
subface_interpolation_matrix_1.resize(nn * nn); |
| 653 |
subface_interpolation_matrix_scalar_0.resize(nn * nn); |
| 654 |
subface_interpolation_matrix_scalar_1.resize(nn * nn); |
| 655 |
|
| 656 |
const bool is_feq = |
| 657 |
dynamic_cast<const FE_Q<dim, spacedim> *>(&fe) != nullptr; |
| 658 |
|
| 659 |
std::vector<Point<1>> fe_q_points = |
| 660 |
is_feq ? QGaussLobatto<1>(nn).get_points() : |
| 661 |
QIterated<1>(QTrapezoid<1>(), nn - 1).get_points(); |
| 662 |
|
| 663 |
const std::vector<Polynomials::Polynomial<double>> poly_feq = |
| 664 |
Polynomials::generate_complete_Lagrange_basis(fe_q_points); |
| 665 |
|
| 666 |
const std::vector<Polynomials::PiecewisePolynomial<double>> |
| 667 |
poly_feq_iso_q1 = |
| 668 |
Polynomials::generate_complete_Lagrange_basis_on_subdivisions(nn - |
| 669 |
1, |
| 670 |
1); |
| 671 |
|
| 672 |
for (unsigned int i = 0, c = 0; i < nn; ++i) |
| 673 |
for (unsigned int j = 0; j < nn; ++j, ++c) |
| 674 |
{ |
| 675 |
subface_interpolation_matrix_scalar_0[c] = |
| 676 |
is_feq ? poly_feq[j].value(0.5 * fe_q_points[i][0]) : |
| 677 |
poly_feq_iso_q1[j].value(0.5 * fe_q_points[i][0]); |
| 678 |
subface_interpolation_matrix_0[c] = |
| 679 |
subface_interpolation_matrix_scalar_0[c]; |
| 680 |
subface_interpolation_matrix_scalar_1[c] = |
| 681 |
is_feq ? |
| 682 |
poly_feq[j].value(0.5 + 0.5 * fe_q_points[i][0]) : |
| 683 |
poly_feq_iso_q1[j].value(0.5 + 0.5 * fe_q_points[i][0]); |
| 684 |
subface_interpolation_matrix_1[c] = |
| 685 |
subface_interpolation_matrix_scalar_1[c]; |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
univariate_shape_data.evaluate_collocation_space(fe, |
| 690 |
quad, |
| 691 |
scalar_lexicographic, |
| 692 |
0); |
| 693 |
|
| 694 |
const auto &shape_data_on_face = univariate_shape_data.shape_data_on_face; |
| 695 |
|
| 696 |
if (element_type == tensor_general && |
| 697 |
univariate_shape_data.check_and_set_shapes_symmetric()) |
| 698 |
{ |
| 699 |
if (dynamic_cast<const FE_Q_iso_Q1<dim, spacedim> *>(&fe) && |
| 700 |
fe.tensor_degree() > 1) |
| 701 |
element_type = tensor_symmetric_no_collocation; |
| 702 |
else if (univariate_shape_data.check_shapes_collocation()) |
| 703 |
element_type = tensor_symmetric_collocation; |
| 704 |
else |
| 705 |
element_type = tensor_symmetric; |
| 706 |
|
| 707 |
if (n_dofs_1d > 2 && element_type == tensor_symmetric) |
| 708 |
{ |
| 709 |
// check if we are a Hermite type |
| 710 |
element_type = tensor_symmetric_hermite; |
| 711 |
for (unsigned int i = 1; i < n_dofs_1d; ++i) |
| 712 |
if (std::abs(get_first_array_element( |
| 713 |
shape_data_on_face[0][i])) > 1e-12) |
| 714 |
element_type = tensor_symmetric; |
| 715 |
for (unsigned int i = 2; i < n_dofs_1d; ++i) |
| 716 |
if (std::abs(get_first_array_element( |
| 717 |
shape_data_on_face[0][n_dofs_1d + i])) > 1e-12) |
| 718 |
element_type = tensor_symmetric; |
| 719 |
} |
| 720 |
} |
| 721 |
else if (element_type == tensor_symmetric_plus_dg0) |
| 722 |
univariate_shape_data.check_and_set_shapes_symmetric(); |
| 723 |
|
| 724 |
univariate_shape_data.nodal_at_cell_boundaries = true; |
| 725 |
for (unsigned int i = 1; i < n_dofs_1d; ++i) |
| 726 |
if (std::abs(get_first_array_element(shape_data_on_face[0][i])) > |
| 727 |
1e-13 || |
| 728 |
std::abs(get_first_array_element(shape_data_on_face[1][i - 1])) > |
| 729 |
1e-13) |
| 730 |
univariate_shape_data.nodal_at_cell_boundaries = false; |
| 731 |
|
| 732 |
if (univariate_shape_data.nodal_at_cell_boundaries == true) |
| 733 |
{ |
| 734 |
face_to_cell_index_nodal.reinit(GeometryInfo<dim>::faces_per_cell, |
| 735 |
dofs_per_component_on_face); |
| 736 |
for (const auto f : GeometryInfo<dim>::face_indices()) |
| 737 |
{ |
| 738 |
const unsigned int direction = f / 2; |
| 739 |
const unsigned int stride = |
| 740 |
direction < dim - 1 ? (fe_degree + 1) : 1; |
| 741 |
int shift = 1; |
| 742 |
for (unsigned int d = 0; d < direction; ++d) |
| 743 |
shift *= fe_degree + 1; |
| 744 |
const unsigned int offset = (f % 2) * fe_degree * shift; |
| 745 |
|
| 746 |
if (direction == 0 || direction == dim - 1) |
| 747 |
for (unsigned int i = 0; i < dofs_per_component_on_face; ++i) |
| 748 |
face_to_cell_index_nodal(f, i) = offset + i * stride; |
| 749 |
else |
| 750 |
// local coordinate system on faces 2 and 3 is zx in |
| 751 |
// deal.II, not xz as expected for tensor products -> adjust |
| 752 |
// that here |
| 753 |
for (unsigned int j = 0; j <= fe_degree; ++j) |
| 754 |
for (unsigned int i = 0; i <= fe_degree; ++i) |
| 755 |
{ |
| 756 |
const unsigned int ind = |
| 757 |
offset + j * dofs_per_component_on_face + i; |
| 758 |
AssertIndexRange(ind, dofs_per_component_on_cell); |
| 759 |
const unsigned int l = i * (fe_degree + 1) + j; |
| 760 |
face_to_cell_index_nodal(f, l) = ind; |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
// face orientation for faces in 3d |
| 765 |
// (similar to MappingInfoStorage::QuadratureDescriptor::initialize) |
| 766 |
if (dim == 3) |
| 767 |
{ |
| 768 |
face_orientations_dofs = compute_orientation_table(fe_degree + 1); |
| 769 |
face_orientations_quad = compute_orientation_table(n_q_points_1d); |
| 770 |
} |
| 771 |
else |
| 772 |
{ |
| 773 |
face_orientations_dofs.reinit(1, 1); |
| 774 |
face_orientations_quad.reinit(1, 1); |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
if (element_type == tensor_symmetric_hermite) |
| 779 |
{ |
| 780 |
face_to_cell_index_hermite.reinit(GeometryInfo<dim>::faces_per_cell, |
| 781 |
2 * dofs_per_component_on_face); |
| 782 |
for (const auto f : GeometryInfo<dim>::face_indices()) |
| 783 |
{ |
| 784 |
const unsigned int direction = f / 2; |
| 785 |
const unsigned int stride = |
| 786 |
direction < dim - 1 ? (fe_degree + 1) : 1; |
| 787 |
int shift = 1; |
| 788 |
for (unsigned int d = 0; d < direction; ++d) |
| 789 |
shift *= fe_degree + 1; |
| 790 |
const unsigned int offset = (f % 2) * fe_degree * shift; |
| 791 |
if (f % 2 == 1) |
| 792 |
shift = -shift; |
| 793 |
|
| 794 |
if (direction == 0 || direction == dim - 1) |
| 795 |
for (unsigned int i = 0; i < dofs_per_component_on_face; ++i) |
| 796 |
{ |
| 797 |
face_to_cell_index_hermite(f, 2 * i) = offset + i * stride; |
| 798 |
face_to_cell_index_hermite(f, 2 * i + 1) = |
| 799 |
offset + i * stride + shift; |
| 800 |
} |
| 801 |
else |
| 802 |
// local coordinate system on faces 2 and 3 is zx in |
| 803 |
// deal.II, not xz as expected for tensor products -> adjust |
| 804 |
// that here |
| 805 |
for (unsigned int j = 0; j <= fe_degree; ++j) |
| 806 |
for (unsigned int i = 0; i <= fe_degree; ++i) |
| 807 |
{ |
| 808 |
const unsigned int ind = |
| 809 |
offset + j * dofs_per_component_on_face + i; |
| 810 |
AssertIndexRange(ind, dofs_per_component_on_cell); |
| 811 |
const unsigned int l = i * (fe_degree + 1) + j; |
| 812 |
face_to_cell_index_hermite(f, 2 * l) = ind; |
| 813 |
face_to_cell_index_hermite(f, 2 * l + 1) = ind + shift; |
| 814 |
} |
| 815 |
} |
| 816 |
} |
| 817 |
|
| 818 |
univariate_shape_data.element_type = this->element_type; |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
template <int dim, int spacedim> |
| 824 |
Point<dim> |
| 825 |
get_unit_point(const FiniteElement<dim, spacedim> &fe, |
| 826 |
const std::vector<unsigned int> &lexicographic) |
| 827 |
{ |
| 828 |
Point<dim> unit_point; |
| 829 |
// to evaluate 1d polynomials, evaluate along the line with the first |
| 830 |
// unit support point, assuming that fe.shape_value(0,unit_point) == |
| 831 |
// 1. otherwise, need other entry point (e.g. generating a 1d element |
| 832 |
// by reading the name, as done before r29356) |
| 833 |
if (fe.has_support_points()) |
| 834 |
unit_point = fe.get_unit_support_points()[lexicographic[0]]; |
| 835 |
Assert(fe.n_dofs_per_cell() == 0 || |
| 836 |
std::abs( |
| 837 |
fe.shape_value_component(lexicographic[0], unit_point, 0) - |
| 838 |
1) < 1e-13, |
| 839 |
ExcInternalError("Could not decode 1d shape functions for the " |
| 840 |
"element " + |
| 841 |
fe.get_name())); |
| 842 |
return unit_point; |
| 843 |
} |
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
template <typename Number> |
| 848 |
template <int dim, int spacedim> |
| 849 |
void |
| 850 |
UnivariateShapeData<Number>::evaluate_shape_functions( |
| 851 |
const FiniteElement<dim, spacedim> &fe, |
| 852 |
const Quadrature<1> &quad, |
| 853 |
const std::vector<unsigned int> &lexicographic, |
| 854 |
const unsigned int direction) |
| 855 |
{ |
| 856 |
const unsigned int n_dofs_1d = |
| 857 |
std::min(fe.n_dofs_per_cell(), fe_degree + 1); |
| 858 |
|
| 859 |
const unsigned int array_size = n_dofs_1d * n_q_points_1d; |
| 860 |
shape_gradients.resize_fast(array_size); |
| 861 |
shape_values.resize_fast(array_size); |
| 862 |
shape_hessians.resize_fast(array_size); |
| 863 |
|
| 864 |
shape_data_on_face[0].resize(3 * n_dofs_1d); |
| 865 |
shape_data_on_face[1].resize(3 * n_dofs_1d); |
| 866 |
values_within_subface[0].resize(array_size); |
| 867 |
values_within_subface[1].resize(array_size); |
| 868 |
gradients_within_subface[0].resize(array_size); |
| 869 |
gradients_within_subface[1].resize(array_size); |
| 870 |
hessians_within_subface[0].resize(array_size); |
| 871 |
hessians_within_subface[1].resize(array_size); |
| 872 |
|
| 873 |
for (unsigned int i = 0; i < n_dofs_1d; ++i) |
| 874 |
{ |
| 875 |
// need to reorder from hierarchical to lexicographic to get the |
| 876 |
// DoFs correct |
| 877 |
const unsigned int my_i = lexicographic[i]; |
| 878 |
for (unsigned int q = 0; q < n_q_points_1d; ++q) |
| 879 |
{ |
| 880 |
Point<dim> q_point = get_unit_point(fe, lexicographic); |
| 881 |
q_point[direction] = quad.get_points()[q][0]; |
| 882 |
|
| 883 |
shape_values[i * n_q_points_1d + q] = |
| 884 |
fe.shape_value_component(my_i, q_point, 0); |
| 885 |
shape_gradients[i * n_q_points_1d + q] = |
| 886 |
fe.shape_grad_component(my_i, q_point, 0)[direction]; |
| 887 |
shape_hessians[i * n_q_points_1d + q] = |
| 888 |
fe.shape_grad_grad_component(my_i, |
| 889 |
q_point, |
| 890 |
0)[direction][direction]; |
| 891 |
|
| 892 |
// evaluate basis functions on the two 1d subfaces (i.e., at the |
| 893 |
// positions divided by one half and shifted by one half, |
| 894 |
// respectively) |
| 895 |
q_point[direction] *= 0.5; |
| 896 |
values_within_subface[0][i * n_q_points_1d + q] = |
| 897 |
fe.shape_value_component(my_i, q_point, 0); |
| 898 |
gradients_within_subface[0][i * n_q_points_1d + q] = |
| 899 |
fe.shape_grad_component(my_i, q_point, 0)[direction]; |
| 900 |
hessians_within_subface[0][i * n_q_points_1d + q] = |
| 901 |
fe.shape_grad_grad_component(my_i, |
| 902 |
q_point, |
| 903 |
0)[direction][direction]; |
| 904 |
q_point[direction] += 0.5; |
| 905 |
values_within_subface[1][i * n_q_points_1d + q] = |
| 906 |
fe.shape_value_component(my_i, q_point, 0); |
| 907 |
gradients_within_subface[1][i * n_q_points_1d + q] = |
| 908 |
fe.shape_grad_component(my_i, q_point, 0)[direction]; |
| 909 |
hessians_within_subface[1][i * n_q_points_1d + q] = |
| 910 |
fe.shape_grad_grad_component(my_i, |
| 911 |
q_point, |
| 912 |
0)[direction][direction]; |
| 913 |
} |
| 914 |
|
| 915 |
// evaluate basis functions on the 1d faces, i.e., in zero and one |
| 916 |
Point<dim> q_point = get_unit_point(fe, lexicographic); |
| 917 |
q_point[direction] = 0; |
| 918 |
shape_data_on_face[0][i] = fe.shape_value_component(my_i, q_point, 0); |
| 919 |
shape_data_on_face[0][i + n_dofs_1d] = |
| 920 |
fe.shape_grad_component(my_i, q_point, 0)[direction]; |
| 921 |
shape_data_on_face[0][i + 2 * n_dofs_1d] = |
| 922 |
fe.shape_grad_grad_component(my_i, |
| 923 |
q_point, |
| 924 |
0)[direction][direction]; |
| 925 |
q_point[direction] = 1; |
| 926 |
shape_data_on_face[1][i] = fe.shape_value_component(my_i, q_point, 0); |
| 927 |
shape_data_on_face[1][i + n_dofs_1d] = |
| 928 |
fe.shape_grad_component(my_i, q_point, 0)[direction]; |
| 929 |
shape_data_on_face[1][i + 2 * n_dofs_1d] = |
| 930 |
fe.shape_grad_grad_component(my_i, |
| 931 |
q_point, |
| 932 |
0)[direction][direction]; |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
template <typename Number> |
| 939 |
template <int dim, int spacedim> |
| 940 |
void |
| 941 |
UnivariateShapeData<Number>::evaluate_collocation_space( |
| 942 |
const FiniteElement<dim, spacedim> &fe, |
| 943 |
const Quadrature<1> &quad, |
| 944 |
const std::vector<unsigned int> &lexicographic, |
| 945 |
const unsigned int direction) |
| 946 |
{ |
| 947 |
const unsigned int n_dofs_1d = |
| 948 |
std::min(fe.n_dofs_per_cell(), fe_degree + 1); |
| 949 |
|
| 950 |
// get gradient and Hessian transformation matrix for the polynomial |
| 951 |
// space associated with the quadrature rule (collocation space). We |
| 952 |
// need to avoid the case with more than a few hundreds of quadrature |
| 953 |
// points when the Lagrange polynomials might underflow. Note that 200 |
| 954 |
// is not an exact value, as different quadrature formulas behave |
| 955 |
// slightly differently, but 200 has been observed to be low enough for |
| 956 |
// all common quadrature formula types. For QGauss, the actual limit is |
| 957 |
// 517 points, for example. |
| 958 |
if (n_q_points_1d >= 200) |
| 959 |
return; |
| 960 |
|
| 961 |
shape_gradients_collocation.resize(n_q_points_1d * n_q_points_1d); |
| 962 |
shape_hessians_collocation.resize(n_q_points_1d * n_q_points_1d); |
| 963 |
const std::vector<Polynomials::Polynomial<double>> poly_coll = |
| 964 |
Polynomials::generate_complete_Lagrange_basis(quad.get_points()); |
| 965 |
std::array<double, 3> values; |
| 966 |
for (unsigned int i = 0; i < n_q_points_1d; ++i) |
| 967 |
for (unsigned int q = 0; q < n_q_points_1d; ++q) |
| 968 |
{ |
| 969 |
poly_coll[i].value(quad.get_points()[q][0], 2, values.data()); |
| 970 |
shape_gradients_collocation[i * n_q_points_1d + q] = values[1]; |
| 971 |
shape_hessians_collocation[i * n_q_points_1d + q] = values[2]; |
| 972 |
} |
| 973 |
|
| 974 |
// compute the inverse shape functions in three steps: we first |
| 975 |
// change from the given quadrature formula and the associated |
| 976 |
// Lagrange polynomials to the Lagrange polynomials at quadrature |
| 977 |
// points. in this basis, we can then perform the second step, which |
| 978 |
// is the computation of a projection matrix from the potentially |
| 979 |
// higher polynomial degree associated to the quadrature points to a |
| 980 |
// polynomial space of degree equal to the degree of the given |
| 981 |
// elements. in the third step, we change from the Lagrange |
| 982 |
// polynomials in the Gauss quadrature points to the polynomial |
| 983 |
// space of the given element |
| 984 |
|
| 985 |
// step 1: change basis from the Lagrange polynomials at the given |
| 986 |
// quadrature points to the Lagrange basis at Gauss quadrature |
| 987 |
// points. this is often the identity operation as we often compute |
| 988 |
// with Gaussian quadrature, but not necessarily so |
| 989 |
QGauss<1> quad_gauss(n_q_points_1d); |
| 990 |
FullMatrix<double> transform_to_gauss(n_q_points_1d, n_q_points_1d); |
| 991 |
for (unsigned int i = 0; i < n_q_points_1d; ++i) |
| 992 |
for (unsigned int j = 0; j < n_q_points_1d; ++j) |
| 993 |
transform_to_gauss(i, j) = poly_coll[j].value(quad_gauss.point(i)[0]); |
| 994 |
|
| 995 |
// step 2: computation for the projection (in reference coordinates) |
| 996 |
// from higher to lower polynomial degree |
| 997 |
// |
| 998 |
// loop over quadrature points, multiply by q-weight on high degree |
| 999 |
// integrate loop going from high degree to low degree loop over new |
| 1000 |
// points, multiply by inverse q-weight on low degree |
| 1001 |
// |
| 1002 |
// This projection step is for the special case of Lagrange |
| 1003 |
// polynomials where most of the interpolation matrices are unit |
| 1004 |
// matrices when applying the inverse mass matrix, so we do not need |
| 1005 |
// to compute much. |
| 1006 |
QGauss<1> quad_project(n_dofs_1d); |
| 1007 |
const std::vector<Polynomials::Polynomial<double>> poly_project = |
| 1008 |
Polynomials::generate_complete_Lagrange_basis( |
| 1009 |
quad_project.get_points()); |
| 1010 |
|
| 1011 |
FullMatrix<double> project_gauss(n_dofs_1d, n_q_points_1d); |
| 1012 |
|
| 1013 |
for (unsigned int i = 0; i < n_dofs_1d; ++i) |
| 1014 |
for (unsigned int q = 0; q < n_q_points_1d; ++q) |
| 1015 |
project_gauss(i, q) = |
| 1016 |
poly_project[i].value(quad_gauss.get_points()[q][0]) * |
| 1017 |
(quad_gauss.weight(q) / quad_project.weight(i)); |
| 1018 |
FullMatrix<double> project_to_dof_space(n_dofs_1d, n_q_points_1d); |
| 1019 |
project_gauss.mmult(project_to_dof_space, transform_to_gauss); |
| 1020 |
|
| 1021 |
// step 3: change the basis back to the given finite element |
| 1022 |
// space. we can use a shortcut for elements that define support |
| 1023 |
// points, in which case we can evaluate the Lagrange polynomials of |
| 1024 |
// the Gauss quadrature in those points. this will give more |
| 1025 |
// accurate results than the inversion of a matrix. for more general |
| 1026 |
// polynomial spaces, we must invert a matrix of a Vandermonde type, |
| 1027 |
// which we do by a Householder transformation to keep roundoff |
| 1028 |
// errors low. |
| 1029 |
inverse_shape_values.resize_fast(shape_values.size()); |
| 1030 |
FullMatrix<double> transform_from_gauss(n_dofs_1d, n_dofs_1d); |
| 1031 |
if (fe.has_support_points()) |
| 1032 |
{ |
| 1033 |
for (unsigned int i = 0; i < n_dofs_1d; ++i) |
| 1034 |
for (unsigned int j = 0; j < n_dofs_1d; ++j) |
| 1035 |
transform_from_gauss(i, j) = poly_project[j].value( |
| 1036 |
fe.get_unit_support_points()[lexicographic[i]][0]); |
| 1037 |
FullMatrix<double> result(n_dofs_1d, n_q_points_1d); |
| 1038 |
transform_from_gauss.mmult(result, project_to_dof_space); |
| 1039 |
|
| 1040 |
// set very small entries to zero - we are in reference space |
| 1041 |
// with normalized numbers, so this is straight-forward to check |
| 1042 |
// here |
| 1043 |
for (unsigned int i = 0; i < n_dofs_1d; ++i) |
| 1044 |
for (unsigned int q = 0; q < n_q_points_1d; ++q) |
| 1045 |
inverse_shape_values[i * n_q_points_1d + q] = |
| 1046 |
std::abs(result(i, q)) < 1e-15 ? 0 : result(i, q); |
| 1047 |
} |
| 1048 |
else |
| 1049 |
{ |
| 1050 |
for (unsigned int i = 0; i < n_dofs_1d; ++i) |
| 1051 |
for (unsigned int j = 0; j < n_dofs_1d; ++j) |
| 1052 |
{ |
| 1053 |
Point<dim> q_point = get_unit_point(fe, lexicographic); |
| 1054 |
q_point[direction] = quad_project.point(i)[0]; |
| 1055 |
|
| 1056 |
transform_from_gauss(i, j) = |
| 1057 |
fe.shape_value_component(lexicographic[j], q_point, 0); |
| 1058 |
} |
| 1059 |
Householder<double> H(transform_from_gauss); |
| 1060 |
Vector<double> in(n_dofs_1d), out(n_dofs_1d); |
| 1061 |
for (unsigned int q = 0; q < n_q_points_1d; ++q) |
| 1062 |
{ |
| 1063 |
for (unsigned int i = 0; i < n_dofs_1d; ++i) |
| 1064 |
in(i) = project_to_dof_space(i, q); |
| 1065 |
H.least_squares(out, in); |
| 1066 |
for (unsigned int i = 0; i < n_dofs_1d; ++i) |
| 1067 |
inverse_shape_values[i * n_q_points_1d + q] = |
| 1068 |
std::abs(out(i)) < 1e-15 ? 0. : out(i); |
| 1069 |
} |
| 1070 |
} |
| 1071 |
quadrature_data_on_face[0].resize(quad.size() * 3); |
| 1072 |
quadrature_data_on_face[1].resize(quad.size() * 3); |
| 1073 |
|
| 1074 |
for (unsigned int i = 0; i < quad.size(); ++i) |
| 1075 |
{ |
| 1076 |
std::array<double, 3> values; |
| 1077 |
poly_coll[i].value(0.0, 2, values.data()); |
| 1078 |
for (unsigned int d = 0; d < 3; ++d) |
| 1079 |
quadrature_data_on_face[0][i + d * quad.size()] = values[d]; |
| 1080 |
poly_coll[i].value(1.0, 2, values.data()); |
| 1081 |
for (unsigned int d = 0; d < 3; ++d) |
| 1082 |
quadrature_data_on_face[1][i + d * quad.size()] = values[d]; |
| 1083 |
} |
| 1084 |
} |
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
template <typename Number> |
| 1089 |
bool |
| 1090 |
UnivariateShapeData<Number>::check_and_set_shapes_symmetric() |
| 1091 |
{ |
| 1092 |
const double zero_tol = |
| 1093 |
std::is_same_v<Number, double> == true ? 1e-12 : 1e-7; |
| 1094 |
// symmetry for values |
| 1095 |
const unsigned int n_dofs_1d = fe_degree + 1; |
| 1096 |
for (unsigned int i = 0; i < (n_dofs_1d + 1) / 2; ++i) |
| 1097 |
for (unsigned int j = 0; j < n_q_points_1d; ++j) |
| 1098 |
if (std::abs(get_first_array_element( |
| 1099 |
shape_values[i * n_q_points_1d + j] - |
| 1100 |
shape_values[(n_dofs_1d - i) * n_q_points_1d - j - 1])) > |
| 1101 |
std::max(zero_tol, |
| 1102 |
zero_tol * std::abs(get_first_array_element( |
| 1103 |
shape_values[i * n_q_points_1d + j])))) |
| 1104 |
return false; |
| 1105 |
|
| 1106 |
// shape values should be zero at x=0.5 for all basis functions except |
| 1107 |
// for the middle one for degrees of 4 and higher |
| 1108 |
if (n_dofs_1d > 3 && n_q_points_1d % 2 == 1 && n_dofs_1d % 2 == 1) |
| 1109 |
{ |
| 1110 |
for (unsigned int i = 0; i < n_dofs_1d / 2; ++i) |
| 1111 |
if (std::abs(get_first_array_element( |
| 1112 |
shape_values[i * n_q_points_1d + n_q_points_1d / 2])) > |
| 1113 |
zero_tol) |
| 1114 |
return false; |
| 1115 |
} |
| 1116 |
|
| 1117 |
// skew-symmetry for gradient, zero of middle basis function in middle |
| 1118 |
// quadrature point. Multiply tolerance by degree of the element to |
| 1119 |
// the power of 1.5 to get a suitable gradient scaling |
| 1120 |
const double zero_tol_gradient = |
| 1121 |
zero_tol * std::sqrt(fe_degree + 1.) * (fe_degree + 1); |
| 1122 |
for (unsigned int i = 0; i < (n_dofs_1d + 1) / 2; ++i) |
| 1123 |
for (unsigned int j = 0; j < n_q_points_1d; ++j) |
| 1124 |
if (std::abs(get_first_array_element( |
| 1125 |
shape_gradients[i * n_q_points_1d + j] + |
| 1126 |
shape_gradients[(n_dofs_1d - i) * n_q_points_1d - j - 1])) > |
| 1127 |
zero_tol_gradient) |
| 1128 |
return false; |
| 1129 |
if (n_dofs_1d % 2 == 1 && n_q_points_1d % 2 == 1) |
| 1130 |
if (std::abs(get_first_array_element( |
| 1131 |
shape_gradients[(n_dofs_1d / 2) * n_q_points_1d + |
| 1132 |
(n_q_points_1d / 2)])) > zero_tol_gradient) |
| 1133 |
return false; |
| 1134 |
|
| 1135 |
// symmetry for Hessian. Multiply tolerance by degree^3 of the element |
| 1136 |
// to get a suitable Hessian scaling |
| 1137 |
const double zero_tol_hessian = |
| 1138 |
zero_tol * (fe_degree + 1) * (fe_degree + 1) * (fe_degree + 1); |
| 1139 |
for (unsigned int i = 0; i < (n_dofs_1d + 1) / 2; ++i) |
| 1140 |
for (unsigned int j = 0; j < n_q_points_1d; ++j) |
| 1141 |
if (std::abs(get_first_array_element( |
| 1142 |
shape_hessians[i * n_q_points_1d + j] - |
| 1143 |
shape_hessians[(n_dofs_1d - i) * n_q_points_1d - j - 1])) > |
| 1144 |
zero_tol_hessian) |
| 1145 |
return false; |
| 1146 |
|
| 1147 |
auto convert_to_eo = [](const AlignedVector<Number> &array, |
| 1148 |
const unsigned n_rows, |
| 1149 |
const unsigned n_cols) { |
| 1150 |
const unsigned int stride = (n_cols + 1) / 2; |
| 1151 |
AlignedVector<Number> array_eo(n_rows * stride); |
| 1152 |
for (unsigned int i = 0; i < n_rows / 2; ++i) |
| 1153 |
for (unsigned int q = 0; q < stride; ++q) |
| 1154 |
{ |
| 1155 |
array_eo[i * stride + q] = |
| 1156 |
0.5 * |
| 1157 |
(array[i * n_cols + q] + array[i * n_cols + n_cols - 1 - q]); |
| 1158 |
array_eo[(n_rows - 1 - i) * stride + q] = |
| 1159 |
0.5 * |
| 1160 |
(array[i * n_cols + q] - array[i * n_cols + n_cols - 1 - q]); |
| 1161 |
} |
| 1162 |
if ((n_rows - 1) % 2 == 0) |
| 1163 |
for (unsigned int q = 0; q < stride; ++q) |
| 1164 |
{ |
| 1165 |
array_eo[(n_rows - 1) / 2 * stride + q] = |
| 1166 |
array[((n_rows - 1) / 2) * n_cols + q]; |
| 1167 |
} |
| 1168 |
|
| 1169 |
return array_eo; |
| 1170 |
}; |
| 1171 |
|
| 1172 |
shape_values_eo = |
| 1173 |
convert_to_eo(shape_values, fe_degree + 1, n_q_points_1d); |
| 1174 |
shape_gradients_eo = |
| 1175 |
convert_to_eo(shape_gradients, fe_degree + 1, n_q_points_1d); |
| 1176 |
shape_hessians_eo = |
| 1177 |
convert_to_eo(shape_hessians, fe_degree + 1, n_q_points_1d); |
| 1178 |
|
| 1179 |
// Avoid underflow of Lagrange polynomials on typical quadrature |
| 1180 |
// formulas (see also above where shape_gradients_collocation and |
| 1181 |
// shape_hessians_collocation is set up). |
| 1182 |
if (n_q_points_1d < 200) |
| 1183 |
{ |
| 1184 |
shape_gradients_collocation_eo = |
| 1185 |
convert_to_eo(shape_gradients_collocation, |
| 1186 |
n_q_points_1d, |
| 1187 |
n_q_points_1d); |
| 1188 |
shape_hessians_collocation_eo = |
| 1189 |
convert_to_eo(shape_hessians_collocation, |
| 1190 |
n_q_points_1d, |
| 1191 |
n_q_points_1d); |
| 1192 |
inverse_shape_values_eo = |
| 1193 |
convert_to_eo(inverse_shape_values, fe_degree + 1, n_q_points_1d); |
| 1194 |
} |
| 1195 |
|
| 1196 |
return true; |
| 1197 |
} |
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
template <typename Number> |
| 1202 |
bool |
| 1203 |
UnivariateShapeData<Number>::check_shapes_collocation() const |
| 1204 |
{ |
| 1205 |
if (fe_degree + 1 != n_q_points_1d) |
| 1206 |
return false; |
| 1207 |
|
| 1208 |
const double zero_tol = |
| 1209 |
std::is_same_v<Number, double> == true ? 1e-12 : 1e-7; |
| 1210 |
// check: identity operation for shape values |
| 1211 |
const unsigned int n_points_1d = fe_degree + 1; |
| 1212 |
for (unsigned int i = 0; i < n_points_1d; ++i) |
| 1213 |
for (unsigned int j = 0; j < n_points_1d; ++j) |
| 1214 |
if (i != j) |
| 1215 |
{ |
| 1216 |
if (std::abs(get_first_array_element( |
| 1217 |
shape_values[i * n_points_1d + j])) > zero_tol) |
| 1218 |
return false; |
| 1219 |
} |
| 1220 |
else |
| 1221 |
{ |
| 1222 |
if (std::abs( |
| 1223 |
get_first_array_element(shape_values[i * n_points_1d + j]) - |
| 1224 |
1.) > zero_tol) |
| 1225 |
return false; |
| 1226 |
} |
| 1227 |
return true; |
| 1228 |
} |
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
template <typename Number> |
| 1233 |
template <int dim, int spacedim> |
| 1234 |
bool |
| 1235 |
ShapeInfo<Number>::is_supported(const FiniteElement<dim, spacedim> &fe) |
| 1236 |
{ |
| 1237 |
if (dynamic_cast<const FE_RaviartThomasNodal<dim> *>(&fe)) |
| 1238 |
return true; |
| 1239 |
|
| 1240 |
for (unsigned int base = 0; base < fe.n_base_elements(); ++base) |
| 1241 |
{ |
| 1242 |
const FiniteElement<dim, spacedim> *fe_ptr = &(fe.base_element(base)); |
| 1243 |
if (fe_ptr->n_components() != 1) |
| 1244 |
return false; |
| 1245 |
|
| 1246 |
// then check if the base element is supported or not |
| 1247 |
if (dynamic_cast<const FE_Poly<dim, spacedim> *>(fe_ptr) != nullptr) |
| 1248 |
{ |
| 1249 |
const FE_Poly<dim, spacedim> *fe_poly_ptr = |
| 1250 |
dynamic_cast<const FE_Poly<dim, spacedim> *>(fe_ptr); |
| 1251 |
// Simplices are a special case since the polynomial family is not |
| 1252 |
// indicative of their support |
| 1253 |
if (dynamic_cast<const FE_SimplexPoly<dim, spacedim> *>( |
| 1254 |
fe_poly_ptr) != nullptr || |
| 1255 |
dynamic_cast<const FE_WedgePoly<dim, spacedim> *>( |
| 1256 |
fe_poly_ptr) != nullptr || |
| 1257 |
dynamic_cast<const FE_PyramidPoly<dim, spacedim> *>( |
| 1258 |
fe_poly_ptr) != nullptr) |
| 1259 |
return true; |
| 1260 |
|
| 1261 |
if (dynamic_cast<const TensorProductPolynomials< |
| 1262 |
dim, |
| 1263 |
Polynomials::Polynomial<double>> *>( |
| 1264 |
&fe_poly_ptr->get_poly_space()) == nullptr && |
| 1265 |
dynamic_cast<const TensorProductPolynomials< |
| 1266 |
dim, |
| 1267 |
Polynomials::PiecewisePolynomial<double>> *>( |
| 1268 |
&fe_poly_ptr->get_poly_space()) == nullptr && |
| 1269 |
dynamic_cast<const FE_DGP<dim, spacedim> *>(fe_ptr) == |
| 1270 |
nullptr && |
| 1271 |
dynamic_cast<const FE_Q_DG0<dim, spacedim> *>(fe_ptr) == |
| 1272 |
nullptr) |
| 1273 |
return false; |
| 1274 |
} |
| 1275 |
else if (dynamic_cast<const FE_Nothing<dim, spacedim> *>(fe_ptr) != |
| 1276 |
nullptr) |
| 1277 |
return true; |
| 1278 |
else |
| 1279 |
return false; |
| 1280 |
} |
| 1281 |
|
| 1282 |
// if we arrived here, all base elements were supported so we can |
| 1283 |
// support the present element |
| 1284 |
return true; |
| 1285 |
} |
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
template <typename Number> |
| 1290 |
Table<2, unsigned int> |
| 1291 |
ShapeInfo<Number>::compute_orientation_table(const unsigned int n) |
| 1292 |
{ |
| 1293 |
Table<2, unsigned int> face_orientations(8, n * n); |
| 1294 |
for (unsigned int j = 0, i = 0; j < n; ++j) |
| 1295 |
for (unsigned int k = 0; k < n; ++k, ++i) |
| 1296 |
{ |
| 1297 |
// face_orientation=true, face_rotation=false, face_flip=false |
| 1298 |
face_orientations[0][i] = i; |
| 1299 |
// face_orientation=false, face_rotation=false, face_flip=false |
| 1300 |
face_orientations[1][i] = j + k * n; |
| 1301 |
// face_orientation=true, face_rotation=true, face_flip=false |
| 1302 |
face_orientations[2][i] = j + (n - 1 - k) * n; |
| 1303 |
// face_orientation=false, face_rotation=true, face_flip=false |
| 1304 |
face_orientations[3][i] = k + (n - 1 - j) * n; |
| 1305 |
// face_orientation=true, face_rotation=false, face_flip=true |
| 1306 |
face_orientations[4][i] = (n - 1 - k) + (n - 1 - j) * n; |
| 1307 |
// face_orientation=false, face_rotation=false, face_flip=true |
| 1308 |
face_orientations[5][i] = (n - 1 - j) + (n - 1 - k) * n; |
| 1309 |
// face_orientation=true, face_rotation=true, face_flip=true |
| 1310 |
face_orientations[6][i] = (n - 1 - j) + k * n; |
| 1311 |
// face_orientation=false, face_rotation=true, face_flip=true |
| 1312 |
face_orientations[7][i] = (n - 1 - k) + j * n; |
| 1313 |
} |
| 1314 |
return face_orientations; |
| 1315 |
} |
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
template <typename Number> |
| 1320 |
std::size_t |
| 1321 |
ShapeInfo<Number>::memory_consumption() const |
| 1322 |
{ |
| 1323 |
std::size_t memory = sizeof(*this); |
| 1324 |
for (const auto &univariate_shape_data : data) |
| 1325 |
memory += univariate_shape_data.memory_consumption(); |
| 1326 |
return memory; |
| 1327 |
} |
| 1328 |
|
| 1329 |
template <typename Number> |
| 1330 |
std::size_t |
| 1331 |
UnivariateShapeData<Number>::memory_consumption() const |
| 1332 |
{ |
| 1333 |
std::size_t memory = sizeof(*this); |
| 1334 |
memory += MemoryConsumption::memory_consumption(shape_values); |
| 1335 |
memory += MemoryConsumption::memory_consumption(shape_gradients); |
| 1336 |
memory += MemoryConsumption::memory_consumption(shape_hessians); |
| 1337 |
memory += |
| 1338 |
MemoryConsumption::memory_consumption(shape_gradients_collocation); |
| 1339 |
memory += |
| 1340 |
MemoryConsumption::memory_consumption(shape_hessians_collocation); |
| 1341 |
memory += MemoryConsumption::memory_consumption(shape_values_eo); |
| 1342 |
memory += MemoryConsumption::memory_consumption(shape_gradients_eo); |
| 1343 |
memory += MemoryConsumption::memory_consumption(shape_hessians_eo); |
| 1344 |
memory += |
| 1345 |
MemoryConsumption::memory_consumption(shape_gradients_collocation_eo); |
| 1346 |
memory += |
| 1347 |
MemoryConsumption::memory_consumption(shape_hessians_collocation_eo); |
| 1348 |
for (unsigned int i = 0; i < 2; ++i) |
| 1349 |
{ |
| 1350 |
memory += |
| 1351 |
MemoryConsumption::memory_consumption(shape_data_on_face[i]); |
| 1352 |
memory += |
| 1353 |
MemoryConsumption::memory_consumption(quadrature_data_on_face[i]); |
| 1354 |
memory += |
| 1355 |
MemoryConsumption::memory_consumption(values_within_subface[i]); |
| 1356 |
memory += |
| 1357 |
MemoryConsumption::memory_consumption(gradients_within_subface[i]); |
| 1358 |
} |
| 1359 |
return memory; |
| 1360 |
} |
| 1361 |
|
| 1362 |
} // namespace MatrixFreeFunctions |
| 1363 |
|
| 1364 |
} // namespace internal |
| 1365 |
|
| 1366 |
|
| 1367 |
DEAL_II_NAMESPACE_CLOSE |
| 1368 |
|
| 1369 |
#endif |