Reference documentation for deal.II version GIT c14369f203 2023-10-01 07:40:02+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
solution_transfer.cc
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2009 - 2023 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 
17 #include <deal.II/base/config.h>
18 
19 #ifdef DEAL_II_WITH_P4EST
20 
23 
25 # include <deal.II/dofs/dof_tools.h>
26 
29 
37 # include <deal.II/lac/vector.h>
38 
39 # include <functional>
40 # include <numeric>
41 
42 
44 
45 
46 namespace
47 {
56  template <typename value_type>
57  std::vector<char>
58  pack_dof_values(std::vector<Vector<value_type>> &dof_values,
59  const unsigned int dofs_per_cell)
60  {
61  for (const auto &values : dof_values)
62  {
63  AssertDimension(values.size(), dofs_per_cell);
64  (void)values;
65  }
66 
67  const std::size_t bytes_per_entry = sizeof(value_type) * dofs_per_cell;
68 
69  std::vector<char> buffer(dof_values.size() * bytes_per_entry);
70  for (unsigned int i = 0; i < dof_values.size(); ++i)
71  std::memcpy(&buffer[i * bytes_per_entry],
72  &dof_values[i](0),
73  bytes_per_entry);
74 
75  return buffer;
76  }
77 
78 
79 
83  template <typename value_type>
84  std::vector<Vector<value_type>>
85  unpack_dof_values(
86  const boost::iterator_range<std::vector<char>::const_iterator> &data_range,
87  const unsigned int dofs_per_cell)
88  {
89  const std::size_t bytes_per_entry = sizeof(value_type) * dofs_per_cell;
90  const unsigned int n_elements = data_range.size() / bytes_per_entry;
91 
92  Assert((data_range.size() % bytes_per_entry == 0), ExcInternalError());
93 
94  std::vector<Vector<value_type>> unpacked_data;
95  unpacked_data.reserve(n_elements);
96  for (unsigned int i = 0; i < n_elements; ++i)
97  {
98  Vector<value_type> dof_values(dofs_per_cell);
99  std::memcpy(&dof_values(0),
100  &(*std::next(data_range.begin(), i * bytes_per_entry)),
101  bytes_per_entry);
102  unpacked_data.emplace_back(std::move(dof_values));
103  }
104 
105  return unpacked_data;
106  }
107 } // namespace
108 
109 
110 
111 namespace parallel
112 {
113  namespace distributed
114  {
115  template <int dim, typename VectorType, int spacedim>
117  const DoFHandler<dim, spacedim> &dof,
118  const bool average_values)
119  : dof_handler(&dof, typeid(*this).name())
120  , average_values(average_values)
121  , handle(numbers::invalid_unsigned_int)
122  {
123  Assert(
124  (dynamic_cast<
126  &dof_handler->get_triangulation()) != nullptr),
127  ExcMessage(
128  "parallel::distributed::SolutionTransfer requires a parallel::distributed::Triangulation object."));
129  }
130 
131 
132 
133  template <int dim, typename VectorType, int spacedim>
134  void
137  const std::vector<const VectorType *> &all_in)
138  {
139  for (unsigned int i = 0; i < all_in.size(); ++i)
140  Assert(all_in[i]->size() == dof_handler->n_dofs(),
141  ExcDimensionMismatch(all_in[i]->size(), dof_handler->n_dofs()));
142 
143  input_vectors = all_in;
144  register_data_attach();
145  }
146 
147 
148 
149  template <int dim, typename VectorType, int spacedim>
150  void
152  {
153  // TODO: casting away constness is bad
156  const_cast<::Triangulation<dim, spacedim> *>(
157  &dof_handler->get_triangulation())));
158  Assert(tria != nullptr, ExcInternalError());
159 
161  ExcMessage("You can only add one solution per "
162  "SolutionTransfer object."));
163 
164  handle = tria->register_data_attach(
165  [this](
166  const typename Triangulation<dim, spacedim>::cell_iterator &cell_,
167  const CellStatus status) {
168  return this->pack_callback(cell_, status);
169  },
170  /*returns_variable_size_data=*/dof_handler->has_hp_capabilities());
171  }
172 
173 
174 
175  template <int dim, typename VectorType, int spacedim>
176  void
178  prepare_for_coarsening_and_refinement(const VectorType &in)
179  {
180  std::vector<const VectorType *> all_in(1, &in);
181  prepare_for_coarsening_and_refinement(all_in);
182  }
183 
184 
185 
186  template <int dim, typename VectorType, int spacedim>
187  void
189  const VectorType &in)
190  {
191  std::vector<const VectorType *> all_in(1, &in);
192  prepare_for_serialization(all_in);
193  }
194 
195 
196 
197  template <int dim, typename VectorType, int spacedim>
198  void
200  const std::vector<const VectorType *> &all_in)
201  {
202  prepare_for_coarsening_and_refinement(all_in);
203  }
204 
205 
206 
207  template <int dim, typename VectorType, int spacedim>
208  void
210  {
211  std::vector<VectorType *> all_in(1, &in);
212  deserialize(all_in);
213  }
214 
215 
216 
217  template <int dim, typename VectorType, int spacedim>
218  void
220  std::vector<VectorType *> &all_in)
221  {
222  register_data_attach();
223 
224  // this makes interpolate() happy
225  input_vectors.resize(all_in.size());
226 
227  interpolate(all_in);
228  }
229 
230 
231  template <int dim, typename VectorType, int spacedim>
232  void
234  std::vector<VectorType *> &all_out)
235  {
236  Assert(input_vectors.size() == all_out.size(),
237  ExcDimensionMismatch(input_vectors.size(), all_out.size()));
238  for (unsigned int i = 0; i < all_out.size(); ++i)
239  Assert(all_out[i]->size() == dof_handler->n_dofs(),
240  ExcDimensionMismatch(all_out[i]->size(), dof_handler->n_dofs()));
241 
242  // TODO: casting away constness is bad
245  const_cast<::Triangulation<dim, spacedim> *>(
246  &dof_handler->get_triangulation())));
247  Assert(tria != nullptr, ExcInternalError());
248 
249  if (average_values)
250  for (auto *const vec : all_out)
251  *vec = 0.0;
252 
253  VectorType valence;
254 
255  // initialize valence vector only if we need to average
256  if (average_values)
257  valence.reinit(*all_out[0]);
258 
260  handle,
261  [this, &all_out, &valence](
262  const typename Triangulation<dim, spacedim>::cell_iterator &cell_,
263  const CellStatus status,
264  const boost::iterator_range<std::vector<char>::const_iterator>
265  &data_range) {
266  this->unpack_callback(cell_, status, data_range, all_out, valence);
267  });
268 
269  if (average_values)
270  {
271  // finalize valence: compress and invert
272  using Number = typename VectorType::value_type;
273  valence.compress(VectorOperation::add);
274  for (const auto i : valence.locally_owned_elements())
275  valence[i] = (static_cast<Number>(valence[i]) == Number() ?
276  Number() :
277  (Number(1.0) / static_cast<Number>(valence[i])));
278  valence.compress(VectorOperation::insert);
279 
280  for (auto *const vec : all_out)
281  {
282  // compress and weight with valence
283  vec->compress(VectorOperation::add);
284  vec->scale(valence);
285  }
286  }
287  else
288  {
289  for (auto *const vec : all_out)
290  vec->compress(VectorOperation::insert);
291  }
292 
293  input_vectors.clear();
295  }
296 
297 
298 
299  template <int dim, typename VectorType, int spacedim>
300  void
302  {
303  std::vector<VectorType *> all_out(1, &out);
304  interpolate(all_out);
305  }
306 
307 
308 
309  template <int dim, typename VectorType, int spacedim>
310  std::vector<char>
312  const typename Triangulation<dim, spacedim>::cell_iterator &cell_,
313  const CellStatus status)
314  {
315  typename DoFHandler<dim, spacedim>::cell_iterator cell(*cell_,
316  dof_handler);
317 
318  // create buffer for each individual object
319  std::vector<::Vector<typename VectorType::value_type>> dof_values(
320  input_vectors.size());
321 
322  unsigned int fe_index = 0;
323  if (dof_handler->has_hp_capabilities())
324  {
325  switch (status)
326  {
329  {
330  fe_index = cell->future_fe_index();
331  break;
332  }
333 
335  {
336  // In case of coarsening, we need to find a suitable FE index
337  // for the parent cell. We choose the 'least dominant fe'
338  // on all children from the associated FECollection.
339 # ifdef DEBUG
340  for (const auto &child : cell->child_iterators())
341  Assert(child->is_active() && child->coarsen_flag_set(),
342  typename ::Triangulation<
343  dim>::ExcInconsistentCoarseningFlags());
344 # endif
345 
346  fe_index = ::internal::hp::DoFHandlerImplementation::
347  dominated_future_fe_on_children<dim, spacedim>(cell);
348  break;
349  }
350 
351  default:
352  Assert(false, ExcInternalError());
353  break;
354  }
355  }
356 
357  const unsigned int dofs_per_cell =
358  dof_handler->get_fe(fe_index).n_dofs_per_cell();
359 
360  if (dofs_per_cell == 0)
361  return std::vector<char>(); // nothing to do for FE_Nothing
362 
363  auto it_input = input_vectors.cbegin();
364  auto it_output = dof_values.begin();
365  for (; it_input != input_vectors.cend(); ++it_input, ++it_output)
366  {
367  it_output->reinit(dofs_per_cell);
368  cell->get_interpolated_dof_values(*(*it_input), *it_output, fe_index);
369  }
370 
371  return pack_dof_values<typename VectorType::value_type>(dof_values,
372  dofs_per_cell);
373  }
374 
375 
376 
377  template <int dim, typename VectorType, int spacedim>
378  void
380  const typename Triangulation<dim, spacedim>::cell_iterator &cell_,
381  const CellStatus status,
382  const boost::iterator_range<std::vector<char>::const_iterator>
383  &data_range,
384  std::vector<VectorType *> &all_out,
385  VectorType &valence)
386  {
387  typename DoFHandler<dim, spacedim>::cell_iterator cell(*cell_,
388  dof_handler);
389 
390  unsigned int fe_index = 0;
391  if (dof_handler->has_hp_capabilities())
392  {
393  switch (status)
394  {
397  {
398  fe_index = cell->active_fe_index();
399  break;
400  }
401 
403  {
404  // After refinement, this particular cell is no longer active,
405  // and its children have inherited its FE index. However, to
406  // unpack the data on the old cell, we need to recover its FE
407  // index from one of the children. Just to be sure, we also
408  // check if all children have the same FE index.
409  fe_index = cell->child(0)->active_fe_index();
410  for (unsigned int child_index = 1;
411  child_index < cell->n_children();
412  ++child_index)
413  Assert(cell->child(child_index)->active_fe_index() ==
414  fe_index,
415  ExcInternalError());
416  break;
417  }
418 
419  default:
420  Assert(false, ExcInternalError());
421  break;
422  }
423  }
424 
425  const unsigned int dofs_per_cell =
426  dof_handler->get_fe(fe_index).n_dofs_per_cell();
427 
428  if (dofs_per_cell == 0)
429  return; // nothing to do for FE_Nothing
430 
431  const std::vector<::Vector<typename VectorType::value_type>>
432  dof_values =
433  unpack_dof_values<typename VectorType::value_type>(data_range,
434  dofs_per_cell);
435 
436  // check if sizes match
437  Assert(dof_values.size() == all_out.size(), ExcInternalError());
438 
439  // check if we have enough dofs provided by the FE object
440  // to interpolate the transferred data correctly
441  for (auto it_dof_values = dof_values.begin();
442  it_dof_values != dof_values.end();
443  ++it_dof_values)
444  Assert(
445  dofs_per_cell == it_dof_values->size(),
446  ExcMessage(
447  "The transferred data was packed with a different number of dofs than the "
448  "currently registered FE object assigned to the DoFHandler has."));
449 
450  // distribute data for each registered vector on mesh
451  auto it_input = dof_values.cbegin();
452  auto it_output = all_out.begin();
453  for (; it_input != dof_values.cend(); ++it_input, ++it_output)
454  if (average_values)
455  cell->distribute_local_to_global_by_interpolation(*it_input,
456  *(*it_output),
457  fe_index);
458  else
459  cell->set_dof_values_by_interpolation(*it_input,
460  *(*it_output),
461  fe_index,
462  true);
463 
464  if (average_values)
465  {
466  // compute valence vector if averaging should be performed
467  Vector<typename VectorType::value_type> ones(dofs_per_cell);
468  ones = 1.0;
469  cell->distribute_local_to_global_by_interpolation(ones,
470  valence,
471  fe_index);
472  }
473  }
474  } // namespace distributed
475 } // namespace parallel
476 
477 
478 // explicit instantiations
479 # include "solution_transfer.inst"
480 
482 
483 #endif
CellStatus
Definition: cell_status.h:32
@ cell_will_be_refined
@ children_will_be_coarsened
unsigned int register_data_attach(const std::function< std::vector< char >(const cell_iterator &, const ::CellStatus)> &pack_callback, const bool returns_variable_size_data)
void notify_ready_to_unpack(const unsigned int handle, const std::function< void(const cell_iterator &, const ::CellStatus, const boost::iterator_range< std::vector< char >::const_iterator > &)> &unpack_callback)
Definition: vector.h:110
std::vector< char > pack_callback(const typename Triangulation< dim, spacedim >::cell_iterator &cell, const CellStatus status)
void unpack_callback(const typename Triangulation< dim, spacedim >::cell_iterator &cell, const CellStatus status, const boost::iterator_range< std::vector< char >::const_iterator > &data_range, std::vector< VectorType * > &all_out, VectorType &valence)
SmartPointer< const DoFHandler< dim, spacedim >, SolutionTransfer< dim, VectorType, spacedim > > dof_handler
void prepare_for_coarsening_and_refinement(const std::vector< const VectorType * > &all_in)
void prepare_for_serialization(const VectorType &in)
void interpolate(std::vector< VectorType * > &all_out)
SolutionTransfer(const DoFHandler< dim, spacedim > &dof_handler, const bool average_values=false)
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:477
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:478
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
#define Assert(cond, exc)
Definition: exceptions.h:1616
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1789
static ::ExceptionBase & ExcMessage(std::string arg1)
void interpolate(const DoFHandler< dim, spacedim > &dof1, const InVector &u1, const DoFHandler< dim, spacedim > &dof2, OutVector &u2)
static const unsigned int invalid_unsigned_int
Definition: types.h:213
unsigned short int fe_index
Definition: types.h:60
const ::Triangulation< dim, spacedim > & tria