| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2020 - 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 |
|
| 16 |
#ifndef dealii_vector_tools_integrate_difference_templates_h |
| 17 |
#define dealii_vector_tools_integrate_difference_templates_h |
| 18 |
|
| 19 |
|
| 20 |
#include <deal.II/hp/fe_values.h> |
| 21 |
|
| 22 |
#include <deal.II/lac/block_vector.h> |
| 23 |
#include <deal.II/lac/la_parallel_block_vector.h> |
| 24 |
#include <deal.II/lac/la_parallel_vector.h> |
| 25 |
#include <deal.II/lac/petsc_block_vector.h> |
| 26 |
#include <deal.II/lac/petsc_vector.h> |
| 27 |
#include <deal.II/lac/trilinos_parallel_block_vector.h> |
| 28 |
#include <deal.II/lac/trilinos_vector.h> |
| 29 |
|
| 30 |
#include <deal.II/numerics/vector_tools_integrate_difference.h> |
| 31 |
|
| 32 |
#include <limits> |
| 33 |
|
| 34 |
DEAL_II_NAMESPACE_OPEN |
| 35 |
|
| 36 |
namespace VectorTools |
| 37 |
{ |
| 38 |
namespace internal |
| 39 |
{ |
| 40 |
template <int dim, int spacedim, typename Number> |
| 41 |
struct IDScratchData |
| 42 |
{ |
| 43 |
IDScratchData(const dealii::hp::MappingCollection<dim, spacedim> &mapping, |
| 44 |
const dealii::hp::FECollection<dim, spacedim> &fe, |
| 45 |
const dealii::hp::QCollection<dim> &q, |
| 46 |
const UpdateFlags update_flags); |
| 47 |
|
| 48 |
IDScratchData(const IDScratchData &data); |
| 49 |
|
| 50 |
void |
| 51 |
resize_vectors(const unsigned int n_q_points, |
| 52 |
const unsigned int n_components); |
| 53 |
|
| 54 |
std::vector<Vector<Number>> function_values; |
| 55 |
std::vector<std::vector<Tensor<1, spacedim, Number>>> function_grads; |
| 56 |
std::vector<double> weight_values; |
| 57 |
std::vector<Vector<double>> weight_vectors; |
| 58 |
|
| 59 |
std::vector<Vector<Number>> psi_values; |
| 60 |
std::vector<std::vector<Tensor<1, spacedim, Number>>> psi_grads; |
| 61 |
std::vector<Number> psi_scalar; |
| 62 |
|
| 63 |
std::vector<Number> tmp_values; |
| 64 |
std::vector<Vector<Number>> tmp_vector_values; |
| 65 |
std::vector<Tensor<1, spacedim, Number>> tmp_gradients; |
| 66 |
std::vector<std::vector<Tensor<1, spacedim, Number>>> |
| 67 |
tmp_vector_gradients; |
| 68 |
|
| 69 |
dealii::hp::FEValues<dim, spacedim> x_fe_values; |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
template <int dim, int spacedim, typename Number> |
| 74 |
IDScratchData<dim, spacedim, Number>::IDScratchData( |
| 75 |
const dealii::hp::MappingCollection<dim, spacedim> &mapping, |
| 76 |
const dealii::hp::FECollection<dim, spacedim> &fe, |
| 77 |
const dealii::hp::QCollection<dim> &q, |
| 78 |
const UpdateFlags update_flags) |
| 79 |
: x_fe_values(mapping, fe, q, update_flags) |
| 80 |
{} |
| 81 |
|
| 82 |
template <int dim, int spacedim, typename Number> |
| 83 |
IDScratchData<dim, spacedim, Number>::IDScratchData( |
| 84 |
const IDScratchData &data) |
| 85 |
: x_fe_values(data.x_fe_values.get_mapping_collection(), |
| 86 |
data.x_fe_values.get_fe_collection(), |
| 87 |
data.x_fe_values.get_quadrature_collection(), |
| 88 |
data.x_fe_values.get_update_flags()) |
| 89 |
{} |
| 90 |
|
| 91 |
template <int dim, int spacedim, typename Number> |
| 92 |
void |
| 93 |
IDScratchData<dim, spacedim, Number>::resize_vectors( |
| 94 |
const unsigned int n_q_points, |
| 95 |
const unsigned int n_components) |
| 96 |
{ |
| 97 |
function_values.resize(n_q_points, Vector<Number>(n_components)); |
| 98 |
function_grads.resize( |
| 99 |
n_q_points, std::vector<Tensor<1, spacedim, Number>>(n_components)); |
| 100 |
|
| 101 |
weight_values.resize(n_q_points); |
| 102 |
weight_vectors.resize(n_q_points, Vector<double>(n_components)); |
| 103 |
|
| 104 |
psi_values.resize(n_q_points, Vector<Number>(n_components)); |
| 105 |
psi_grads.resize(n_q_points, |
| 106 |
std::vector<Tensor<1, spacedim, Number>>(n_components)); |
| 107 |
psi_scalar.resize(n_q_points); |
| 108 |
|
| 109 |
tmp_values.resize(n_q_points); |
| 110 |
tmp_vector_values.resize(n_q_points, Vector<Number>(n_components)); |
| 111 |
tmp_gradients.resize(n_q_points); |
| 112 |
tmp_vector_gradients.resize( |
| 113 |
n_q_points, std::vector<Tensor<1, spacedim, Number>>(n_components)); |
| 114 |
} |
| 115 |
|
| 116 |
namespace internal |
| 117 |
{ |
| 118 |
template <typename number> |
| 119 |
double |
| 120 |
mean_to_double(const number &mean_value) |
| 121 |
{ |
| 122 |
return mean_value; |
| 123 |
} |
| 124 |
|
| 125 |
template <typename number> |
| 126 |
double |
| 127 |
mean_to_double(const std::complex<number> &mean_value) |
| 128 |
{ |
| 129 |
// we need to return double as a norm, but mean value is a complex |
| 130 |
// number. Panic and return real-part while warning the user that |
| 131 |
// they shall never do that. |
| 132 |
Assert( |
| 133 |
false, |
| 134 |
ExcMessage( |
| 135 |
"Mean value norm is not implemented for complex-valued vectors")); |
| 136 |
return mean_value.real(); |
| 137 |
} |
| 138 |
} // namespace internal |
| 139 |
|
| 140 |
|
| 141 |
// avoid compiling inner function for many vector types when we always |
| 142 |
// really do the same thing by putting the main work into this helper |
| 143 |
// function |
| 144 |
template <int dim, int spacedim, typename Number> |
| 145 |
double |
| 146 |
integrate_difference_inner(const Function<spacedim, Number> &exact_solution, |
| 147 |
const NormType &norm, |
| 148 |
const Function<spacedim> *weight, |
| 149 |
const UpdateFlags update_flags, |
| 150 |
const double exponent, |
| 151 |
const unsigned int n_components, |
| 152 |
IDScratchData<dim, spacedim, Number> &data) |
| 153 |
{ |
| 154 |
const bool fe_is_system = (n_components != 1); |
| 155 |
const dealii::FEValues<dim, spacedim> &fe_values = |
| 156 |
data.x_fe_values.get_present_fe_values(); |
| 157 |
|
| 158 |
if (weight != nullptr) |
| 159 |
{ |
| 160 |
if (weight->n_components > 1) |
| 161 |
weight->vector_value_list(fe_values.get_quadrature_points(), |
| 162 |
data.weight_vectors); |
| 163 |
else |
| 164 |
{ |
| 165 |
weight->value_list(fe_values.get_quadrature_points(), |
| 166 |
data.weight_values); |
| 167 |
for (const auto k : fe_values.quadrature_point_indices()) |
| 168 |
data.weight_vectors[k] = data.weight_values[k]; |
| 169 |
} |
| 170 |
} |
| 171 |
else |
| 172 |
{ |
| 173 |
for (const auto k : fe_values.quadrature_point_indices()) |
| 174 |
data.weight_vectors[k] = 1.; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
if (update_flags & update_values) |
| 179 |
{ |
| 180 |
// first compute the exact solution (vectors) at the quadrature |
| 181 |
// points. try to do this as efficient as possible by avoiding a |
| 182 |
// second virtual function call in case the function really has only |
| 183 |
// one component |
| 184 |
// |
| 185 |
// TODO: we have to work a bit here because the Function<dim,double> |
| 186 |
// interface of the argument denoting the exact function only |
| 187 |
// provides us with double/Tensor<1,dim> values, rather than |
| 188 |
// with the correct data type. so evaluate into a temp |
| 189 |
// object, then copy around |
| 190 |
if (fe_is_system) |
| 191 |
{ |
| 192 |
exact_solution.vector_value_list( |
| 193 |
fe_values.get_quadrature_points(), data.tmp_vector_values); |
| 194 |
for (const auto i : fe_values.quadrature_point_indices()) |
| 195 |
data.psi_values[i] = data.tmp_vector_values[i]; |
| 196 |
} |
| 197 |
else |
| 198 |
{ |
| 199 |
exact_solution.value_list(fe_values.get_quadrature_points(), |
| 200 |
data.tmp_values); |
| 201 |
for (const auto i : fe_values.quadrature_point_indices()) |
| 202 |
data.psi_values[i](0) = data.tmp_values[i]; |
| 203 |
} |
| 204 |
|
| 205 |
// then subtract finite element fe_function |
| 206 |
for (const auto q : fe_values.quadrature_point_indices()) |
| 207 |
for (unsigned int i = 0; i < data.psi_values[q].size(); ++i) |
| 208 |
data.psi_values[q][i] -= data.function_values[q][i]; |
| 209 |
} |
| 210 |
|
| 211 |
// Do the same for gradients, if required |
| 212 |
if (update_flags & update_gradients) |
| 213 |
{ |
| 214 |
// try to be a little clever to avoid recursive virtual function |
| 215 |
// calls when calling gradient_list for functions that are really |
| 216 |
// scalar functions |
| 217 |
if (fe_is_system) |
| 218 |
{ |
| 219 |
exact_solution.vector_gradient_list( |
| 220 |
fe_values.get_quadrature_points(), data.tmp_vector_gradients); |
| 221 |
for (const auto i : fe_values.quadrature_point_indices()) |
| 222 |
for (unsigned int comp = 0; comp < data.psi_grads[i].size(); |
| 223 |
++comp) |
| 224 |
data.psi_grads[i][comp] = data.tmp_vector_gradients[i][comp]; |
| 225 |
} |
| 226 |
else |
| 227 |
{ |
| 228 |
exact_solution.gradient_list(fe_values.get_quadrature_points(), |
| 229 |
data.tmp_gradients); |
| 230 |
for (const auto i : fe_values.quadrature_point_indices()) |
| 231 |
data.psi_grads[i][0] = data.tmp_gradients[i]; |
| 232 |
} |
| 233 |
|
| 234 |
// then subtract finite element function_grads. We need to be |
| 235 |
// careful in the codimension one case, since there we only have |
| 236 |
// tangential gradients in the finite element function, not the full |
| 237 |
// gradient. This is taken care of, by subtracting the normal |
| 238 |
// component of the gradient from the exact function. |
| 239 |
if (update_flags & update_normal_vectors) |
| 240 |
for (unsigned int k = 0; k < n_components; ++k) |
| 241 |
for (const auto q : fe_values.quadrature_point_indices()) |
| 242 |
{ |
| 243 |
// compute (f.n) n |
| 244 |
const typename ProductType<Number, double>::type f_dot_n = |
| 245 |
data.psi_grads[q][k] * fe_values.normal_vector(q); |
| 246 |
const Tensor<1, spacedim, Number> f_dot_n_times_n = |
| 247 |
f_dot_n * fe_values.normal_vector(q); |
| 248 |
|
| 249 |
data.psi_grads[q][k] -= |
| 250 |
(data.function_grads[q][k] + f_dot_n_times_n); |
| 251 |
} |
| 252 |
else |
| 253 |
for (unsigned int k = 0; k < n_components; ++k) |
| 254 |
for (const auto q : fe_values.quadrature_point_indices()) |
| 255 |
for (unsigned int d = 0; d < spacedim; ++d) |
| 256 |
data.psi_grads[q][k][d] -= data.function_grads[q][k][d]; |
| 257 |
} |
| 258 |
|
| 259 |
double diff = 0; |
| 260 |
Number diff_mean = 0; |
| 261 |
|
| 262 |
// First work on function values: |
| 263 |
switch (norm) |
| 264 |
{ |
| 265 |
case mean: |
| 266 |
// Compute values in quadrature points and integrate |
| 267 |
for (const auto q : fe_values.quadrature_point_indices()) |
| 268 |
{ |
| 269 |
Number sum = 0; |
| 270 |
for (unsigned int k = 0; k < n_components; ++k) |
| 271 |
if (data.weight_vectors[q](k) != 0) |
| 272 |
sum += data.psi_values[q](k) * data.weight_vectors[q](k); |
| 273 |
diff_mean += sum * fe_values.JxW(q); |
| 274 |
} |
| 275 |
break; |
| 276 |
|
| 277 |
case Lp_norm: |
| 278 |
case L1_norm: |
| 279 |
case W1p_norm: |
| 280 |
// Compute values in quadrature points and integrate |
| 281 |
for (const auto q : fe_values.quadrature_point_indices()) |
| 282 |
{ |
| 283 |
double sum = 0; |
| 284 |
for (unsigned int k = 0; k < n_components; ++k) |
| 285 |
if (data.weight_vectors[q](k) != 0) |
| 286 |
sum += std::pow(static_cast<double>( |
| 287 |
numbers::NumberTraits<Number>::abs_square( |
| 288 |
data.psi_values[q](k))), |
| 289 |
exponent / 2.) * |
| 290 |
data.weight_vectors[q](k); |
| 291 |
diff += sum * fe_values.JxW(q); |
| 292 |
} |
| 293 |
|
| 294 |
// Compute the root only if no derivative values are added later |
| 295 |
if (!(update_flags & update_gradients)) |
| 296 |
diff = std::pow(diff, 1. / exponent); |
| 297 |
break; |
| 298 |
|
| 299 |
case L2_norm: |
| 300 |
case H1_norm: |
| 301 |
// Compute values in quadrature points and integrate |
| 302 |
for (const auto q : fe_values.quadrature_point_indices()) |
| 303 |
{ |
| 304 |
double sum = 0; |
| 305 |
for (unsigned int k = 0; k < n_components; ++k) |
| 306 |
if (data.weight_vectors[q](k) != 0) |
| 307 |
sum += numbers::NumberTraits<Number>::abs_square( |
| 308 |
data.psi_values[q](k)) * |
| 309 |
data.weight_vectors[q](k); |
| 310 |
diff += sum * fe_values.JxW(q); |
| 311 |
} |
| 312 |
// Compute the root only, if no derivative values are added later |
| 313 |
if (norm == L2_norm) |
| 314 |
diff = std::sqrt(diff); |
| 315 |
break; |
| 316 |
|
| 317 |
case Linfty_norm: |
| 318 |
case W1infty_norm: |
| 319 |
for (const auto q : fe_values.quadrature_point_indices()) |
| 320 |
for (unsigned int k = 0; k < n_components; ++k) |
| 321 |
if (data.weight_vectors[q](k) != 0) |
| 322 |
diff = std::max(diff, |
| 323 |
double(std::abs(data.psi_values[q](k) * |
| 324 |
data.weight_vectors[q](k)))); |
| 325 |
break; |
| 326 |
|
| 327 |
case H1_seminorm: |
| 328 |
case Hdiv_seminorm: |
| 329 |
case W1p_seminorm: |
| 330 |
case W1infty_seminorm: |
| 331 |
// function values are not used for these norms |
| 332 |
break; |
| 333 |
|
| 334 |
default: |
| 335 |
DEAL_II_NOT_IMPLEMENTED(); |
| 336 |
break; |
| 337 |
} |
| 338 |
|
| 339 |
// Now compute terms depending on derivatives: |
| 340 |
switch (norm) |
| 341 |
{ |
| 342 |
case W1p_seminorm: |
| 343 |
case W1p_norm: |
| 344 |
for (const auto q : fe_values.quadrature_point_indices()) |
| 345 |
{ |
| 346 |
double sum = 0; |
| 347 |
for (unsigned int k = 0; k < n_components; ++k) |
| 348 |
if (data.weight_vectors[q](k) != 0) |
| 349 |
sum += std::pow(data.psi_grads[q][k].norm_square(), |
| 350 |
exponent / 2.) * |
| 351 |
data.weight_vectors[q](k); |
| 352 |
diff += sum * fe_values.JxW(q); |
| 353 |
} |
| 354 |
diff = std::pow(diff, 1. / exponent); |
| 355 |
break; |
| 356 |
|
| 357 |
case H1_seminorm: |
| 358 |
case H1_norm: |
| 359 |
for (const auto q : fe_values.quadrature_point_indices()) |
| 360 |
{ |
| 361 |
double sum = 0; |
| 362 |
for (unsigned int k = 0; k < n_components; ++k) |
| 363 |
if (data.weight_vectors[q](k) != 0) |
| 364 |
sum += data.psi_grads[q][k].norm_square() * |
| 365 |
data.weight_vectors[q](k); |
| 366 |
diff += sum * fe_values.JxW(q); |
| 367 |
} |
| 368 |
diff = std::sqrt(diff); |
| 369 |
break; |
| 370 |
|
| 371 |
case Hdiv_seminorm: |
| 372 |
for (const auto q : fe_values.quadrature_point_indices()) |
| 373 |
{ |
| 374 |
unsigned int idx = 0; |
| 375 |
if (weight != nullptr) |
| 376 |
for (; idx < n_components; ++idx) |
| 377 |
if (data.weight_vectors[0](idx) > 0) |
| 378 |
break; |
| 379 |
|
| 380 |
Assert( |
| 381 |
n_components >= idx + dim, |
| 382 |
ExcMessage( |
| 383 |
"You can only ask for the Hdiv norm for a finite element " |
| 384 |
"with at least 'dim' components. In that case, this function " |
| 385 |
"will find the index of the first non-zero weight and take " |
| 386 |
"the divergence of the 'dim' components that follow it.")); |
| 387 |
|
| 388 |
Number sum = 0; |
| 389 |
// take the trace of the derivatives scaled by the weight and |
| 390 |
// square it |
| 391 |
for (unsigned int k = idx; k < idx + dim; ++k) |
| 392 |
if (data.weight_vectors[q](k) != 0) |
| 393 |
sum += data.psi_grads[q][k][k - idx] * |
| 394 |
std::sqrt(data.weight_vectors[q](k)); |
| 395 |
diff += numbers::NumberTraits<Number>::abs_square(sum) * |
| 396 |
fe_values.JxW(q); |
| 397 |
} |
| 398 |
diff = std::sqrt(diff); |
| 399 |
break; |
| 400 |
|
| 401 |
case W1infty_seminorm: |
| 402 |
case W1infty_norm: |
| 403 |
{ |
| 404 |
double t = 0; |
| 405 |
for (const auto q : fe_values.quadrature_point_indices()) |
| 406 |
for (unsigned int k = 0; k < n_components; ++k) |
| 407 |
if (data.weight_vectors[q](k) != 0) |
| 408 |
for (unsigned int d = 0; d < dim; ++d) |
| 409 |
t = std::max(t, |
| 410 |
double(std::abs(data.psi_grads[q][k][d]) * |
| 411 |
data.weight_vectors[q](k))); |
| 412 |
|
| 413 |
// then add seminorm to norm if that had previously been |
| 414 |
// computed |
| 415 |
diff += t; |
| 416 |
} |
| 417 |
break; |
| 418 |
default: |
| 419 |
break; |
| 420 |
} |
| 421 |
|
| 422 |
if (norm == mean) |
| 423 |
diff = internal::mean_to_double(diff_mean); |
| 424 |
|
| 425 |
// append result of this cell to the end of the vector |
| 426 |
AssertIsFinite(diff); |
| 427 |
return diff; |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
template <int dim, int spacedim, typename Number, class OutVector> |
| 433 |
DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<OutVector>) |
| 434 |
void do_integrate_difference( |
| 435 |
const dealii::hp::MappingCollection<dim, spacedim> &mapping, |
| 436 |
const DoFHandler<dim, spacedim> &dof, |
| 437 |
const ReadVector<Number> &fe_function, |
| 438 |
const Function<spacedim, Number> &exact_solution, |
| 439 |
OutVector &difference, |
| 440 |
const dealii::hp::QCollection<dim> &q, |
| 441 |
const NormType &norm, |
| 442 |
const Function<spacedim> *weight, |
| 443 |
const double exponent_1) |
| 444 |
{ |
| 445 |
// we mark the "exponent" parameter to this function "const" since it is |
| 446 |
// strictly incoming, but we need to set it to something different later |
| 447 |
// on, if necessary, so have a read-write version of it: |
| 448 |
double exponent = exponent_1; |
| 449 |
|
| 450 |
const unsigned int n_components = dof.get_fe(0).n_components(); |
| 451 |
|
| 452 |
AssertDimension(exact_solution.n_components, n_components); |
| 453 |
|
| 454 |
if (weight != nullptr) |
| 455 |
{ |
| 456 |
Assert((weight->n_components == 1) || |
| 457 |
(weight->n_components == n_components), |
| 458 |
ExcDimensionMismatch(weight->n_components, n_components)); |
| 459 |
} |
| 460 |
|
| 461 |
difference.reinit(dof.get_triangulation().n_active_cells()); |
| 462 |
|
| 463 |
switch (norm) |
| 464 |
{ |
| 465 |
case L2_norm: |
| 466 |
case H1_seminorm: |
| 467 |
case H1_norm: |
| 468 |
case Hdiv_seminorm: |
| 469 |
exponent = 2.; |
| 470 |
break; |
| 471 |
|
| 472 |
case L1_norm: |
| 473 |
exponent = 1.; |
| 474 |
break; |
| 475 |
|
| 476 |
default: |
| 477 |
break; |
| 478 |
} |
| 479 |
|
| 480 |
UpdateFlags update_flags = |
| 481 |
UpdateFlags(update_quadrature_points | update_JxW_values); |
| 482 |
switch (norm) |
| 483 |
{ |
| 484 |
case H1_seminorm: |
| 485 |
case Hdiv_seminorm: |
| 486 |
case W1p_seminorm: |
| 487 |
case W1infty_seminorm: |
| 488 |
update_flags |= UpdateFlags(update_gradients); |
| 489 |
if (spacedim == dim + 1) |
| 490 |
update_flags |= UpdateFlags(update_normal_vectors); |
| 491 |
|
| 492 |
break; |
| 493 |
|
| 494 |
case H1_norm: |
| 495 |
case W1p_norm: |
| 496 |
case W1infty_norm: |
| 497 |
update_flags |= UpdateFlags(update_gradients); |
| 498 |
if (spacedim == dim + 1) |
| 499 |
update_flags |= UpdateFlags(update_normal_vectors); |
| 500 |
DEAL_II_FALLTHROUGH; |
| 501 |
|
| 502 |
default: |
| 503 |
update_flags |= UpdateFlags(update_values); |
| 504 |
break; |
| 505 |
} |
| 506 |
|
| 507 |
const dealii::hp::FECollection<dim, spacedim> &fe_collection = |
| 508 |
dof.get_fe_collection(); |
| 509 |
IDScratchData<dim, spacedim, Number> data(mapping, |
| 510 |
fe_collection, |
| 511 |
q, |
| 512 |
update_flags); |
| 513 |
|
| 514 |
// loop over all cells |
| 515 |
for (const auto &cell : dof.active_cell_iterators()) |
| 516 |
if (cell->is_locally_owned()) |
| 517 |
{ |
| 518 |
// initialize for this cell |
| 519 |
data.x_fe_values.reinit(cell); |
| 520 |
|
| 521 |
const dealii::FEValues<dim, spacedim> &fe_values = |
| 522 |
data.x_fe_values.get_present_fe_values(); |
| 523 |
const unsigned int n_q_points = fe_values.n_quadrature_points; |
| 524 |
data.resize_vectors(n_q_points, n_components); |
| 525 |
|
| 526 |
if (update_flags & update_values) |
| 527 |
fe_values.get_function_values(fe_function, data.function_values); |
| 528 |
if (update_flags & update_gradients) |
| 529 |
fe_values.get_function_gradients(fe_function, |
| 530 |
data.function_grads); |
| 531 |
|
| 532 |
difference(cell->active_cell_index()) = |
| 533 |
integrate_difference_inner<dim, spacedim, Number>(exact_solution, |
| 534 |
norm, |
| 535 |
weight, |
| 536 |
update_flags, |
| 537 |
exponent, |
| 538 |
n_components, |
| 539 |
data); |
| 540 |
} |
| 541 |
else |
| 542 |
// the cell is a ghost cell or is artificial. write a zero into the |
| 543 |
// corresponding value of the returned vector |
| 544 |
difference(cell->active_cell_index()) = 0; |
| 545 |
} |
| 546 |
|
| 547 |
} // namespace internal |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
template <int dim, typename Number, class OutVector, int spacedim> |
| 552 |
DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<OutVector>) |
| 553 |
void integrate_difference(const Mapping<dim, spacedim> &mapping, |
| 554 |
const DoFHandler<dim, spacedim> &dof, |
| 555 |
const ReadVector<Number> &fe_function, |
| 556 |
const Function<spacedim, Number> &exact_solution, |
| 557 |
OutVector &difference, |
| 558 |
const Quadrature<dim> &q, |
| 559 |
const NormType &norm, |
| 560 |
const Function<spacedim> *weight, |
| 561 |
const double exponent) |
| 562 |
{ |
| 563 |
internal::do_integrate_difference(hp::MappingCollection<dim, spacedim>( |
| 564 |
mapping), |
| 565 |
dof, |
| 566 |
fe_function, |
| 567 |
exact_solution, |
| 568 |
difference, |
| 569 |
hp::QCollection<dim>(q), |
| 570 |
norm, |
| 571 |
weight, |
| 572 |
exponent); |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
template <int dim, typename Number, class OutVector, int spacedim> |
| 577 |
DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<OutVector>) |
| 578 |
void integrate_difference(const DoFHandler<dim, spacedim> &dof, |
| 579 |
const ReadVector<Number> &fe_function, |
| 580 |
const Function<spacedim, Number> &exact_solution, |
| 581 |
OutVector &difference, |
| 582 |
const Quadrature<dim> &q, |
| 583 |
const NormType &norm, |
| 584 |
const Function<spacedim> *weight, |
| 585 |
const double exponent) |
| 586 |
{ |
| 587 |
internal::do_integrate_difference( |
| 588 |
hp::StaticMappingQ1<dim, spacedim>::mapping_collection, |
| 589 |
dof, |
| 590 |
fe_function, |
| 591 |
exact_solution, |
| 592 |
difference, |
| 593 |
hp::QCollection<dim>(q), |
| 594 |
norm, |
| 595 |
weight, |
| 596 |
exponent); |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
template <int dim, typename Number, class OutVector, int spacedim> |
| 601 |
DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<OutVector>) |
| 602 |
void integrate_difference( |
| 603 |
const dealii::hp::MappingCollection<dim, spacedim> &mapping, |
| 604 |
const DoFHandler<dim, spacedim> &dof, |
| 605 |
const ReadVector<Number> &fe_function, |
| 606 |
const Function<spacedim, Number> &exact_solution, |
| 607 |
OutVector &difference, |
| 608 |
const dealii::hp::QCollection<dim> &q, |
| 609 |
const NormType &norm, |
| 610 |
const Function<spacedim> *weight, |
| 611 |
const double exponent) |
| 612 |
{ |
| 613 |
internal::do_integrate_difference(mapping, |
| 614 |
dof, |
| 615 |
fe_function, |
| 616 |
exact_solution, |
| 617 |
difference, |
| 618 |
q, |
| 619 |
norm, |
| 620 |
weight, |
| 621 |
exponent); |
| 622 |
} |
| 623 |
|
| 624 |
template <int dim, typename Number, class OutVector, int spacedim> |
| 625 |
DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<OutVector>) |
| 626 |
void integrate_difference(const DoFHandler<dim, spacedim> &dof, |
| 627 |
const ReadVector<Number> &fe_function, |
| 628 |
const Function<spacedim, Number> &exact_solution, |
| 629 |
OutVector &difference, |
| 630 |
const dealii::hp::QCollection<dim> &q, |
| 631 |
const NormType &norm, |
| 632 |
const Function<spacedim> *weight, |
| 633 |
const double exponent) |
| 634 |
{ |
| 635 |
internal::do_integrate_difference( |
| 636 |
hp::StaticMappingQ1<dim, spacedim>::mapping_collection, |
| 637 |
dof, |
| 638 |
fe_function, |
| 639 |
exact_solution, |
| 640 |
difference, |
| 641 |
q, |
| 642 |
norm, |
| 643 |
weight, |
| 644 |
exponent); |
| 645 |
} |
| 646 |
|
| 647 |
template <int dim, int spacedim, class InVector> |
| 648 |
DEAL_II_CXX20_REQUIRES(concepts::is_dealii_vector_type<InVector>) |
| 649 |
double compute_global_error(const Triangulation<dim, spacedim> &tria, |
| 650 |
const InVector &cellwise_error, |
| 651 |
const NormType &norm, |
| 652 |
const double exponent) |
| 653 |
{ |
| 654 |
Assert(cellwise_error.size() == tria.n_active_cells(), |
| 655 |
ExcMessage("input vector cell_error has invalid size!")); |
| 656 |
if constexpr (running_in_debug_mode()) |
| 657 |
{ |
| 658 |
{ |
| 659 |
// check that off-processor entries are zero. Otherwise we will |
| 660 |
// compute wrong results below! |
| 661 |
typename InVector::size_type i = 0; |
| 662 |
typename Triangulation<dim, spacedim>::active_cell_iterator it = |
| 663 |
tria.begin_active(); |
| 664 |
for (; i < cellwise_error.size(); ++i, ++it) |
| 665 |
if (!it->is_locally_owned()) |
| 666 |
Assert( |
| 667 |
std::fabs(cellwise_error[i]) < 1e-20, |
| 668 |
ExcMessage( |
| 669 |
"cellwise_error of cells that are not locally owned need to be zero!")); |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
const MPI_Comm comm = tria.get_mpi_communicator(); |
| 674 |
|
| 675 |
switch (norm) |
| 676 |
{ |
| 677 |
case L2_norm: |
| 678 |
case H1_seminorm: |
| 679 |
case H1_norm: |
| 680 |
case Hdiv_seminorm: |
| 681 |
{ |
| 682 |
const double local = cellwise_error.l2_norm(); |
| 683 |
return std::sqrt(Utilities::MPI::sum(local * local, comm)); |
| 684 |
} |
| 685 |
|
| 686 |
case L1_norm: |
| 687 |
{ |
| 688 |
const double local = cellwise_error.l1_norm(); |
| 689 |
return Utilities::MPI::sum(local, comm); |
| 690 |
} |
| 691 |
|
| 692 |
case Linfty_norm: |
| 693 |
case W1infty_seminorm: |
| 694 |
{ |
| 695 |
const double local = cellwise_error.linfty_norm(); |
| 696 |
return Utilities::MPI::max(local, comm); |
| 697 |
} |
| 698 |
|
| 699 |
case W1infty_norm: |
| 700 |
{ |
| 701 |
AssertThrow(false, |
| 702 |
ExcMessage( |
| 703 |
"compute_global_error() is impossible for " |
| 704 |
"the W1infty_norm. See the documentation for " |
| 705 |
"NormType::W1infty_norm for more information.")); |
| 706 |
return std::numeric_limits<double>::infinity(); |
| 707 |
} |
| 708 |
|
| 709 |
case mean: |
| 710 |
{ |
| 711 |
// Note: mean is defined as int_\Omega f = sum_K \int_K f, so we |
| 712 |
// need the sum of the cellwise errors not the Euclidean mean |
| 713 |
// value that is returned by Vector<>::mean_value(). |
| 714 |
const double local = |
| 715 |
cellwise_error.mean_value() * cellwise_error.size(); |
| 716 |
return Utilities::MPI::sum(local, comm); |
| 717 |
} |
| 718 |
|
| 719 |
case Lp_norm: |
| 720 |
case W1p_norm: |
| 721 |
case W1p_seminorm: |
| 722 |
{ |
| 723 |
double local = 0; |
| 724 |
typename InVector::size_type i; |
| 725 |
typename Triangulation<dim, spacedim>::active_cell_iterator it = |
| 726 |
tria.begin_active(); |
| 727 |
for (i = 0; i < cellwise_error.size(); ++i, ++it) |
| 728 |
if (it->is_locally_owned()) |
| 729 |
local += std::pow(cellwise_error[i], exponent); |
| 730 |
|
| 731 |
return std::pow(Utilities::MPI::sum(local, comm), 1. / exponent); |
| 732 |
} |
| 733 |
|
| 734 |
default: |
| 735 |
AssertThrow(false, ExcNotImplemented()); |
| 736 |
break; |
| 737 |
} |
| 738 |
return 0.0; |
| 739 |
} |
| 740 |
} // namespace VectorTools |
| 741 |
|
| 742 |
DEAL_II_NAMESPACE_CLOSE |
| 743 |
|
| 744 |
#endif // dealii_vector_tools_integrate_difference_templates_h |