| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2016 - 2024 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_mg_transfer_matrix_free_h |
| 16 |
#define dealii_mg_transfer_matrix_free_h |
| 17 |
|
| 18 |
#include <deal.II/base/config.h> |
| 19 |
|
| 20 |
#include <deal.II/base/mg_level_object.h> |
| 21 |
#include <deal.II/base/vectorization.h> |
| 22 |
|
| 23 |
#include <deal.II/dofs/dof_handler.h> |
| 24 |
|
| 25 |
#include <deal.II/lac/la_parallel_block_vector.h> |
| 26 |
#include <deal.II/lac/la_parallel_vector.h> |
| 27 |
|
| 28 |
#include <deal.II/multigrid/mg_base.h> |
| 29 |
#include <deal.II/multigrid/mg_constrained_dofs.h> |
| 30 |
#include <deal.II/multigrid/mg_transfer.h> |
| 31 |
#include <deal.II/multigrid/mg_transfer_internal.h> |
| 32 |
|
| 33 |
|
| 34 |
DEAL_II_NAMESPACE_OPEN |
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* @addtogroup mg |
| 39 |
* @{ |
| 40 |
*/ |
| 41 |
|
| 42 |
/** |
| 43 |
* Implementation of the MGTransferBase interface for which the transfer |
| 44 |
* operations is implemented in a matrix-free way based on the interpolation |
| 45 |
* matrices of the underlying finite element. This requires considerably less |
| 46 |
* memory than MGTransferPrebuilt and can also be considerably faster than |
| 47 |
* that variant. |
| 48 |
* |
| 49 |
* This class currently only works for tensor-product finite elements based on |
| 50 |
* FE_Q and FE_DGQ elements, including systems involving multiple components |
| 51 |
* of one of these elements. Systems with different elements or other elements |
| 52 |
* are currently not implemented. |
| 53 |
*/ |
| 54 |
template <int dim, typename Number> |
| 55 |
class MGTransferMatrixFree |
| 56 |
: public MGLevelGlobalTransfer<LinearAlgebra::distributed::Vector<Number>> |
| 57 |
{ |
| 58 |
public: |
| 59 |
/** |
| 60 |
* Constructor without constraint matrices. Use this constructor only with |
| 61 |
* discontinuous finite elements or with no local refinement. |
| 62 |
*/ |
| 63 |
MGTransferMatrixFree(); |
| 64 |
|
| 65 |
/** |
| 66 |
* Constructor with constraints. Equivalent to the default constructor |
| 67 |
* followed by initialize_constraints(). |
| 68 |
*/ |
| 69 |
MGTransferMatrixFree(const MGConstrainedDoFs &mg_constrained_dofs); |
| 70 |
|
| 71 |
/** |
| 72 |
* Destructor. |
| 73 |
*/ |
| 74 |
virtual ~MGTransferMatrixFree() override = default; |
| 75 |
|
| 76 |
/** |
| 77 |
* Initialize the constraints to be used in build(). |
| 78 |
*/ |
| 79 |
void |
| 80 |
initialize_constraints(const MGConstrainedDoFs &mg_constrained_dofs); |
| 81 |
|
| 82 |
/** |
| 83 |
* Reset the object to the state it had right after the default constructor. |
| 84 |
*/ |
| 85 |
void |
| 86 |
clear(); |
| 87 |
|
| 88 |
/** |
| 89 |
* Actually build the information for the prolongation for each level. |
| 90 |
* |
| 91 |
* The optional second argument of external partitioners allows the user to |
| 92 |
* suggest vector partitioning on the levels. In case the partitioners |
| 93 |
* are found to contain all ghost unknowns that are visited through the |
| 94 |
* transfer, the given partitioners are chosen. This ensures compatibility |
| 95 |
* of vectors during prolongate and restrict with external partitioners as |
| 96 |
* given by the user, which in turn saves some copy operations. However, in |
| 97 |
* case there are unknowns missing -- and this is typically the case at some |
| 98 |
* point during h-coarsening since processors will need to drop out and |
| 99 |
* thus children's unknowns on some processor will be needed as ghosts to a |
| 100 |
* parent cell on another processor -- the provided external partitioners are |
| 101 |
* ignored and internal variants are used instead. |
| 102 |
*/ |
| 103 |
void |
| 104 |
build(const DoFHandler<dim> &dof_handler, |
| 105 |
const std::vector<std::shared_ptr<const Utilities::MPI::Partitioner>> |
| 106 |
&external_partitioners = |
| 107 |
std::vector<std::shared_ptr<const Utilities::MPI::Partitioner>>()); |
| 108 |
|
| 109 |
/** |
| 110 |
* Same as above but taking a lambda for initializing vector instead of |
| 111 |
* partitioners. |
| 112 |
*/ |
| 113 |
void |
| 114 |
build(const DoFHandler<dim> &dof_handler, |
| 115 |
const std::function<void(const unsigned int, |
| 116 |
LinearAlgebra::distributed::Vector<Number> &)> |
| 117 |
&initialize_dof_vector); |
| 118 |
|
| 119 |
/** |
| 120 |
* Prolongate a vector from level <tt>to_level-1</tt> to level |
| 121 |
* <tt>to_level</tt> using the embedding matrices of the underlying finite |
| 122 |
* element. The previous content of <tt>dst</tt> is overwritten. |
| 123 |
* |
| 124 |
* @param to_level The index of the level to prolongate to, which is the |
| 125 |
* level of @p dst. |
| 126 |
* |
| 127 |
* @param src is a vector with as many elements as there are degrees of |
| 128 |
* freedom on the coarser level involved. |
| 129 |
* |
| 130 |
* @param dst has as many elements as there are degrees of freedom on the |
| 131 |
* finer level. |
| 132 |
*/ |
| 133 |
virtual void |
| 134 |
prolongate( |
| 135 |
const unsigned int to_level, |
| 136 |
LinearAlgebra::distributed::Vector<Number> &dst, |
| 137 |
const LinearAlgebra::distributed::Vector<Number> &src) const override; |
| 138 |
|
| 139 |
virtual void |
| 140 |
prolongate_and_add( |
| 141 |
const unsigned int to_level, |
| 142 |
LinearAlgebra::distributed::Vector<Number> &dst, |
| 143 |
const LinearAlgebra::distributed::Vector<Number> &src) const override; |
| 144 |
|
| 145 |
/** |
| 146 |
* Restrict a vector from level <tt>from_level</tt> to level |
| 147 |
* <tt>from_level-1</tt> using the transpose operation of the prolongate() |
| 148 |
* method. If the region covered by cells on level <tt>from_level</tt> is |
| 149 |
* smaller than that of level <tt>from_level-1</tt> (local refinement), then |
| 150 |
* some degrees of freedom in <tt>dst</tt> are active and will not be |
| 151 |
* altered. For the other degrees of freedom, the result of the restriction |
| 152 |
* is added. |
| 153 |
* |
| 154 |
* @param from_level The index of the level to restrict from, which is the |
| 155 |
* level of @p src. |
| 156 |
* |
| 157 |
* @param src is a vector with as many elements as there are degrees of |
| 158 |
* freedom on the finer level involved. |
| 159 |
* |
| 160 |
* @param dst has as many elements as there are degrees of freedom on the |
| 161 |
* coarser level. |
| 162 |
*/ |
| 163 |
virtual void |
| 164 |
restrict_and_add( |
| 165 |
const unsigned int from_level, |
| 166 |
LinearAlgebra::distributed::Vector<Number> &dst, |
| 167 |
const LinearAlgebra::distributed::Vector<Number> &src) const override; |
| 168 |
|
| 169 |
/** |
| 170 |
* Interpolate fine-mesh field @p src to each multigrid level in |
| 171 |
* @p dof_handler and store the result in @p dst. This function is different |
| 172 |
* from restriction, where a weighted residual is |
| 173 |
* transferred to a coarser level (transposition of prolongation matrix). |
| 174 |
* |
| 175 |
* The argument @p dst has to be initialized with the correct size according |
| 176 |
* to the number of levels of the triangulation. |
| 177 |
* |
| 178 |
* If an inner vector of @p dst is empty or has incorrect locally owned size, |
| 179 |
* it will be resized to locally relevant degrees of freedom on each level. |
| 180 |
* |
| 181 |
* The use of this function is demonstrated in step-66. |
| 182 |
*/ |
| 183 |
template <typename BlockVectorType2> |
| 184 |
void |
| 185 |
interpolate_to_mg( |
| 186 |
const DoFHandler<dim> &dof_handler, |
| 187 |
MGLevelObject<LinearAlgebra::distributed::Vector<Number>> &dst, |
| 188 |
const BlockVectorType2 &src) const; |
| 189 |
|
| 190 |
/** |
| 191 |
* Memory used by this object. |
| 192 |
*/ |
| 193 |
std::size_t |
| 194 |
memory_consumption() const; |
| 195 |
|
| 196 |
private: |
| 197 |
/** |
| 198 |
* A variable storing the degree of the finite element contained in the |
| 199 |
* DoFHandler passed to build(). The selection of the computational kernel is |
| 200 |
* based on this number. |
| 201 |
*/ |
| 202 |
unsigned int fe_degree; |
| 203 |
|
| 204 |
/** |
| 205 |
* A variable storing whether the element is continuous and there is a joint |
| 206 |
* degree of freedom in the center of the 1d line. |
| 207 |
*/ |
| 208 |
bool element_is_continuous; |
| 209 |
|
| 210 |
/** |
| 211 |
* A variable storing the number of components in the finite element contained |
| 212 |
* in the DoFHandler passed to build(). |
| 213 |
*/ |
| 214 |
unsigned int n_components; |
| 215 |
|
| 216 |
/** |
| 217 |
* A variable storing the number of degrees of freedom on all child cells. It |
| 218 |
* is <tt>2<sup>dim</sup>*fe.n_dofs_per_cell()</tt> for DG elements and |
| 219 |
* somewhat less for continuous elements. |
| 220 |
*/ |
| 221 |
unsigned int n_child_cell_dofs; |
| 222 |
|
| 223 |
/** |
| 224 |
* This variable holds the indices for cells on a given level, extracted from |
| 225 |
* DoFHandler for fast access. All DoF indices on a given level are stored as |
| 226 |
* a plain array (since this class assumes constant DoFs per cell). To index |
| 227 |
* into this array, use the cell number times dofs_per_cell. |
| 228 |
* |
| 229 |
* This array first is arranged such that all locally owned level cells come |
| 230 |
* first (found in the variable n_owned_level_cells) and then other cells |
| 231 |
* necessary for the transfer to the next level. |
| 232 |
*/ |
| 233 |
std::vector<std::vector<unsigned int>> level_dof_indices; |
| 234 |
|
| 235 |
/** |
| 236 |
* A variable storing the connectivity from parent to child cell numbers for |
| 237 |
* each level. |
| 238 |
*/ |
| 239 |
std::vector<std::vector<std::pair<unsigned int, unsigned int>>> |
| 240 |
parent_child_connect; |
| 241 |
|
| 242 |
/** |
| 243 |
* A variable storing the number of cells owned on a given process (sets the |
| 244 |
* bounds for the worker loops) for each level. |
| 245 |
*/ |
| 246 |
std::vector<unsigned int> n_owned_level_cells; |
| 247 |
|
| 248 |
/** |
| 249 |
* This variable holds the one-dimensional embedding (prolongation) matrix |
| 250 |
* from parent element to all the children. |
| 251 |
*/ |
| 252 |
AlignedVector<VectorizedArray<Number>> prolongation_matrix_1d; |
| 253 |
|
| 254 |
/** |
| 255 |
* This variable holds the temporary values for the tensor evaluation |
| 256 |
*/ |
| 257 |
mutable AlignedVector<VectorizedArray<Number>> evaluation_data; |
| 258 |
|
| 259 |
/** |
| 260 |
* For continuous elements, restriction is not additive and we need to |
| 261 |
* weight the result at the end of prolongation (and at the start of |
| 262 |
* restriction) by the valence of the degrees of freedom, i.e., on how many |
| 263 |
* elements they appear. We store the data in vectorized form to allow for |
| 264 |
* cheap access. Moreover, we utilize the fact that we only need to store |
| 265 |
* <tt>3<sup>dim</sup></tt> indices. |
| 266 |
* |
| 267 |
* Data is organized in terms of each level (outer vector) and the cells on |
| 268 |
* each level (inner vector). |
| 269 |
*/ |
| 270 |
std::vector<AlignedVector<VectorizedArray<Number>>> weights_on_refined; |
| 271 |
|
| 272 |
/** |
| 273 |
* A variable storing the local indices of Dirichlet boundary conditions on |
| 274 |
* cells for all levels (outer index), the cells within the levels (second |
| 275 |
* index), and the indices on the cell (inner index). |
| 276 |
*/ |
| 277 |
std::vector<std::vector<std::vector<unsigned short>>> dirichlet_indices; |
| 278 |
|
| 279 |
/** |
| 280 |
* A vector that holds shared pointers to the partitioners of the |
| 281 |
* transfer. These partitioners might be shared with what was passed in from |
| 282 |
* the outside through build() or be shared with the level vectors inherited |
| 283 |
* from MGLevelGlobalTransfer. |
| 284 |
*/ |
| 285 |
MGLevelObject<std::shared_ptr<const Utilities::MPI::Partitioner>> |
| 286 |
vector_partitioners; |
| 287 |
|
| 288 |
/** |
| 289 |
* Perform the prolongation operation. |
| 290 |
*/ |
| 291 |
template <int degree> |
| 292 |
void |
| 293 |
do_prolongate_add( |
| 294 |
const unsigned int to_level, |
| 295 |
LinearAlgebra::distributed::Vector<Number> &dst, |
| 296 |
const LinearAlgebra::distributed::Vector<Number> &src) const; |
| 297 |
|
| 298 |
/** |
| 299 |
* Performs the restriction operation. |
| 300 |
*/ |
| 301 |
template <int degree> |
| 302 |
void |
| 303 |
do_restrict_add(const unsigned int from_level, |
| 304 |
LinearAlgebra::distributed::Vector<Number> &dst, |
| 305 |
const LinearAlgebra::distributed::Vector<Number> &src) const; |
| 306 |
}; |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
/** |
| 311 |
* Base class of MGTransferBlockMatrixFree. While MGTransferBlockMatrixFree |
| 312 |
* contains all the setup routines of the transfer operators for the blocks, |
| 313 |
* this class simply applies them, e.g., for restricting and prolongating. |
| 314 |
*/ |
| 315 |
template <int dim, typename Number, typename TransferType> |
| 316 |
class MGTransferBlockMatrixFreeBase |
| 317 |
: public MGTransferBase<LinearAlgebra::distributed::BlockVector<Number>> |
| 318 |
{ |
| 319 |
public: |
| 320 |
MGTransferBlockMatrixFreeBase(const bool same_for_all) |
| 321 |
: same_for_all(same_for_all) |
| 322 |
{} |
| 323 |
|
| 324 |
/** |
| 325 |
* Prolongate a vector from level <tt>to_level-1</tt> to level |
| 326 |
* <tt>to_level</tt> using the embedding matrices of the underlying finite |
| 327 |
* element. The previous content of <tt>dst</tt> is overwritten. |
| 328 |
* |
| 329 |
* @param to_level The index of the level to prolongate to, which is the |
| 330 |
* level of @p dst. |
| 331 |
* |
| 332 |
* @param src is a vector with as many elements as there are degrees of |
| 333 |
* freedom on the coarser level involved. |
| 334 |
* |
| 335 |
* @param dst has as many elements as there are degrees of freedom on the |
| 336 |
* finer level. |
| 337 |
*/ |
| 338 |
virtual void |
| 339 |
prolongate( |
| 340 |
const unsigned int to_level, |
| 341 |
LinearAlgebra::distributed::BlockVector<Number> &dst, |
| 342 |
const LinearAlgebra::distributed::BlockVector<Number> &src) const override; |
| 343 |
|
| 344 |
virtual void |
| 345 |
prolongate_and_add( |
| 346 |
const unsigned int to_level, |
| 347 |
LinearAlgebra::distributed::BlockVector<Number> &dst, |
| 348 |
const LinearAlgebra::distributed::BlockVector<Number> &src) const override; |
| 349 |
|
| 350 |
/** |
| 351 |
* Restrict a vector from level <tt>from_level</tt> to level |
| 352 |
* <tt>from_level-1</tt> using the transpose operation of the prolongate() |
| 353 |
* method. If the region covered by cells on level <tt>from_level</tt> is |
| 354 |
* smaller than that of level <tt>from_level-1</tt> (local refinement), then |
| 355 |
* some degrees of freedom in <tt>dst</tt> are active and will not be |
| 356 |
* altered. For the other degrees of freedom, the result of the restriction |
| 357 |
* is added. |
| 358 |
* |
| 359 |
* @param from_level The index of the level to restrict from, which is the |
| 360 |
* level of @p src. |
| 361 |
* |
| 362 |
* @param src is a vector with as many elements as there are degrees of |
| 363 |
* freedom on the finer level involved. |
| 364 |
* |
| 365 |
* @param dst has as many elements as there are degrees of freedom on the |
| 366 |
* coarser level. |
| 367 |
*/ |
| 368 |
virtual void |
| 369 |
restrict_and_add( |
| 370 |
const unsigned int from_level, |
| 371 |
LinearAlgebra::distributed::BlockVector<Number> &dst, |
| 372 |
const LinearAlgebra::distributed::BlockVector<Number> &src) const override; |
| 373 |
|
| 374 |
/** |
| 375 |
* Transfer from a block-vector on the global grid to block-vectors defined |
| 376 |
* on each of the levels separately for active degrees of freedom. |
| 377 |
* In particular, for a globally refined mesh only the finest level in @p dst |
| 378 |
* is filled as a plain copy of @p src. All the other level objects are left |
| 379 |
* untouched. |
| 380 |
* |
| 381 |
* This function will initialize @p dst accordingly if needed as required by |
| 382 |
* the Multigrid class. |
| 383 |
*/ |
| 384 |
template <typename BlockVectorType2> |
| 385 |
void |
| 386 |
copy_to_mg( |
| 387 |
const DoFHandler<dim> &dof_handler, |
| 388 |
MGLevelObject<LinearAlgebra::distributed::BlockVector<Number>> &dst, |
| 389 |
const BlockVectorType2 &src) const; |
| 390 |
|
| 391 |
/** |
| 392 |
* Same as above for the case that each block has its own DoFHandler. |
| 393 |
*/ |
| 394 |
template <typename BlockVectorType2> |
| 395 |
void |
| 396 |
copy_to_mg( |
| 397 |
const std::vector<const DoFHandler<dim> *> &dof_handler, |
| 398 |
MGLevelObject<LinearAlgebra::distributed::BlockVector<Number>> &dst, |
| 399 |
const BlockVectorType2 &src) const; |
| 400 |
|
| 401 |
/** |
| 402 |
* Transfer from multi-level block-vector to normal vector. |
| 403 |
*/ |
| 404 |
template <typename BlockVectorType2> |
| 405 |
void |
| 406 |
copy_from_mg( |
| 407 |
const DoFHandler<dim> &dof_handler, |
| 408 |
BlockVectorType2 &dst, |
| 409 |
const MGLevelObject<LinearAlgebra::distributed::BlockVector<Number>> &src) |
| 410 |
const; |
| 411 |
|
| 412 |
/** |
| 413 |
* Same as above for the case that each block has its own DoFHandler. |
| 414 |
*/ |
| 415 |
template <typename BlockVectorType2> |
| 416 |
void |
| 417 |
copy_from_mg( |
| 418 |
const std::vector<const DoFHandler<dim> *> &dof_handler, |
| 419 |
BlockVectorType2 &dst, |
| 420 |
const MGLevelObject<LinearAlgebra::distributed::BlockVector<Number>> &src) |
| 421 |
const; |
| 422 |
|
| 423 |
/** |
| 424 |
* This class can both be used with a single DoFHandler |
| 425 |
* or a separate DoFHandler for each block. |
| 426 |
*/ |
| 427 |
static const bool supports_dof_handler_vector = true; |
| 428 |
|
| 429 |
protected: |
| 430 |
/** |
| 431 |
* Return the right non-block transfer operator. Has to be implemented by |
| 432 |
* the derived class. |
| 433 |
*/ |
| 434 |
virtual const TransferType & |
| 435 |
get_matrix_free_transfer(const unsigned int b) const = 0; |
| 436 |
|
| 437 |
/** |
| 438 |
* A flag to indicate whether the same DoFHandler is used for all |
| 439 |
* the components or if each block has its own DoFHandler. |
| 440 |
*/ |
| 441 |
const bool same_for_all; |
| 442 |
}; |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
/** |
| 447 |
* Implementation of the MGTransferBase interface for which the transfer |
| 448 |
* operations is implemented in a matrix-free way based on the interpolation |
| 449 |
* matrices of the underlying finite element. This requires considerably less |
| 450 |
* memory than MGTransferPrebuilt and can also be considerably faster than |
| 451 |
* that variant. |
| 452 |
* |
| 453 |
* This class works with LinearAlgebra::distributed::BlockVector and |
| 454 |
* performs exactly the same transfer operations for each block as |
| 455 |
* MGTransferMatrixFree. |
| 456 |
* Both the cases that the same DoFHandler is used for all the blocks |
| 457 |
* and that each block uses its own DoFHandler are supported. |
| 458 |
*/ |
| 459 |
template <int dim, typename Number> |
| 460 |
class MGTransferBlockMatrixFree |
| 461 |
: public MGTransferBlockMatrixFreeBase<dim, |
| 462 |
Number, |
| 463 |
MGTransferMatrixFree<dim, Number>> |
| 464 |
{ |
| 465 |
public: |
| 466 |
/** |
| 467 |
* Constructor without constraint matrices. Use this constructor only with |
| 468 |
* discontinuous finite elements or with no local refinement. |
| 469 |
*/ |
| 470 |
MGTransferBlockMatrixFree() = default; |
| 471 |
|
| 472 |
/** |
| 473 |
* Constructor with constraints. Equivalent to the default constructor |
| 474 |
* followed by initialize_constraints(). |
| 475 |
*/ |
| 476 |
MGTransferBlockMatrixFree(const MGConstrainedDoFs &mg_constrained_dofs); |
| 477 |
|
| 478 |
/** |
| 479 |
* Same as above for the case that each block has its own DoFHandler. |
| 480 |
*/ |
| 481 |
MGTransferBlockMatrixFree( |
| 482 |
const std::vector<MGConstrainedDoFs> &mg_constrained_dofs); |
| 483 |
|
| 484 |
/** |
| 485 |
* Destructor. |
| 486 |
*/ |
| 487 |
virtual ~MGTransferBlockMatrixFree() override = default; |
| 488 |
|
| 489 |
/** |
| 490 |
* Initialize the constraints to be used in build(). |
| 491 |
*/ |
| 492 |
void |
| 493 |
initialize_constraints(const MGConstrainedDoFs &mg_constrained_dofs); |
| 494 |
|
| 495 |
/** |
| 496 |
* Same as above for the case that each block has its own DoFHandler. |
| 497 |
*/ |
| 498 |
void |
| 499 |
initialize_constraints( |
| 500 |
const std::vector<MGConstrainedDoFs> &mg_constrained_dofs); |
| 501 |
|
| 502 |
/** |
| 503 |
* Reset the object to the state it had right after the default constructor. |
| 504 |
*/ |
| 505 |
void |
| 506 |
clear(); |
| 507 |
|
| 508 |
/** |
| 509 |
* Actually build the information for the prolongation for each level. |
| 510 |
*/ |
| 511 |
void |
| 512 |
build(const DoFHandler<dim> &dof_handler); |
| 513 |
|
| 514 |
/** |
| 515 |
* Same as above for the case that each block has its own DoFHandler. |
| 516 |
*/ |
| 517 |
void |
| 518 |
build(const std::vector<const DoFHandler<dim> *> &dof_handler); |
| 519 |
|
| 520 |
/** |
| 521 |
* Memory used by this object. |
| 522 |
*/ |
| 523 |
std::size_t |
| 524 |
memory_consumption() const; |
| 525 |
|
| 526 |
protected: |
| 527 |
const MGTransferMatrixFree<dim, Number> & |
| 528 |
get_matrix_free_transfer(const unsigned int b) const override; |
| 529 |
|
| 530 |
private: |
| 531 |
/** |
| 532 |
* Non-block matrix-free versions of transfer operation. |
| 533 |
*/ |
| 534 |
std::vector<MGTransferMatrixFree<dim, Number>> matrix_free_transfer_vector; |
| 535 |
}; |
| 536 |
|
| 537 |
|
| 538 |
/** @} */ |
| 539 |
|
| 540 |
|
| 541 |
//------------------------ templated functions ------------------------- |
| 542 |
#ifndef DOXYGEN |
| 543 |
|
| 544 |
|
| 545 |
template <int dim, typename Number> |
| 546 |
template <typename BlockVectorType2> |
| 547 |
void |
| 548 |
MGTransferMatrixFree<dim, Number>::interpolate_to_mg( |
| 549 |
const DoFHandler<dim> &dof_handler, |
| 550 |
MGLevelObject<LinearAlgebra::distributed::Vector<Number>> &dst, |
| 551 |
const BlockVectorType2 &src) const |
| 552 |
{ |
| 553 |
const unsigned int min_level = dst.min_level(); |
| 554 |
const unsigned int max_level = dst.max_level(); |
| 555 |
|
| 556 |
Assert(max_level == dof_handler.get_triangulation().n_global_levels() - 1, |
| 557 |
ExcDimensionMismatch( |
| 558 |
max_level, dof_handler.get_triangulation().n_global_levels() - 1)); |
| 559 |
|
| 560 |
const auto &fe = dof_handler.get_fe(); |
| 561 |
|
| 562 |
for (unsigned int level = min_level; level <= max_level; ++level) |
| 563 |
if (dst[level].size() != dof_handler.n_dofs(level) || |
| 564 |
dst[level].locally_owned_size() != |
| 565 |
dof_handler.locally_owned_mg_dofs(level).n_elements()) |
| 566 |
dst[level].reinit(this->vector_partitioners[level]); |
| 567 |
|
| 568 |
// copy fine level vector to active cells in MG hierarchy |
| 569 |
this->copy_to_mg(dof_handler, dst, src, true); |
| 570 |
|
| 571 |
// FIXME: maybe need to store hanging nodes constraints per level? |
| 572 |
// MGConstrainedDoFs does NOT keep this info right now, only periodicity |
| 573 |
// constraints... |
| 574 |
|
| 575 |
// do the transfer from level to level-1: |
| 576 |
dst[max_level].update_ghost_values(); |
| 577 |
|
| 578 |
for (unsigned int level = max_level; level > min_level; --level) |
| 579 |
{ |
| 580 |
// auxiliary vector which always has ghost elements |
| 581 |
const LinearAlgebra::distributed::Vector<Number> *input = nullptr; |
| 582 |
LinearAlgebra::distributed::Vector<Number> ghosted_fine; |
| 583 |
if (dst[level].get_partitioner().get() == |
| 584 |
this->vector_partitioners[level].get()) |
| 585 |
input = &dst[level]; |
| 586 |
else |
| 587 |
{ |
| 588 |
ghosted_fine.reinit(this->vector_partitioners[level]); |
| 589 |
ghosted_fine.copy_locally_owned_data_from(dst[level]); |
| 590 |
ghosted_fine.update_ghost_values(); |
| 591 |
input = &ghosted_fine; |
| 592 |
} |
| 593 |
|
| 594 |
std::vector<Number> dof_values_coarse(fe.n_dofs_per_cell()); |
| 595 |
Vector<Number> dof_values_fine(fe.n_dofs_per_cell()); |
| 596 |
Vector<Number> tmp(fe.n_dofs_per_cell()); |
| 597 |
std::vector<types::global_dof_index> dof_indices(fe.n_dofs_per_cell()); |
| 598 |
for (const auto &cell : dof_handler.cell_iterators_on_level(level - 1)) |
| 599 |
if (cell->is_locally_owned_on_level()) |
| 600 |
{ |
| 601 |
// if we get to a cell without children (== active), we can |
| 602 |
// skip it as there values should be already set by the |
| 603 |
// equivalent of copy_to_mg() |
| 604 |
if (cell->is_active()) |
| 605 |
continue; |
| 606 |
|
| 607 |
std::fill(dof_values_coarse.begin(), dof_values_coarse.end(), 0.); |
| 608 |
for (unsigned int child = 0; child < cell->n_children(); ++child) |
| 609 |
{ |
| 610 |
cell->child(child)->get_mg_dof_indices(dof_indices); |
| 611 |
|
| 612 |
internal::MGTransfer::resolve_identity_constraints( |
| 613 |
this->mg_constrained_dofs, level, dof_indices); |
| 614 |
|
| 615 |
for (unsigned int i = 0; i < fe.n_dofs_per_cell(); ++i) |
| 616 |
dof_values_fine(i) = (*input)(dof_indices[i]); |
| 617 |
fe.get_restriction_matrix(child, cell->refinement_case()) |
| 618 |
.vmult(tmp, dof_values_fine); |
| 619 |
for (unsigned int i = 0; i < fe.n_dofs_per_cell(); ++i) |
| 620 |
if (fe.restriction_is_additive(i)) |
| 621 |
dof_values_coarse[i] += tmp[i]; |
| 622 |
else if (tmp(i) != 0.) |
| 623 |
dof_values_coarse[i] = tmp[i]; |
| 624 |
} |
| 625 |
cell->get_mg_dof_indices(dof_indices); |
| 626 |
for (unsigned int i = 0; i < fe.n_dofs_per_cell(); ++i) |
| 627 |
if (dof_handler.locally_owned_mg_dofs(level - 1).is_element( |
| 628 |
dof_indices[i])) |
| 629 |
dst[level - 1](dof_indices[i]) = dof_values_coarse[i]; |
| 630 |
} |
| 631 |
|
| 632 |
dst[level - 1].update_ghost_values(); |
| 633 |
} |
| 634 |
|
| 635 |
for (unsigned int level = min_level; level <= max_level; ++level) |
| 636 |
dst[level].zero_out_ghost_values(); |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
template <int dim, typename Number, typename TransferType> |
| 642 |
template <typename BlockVectorType2> |
| 643 |
void |
| 644 |
MGTransferBlockMatrixFreeBase<dim, Number, TransferType>::copy_to_mg( |
| 645 |
const DoFHandler<dim> &dof_handler, |
| 646 |
MGLevelObject<LinearAlgebra::distributed::BlockVector<Number>> &dst, |
| 647 |
const BlockVectorType2 &src) const |
| 648 |
{ |
| 649 |
Assert(same_for_all, |
| 650 |
ExcMessage( |
| 651 |
"This object was initialized with support for usage with one " |
| 652 |
"DoFHandler for each block, but this method assumes that " |
| 653 |
"the same DoFHandler is used for all the blocks!")); |
| 654 |
const std::vector<const DoFHandler<dim> *> mg_dofs(src.n_blocks(), |
| 655 |
&dof_handler); |
| 656 |
|
| 657 |
copy_to_mg(mg_dofs, dst, src); |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
template <int dim, typename Number, typename TransferType> |
| 663 |
template <typename BlockVectorType2> |
| 664 |
void |
| 665 |
MGTransferBlockMatrixFreeBase<dim, Number, TransferType>::copy_to_mg( |
| 666 |
const std::vector<const DoFHandler<dim> *> &dof_handler, |
| 667 |
MGLevelObject<LinearAlgebra::distributed::BlockVector<Number>> &dst, |
| 668 |
const BlockVectorType2 &src) const |
| 669 |
{ |
| 670 |
const unsigned int n_blocks = src.n_blocks(); |
| 671 |
AssertDimension(dof_handler.size(), n_blocks); |
| 672 |
|
| 673 |
if (n_blocks == 0) |
| 674 |
return; |
| 675 |
|
| 676 |
const unsigned int min_level = dst.min_level(); |
| 677 |
const unsigned int max_level = dst.max_level(); |
| 678 |
|
| 679 |
for (unsigned int level = min_level; level <= max_level; ++level) |
| 680 |
if (dst[level].n_blocks() != n_blocks) |
| 681 |
dst[level].reinit(n_blocks); |
| 682 |
|
| 683 |
// FIXME: this a quite ugly as we need a temporary object: |
| 684 |
MGLevelObject<LinearAlgebra::distributed::Vector<Number>> dst_non_block( |
| 685 |
min_level, max_level); |
| 686 |
|
| 687 |
for (unsigned int b = 0; b < n_blocks; ++b) |
| 688 |
{ |
| 689 |
const unsigned int data_block = same_for_all ? 0 : b; |
| 690 |
get_matrix_free_transfer(data_block) |
| 691 |
.copy_to_mg(*dof_handler[b], dst_non_block, src.block(b)); |
| 692 |
|
| 693 |
for (unsigned int l = min_level; l <= max_level; ++l) |
| 694 |
dst[l].block(b) = dst_non_block[l]; |
| 695 |
} |
| 696 |
|
| 697 |
for (unsigned int level = min_level; level <= max_level; ++level) |
| 698 |
dst[level].collect_sizes(); |
| 699 |
} |
| 700 |
|
| 701 |
template <int dim, typename Number, typename TransferType> |
| 702 |
template <typename BlockVectorType2> |
| 703 |
void |
| 704 |
MGTransferBlockMatrixFreeBase<dim, Number, TransferType>::copy_from_mg( |
| 705 |
const DoFHandler<dim> &dof_handler, |
| 706 |
BlockVectorType2 &dst, |
| 707 |
const MGLevelObject<LinearAlgebra::distributed::BlockVector<Number>> &src) |
| 708 |
const |
| 709 |
{ |
| 710 |
Assert(same_for_all, |
| 711 |
ExcMessage( |
| 712 |
"This object was initialized with support for usage with one " |
| 713 |
"DoFHandler for each block, but this method assumes that " |
| 714 |
"the same DoFHandler is used for all the blocks!")); |
| 715 |
const std::vector<const DoFHandler<dim> *> mg_dofs(dst.n_blocks(), |
| 716 |
&dof_handler); |
| 717 |
|
| 718 |
copy_from_mg(mg_dofs, dst, src); |
| 719 |
} |
| 720 |
|
| 721 |
template <int dim, typename Number, typename TransferType> |
| 722 |
template <typename BlockVectorType2> |
| 723 |
void |
| 724 |
MGTransferBlockMatrixFreeBase<dim, Number, TransferType>::copy_from_mg( |
| 725 |
const std::vector<const DoFHandler<dim> *> &dof_handler, |
| 726 |
BlockVectorType2 &dst, |
| 727 |
const MGLevelObject<LinearAlgebra::distributed::BlockVector<Number>> &src) |
| 728 |
const |
| 729 |
{ |
| 730 |
const unsigned int n_blocks = dst.n_blocks(); |
| 731 |
AssertDimension(dof_handler.size(), n_blocks); |
| 732 |
|
| 733 |
if (n_blocks == 0) |
| 734 |
return; |
| 735 |
|
| 736 |
const unsigned int min_level = src.min_level(); |
| 737 |
const unsigned int max_level = src.max_level(); |
| 738 |
|
| 739 |
for (unsigned int l = min_level; l <= max_level; ++l) |
| 740 |
AssertDimension(src[l].n_blocks(), dst.n_blocks()); |
| 741 |
|
| 742 |
// FIXME: this a quite ugly as we need a temporary object: |
| 743 |
MGLevelObject<LinearAlgebra::distributed::Vector<Number>> src_non_block( |
| 744 |
min_level, max_level); |
| 745 |
|
| 746 |
for (unsigned int b = 0; b < n_blocks; ++b) |
| 747 |
{ |
| 748 |
for (unsigned int l = min_level; l <= max_level; ++l) |
| 749 |
{ |
| 750 |
src_non_block[l].reinit(src[l].block(b)); |
| 751 |
src_non_block[l] = src[l].block(b); |
| 752 |
} |
| 753 |
const unsigned int data_block = same_for_all ? 0 : b; |
| 754 |
get_matrix_free_transfer(data_block) |
| 755 |
.copy_from_mg(*dof_handler[b], dst.block(b), src_non_block); |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
#endif // DOXYGEN |
| 762 |
|
| 763 |
|
| 764 |
DEAL_II_NAMESPACE_CLOSE |
| 765 |
|
| 766 |
#endif |