Reference documentation for deal.II version 8.4.2
solution_transfer.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2009 - 2015 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 at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 
17 #include <deal.II/base/config.h>
18 
19 #ifdef DEAL_II_WITH_P4EST
20 
21 #include <deal.II/lac/vector.h>
22 #include <deal.II/lac/block_vector.h>
23 #include <deal.II/lac/parallel_vector.h>
24 #include <deal.II/lac/parallel_block_vector.h>
25 #include <deal.II/lac/petsc_vector.h>
26 #include <deal.II/lac/petsc_block_vector.h>
27 #include <deal.II/lac/trilinos_vector.h>
28 #include <deal.II/lac/trilinos_block_vector.h>
29 
30 #include <deal.II/distributed/solution_transfer.h>
31 #include <deal.II/distributed/tria.h>
32 #include <deal.II/dofs/dof_tools.h>
33 #include <deal.II/dofs/dof_accessor.h>
34 #include <deal.II/grid/tria_accessor.h>
35 #include <deal.II/grid/tria_iterator.h>
36 
37 #include <deal.II/base/std_cxx11/bind.h>
38 
39 DEAL_II_NAMESPACE_OPEN
40 
41 namespace parallel
42 {
43  namespace distributed
44  {
45 
46  template<int dim, typename VectorType, typename DoFHandlerType>
48  :
49  dof_handler(&dof, typeid(*this).name())
50  {
52  (&dof_handler->get_triangulation()) != 0,
53  ExcMessage("parallel::distributed::SolutionTransfer requires a parallel::distributed::Triangulation object."));
54  }
55 
56 
57 
58  template<int dim, typename VectorType, typename DoFHandlerType>
60  {}
61 
62 
63 
64  template<int dim, typename VectorType, typename DoFHandlerType>
65  void
67  prepare_for_coarsening_and_refinement (const std::vector<const VectorType *> &all_in)
68  {
69  input_vectors = all_in;
70  register_data_attach( get_data_size() * input_vectors.size() );
71  }
72 
73 
74 
75  template<int dim, typename VectorType, typename DoFHandlerType>
76  void
78  {
79  Assert(size > 0, ExcMessage("Please transfer at least one vector!"));
80 
81 //TODO: casting away constness is bad
85  (&dof_handler->get_triangulation())));
86  Assert (tria != 0, ExcInternalError());
87 
88  offset
89  = tria->register_data_attach(size,
90  std_cxx11::bind(&SolutionTransfer<dim, VectorType,
91  DoFHandlerType>::pack_callback,
92  this,
93  std_cxx11::_1,
94  std_cxx11::_2,
95  std_cxx11::_3));
96 
97  }
98 
99 
100 
101  template<int dim, typename VectorType, typename DoFHandlerType>
102  void
105  {
106  std::vector<const VectorType *> all_in(1, &in);
108  }
109 
110 
111 
112  template<int dim, typename VectorType, typename DoFHandlerType>
113  void
115  {
116  std::vector<const VectorType *> all_in(1, &in);
117  prepare_serialization(all_in);
118  }
119 
120 
121 
122  template<int dim, typename VectorType, typename DoFHandlerType>
123  void
125  (const std::vector<const VectorType *> &all_in)
126  {
128  }
129 
130 
131 
132  template<int dim, typename VectorType, typename DoFHandlerType>
133  void
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  {
146  register_data_attach( get_data_size() * all_in.size() );
147 
148  // this makes interpolate() happy
149  input_vectors.resize(all_in.size());
150 
151  interpolate(all_in);
152  }
153 
154 
155  template<int dim, typename VectorType, typename DoFHandlerType>
156  void
158  {
159  Assert(input_vectors.size()==all_out.size(),
160  ExcDimensionMismatch(input_vectors.size(), all_out.size()) );
161 
162 //TODO: casting away constness is bad
166  (&dof_handler->get_triangulation())));
167  Assert (tria != 0, ExcInternalError());
168 
170  std_cxx11::bind(&SolutionTransfer<dim, VectorType,
171  DoFHandlerType>::unpack_callback,
172  this,
173  std_cxx11::_1,
174  std_cxx11::_2,
175  std_cxx11::_3,
176  std_cxx11::ref(all_out)));
177 
178 
179  for (typename std::vector<VectorType *>::iterator it=all_out.begin();
180  it !=all_out.end();
181  ++it)
182  (*it)->compress(::VectorOperation::insert);
183 
184  input_vectors.clear();
185  }
186 
187 
188 
189  template<int dim, typename VectorType, typename DoFHandlerType>
190  void
192  {
193  std::vector<VectorType *> all_out(1, &out);
194  interpolate(all_out);
195  }
196 
197 
198 
199  template<int dim, typename VectorType, typename DoFHandlerType>
200  unsigned int
202  {
203  return sizeof(double)* DoFTools::max_dofs_per_cell(*dof_handler);
204  }
205 
206 
207  template<int dim, typename VectorType, typename DoFHandlerType>
208  void
211  const typename Triangulation<dim,DoFHandlerType::space_dimension>::CellStatus /*status*/,
212  void *data)
213  {
214  typename VectorType::value_type *data_store = reinterpret_cast<typename VectorType::value_type *>(data);
215 
216  typename DoFHandlerType::cell_iterator cell(*cell_, dof_handler);
217 
218  const unsigned int dofs_per_cell=cell->get_fe().dofs_per_cell;
219  ::Vector<typename VectorType::value_type> dofvalues(dofs_per_cell);
220  for (typename std::vector<const VectorType *>::iterator it=input_vectors.begin();
221  it !=input_vectors.end();
222  ++it)
223  {
224  cell->get_interpolated_dof_values(*(*it), dofvalues);
225  Assert (typeid(typename VectorType::value_type) == typeid(double), ExcNotImplemented());
226  std::memcpy(data_store, &dofvalues(0), sizeof(typename VectorType::value_type)*dofs_per_cell);
227  data_store += dofs_per_cell;
228  }
229  }
230 
231 
232  template<int dim, typename VectorType, typename DoFHandlerType>
233  void
236  const typename Triangulation<dim,DoFHandlerType::space_dimension>::CellStatus /*status*/,
237  const void *data,
238  std::vector<VectorType *> &all_out)
239  {
240  typename DoFHandlerType::cell_iterator
241  cell(*cell_, dof_handler);
242 
243  const unsigned int dofs_per_cell=cell->get_fe().dofs_per_cell;
244  ::Vector<typename VectorType::value_type> dofvalues(dofs_per_cell);
245  const typename VectorType::value_type *data_store = reinterpret_cast<const typename VectorType::value_type *>(data);
246 
247  for (typename std::vector<VectorType *>::iterator it = all_out.begin();
248  it != all_out.end();
249  ++it)
250  {
251  Assert (typeid(typename VectorType::value_type) == typeid(double), ExcNotImplemented());
252  std::memcpy(&dofvalues(0), data_store, sizeof(typename VectorType::value_type)*dofs_per_cell);
253  cell->set_dof_values_by_interpolation(dofvalues, *(*it));
254  data_store += dofs_per_cell;
255  }
256  }
257 
258 
259  }
260 }
261 
262 
263 // explicit instantiations
264 #include "solution_transfer.inst"
265 
266 DEAL_II_NAMESPACE_CLOSE
267 
268 #endif
void prepare_for_coarsening_and_refinement(const std::vector< const VectorType *> &all_in)
unsigned int register_data_attach(const std::size_t size, const std_cxx11::function< void(const cell_iterator &, const CellStatus, void *)> &pack_callback)
Definition: tria.cc:4065
SolutionTransfer(const DoFHandlerType &dof)
::ExceptionBase & ExcMessage(std::string arg1)
void pack_callback(const typename Triangulation< dim, DoFHandlerType::space_dimension >::cell_iterator &cell, const typename Triangulation< dim, DoFHandlerType::space_dimension >::CellStatus status, void *data)
void interpolate(std::vector< VectorType *> &all_out)
void notify_ready_to_unpack(const unsigned int offset, const std_cxx11::function< void(const cell_iterator &, const CellStatus, const void *)> &unpack_callback)
Definition: tria.cc:4088
#define Assert(cond, exc)
Definition: exceptions.h:294
SmartPointer< const DoFHandlerType, SolutionTransfer< dim, VectorType, DoFHandlerType > > dof_handler
void prepare_serialization(const VectorType &in)
std::vector< const VectorType * > input_vectors
void unpack_callback(const typename Triangulation< dim, DoFHandlerType::space_dimension >::cell_iterator &cell, const typename Triangulation< dim, DoFHandlerType::space_dimension >::CellStatus status, const void *data, std::vector< VectorType *> &all_out)
::Triangulation< dim, spacedim >::cell_iterator cell_iterator
Definition: tria.h:348