| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 1998 - 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_error_estimator_templates_h |
| 16 |
#define dealii_error_estimator_templates_h |
| 17 |
|
| 18 |
#include <deal.II/base/config.h> |
| 19 |
|
| 20 |
#include <deal.II/base/geometry_info.h> |
| 21 |
#include <deal.II/base/numbers.h> |
| 22 |
#include <deal.II/base/quadrature.h> |
| 23 |
#include <deal.II/base/quadrature_lib.h> |
| 24 |
#include <deal.II/base/work_stream.h> |
| 25 |
|
| 26 |
#include <deal.II/distributed/tria_base.h> |
| 27 |
|
| 28 |
#include <deal.II/dofs/dof_accessor.h> |
| 29 |
#include <deal.II/dofs/dof_handler.h> |
| 30 |
|
| 31 |
#include <deal.II/fe/fe.h> |
| 32 |
#include <deal.II/fe/fe_update_flags.h> |
| 33 |
#include <deal.II/fe/fe_values.h> |
| 34 |
|
| 35 |
#include <deal.II/grid/tria_iterator.h> |
| 36 |
|
| 37 |
#include <deal.II/hp/fe_values.h> |
| 38 |
#include <deal.II/hp/mapping_collection.h> |
| 39 |
#include <deal.II/hp/q_collection.h> |
| 40 |
|
| 41 |
#include <deal.II/lac/block_vector.h> |
| 42 |
#include <deal.II/lac/la_parallel_block_vector.h> |
| 43 |
#include <deal.II/lac/la_parallel_vector.h> |
| 44 |
#include <deal.II/lac/petsc_block_vector.h> |
| 45 |
#include <deal.II/lac/petsc_vector.h> |
| 46 |
#include <deal.II/lac/trilinos_parallel_block_vector.h> |
| 47 |
#include <deal.II/lac/trilinos_vector.h> |
| 48 |
#include <deal.II/lac/vector.h> |
| 49 |
|
| 50 |
#include <deal.II/numerics/error_estimator.h> |
| 51 |
|
| 52 |
#include <algorithm> |
| 53 |
#include <cmath> |
| 54 |
#include <functional> |
| 55 |
#include <limits> |
| 56 |
#include <numeric> |
| 57 |
#include <vector> |
| 58 |
|
| 59 |
DEAL_II_NAMESPACE_OPEN |
| 60 |
|
| 61 |
namespace internal |
| 62 |
{ |
| 63 |
/** |
| 64 |
* All small temporary data objects that are needed once per thread by the |
| 65 |
* several functions of the error estimator are gathered in this struct. |
| 66 |
* The reason for this structure is mainly that we have a number of |
| 67 |
* functions that operate on cells or faces and need a number of small |
| 68 |
* temporary data objects. Since these functions may run in parallel, we |
| 69 |
* cannot make these objects member variables of the enclosing class. On |
| 70 |
* the other hand, declaring them locally in each of these functions would |
| 71 |
* require their reallocating every time we visit the next cell or face, |
| 72 |
* which we found can take a significant amount of time if it happens |
| 73 |
* often even in the single threaded case (10-20 per cent in our |
| 74 |
* measurements); however, most importantly, memory allocation requires |
| 75 |
* synchronization in multithreaded mode. While that is done by the C++ |
| 76 |
* library and has not to be handcoded, it nevertheless seriously damages |
| 77 |
* the ability to efficiently run the functions of this class in parallel, |
| 78 |
* since they are quite often blocked by these synchronization points, |
| 79 |
* slowing everything down by a factor of two or three. |
| 80 |
* |
| 81 |
* Thus, every thread gets an instance of this class to work with and |
| 82 |
* needs not allocate memory itself, or synchronize with other threads. |
| 83 |
* |
| 84 |
* The sizes of the arrays are initialized with the maximal number of |
| 85 |
* entries necessary for the hp-case. Within the loop over individual |
| 86 |
* cells, we then resize the arrays as necessary. Since for std::vector |
| 87 |
* resizing to a smaller size doesn't imply memory allocation, this is |
| 88 |
* fast. |
| 89 |
*/ |
| 90 |
template <int dim, int spacedim, typename number> |
| 91 |
struct ParallelData |
| 92 |
{ |
| 93 |
/** |
| 94 |
* The finite element to be used. |
| 95 |
*/ |
| 96 |
const dealii::hp::FECollection<dim, spacedim> finite_element; |
| 97 |
|
| 98 |
/** |
| 99 |
* The quadrature formulas to be used for the faces. |
| 100 |
*/ |
| 101 |
const dealii::hp::QCollection<dim - 1> face_quadratures; |
| 102 |
|
| 103 |
/** |
| 104 |
* FEFaceValues objects to integrate over the faces of the current and |
| 105 |
* potentially of neighbor cells. |
| 106 |
*/ |
| 107 |
dealii::hp::FEFaceValues<dim, spacedim> fe_face_values_cell; |
| 108 |
dealii::hp::FEFaceValues<dim, spacedim> fe_face_values_neighbor; |
| 109 |
dealii::hp::FESubfaceValues<dim, spacedim> fe_subface_values; |
| 110 |
|
| 111 |
/** |
| 112 |
* A vector to store the jump of the normal vectors in the quadrature |
| 113 |
* points for each of the solution vectors (i.e. a temporary value). |
| 114 |
* This vector is not allocated inside the functions that use it, but |
| 115 |
* rather globally, since memory allocation is slow, in particular in |
| 116 |
* presence of multiple threads where synchronization makes things even |
| 117 |
* slower. |
| 118 |
*/ |
| 119 |
std::vector<std::vector<std::vector<number>>> phi; |
| 120 |
|
| 121 |
/** |
| 122 |
* A vector for the gradients of the finite element function on one cell |
| 123 |
* |
| 124 |
* Let psi be a short name for <tt>a grad u_h</tt>, where the third |
| 125 |
* index be the component of the finite element, and the second index |
| 126 |
* the number of the quadrature point. The first index denotes the index |
| 127 |
* of the solution vector. |
| 128 |
*/ |
| 129 |
std::vector<std::vector<std::vector<Tensor<1, spacedim, number>>>> psi; |
| 130 |
|
| 131 |
/** |
| 132 |
* The same vector for a neighbor cell |
| 133 |
*/ |
| 134 |
std::vector<std::vector<std::vector<Tensor<1, spacedim, number>>>> |
| 135 |
neighbor_psi; |
| 136 |
|
| 137 |
/** |
| 138 |
* The normal vectors of the finite element function on one face |
| 139 |
*/ |
| 140 |
std::vector<Tensor<1, spacedim>> normal_vectors; |
| 141 |
|
| 142 |
/** |
| 143 |
* Normal vectors of the opposing face. |
| 144 |
*/ |
| 145 |
std::vector<Tensor<1, spacedim>> neighbor_normal_vectors; |
| 146 |
|
| 147 |
/** |
| 148 |
* Two arrays needed for the values of coefficients in the jumps, if |
| 149 |
* they are given. |
| 150 |
*/ |
| 151 |
std::vector<double> coefficient_values1; |
| 152 |
std::vector<dealii::Vector<double>> coefficient_values; |
| 153 |
|
| 154 |
/** |
| 155 |
* Array for the products of Jacobian determinants and weights of |
| 156 |
* quadraturs points. |
| 157 |
*/ |
| 158 |
std::vector<double> JxW_values; |
| 159 |
|
| 160 |
/** |
| 161 |
* The subdomain id we are to care for. |
| 162 |
*/ |
| 163 |
const types::subdomain_id subdomain_id; |
| 164 |
/** |
| 165 |
* The material id we are to care for. |
| 166 |
*/ |
| 167 |
const types::material_id material_id; |
| 168 |
|
| 169 |
/** |
| 170 |
* Some more references to input data to the |
| 171 |
* KellyErrorEstimator::estimate() function. |
| 172 |
*/ |
| 173 |
const std::map<types::boundary_id, const Function<spacedim, number> *> |
| 174 |
*neumann_bc; |
| 175 |
const ComponentMask component_mask; |
| 176 |
const Function<spacedim> *coefficients; |
| 177 |
|
| 178 |
/** |
| 179 |
* Constructor. |
| 180 |
*/ |
| 181 |
template <class FE> |
| 182 |
ParallelData(const FE &fe, |
| 183 |
const dealii::hp::QCollection<dim - 1> &face_quadratures, |
| 184 |
const dealii::hp::MappingCollection<dim, spacedim> &mapping, |
| 185 |
const bool need_quadrature_points, |
| 186 |
const unsigned int n_solution_vectors, |
| 187 |
const types::subdomain_id subdomain_id, |
| 188 |
const types::material_id material_id, |
| 189 |
const std::map<types::boundary_id, |
| 190 |
const Function<spacedim, number> *> *neumann_bc, |
| 191 |
const ComponentMask &component_mask, |
| 192 |
const Function<spacedim> *coefficients); |
| 193 |
|
| 194 |
/** |
| 195 |
* Resize the arrays so that they fit the number of quadrature points |
| 196 |
* associated with the given finite element index into the |
| 197 |
* hp-collections. |
| 198 |
*/ |
| 199 |
void |
| 200 |
resize(const unsigned int active_fe_index); |
| 201 |
}; |
| 202 |
|
| 203 |
|
| 204 |
template <int dim, int spacedim, typename number> |
| 205 |
template <class FE> |
| 206 |
ParallelData<dim, spacedim, number>::ParallelData( |
| 207 |
const FE &fe, |
| 208 |
const dealii::hp::QCollection<dim - 1> &face_quadratures, |
| 209 |
const dealii::hp::MappingCollection<dim, spacedim> &mapping, |
| 210 |
const bool need_quadrature_points, |
| 211 |
const unsigned int n_solution_vectors, |
| 212 |
const types::subdomain_id subdomain_id, |
| 213 |
const types::material_id material_id, |
| 214 |
const std::map<types::boundary_id, const Function<spacedim, number> *> |
| 215 |
*neumann_bc, |
| 216 |
const ComponentMask &component_mask, |
| 217 |
const Function<spacedim> *coefficients) |
| 218 |
: finite_element(fe) |
| 219 |
, face_quadratures(face_quadratures) |
| 220 |
, fe_face_values_cell(mapping, |
| 221 |
finite_element, |
| 222 |
face_quadratures, |
| 223 |
update_gradients | update_JxW_values | |
| 224 |
(need_quadrature_points ? update_quadrature_points : |
| 225 |
UpdateFlags()) | |
| 226 |
update_normal_vectors) |
| 227 |
, fe_face_values_neighbor(mapping, |
| 228 |
finite_element, |
| 229 |
face_quadratures, |
| 230 |
update_gradients | update_normal_vectors) |
| 231 |
, fe_subface_values(mapping, |
| 232 |
finite_element, |
| 233 |
face_quadratures, |
| 234 |
update_gradients | update_normal_vectors) |
| 235 |
, phi(n_solution_vectors, |
| 236 |
std::vector<std::vector<number>>( |
| 237 |
face_quadratures.max_n_quadrature_points(), |
| 238 |
std::vector<number>(fe.n_components()))) |
| 239 |
, psi(n_solution_vectors, |
| 240 |
std::vector<std::vector<Tensor<1, spacedim, number>>>( |
| 241 |
face_quadratures.max_n_quadrature_points(), |
| 242 |
std::vector<Tensor<1, spacedim, number>>(fe.n_components()))) |
| 243 |
, neighbor_psi(n_solution_vectors, |
| 244 |
std::vector<std::vector<Tensor<1, spacedim, number>>>( |
| 245 |
face_quadratures.max_n_quadrature_points(), |
| 246 |
std::vector<Tensor<1, spacedim, number>>( |
| 247 |
fe.n_components()))) |
| 248 |
, normal_vectors(face_quadratures.max_n_quadrature_points()) |
| 249 |
, neighbor_normal_vectors(face_quadratures.max_n_quadrature_points()) |
| 250 |
, coefficient_values1(face_quadratures.max_n_quadrature_points()) |
| 251 |
, coefficient_values(face_quadratures.max_n_quadrature_points(), |
| 252 |
dealii::Vector<double>(fe.n_components())) |
| 253 |
, JxW_values(face_quadratures.max_n_quadrature_points()) |
| 254 |
, subdomain_id(subdomain_id) |
| 255 |
, material_id(material_id) |
| 256 |
, neumann_bc(neumann_bc) |
| 257 |
, component_mask(component_mask) |
| 258 |
, coefficients(coefficients) |
| 259 |
{} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
template <int dim, int spacedim, typename number> |
| 264 |
void |
| 265 |
ParallelData<dim, spacedim, number>::resize( |
| 266 |
const unsigned int active_fe_index) |
| 267 |
{ |
| 268 |
const unsigned int n_q_points = face_quadratures[active_fe_index].size(); |
| 269 |
const unsigned int n_components = finite_element.n_components(); |
| 270 |
|
| 271 |
normal_vectors.resize(n_q_points); |
| 272 |
neighbor_normal_vectors.resize(n_q_points); |
| 273 |
coefficient_values1.resize(n_q_points); |
| 274 |
coefficient_values.resize(n_q_points); |
| 275 |
JxW_values.resize(n_q_points); |
| 276 |
|
| 277 |
for (unsigned int i = 0; i < phi.size(); ++i) |
| 278 |
{ |
| 279 |
phi[i].resize(n_q_points); |
| 280 |
psi[i].resize(n_q_points); |
| 281 |
neighbor_psi[i].resize(n_q_points); |
| 282 |
|
| 283 |
for (unsigned int qp = 0; qp < n_q_points; ++qp) |
| 284 |
{ |
| 285 |
phi[i][qp].resize(n_components); |
| 286 |
psi[i][qp].resize(n_components); |
| 287 |
neighbor_psi[i][qp].resize(n_components); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
for (unsigned int qp = 0; qp < n_q_points; ++qp) |
| 292 |
coefficient_values[qp].reinit(n_components); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
/** |
| 298 |
* Copy data from the local_face_integrals map of a single ParallelData |
| 299 |
* object into a global such map. This is the copier stage of a WorkStream |
| 300 |
* pipeline. |
| 301 |
*/ |
| 302 |
template <int dim, int spacedim> |
| 303 |
void |
| 304 |
copy_local_to_global( |
| 305 |
const std::map<typename DoFHandler<dim, spacedim>::face_iterator, |
| 306 |
std::vector<double>> &local_face_integrals, |
| 307 |
std::map<typename DoFHandler<dim, spacedim>::face_iterator, |
| 308 |
std::vector<double>> &face_integrals) |
| 309 |
{ |
| 310 |
// now copy locally computed elements into the global map |
| 311 |
for (typename std::map<typename DoFHandler<dim, spacedim>::face_iterator, |
| 312 |
std::vector<double>>::const_iterator p = |
| 313 |
local_face_integrals.begin(); |
| 314 |
p != local_face_integrals.end(); |
| 315 |
++p) |
| 316 |
{ |
| 317 |
// double check that the element does not already exists in the |
| 318 |
// global map |
| 319 |
Assert(face_integrals.find(p->first) == face_integrals.end(), |
| 320 |
ExcInternalError()); |
| 321 |
|
| 322 |
for (unsigned int i = 0; i < p->second.size(); ++i) |
| 323 |
{ |
| 324 |
Assert(numbers::is_finite(p->second[i]), ExcInternalError()); |
| 325 |
Assert(p->second[i] >= 0, ExcInternalError()); |
| 326 |
} |
| 327 |
|
| 328 |
face_integrals[p->first] = p->second; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
/** |
| 334 |
* Actually do the computation based on the evaluated gradients in |
| 335 |
* ParallelData. |
| 336 |
*/ |
| 337 |
template <int dim, int spacedim, typename number> |
| 338 |
std::vector<double> |
| 339 |
integrate_over_face( |
| 340 |
ParallelData<dim, spacedim, number> ¶llel_data, |
| 341 |
const typename DoFHandler<dim, spacedim>::face_iterator &face, |
| 342 |
dealii::hp::FEFaceValues<dim, spacedim> &fe_face_values_cell) |
| 343 |
{ |
| 344 |
const unsigned int n_q_points = parallel_data.psi[0].size(), |
| 345 |
n_components = |
| 346 |
parallel_data.finite_element.n_components(), |
| 347 |
n_solution_vectors = parallel_data.psi.size(); |
| 348 |
|
| 349 |
// now psi contains the following: |
| 350 |
// - for an internal face, psi=[grad u] |
| 351 |
// - for a neumann boundary face, psi=grad u |
| 352 |
// each component being the mentioned value at one of the quadrature |
| 353 |
// points |
| 354 |
|
| 355 |
// next we have to multiply this with the normal vector. Since we have |
| 356 |
// taken the difference of gradients for internal faces, we may chose |
| 357 |
// the normal vector of one cell, taking that of the neighbor would only |
| 358 |
// change the sign. We take the outward normal. |
| 359 |
|
| 360 |
parallel_data.normal_vectors = |
| 361 |
fe_face_values_cell.get_present_fe_values().get_normal_vectors(); |
| 362 |
|
| 363 |
for (unsigned int n = 0; n < n_solution_vectors; ++n) |
| 364 |
for (unsigned int component = 0; component < n_components; ++component) |
| 365 |
for (unsigned int point = 0; point < n_q_points; ++point) |
| 366 |
parallel_data.phi[n][point][component] = |
| 367 |
(parallel_data.psi[n][point][component] * |
| 368 |
parallel_data.normal_vectors[point]); |
| 369 |
|
| 370 |
if (face->at_boundary() == false) |
| 371 |
{ |
| 372 |
// compute the jump in the gradients |
| 373 |
|
| 374 |
for (unsigned int n = 0; n < n_solution_vectors; ++n) |
| 375 |
for (unsigned int component = 0; component < n_components; |
| 376 |
++component) |
| 377 |
for (unsigned int p = 0; p < n_q_points; ++p) |
| 378 |
parallel_data.phi[n][p][component] += |
| 379 |
(parallel_data.neighbor_psi[n][p][component] * |
| 380 |
parallel_data.neighbor_normal_vectors[p]); |
| 381 |
} |
| 382 |
|
| 383 |
// if a coefficient was given: use that to scale the jump in the |
| 384 |
// gradient |
| 385 |
if (parallel_data.coefficients != nullptr) |
| 386 |
{ |
| 387 |
// scalar coefficient |
| 388 |
if (parallel_data.coefficients->n_components == 1) |
| 389 |
{ |
| 390 |
parallel_data.coefficients->value_list( |
| 391 |
fe_face_values_cell.get_present_fe_values() |
| 392 |
.get_quadrature_points(), |
| 393 |
parallel_data.coefficient_values1); |
| 394 |
for (unsigned int n = 0; n < n_solution_vectors; ++n) |
| 395 |
for (unsigned int component = 0; component < n_components; |
| 396 |
++component) |
| 397 |
for (unsigned int point = 0; point < n_q_points; ++point) |
| 398 |
parallel_data.phi[n][point][component] *= |
| 399 |
parallel_data.coefficient_values1[point]; |
| 400 |
} |
| 401 |
else |
| 402 |
// vector-valued coefficient |
| 403 |
{ |
| 404 |
parallel_data.coefficients->vector_value_list( |
| 405 |
fe_face_values_cell.get_present_fe_values() |
| 406 |
.get_quadrature_points(), |
| 407 |
parallel_data.coefficient_values); |
| 408 |
for (unsigned int n = 0; n < n_solution_vectors; ++n) |
| 409 |
for (unsigned int component = 0; component < n_components; |
| 410 |
++component) |
| 411 |
for (unsigned int point = 0; point < n_q_points; ++point) |
| 412 |
parallel_data.phi[n][point][component] *= |
| 413 |
parallel_data.coefficient_values[point](component); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
if (face->at_boundary() == true) |
| 419 |
// neumann boundary face. compute difference between normal derivative |
| 420 |
// and boundary function |
| 421 |
{ |
| 422 |
const types::boundary_id boundary_id = face->boundary_id(); |
| 423 |
|
| 424 |
Assert(parallel_data.neumann_bc->find(boundary_id) != |
| 425 |
parallel_data.neumann_bc->end(), |
| 426 |
ExcInternalError()); |
| 427 |
// get the values of the boundary function at the quadrature points |
| 428 |
if (n_components == 1) |
| 429 |
{ |
| 430 |
std::vector<number> g(n_q_points); |
| 431 |
parallel_data.neumann_bc->find(boundary_id) |
| 432 |
->second->value_list(fe_face_values_cell.get_present_fe_values() |
| 433 |
.get_quadrature_points(), |
| 434 |
g); |
| 435 |
|
| 436 |
for (unsigned int n = 0; n < n_solution_vectors; ++n) |
| 437 |
for (unsigned int point = 0; point < n_q_points; ++point) |
| 438 |
parallel_data.phi[n][point][0] -= g[point]; |
| 439 |
} |
| 440 |
else |
| 441 |
{ |
| 442 |
std::vector<dealii::Vector<number>> g( |
| 443 |
n_q_points, dealii::Vector<number>(n_components)); |
| 444 |
parallel_data.neumann_bc->find(boundary_id) |
| 445 |
->second->vector_value_list(fe_face_values_cell |
| 446 |
.get_present_fe_values() |
| 447 |
.get_quadrature_points(), |
| 448 |
g); |
| 449 |
|
| 450 |
for (unsigned int n = 0; n < n_solution_vectors; ++n) |
| 451 |
for (unsigned int component = 0; component < n_components; |
| 452 |
++component) |
| 453 |
for (unsigned int point = 0; point < n_q_points; ++point) |
| 454 |
parallel_data.phi[n][point][component] -= g[point](component); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
// now phi contains the following: |
| 461 |
// - for an internal face, phi=[a du/dn] |
| 462 |
// - for a neumann boundary face, phi=a du/dn-g |
| 463 |
// each component being the mentioned value at one of the quadrature |
| 464 |
// points |
| 465 |
|
| 466 |
parallel_data.JxW_values = |
| 467 |
fe_face_values_cell.get_present_fe_values().get_JxW_values(); |
| 468 |
|
| 469 |
// take the square of the phi[i] for integration, and sum up |
| 470 |
std::vector<double> face_integral(n_solution_vectors, 0); |
| 471 |
for (unsigned int n = 0; n < n_solution_vectors; ++n) |
| 472 |
for (unsigned int component = 0; component < n_components; ++component) |
| 473 |
if (parallel_data.component_mask[component] == true) |
| 474 |
for (unsigned int p = 0; p < n_q_points; ++p) |
| 475 |
face_integral[n] += numbers::NumberTraits<number>::abs_square( |
| 476 |
parallel_data.phi[n][p][component]) * |
| 477 |
parallel_data.JxW_values[p]; |
| 478 |
|
| 479 |
return face_integral; |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
* A factor to scale the integral for the face at the boundary. Used for |
| 484 |
* Neumann BC. |
| 485 |
*/ |
| 486 |
template <int dim, int spacedim> |
| 487 |
double |
| 488 |
boundary_face_factor( |
| 489 |
const typename DoFHandler<dim, spacedim>::active_cell_iterator &cell, |
| 490 |
const unsigned int face_no, |
| 491 |
const dealii::hp::FEFaceValues<dim, spacedim> &fe_face_values_cell, |
| 492 |
const typename KellyErrorEstimator<dim, spacedim>::Strategy strategy) |
| 493 |
{ |
| 494 |
switch (strategy) |
| 495 |
{ |
| 496 |
case KellyErrorEstimator<dim, spacedim>::cell_diameter_over_24: |
| 497 |
{ |
| 498 |
return 1.0; |
| 499 |
} |
| 500 |
case KellyErrorEstimator<dim, spacedim>::cell_diameter: |
| 501 |
{ |
| 502 |
return 1.0; |
| 503 |
} |
| 504 |
case KellyErrorEstimator<dim, |
| 505 |
spacedim>::face_diameter_over_twice_max_degree: |
| 506 |
{ |
| 507 |
const double cell_degree = |
| 508 |
fe_face_values_cell.get_fe_collection()[cell->active_fe_index()] |
| 509 |
.degree; |
| 510 |
return cell->face(face_no)->diameter() / cell_degree; |
| 511 |
} |
| 512 |
default: |
| 513 |
{ |
| 514 |
DEAL_II_NOT_IMPLEMENTED(); |
| 515 |
return std::numeric_limits<double>::lowest(); |
| 516 |
} |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
/** |
| 522 |
* A factor to scale the integral for the regular face. |
| 523 |
*/ |
| 524 |
template <int dim, int spacedim> |
| 525 |
double |
| 526 |
regular_face_factor( |
| 527 |
const typename DoFHandler<dim, spacedim>::active_cell_iterator &cell, |
| 528 |
const unsigned int face_no, |
| 529 |
const dealii::hp::FEFaceValues<dim, spacedim> &fe_face_values_cell, |
| 530 |
const dealii::hp::FEFaceValues<dim, spacedim> &fe_face_values_neighbor, |
| 531 |
const typename KellyErrorEstimator<dim, spacedim>::Strategy strategy) |
| 532 |
{ |
| 533 |
switch (strategy) |
| 534 |
{ |
| 535 |
case KellyErrorEstimator<dim, spacedim>::cell_diameter_over_24: |
| 536 |
{ |
| 537 |
return 1.0; |
| 538 |
} |
| 539 |
case KellyErrorEstimator<dim, spacedim>::cell_diameter: |
| 540 |
{ |
| 541 |
return 1.0; |
| 542 |
} |
| 543 |
case KellyErrorEstimator<dim, |
| 544 |
spacedim>::face_diameter_over_twice_max_degree: |
| 545 |
{ |
| 546 |
const double cell_degree = |
| 547 |
fe_face_values_cell.get_fe_collection()[cell->active_fe_index()] |
| 548 |
.degree; |
| 549 |
const double neighbor_degree = |
| 550 |
fe_face_values_neighbor |
| 551 |
.get_fe_collection()[cell->neighbor(face_no)->active_fe_index()] |
| 552 |
.degree; |
| 553 |
return cell->face(face_no)->diameter() / |
| 554 |
std::max(cell_degree, neighbor_degree) / 2.0; |
| 555 |
} |
| 556 |
default: |
| 557 |
{ |
| 558 |
DEAL_II_NOT_IMPLEMENTED(); |
| 559 |
return std::numeric_limits<double>::lowest(); |
| 560 |
} |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
/** |
| 565 |
* A factor to scale the integral for the irregular face. |
| 566 |
*/ |
| 567 |
template <int dim, int spacedim> |
| 568 |
double |
| 569 |
irregular_face_factor( |
| 570 |
const typename DoFHandler<dim, spacedim>::active_cell_iterator &cell, |
| 571 |
const typename DoFHandler<dim, spacedim>::active_cell_iterator |
| 572 |
&neighbor_child, |
| 573 |
const unsigned int face_no, |
| 574 |
const unsigned int subface_no, |
| 575 |
const dealii::hp::FEFaceValues<dim, spacedim> &fe_face_values, |
| 576 |
dealii::hp::FESubfaceValues<dim, spacedim> &fe_subface_values, |
| 577 |
const typename KellyErrorEstimator<dim, spacedim>::Strategy strategy) |
| 578 |
{ |
| 579 |
switch (strategy) |
| 580 |
{ |
| 581 |
case KellyErrorEstimator<dim, spacedim>::cell_diameter_over_24: |
| 582 |
{ |
| 583 |
return 1.0; |
| 584 |
} |
| 585 |
case KellyErrorEstimator<dim, spacedim>::cell_diameter: |
| 586 |
{ |
| 587 |
return 1.0; |
| 588 |
} |
| 589 |
case KellyErrorEstimator<dim, |
| 590 |
spacedim>::face_diameter_over_twice_max_degree: |
| 591 |
{ |
| 592 |
const double cell_degree = |
| 593 |
fe_face_values.get_fe_collection()[cell->active_fe_index()] |
| 594 |
.degree; |
| 595 |
const double neighbor_child_degree = |
| 596 |
fe_subface_values |
| 597 |
.get_fe_collection()[neighbor_child->active_fe_index()] |
| 598 |
.degree; |
| 599 |
return cell->face(face_no)->child(subface_no)->diameter() / |
| 600 |
std::max(neighbor_child_degree, cell_degree) / 2.0; |
| 601 |
} |
| 602 |
default: |
| 603 |
{ |
| 604 |
DEAL_II_NOT_IMPLEMENTED(); |
| 605 |
return std::numeric_limits<double>::lowest(); |
| 606 |
} |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
/** |
| 611 |
* A factor used when summing up all the contribution from different faces |
| 612 |
* of each cell. |
| 613 |
*/ |
| 614 |
template <int dim, int spacedim> |
| 615 |
double |
| 616 |
cell_factor( |
| 617 |
const typename DoFHandler<dim, spacedim>::active_cell_iterator &cell, |
| 618 |
const unsigned int /*face_no*/, |
| 619 |
const DoFHandler<dim, spacedim> & /*dof_handler*/, |
| 620 |
const typename KellyErrorEstimator<dim, spacedim>::Strategy strategy) |
| 621 |
{ |
| 622 |
switch (strategy) |
| 623 |
{ |
| 624 |
case KellyErrorEstimator<dim, spacedim>::cell_diameter_over_24: |
| 625 |
{ |
| 626 |
return cell->diameter() / 24; |
| 627 |
} |
| 628 |
case KellyErrorEstimator<dim, spacedim>::cell_diameter: |
| 629 |
{ |
| 630 |
return cell->diameter(); |
| 631 |
} |
| 632 |
case KellyErrorEstimator<dim, |
| 633 |
spacedim>::face_diameter_over_twice_max_degree: |
| 634 |
{ |
| 635 |
return 1.0; |
| 636 |
} |
| 637 |
default: |
| 638 |
{ |
| 639 |
DEAL_II_NOT_IMPLEMENTED(); |
| 640 |
return std::numeric_limits<double>::lowest(); |
| 641 |
} |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
/** |
| 648 |
* Actually do the computation on a face which has no hanging nodes (it is |
| 649 |
* regular), i.e. either on the other side there is nirvana (face is at |
| 650 |
* boundary), or the other side's refinement level is the same as that of |
| 651 |
* this side, then handle the integration of these both cases together. |
| 652 |
*/ |
| 653 |
template <typename Number, int dim, int spacedim> |
| 654 |
void |
| 655 |
integrate_over_regular_face( |
| 656 |
const ArrayView<const ReadVector<Number> *> &solutions, |
| 657 |
ParallelData<dim, spacedim, Number> ¶llel_data, |
| 658 |
std::map<typename DoFHandler<dim, spacedim>::face_iterator, |
| 659 |
std::vector<double>> &local_face_integrals, |
| 660 |
const typename DoFHandler<dim, spacedim>::active_cell_iterator &cell, |
| 661 |
const unsigned int face_no, |
| 662 |
dealii::hp::FEFaceValues<dim, spacedim> &fe_face_values_cell, |
| 663 |
dealii::hp::FEFaceValues<dim, spacedim> &fe_face_values_neighbor, |
| 664 |
const typename KellyErrorEstimator<dim, spacedim>::Strategy strategy) |
| 665 |
{ |
| 666 |
const typename DoFHandler<dim, spacedim>::face_iterator face = |
| 667 |
cell->face(face_no); |
| 668 |
const unsigned int n_solution_vectors = solutions.size(); |
| 669 |
|
| 670 |
|
| 671 |
// initialize data of the restriction |
| 672 |
// of this cell to the present face |
| 673 |
fe_face_values_cell.reinit(cell, face_no, cell->active_fe_index()); |
| 674 |
|
| 675 |
// get gradients of the finite element |
| 676 |
// function on this cell |
| 677 |
for (unsigned int n = 0; n < n_solution_vectors; ++n) |
| 678 |
fe_face_values_cell.get_present_fe_values().get_function_gradients( |
| 679 |
*solutions[n], parallel_data.psi[n]); |
| 680 |
|
| 681 |
double factor; |
| 682 |
// now compute over the other side of the face |
| 683 |
if (face->at_boundary() == false) |
| 684 |
// internal face; integrate jump of gradient across this face |
| 685 |
{ |
| 686 |
Assert(cell->neighbor(face_no).state() == IteratorState::valid, |
| 687 |
ExcInternalError()); |
| 688 |
|
| 689 |
const typename DoFHandler<dim, spacedim>::active_cell_iterator |
| 690 |
neighbor = cell->neighbor(face_no); |
| 691 |
|
| 692 |
// find which number the current face has relative to the |
| 693 |
// neighboring cell |
| 694 |
const unsigned int neighbor_neighbor = |
| 695 |
cell->neighbor_of_neighbor(face_no); |
| 696 |
Assert(neighbor_neighbor < GeometryInfo<dim>::faces_per_cell, |
| 697 |
ExcInternalError()); |
| 698 |
|
| 699 |
// get restriction of finite element function of @p{neighbor} to the |
| 700 |
// common face. in the hp-case, use the quadrature formula that |
| 701 |
// matches the one we would use for the present cell |
| 702 |
fe_face_values_neighbor.reinit(neighbor, |
| 703 |
neighbor_neighbor, |
| 704 |
cell->active_fe_index()); |
| 705 |
|
| 706 |
factor = regular_face_factor<dim, spacedim>(cell, |
| 707 |
face_no, |
| 708 |
fe_face_values_cell, |
| 709 |
fe_face_values_neighbor, |
| 710 |
strategy); |
| 711 |
|
| 712 |
// get gradients on neighbor cell |
| 713 |
for (unsigned int n = 0; n < n_solution_vectors; ++n) |
| 714 |
{ |
| 715 |
fe_face_values_neighbor.get_present_fe_values() |
| 716 |
.get_function_gradients(*solutions[n], |
| 717 |
parallel_data.neighbor_psi[n]); |
| 718 |
} |
| 719 |
|
| 720 |
parallel_data.neighbor_normal_vectors = |
| 721 |
fe_face_values_neighbor.get_present_fe_values().get_normal_vectors(); |
| 722 |
} |
| 723 |
else |
| 724 |
{ |
| 725 |
factor = boundary_face_factor<dim, spacedim>(cell, |
| 726 |
face_no, |
| 727 |
fe_face_values_cell, |
| 728 |
strategy); |
| 729 |
} |
| 730 |
|
| 731 |
// now go to the generic function that does all the other things |
| 732 |
local_face_integrals[face] = |
| 733 |
integrate_over_face(parallel_data, face, fe_face_values_cell); |
| 734 |
|
| 735 |
for (unsigned int i = 0; i < local_face_integrals[face].size(); ++i) |
| 736 |
local_face_integrals[face][i] *= factor; |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
/** |
| 742 |
* The same applies as for the function above, except that integration is |
| 743 |
* over face @p face_no of @p cell, where the respective neighbor is |
| 744 |
* refined, so that the integration is a bit more complex. |
| 745 |
*/ |
| 746 |
template <typename Number, int dim, int spacedim> |
| 747 |
void |
| 748 |
integrate_over_irregular_face( |
| 749 |
const ArrayView<const ReadVector<Number> *> &solutions, |
| 750 |
ParallelData<dim, spacedim, Number> ¶llel_data, |
| 751 |
std::map<typename DoFHandler<dim, spacedim>::face_iterator, |
| 752 |
std::vector<double>> &local_face_integrals, |
| 753 |
const typename DoFHandler<dim, spacedim>::active_cell_iterator &cell, |
| 754 |
const unsigned int face_no, |
| 755 |
dealii::hp::FEFaceValues<dim, spacedim> &fe_face_values, |
| 756 |
dealii::hp::FESubfaceValues<dim, spacedim> &fe_subface_values, |
| 757 |
const typename KellyErrorEstimator<dim, spacedim>::Strategy strategy) |
| 758 |
{ |
| 759 |
const auto neighbor = cell->neighbor(face_no); |
| 760 |
const unsigned int n_solution_vectors = solutions.size(); |
| 761 |
const auto face = cell->face(face_no); |
| 762 |
|
| 763 |
Assert(neighbor.state() == IteratorState::valid, ExcInternalError()); |
| 764 |
Assert(face->has_children(), ExcInternalError()); |
| 765 |
|
| 766 |
// set up a vector of the gradients of the finite element function on |
| 767 |
// this cell at the quadrature points |
| 768 |
// |
| 769 |
// let psi be a short name for [a grad u_h], where the second index be |
| 770 |
// the component of the finite element, and the first index the number |
| 771 |
// of the quadrature point |
| 772 |
|
| 773 |
// store which number @p{cell} has in the list of neighbors of |
| 774 |
// @p{neighbor} |
| 775 |
const unsigned int neighbor_neighbor = cell->neighbor_of_neighbor(face_no); |
| 776 |
Assert(neighbor_neighbor < GeometryInfo<dim>::faces_per_cell, |
| 777 |
ExcInternalError()); |
| 778 |
|
| 779 |
// loop over all subfaces |
| 780 |
for (unsigned int subface_no = 0; subface_no < face->n_children(); |
| 781 |
++subface_no) |
| 782 |
{ |
| 783 |
// get an iterator pointing to the cell behind the present subface |
| 784 |
const typename DoFHandler<dim, spacedim>::active_cell_iterator |
| 785 |
neighbor_child = cell->neighbor_child_on_subface(face_no, subface_no); |
| 786 |
Assert(neighbor_child->is_active(), ExcInternalError()); |
| 787 |
|
| 788 |
// restrict the finite element on the present cell to the subface |
| 789 |
fe_subface_values.reinit(cell, |
| 790 |
face_no, |
| 791 |
subface_no, |
| 792 |
cell->active_fe_index()); |
| 793 |
|
| 794 |
// restrict the finite element on the neighbor cell to the common |
| 795 |
// @p{subface}. |
| 796 |
fe_face_values.reinit(neighbor_child, |
| 797 |
neighbor_neighbor, |
| 798 |
cell->active_fe_index()); |
| 799 |
|
| 800 |
const double factor = |
| 801 |
irregular_face_factor<dim, spacedim>(cell, |
| 802 |
neighbor_child, |
| 803 |
face_no, |
| 804 |
subface_no, |
| 805 |
fe_face_values, |
| 806 |
fe_subface_values, |
| 807 |
strategy); |
| 808 |
|
| 809 |
// store the gradient of the solution in psi |
| 810 |
for (unsigned int n = 0; n < n_solution_vectors; ++n) |
| 811 |
fe_subface_values.get_present_fe_values().get_function_gradients( |
| 812 |
*solutions[n], parallel_data.psi[n]); |
| 813 |
|
| 814 |
// store the gradient from the neighbor's side in @p{neighbor_psi} |
| 815 |
for (unsigned int n = 0; n < n_solution_vectors; ++n) |
| 816 |
fe_face_values.get_present_fe_values().get_function_gradients( |
| 817 |
*solutions[n], parallel_data.neighbor_psi[n]); |
| 818 |
|
| 819 |
// call generic evaluate function |
| 820 |
parallel_data.neighbor_normal_vectors = |
| 821 |
fe_subface_values.get_present_fe_values().get_normal_vectors(); |
| 822 |
|
| 823 |
local_face_integrals[neighbor_child->face(neighbor_neighbor)] = |
| 824 |
integrate_over_face(parallel_data, face, fe_face_values); |
| 825 |
for (unsigned int i = 0; |
| 826 |
i < local_face_integrals[neighbor_child->face(neighbor_neighbor)] |
| 827 |
.size(); |
| 828 |
i++) |
| 829 |
local_face_integrals[neighbor_child->face(neighbor_neighbor)][i] *= |
| 830 |
factor; |
| 831 |
} |
| 832 |
|
| 833 |
// finally loop over all subfaces to collect the contributions of the |
| 834 |
// subfaces and store them with the parent face |
| 835 |
std::vector<double> sum(n_solution_vectors, 0); |
| 836 |
for (unsigned int subface_no = 0; subface_no < face->n_children(); |
| 837 |
++subface_no) |
| 838 |
{ |
| 839 |
Assert(local_face_integrals.find(face->child(subface_no)) != |
| 840 |
local_face_integrals.end(), |
| 841 |
ExcInternalError()); |
| 842 |
Assert(local_face_integrals[face->child(subface_no)][0] >= 0, |
| 843 |
ExcInternalError()); |
| 844 |
|
| 845 |
for (unsigned int n = 0; n < n_solution_vectors; ++n) |
| 846 |
sum[n] += local_face_integrals[face->child(subface_no)][n]; |
| 847 |
} |
| 848 |
|
| 849 |
local_face_integrals[face] = sum; |
| 850 |
} |
| 851 |
|
| 852 |
|
| 853 |
/** |
| 854 |
* Computate the error on the faces of a single cell. |
| 855 |
* |
| 856 |
* This function is only needed in two or three dimensions. The error |
| 857 |
* estimator in one dimension is implemented separately. |
| 858 |
*/ |
| 859 |
template <typename Number, int dim, int spacedim> |
| 860 |
void |
| 861 |
estimate_one_cell( |
| 862 |
const typename DoFHandler<dim, spacedim>::active_cell_iterator &cell, |
| 863 |
ParallelData<dim, spacedim, Number> ¶llel_data, |
| 864 |
std::map<typename DoFHandler<dim, spacedim>::face_iterator, |
| 865 |
std::vector<double>> &local_face_integrals, |
| 866 |
const ArrayView<const ReadVector<Number> *> &solutions, |
| 867 |
const typename KellyErrorEstimator<dim, spacedim>::Strategy strategy) |
| 868 |
{ |
| 869 |
const unsigned int n_solution_vectors = solutions.size(); |
| 870 |
|
| 871 |
const types::subdomain_id subdomain_id = parallel_data.subdomain_id; |
| 872 |
const unsigned int material_id = parallel_data.material_id; |
| 873 |
|
| 874 |
// empty our own copy of the local face integrals |
| 875 |
local_face_integrals.clear(); |
| 876 |
|
| 877 |
// loop over all faces of this cell |
| 878 |
for (const unsigned int face_no : cell->face_indices()) |
| 879 |
{ |
| 880 |
const typename DoFHandler<dim, spacedim>::face_iterator face = |
| 881 |
cell->face(face_no); |
| 882 |
|
| 883 |
// make sure we do work only once: this face may either be regular |
| 884 |
// or irregular. if it is regular and has a neighbor, then we visit |
| 885 |
// the face twice, once from every side. let the one with the lower |
| 886 |
// index do the work. if it is at the boundary, or if the face is |
| 887 |
// irregular, then do the work below |
| 888 |
if ((face->has_children() == false) && !cell->at_boundary(face_no) && |
| 889 |
(!cell->neighbor_is_coarser(face_no) && |
| 890 |
(cell->neighbor(face_no)->index() < cell->index() || |
| 891 |
(cell->neighbor(face_no)->index() == cell->index() && |
| 892 |
cell->neighbor(face_no)->level() < cell->level())))) |
| 893 |
continue; |
| 894 |
|
| 895 |
// if the neighboring cell is less refined than the present one, |
| 896 |
// then do nothing since we integrate over the subfaces when we |
| 897 |
// visit the coarse cells. |
| 898 |
if (face->at_boundary() == false) |
| 899 |
if (cell->neighbor_is_coarser(face_no)) |
| 900 |
continue; |
| 901 |
|
| 902 |
// if this face is part of the boundary but not of the neumann |
| 903 |
// boundary -> nothing to do. However, to make things easier when |
| 904 |
// summing up the contributions of the faces of cells, we enter this |
| 905 |
// face into the list of faces with contribution zero. |
| 906 |
if (face->at_boundary() && |
| 907 |
(parallel_data.neumann_bc->find(face->boundary_id()) == |
| 908 |
parallel_data.neumann_bc->end())) |
| 909 |
{ |
| 910 |
local_face_integrals[face] = |
| 911 |
std::vector<double>(n_solution_vectors, 0.); |
| 912 |
continue; |
| 913 |
} |
| 914 |
|
| 915 |
// finally: note that we only have to do something if either the |
| 916 |
// present cell is on the subdomain we care for (and the same for |
| 917 |
// material_id), or if one of the neighbors behind the face is on |
| 918 |
// the subdomain we care for |
| 919 |
if (!(((subdomain_id == numbers::invalid_subdomain_id) || |
| 920 |
(cell->subdomain_id() == subdomain_id)) && |
| 921 |
((material_id == numbers::invalid_material_id) || |
| 922 |
(cell->material_id() == material_id)))) |
| 923 |
{ |
| 924 |
// ok, cell is unwanted, but maybe its neighbor behind the face |
| 925 |
// we presently work on? oh is there a face at all? |
| 926 |
if (face->at_boundary()) |
| 927 |
continue; |
| 928 |
|
| 929 |
bool care_for_cell = false; |
| 930 |
if (face->has_children() == false) |
| 931 |
care_for_cell |= |
| 932 |
((cell->neighbor(face_no)->subdomain_id() == subdomain_id) || |
| 933 |
(subdomain_id == numbers::invalid_subdomain_id)) && |
| 934 |
((cell->neighbor(face_no)->material_id() == material_id) || |
| 935 |
(material_id == numbers::invalid_material_id)); |
| 936 |
else |
| 937 |
{ |
| 938 |
for (unsigned int sf = 0; sf < face->n_children(); ++sf) |
| 939 |
if (((cell->neighbor_child_on_subface(face_no, sf) |
| 940 |
->subdomain_id() == subdomain_id) && |
| 941 |
(material_id == numbers::invalid_material_id)) || |
| 942 |
((cell->neighbor_child_on_subface(face_no, sf) |
| 943 |
->material_id() == material_id) && |
| 944 |
(subdomain_id == numbers::invalid_subdomain_id))) |
| 945 |
{ |
| 946 |
care_for_cell = true; |
| 947 |
break; |
| 948 |
} |
| 949 |
} |
| 950 |
|
| 951 |
// so if none of the neighbors cares for this subdomain or |
| 952 |
// material either, then try next face |
| 953 |
if (care_for_cell == false) |
| 954 |
continue; |
| 955 |
} |
| 956 |
|
| 957 |
// so now we know that we care for this face, let's do something |
| 958 |
// about it. first re-size the arrays we may use to the correct |
| 959 |
// size: |
| 960 |
parallel_data.resize(cell->active_fe_index()); |
| 961 |
|
| 962 |
|
| 963 |
// then do the actual integration |
| 964 |
if (face->has_children() == false) |
| 965 |
// if the face is a regular one, i.e. either on the other side |
| 966 |
// there is nirvana (face is at boundary), or the other side's |
| 967 |
// refinement level is the same as that of this side, then handle |
| 968 |
// the integration of these both cases together |
| 969 |
integrate_over_regular_face(solutions, |
| 970 |
parallel_data, |
| 971 |
local_face_integrals, |
| 972 |
cell, |
| 973 |
face_no, |
| 974 |
parallel_data.fe_face_values_cell, |
| 975 |
parallel_data.fe_face_values_neighbor, |
| 976 |
strategy); |
| 977 |
|
| 978 |
else |
| 979 |
// otherwise we need to do some special computations which do not |
| 980 |
// fit into the framework of the above function |
| 981 |
integrate_over_irregular_face(solutions, |
| 982 |
parallel_data, |
| 983 |
local_face_integrals, |
| 984 |
cell, |
| 985 |
face_no, |
| 986 |
parallel_data.fe_face_values_cell, |
| 987 |
parallel_data.fe_subface_values, |
| 988 |
strategy); |
| 989 |
} |
| 990 |
} |
| 991 |
} // namespace internal |
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
// the following function is still independent of dimension, but it |
| 996 |
// calls dimension dependent functions |
| 997 |
template <int dim, int spacedim> |
| 998 |
template <typename Number> |
| 999 |
void |
| 1000 |
KellyErrorEstimator<dim, spacedim>::estimate( |
| 1001 |
const Mapping<dim, spacedim> &mapping, |
| 1002 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 1003 |
const Quadrature<dim - 1> &quadrature, |
| 1004 |
const std::map<types::boundary_id, const Function<spacedim, Number> *> |
| 1005 |
&neumann_bc, |
| 1006 |
const ReadVector<Number> &solution, |
| 1007 |
Vector<float> &error, |
| 1008 |
const ComponentMask &component_mask, |
| 1009 |
const Function<spacedim> *coefficients, |
| 1010 |
const unsigned int n_threads, |
| 1011 |
const types::subdomain_id subdomain_id, |
| 1012 |
const types::material_id material_id, |
| 1013 |
const Strategy strategy) |
| 1014 |
{ |
| 1015 |
// just pass on to the other function |
| 1016 |
std::vector<const ReadVector<Number> *> solutions(1, &solution); |
| 1017 |
std::vector<Vector<float> *> errors(1, &error); |
| 1018 |
ArrayView<Vector<float> *> error_view = make_array_view(errors); |
| 1019 |
estimate(mapping, |
| 1020 |
dof_handler, |
| 1021 |
quadrature, |
| 1022 |
neumann_bc, |
| 1023 |
make_array_view(solutions), |
| 1024 |
error_view, |
| 1025 |
component_mask, |
| 1026 |
coefficients, |
| 1027 |
n_threads, |
| 1028 |
subdomain_id, |
| 1029 |
material_id, |
| 1030 |
strategy); |
| 1031 |
} |
| 1032 |
|
| 1033 |
|
| 1034 |
template <int dim, int spacedim> |
| 1035 |
template <typename Number> |
| 1036 |
void |
| 1037 |
KellyErrorEstimator<dim, spacedim>::estimate( |
| 1038 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 1039 |
const Quadrature<dim - 1> &quadrature, |
| 1040 |
const std::map<types::boundary_id, const Function<spacedim, Number> *> |
| 1041 |
&neumann_bc, |
| 1042 |
const ReadVector<Number> &solution, |
| 1043 |
Vector<float> &error, |
| 1044 |
const ComponentMask &component_mask, |
| 1045 |
const Function<spacedim> *coefficients, |
| 1046 |
const unsigned int n_threads, |
| 1047 |
const types::subdomain_id subdomain_id, |
| 1048 |
const types::material_id material_id, |
| 1049 |
const Strategy strategy) |
| 1050 |
{ |
| 1051 |
estimate(get_default_linear_mapping(dof_handler.get_triangulation()), |
| 1052 |
dof_handler, |
| 1053 |
quadrature, |
| 1054 |
neumann_bc, |
| 1055 |
solution, |
| 1056 |
error, |
| 1057 |
component_mask, |
| 1058 |
coefficients, |
| 1059 |
n_threads, |
| 1060 |
subdomain_id, |
| 1061 |
material_id, |
| 1062 |
strategy); |
| 1063 |
} |
| 1064 |
|
| 1065 |
|
| 1066 |
template <int dim, int spacedim> |
| 1067 |
template <typename Number> |
| 1068 |
void |
| 1069 |
KellyErrorEstimator<dim, spacedim>::estimate( |
| 1070 |
const hp::MappingCollection<dim, spacedim> &mapping, |
| 1071 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 1072 |
const hp::QCollection<dim - 1> &quadrature, |
| 1073 |
const std::map<types::boundary_id, const Function<spacedim, Number> *> |
| 1074 |
&neumann_bc, |
| 1075 |
const ReadVector<Number> &solution, |
| 1076 |
Vector<float> &error, |
| 1077 |
const ComponentMask &component_mask, |
| 1078 |
const Function<spacedim> *coefficients, |
| 1079 |
const unsigned int n_threads, |
| 1080 |
const types::subdomain_id subdomain_id, |
| 1081 |
const types::material_id material_id, |
| 1082 |
const Strategy strategy) |
| 1083 |
{ |
| 1084 |
// just pass on to the other function |
| 1085 |
std::vector<const ReadVector<Number> *> solutions(1, &solution); |
| 1086 |
std::vector<Vector<float> *> errors(1, &error); |
| 1087 |
ArrayView<Vector<float> *> error_view = make_array_view(errors); |
| 1088 |
estimate(mapping, |
| 1089 |
dof_handler, |
| 1090 |
quadrature, |
| 1091 |
neumann_bc, |
| 1092 |
make_array_view(solutions), |
| 1093 |
error_view, |
| 1094 |
component_mask, |
| 1095 |
coefficients, |
| 1096 |
n_threads, |
| 1097 |
subdomain_id, |
| 1098 |
material_id, |
| 1099 |
strategy); |
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
template <int dim, int spacedim> |
| 1104 |
template <typename Number> |
| 1105 |
void |
| 1106 |
KellyErrorEstimator<dim, spacedim>::estimate( |
| 1107 |
const Mapping<dim, spacedim> &mapping, |
| 1108 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 1109 |
const hp::QCollection<dim - 1> &quadrature, |
| 1110 |
const std::map<types::boundary_id, const Function<spacedim, Number> *> |
| 1111 |
&neumann_bc, |
| 1112 |
const ReadVector<Number> &solution, |
| 1113 |
Vector<float> &error, |
| 1114 |
const ComponentMask &component_mask, |
| 1115 |
const Function<spacedim> *coefficients, |
| 1116 |
const unsigned int n_threads, |
| 1117 |
const types::subdomain_id subdomain_id, |
| 1118 |
const types::material_id material_id, |
| 1119 |
const Strategy strategy) |
| 1120 |
{ |
| 1121 |
// DEPRECATED |
| 1122 |
// just pass on to the other function |
| 1123 |
std::vector<const ReadVector<Number> *> solutions(1, &solution); |
| 1124 |
std::vector<Vector<float> *> errors(1, &error); |
| 1125 |
ArrayView<Vector<float> *> error_view = make_array_view(errors); |
| 1126 |
const hp::MappingCollection<dim, spacedim> mapping_collection(mapping); |
| 1127 |
estimate(mapping_collection, |
| 1128 |
dof_handler, |
| 1129 |
quadrature, |
| 1130 |
neumann_bc, |
| 1131 |
make_array_view(solutions), |
| 1132 |
error_view, |
| 1133 |
component_mask, |
| 1134 |
coefficients, |
| 1135 |
n_threads, |
| 1136 |
subdomain_id, |
| 1137 |
material_id, |
| 1138 |
strategy); |
| 1139 |
} |
| 1140 |
|
| 1141 |
|
| 1142 |
template <int dim, int spacedim> |
| 1143 |
template <typename Number> |
| 1144 |
void |
| 1145 |
KellyErrorEstimator<dim, spacedim>::estimate( |
| 1146 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 1147 |
const hp::QCollection<dim - 1> &quadrature, |
| 1148 |
const std::map<types::boundary_id, const Function<spacedim, Number> *> |
| 1149 |
&neumann_bc, |
| 1150 |
const ReadVector<Number> &solution, |
| 1151 |
Vector<float> &error, |
| 1152 |
const ComponentMask &component_mask, |
| 1153 |
const Function<spacedim> *coefficients, |
| 1154 |
const unsigned int n_threads, |
| 1155 |
const types::subdomain_id subdomain_id, |
| 1156 |
const types::material_id material_id, |
| 1157 |
const Strategy strategy) |
| 1158 |
{ |
| 1159 |
const hp::MappingCollection<dim, spacedim> mapping( |
| 1160 |
get_default_linear_mapping(dof_handler.get_triangulation())); |
| 1161 |
estimate(mapping, |
| 1162 |
dof_handler, |
| 1163 |
quadrature, |
| 1164 |
neumann_bc, |
| 1165 |
solution, |
| 1166 |
error, |
| 1167 |
component_mask, |
| 1168 |
coefficients, |
| 1169 |
n_threads, |
| 1170 |
subdomain_id, |
| 1171 |
material_id, |
| 1172 |
strategy); |
| 1173 |
} |
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
template <int dim, int spacedim> |
| 1178 |
template <typename Number> |
| 1179 |
void |
| 1180 |
KellyErrorEstimator<dim, spacedim>::estimate( |
| 1181 |
const hp::MappingCollection<dim, spacedim> &mapping, |
| 1182 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 1183 |
const hp::QCollection<dim - 1> &face_quadratures, |
| 1184 |
const std::map<types::boundary_id, const Function<spacedim, Number> *> |
| 1185 |
&neumann_bc, |
| 1186 |
const ArrayView<const ReadVector<Number> *> &solutions, |
| 1187 |
ArrayView<Vector<float> *> &errors, |
| 1188 |
const ComponentMask &component_mask, |
| 1189 |
const Function<spacedim> *coefficients, |
| 1190 |
const unsigned int, |
| 1191 |
const types::subdomain_id subdomain_id_, |
| 1192 |
const types::material_id material_id, |
| 1193 |
const Strategy strategy) |
| 1194 |
{ |
| 1195 |
types::subdomain_id subdomain_id = numbers::invalid_subdomain_id; |
| 1196 |
if (const auto *triangulation = dynamic_cast< |
| 1197 |
const parallel::DistributedTriangulationBase<dim, spacedim> *>( |
| 1198 |
&dof_handler.get_triangulation())) |
| 1199 |
{ |
| 1200 |
Assert((subdomain_id_ == numbers::invalid_subdomain_id) || |
| 1201 |
(subdomain_id_ == triangulation->locally_owned_subdomain()), |
| 1202 |
ExcMessage( |
| 1203 |
"For distributed Triangulation objects and associated " |
| 1204 |
"DoFHandler objects, asking for any subdomain other than the " |
| 1205 |
"locally owned one does not make sense.")); |
| 1206 |
subdomain_id = triangulation->locally_owned_subdomain(); |
| 1207 |
} |
| 1208 |
else |
| 1209 |
{ |
| 1210 |
subdomain_id = subdomain_id_; |
| 1211 |
} |
| 1212 |
|
| 1213 |
const unsigned int n_components = dof_handler.get_fe(0).n_components(); |
| 1214 |
|
| 1215 |
// sanity checks |
| 1216 |
Assert(solutions.size() > 0, ExcNoSolutions()); |
| 1217 |
Assert(solutions.size() == errors.size(), |
| 1218 |
ExcIncompatibleNumberOfElements(solutions.size(), errors.size())); |
| 1219 |
|
| 1220 |
for (const auto &boundary_function : neumann_bc) |
| 1221 |
{ |
| 1222 |
(void)boundary_function; |
| 1223 |
Assert(boundary_function.second->n_components == n_components, |
| 1224 |
ExcInvalidBoundaryFunction(boundary_function.first, |
| 1225 |
boundary_function.second->n_components, |
| 1226 |
n_components)); |
| 1227 |
} |
| 1228 |
|
| 1229 |
Assert(component_mask.represents_n_components(n_components), |
| 1230 |
ExcInvalidComponentMask()); |
| 1231 |
Assert(component_mask.n_selected_components(n_components) > 0, |
| 1232 |
ExcInvalidComponentMask()); |
| 1233 |
|
| 1234 |
Assert((coefficients == nullptr) || |
| 1235 |
(coefficients->n_components == n_components) || |
| 1236 |
(coefficients->n_components == 1), |
| 1237 |
ExcInvalidCoefficient()); |
| 1238 |
|
| 1239 |
for (unsigned int n = 0; n < solutions.size(); ++n) |
| 1240 |
Assert(solutions[n]->size() == dof_handler.n_dofs(), |
| 1241 |
ExcDimensionMismatch(solutions[n]->size(), dof_handler.n_dofs())); |
| 1242 |
|
| 1243 |
const unsigned int n_solution_vectors = solutions.size(); |
| 1244 |
|
| 1245 |
// Map of integrals indexed by the corresponding face. In this map we store |
| 1246 |
// the integrated jump of the gradient for each face. At the end of the |
| 1247 |
// function, we again loop over the cells and collect the contributions of |
| 1248 |
// the different faces of the cell. |
| 1249 |
std::map<typename DoFHandler<dim, spacedim>::face_iterator, |
| 1250 |
std::vector<double>> |
| 1251 |
face_integrals; |
| 1252 |
|
| 1253 |
// all the data needed in the error estimator by each of the threads is |
| 1254 |
// gathered in the following structures |
| 1255 |
const internal::ParallelData<dim, spacedim, Number> parallel_data( |
| 1256 |
dof_handler.get_fe_collection(), |
| 1257 |
face_quadratures, |
| 1258 |
mapping, |
| 1259 |
(!neumann_bc.empty() || (coefficients != nullptr)), |
| 1260 |
solutions.size(), |
| 1261 |
subdomain_id, |
| 1262 |
material_id, |
| 1263 |
&neumann_bc, |
| 1264 |
component_mask, |
| 1265 |
coefficients); |
| 1266 |
std::map<typename DoFHandler<dim, spacedim>::face_iterator, |
| 1267 |
std::vector<double>> |
| 1268 |
sample_local_face_integrals; |
| 1269 |
|
| 1270 |
// now let's work on all those cells: |
| 1271 |
WorkStream::run( |
| 1272 |
dof_handler.begin_active(), |
| 1273 |
static_cast<typename DoFHandler<dim, spacedim>::active_cell_iterator>( |
| 1274 |
dof_handler.end()), |
| 1275 |
[&solutions, strategy]( |
| 1276 |
const typename DoFHandler<dim, spacedim>::active_cell_iterator &cell, |
| 1277 |
internal::ParallelData<dim, spacedim, Number> ¶llel_data, |
| 1278 |
std::map<typename DoFHandler<dim, spacedim>::face_iterator, |
| 1279 |
std::vector<double>> &local_face_integrals) { |
| 1280 |
internal::estimate_one_cell( |
| 1281 |
cell, parallel_data, local_face_integrals, solutions, strategy); |
| 1282 |
}, |
| 1283 |
[&face_integrals]( |
| 1284 |
const std::map<typename DoFHandler<dim, spacedim>::face_iterator, |
| 1285 |
std::vector<double>> &local_face_integrals) { |
| 1286 |
internal::copy_local_to_global<dim, spacedim>(local_face_integrals, |
| 1287 |
face_integrals); |
| 1288 |
}, |
| 1289 |
parallel_data, |
| 1290 |
sample_local_face_integrals); |
| 1291 |
|
| 1292 |
// finally add up the contributions of the faces for each cell |
| 1293 |
|
| 1294 |
// reserve one slot for each cell and set it to zero |
| 1295 |
for (unsigned int n = 0; n < n_solution_vectors; ++n) |
| 1296 |
{ |
| 1297 |
(*errors[n]).reinit(dof_handler.get_triangulation().n_active_cells()); |
| 1298 |
for (unsigned int i = 0; |
| 1299 |
i < dof_handler.get_triangulation().n_active_cells(); |
| 1300 |
++i) |
| 1301 |
(*errors[n])(i) = 0; |
| 1302 |
} |
| 1303 |
|
| 1304 |
// now walk over all cells and collect information from the faces. only do |
| 1305 |
// something if this is a cell we care for based on the subdomain id |
| 1306 |
for (const auto &cell : dof_handler.active_cell_iterators()) |
| 1307 |
if (((subdomain_id == numbers::invalid_subdomain_id) || |
| 1308 |
(cell->subdomain_id() == subdomain_id)) && |
| 1309 |
((material_id == numbers::invalid_material_id) || |
| 1310 |
(cell->material_id() == material_id))) |
| 1311 |
{ |
| 1312 |
const unsigned int present_cell = cell->active_cell_index(); |
| 1313 |
|
| 1314 |
// loop over all faces of this cell |
| 1315 |
for (const unsigned int face_no : cell->face_indices()) |
| 1316 |
{ |
| 1317 |
Assert(face_integrals.find(cell->face(face_no)) != |
| 1318 |
face_integrals.end(), |
| 1319 |
ExcInternalError()); |
| 1320 |
const double factor = internal::cell_factor<dim, spacedim>( |
| 1321 |
cell, face_no, dof_handler, strategy); |
| 1322 |
|
| 1323 |
for (unsigned int n = 0; n < n_solution_vectors; ++n) |
| 1324 |
{ |
| 1325 |
// make sure that we have written a meaningful value into this |
| 1326 |
// slot |
| 1327 |
Assert(face_integrals[cell->face(face_no)][n] >= 0, |
| 1328 |
ExcInternalError()); |
| 1329 |
|
| 1330 |
(*errors[n])(present_cell) += |
| 1331 |
(face_integrals[cell->face(face_no)][n] * factor); |
| 1332 |
} |
| 1333 |
} |
| 1334 |
|
| 1335 |
for (unsigned int n = 0; n < n_solution_vectors; ++n) |
| 1336 |
(*errors[n])(present_cell) = std::sqrt((*errors[n])(present_cell)); |
| 1337 |
} |
| 1338 |
} |
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
template <int dim, int spacedim> |
| 1343 |
template <typename Number> |
| 1344 |
void |
| 1345 |
KellyErrorEstimator<dim, spacedim>::estimate( |
| 1346 |
const Mapping<dim, spacedim> &mapping, |
| 1347 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 1348 |
const hp::QCollection<dim - 1> &quadrature, |
| 1349 |
const std::map<types::boundary_id, const Function<spacedim, Number> *> |
| 1350 |
&neumann_bc, |
| 1351 |
const ArrayView<const ReadVector<Number> *> &solutions, |
| 1352 |
ArrayView<Vector<float> *> &errors, |
| 1353 |
const ComponentMask &component_mask, |
| 1354 |
const Function<spacedim> *coefficients, |
| 1355 |
const unsigned int n_threads, |
| 1356 |
const types::subdomain_id subdomain_id, |
| 1357 |
const types::material_id material_id, |
| 1358 |
const Strategy strategy) |
| 1359 |
{ |
| 1360 |
// DEPRECATED |
| 1361 |
// just pass on to the other function |
| 1362 |
const hp::MappingCollection<dim, spacedim> mapping_collection(mapping); |
| 1363 |
estimate(mapping_collection, |
| 1364 |
dof_handler, |
| 1365 |
quadrature, |
| 1366 |
neumann_bc, |
| 1367 |
solutions, |
| 1368 |
errors, |
| 1369 |
component_mask, |
| 1370 |
coefficients, |
| 1371 |
n_threads, |
| 1372 |
subdomain_id, |
| 1373 |
material_id, |
| 1374 |
strategy); |
| 1375 |
} |
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
template <int dim, int spacedim> |
| 1380 |
template <typename Number> |
| 1381 |
void |
| 1382 |
KellyErrorEstimator<dim, spacedim>::estimate( |
| 1383 |
const Mapping<dim, spacedim> &mapping, |
| 1384 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 1385 |
const Quadrature<dim - 1> &quadrature, |
| 1386 |
const std::map<types::boundary_id, const Function<spacedim, Number> *> |
| 1387 |
&neumann_bc, |
| 1388 |
const ArrayView<const ReadVector<Number> *> &solutions, |
| 1389 |
ArrayView<Vector<float> *> &errors, |
| 1390 |
const ComponentMask &component_mask, |
| 1391 |
const Function<spacedim> *coefficients, |
| 1392 |
const unsigned int n_threads, |
| 1393 |
const types::subdomain_id subdomain_id, |
| 1394 |
const types::material_id material_id, |
| 1395 |
const Strategy strategy) |
| 1396 |
{ |
| 1397 |
const hp::MappingCollection<dim, spacedim> mapping_collection(mapping); |
| 1398 |
const hp::QCollection<dim - 1> quadrature_collection(quadrature); |
| 1399 |
estimate(mapping_collection, |
| 1400 |
dof_handler, |
| 1401 |
quadrature_collection, |
| 1402 |
neumann_bc, |
| 1403 |
solutions, |
| 1404 |
errors, |
| 1405 |
component_mask, |
| 1406 |
coefficients, |
| 1407 |
n_threads, |
| 1408 |
subdomain_id, |
| 1409 |
material_id, |
| 1410 |
strategy); |
| 1411 |
} |
| 1412 |
|
| 1413 |
|
| 1414 |
template <int dim, int spacedim> |
| 1415 |
template <typename Number> |
| 1416 |
void |
| 1417 |
KellyErrorEstimator<dim, spacedim>::estimate( |
| 1418 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 1419 |
const Quadrature<dim - 1> &quadrature, |
| 1420 |
const std::map<types::boundary_id, const Function<spacedim, Number> *> |
| 1421 |
&neumann_bc, |
| 1422 |
const ArrayView<const ReadVector<Number> *> &solutions, |
| 1423 |
ArrayView<Vector<float> *> &errors, |
| 1424 |
const ComponentMask &component_mask, |
| 1425 |
const Function<spacedim> *coefficients, |
| 1426 |
const unsigned int n_threads, |
| 1427 |
const types::subdomain_id subdomain_id, |
| 1428 |
const types::material_id material_id, |
| 1429 |
const Strategy strategy) |
| 1430 |
{ |
| 1431 |
estimate(get_default_linear_mapping(dof_handler.get_triangulation()), |
| 1432 |
dof_handler, |
| 1433 |
quadrature, |
| 1434 |
neumann_bc, |
| 1435 |
solutions, |
| 1436 |
errors, |
| 1437 |
component_mask, |
| 1438 |
coefficients, |
| 1439 |
n_threads, |
| 1440 |
subdomain_id, |
| 1441 |
material_id, |
| 1442 |
strategy); |
| 1443 |
} |
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
template <int dim, int spacedim> |
| 1448 |
template <typename Number> |
| 1449 |
void |
| 1450 |
KellyErrorEstimator<dim, spacedim>::estimate( |
| 1451 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 1452 |
const hp::QCollection<dim - 1> &quadrature, |
| 1453 |
const std::map<types::boundary_id, const Function<spacedim, Number> *> |
| 1454 |
&neumann_bc, |
| 1455 |
const ArrayView<const ReadVector<Number> *> &solutions, |
| 1456 |
ArrayView<Vector<float> *> &errors, |
| 1457 |
const ComponentMask &component_mask, |
| 1458 |
const Function<spacedim> *coefficients, |
| 1459 |
const unsigned int n_threads, |
| 1460 |
const types::subdomain_id subdomain_id, |
| 1461 |
const types::material_id material_id, |
| 1462 |
const Strategy strategy) |
| 1463 |
{ |
| 1464 |
const hp::MappingCollection<dim, spacedim> mapping( |
| 1465 |
get_default_linear_mapping(dof_handler.get_triangulation())); |
| 1466 |
estimate(mapping, |
| 1467 |
dof_handler, |
| 1468 |
quadrature, |
| 1469 |
neumann_bc, |
| 1470 |
solutions, |
| 1471 |
errors, |
| 1472 |
component_mask, |
| 1473 |
coefficients, |
| 1474 |
n_threads, |
| 1475 |
subdomain_id, |
| 1476 |
material_id, |
| 1477 |
strategy); |
| 1478 |
} |
| 1479 |
|
| 1480 |
DEAL_II_NAMESPACE_CLOSE |
| 1481 |
|
| 1482 |
#endif |