| 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 |
#ifndef dealii_vector_tools_interpolate_templates_h |
| 16 |
#define dealii_vector_tools_interpolate_templates_h |
| 17 |
|
| 18 |
|
| 19 |
#include <deal.II/dofs/dof_tools.h> |
| 20 |
|
| 21 |
#include <deal.II/fe/fe_nothing.h> |
| 22 |
#include <deal.II/fe/fe_q.h> |
| 23 |
#include <deal.II/fe/fe_system.h> |
| 24 |
|
| 25 |
#include <deal.II/grid/filtered_iterator.h> |
| 26 |
#include <deal.II/grid/grid_tools.h> |
| 27 |
#include <deal.II/grid/intergrid_map.h> |
| 28 |
|
| 29 |
#include <deal.II/hp/fe_values.h> |
| 30 |
|
| 31 |
#include <deal.II/lac/block_vector.h> |
| 32 |
#include <deal.II/lac/la_parallel_block_vector.h> |
| 33 |
#include <deal.II/lac/la_parallel_vector.h> |
| 34 |
#include <deal.II/lac/petsc_block_vector.h> |
| 35 |
#include <deal.II/lac/petsc_vector.h> |
| 36 |
#include <deal.II/lac/trilinos_parallel_block_vector.h> |
| 37 |
#include <deal.II/lac/trilinos_vector.h> |
| 38 |
|
| 39 |
#include <deal.II/multigrid/mg_transfer_global_coarsening.h> |
| 40 |
|
| 41 |
#include <deal.II/numerics/vector_tools_interpolate.h> |
| 42 |
|
| 43 |
DEAL_II_NAMESPACE_OPEN |
| 44 |
|
| 45 |
namespace LinearAlgebra |
| 46 |
{ |
| 47 |
namespace EpetraWrappers |
| 48 |
{ |
| 49 |
class Vector; |
| 50 |
} |
| 51 |
} // namespace LinearAlgebra |
| 52 |
|
| 53 |
|
| 54 |
namespace VectorTools |
| 55 |
{ |
| 56 |
// This namespace contains the actual implementation called |
| 57 |
// by VectorTools::interpolate and variants (such as |
| 58 |
// VectorTools::interpolate_by_material_id). |
| 59 |
namespace internal |
| 60 |
{ |
| 61 |
// A small helper function to transform a component range starting |
| 62 |
// at offset from the real to the unit cell according to the |
| 63 |
// supplied conformity. The function_values vector is transformed |
| 64 |
// in place. |
| 65 |
// |
| 66 |
// FIXME: This should be refactored into the mapping (i.e. |
| 67 |
// implement the inverse function of Mapping<dim, spacedim>::transform). |
| 68 |
// Further, the finite element should make the information about |
| 69 |
// the correct mapping directly accessible (i.e. which MappingKind |
| 70 |
// should be used). Using fe.conforming_space might be a bit of a |
| 71 |
// problem because we only support doing nothing, Hcurl, and Hdiv |
| 72 |
// conforming mappings. |
| 73 |
// |
| 74 |
// Input: |
| 75 |
// conformity: conformity of the finite element, used to select |
| 76 |
// appropriate type of transformation |
| 77 |
// fe_values_jacobians: used for jacobians (and inverses of |
| 78 |
// jacobians). the object is supposed to be |
| 79 |
// reinit()'d for the current cell |
| 80 |
// function_values, offset: function_values is manipulated in place |
| 81 |
// starting at position offset |
| 82 |
template <int dim, int spacedim, typename FEValuesType, typename T3> |
| 83 |
void |
| 84 |
transform(const typename FiniteElementData<dim>::Conformity conformity, |
| 85 |
const unsigned int offset, |
| 86 |
const FEValuesType &fe_values_jacobians, |
| 87 |
T3 &function_values) |
| 88 |
{ |
| 89 |
switch (conformity) |
| 90 |
{ |
| 91 |
case FiniteElementData<dim>::Hcurl: |
| 92 |
// See Monk, Finite Element Methods for Maxwell's Equations, |
| 93 |
// p. 77ff, formula (3.76) and Corollary 3.58. |
| 94 |
// For given mapping F_K: \hat K \to K, we have to transform |
| 95 |
// \hat u = (dF_K)^T u\circ F_K |
| 96 |
|
| 97 |
for (unsigned int i = 0; i < function_values.size(); ++i) |
| 98 |
{ |
| 99 |
const auto &jacobians = |
| 100 |
fe_values_jacobians.get_present_fe_values().get_jacobians(); |
| 101 |
|
| 102 |
const ArrayView<typename T3::value_type::value_type> source( |
| 103 |
&function_values[i][0] + offset, dim); |
| 104 |
|
| 105 |
Tensor<1, |
| 106 |
dim, |
| 107 |
typename ProductType<typename T3::value_type::value_type, |
| 108 |
double>::type> |
| 109 |
destination; |
| 110 |
|
| 111 |
// value[m] <- sum jacobian_transpose[m][n] * old_value[n]: |
| 112 |
TensorAccessors::contract<1, 2, 1, dim>( |
| 113 |
destination, jacobians[i].transpose(), source); |
| 114 |
|
| 115 |
// now copy things back into the input=output vector |
| 116 |
for (unsigned int d = 0; d < dim; ++d) |
| 117 |
source[d] = destination[d]; |
| 118 |
} |
| 119 |
break; |
| 120 |
|
| 121 |
case FiniteElementData<dim>::Hdiv: |
| 122 |
// See Monk, Finite Element Methods for Maxwell's Equations, |
| 123 |
// p. 79ff, formula (3.77) and Lemma 3.59. |
| 124 |
// For given mapping F_K: \hat K \to K, we have to transform |
| 125 |
// \hat w = det(dF_K) (dF_K)^{-1} w\circ F_K |
| 126 |
|
| 127 |
for (unsigned int i = 0; i < function_values.size(); ++i) |
| 128 |
{ |
| 129 |
const auto &jacobians = |
| 130 |
fe_values_jacobians.get_present_fe_values().get_jacobians(); |
| 131 |
const auto &inverse_jacobians = |
| 132 |
fe_values_jacobians.get_present_fe_values() |
| 133 |
.get_inverse_jacobians(); |
| 134 |
|
| 135 |
const ArrayView<typename T3::value_type::value_type> source( |
| 136 |
&function_values[i][0] + offset, dim); |
| 137 |
|
| 138 |
Tensor<1, |
| 139 |
dim, |
| 140 |
typename ProductType<typename T3::value_type::value_type, |
| 141 |
double>::type> |
| 142 |
destination; |
| 143 |
|
| 144 |
// value[m] <- sum inverse_jacobians[m][n] * old_value[n]: |
| 145 |
TensorAccessors::contract<1, 2, 1, dim>(destination, |
| 146 |
inverse_jacobians[i], |
| 147 |
source); |
| 148 |
destination *= jacobians[i].determinant(); |
| 149 |
|
| 150 |
// now copy things back into the input=output vector |
| 151 |
for (unsigned int d = 0; d < dim; ++d) |
| 152 |
source[d] = destination[d]; |
| 153 |
} |
| 154 |
break; |
| 155 |
|
| 156 |
case FiniteElementData<dim>::H1: |
| 157 |
DEAL_II_FALLTHROUGH; |
| 158 |
case FiniteElementData<dim>::L2: |
| 159 |
// See Monk, Finite Element Methods for Maxwell's Equations, |
| 160 |
// p. 77ff, formula (3.74). |
| 161 |
// For given mapping F_K: \hat K \to K, we have to transform |
| 162 |
// \hat p = p\circ F_K |
| 163 |
// i.e., do nothing. |
| 164 |
break; |
| 165 |
|
| 166 |
default: |
| 167 |
// In case we deal with an unknown conformity, just assume we |
| 168 |
// deal with a Lagrange element and do nothing. |
| 169 |
break; |
| 170 |
|
| 171 |
} /*switch*/ |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
// A small helper function that iteratively applies above transform |
| 176 |
// function to a vector function_values recursing over a given finite |
| 177 |
// element decomposing it into base elements: |
| 178 |
// |
| 179 |
// Input |
| 180 |
// fe: the full finite element corresponding to function_values |
| 181 |
// [ rest see above] |
| 182 |
// Output: the offset after we have handled the element at |
| 183 |
// a given offset |
| 184 |
template <int dim, int spacedim, typename FEValuesType, typename T3> |
| 185 |
unsigned int |
| 186 |
apply_transform(const FiniteElement<dim, spacedim> &fe, |
| 187 |
const unsigned int offset, |
| 188 |
const FEValuesType &fe_values_jacobians, |
| 189 |
T3 &function_values) |
| 190 |
{ |
| 191 |
if (fe.n_base_elements() > 1 || fe.element_multiplicity(0) > 1) |
| 192 |
{ |
| 193 |
// In case of an FESystem transform every (vector) component |
| 194 |
// separately: |
| 195 |
unsigned current_offset = offset; |
| 196 |
for (unsigned int i = 0; i < fe.n_base_elements(); ++i) |
| 197 |
{ |
| 198 |
const auto &base_fe = fe.base_element(i); |
| 199 |
const auto multiplicity = fe.element_multiplicity(i); |
| 200 |
for (unsigned int m = 0; m < multiplicity; ++m) |
| 201 |
{ |
| 202 |
// recursively call apply_transform to make sure to |
| 203 |
// correctly handle nested FE systems. |
| 204 |
current_offset = apply_transform(base_fe, |
| 205 |
current_offset, |
| 206 |
fe_values_jacobians, |
| 207 |
function_values); |
| 208 |
} |
| 209 |
} |
| 210 |
return current_offset; |
| 211 |
} |
| 212 |
else |
| 213 |
{ |
| 214 |
transform<dim, spacedim>(fe.conforming_space, |
| 215 |
offset, |
| 216 |
fe_values_jacobians, |
| 217 |
function_values); |
| 218 |
return (offset + fe.n_components()); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
// Internal implementation of interpolate that takes a generic functor |
| 224 |
// function such that function(cell) is of type |
| 225 |
// Function<spacedim, typename VectorType::value_type>* |
| 226 |
// |
| 227 |
// A given cell is skipped if function(cell) == nullptr |
| 228 |
template <int dim, int spacedim, typename VectorType, typename T> |
| 229 |
DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>) |
| 230 |
void interpolate( |
| 231 |
const hp::MappingCollection<dim, spacedim> &mapping_collection, |
| 232 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 233 |
T &function, |
| 234 |
VectorType &vec, |
| 235 |
const ComponentMask &component_mask, |
| 236 |
const unsigned int level = numbers::invalid_unsigned_int) |
| 237 |
{ |
| 238 |
Assert(component_mask.represents_n_components( |
| 239 |
dof_handler.get_fe_collection().n_components()), |
| 240 |
ExcMessage( |
| 241 |
"The number of components in the mask has to be either " |
| 242 |
"zero or equal to the number of components in the finite " |
| 243 |
"element.")); |
| 244 |
|
| 245 |
if (level == numbers::invalid_unsigned_int) |
| 246 |
AssertDimension(vec.size(), dof_handler.n_dofs()); |
| 247 |
else |
| 248 |
AssertDimension(vec.size(), dof_handler.n_dofs(level)); |
| 249 |
|
| 250 |
Assert(component_mask.n_selected_components( |
| 251 |
dof_handler.get_fe_collection().n_components()) > 0, |
| 252 |
ComponentMask::ExcNoComponentSelected()); |
| 253 |
|
| 254 |
// |
| 255 |
// Computing the generalized interpolant isn't quite as straightforward |
| 256 |
// as for classical Lagrange elements. A major complication is the fact |
| 257 |
// it generally doesn't hold true that a function evaluates to the same |
| 258 |
// dof coefficient on different cells. This means *setting* the value |
| 259 |
// of a (global) degree of freedom computed on one cell doesn't |
| 260 |
// necessarily lead to the same result when computed on a neighboring |
| 261 |
// cell (that shares the same global degree of freedom). |
| 262 |
// |
| 263 |
// We thus, do the following operation: |
| 264 |
// |
| 265 |
// On each cell: |
| 266 |
// |
| 267 |
// - We first determine all function values u(x_i) in generalized |
| 268 |
// support points |
| 269 |
// |
| 270 |
// - We transform these function values back to the unit cell |
| 271 |
// according to the conformity of the component (scalar, Hdiv, or |
| 272 |
// Hcurl conforming); see [Monk, Finite Element Methods for Maxwell's |
| 273 |
// Equations, p.77ff Section 3.9] for details. This results in |
| 274 |
// \hat u(\hat x_i) |
| 275 |
// |
| 276 |
// - We convert these generalized support point values to nodal values |
| 277 |
// |
| 278 |
// - For every global dof we take the average 1 / n_K \sum_{K} dof_K |
| 279 |
// where n_K is the number of cells sharing the global dof and dof_K |
| 280 |
// is the computed value on the cell K. |
| 281 |
// |
| 282 |
// For every degree of freedom that is shared by k cells, we compute |
| 283 |
// its value on all k cells and take the weighted average with respect |
| 284 |
// to the JxW values. |
| 285 |
// |
| 286 |
|
| 287 |
using number = typename VectorType::value_type; |
| 288 |
|
| 289 |
const hp::FECollection<dim, spacedim> &fe( |
| 290 |
dof_handler.get_fe_collection()); |
| 291 |
|
| 292 |
std::vector<types::global_dof_index> dofs_on_cell(fe.max_dofs_per_cell()); |
| 293 |
|
| 294 |
// Temporary storage for cell-wise interpolation operation. We store a |
| 295 |
// variant for every FE we encounter to speed up resizing operations. |
| 296 |
// The first vector is used for local function evaluation. The vector |
| 297 |
// dof_values is used to store intermediate cell-wise interpolation |
| 298 |
// results (see the detailed explanation in the for loop further down |
| 299 |
// below). |
| 300 |
|
| 301 |
std::vector<std::vector<Vector<number>>> fe_function_values(fe.size()); |
| 302 |
std::vector<std::vector<number>> fe_dof_values(fe.size()); |
| 303 |
|
| 304 |
// We will need two temporary global vectors that store the new values |
| 305 |
// and weights. |
| 306 |
VectorType interpolation; |
| 307 |
VectorType weights; |
| 308 |
interpolation.reinit(vec); |
| 309 |
weights.reinit(vec); |
| 310 |
|
| 311 |
// Store locally owned dofs, so that we can skip all non-local dofs, |
| 312 |
// if they do not need to be interpolated. |
| 313 |
const IndexSet locally_owned_dofs = vec.locally_owned_elements(); |
| 314 |
|
| 315 |
// We use an FEValues object to transform all generalized support |
| 316 |
// points from the unit cell to the real cell coordinates. Thus, |
| 317 |
// initialize a quadrature with all generalized support points and |
| 318 |
// create an FEValues object with it. |
| 319 |
|
| 320 |
std::vector<bool> needs_expensive_algorithm(fe.size(), true); |
| 321 |
hp::QCollection<dim> support_quadrature; |
| 322 |
for (unsigned int fe_index = 0; fe_index < fe.size(); ++fe_index) |
| 323 |
{ |
| 324 |
const auto &fe_i = fe[fe_index]; |
| 325 |
// If the finite element has no dofs, we can skip it |
| 326 |
if (fe_i.dofs_per_cell == 0) |
| 327 |
{ |
| 328 |
support_quadrature.push_back(Quadrature<dim>()); |
| 329 |
continue; |
| 330 |
} |
| 331 |
Assert(fe_i.has_generalized_support_points(), |
| 332 |
ExcMessage( |
| 333 |
"The finite element does not have generalized support " |
| 334 |
"points. This is required for interpolation.")); |
| 335 |
const auto &points = fe_i.get_generalized_support_points(); |
| 336 |
support_quadrature.push_back(Quadrature<dim>(points)); |
| 337 |
if (fe_i.n_base_elements() == 1 && |
| 338 |
fe_i.element_multiplicity(0) == fe.n_components() && |
| 339 |
fe_i.has_support_points()) |
| 340 |
{ |
| 341 |
const auto &fe_base = fe_i.base_element(0); |
| 342 |
bool all_points_equal = true; |
| 343 |
// Check points for exact equality - they are either copied |
| 344 |
// inside an FESystem or genuinely different, so no need for a |
| 345 |
// tolerance |
| 346 |
for (unsigned int i = 0; i < fe_base.n_dofs_per_cell(); ++i) |
| 347 |
if (fe_base.get_unit_support_points()[i].distance(points[i]) > |
| 348 |
0.) |
| 349 |
{ |
| 350 |
all_points_equal = false; |
| 351 |
break; |
| 352 |
} |
| 353 |
if (all_points_equal) |
| 354 |
needs_expensive_algorithm[fe_index] = false; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
// An FEValues object to evaluate (generalized) support point |
| 359 |
// locations as well as Jacobians and their inverses. |
| 360 |
// The latter are only needed for Hcurl or Hdiv conforming elements, |
| 361 |
// but we'll just always include them. |
| 362 |
hp::FEValues<dim, spacedim> fe_values(mapping_collection, |
| 363 |
fe, |
| 364 |
support_quadrature, |
| 365 |
update_quadrature_points | |
| 366 |
update_jacobians | |
| 367 |
update_inverse_jacobians); |
| 368 |
|
| 369 |
// |
| 370 |
// Now loop over all locally owned, active cells. |
| 371 |
// |
| 372 |
const auto runner = [&](const auto &cell) { |
| 373 |
const unsigned int fe_index = cell->active_fe_index(); |
| 374 |
|
| 375 |
// Do nothing if there are no local degrees of freedom. |
| 376 |
if (fe[fe_index].n_dofs_per_cell() == 0) |
| 377 |
return; |
| 378 |
|
| 379 |
// Skip processing of the current cell if the function object is |
| 380 |
// invalid. This is used by interpolate_by_material_id to skip |
| 381 |
// interpolating over cells with unknown material id. |
| 382 |
if (!function(cell)) |
| 383 |
return; |
| 384 |
|
| 385 |
// Get transformed, generalized support points |
| 386 |
fe_values.reinit(cell); |
| 387 |
const std::vector<Point<spacedim>> &generalized_support_points = |
| 388 |
fe_values.get_present_fe_values().get_quadrature_points(); |
| 389 |
|
| 390 |
// Get indices of the dofs on this cell |
| 391 |
const auto n_dofs = fe[fe_index].n_dofs_per_cell(); |
| 392 |
dofs_on_cell.resize(n_dofs); |
| 393 |
cell->get_active_or_mg_dof_indices(dofs_on_cell); |
| 394 |
|
| 395 |
// Prepare temporary storage |
| 396 |
auto &function_values = fe_function_values[fe_index]; |
| 397 |
auto &dof_values = fe_dof_values[fe_index]; |
| 398 |
|
| 399 |
const auto n_components = fe[fe_index].n_components(); |
| 400 |
// Only resize (and create sample entry) if sizes do not match |
| 401 |
if (function_values.size() != generalized_support_points.size()) |
| 402 |
function_values.resize(generalized_support_points.size(), |
| 403 |
Vector<number>(n_components)); |
| 404 |
|
| 405 |
// Get all function values: |
| 406 |
AssertDimension(n_components, function(cell)->n_components); |
| 407 |
function(cell)->vector_value_list(generalized_support_points, |
| 408 |
function_values); |
| 409 |
|
| 410 |
// For the simple case with elements with support points, we will |
| 411 |
// simply use the interpolated DoF values in the access loop further |
| 412 |
// down. Otherwise, we have to transform all function values from |
| 413 |
// the real cell back to the unit cell. We query the finite element |
| 414 |
// for the correct transformation. Matters get a bit more |
| 415 |
// complicated because we have to apply said transformation for |
| 416 |
// every base element. |
| 417 |
if (needs_expensive_algorithm[fe_index]) |
| 418 |
{ |
| 419 |
dof_values.resize(n_dofs); |
| 420 |
const unsigned int offset = |
| 421 |
apply_transform(fe[fe_index], |
| 422 |
/* starting_offset = */ 0, |
| 423 |
fe_values, |
| 424 |
function_values); |
| 425 |
Assert(offset == n_components, ExcInternalError()); |
| 426 |
|
| 427 |
FETools::convert_generalized_support_point_values_to_dof_values( |
| 428 |
fe[fe_index], function_values, dof_values); |
| 429 |
} |
| 430 |
|
| 431 |
for (unsigned int i = 0; i < n_dofs; ++i) |
| 432 |
{ |
| 433 |
const auto &nonzero_components = |
| 434 |
fe[fe_index].get_nonzero_components(i); |
| 435 |
|
| 436 |
// Figure out whether the component mask applies. We assume |
| 437 |
// that we are allowed to set degrees of freedom if at least |
| 438 |
// one of the components (of the dof) is selected. |
| 439 |
bool selected = false; |
| 440 |
for (unsigned int c = 0; c < nonzero_components.size(); ++c) |
| 441 |
selected = |
| 442 |
selected || (nonzero_components[c] && component_mask[c]); |
| 443 |
|
| 444 |
if (selected) |
| 445 |
{ |
| 446 |
if constexpr (running_in_debug_mode()) |
| 447 |
{ |
| 448 |
// make sure that all selected base elements are indeed |
| 449 |
// interpolatory |
| 450 |
|
| 451 |
if (const auto fe_system = |
| 452 |
dynamic_cast<const FESystem<dim> *>(&fe[fe_index])) |
| 453 |
{ |
| 454 |
const auto index = |
| 455 |
fe_system->system_to_base_index(i).first.first; |
| 456 |
Assert(fe_system->base_element(index) |
| 457 |
.has_generalized_support_points(), |
| 458 |
ExcMessage("The component mask supplied to " |
| 459 |
"VectorTools::interpolate selects a " |
| 460 |
"non-interpolatory element.")); |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
// Add local values to the global vectors |
| 465 |
if (needs_expensive_algorithm[fe_index]) |
| 466 |
::dealii::internal::ElementAccess<VectorType>::add( |
| 467 |
dof_values[i], dofs_on_cell[i], interpolation); |
| 468 |
else |
| 469 |
{ |
| 470 |
const auto base_index = |
| 471 |
fe[fe_index].system_to_base_index(i); |
| 472 |
::dealii::internal::ElementAccess<VectorType>::add( |
| 473 |
function_values[base_index.second] |
| 474 |
[base_index.first.second], |
| 475 |
dofs_on_cell[i], |
| 476 |
interpolation); |
| 477 |
} |
| 478 |
::dealii::internal::ElementAccess<VectorType>::add( |
| 479 |
typename VectorType::value_type(1.0), |
| 480 |
dofs_on_cell[i], |
| 481 |
weights); |
| 482 |
} |
| 483 |
else |
| 484 |
{ |
| 485 |
// If a component is ignored, copy the dof values |
| 486 |
// from the vector "vec", but only if they are locally |
| 487 |
// available |
| 488 |
if (locally_owned_dofs.is_element(dofs_on_cell[i])) |
| 489 |
{ |
| 490 |
const auto value = |
| 491 |
::dealii::internal::ElementAccess<VectorType>::get( |
| 492 |
vec, dofs_on_cell[i]); |
| 493 |
::dealii::internal::ElementAccess<VectorType>::add( |
| 494 |
value, dofs_on_cell[i], interpolation); |
| 495 |
::dealii::internal::ElementAccess<VectorType>::add( |
| 496 |
typename VectorType::value_type(1.0), |
| 497 |
dofs_on_cell[i], |
| 498 |
weights); |
| 499 |
} |
| 500 |
} |
| 501 |
} |
| 502 |
}; |
| 503 |
|
| 504 |
if (level == numbers::invalid_unsigned_int) |
| 505 |
{ |
| 506 |
for (const auto &cell : dof_handler.active_cell_iterators()) |
| 507 |
{ |
| 508 |
if (cell->is_locally_owned()) |
| 509 |
runner(cell); |
| 510 |
} |
| 511 |
} |
| 512 |
else |
| 513 |
{ |
| 514 |
for (const auto &cell : dof_handler.mg_cell_iterators_on_level(level)) |
| 515 |
if (cell->is_locally_owned_on_level()) |
| 516 |
runner(cell); |
| 517 |
} |
| 518 |
|
| 519 |
interpolation.compress(VectorOperation::add); |
| 520 |
weights.compress(VectorOperation::add); |
| 521 |
|
| 522 |
for (const auto i : interpolation.locally_owned_elements()) |
| 523 |
{ |
| 524 |
const auto weight = |
| 525 |
::dealii::internal::ElementAccess<VectorType>::get(weights, i); |
| 526 |
|
| 527 |
// See if we touched this DoF at all. If so, set the average |
| 528 |
// of the value we computed in the output vector. Otherwise, |
| 529 |
// don't touch the value at all. |
| 530 |
if (weight != number(0)) |
| 531 |
{ |
| 532 |
const auto value = |
| 533 |
::dealii::internal::ElementAccess<VectorType>::get( |
| 534 |
interpolation, i); |
| 535 |
::dealii::internal::ElementAccess<VectorType>::set(value / weight, |
| 536 |
i, |
| 537 |
vec); |
| 538 |
} |
| 539 |
} |
| 540 |
vec.compress(VectorOperation::insert); |
| 541 |
} |
| 542 |
|
| 543 |
} // namespace internal |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
template <int dim, int spacedim, typename VectorType> |
| 548 |
DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>) |
| 549 |
void interpolate( |
| 550 |
const hp::MappingCollection<dim, spacedim> &mapping, |
| 551 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 552 |
const Function<spacedim, typename VectorType::value_type> &function, |
| 553 |
VectorType &vec, |
| 554 |
const ComponentMask &component_mask, |
| 555 |
const unsigned int level) |
| 556 |
{ |
| 557 |
AssertDimension(dof_handler.get_fe_collection().n_components(), |
| 558 |
function.n_components); |
| 559 |
|
| 560 |
// Create a small lambda capture wrapping function and call the |
| 561 |
// internal implementation |
| 562 |
const auto function_map = [&function](const auto &) |
| 563 |
-> const Function<spacedim, typename VectorType::value_type> * { |
| 564 |
return &function; |
| 565 |
}; |
| 566 |
|
| 567 |
internal::interpolate( |
| 568 |
mapping, dof_handler, function_map, vec, component_mask, level); |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
template <int dim, int spacedim, typename VectorType> |
| 574 |
DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>) |
| 575 |
void interpolate( |
| 576 |
const Mapping<dim, spacedim> &mapping, |
| 577 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 578 |
const Function<spacedim, typename VectorType::value_type> &function, |
| 579 |
VectorType &vec, |
| 580 |
const ComponentMask &component_mask, |
| 581 |
const unsigned int level) |
| 582 |
{ |
| 583 |
interpolate(hp::MappingCollection<dim, spacedim>(mapping), |
| 584 |
dof_handler, |
| 585 |
function, |
| 586 |
vec, |
| 587 |
component_mask, |
| 588 |
level); |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
template <int dim, int spacedim, typename VectorType> |
| 594 |
DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>) |
| 595 |
void interpolate( |
| 596 |
const DoFHandler<dim, spacedim> &dof, |
| 597 |
const Function<spacedim, typename VectorType::value_type> &function, |
| 598 |
VectorType &vec, |
| 599 |
const ComponentMask &component_mask, |
| 600 |
const unsigned int level) |
| 601 |
{ |
| 602 |
AssertDimension(dof.get_fe_collection().n_components(), |
| 603 |
function.n_components); |
| 604 |
interpolate(get_default_linear_mapping(dof.get_triangulation()), |
| 605 |
dof, |
| 606 |
function, |
| 607 |
vec, |
| 608 |
component_mask, |
| 609 |
level); |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
template <int dim, class InVector, class OutVector, int spacedim> |
| 615 |
DEAL_II_CXX20_REQUIRES(concepts::is_dealii_vector_type<InVector> && |
| 616 |
concepts::is_writable_dealii_vector_type<OutVector>) |
| 617 |
void interpolate(const DoFHandler<dim, spacedim> &dof_1, |
| 618 |
const DoFHandler<dim, spacedim> &dof_2, |
| 619 |
const FullMatrix<double> &transfer, |
| 620 |
const InVector &data_1, |
| 621 |
OutVector &data_2) |
| 622 |
{ |
| 623 |
using number = typename OutVector::value_type; |
| 624 |
Vector<number> cell_data_1(dof_1.get_fe().n_dofs_per_cell()); |
| 625 |
Vector<number> cell_data_2(dof_2.get_fe().n_dofs_per_cell()); |
| 626 |
|
| 627 |
// Reset output vector. |
| 628 |
data_2 = static_cast<number>(0); |
| 629 |
|
| 630 |
// Store how many cells share each dof (unghosted). |
| 631 |
OutVector touch_count; |
| 632 |
touch_count.reinit(data_2); |
| 633 |
|
| 634 |
std::vector<types::global_dof_index> local_dof_indices( |
| 635 |
dof_2.get_fe().n_dofs_per_cell()); |
| 636 |
|
| 637 |
typename DoFHandler<dim, spacedim>::active_cell_iterator cell_1 = |
| 638 |
dof_1.begin_active(); |
| 639 |
typename DoFHandler<dim, spacedim>::active_cell_iterator cell_2 = |
| 640 |
dof_2.begin_active(); |
| 641 |
const typename DoFHandler<dim, spacedim>::cell_iterator end_1 = dof_1.end(); |
| 642 |
|
| 643 |
for (; cell_1 != end_1; ++cell_1, ++cell_2) |
| 644 |
{ |
| 645 |
if (cell_1->is_locally_owned()) |
| 646 |
{ |
| 647 |
Assert(cell_2->is_locally_owned(), ExcInternalError()); |
| 648 |
|
| 649 |
// Perform dof interpolation. |
| 650 |
cell_1->get_dof_values(data_1, cell_data_1); |
| 651 |
transfer.vmult(cell_data_2, cell_data_1); |
| 652 |
|
| 653 |
cell_2->get_dof_indices(local_dof_indices); |
| 654 |
|
| 655 |
// Distribute cell vector. |
| 656 |
for (unsigned int j = 0; j < dof_2.get_fe().n_dofs_per_cell(); ++j) |
| 657 |
{ |
| 658 |
::dealii::internal::ElementAccess<OutVector>::add( |
| 659 |
cell_data_2(j), local_dof_indices[j], data_2); |
| 660 |
|
| 661 |
// Count cells that share each dof. |
| 662 |
::dealii::internal::ElementAccess<OutVector>::add( |
| 663 |
static_cast<number>(1), local_dof_indices[j], touch_count); |
| 664 |
} |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
// Collect information over all the parallel processes. |
| 669 |
data_2.compress(VectorOperation::add); |
| 670 |
touch_count.compress(VectorOperation::add); |
| 671 |
|
| 672 |
// Compute the mean value of the sum which has been placed in |
| 673 |
// each entry of the output vector only at locally owned elements. |
| 674 |
for (const auto i : data_2.locally_owned_elements()) |
| 675 |
{ |
| 676 |
const number touch_count_i = |
| 677 |
::dealii::internal::ElementAccess<OutVector>::get(touch_count, i); |
| 678 |
|
| 679 |
Assert(touch_count_i != static_cast<number>(0), ExcInternalError()); |
| 680 |
|
| 681 |
const number value = |
| 682 |
::dealii::internal::ElementAccess<OutVector>::get(data_2, i) / |
| 683 |
touch_count_i; |
| 684 |
|
| 685 |
::dealii::internal::ElementAccess<OutVector>::set(value, i, data_2); |
| 686 |
} |
| 687 |
|
| 688 |
// Compress data_2 to set the proper values on all the parallel processes. |
| 689 |
data_2.compress(VectorOperation::insert); |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
template <int dim, int spacedim, typename VectorType> |
| 695 |
DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>) |
| 696 |
void get_position_vector(const DoFHandler<dim, spacedim> &dh, |
| 697 |
VectorType &vector, |
| 698 |
const ComponentMask &mask) |
| 699 |
{ |
| 700 |
const FiniteElement<dim, spacedim> &fe = dh.get_fe(); |
| 701 |
get_position_vector( |
| 702 |
*fe.reference_cell().template get_default_mapping<dim, spacedim>( |
| 703 |
fe.degree), |
| 704 |
dh, |
| 705 |
vector, |
| 706 |
mask); |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
template <int dim, int spacedim, typename VectorType> |
| 712 |
DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>) |
| 713 |
void get_position_vector(const Mapping<dim, spacedim> &map_q, |
| 714 |
const DoFHandler<dim, spacedim> &dh, |
| 715 |
VectorType &vector, |
| 716 |
const ComponentMask &mask) |
| 717 |
{ |
| 718 |
AssertDimension(vector.size(), dh.n_dofs()); |
| 719 |
const FiniteElement<dim, spacedim> &fe = dh.get_fe(); |
| 720 |
|
| 721 |
// Construct default fe_mask; |
| 722 |
const ComponentMask fe_mask( |
| 723 |
mask.size() ? mask : |
| 724 |
ComponentMask(fe.get_nonzero_components(0).size(), true)); |
| 725 |
|
| 726 |
AssertDimension(fe_mask.size(), fe.get_nonzero_components(0).size()); |
| 727 |
|
| 728 |
std::vector<unsigned int> fe_to_real(fe_mask.size(), |
| 729 |
numbers::invalid_unsigned_int); |
| 730 |
unsigned int size = 0; |
| 731 |
for (unsigned int i = 0; i < fe_mask.size(); ++i) |
| 732 |
{ |
| 733 |
if (fe_mask[i]) |
| 734 |
fe_to_real[i] = size++; |
| 735 |
} |
| 736 |
Assert( |
| 737 |
size == spacedim, |
| 738 |
ExcMessage( |
| 739 |
"The Component Mask you provided is invalid. It has to select exactly spacedim entries.")); |
| 740 |
|
| 741 |
|
| 742 |
if (fe.has_support_points()) |
| 743 |
{ |
| 744 |
const Quadrature<dim> quad(fe.get_unit_support_points()); |
| 745 |
|
| 746 |
FEValues<dim, spacedim> fe_v(map_q, fe, quad, update_quadrature_points); |
| 747 |
std::vector<types::global_dof_index> dofs(fe.n_dofs_per_cell()); |
| 748 |
|
| 749 |
AssertDimension(fe.n_dofs_per_cell(), |
| 750 |
fe.get_unit_support_points().size()); |
| 751 |
Assert(fe.is_primitive(), |
| 752 |
ExcMessage("FE is not Primitive! This won't work.")); |
| 753 |
|
| 754 |
for (const auto &cell : |
| 755 |
dh.active_cell_iterators() | IteratorFilters::LocallyOwnedCell()) |
| 756 |
{ |
| 757 |
fe_v.reinit(cell); |
| 758 |
cell->get_dof_indices(dofs); |
| 759 |
const std::vector<Point<spacedim>> &points = |
| 760 |
fe_v.get_quadrature_points(); |
| 761 |
for (unsigned int q = 0; q < points.size(); ++q) |
| 762 |
{ |
| 763 |
const unsigned int comp = fe.system_to_component_index(q).first; |
| 764 |
if (fe_mask[comp]) |
| 765 |
::dealii::internal::ElementAccess<VectorType>::set( |
| 766 |
points[q][fe_to_real[comp]], dofs[q], vector); |
| 767 |
} |
| 768 |
} |
| 769 |
} |
| 770 |
else |
| 771 |
{ |
| 772 |
// Construct a FiniteElement with FE_Q^spacedim, and call this |
| 773 |
// function again. |
| 774 |
// |
| 775 |
// Once we have this, interpolate with the given finite element |
| 776 |
// to get a Mapping which is interpolatory at the support points |
| 777 |
// of FE_Q(fe.degree()) |
| 778 |
const FESystem<dim, spacedim> *fe_system = |
| 779 |
dynamic_cast<const FESystem<dim, spacedim> *>(&fe); |
| 780 |
Assert(fe_system, ExcNotImplemented()); |
| 781 |
unsigned int degree = numbers::invalid_unsigned_int; |
| 782 |
|
| 783 |
// Get information about the blocks |
| 784 |
for (unsigned int i = 0; i < fe_mask.size(); ++i) |
| 785 |
if (fe_mask[i]) |
| 786 |
{ |
| 787 |
const unsigned int base_i = |
| 788 |
fe_system->component_to_base_index(i).first; |
| 789 |
Assert(degree == numbers::invalid_unsigned_int || |
| 790 |
degree == fe_system->base_element(base_i).degree, |
| 791 |
ExcNotImplemented()); |
| 792 |
Assert(fe_system->base_element(base_i).is_primitive(), |
| 793 |
ExcNotImplemented()); |
| 794 |
degree = fe_system->base_element(base_i).degree; |
| 795 |
} |
| 796 |
|
| 797 |
// We create an intermediate FE_Q vector space, and then |
| 798 |
// interpolate from that vector space to this one, by |
| 799 |
// carefully selecting the right components. |
| 800 |
|
| 801 |
FESystem<dim, spacedim> feq(FE_Q<dim, spacedim>(degree), spacedim); |
| 802 |
DoFHandler<dim, spacedim> dhq(dh.get_triangulation()); |
| 803 |
dhq.distribute_dofs(feq); |
| 804 |
Vector<double> eulerq(dhq.n_dofs()); |
| 805 |
const ComponentMask maskq(spacedim, true); |
| 806 |
get_position_vector(map_q, dhq, eulerq); |
| 807 |
|
| 808 |
FullMatrix<double> transfer(fe.n_dofs_per_cell(), |
| 809 |
feq.n_dofs_per_cell()); |
| 810 |
FullMatrix<double> local_transfer(feq.n_dofs_per_cell()); |
| 811 |
const std::vector<Point<dim>> &points = feq.get_unit_support_points(); |
| 812 |
|
| 813 |
// Here we construct the interpolation matrix from |
| 814 |
// FE_Q^spacedim to the FiniteElement used by |
| 815 |
// euler_dof_handler. |
| 816 |
// |
| 817 |
// In order to construct such interpolation matrix, we have to |
| 818 |
// solve the following system: |
| 819 |
// |
| 820 |
// v_j phi_j(q_i) = w_k psi_k(q_i) = w_k delta_ki = w_i |
| 821 |
// |
| 822 |
// where psi_k are the basis functions for fe_q, and phi_i are |
| 823 |
// the basis functions of the target space while q_i are the |
| 824 |
// support points for the fe_q space. With this choice of |
| 825 |
// interpolation points, on the matrices is the identity |
| 826 |
// matrix, and we have to invert only one matrix. The |
| 827 |
// resulting vector will be interpolatory at the support |
| 828 |
// points of fe_q, even if the finite element does not have |
| 829 |
// support points. |
| 830 |
// |
| 831 |
// Morally, we should invert the matrix T_ij = phi_i(q_j), |
| 832 |
// however in general this matrix is not invertible, since |
| 833 |
// there may be components which do not contribute to the |
| 834 |
// displacement vector. Since we are not interested in those |
| 835 |
// components, we construct a square matrix with the same |
| 836 |
// number of components of the FE_Q system. The FE_Q system |
| 837 |
// was constructed above in such a way that the polynomial |
| 838 |
// degree of the FE_Q system and that of the given FE are the |
| 839 |
// same on the cell, which should guarantee that, for the |
| 840 |
// displacement components only, the interpolation matrix is |
| 841 |
// invertible. We construct a mapping between indices first, |
| 842 |
// and check that this is the case. If not, we bail out, not |
| 843 |
// knowing what to do in this case. |
| 844 |
|
| 845 |
std::vector<unsigned int> fe_to_feq(fe.n_dofs_per_cell(), |
| 846 |
numbers::invalid_unsigned_int); |
| 847 |
unsigned int index = 0; |
| 848 |
for (unsigned int i = 0; i < fe.n_dofs_per_cell(); ++i) |
| 849 |
if (fe_mask[fe.system_to_component_index(i).first]) |
| 850 |
fe_to_feq[i] = index++; |
| 851 |
|
| 852 |
// If index is not the same as feq.n_dofs_per_cell(), we won't |
| 853 |
// know how to invert the resulting matrix. Bail out. |
| 854 |
Assert(index == feq.n_dofs_per_cell(), ExcNotImplemented()); |
| 855 |
|
| 856 |
for (unsigned int j = 0; j < fe.n_dofs_per_cell(); ++j) |
| 857 |
{ |
| 858 |
const unsigned int comp_j = fe.system_to_component_index(j).first; |
| 859 |
if (fe_mask[comp_j]) |
| 860 |
for (unsigned int i = 0; i < points.size(); ++i) |
| 861 |
{ |
| 862 |
if (fe_to_real[comp_j] == |
| 863 |
feq.system_to_component_index(i).first) |
| 864 |
local_transfer(i, fe_to_feq[j]) = |
| 865 |
fe.shape_value(j, points[i]); |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
// Now we construct the rectangular interpolation matrix. This |
| 870 |
// one is filled only with the information from the components |
| 871 |
// of the displacement. The rest is set to zero. |
| 872 |
local_transfer.invert(local_transfer); |
| 873 |
for (unsigned int i = 0; i < fe.n_dofs_per_cell(); ++i) |
| 874 |
if (fe_to_feq[i] != numbers::invalid_unsigned_int) |
| 875 |
for (unsigned int j = 0; j < feq.n_dofs_per_cell(); ++j) |
| 876 |
transfer(i, j) = local_transfer(fe_to_feq[i], j); |
| 877 |
|
| 878 |
// The interpolation matrix is then passed to the |
| 879 |
// VectorTools::interpolate() function to generate the correct |
| 880 |
// interpolation. |
| 881 |
interpolate(dhq, dh, transfer, eulerq, vector); |
| 882 |
} |
| 883 |
} |
| 884 |
|
| 885 |
template <int dim, int spacedim, typename VectorType> |
| 886 |
DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>) |
| 887 |
void interpolate_based_on_material_id( |
| 888 |
const Mapping<dim, spacedim> &mapping, |
| 889 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 890 |
const std::map<types::material_id, |
| 891 |
const Function<spacedim, typename VectorType::value_type> *> |
| 892 |
&functions, |
| 893 |
VectorType &vec, |
| 894 |
const ComponentMask &component_mask) |
| 895 |
{ |
| 896 |
// Create a small lambda capture wrapping the function map and call the |
| 897 |
// internal implementation |
| 898 |
const auto function_map = |
| 899 |
[&functions]( |
| 900 |
const typename DoFHandler<dim, spacedim>::active_cell_iterator &cell) |
| 901 |
-> const Function<spacedim, typename VectorType::value_type> * { |
| 902 |
const auto function = functions.find(cell->material_id()); |
| 903 |
if (function != functions.end()) |
| 904 |
return function->second; |
| 905 |
else |
| 906 |
return nullptr; |
| 907 |
}; |
| 908 |
|
| 909 |
internal::interpolate(hp::MappingCollection<dim, spacedim>(mapping), |
| 910 |
dof_handler, |
| 911 |
function_map, |
| 912 |
vec, |
| 913 |
component_mask); |
| 914 |
} |
| 915 |
|
| 916 |
namespace internal |
| 917 |
{ |
| 918 |
/** |
| 919 |
* Return whether the cell and all of its descendants are locally owned. |
| 920 |
*/ |
| 921 |
template <typename cell_iterator> |
| 922 |
bool |
| 923 |
is_locally_owned(const cell_iterator &cell) |
| 924 |
{ |
| 925 |
if (cell->is_active()) |
| 926 |
return cell->is_locally_owned(); |
| 927 |
|
| 928 |
for (unsigned int c = 0; c < cell->n_children(); ++c) |
| 929 |
if (!is_locally_owned(cell->child(c))) |
| 930 |
return false; |
| 931 |
|
| 932 |
return true; |
| 933 |
} |
| 934 |
} // namespace internal |
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
template <int dim, int spacedim, typename VectorType> |
| 939 |
DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>) |
| 940 |
void interpolate_to_different_mesh( |
| 941 |
const DoFHandler<dim, spacedim> &dof_handler_1, |
| 942 |
const VectorType &u1, |
| 943 |
const DoFHandler<dim, spacedim> &dof_handler_2, |
| 944 |
VectorType &u2) |
| 945 |
{ |
| 946 |
Assert(GridTools::have_same_coarse_mesh(dof_handler_1, dof_handler_2), |
| 947 |
ExcMessage("The two DoF handlers must represent triangulations that " |
| 948 |
"have the same coarse meshes")); |
| 949 |
|
| 950 |
InterGridMap<DoFHandler<dim, spacedim>> intergridmap; |
| 951 |
intergridmap.make_mapping(dof_handler_1, dof_handler_2); |
| 952 |
|
| 953 |
AffineConstraints<typename VectorType::value_type> dummy; |
| 954 |
dummy.close(); |
| 955 |
|
| 956 |
interpolate_to_different_mesh(intergridmap, u1, dummy, u2); |
| 957 |
} |
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
template <int dim, int spacedim, typename VectorType> |
| 962 |
DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>) |
| 963 |
void interpolate_to_different_mesh( |
| 964 |
const DoFHandler<dim, spacedim> &dof_handler_1, |
| 965 |
const VectorType &u1, |
| 966 |
const DoFHandler<dim, spacedim> &dof_handler_2, |
| 967 |
const AffineConstraints<typename VectorType::value_type> &constraints, |
| 968 |
VectorType &u2) |
| 969 |
{ |
| 970 |
Assert(GridTools::have_same_coarse_mesh(dof_handler_1, dof_handler_2), |
| 971 |
ExcMessage("The two DoF handlers must represent triangulations that " |
| 972 |
"have the same coarse meshes")); |
| 973 |
|
| 974 |
InterGridMap<DoFHandler<dim, spacedim>> intergridmap; |
| 975 |
intergridmap.make_mapping(dof_handler_1, dof_handler_2); |
| 976 |
|
| 977 |
interpolate_to_different_mesh(intergridmap, u1, constraints, u2); |
| 978 |
} |
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
template <int dim, int spacedim, typename VectorType> |
| 983 |
DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>) |
| 984 |
void interpolate_to_different_mesh( |
| 985 |
const InterGridMap<DoFHandler<dim, spacedim>> &intergridmap, |
| 986 |
const VectorType &u1, |
| 987 |
const AffineConstraints<typename VectorType::value_type> &constraints, |
| 988 |
VectorType &u2) |
| 989 |
{ |
| 990 |
const DoFHandler<dim, spacedim> &dof_handler_1 = |
| 991 |
intergridmap.get_source_grid(); |
| 992 |
const DoFHandler<dim, spacedim> &dof_handler_2 = |
| 993 |
intergridmap.get_destination_grid(); |
| 994 |
|
| 995 |
Assert( |
| 996 |
dof_handler_1.get_fe_collection() == dof_handler_2.get_fe_collection(), |
| 997 |
ExcMessage("The FECollections of both DoFHandler objects must match")); |
| 998 |
AssertDimension(u1.size(), dof_handler_1.n_dofs()); |
| 999 |
AssertDimension(u2.size(), dof_handler_2.n_dofs()); |
| 1000 |
|
| 1001 |
Vector<typename VectorType::value_type> cache; |
| 1002 |
|
| 1003 |
// Looping over the finest common |
| 1004 |
// mesh, this means that source and |
| 1005 |
// destination cells have to be on the |
| 1006 |
// same level and at least one has to |
| 1007 |
// be active. |
| 1008 |
// |
| 1009 |
// Therefore, loop over all cells |
| 1010 |
// (active and inactive) of the source |
| 1011 |
// grid .. |
| 1012 |
for (const auto &cell1 : dof_handler_1.cell_iterators()) |
| 1013 |
{ |
| 1014 |
const typename DoFHandler<dim, spacedim>::cell_iterator cell2 = |
| 1015 |
intergridmap[cell1]; |
| 1016 |
|
| 1017 |
// .. and skip if source and destination |
| 1018 |
// cells are not on the same level .. |
| 1019 |
if (cell1->level() != cell2->level()) |
| 1020 |
continue; |
| 1021 |
// .. or none of them is active. |
| 1022 |
if (!cell1->is_active() && !cell2->is_active()) |
| 1023 |
continue; |
| 1024 |
|
| 1025 |
Assert( |
| 1026 |
internal::is_locally_owned(cell1) == |
| 1027 |
internal::is_locally_owned(cell2), |
| 1028 |
ExcMessage( |
| 1029 |
"The two Triangulations are required to have the same parallel partitioning.")); |
| 1030 |
|
| 1031 |
// Skip foreign cells. |
| 1032 |
if (cell1->is_active() && !cell1->is_locally_owned()) |
| 1033 |
continue; |
| 1034 |
if (cell2->is_active() && !cell2->is_locally_owned()) |
| 1035 |
continue; |
| 1036 |
|
| 1037 |
// Get and set the corresponding |
| 1038 |
// dof_values by interpolation. |
| 1039 |
if (cell1->is_active()) |
| 1040 |
{ |
| 1041 |
cache.reinit(cell1->get_fe().n_dofs_per_cell()); |
| 1042 |
cell1->get_interpolated_dof_values(u1, |
| 1043 |
cache, |
| 1044 |
cell1->active_fe_index()); |
| 1045 |
cell2->set_dof_values_by_interpolation(cache, |
| 1046 |
u2, |
| 1047 |
cell1->active_fe_index()); |
| 1048 |
} |
| 1049 |
else |
| 1050 |
{ |
| 1051 |
cache.reinit(cell2->get_fe().n_dofs_per_cell()); |
| 1052 |
cell1->get_interpolated_dof_values(u1, |
| 1053 |
cache, |
| 1054 |
cell2->active_fe_index()); |
| 1055 |
cell2->set_dof_values_by_interpolation(cache, |
| 1056 |
u2, |
| 1057 |
cell2->active_fe_index()); |
| 1058 |
} |
| 1059 |
} |
| 1060 |
|
| 1061 |
// finish the work on parallel vectors |
| 1062 |
u2.compress(VectorOperation::insert); |
| 1063 |
// Apply hanging node constraints. |
| 1064 |
constraints.distribute(u2); |
| 1065 |
} |
| 1066 |
|
| 1067 |
|
| 1068 |
// Some helper functions. Much of the functions here is taken from a similar |
| 1069 |
// implementation in data_out_dof_data.templates.h. |
| 1070 |
namespace InterpolateBetweenMeshes |
| 1071 |
{ |
| 1072 |
template <int dim, int spacedim, typename Number> |
| 1073 |
void |
| 1074 |
create_vector(const DoFHandler<dim, spacedim> &dof_handler, |
| 1075 |
LinearAlgebra::distributed::Vector<Number> &u) |
| 1076 |
{ |
| 1077 |
const IndexSet &locally_owned_dofs = dof_handler.locally_owned_dofs(); |
| 1078 |
|
| 1079 |
const IndexSet locally_relevant_dofs = |
| 1080 |
DoFTools::extract_locally_relevant_dofs(dof_handler); |
| 1081 |
|
| 1082 |
Assert(locally_owned_dofs.is_contiguous(), |
| 1083 |
ExcMessage("You are trying to use vectors with non-contiguous " |
| 1084 |
"locally-owned index sets. This is not possible.")); |
| 1085 |
|
| 1086 |
u.reinit(locally_owned_dofs, |
| 1087 |
locally_relevant_dofs, |
| 1088 |
dof_handler.get_mpi_communicator()); |
| 1089 |
} |
| 1090 |
|
| 1091 |
|
| 1092 |
/** |
| 1093 |
* Copy the data from an arbitrary non-block vector to a |
| 1094 |
* LinearAlgebra::distributed::Vector. |
| 1095 |
*/ |
| 1096 |
template <typename VectorType, typename Number> |
| 1097 |
void |
| 1098 |
copy_locally_owned_data_from( |
| 1099 |
const VectorType &src, |
| 1100 |
LinearAlgebra::distributed::Vector<Number> &dst) |
| 1101 |
{ |
| 1102 |
if constexpr (!IsBlockVector<VectorType>::value) |
| 1103 |
{ |
| 1104 |
// If source and destination vector have the same underlying scalar, |
| 1105 |
// we can directly import elements by using only one temporary vector: |
| 1106 |
if constexpr (std::is_same_v<typename VectorType::value_type, Number>) |
| 1107 |
{ |
| 1108 |
LinearAlgebra::ReadWriteVector<typename VectorType::value_type> |
| 1109 |
temp; |
| 1110 |
temp.reinit(src.locally_owned_elements()); |
| 1111 |
temp.import_elements(src, VectorOperation::insert); |
| 1112 |
|
| 1113 |
dst.import_elements(temp, VectorOperation::insert); |
| 1114 |
} |
| 1115 |
else |
| 1116 |
// The source and destination vector have different scalar types. We |
| 1117 |
// need to split the parallel import and local copy operations into |
| 1118 |
// two phases |
| 1119 |
{ |
| 1120 |
LinearAlgebra::ReadWriteVector<typename VectorType::value_type> |
| 1121 |
temp; |
| 1122 |
temp.reinit(src.locally_owned_elements()); |
| 1123 |
temp.import_elements(src, VectorOperation::insert); |
| 1124 |
|
| 1125 |
LinearAlgebra::ReadWriteVector<Number> temp2; |
| 1126 |
temp2.reinit(temp, true); |
| 1127 |
temp2 = temp; |
| 1128 |
|
| 1129 |
dst.import_elements(temp2, VectorOperation::insert); |
| 1130 |
} |
| 1131 |
} |
| 1132 |
else |
| 1133 |
DEAL_II_NOT_IMPLEMENTED(); |
| 1134 |
} |
| 1135 |
|
| 1136 |
#ifdef DEAL_II_WITH_TRILINOS |
| 1137 |
template <typename Number> |
| 1138 |
void |
| 1139 |
copy_locally_owned_data_from( |
| 1140 |
const TrilinosWrappers::MPI::Vector &src, |
| 1141 |
LinearAlgebra::distributed::Vector<Number> &dst) |
| 1142 |
{ |
| 1143 |
// ReadWriteVector does not work for ghosted |
| 1144 |
// TrilinosWrappers::MPI::Vector objects. Fall back to copy the |
| 1145 |
// entries manually. |
| 1146 |
for (const auto i : dst.locally_owned_elements()) |
| 1147 |
dst[i] = src[i]; |
| 1148 |
} |
| 1149 |
#endif |
| 1150 |
|
| 1151 |
#ifdef DEAL_II_TRILINOS_WITH_TPETRA |
| 1152 |
template <typename Number, typename MemorySpace> |
| 1153 |
void |
| 1154 |
copy_locally_owned_data_from( |
| 1155 |
const LinearAlgebra::TpetraWrappers::Vector<Number, MemorySpace> &src, |
| 1156 |
LinearAlgebra::distributed::Vector<Number> &dst) |
| 1157 |
{ |
| 1158 |
// ReadWriteVector does not work for ghosted |
| 1159 |
// TrilinosWrappers::MPI::Vector objects. Fall back to copy the |
| 1160 |
// entries manually. |
| 1161 |
for (const auto i : dst.locally_owned_elements()) |
| 1162 |
dst[i] = src[i]; |
| 1163 |
} |
| 1164 |
#endif |
| 1165 |
} // namespace InterpolateBetweenMeshes |
| 1166 |
|
| 1167 |
|
| 1168 |
template <int dim, int spacedim, typename VectorType> |
| 1169 |
DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>) |
| 1170 |
void interpolate_to_coarser_mesh( |
| 1171 |
const DoFHandler<dim, spacedim> &dof_handler_fine, |
| 1172 |
const VectorType &u_fine, |
| 1173 |
const DoFHandler<dim, spacedim> &dof_handler_coarse, |
| 1174 |
const AffineConstraints<typename VectorType::value_type> |
| 1175 |
&constraints_coarse, |
| 1176 |
VectorType &u_coarse) |
| 1177 |
{ |
| 1178 |
Assert(GridTools::have_same_coarse_mesh(dof_handler_coarse, |
| 1179 |
dof_handler_fine), |
| 1180 |
ExcMessage("The two DoF handlers must represent triangulations that " |
| 1181 |
"have the same coarse meshes")); |
| 1182 |
AssertDimension(dof_handler_fine.n_dofs(), u_fine.size()); |
| 1183 |
AssertDimension(dof_handler_coarse.n_dofs(), u_coarse.size()); |
| 1184 |
|
| 1185 |
using LAVector = |
| 1186 |
LinearAlgebra::distributed::Vector<typename VectorType::value_type>; |
| 1187 |
|
| 1188 |
// Create and initialize a version of the coarse vector using the |
| 1189 |
// p::d::Vector type: |
| 1190 |
LAVector my_u_coarse; |
| 1191 |
InterpolateBetweenMeshes::create_vector(dof_handler_coarse, my_u_coarse); |
| 1192 |
|
| 1193 |
// Then do the same for the fine vector, and initialize it with a copy |
| 1194 |
// of the source vector |
| 1195 |
LAVector my_u_fine; |
| 1196 |
InterpolateBetweenMeshes::create_vector(dof_handler_fine, my_u_fine); |
| 1197 |
InterpolateBetweenMeshes::copy_locally_owned_data_from(u_fine, my_u_fine); |
| 1198 |
my_u_fine.update_ghost_values(); |
| 1199 |
|
| 1200 |
|
| 1201 |
// Set up transfer operator. The transfer object also wants a constraints |
| 1202 |
// object for the fine level, because it implements both the up- and |
| 1203 |
// down-transfers. But we here only ever need the down-transfer, which |
| 1204 |
// never touches the fine constraints. So we can use a dummy object for |
| 1205 |
// that. |
| 1206 |
AffineConstraints<typename VectorType::value_type> constraints_fine( |
| 1207 |
dof_handler_fine.locally_owned_dofs(), |
| 1208 |
DoFTools::extract_locally_relevant_dofs(dof_handler_fine)); |
| 1209 |
constraints_fine.close(); |
| 1210 |
|
| 1211 |
MGTwoLevelTransfer<dim, LAVector> transfer; |
| 1212 |
transfer.reinit(dof_handler_fine, |
| 1213 |
dof_handler_coarse, |
| 1214 |
constraints_fine, |
| 1215 |
constraints_coarse); |
| 1216 |
|
| 1217 |
// Then perform the interpolation from fine to coarse mesh: |
| 1218 |
transfer.interpolate(my_u_coarse, my_u_fine); |
| 1219 |
|
| 1220 |
// Finally, apply the constraints to make sure that the resulting |
| 1221 |
// object is conforming. Then copy it back into the user vector. |
| 1222 |
// (At the time of writing, LinearAlgebra::EpetraWrappers::Vector |
| 1223 |
// does not allow individual element access via operator() or |
| 1224 |
// operator[]. So disallow this vector type for the moment here.) |
| 1225 |
constraints_coarse.distribute(my_u_coarse); |
| 1226 |
if constexpr (!std::is_same_v<VectorType, |
| 1227 |
LinearAlgebra::EpetraWrappers::Vector>) |
| 1228 |
{ |
| 1229 |
for (const auto i : u_coarse.locally_owned_elements()) |
| 1230 |
u_coarse(i) = my_u_coarse(i); |
| 1231 |
} |
| 1232 |
else |
| 1233 |
DEAL_II_NOT_IMPLEMENTED(); |
| 1234 |
} |
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
template <int dim, int spacedim, typename VectorType> |
| 1239 |
DEAL_II_CXX20_REQUIRES(concepts::is_writable_dealii_vector_type<VectorType>) |
| 1240 |
void interpolate_to_finer_mesh( |
| 1241 |
const DoFHandler<dim, spacedim> &dof_handler_coarse, |
| 1242 |
const VectorType &u_coarse, |
| 1243 |
const DoFHandler<dim, spacedim> &dof_handler_fine, |
| 1244 |
const AffineConstraints<typename VectorType::value_type> &constraints_fine, |
| 1245 |
VectorType &u_fine) |
| 1246 |
{ |
| 1247 |
Assert(GridTools::have_same_coarse_mesh(dof_handler_fine, |
| 1248 |
dof_handler_coarse), |
| 1249 |
ExcMessage("The two DoF handlers must represent triangulations that " |
| 1250 |
"have the same coarse meshes")); |
| 1251 |
AssertDimension(dof_handler_fine.n_dofs(), u_fine.size()); |
| 1252 |
AssertDimension(dof_handler_coarse.n_dofs(), u_coarse.size()); |
| 1253 |
|
| 1254 |
using LAVector = |
| 1255 |
LinearAlgebra::distributed::Vector<typename VectorType::value_type>; |
| 1256 |
|
| 1257 |
// Create and initialize a version of the coarse vector using the |
| 1258 |
// p::d::Vector type. Copy the source vector |
| 1259 |
LAVector my_u_coarse; |
| 1260 |
InterpolateBetweenMeshes::create_vector(dof_handler_coarse, my_u_coarse); |
| 1261 |
InterpolateBetweenMeshes::copy_locally_owned_data_from(u_coarse, |
| 1262 |
my_u_coarse); |
| 1263 |
my_u_coarse.update_ghost_values(); |
| 1264 |
|
| 1265 |
// Then do the same for the fine vector, and initialize it with a copy |
| 1266 |
// of the source vector |
| 1267 |
LAVector my_u_fine; |
| 1268 |
InterpolateBetweenMeshes::create_vector(dof_handler_fine, my_u_fine); |
| 1269 |
|
| 1270 |
// Set up transfer operator. The transfer object also wants a constraints |
| 1271 |
// object for the coarse level, because it implements both the up- and |
| 1272 |
// down-transfers. But we here only ever need the up-transfer, which |
| 1273 |
// never touches the coarse constraints. So we can use a dummy object for |
| 1274 |
// that. |
| 1275 |
AffineConstraints<typename VectorType::value_type> constraints_coarse( |
| 1276 |
dof_handler_coarse.locally_owned_dofs(), |
| 1277 |
DoFTools::extract_locally_relevant_dofs(dof_handler_coarse)); |
| 1278 |
constraints_coarse.close(); |
| 1279 |
|
| 1280 |
MGTwoLevelTransfer<dim, LAVector> transfer; |
| 1281 |
transfer.reinit(dof_handler_fine, |
| 1282 |
dof_handler_coarse, |
| 1283 |
constraints_fine, |
| 1284 |
constraints_coarse); |
| 1285 |
|
| 1286 |
// Then perform the interpolation from coarse to fine mesh. (Note that |
| 1287 |
// we add to my_u_fine, but that vector is initially a zero vector, |
| 1288 |
// so we are really just writing into it.) |
| 1289 |
transfer.prolongate_and_add(my_u_fine, my_u_coarse); |
| 1290 |
|
| 1291 |
// Finally, apply the constraints to make sure that the resulting |
| 1292 |
// object is conforming. Then copy it back into the user vector. |
| 1293 |
// (At the time of writing, LinearAlgebra::EpetraWrappers::Vector |
| 1294 |
// does not allow individual element access via operator() or |
| 1295 |
// operator[]. So disallow this vector type for the moment here.) |
| 1296 |
constraints_fine.distribute(my_u_fine); |
| 1297 |
if constexpr (!std::is_same_v<VectorType, |
| 1298 |
LinearAlgebra::EpetraWrappers::Vector>) |
| 1299 |
{ |
| 1300 |
for (const auto i : u_fine.locally_owned_elements()) |
| 1301 |
u_fine(i) = my_u_fine(i); |
| 1302 |
} |
| 1303 |
else |
| 1304 |
DEAL_II_NOT_IMPLEMENTED(); |
| 1305 |
} |
| 1306 |
|
| 1307 |
|
| 1308 |
} // namespace VectorTools |
| 1309 |
|
| 1310 |
DEAL_II_NAMESPACE_CLOSE |
| 1311 |
|
| 1312 |
#endif // dealii_vector_tools_interpolate_templates_h |