Reference documentation for deal.II version 9.1.0-pre
solution_transfer.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2009 - 2017 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 
21 # include <deal.II/distributed/solution_transfer.h>
22 # include <deal.II/distributed/tria.h>
23 
24 # include <deal.II/dofs/dof_accessor.h>
25 # include <deal.II/dofs/dof_tools.h>
26 
27 # include <deal.II/grid/tria_accessor.h>
28 # include <deal.II/grid/tria_iterator.h>
29 
30 # include <deal.II/lac/block_vector.h>
31 # include <deal.II/lac/la_parallel_block_vector.h>
32 # include <deal.II/lac/la_parallel_vector.h>
33 # include <deal.II/lac/petsc_block_vector.h>
34 # include <deal.II/lac/petsc_vector.h>
35 # include <deal.II/lac/trilinos_parallel_block_vector.h>
36 # include <deal.II/lac/trilinos_vector.h>
37 # include <deal.II/lac/vector.h>
38 
39 # include <functional>
40 
41 DEAL_II_NAMESPACE_OPEN
42 
43 namespace parallel
44 {
45  namespace distributed
46  {
47  template <int dim, typename VectorType, typename DoFHandlerType>
49  const DoFHandlerType &dof)
50  : dof_handler(&dof, typeid(*this).name())
51  , handle(numbers::invalid_unsigned_int)
52  {
53  Assert(
54  (dynamic_cast<const parallel::distributed::
56  &dof_handler->get_triangulation()) != nullptr),
57  ExcMessage(
58  "parallel::distributed::SolutionTransfer requires a parallel::distributed::Triangulation object."));
59  }
60 
61 
62 
63  template <int dim, typename VectorType, typename DoFHandlerType>
64  void
67  const std::vector<const VectorType *> &all_in)
68  {
69  input_vectors = all_in;
71  }
72 
73 
74 
75  template <int dim, typename VectorType, typename DoFHandlerType>
76  void
78  {
79  // TODO: casting away constness is bad
81  *tria = (dynamic_cast<parallel::distributed::Triangulation<
82  dim,
83  DoFHandlerType::space_dimension> *>(
85  *>(&dof_handler->get_triangulation())));
86  Assert(tria != nullptr, ExcInternalError());
87 
89  std::bind(
91  this,
92  std::placeholders::_1,
93  std::placeholders::_2),
94  /*returns_variable_size_data=*/false);
95  }
96 
97 
98 
99  template <int dim, typename VectorType, typename DoFHandlerType>
100  void
103  {
104  std::vector<const VectorType *> all_in(1, &in);
106  }
107 
108 
109 
110  template <int dim, typename VectorType, typename DoFHandlerType>
111  void
113  const VectorType &in)
114  {
115  std::vector<const VectorType *> all_in(1, &in);
116  prepare_serialization(all_in);
117  }
118 
119 
120 
121  template <int dim, typename VectorType, typename DoFHandlerType>
122  void
124  const std::vector<const VectorType *> &all_in)
125  {
127  }
128 
129 
130 
131  template <int dim, typename VectorType, typename DoFHandlerType>
132  void
134  VectorType &in)
135  {
136  std::vector<VectorType *> all_in(1, &in);
137  deserialize(all_in);
138  }
139 
140 
141 
142  template <int dim, typename VectorType, typename DoFHandlerType>
143  void
145  std::vector<VectorType *> &all_in)
146  {
148 
149  // this makes interpolate() happy
150  input_vectors.resize(all_in.size());
151 
152  interpolate(all_in);
153  }
154 
155 
156  template <int dim, typename VectorType, typename DoFHandlerType>
157  void
159  std::vector<VectorType *> &all_out)
160  {
161  Assert(input_vectors.size() == all_out.size(),
162  ExcDimensionMismatch(input_vectors.size(), all_out.size()));
163 
164  // TODO: casting away constness is bad
166  *tria = (dynamic_cast<parallel::distributed::Triangulation<
167  dim,
168  DoFHandlerType::space_dimension> *>(
170  *>(&dof_handler->get_triangulation())));
171  Assert(tria != nullptr, ExcInternalError());
172 
174  handle,
175  std::bind(
177  this,
178  std::placeholders::_1,
179  std::placeholders::_2,
180  std::placeholders::_3,
181  std::ref(all_out)));
182 
183 
184  for (typename std::vector<VectorType *>::iterator it = all_out.begin();
185  it != all_out.end();
186  ++it)
187  (*it)->compress(::VectorOperation::insert);
188 
189  input_vectors.clear();
190  }
191 
192 
193 
194  template <int dim, typename VectorType, typename DoFHandlerType>
195  void
197  VectorType &out)
198  {
199  std::vector<VectorType *> all_out(1, &out);
200  interpolate(all_out);
201  }
202 
203 
204 
205  template <int dim, typename VectorType, typename DoFHandlerType>
206  std::vector<char>
209  cell_iterator &cell_,
211  CellStatus /*status*/)
212  {
213  typename DoFHandlerType::cell_iterator cell(*cell_, dof_handler);
214 
215  // create buffer for each individual object
216  std::vector<::Vector<typename VectorType::value_type>> dofvalues(
217  input_vectors.size());
218 
219  auto cit_input = input_vectors.cbegin();
220  auto it_output = dofvalues.begin();
221  for (; cit_input != input_vectors.cend(); ++cit_input, ++it_output)
222  {
223  it_output->reinit(cell->get_fe().dofs_per_cell);
224  cell->get_interpolated_dof_values(*(*cit_input), *it_output);
225  }
226 
227  // to get consistent data sizes on each cell for the fixed size transfer,
228  // we won't allow compression
229  return Utilities::pack(dofvalues, /*allow_compression=*/false);
230  }
231 
232 
233 
234  template <int dim, typename VectorType, typename DoFHandlerType>
235  void
238  cell_iterator &cell_,
240  CellStatus /*status*/,
241  const boost::iterator_range<std::vector<char>::const_iterator>
242  & data_range,
243  std::vector<VectorType *> &all_out)
244  {
245  typename DoFHandlerType::cell_iterator cell(*cell_, dof_handler);
246 
247  const std::vector<::Vector<typename VectorType::value_type>>
248  dofvalues = Utilities::unpack<
249  std::vector<::Vector<typename VectorType::value_type>>>(
250  data_range.begin(), data_range.end(), /*allow_compression=*/false);
251 
252  // check if sizes match
253  Assert(dofvalues.size() == all_out.size(), ExcInternalError());
254 
255  // check if we have enough dofs provided by the FE object
256  // to interpolate the transferred data correctly
257  for (auto it_dofvalues = dofvalues.begin();
258  it_dofvalues != dofvalues.end();
259  ++it_dofvalues)
260  Assert(
261  cell->get_fe().dofs_per_cell == it_dofvalues->size(),
262  ExcMessage(
263  "The transferred data was packed with a different number of dofs than the"
264  "currently registered FE object assigned to the DoFHandler has."));
265 
266  // distribute data for each registered vector on mesh
267  auto it_input = dofvalues.cbegin();
268  auto it_output = all_out.begin();
269  for (; it_input != dofvalues.cend(); ++it_input, ++it_output)
270  cell->set_dof_values_by_interpolation(*it_input, *(*it_output));
271  }
272 
273 
274  } // namespace distributed
275 } // namespace parallel
276 
277 
278 // explicit instantiations
279 # include "solution_transfer.inst"
280 
281 DEAL_II_NAMESPACE_CLOSE
282 
283 #endif
unsigned int register_data_attach(const std::function< std::vector< char >(const cell_iterator &, const CellStatus)> &pack_callback, const bool returns_variable_size_data)
Definition: tria.cc:4342
std::vector< char > pack_callback(const typename Triangulation< dim, DoFHandlerType::space_dimension >::cell_iterator &cell, const typename Triangulation< dim, DoFHandlerType::space_dimension >::CellStatus status)
SolutionTransfer(const DoFHandlerType &dof)
void interpolate(std::vector< VectorType * > &all_out)
static::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:1227
static::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
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: tria.cc:4372
void unpack_callback(const typename Triangulation< dim, DoFHandlerType::space_dimension >::cell_iterator &cell, const typename Triangulation< dim, DoFHandlerType::space_dimension >::CellStatus status, const boost::iterator_range< std::vector< char >::const_iterator > &data_range, std::vector< VectorType * > &all_out)
size_t pack(const T &object, std::vector< char > &dest_buffer, const bool allow_compression=true)
Definition: utilities.h:1046
void prepare_serialization(const VectorType &in)
void prepare_for_coarsening_and_refinement(const std::vector< const VectorType * > &all_in)
std::vector< const VectorType * > input_vectors
SmartPointer< const DoFHandlerType, SolutionTransfer< dim, VectorType, DoFHandlerType > > dof_handler
T unpack(const std::vector< char > &buffer, const bool allow_compression=true)
Definition: utilities.h:1186
static::ExceptionBase & ExcInternalError()