| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2001 - 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_h |
| 16 |
#define dealii_mg_transfer_h |
| 17 |
|
| 18 |
#include <deal.II/base/config.h> |
| 19 |
|
| 20 |
#include <deal.II/base/mg_level_object.h> |
| 21 |
|
| 22 |
#include <deal.II/distributed/tria_base.h> |
| 23 |
|
| 24 |
#include <deal.II/dofs/dof_handler.h> |
| 25 |
|
| 26 |
#include <deal.II/lac/affine_constraints.h> |
| 27 |
#include <deal.II/lac/block_sparsity_pattern.h> |
| 28 |
#include <deal.II/lac/block_vector.h> |
| 29 |
#include <deal.II/lac/la_parallel_vector.h> |
| 30 |
#include <deal.II/lac/petsc_sparse_matrix.h> |
| 31 |
#include <deal.II/lac/petsc_vector.h> |
| 32 |
#include <deal.II/lac/sparse_matrix.h> |
| 33 |
#include <deal.II/lac/trilinos_sparse_matrix.h> |
| 34 |
#include <deal.II/lac/vector_memory.h> |
| 35 |
|
| 36 |
#include <deal.II/multigrid/mg_base.h> |
| 37 |
#include <deal.II/multigrid/mg_constrained_dofs.h> |
| 38 |
|
| 39 |
#include <memory> |
| 40 |
|
| 41 |
|
| 42 |
DEAL_II_NAMESPACE_OPEN |
| 43 |
|
| 44 |
|
| 45 |
namespace internal |
| 46 |
{ |
| 47 |
template <typename VectorType> |
| 48 |
struct MatrixSelector |
| 49 |
{ |
| 50 |
using Sparsity = ::dealii::SparsityPattern; |
| 51 |
using Matrix = ::dealii::SparseMatrix<typename VectorType::value_type>; |
| 52 |
|
| 53 |
static const bool requires_distributed_sparsity_pattern = false; |
| 54 |
|
| 55 |
template <typename SparsityPatternType, int dim, int spacedim> |
| 56 |
static void |
| 57 |
reinit(Matrix &matrix, |
| 58 |
Sparsity &sparsity, |
| 59 |
int level, |
| 60 |
const SparsityPatternType &sp, |
| 61 |
const DoFHandler<dim, spacedim> &) |
| 62 |
{ |
| 63 |
sparsity.copy_from(sp); |
| 64 |
(void)level; |
| 65 |
matrix.reinit(sparsity); |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
#ifdef DEAL_II_WITH_TRILINOS |
| 70 |
template <typename Number> |
| 71 |
struct MatrixSelector<LinearAlgebra::distributed::Vector<Number>> |
| 72 |
{ |
| 73 |
using Sparsity = ::dealii::TrilinosWrappers::SparsityPattern; |
| 74 |
using Matrix = ::dealii::TrilinosWrappers::SparseMatrix; |
| 75 |
|
| 76 |
static const bool requires_distributed_sparsity_pattern = false; |
| 77 |
|
| 78 |
template <typename SparsityPatternType, int dim, int spacedim> |
| 79 |
static void |
| 80 |
reinit(Matrix &matrix, |
| 81 |
Sparsity &, |
| 82 |
int level, |
| 83 |
const SparsityPatternType &sp, |
| 84 |
const DoFHandler<dim, spacedim> &dh) |
| 85 |
{ |
| 86 |
const MPI_Comm communicator = dh.get_mpi_communicator(); |
| 87 |
|
| 88 |
matrix.reinit(dh.locally_owned_mg_dofs(level + 1), |
| 89 |
dh.locally_owned_mg_dofs(level), |
| 90 |
sp, |
| 91 |
communicator, |
| 92 |
true); |
| 93 |
} |
| 94 |
}; |
| 95 |
|
| 96 |
template <> |
| 97 |
struct MatrixSelector<dealii::TrilinosWrappers::MPI::Vector> |
| 98 |
{ |
| 99 |
using Sparsity = ::dealii::TrilinosWrappers::SparsityPattern; |
| 100 |
using Matrix = ::dealii::TrilinosWrappers::SparseMatrix; |
| 101 |
|
| 102 |
static const bool requires_distributed_sparsity_pattern = false; |
| 103 |
|
| 104 |
template <typename SparsityPatternType, int dim, int spacedim> |
| 105 |
static void |
| 106 |
reinit(Matrix &matrix, |
| 107 |
Sparsity &, |
| 108 |
int level, |
| 109 |
const SparsityPatternType &sp, |
| 110 |
const DoFHandler<dim, spacedim> &dh) |
| 111 |
{ |
| 112 |
const MPI_Comm communicator = dh.get_mpi_communicator(); |
| 113 |
|
| 114 |
matrix.reinit(dh.locally_owned_mg_dofs(level + 1), |
| 115 |
dh.locally_owned_mg_dofs(level), |
| 116 |
sp, |
| 117 |
communicator, |
| 118 |
true); |
| 119 |
} |
| 120 |
}; |
| 121 |
|
| 122 |
# ifdef DEAL_II_WITH_MPI |
| 123 |
# ifdef DEAL_II_TRILINOS_WITH_TPETRA |
| 124 |
template <typename Number, typename MemorySpace> |
| 125 |
struct MatrixSelector< |
| 126 |
dealii::LinearAlgebra::TpetraWrappers::Vector<Number, MemorySpace>> |
| 127 |
{ |
| 128 |
using Sparsity = ::dealii::TrilinosWrappers::SparsityPattern; |
| 129 |
using Matrix = ::dealii::TrilinosWrappers::SparseMatrix; |
| 130 |
|
| 131 |
static const bool requires_distributed_sparsity_pattern = false; |
| 132 |
|
| 133 |
template <typename SparsityPatternType, int dim, int spacedim> |
| 134 |
static void |
| 135 |
reinit(Matrix &matrix, |
| 136 |
Sparsity &, |
| 137 |
int level, |
| 138 |
const SparsityPatternType &sp, |
| 139 |
const DoFHandler<dim, spacedim> &dh) |
| 140 |
{ |
| 141 |
const MPI_Comm communicator = dh.get_mpi_communicator(); |
| 142 |
|
| 143 |
matrix.reinit(dh.locally_owned_mg_dofs(level + 1), |
| 144 |
dh.locally_owned_mg_dofs(level), |
| 145 |
sp, |
| 146 |
communicator, |
| 147 |
true); |
| 148 |
} |
| 149 |
}; |
| 150 |
# endif |
| 151 |
|
| 152 |
template <> |
| 153 |
struct MatrixSelector<dealii::LinearAlgebra::EpetraWrappers::Vector> |
| 154 |
{ |
| 155 |
using Sparsity = ::dealii::TrilinosWrappers::SparsityPattern; |
| 156 |
using Matrix = ::dealii::TrilinosWrappers::SparseMatrix; |
| 157 |
|
| 158 |
static const bool requires_distributed_sparsity_pattern = false; |
| 159 |
|
| 160 |
template <typename SparsityPatternType, int dim, int spacedim> |
| 161 |
static void |
| 162 |
reinit(Matrix &matrix, |
| 163 |
Sparsity &, |
| 164 |
int level, |
| 165 |
const SparsityPatternType &sp, |
| 166 |
const DoFHandler<dim, spacedim> &dh) |
| 167 |
{ |
| 168 |
const MPI_Comm communicator = dh.get_mpi_communicator(); |
| 169 |
|
| 170 |
matrix.reinit(dh.locally_owned_mg_dofs(level + 1), |
| 171 |
dh.locally_owned_mg_dofs(level), |
| 172 |
sp, |
| 173 |
communicator, |
| 174 |
true); |
| 175 |
} |
| 176 |
}; |
| 177 |
# endif |
| 178 |
|
| 179 |
#else |
| 180 |
// ! DEAL_II_WITH_TRILINOS |
| 181 |
template <typename Number> |
| 182 |
struct MatrixSelector<LinearAlgebra::distributed::Vector<Number>> |
| 183 |
{ |
| 184 |
using Sparsity = ::dealii::SparsityPattern; |
| 185 |
using Matrix = ::dealii::SparseMatrix<Number>; |
| 186 |
|
| 187 |
static const bool requires_distributed_sparsity_pattern = false; |
| 188 |
|
| 189 |
template <typename SparsityPatternType, int dim, int spacedim> |
| 190 |
static void |
| 191 |
reinit(Matrix &, |
| 192 |
Sparsity &, |
| 193 |
int, |
| 194 |
const SparsityPatternType &, |
| 195 |
const DoFHandler<dim, spacedim> &) |
| 196 |
{ |
| 197 |
AssertThrow( |
| 198 |
false, |
| 199 |
ExcNotImplemented( |
| 200 |
"ERROR: MGTransferPrebuilt with LinearAlgebra::distributed::Vector currently " |
| 201 |
"needs deal.II to be configured with Trilinos.")); |
| 202 |
} |
| 203 |
}; |
| 204 |
|
| 205 |
#endif |
| 206 |
|
| 207 |
#ifdef DEAL_II_WITH_PETSC |
| 208 |
template <> |
| 209 |
struct MatrixSelector<dealii::PETScWrappers::MPI::Vector> |
| 210 |
{ |
| 211 |
using Sparsity = ::dealii::DynamicSparsityPattern; |
| 212 |
using Matrix = ::dealii::PETScWrappers::MPI::SparseMatrix; |
| 213 |
|
| 214 |
static const bool requires_distributed_sparsity_pattern = true; |
| 215 |
|
| 216 |
template <typename SparsityPatternType, int dim, int spacedim> |
| 217 |
static void |
| 218 |
reinit(Matrix &matrix, |
| 219 |
Sparsity &, |
| 220 |
int level, |
| 221 |
const SparsityPatternType &sp, |
| 222 |
const DoFHandler<dim, spacedim> &dh) |
| 223 |
{ |
| 224 |
const MPI_Comm communicator = dh.get_mpi_communicator(); |
| 225 |
|
| 226 |
// Reinit PETSc matrix |
| 227 |
matrix.reinit(dh.locally_owned_mg_dofs(level + 1), |
| 228 |
dh.locally_owned_mg_dofs(level), |
| 229 |
sp, |
| 230 |
communicator); |
| 231 |
} |
| 232 |
}; |
| 233 |
#endif |
| 234 |
} // namespace internal |
| 235 |
|
| 236 |
/* |
| 237 |
* MGTransferBase is defined in mg_base.h |
| 238 |
*/ |
| 239 |
|
| 240 |
/** |
| 241 |
* @addtogroup mg |
| 242 |
* @{ |
| 243 |
*/ |
| 244 |
|
| 245 |
/** |
| 246 |
* Implementation of transfer between the global vectors and the multigrid |
| 247 |
* levels for use in the derived class MGTransferPrebuilt and other classes. |
| 248 |
*/ |
| 249 |
template <typename VectorType> |
| 250 |
class MGLevelGlobalTransfer : public MGTransferBase<VectorType> |
| 251 |
{ |
| 252 |
public: |
| 253 |
/** |
| 254 |
* Reset the object to the state it had right after the default constructor. |
| 255 |
*/ |
| 256 |
void |
| 257 |
clear(); |
| 258 |
|
| 259 |
/** |
| 260 |
* Transfer from a vector on the global grid to vectors defined on each of |
| 261 |
* the levels separately for the active degrees of freedom. In particular, |
| 262 |
* for a globally refined mesh only the finest level in @p dst is filled as a |
| 263 |
* plain copy of @p src. All the other level objects are left untouched. |
| 264 |
*/ |
| 265 |
template <int dim, class InVector, int spacedim> |
| 266 |
void |
| 267 |
copy_to_mg(const DoFHandler<dim, spacedim> &dof_handler, |
| 268 |
MGLevelObject<VectorType> &dst, |
| 269 |
const InVector &src) const; |
| 270 |
|
| 271 |
/** |
| 272 |
* Transfer from multi-level vector to normal vector. |
| 273 |
* |
| 274 |
* Copies data from active portions of an MGVector into the respective |
| 275 |
* positions of a <tt>Vector<number></tt>. In order to keep the result |
| 276 |
* consistent, constrained degrees of freedom are set to zero. |
| 277 |
*/ |
| 278 |
template <int dim, class OutVector, int spacedim> |
| 279 |
void |
| 280 |
copy_from_mg(const DoFHandler<dim, spacedim> &dof_handler, |
| 281 |
OutVector &dst, |
| 282 |
const MGLevelObject<VectorType> &src) const; |
| 283 |
|
| 284 |
/** |
| 285 |
* Add a multi-level vector to a normal vector. |
| 286 |
* |
| 287 |
* Works as the previous function, but probably not for continuous elements. |
| 288 |
*/ |
| 289 |
template <int dim, class OutVector, int spacedim> |
| 290 |
void |
| 291 |
copy_from_mg_add(const DoFHandler<dim, spacedim> &dof_handler, |
| 292 |
OutVector &dst, |
| 293 |
const MGLevelObject<VectorType> &src) const; |
| 294 |
|
| 295 |
/** |
| 296 |
* If this object operates on BlockVector objects, we need to describe how |
| 297 |
* the individual vector components are mapped to the blocks of a vector. |
| 298 |
* For example, for a Stokes system, we have dim+1 vector components for |
| 299 |
* velocity and pressure, but we may want to use block vectors with only two |
| 300 |
* blocks for all velocities in one block, and the pressure variables in the |
| 301 |
* other. |
| 302 |
* |
| 303 |
* By default, if this function is not called, block vectors have as many |
| 304 |
* blocks as the finite element has vector components. However, this can be |
| 305 |
* changed by calling this function with an array that describes how vector |
| 306 |
* components are to be grouped into blocks. The meaning of the argument is |
| 307 |
* the same as the one given to the DoFTools::count_dofs_per_component |
| 308 |
* function. |
| 309 |
*/ |
| 310 |
void |
| 311 |
set_component_to_block_map(const std::vector<unsigned int> &map); |
| 312 |
|
| 313 |
/** |
| 314 |
* Memory used by this object. |
| 315 |
*/ |
| 316 |
std::size_t |
| 317 |
memory_consumption() const; |
| 318 |
|
| 319 |
/** |
| 320 |
* Print the copy index fields for debugging purposes. |
| 321 |
*/ |
| 322 |
void |
| 323 |
print_indices(std::ostream &os) const; |
| 324 |
|
| 325 |
protected: |
| 326 |
/** |
| 327 |
* Internal function to @p fill copy_indices*. Called by derived classes. |
| 328 |
*/ |
| 329 |
template <int dim, int spacedim> |
| 330 |
void |
| 331 |
fill_and_communicate_copy_indices( |
| 332 |
const DoFHandler<dim, spacedim> &dof_handler); |
| 333 |
|
| 334 |
/** |
| 335 |
* Sizes of the multi-level vectors. |
| 336 |
*/ |
| 337 |
std::vector<types::global_dof_index> sizes; |
| 338 |
|
| 339 |
/** |
| 340 |
* Mapping for the copy_to_mg() and copy_from_mg() functions. Here only |
| 341 |
* index pairs locally owned |
| 342 |
* |
| 343 |
* The data is organized as follows: one vector per level. Each element of |
| 344 |
* these vectors contains first the global index, then the level index. |
| 345 |
*/ |
| 346 |
std::vector< |
| 347 |
std::vector<std::pair<types::global_dof_index, types::global_dof_index>>> |
| 348 |
copy_indices; |
| 349 |
|
| 350 |
/** |
| 351 |
* Additional degrees of freedom for the copy_to_mg() function. These are |
| 352 |
* the ones where the global degree of freedom is locally owned and the |
| 353 |
* level degree of freedom is not. |
| 354 |
* |
| 355 |
* Organization of the data is like for @p copy_indices_mine. |
| 356 |
*/ |
| 357 |
std::vector< |
| 358 |
std::vector<std::pair<types::global_dof_index, types::global_dof_index>>> |
| 359 |
copy_indices_global_mine; |
| 360 |
|
| 361 |
/** |
| 362 |
* Additional degrees of freedom for the copy_from_mg() function. These are |
| 363 |
* the ones where the level degree of freedom is locally owned and the |
| 364 |
* global degree of freedom is not. |
| 365 |
* |
| 366 |
* Organization of the data is like for @p copy_indices_mine. |
| 367 |
*/ |
| 368 |
std::vector< |
| 369 |
std::vector<std::pair<types::global_dof_index, types::global_dof_index>>> |
| 370 |
copy_indices_level_mine; |
| 371 |
|
| 372 |
/** |
| 373 |
* This variable stores whether the copy operation from the global to the |
| 374 |
* level vector is actually a plain copy to the finest level. This means that |
| 375 |
* the grid has no adaptive refinement and the numbering on the finest |
| 376 |
* multigrid level is the same as in the global case. |
| 377 |
*/ |
| 378 |
bool perform_plain_copy; |
| 379 |
|
| 380 |
/** |
| 381 |
* The vector that stores what has been given to the |
| 382 |
* set_component_to_block_map() function. |
| 383 |
*/ |
| 384 |
std::vector<unsigned int> component_to_block_map; |
| 385 |
|
| 386 |
/** |
| 387 |
* The mg_constrained_dofs of the level systems. |
| 388 |
*/ |
| 389 |
ObserverPointer<const MGConstrainedDoFs> mg_constrained_dofs; |
| 390 |
|
| 391 |
private: |
| 392 |
/** |
| 393 |
* This function is called to make sure that build() has been invoked. |
| 394 |
*/ |
| 395 |
template <int dim, int spacedim> |
| 396 |
void |
| 397 |
assert_built(const DoFHandler<dim, spacedim> &dof_handler) const; |
| 398 |
}; |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
/** |
| 403 |
* Implementation of transfer between the global vectors and the multigrid |
| 404 |
* levels for use in the derived class MGTransferPrebuilt and other classes. |
| 405 |
* This class is a specialization for the case of |
| 406 |
* LinearAlgebra::distributed::Vector that requires a few different calling |
| 407 |
* routines as compared to the %parallel vectors in the PETScWrappers and |
| 408 |
* TrilinosWrappers namespaces. |
| 409 |
*/ |
| 410 |
template <typename Number, typename MemorySpace> |
| 411 |
class MGLevelGlobalTransfer< |
| 412 |
LinearAlgebra::distributed::Vector<Number, MemorySpace>> |
| 413 |
: public MGTransferBase< |
| 414 |
LinearAlgebra::distributed::Vector<Number, MemorySpace>> |
| 415 |
{ |
| 416 |
public: |
| 417 |
/** |
| 418 |
* Reset the object to the state it had right after the default constructor. |
| 419 |
*/ |
| 420 |
void |
| 421 |
clear(); |
| 422 |
|
| 423 |
/** |
| 424 |
* Transfer from a vector on the global grid to vectors defined on each of |
| 425 |
* the levels separately for the active degrees of freedom. In particular, for |
| 426 |
* a globally refined mesh only the finest level in @p dst is filled as a |
| 427 |
* plain copy of @p src. All the other level objects are left untouched. |
| 428 |
*/ |
| 429 |
template <int dim, typename Number2, int spacedim> |
| 430 |
void |
| 431 |
copy_to_mg( |
| 432 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 433 |
MGLevelObject<LinearAlgebra::distributed::Vector<Number, MemorySpace>> &dst, |
| 434 |
const LinearAlgebra::distributed::Vector<Number2, MemorySpace> &src) const; |
| 435 |
|
| 436 |
/** |
| 437 |
* Transfer from multi-level vector to normal vector. |
| 438 |
* |
| 439 |
* Copies data from active portions of an MGVector into the respective |
| 440 |
* positions of a <tt>Vector<number></tt>. In order to keep the result |
| 441 |
* consistent, constrained degrees of freedom are set to zero. |
| 442 |
*/ |
| 443 |
template <int dim, typename Number2, int spacedim> |
| 444 |
void |
| 445 |
copy_from_mg( |
| 446 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 447 |
LinearAlgebra::distributed::Vector<Number2, MemorySpace> &dst, |
| 448 |
const MGLevelObject<LinearAlgebra::distributed::Vector<Number, MemorySpace>> |
| 449 |
&src) const; |
| 450 |
|
| 451 |
/** |
| 452 |
* Add a multi-level vector to a normal vector. |
| 453 |
* |
| 454 |
* Works as the previous function, but probably not for continuous elements. |
| 455 |
*/ |
| 456 |
template <int dim, typename Number2, int spacedim> |
| 457 |
void |
| 458 |
copy_from_mg_add( |
| 459 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 460 |
LinearAlgebra::distributed::Vector<Number2, MemorySpace> &dst, |
| 461 |
const MGLevelObject<LinearAlgebra::distributed::Vector<Number, MemorySpace>> |
| 462 |
&src) const; |
| 463 |
|
| 464 |
/** |
| 465 |
* If this object operates on BlockVector objects, we need to describe how |
| 466 |
* the individual vector components are mapped to the blocks of a vector. |
| 467 |
* For example, for a Stokes system, we have dim+1 vector components for |
| 468 |
* velocity and pressure, but we may want to use block vectors with only two |
| 469 |
* blocks for all velocities in one block, and the pressure variables in the |
| 470 |
* other. |
| 471 |
* |
| 472 |
* By default, if this function is not called, block vectors have as many |
| 473 |
* blocks as the finite element has vector components. However, this can be |
| 474 |
* changed by calling this function with an array that describes how vector |
| 475 |
* components are to be grouped into blocks. The meaning of the argument is |
| 476 |
* the same as the one given to the DoFTools::count_dofs_per_component |
| 477 |
* function. |
| 478 |
*/ |
| 479 |
void |
| 480 |
set_component_to_block_map(const std::vector<unsigned int> &map); |
| 481 |
|
| 482 |
/** |
| 483 |
* Memory used by this object. |
| 484 |
*/ |
| 485 |
std::size_t |
| 486 |
memory_consumption() const; |
| 487 |
|
| 488 |
/** |
| 489 |
* Print the copy index fields for debugging purposes. |
| 490 |
*/ |
| 491 |
void |
| 492 |
print_indices(std::ostream &os) const; |
| 493 |
|
| 494 |
protected: |
| 495 |
/** |
| 496 |
* Internal function to perform transfer of residuals or solutions |
| 497 |
* basesd on the flag @p solution_transfer. |
| 498 |
*/ |
| 499 |
template <int dim, typename Number2, int spacedim> |
| 500 |
void |
| 501 |
copy_to_mg( |
| 502 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 503 |
MGLevelObject<LinearAlgebra::distributed::Vector<Number, MemorySpace>> &dst, |
| 504 |
const LinearAlgebra::distributed::Vector<Number2, MemorySpace> &src, |
| 505 |
const bool solution_transfer) const; |
| 506 |
|
| 507 |
/** |
| 508 |
* Internal function to @p fill copy_indices*. Called by derived classes. |
| 509 |
*/ |
| 510 |
template <int dim, int spacedim> |
| 511 |
void |
| 512 |
fill_and_communicate_copy_indices( |
| 513 |
const DoFHandler<dim, spacedim> &dof_handler); |
| 514 |
|
| 515 |
/** |
| 516 |
* Sizes of the multi-level vectors. |
| 517 |
*/ |
| 518 |
std::vector<types::global_dof_index> sizes; |
| 519 |
|
| 520 |
/** |
| 521 |
* Mapping for the copy_to_mg() and copy_from_mg() functions. Here only |
| 522 |
* index pairs locally owned is stored. |
| 523 |
* |
| 524 |
* The data is organized as follows: one table per level. This table has two |
| 525 |
* rows. The first row contains the global index, the second one the level |
| 526 |
* index. |
| 527 |
*/ |
| 528 |
std::vector<Table<2, unsigned int>> copy_indices; |
| 529 |
|
| 530 |
/** |
| 531 |
* Same as above, but used to transfer solution vectors. |
| 532 |
*/ |
| 533 |
std::vector<Table<2, unsigned int>> solution_copy_indices; |
| 534 |
|
| 535 |
/** |
| 536 |
* Additional degrees of freedom for the copy_to_mg() function. These are |
| 537 |
* the ones where the global degree of freedom is locally owned and the |
| 538 |
* level degree of freedom is not. |
| 539 |
* |
| 540 |
* Organization of the data is like for @p copy_indices. |
| 541 |
*/ |
| 542 |
std::vector<Table<2, unsigned int>> copy_indices_global_mine; |
| 543 |
|
| 544 |
/** |
| 545 |
* Additional degrees of freedom for the copy_from_mg() function. These are |
| 546 |
* the ones where the level degree of freedom is locally owned and the |
| 547 |
* global degree of freedom is not. |
| 548 |
* |
| 549 |
* Organization of the data is like for @p copy_indices. |
| 550 |
*/ |
| 551 |
std::vector<Table<2, unsigned int>> copy_indices_level_mine; |
| 552 |
|
| 553 |
/** |
| 554 |
* Same as above, but used to transfer solution vectors. |
| 555 |
*/ |
| 556 |
std::vector<Table<2, unsigned int>> solution_copy_indices_level_mine; |
| 557 |
|
| 558 |
/** |
| 559 |
* This variable stores whether the copy operation from the global to the |
| 560 |
* level vector is actually a plain copy to the finest level. This means that |
| 561 |
* the grid has no adaptive refinement and the numbering on the finest |
| 562 |
* multigrid level is the same as in the global case. |
| 563 |
*/ |
| 564 |
bool perform_plain_copy; |
| 565 |
|
| 566 |
/** |
| 567 |
* This variable stores whether the copy operation from the global to the |
| 568 |
* level vector is actually a plain copy to the finest level except for a |
| 569 |
* renumbering within the finest level of the degrees of freedom. This means |
| 570 |
* that the grid has no adaptive refinement. |
| 571 |
*/ |
| 572 |
bool perform_renumbered_plain_copy; |
| 573 |
|
| 574 |
/** |
| 575 |
* The vector that stores what has been given to the |
| 576 |
* set_component_to_block_map() function. |
| 577 |
*/ |
| 578 |
std::vector<unsigned int> component_to_block_map; |
| 579 |
|
| 580 |
/** |
| 581 |
* The mg_constrained_dofs of the level systems. |
| 582 |
*/ |
| 583 |
ObserverPointer<const MGConstrainedDoFs> mg_constrained_dofs; |
| 584 |
|
| 585 |
/** |
| 586 |
* In the function copy_to_mg, we need to access ghosted entries of the |
| 587 |
* global vector for inserting into the level vectors. This vector is |
| 588 |
* populated with those entries. |
| 589 |
*/ |
| 590 |
mutable LinearAlgebra::distributed::Vector<Number, MemorySpace> |
| 591 |
ghosted_global_vector; |
| 592 |
|
| 593 |
/** |
| 594 |
* Same as above but used when working with solution vectors. |
| 595 |
*/ |
| 596 |
mutable LinearAlgebra::distributed::Vector<Number, MemorySpace> |
| 597 |
solution_ghosted_global_vector; |
| 598 |
|
| 599 |
/** |
| 600 |
* In the function copy_from_mg, we access all level vectors with certain |
| 601 |
* ghost entries for inserting the result into a global vector. |
| 602 |
*/ |
| 603 |
mutable MGLevelObject<LinearAlgebra::distributed::Vector<Number, MemorySpace>> |
| 604 |
ghosted_level_vector; |
| 605 |
|
| 606 |
/** |
| 607 |
* Function to initialize internal level vectors. |
| 608 |
*/ |
| 609 |
std::function<void(const unsigned int, |
| 610 |
LinearAlgebra::distributed::Vector<Number, MemorySpace> &)> |
| 611 |
initialize_dof_vector; |
| 612 |
|
| 613 |
private: |
| 614 |
/** |
| 615 |
* This function is called to make sure that build() has been invoked. |
| 616 |
*/ |
| 617 |
template <int dim, int spacedim> |
| 618 |
void |
| 619 |
assert_built(const DoFHandler<dim, spacedim> &dof_handler) const; |
| 620 |
}; |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
/** |
| 625 |
* Implementation of the MGTransferBase interface for which the transfer |
| 626 |
* operations are prebuilt upon construction of the object of this class as |
| 627 |
* matrices. This is the fast way, since it only needs to build the operation |
| 628 |
* once by looping over all cells and storing the result in a matrix for each |
| 629 |
* level, but requires additional memory. |
| 630 |
* |
| 631 |
* See MGTransferBase to find out which of the transfer classes is best for |
| 632 |
* your needs. |
| 633 |
*/ |
| 634 |
template <typename VectorType> |
| 635 |
class MGTransferPrebuilt : public MGLevelGlobalTransfer<VectorType> |
| 636 |
{ |
| 637 |
public: |
| 638 |
/** |
| 639 |
* Constructor without constraint matrices. Use this constructor only with |
| 640 |
* discontinuous finite elements or with no local refinement. |
| 641 |
*/ |
| 642 |
MGTransferPrebuilt() = default; |
| 643 |
|
| 644 |
/** |
| 645 |
* Constructor with constraints. Equivalent to the default constructor |
| 646 |
* followed by initialize_constraints(). |
| 647 |
*/ |
| 648 |
MGTransferPrebuilt(const MGConstrainedDoFs &mg_constrained_dofs); |
| 649 |
|
| 650 |
/** |
| 651 |
* Destructor. |
| 652 |
*/ |
| 653 |
virtual ~MGTransferPrebuilt() override = default; |
| 654 |
|
| 655 |
/** |
| 656 |
* Initialize the constraints to be used in build(). |
| 657 |
*/ |
| 658 |
void |
| 659 |
initialize_constraints(const MGConstrainedDoFs &mg_constrained_dofs); |
| 660 |
|
| 661 |
/** |
| 662 |
* Reset the object to the state it had right after the default constructor. |
| 663 |
*/ |
| 664 |
void |
| 665 |
clear(); |
| 666 |
|
| 667 |
/** |
| 668 |
* Actually build the information required for the transfer operations. Needs |
| 669 |
* to be called before prolongate() or restrict_and_add() can be used. |
| 670 |
*/ |
| 671 |
template <int dim, int spacedim> |
| 672 |
void |
| 673 |
build(const DoFHandler<dim, spacedim> &dof_handler); |
| 674 |
|
| 675 |
/** |
| 676 |
* Prolongate a vector from level <tt>to_level-1</tt> to level |
| 677 |
* <tt>to_level</tt> using the embedding matrices of the underlying finite |
| 678 |
* element. The previous content of <tt>dst</tt> is overwritten. |
| 679 |
* |
| 680 |
* @arg src is a vector with as many elements as there are degrees of |
| 681 |
* freedom on the coarser level involved. |
| 682 |
* |
| 683 |
* @arg dst has as many elements as there are degrees of freedom on the |
| 684 |
* finer level. |
| 685 |
*/ |
| 686 |
virtual void |
| 687 |
prolongate(const unsigned int to_level, |
| 688 |
VectorType &dst, |
| 689 |
const VectorType &src) const override; |
| 690 |
|
| 691 |
/** |
| 692 |
* Restrict a vector from level <tt>from_level</tt> to level |
| 693 |
* <tt>from_level-1</tt> using the transpose operation of the @p prolongate |
| 694 |
* method. If the region covered by cells on level <tt>from_level</tt> is |
| 695 |
* smaller than that of level <tt>from_level-1</tt> (local refinement), then |
| 696 |
* some degrees of freedom in <tt>dst</tt> are active and will not be |
| 697 |
* altered. For the other degrees of freedom, the result of the restriction |
| 698 |
* is added. |
| 699 |
* |
| 700 |
* @arg src is a vector with as many elements as there are degrees of |
| 701 |
* freedom on the finer level involved. |
| 702 |
* |
| 703 |
* @arg dst has as many elements as there are degrees of freedom on the |
| 704 |
* coarser level. |
| 705 |
*/ |
| 706 |
virtual void |
| 707 |
restrict_and_add(const unsigned int from_level, |
| 708 |
VectorType &dst, |
| 709 |
const VectorType &src) const override; |
| 710 |
|
| 711 |
/** |
| 712 |
* Finite element does not provide prolongation matrices. |
| 713 |
*/ |
| 714 |
DeclException0(ExcNoProlongation); |
| 715 |
|
| 716 |
/** |
| 717 |
* You have to call build() before using this object. |
| 718 |
*/ |
| 719 |
DeclException0(ExcMatricesNotBuilt); |
| 720 |
|
| 721 |
/** |
| 722 |
* Memory used by this object. |
| 723 |
*/ |
| 724 |
std::size_t |
| 725 |
memory_consumption() const; |
| 726 |
|
| 727 |
/** |
| 728 |
* Print all the matrices for debugging purposes. |
| 729 |
*/ |
| 730 |
void |
| 731 |
print_matrices(std::ostream &os) const; |
| 732 |
|
| 733 |
private: |
| 734 |
/** |
| 735 |
* Sparsity patterns for transfer matrices. |
| 736 |
*/ |
| 737 |
std::vector< |
| 738 |
std::shared_ptr<typename internal::MatrixSelector<VectorType>::Sparsity>> |
| 739 |
prolongation_sparsities; |
| 740 |
|
| 741 |
/** |
| 742 |
* The actual prolongation matrix. column indices belong to the dof indices |
| 743 |
* of the parent cell, i.e. the coarse level. while row indices belong to |
| 744 |
* the child cell, i.e. the fine level. |
| 745 |
*/ |
| 746 |
std::vector< |
| 747 |
std::shared_ptr<typename internal::MatrixSelector<VectorType>::Matrix>> |
| 748 |
prolongation_matrices; |
| 749 |
|
| 750 |
/** |
| 751 |
* Degrees of freedom on the refinement edge excluding those on the |
| 752 |
* boundary. |
| 753 |
*/ |
| 754 |
std::vector<std::vector<bool>> interface_dofs; |
| 755 |
}; |
| 756 |
|
| 757 |
|
| 758 |
/** @} */ |
| 759 |
|
| 760 |
|
| 761 |
DEAL_II_NAMESPACE_CLOSE |
| 762 |
|
| 763 |
#endif |