| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2019 - 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_cell_data_transfer_templates_h |
| 16 |
#define dealii_cell_data_transfer_templates_h |
| 17 |
|
| 18 |
|
| 19 |
#include <deal.II/base/config.h> |
| 20 |
|
| 21 |
#include <deal.II/distributed/tria.h> |
| 22 |
|
| 23 |
#include <deal.II/numerics/cell_data_transfer.h> |
| 24 |
|
| 25 |
#include <set> |
| 26 |
|
| 27 |
|
| 28 |
DEAL_II_NAMESPACE_OPEN |
| 29 |
|
| 30 |
|
| 31 |
namespace internal |
| 32 |
{ |
| 33 |
namespace CellDataTransferImplementation |
| 34 |
{ |
| 35 |
template <typename VectorType> |
| 36 |
void |
| 37 |
post_unpack_action(VectorType &out) |
| 38 |
{ |
| 39 |
out.compress(VectorOperation::insert); |
| 40 |
} |
| 41 |
|
| 42 |
template <typename value_type> |
| 43 |
void |
| 44 |
post_unpack_action(std::vector<value_type> &) |
| 45 |
{ |
| 46 |
// Do nothing for std::vector as VectorType. |
| 47 |
} |
| 48 |
} // namespace CellDataTransferImplementation |
| 49 |
} // namespace internal |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
template <int dim, int spacedim, typename VectorType> |
| 54 |
CellDataTransfer<dim, spacedim, VectorType>::CellDataTransfer( |
| 55 |
const Triangulation<dim, spacedim> &triangulation, |
| 56 |
const std::function<std::vector<value_type>( |
| 57 |
const typename Triangulation<dim, spacedim>::cell_iterator &parent, |
| 58 |
const value_type parent_value)> refinement_strategy, |
| 59 |
const std::function<value_type( |
| 60 |
const typename Triangulation<dim, spacedim>::cell_iterator &parent, |
| 61 |
const std::vector<value_type> &children_values)> coarsening_strategy) |
| 62 |
: triangulation(&triangulation, typeid(*this).name()) |
| 63 |
, refinement_strategy(refinement_strategy) |
| 64 |
, coarsening_strategy(coarsening_strategy) |
| 65 |
, n_active_cells_pre(numbers::invalid_unsigned_int) |
| 66 |
{ |
| 67 |
Assert( |
| 68 |
(dynamic_cast<const parallel::distributed::Triangulation<dim, spacedim> *>( |
| 69 |
&triangulation) == nullptr), |
| 70 |
ExcMessage("You are calling the CellDataTransfer class " |
| 71 |
"with a parallel::distributed::Triangulation. " |
| 72 |
"You probably want to use the " |
| 73 |
"parallel::distributed::CellDataTransfer class.")); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
template <int dim, int spacedim, typename VectorType> |
| 79 |
void |
| 80 |
CellDataTransfer<dim, spacedim, VectorType>:: |
| 81 |
prepare_for_coarsening_and_refinement() |
| 82 |
{ |
| 83 |
// Cleanup previous indices. |
| 84 |
refined_cells_active_index.clear(); |
| 85 |
coarsened_cells_active_index.clear(); |
| 86 |
persisting_cells_active_index.clear(); |
| 87 |
|
| 88 |
for (const auto &cell : triangulation->active_cell_iterators()) |
| 89 |
{ |
| 90 |
// Store iterator and active cell index of cells that will be refined. |
| 91 |
if (cell->refine_flag_set()) |
| 92 |
{ |
| 93 |
refined_cells_active_index.insert({cell, cell->active_cell_index()}); |
| 94 |
} |
| 95 |
else if (cell->coarsen_flag_set()) |
| 96 |
{ |
| 97 |
// Gather the iterator to the parent cell of cells that will be |
| 98 |
// coarsened. Store it together with the active cell indices of all |
| 99 |
// its children. |
| 100 |
Assert(cell->level() > 0, ExcInternalError()); |
| 101 |
const auto &parent = cell->parent(); |
| 102 |
|
| 103 |
// Check if the active_cell_indices for the current cell have |
| 104 |
// been determined already. |
| 105 |
if (coarsened_cells_active_index.find(parent) == |
| 106 |
coarsened_cells_active_index.end()) |
| 107 |
{ |
| 108 |
std::set<unsigned int> indices_children; |
| 109 |
for (const auto &sibling : parent->child_iterators()) |
| 110 |
{ |
| 111 |
Assert(sibling->is_active() && sibling->coarsen_flag_set(), |
| 112 |
typename dealii::Triangulation< |
| 113 |
dim>::ExcInconsistentCoarseningFlags()); |
| 114 |
|
| 115 |
indices_children.insert(sibling->active_cell_index()); |
| 116 |
} |
| 117 |
AssertDimension(indices_children.size(), parent->n_children()); |
| 118 |
|
| 119 |
coarsened_cells_active_index.insert({parent, indices_children}); |
| 120 |
} |
| 121 |
} |
| 122 |
else |
| 123 |
{ |
| 124 |
// Store iterator and active cell index of all other cells. |
| 125 |
persisting_cells_active_index.insert( |
| 126 |
{cell, cell->active_cell_index()}); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
n_active_cells_pre = triangulation->n_active_cells(); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
template <int dim, int spacedim, typename VectorType> |
| 136 |
void |
| 137 |
CellDataTransfer<dim, spacedim, VectorType>::unpack(const VectorType &in, |
| 138 |
VectorType &out) |
| 139 |
{ |
| 140 |
if constexpr (running_in_debug_mode()) |
| 141 |
{ |
| 142 |
Assert(in.size() == n_active_cells_pre, |
| 143 |
ExcDimensionMismatch(in.size(), n_active_cells_pre)); |
| 144 |
Assert(out.size() == triangulation->n_active_cells(), |
| 145 |
ExcDimensionMismatch(out.size(), triangulation->n_active_cells())); |
| 146 |
} |
| 147 |
|
| 148 |
// Transfer data of persisting cells. |
| 149 |
for (const auto &persisting : persisting_cells_active_index) |
| 150 |
{ |
| 151 |
Assert(persisting.first->is_active(), ExcInternalError()); |
| 152 |
out[persisting.first->active_cell_index()] = in[persisting.second]; |
| 153 |
} |
| 154 |
|
| 155 |
// Transfer data of the parent cell to all of its children that it has been |
| 156 |
// refined to. |
| 157 |
std::vector<value_type> children_values; |
| 158 |
for (const auto &refined : refined_cells_active_index) |
| 159 |
{ |
| 160 |
// Decide how to handle the previous data. |
| 161 |
children_values = refinement_strategy(refined.first, in[refined.second]); |
| 162 |
Assert(refined.first->n_children() == children_values.size(), |
| 163 |
ExcInternalError()); |
| 164 |
|
| 165 |
// Set values for all children cells. |
| 166 |
for (unsigned int child_index = 0; |
| 167 |
child_index < refined.first->n_children(); |
| 168 |
++child_index) |
| 169 |
{ |
| 170 |
const auto &child = refined.first->child(child_index); |
| 171 |
Assert(child->is_active(), ExcInternalError()); |
| 172 |
out[child->active_cell_index()] = children_values[child_index]; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
// Transfer data from the former children to the cell that they have been |
| 177 |
// coarsened to. |
| 178 |
for (const auto &coarsened : coarsened_cells_active_index) |
| 179 |
{ |
| 180 |
// Get previous values of former children. |
| 181 |
children_values.resize(coarsened.second.size()); |
| 182 |
|
| 183 |
auto indices_it = coarsened.second.cbegin(); |
| 184 |
auto values_it = children_values.begin(); |
| 185 |
for (; indices_it != coarsened.second.cend(); ++indices_it, ++values_it) |
| 186 |
*values_it = in[*indices_it]; |
| 187 |
Assert(values_it == children_values.end(), ExcInternalError()); |
| 188 |
|
| 189 |
// Decide how to handle the previous data. |
| 190 |
const value_type parent_value = |
| 191 |
coarsening_strategy(coarsened.first, children_values); |
| 192 |
|
| 193 |
// Set value for the parent cell. |
| 194 |
Assert(coarsened.first->is_active(), ExcInternalError()); |
| 195 |
out[coarsened.first->active_cell_index()] = parent_value; |
| 196 |
} |
| 197 |
|
| 198 |
internal::CellDataTransferImplementation::post_unpack_action(out); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
DEAL_II_NAMESPACE_CLOSE |
| 203 |
|
| 204 |
#endif /* dealii_cell_data_transfer_templates_h */ |