Reference documentation for deal.II version GIT d7aca55de5 2022-08-10 12:50: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 - 2022 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 
160  handle = tria->register_data_attach(
161  [this](
162  const typename Triangulation<dim, spacedim>::cell_iterator &cell_,
163  const typename Triangulation<dim, spacedim>::CellStatus status) {
164  return this->pack_callback(cell_, status);
165  },
166  /*returns_variable_size_data=*/dof_handler->has_hp_capabilities());
167  }
168 
169 
170 
171  template <int dim, typename VectorType, int spacedim>
172  void
174  prepare_for_coarsening_and_refinement(const VectorType &in)
175  {
176  std::vector<const VectorType *> all_in(1, &in);
177  prepare_for_coarsening_and_refinement(all_in);
178  }
179 
180 
181 
182  template <int dim, typename VectorType, int spacedim>
183  void
185  const VectorType &in)
186  {
187  std::vector<const VectorType *> all_in(1, &in);
188  prepare_for_serialization(all_in);
189  }
190 
191 
192 
193  template <int dim, typename VectorType, int spacedim>
194  void
196  const std::vector<const VectorType *> &all_in)
197  {
198  prepare_for_coarsening_and_refinement(all_in);
199  }
200 
201 
202 
203  template <int dim, typename VectorType, int spacedim>
204  void
206  {
207  std::vector<VectorType *> all_in(1, &in);
208  deserialize(all_in);
209  }
210 
211 
212 
213  template <int dim, typename VectorType, int spacedim>
214  void
216  std::vector<VectorType *> &all_in)
217  {
218  register_data_attach();
219 
220  // this makes interpolate() happy
221  input_vectors.resize(all_in.size());
222 
223  interpolate(all_in);
224  }
225 
226 
227  template <int dim, typename VectorType, int spacedim>
228  void
230  std::vector<VectorType *> &all_out)
231  {
232  Assert(input_vectors.size() == all_out.size(),
233  ExcDimensionMismatch(input_vectors.size(), all_out.size()));
234  for (unsigned int i = 0; i < all_out.size(); ++i)
235  Assert(all_out[i]->size() == dof_handler->n_dofs(),
236  ExcDimensionMismatch(all_out[i]->size(), dof_handler->n_dofs()));
237 
238  // TODO: casting away constness is bad
241  const_cast<::Triangulation<dim, spacedim> *>(
242  &dof_handler->get_triangulation())));
243  Assert(tria != nullptr, ExcInternalError());
244 
245  if (average_values)
246  for (const auto vec : all_out)
247  *vec = 0.0;
248 
249  VectorType valence;
250 
251  // initialize valence vector only if we need to average
252  if (average_values)
253  valence.reinit(*all_out[0]);
254 
255  tria->notify_ready_to_unpack(
256  handle,
257  [this, &all_out, &valence](
258  const typename Triangulation<dim, spacedim>::cell_iterator &cell_,
259  const typename Triangulation<dim, spacedim>::CellStatus status,
260  const boost::iterator_range<std::vector<char>::const_iterator>
261  &data_range) {
262  this->unpack_callback(cell_, status, data_range, all_out, valence);
263  });
264 
265  if (average_values)
266  {
267  // finalize valence: compress and invert
268  using Number = typename VectorType::value_type;
269  valence.compress(::VectorOperation::add);
270  for (const auto i : valence.locally_owned_elements())
271  valence[i] = (static_cast<Number>(valence[i]) == Number() ?
272  Number() :
273  (Number(1.0) / static_cast<Number>(valence[i])));
274  valence.compress(::VectorOperation::insert);
275 
276  for (const auto vec : all_out)
277  {
278  // compress and weight with valence
279  vec->compress(::VectorOperation::add);
280  vec->scale(valence);
281  }
282  }
283  else
284  {
285  for (const auto vec : all_out)
286  vec->compress(::VectorOperation::insert);
287  }
288 
289  input_vectors.clear();
290  }
291 
292 
293 
294  template <int dim, typename VectorType, int spacedim>
295  void
297  {
298  std::vector<VectorType *> all_out(1, &out);
299  interpolate(all_out);
300  }
301 
302 
303 
304  template <int dim, typename VectorType, int spacedim>
305  std::vector<char>
307  const typename Triangulation<dim, spacedim>::cell_iterator &cell_,
308  const typename Triangulation<dim, spacedim>::CellStatus status)
309  {
310  typename DoFHandler<dim, spacedim>::cell_iterator cell(*cell_,
311  dof_handler);
312 
313  // create buffer for each individual object
314  std::vector<::Vector<typename VectorType::value_type>> dof_values(
315  input_vectors.size());
316 
317  unsigned int fe_index = 0;
318  if (dof_handler->has_hp_capabilities())
319  {
320  switch (status)
321  {
323  spacedim>::CELL_PERSIST:
325  spacedim>::CELL_REFINE:
326  {
327  fe_index = cell->future_fe_index();
328  break;
329  }
330 
332  spacedim>::CELL_COARSEN:
333  {
334  // In case of coarsening, we need to find a suitable FE index
335  // for the parent cell. We choose the 'least dominant fe'
336  // on all children from the associated FECollection.
337 # ifdef DEBUG
338  for (const auto &child : cell->child_iterators())
339  Assert(child->is_active() && child->coarsen_flag_set(),
340  typename ::Triangulation<
341  dim>::ExcInconsistentCoarseningFlags());
342 # endif
343 
344  fe_index = ::internal::hp::DoFHandlerImplementation::
345  dominated_future_fe_on_children<dim, spacedim>(cell);
346  break;
347  }
348 
349  default:
350  Assert(false, ExcInternalError());
351  break;
352  }
353  }
354 
355  const unsigned int dofs_per_cell =
356  dof_handler->get_fe(fe_index).n_dofs_per_cell();
357 
358  if (dofs_per_cell == 0)
359  return std::vector<char>(); // nothing to do for FE_Nothing
360 
361  auto it_input = input_vectors.cbegin();
362  auto it_output = dof_values.begin();
363  for (; it_input != input_vectors.cend(); ++it_input, ++it_output)
364  {
365  it_output->reinit(dofs_per_cell);
366  cell->get_interpolated_dof_values(*(*it_input), *it_output, fe_index);
367  }
368 
369  return pack_dof_values<typename VectorType::value_type>(dof_values,
370  dofs_per_cell);
371  }
372 
373 
374 
375  template <int dim, typename VectorType, int spacedim>
376  void
378  const typename Triangulation<dim, spacedim>::cell_iterator &cell_,
379  const typename Triangulation<dim, spacedim>::CellStatus status,
380  const boost::iterator_range<std::vector<char>::const_iterator>
381  & data_range,
382  std::vector<VectorType *> &all_out,
383  VectorType & valence)
384  {
385  typename DoFHandler<dim, spacedim>::cell_iterator cell(*cell_,
386  dof_handler);
387 
388  unsigned int fe_index = 0;
389  if (dof_handler->has_hp_capabilities())
390  {
391  switch (status)
392  {
394  spacedim>::CELL_PERSIST:
396  spacedim>::CELL_COARSEN:
397  {
398  fe_index = cell->active_fe_index();
399  break;
400  }
401 
403  spacedim>::CELL_REFINE:
404  {
405  // After refinement, this particular cell is no longer active,
406  // and its children have inherited its FE index. However, to
407  // unpack the data on the old cell, we need to recover its FE
408  // index from one of the children. Just to be sure, we also
409  // check if all children have the same FE index.
410  fe_index = cell->child(0)->active_fe_index();
411  for (unsigned int child_index = 1;
412  child_index < cell->n_children();
413  ++child_index)
414  Assert(cell->child(child_index)->active_fe_index() ==
415  fe_index,
416  ExcInternalError());
417  break;
418  }
419 
420  default:
421  Assert(false, ExcInternalError());
422  break;
423  }
424  }
425 
426  const unsigned int dofs_per_cell =
427  dof_handler->get_fe(fe_index).n_dofs_per_cell();
428 
429  if (dofs_per_cell == 0)
430  return; // nothing to do for FE_Nothing
431 
432  const std::vector<::Vector<typename VectorType::value_type>>
433  dof_values =
434  unpack_dof_values<typename VectorType::value_type>(data_range,
435  dofs_per_cell);
436 
437  // check if sizes match
438  Assert(dof_values.size() == all_out.size(), ExcInternalError());
439 
440  // check if we have enough dofs provided by the FE object
441  // to interpolate the transferred data correctly
442  for (auto it_dof_values = dof_values.begin();
443  it_dof_values != dof_values.end();
444  ++it_dof_values)
445  Assert(
446  dofs_per_cell == it_dof_values->size(),
447  ExcMessage(
448  "The transferred data was packed with a different number of dofs than the "
449  "currently registered FE object assigned to the DoFHandler has."));
450 
451  // distribute data for each registered vector on mesh
452  auto it_input = dof_values.cbegin();
453  auto it_output = all_out.begin();
454  for (; it_input != dof_values.cend(); ++it_input, ++it_output)
455  if (average_values)
456  cell->distribute_local_to_global_by_interpolation(*it_input,
457  *(*it_output),
458  fe_index);
459  else
460  cell->set_dof_values_by_interpolation(*it_input,
461  *(*it_output),
462  fe_index,
463  true);
464 
465  if (average_values)
466  {
467  // compute valence vector if averaging should be performed
468  Vector<typename VectorType::value_type> ones(dofs_per_cell);
469  ones = 1.0;
470  cell->distribute_local_to_global_by_interpolation(ones,
471  valence,
472  fe_index);
473  }
474  }
475  } // namespace distributed
476 } // namespace parallel
477 
478 
479 // explicit instantiations
480 # include "solution_transfer.inst"
481 
483 
484 #endif
virtual void clear()
Definition: vector.h:110
void unpack_callback(const typename Triangulation< dim, spacedim >::cell_iterator &cell, const typename Triangulation< dim, spacedim >::CellStatus status, const boost::iterator_range< std::vector< char >::const_iterator > &data_range, std::vector< VectorType * > &all_out, VectorType &valence)
std::vector< char > pack_callback(const typename Triangulation< dim, spacedim >::cell_iterator &cell, const typename Triangulation< dim, spacedim >::CellStatus status)
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:442
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:443
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
#define Assert(cond, exc)
Definition: exceptions.h:1473
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1667
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:201
const ::Triangulation< dim, spacedim > & tria