| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2003 - 2025 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
|
| 16 |
#ifndef dealii_mg_transfer_templates_h |
| 17 |
#define dealii_mg_transfer_templates_h |
| 18 |
|
| 19 |
#include <deal.II/base/config.h> |
| 20 |
|
| 21 |
#include <deal.II/distributed/tria.h> |
| 22 |
|
| 23 |
#include <deal.II/dofs/dof_accessor.h> |
| 24 |
|
| 25 |
#include <deal.II/fe/fe.h> |
| 26 |
|
| 27 |
#include <deal.II/grid/tria_iterator.h> |
| 28 |
|
| 29 |
#include <deal.II/lac/petsc_vector.h> |
| 30 |
#include <deal.II/lac/sparse_matrix.h> |
| 31 |
#include <deal.II/lac/trilinos_epetra_vector.h> |
| 32 |
#include <deal.II/lac/trilinos_vector.h> |
| 33 |
|
| 34 |
#include <deal.II/multigrid/mg_base.h> |
| 35 |
#include <deal.II/multigrid/mg_tools.h> |
| 36 |
#include <deal.II/multigrid/mg_transfer.h> |
| 37 |
|
| 38 |
#include <algorithm> |
| 39 |
|
| 40 |
// Here you can turn on some cout statements and MPI Barriers for debugging: |
| 41 |
// #define DEBUG_OUTPUT |
| 42 |
|
| 43 |
DEAL_II_NAMESPACE_OPEN |
| 44 |
|
| 45 |
|
| 46 |
namespace internal |
| 47 |
{ |
| 48 |
namespace MGTransfer |
| 49 |
{ |
| 50 |
/** |
| 51 |
* Adjust vectors on all levels to correct size. Here, we just count the |
| 52 |
* numbers of degrees of freedom on each level and @p reinit each level |
| 53 |
* vector to this length. For compatibility reasons with the next function |
| 54 |
* the target_component is added here but is not used. |
| 55 |
*/ |
| 56 |
template <int dim, typename number, int spacedim> |
| 57 |
void |
| 58 |
reinit_vector(const DoFHandler<dim, spacedim> &dof_handler, |
| 59 |
const std::vector<unsigned int> &, |
| 60 |
MGLevelObject<dealii::Vector<number>> &v) |
| 61 |
{ |
| 62 |
for (unsigned int level = v.min_level(); level <= v.max_level(); ++level) |
| 63 |
{ |
| 64 |
unsigned int n = dof_handler.n_dofs(level); |
| 65 |
v[level].reinit(n); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Adjust vectors on all levels to correct size. Here, we just count the |
| 71 |
* numbers of degrees of freedom on each level and @p reinit each level |
| 72 |
* vector to this length. The target_component is handed to |
| 73 |
* MGTools::count_dofs_per_block. See for documentation there. |
| 74 |
*/ |
| 75 |
template <int dim, typename number, int spacedim> |
| 76 |
void |
| 77 |
reinit_vector(const DoFHandler<dim, spacedim> &dof_handler, |
| 78 |
std::vector<unsigned int> target_component, |
| 79 |
MGLevelObject<BlockVector<number>> &v) |
| 80 |
{ |
| 81 |
const unsigned int n_blocks = dof_handler.get_fe().n_blocks(); |
| 82 |
if (target_component.empty()) |
| 83 |
{ |
| 84 |
target_component.resize(n_blocks); |
| 85 |
for (unsigned int i = 0; i < n_blocks; ++i) |
| 86 |
target_component[i] = i; |
| 87 |
} |
| 88 |
Assert(target_component.size() == n_blocks, |
| 89 |
ExcDimensionMismatch(target_component.size(), n_blocks)); |
| 90 |
const unsigned int max_block = |
| 91 |
*std::max_element(target_component.begin(), target_component.end()); |
| 92 |
const unsigned int n_target_blocks = max_block + 1; |
| 93 |
|
| 94 |
std::vector<std::vector<types::global_dof_index>> ndofs( |
| 95 |
dof_handler.get_triangulation().n_levels(), |
| 96 |
std::vector<types::global_dof_index>(n_target_blocks)); |
| 97 |
MGTools::count_dofs_per_block(dof_handler, ndofs, target_component); |
| 98 |
|
| 99 |
for (unsigned int level = v.min_level(); level <= v.max_level(); ++level) |
| 100 |
{ |
| 101 |
v[level].reinit(n_target_blocks); |
| 102 |
for (unsigned int b = 0; b < n_target_blocks; ++b) |
| 103 |
v[level].block(b).reinit(ndofs[level][b]); |
| 104 |
v[level].collect_sizes(); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
#ifdef DEAL_II_WITH_TRILINOS |
| 110 |
/** |
| 111 |
* Adjust vectors on all levels to correct size. Here, we just count the |
| 112 |
* numbers of degrees of freedom on each level and @p reinit each level |
| 113 |
* vector to this length. |
| 114 |
*/ |
| 115 |
template <int dim, int spacedim> |
| 116 |
void |
| 117 |
reinit_vector(const DoFHandler<dim, spacedim> &dof_handler, |
| 118 |
const std::vector<unsigned int> &, |
| 119 |
MGLevelObject<TrilinosWrappers::MPI::Vector> &v) |
| 120 |
{ |
| 121 |
const dealii::parallel::TriangulationBase<dim, spacedim> *tria = |
| 122 |
(dynamic_cast<const dealii::parallel::TriangulationBase<dim, spacedim> |
| 123 |
*>(&dof_handler.get_triangulation())); |
| 124 |
AssertThrow( |
| 125 |
tria != nullptr, |
| 126 |
ExcMessage( |
| 127 |
"multigrid with Trilinos vectors only works with a parallel Triangulation!")); |
| 128 |
|
| 129 |
for (unsigned int level = v.min_level(); level <= v.max_level(); ++level) |
| 130 |
{ |
| 131 |
v[level].reinit(dof_handler.locally_owned_mg_dofs(level), |
| 132 |
tria->get_mpi_communicator()); |
| 133 |
} |
| 134 |
} |
| 135 |
#endif |
| 136 |
|
| 137 |
#ifdef DEAL_II_WITH_PETSC |
| 138 |
/** |
| 139 |
* Adjust vectors on all levels to correct size. Here, we just count the |
| 140 |
* numbers of degrees of freedom on each level and @p reinit each level |
| 141 |
* vector to this length. |
| 142 |
*/ |
| 143 |
template <int dim, int spacedim> |
| 144 |
void |
| 145 |
reinit_vector(const DoFHandler<dim, spacedim> &dof_handler, |
| 146 |
const std::vector<unsigned int> &, |
| 147 |
MGLevelObject<PETScWrappers::MPI::Vector> &v) |
| 148 |
{ |
| 149 |
const dealii::parallel::TriangulationBase<dim, spacedim> *tria = |
| 150 |
(dynamic_cast<const dealii::parallel::TriangulationBase<dim, spacedim> |
| 151 |
*>(&dof_handler.get_triangulation())); |
| 152 |
AssertThrow( |
| 153 |
tria != nullptr, |
| 154 |
ExcMessage( |
| 155 |
"multigrid with parallel PETSc vectors only works with a parallel Triangulation!")); |
| 156 |
|
| 157 |
for (unsigned int level = v.min_level(); level <= v.max_level(); ++level) |
| 158 |
{ |
| 159 |
v[level].reinit(dof_handler.locally_owned_mg_dofs(level), |
| 160 |
tria->get_mpi_communicator()); |
| 161 |
} |
| 162 |
} |
| 163 |
#endif |
| 164 |
} // namespace MGTransfer |
| 165 |
} // namespace internal |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
/* ------------------ MGLevelGlobalTransfer<VectorType> ----------------- */ |
| 170 |
|
| 171 |
|
| 172 |
namespace internal |
| 173 |
{ |
| 174 |
// generic copy function of two different vectors -> need to access each |
| 175 |
// individual entry |
| 176 |
template <typename T, typename V> |
| 177 |
void |
| 178 |
copy_vector(const std::vector< |
| 179 |
std::pair<types::global_dof_index, types::global_dof_index>> |
| 180 |
©_indices, |
| 181 |
const T &src, |
| 182 |
V &dst) |
| 183 |
{ |
| 184 |
// we should have copy_index.second == copy_index.first, therefore we can |
| 185 |
// use the same function for both copying to mg as well as copying from mg |
| 186 |
for (const auto ©_index : copy_indices) |
| 187 |
dst(copy_index.first) = src(copy_index.first); |
| 188 |
dst.compress(VectorOperation::insert); |
| 189 |
} |
| 190 |
|
| 191 |
// specialized copy function for the same vector |
| 192 |
template <typename T> |
| 193 |
void |
| 194 |
copy_vector(const std::vector< |
| 195 |
std::pair<types::global_dof_index, types::global_dof_index>> &, |
| 196 |
const T &src, |
| 197 |
T &dst) |
| 198 |
{ |
| 199 |
dst = src; |
| 200 |
} |
| 201 |
} // namespace internal |
| 202 |
|
| 203 |
|
| 204 |
template <typename VectorType> |
| 205 |
template <int dim, class InVector, int spacedim> |
| 206 |
void |
| 207 |
MGLevelGlobalTransfer<VectorType>::copy_to_mg( |
| 208 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 209 |
MGLevelObject<VectorType> &dst, |
| 210 |
const InVector &src) const |
| 211 |
{ |
| 212 |
assert_built(dof_handler); |
| 213 |
AssertIndexRange(dst.max_level(), |
| 214 |
dof_handler.get_triangulation().n_global_levels()); |
| 215 |
AssertIndexRange(dst.min_level(), dst.max_level() + 1); |
| 216 |
internal::MGTransfer::reinit_vector(dof_handler, component_to_block_map, dst); |
| 217 |
#ifdef DEBUG_OUTPUT |
| 218 |
std::cout << "copy_to_mg src " << src.l2_norm() << std::endl; |
| 219 |
int ierr = MPI_Barrier(dof_handler.get_mpi_communicator()); |
| 220 |
AssertThrowMPI(ierr); |
| 221 |
#endif |
| 222 |
|
| 223 |
if (perform_plain_copy) |
| 224 |
{ |
| 225 |
// if the finest multigrid level covers the whole domain (i.e., no |
| 226 |
// adaptive refinement) and the numbering of the finest level DoFs and |
| 227 |
// the global DoFs are the same, we can do a plain copy |
| 228 |
AssertDimension(dst[dst.max_level()].size(), src.size()); |
| 229 |
internal::copy_vector(copy_indices[dst.max_level()], |
| 230 |
src, |
| 231 |
dst[dst.max_level()]); |
| 232 |
return; |
| 233 |
} |
| 234 |
|
| 235 |
for (unsigned int level = dst.max_level() + 1; level != dst.min_level();) |
| 236 |
{ |
| 237 |
--level; |
| 238 |
#ifdef DEBUG_OUTPUT |
| 239 |
ierr = MPI_Barrier(dof_handler.get_mpi_communicator()); |
| 240 |
AssertThrowMPI(ierr); |
| 241 |
#endif |
| 242 |
|
| 243 |
VectorType &dst_level = dst[level]; |
| 244 |
|
| 245 |
// first copy local unknowns |
| 246 |
for (const auto ©_index : copy_indices[level]) |
| 247 |
dst_level(copy_index.second) = src(copy_index.first); |
| 248 |
|
| 249 |
// Do the same for the indices where the global index is local, but the |
| 250 |
// local index is not |
| 251 |
for (const auto ©_index : copy_indices_global_mine[level]) |
| 252 |
dst_level(copy_index.second) = src(copy_index.first); |
| 253 |
|
| 254 |
dst_level.compress(VectorOperation::insert); |
| 255 |
|
| 256 |
#ifdef DEBUG_OUTPUT |
| 257 |
ierr = MPI_Barrier(dof_handler.get_mpi_communicator()); |
| 258 |
AssertThrowMPI(ierr); |
| 259 |
std::cout << "copy_to_mg dst " << level << ' ' << dst_level.l2_norm() |
| 260 |
<< std::endl; |
| 261 |
#endif |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
template <typename VectorType> |
| 268 |
template <int dim, class OutVector, int spacedim> |
| 269 |
void |
| 270 |
MGLevelGlobalTransfer<VectorType>::copy_from_mg( |
| 271 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 272 |
OutVector &dst, |
| 273 |
const MGLevelObject<VectorType> &src) const |
| 274 |
{ |
| 275 |
assert_built(dof_handler); |
| 276 |
AssertIndexRange(src.max_level(), |
| 277 |
dof_handler.get_triangulation().n_global_levels()); |
| 278 |
AssertIndexRange(src.min_level(), src.max_level() + 1); |
| 279 |
if (perform_plain_copy) |
| 280 |
{ |
| 281 |
AssertDimension(dst.size(), src[src.max_level()].size()); |
| 282 |
internal::copy_vector(copy_indices[src.max_level()], |
| 283 |
src[src.max_level()], |
| 284 |
dst); |
| 285 |
return; |
| 286 |
} |
| 287 |
|
| 288 |
// For non-DG: degrees of freedom in the refinement face may need special |
| 289 |
// attention, since they belong to the coarse level, but have fine level |
| 290 |
// basis functions |
| 291 |
dst = 0; |
| 292 |
for (unsigned int level = src.min_level(); level <= src.max_level(); ++level) |
| 293 |
{ |
| 294 |
#ifdef DEBUG_OUTPUT |
| 295 |
int ierr = MPI_Barrier(dof_handler.get_mpi_communicator()); |
| 296 |
AssertThrowMPI(ierr); |
| 297 |
std::cout << "copy_from_mg src " << level << ' ' << src[level].l2_norm() |
| 298 |
<< std::endl; |
| 299 |
ierr = MPI_Barrier(dof_handler.get_mpi_communicator()); |
| 300 |
AssertThrowMPI(ierr); |
| 301 |
#endif |
| 302 |
|
| 303 |
const VectorType &src_level = src[level]; |
| 304 |
|
| 305 |
// First copy all indices local to this process |
| 306 |
for (const auto ©_index : copy_indices[level]) |
| 307 |
dst(copy_index.first) = src_level(copy_index.second); |
| 308 |
|
| 309 |
// Do the same for the indices where the level index is local, but the |
| 310 |
// global index is not |
| 311 |
for (const auto ©_index : copy_indices_level_mine[level]) |
| 312 |
dst(copy_index.first) = src_level(copy_index.second); |
| 313 |
|
| 314 |
#ifdef DEBUG_OUTPUT |
| 315 |
{ |
| 316 |
dst.compress(VectorOperation::insert); |
| 317 |
ierr = MPI_Barrier(dof_handler.get_mpi_communicator()); |
| 318 |
AssertThrowMPI(ierr); |
| 319 |
std::cout << "copy_from_mg level=" << level << ' ' << dst.l2_norm() |
| 320 |
<< std::endl; |
| 321 |
} |
| 322 |
#endif |
| 323 |
} |
| 324 |
dst.compress(VectorOperation::insert); |
| 325 |
#ifdef DEBUG_OUTPUT |
| 326 |
const int ierr = MPI_Barrier(dof_handler.get_mpi_communicator()); |
| 327 |
AssertThrowMPI(ierr); |
| 328 |
std::cout << "copy_from_mg " << dst.l2_norm() << std::endl; |
| 329 |
#endif |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
template <typename VectorType> |
| 335 |
template <int dim, class OutVector, int spacedim> |
| 336 |
void |
| 337 |
MGLevelGlobalTransfer<VectorType>::copy_from_mg_add( |
| 338 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 339 |
OutVector &dst, |
| 340 |
const MGLevelObject<VectorType> &src) const |
| 341 |
{ |
| 342 |
assert_built(dof_handler); |
| 343 |
// For non-DG: degrees of freedom in the refinement face may need special |
| 344 |
// attention, since they belong to the coarse level, but have fine level |
| 345 |
// basis functions |
| 346 |
for (unsigned int level = src.min_level(); level <= src.max_level(); ++level) |
| 347 |
{ |
| 348 |
const VectorType &src_level = src[level]; |
| 349 |
|
| 350 |
// First add all indices local to this process |
| 351 |
for (const auto ©_index : copy_indices[level]) |
| 352 |
dst(copy_index.first) += src_level(copy_index.second); |
| 353 |
|
| 354 |
// Do the same for the indices where the level index is local, but the |
| 355 |
// global index is not |
| 356 |
for (const auto ©_index : copy_indices_level_mine[level]) |
| 357 |
dst(copy_index.first) += src_level(copy_index.second); |
| 358 |
} |
| 359 |
dst.compress(VectorOperation::add); |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
template <typename VectorType> |
| 365 |
void |
| 366 |
MGLevelGlobalTransfer<VectorType>::set_component_to_block_map( |
| 367 |
const std::vector<unsigned int> &map) |
| 368 |
{ |
| 369 |
component_to_block_map = map; |
| 370 |
} |
| 371 |
|
| 372 |
template <typename VectorType> |
| 373 |
template <int dim, int spacedim> |
| 374 |
void |
| 375 |
MGLevelGlobalTransfer<VectorType>::assert_built( |
| 376 |
const DoFHandler<dim, spacedim> &dof_handler) const |
| 377 |
{ |
| 378 |
(void)dof_handler; |
| 379 |
Assert(copy_indices.size() == |
| 380 |
dof_handler.get_triangulation().n_global_levels(), |
| 381 |
ExcMessage("MGTransfer::build() has not been called!")); |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
/* --------- MGLevelGlobalTransfer<LinearAlgebra::distributed::Vector> ------- |
| 386 |
*/ |
| 387 |
|
| 388 |
template <typename Number, typename MemorySpace> |
| 389 |
template <int dim, typename Number2, int spacedim> |
| 390 |
void |
| 391 |
MGLevelGlobalTransfer<LinearAlgebra::distributed::Vector<Number, MemorySpace>>:: |
| 392 |
copy_to_mg( |
| 393 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 394 |
MGLevelObject<LinearAlgebra::distributed::Vector<Number, MemorySpace>> &dst, |
| 395 |
const LinearAlgebra::distributed::Vector<Number2, MemorySpace> &src) const |
| 396 |
{ |
| 397 |
assert_built(dof_handler); |
| 398 |
copy_to_mg(dof_handler, dst, src, false); |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
template <typename Number, typename MemorySpace> |
| 403 |
template <int dim, typename Number2, int spacedim> |
| 404 |
void |
| 405 |
MGLevelGlobalTransfer<LinearAlgebra::distributed::Vector<Number, MemorySpace>>:: |
| 406 |
copy_to_mg( |
| 407 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 408 |
MGLevelObject<LinearAlgebra::distributed::Vector<Number, MemorySpace>> &dst, |
| 409 |
const LinearAlgebra::distributed::Vector<Number2, MemorySpace> &src, |
| 410 |
const bool solution_transfer) const |
| 411 |
{ |
| 412 |
assert_built(dof_handler); |
| 413 |
LinearAlgebra::distributed::Vector<Number> &this_ghosted_global_vector = |
| 414 |
solution_transfer ? solution_ghosted_global_vector : ghosted_global_vector; |
| 415 |
const std::vector<Table<2, unsigned int>> &this_copy_indices = |
| 416 |
solution_transfer ? solution_copy_indices : copy_indices; |
| 417 |
const std::vector<Table<2, unsigned int>> &this_copy_indices_level_mine = |
| 418 |
solution_transfer ? solution_copy_indices_level_mine : |
| 419 |
copy_indices_level_mine; |
| 420 |
|
| 421 |
AssertIndexRange(dst.max_level(), |
| 422 |
dof_handler.get_triangulation().n_global_levels()); |
| 423 |
AssertIndexRange(dst.min_level(), dst.max_level() + 1); |
| 424 |
|
| 425 |
for (unsigned int level = dst.min_level(); level <= dst.max_level(); ++level) |
| 426 |
if (dst[level].size() != dof_handler.n_dofs(level) || |
| 427 |
dst[level].locally_owned_size() != |
| 428 |
dof_handler.locally_owned_mg_dofs(level).n_elements()) |
| 429 |
{ |
| 430 |
// In case a ghosted level vector has been initialized, we can simply |
| 431 |
// use that as a template for the vector partitioning. If not, we |
| 432 |
// resort to the locally owned range of the dof handler. |
| 433 |
if (initialize_dof_vector) |
| 434 |
initialize_dof_vector(level, dst[level]); |
| 435 |
else if (level <= ghosted_level_vector.max_level() && |
| 436 |
ghosted_level_vector[level].size() == |
| 437 |
dof_handler.n_dofs(level)) |
| 438 |
dst[level].reinit(ghosted_level_vector[level], false); |
| 439 |
else |
| 440 |
dst[level].reinit(dof_handler.locally_owned_mg_dofs(level), |
| 441 |
dof_handler.get_mpi_communicator()); |
| 442 |
} |
| 443 |
else if ((perform_plain_copy == false && |
| 444 |
perform_renumbered_plain_copy == false) || |
| 445 |
level != dst.max_level()) |
| 446 |
dst[level] = 0; |
| 447 |
|
| 448 |
if (perform_plain_copy) |
| 449 |
{ |
| 450 |
// In this case, we can simply copy the local range. |
| 451 |
AssertDimension(dst[dst.max_level()].locally_owned_size(), |
| 452 |
src.locally_owned_size()); |
| 453 |
dst[dst.max_level()].copy_locally_owned_data_from(src); |
| 454 |
return; |
| 455 |
} |
| 456 |
else if (perform_renumbered_plain_copy) |
| 457 |
{ |
| 458 |
AssertDimension(dst[dst.max_level()].locally_owned_size(), |
| 459 |
src.locally_owned_size()); |
| 460 |
AssertDimension(this_copy_indices.back().n_cols(), |
| 461 |
src.locally_owned_size()); |
| 462 |
Assert(copy_indices_level_mine.back().n_rows() == 0, ExcInternalError()); |
| 463 |
LinearAlgebra::distributed::Vector<Number> &dst_level = |
| 464 |
dst[dst.max_level()]; |
| 465 |
// as opposed to the copy_unknowns lambda below, we here know that all |
| 466 |
// src elements will be touched, so we only need to do indirect |
| 467 |
// addressing on one index |
| 468 |
for (unsigned int i = 0; i < this_copy_indices.back().n_cols(); ++i) |
| 469 |
dst_level.local_element(this_copy_indices.back()(1, i)) = |
| 470 |
src.local_element(i); |
| 471 |
return; |
| 472 |
} |
| 473 |
|
| 474 |
// the ghosted vector should already have the correct local size (but |
| 475 |
// different parallel layout) |
| 476 |
AssertDimension(ghosted_global_vector.locally_owned_size(), |
| 477 |
src.locally_owned_size()); |
| 478 |
|
| 479 |
// copy the source vector to the temporary vector that we hold for the |
| 480 |
// purpose of data exchange |
| 481 |
this_ghosted_global_vector = src; |
| 482 |
this_ghosted_global_vector.update_ghost_values(); |
| 483 |
|
| 484 |
for (unsigned int level = dst.max_level() + 1; level != dst.min_level();) |
| 485 |
{ |
| 486 |
--level; |
| 487 |
|
| 488 |
LinearAlgebra::distributed::Vector<Number> &dst_level = dst[level]; |
| 489 |
|
| 490 |
auto copy_unknowns = [&](const Table<2, unsigned int> &indices) { |
| 491 |
for (unsigned int i = 0; i < indices.n_cols(); ++i) |
| 492 |
dst_level.local_element(indices(1, i)) = |
| 493 |
this_ghosted_global_vector.local_element(indices(0, i)); |
| 494 |
}; |
| 495 |
|
| 496 |
// first copy local unknowns |
| 497 |
copy_unknowns(this_copy_indices[level]); |
| 498 |
|
| 499 |
// Do the same for the indices where the level index is local, but the |
| 500 |
// global index is not |
| 501 |
copy_unknowns(this_copy_indices_level_mine[level]); |
| 502 |
|
| 503 |
dst_level.compress(VectorOperation::insert); |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
template <typename Number, typename MemorySpace> |
| 510 |
template <int dim, typename Number2, int spacedim> |
| 511 |
void |
| 512 |
MGLevelGlobalTransfer<LinearAlgebra::distributed::Vector<Number, MemorySpace>>:: |
| 513 |
copy_from_mg( |
| 514 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 515 |
LinearAlgebra::distributed::Vector<Number2, MemorySpace> &dst, |
| 516 |
const MGLevelObject<LinearAlgebra::distributed::Vector<Number, MemorySpace>> |
| 517 |
&src) const |
| 518 |
{ |
| 519 |
assert_built(dof_handler); |
| 520 |
AssertIndexRange(src.max_level(), |
| 521 |
dof_handler.get_triangulation().n_global_levels()); |
| 522 |
AssertIndexRange(src.min_level(), src.max_level() + 1); |
| 523 |
if (perform_plain_copy) |
| 524 |
{ |
| 525 |
// In this case, we can simply copy the local range. To avoid having |
| 526 |
// stray data in ghost entries of the destination, make sure to clear |
| 527 |
// them here. |
| 528 |
dst.zero_out_ghost_values(); |
| 529 |
AssertDimension(src[src.max_level()].locally_owned_size(), |
| 530 |
dst.locally_owned_size()); |
| 531 |
dst.copy_locally_owned_data_from(src[src.max_level()]); |
| 532 |
return; |
| 533 |
} |
| 534 |
else if (perform_renumbered_plain_copy) |
| 535 |
{ |
| 536 |
AssertDimension(src[src.max_level()].locally_owned_size(), |
| 537 |
dst.locally_owned_size()); |
| 538 |
AssertDimension(copy_indices.back().n_cols(), dst.locally_owned_size()); |
| 539 |
Assert(copy_indices_global_mine.back().n_rows() == 0, ExcInternalError()); |
| 540 |
Assert(copy_indices_global_mine.back().empty(), ExcInternalError()); |
| 541 |
const LinearAlgebra::distributed::Vector<Number> &src_level = |
| 542 |
src[src.max_level()]; |
| 543 |
dst.zero_out_ghost_values(); |
| 544 |
for (unsigned int i = 0; i < copy_indices.back().n_cols(); ++i) |
| 545 |
dst.local_element(i) = |
| 546 |
src_level.local_element(copy_indices.back()(1, i)); |
| 547 |
return; |
| 548 |
} |
| 549 |
|
| 550 |
// For non-DG: degrees of freedom in the refinement face may need special |
| 551 |
// attention, since they belong to the coarse level, but have fine level |
| 552 |
// basis functions |
| 553 |
|
| 554 |
dst = 0; |
| 555 |
for (unsigned int level = src.min_level(); level <= src.max_level(); ++level) |
| 556 |
{ |
| 557 |
// the ghosted vector should already have the correct local size (but |
| 558 |
// different parallel layout) |
| 559 |
if (ghosted_level_vector[level].size() > 0) |
| 560 |
AssertDimension(ghosted_level_vector[level].locally_owned_size(), |
| 561 |
src[level].locally_owned_size()); |
| 562 |
|
| 563 |
// the first time around, we copy the source vector to the temporary |
| 564 |
// vector that we hold for the purpose of data exchange |
| 565 |
LinearAlgebra::distributed::Vector<Number> &ghosted_vector = |
| 566 |
ghosted_level_vector[level]; |
| 567 |
|
| 568 |
if (ghosted_level_vector[level].size() > 0) |
| 569 |
ghosted_vector = src[level]; |
| 570 |
|
| 571 |
const auto *const ghosted_vector_ptr = |
| 572 |
(ghosted_level_vector[level].size() > 0) ? &ghosted_vector : |
| 573 |
&src[level]; |
| 574 |
|
| 575 |
ghosted_vector_ptr->update_ghost_values(); |
| 576 |
|
| 577 |
|
| 578 |
auto copy_unknowns = [&](const Table<2, unsigned int> &indices) { |
| 579 |
for (unsigned int i = 0; i < indices.n_cols(); ++i) |
| 580 |
dst.local_element(indices(0, i)) = |
| 581 |
ghosted_vector_ptr->local_element(indices(1, i)); |
| 582 |
}; |
| 583 |
|
| 584 |
// first copy local unknowns |
| 585 |
copy_unknowns(copy_indices[level]); |
| 586 |
|
| 587 |
// Do the same for the indices where the level index is local, but the |
| 588 |
// global index is not |
| 589 |
copy_unknowns(copy_indices_global_mine[level]); |
| 590 |
} |
| 591 |
dst.compress(VectorOperation::insert); |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
template <typename Number, typename MemorySpace> |
| 597 |
template <int dim, typename Number2, int spacedim> |
| 598 |
void |
| 599 |
MGLevelGlobalTransfer<LinearAlgebra::distributed::Vector<Number, MemorySpace>>:: |
| 600 |
copy_from_mg_add( |
| 601 |
const DoFHandler<dim, spacedim> &dof_handler, |
| 602 |
LinearAlgebra::distributed::Vector<Number2, MemorySpace> &dst, |
| 603 |
const MGLevelObject<LinearAlgebra::distributed::Vector<Number, MemorySpace>> |
| 604 |
&src) const |
| 605 |
{ |
| 606 |
assert_built(dof_handler); |
| 607 |
// For non-DG: degrees of freedom in the refinement face may need special |
| 608 |
// attention, since they belong to the coarse level, but have fine level |
| 609 |
// basis functions |
| 610 |
|
| 611 |
dst.zero_out_ghost_values(); |
| 612 |
for (unsigned int level = src.min_level(); level <= src.max_level(); ++level) |
| 613 |
{ |
| 614 |
// the ghosted vector should already have the correct local size (but |
| 615 |
// different parallel layout) |
| 616 |
AssertDimension(ghosted_level_vector[level].locally_owned_size(), |
| 617 |
src[level].locally_owned_size()); |
| 618 |
|
| 619 |
// the first time around, we copy the source vector to the temporary |
| 620 |
// vector that we hold for the purpose of data exchange |
| 621 |
LinearAlgebra::distributed::Vector<Number> &ghosted_vector = |
| 622 |
ghosted_level_vector[level]; |
| 623 |
ghosted_vector = src[level]; |
| 624 |
ghosted_vector.update_ghost_values(); |
| 625 |
|
| 626 |
auto copy_unknowns = [&](const Table<2, unsigned int> &indices) { |
| 627 |
for (unsigned int i = 0; i < indices.n_cols(); ++i) |
| 628 |
dst.local_element(indices(0, i)) += |
| 629 |
ghosted_vector.local_element(indices(1, i)); |
| 630 |
}; |
| 631 |
|
| 632 |
// first add local unknowns |
| 633 |
copy_unknowns(copy_indices[level]); |
| 634 |
|
| 635 |
// Do the same for the indices where the level index is local, but the |
| 636 |
// global index is not |
| 637 |
copy_unknowns(copy_indices_global_mine[level]); |
| 638 |
} |
| 639 |
dst.compress(VectorOperation::add); |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
template <typename Number, typename MemorySpace> |
| 645 |
void |
| 646 |
MGLevelGlobalTransfer<LinearAlgebra::distributed::Vector<Number, MemorySpace>>:: |
| 647 |
set_component_to_block_map(const std::vector<unsigned int> &map) |
| 648 |
{ |
| 649 |
component_to_block_map = map; |
| 650 |
} |
| 651 |
|
| 652 |
template <typename Number, typename MemorySpace> |
| 653 |
template <int dim, int spacedim> |
| 654 |
void |
| 655 |
MGLevelGlobalTransfer<LinearAlgebra::distributed::Vector<Number, MemorySpace>>:: |
| 656 |
assert_built(const DoFHandler<dim, spacedim> &dof_handler) const |
| 657 |
{ |
| 658 |
(void)dof_handler; |
| 659 |
Assert(copy_indices.size() == |
| 660 |
dof_handler.get_triangulation().n_global_levels(), |
| 661 |
ExcMessage("MGTransfer::build() has not been called!")); |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
DEAL_II_NAMESPACE_CLOSE |
| 666 |
|
| 667 |
#endif |