60 template <
typename value_type>
63 const unsigned int dofs_per_cell)
65 for (
const auto &values : dof_values)
71 const std::size_t bytes_per_entry =
sizeof(value_type) * dofs_per_cell;
73 std::vector<char> buffer(dof_values.size() * bytes_per_entry);
74 for (
unsigned int i = 0; i < dof_values.size(); ++i)
75 std::memcpy(&buffer[i * bytes_per_entry],
87 template <
typename value_type>
88 std::vector<Vector<value_type>>
90 const boost::iterator_range<std::vector<char>::const_iterator> &data_range,
91 const unsigned int dofs_per_cell)
93 const std::size_t bytes_per_entry =
sizeof(value_type) * dofs_per_cell;
94 const unsigned int n_elements = data_range.size() / bytes_per_entry;
98 std::vector<Vector<value_type>> unpacked_data;
99 unpacked_data.reserve(n_elements);
100 for (
unsigned int i = 0; i < n_elements; ++i)
103 std::memcpy(&dof_values(0),
104 &(*std::next(data_range.begin(), i * bytes_per_entry)),
106 unpacked_data.emplace_back(std::move(dof_values));
109 return unpacked_data;
115template <
int dim,
typename VectorType,
int spacedim>
118 const bool average_values)
119 : dof_handler(&dof, typeid(*this).name())
120 , average_values(average_values)
121 , handle(
numbers::invalid_unsigned_int)
126template <
int dim,
typename VectorType,
int spacedim>
130 const std::vector<const VectorType *> &all_in)
132 const ::internal::parallel::shared::
133 TemporarilyRestoreSubdomainIds<dim, spacedim>
134 subdomain_modifier(dof_handler->get_triangulation());
136 for (
unsigned int i = 0; i < all_in.size(); ++i)
137 Assert(all_in[i]->
size() == dof_handler->n_dofs(),
140 input_vectors = all_in;
141 register_data_attach();
146template <
int dim,
typename VectorType,
int spacedim>
151 std::vector<const VectorType *> temp(all_in.size());
153 for (std::size_t i = 0; i < temp.size(); ++i)
154 temp[i] = &(all_in[i]);
156 this->prepare_for_coarsening_and_refinement(temp);
161template <
int dim,
typename VectorType,
int spacedim>
167 &dof_handler->get_triangulation());
171 ExcMessage(
"You can only add one solution per "
172 "SolutionTransfer object."));
174 handle = tria->register_data_attach(
177 return this->pack_callback(cell_, status);
179 dof_handler->has_hp_capabilities());
184template <
int dim,
typename VectorType,
int spacedim>
189 std::vector<const VectorType *> all_in(1, &in);
190 prepare_for_coarsening_and_refinement(all_in);
195template <
int dim,
typename VectorType,
int spacedim>
198 const VectorType &in)
200 std::vector<const VectorType *> all_in(1, &in);
201 prepare_for_serialization(all_in);
206template <
int dim,
typename VectorType,
int spacedim>
209 const std::vector<const VectorType *> &all_in)
211 prepare_for_coarsening_and_refinement(all_in);
216template <
int dim,
typename VectorType,
int spacedim>
220 std::vector<VectorType *> all_in(1, &in);
226template <
int dim,
typename VectorType,
int spacedim>
229 std::vector<VectorType *> &all_in)
231 register_data_attach();
234 input_vectors.resize(all_in.size());
240template <
int dim,
typename VectorType,
int spacedim>
243 std::vector<VectorType *> &all_out)
245 const ::internal::parallel::shared::
246 TemporarilyRestoreSubdomainIds<dim, spacedim>
247 subdomain_modifier(dof_handler->get_triangulation());
249 Assert(input_vectors.size() == all_out.size(),
251 for (
unsigned int i = 0; i < all_out.size(); ++i)
252 Assert(all_out[i]->
size() == dof_handler->n_dofs(),
257 &dof_handler->get_triangulation());
262 "You can only call interpolate() once per SolutionTransfer object."));
264 using Number =
typename VectorType::value_type;
267 for (
auto *
const vec : all_out)
274 valence.reinit(*all_out[0]);
276 tria->notify_ready_to_unpack(
278 [
this, &all_out, &valence](
281 const boost::iterator_range<std::vector<char>::const_iterator>
283 this->unpack_callback(cell_, status, data_range, all_out, valence);
290 for (
const auto i : valence.locally_owned_elements())
291 valence[i] = (
static_cast<Number
>(valence[i]) == Number() ?
293 (Number(1.0) /
static_cast<Number
>(valence[i])));
296 for (
auto *
const vec : all_out)
305 for (
auto *
const vec : all_out)
309 input_vectors.clear();
314template <
int dim,
typename VectorType,
int spacedim>
317 std::vector<VectorType> &all_out)
319 std::vector<VectorType *> temp(all_out.size());
321 for (std::size_t i = 0; i < temp.size(); ++i)
322 temp[i] = &(all_out[i]);
324 this->interpolate(temp);
329template <
int dim,
typename VectorType,
int spacedim>
333 std::vector<VectorType *> all_out(1, &out);
334 interpolate(all_out);
339template <
int dim,
typename VectorType,
int spacedim>
348 std::vector<::Vector<typename VectorType::value_type>> dof_values(
349 input_vectors.size());
351 unsigned int fe_index = 0;
352 if (dof_handler->has_hp_capabilities())
359 fe_index = cell->future_fe_index();
369 for (
const auto &child : cell->child_iterators())
370 Assert(child->is_active() && child->coarsen_flag_set(),
371 typename ::Triangulation<
372 dim>::ExcInconsistentCoarseningFlags());
375 fe_index = ::internal::hp::DoFHandlerImplementation::
376 dominated_future_fe_on_children<dim, spacedim>(cell);
386 const unsigned int dofs_per_cell =
387 dof_handler->get_fe(fe_index).n_dofs_per_cell();
389 if (dofs_per_cell == 0)
390 return std::vector<char>();
392 auto it_input = input_vectors.cbegin();
393 auto it_output = dof_values.begin();
394 for (; it_input != input_vectors.cend(); ++it_input, ++it_output)
396 it_output->reinit(dofs_per_cell);
397 cell->get_interpolated_dof_values(*(*it_input), *it_output, fe_index);
400 return pack_dof_values<typename VectorType::value_type>(dof_values,
406template <
int dim,
typename VectorType,
int spacedim>
411 const boost::iterator_range<std::vector<char>::const_iterator> &data_range,
412 std::vector<VectorType *> &all_out,
417 unsigned int fe_index = 0;
418 if (dof_handler->has_hp_capabilities())
425 fe_index = cell->active_fe_index();
436 fe_index = cell->child(0)->active_fe_index();
437 for (
unsigned int child_index = 1;
438 child_index < cell->n_children();
440 Assert(cell->child(child_index)->active_fe_index() == fe_index,
451 const unsigned int dofs_per_cell =
452 dof_handler->get_fe(fe_index).n_dofs_per_cell();
454 if (dofs_per_cell == 0)
457 const std::vector<::Vector<typename VectorType::value_type>>
459 unpack_dof_values<typename VectorType::value_type>(data_range,
467 for (
auto it_dof_values = dof_values.begin();
468 it_dof_values != dof_values.end();
471 dofs_per_cell == it_dof_values->size(),
473 "The transferred data was packed with a different number of dofs than the "
474 "currently registered FE object assigned to the DoFHandler has."));
477 auto it_input = dof_values.cbegin();
478 auto it_output = all_out.begin();
479 for (; it_input != dof_values.cend(); ++it_input, ++it_output)
481 cell->distribute_local_to_global_by_interpolation(*it_input,
485 cell->set_dof_values_by_interpolation(*it_input,
495 cell->distribute_local_to_global_by_interpolation(ones,
503template <
int dim,
typename VectorType,
int spacedim>
515 template <
int dim,
typename VectorType,
int spacedim>
518 : dof_handler(&dof, typeid(*this).name())
526 "You are calling the ::SolutionTransfer class "
527 "with a DoFHandler that is built on a "
528 "parallel::distributed::Triangulation. This will not "
529 "work for parallel computations. You probably want to "
530 "use the parallel::distributed::SolutionTransfer class."));
535 template <
int dim,
typename VectorType,
int spacedim>
543 template <
int dim,
typename VectorType,
int spacedim>
547 indices_on_cell.clear();
548 dof_values_on_cell.clear();
556 template <
int dim,
typename VectorType,
int spacedim>
560 Assert(prepared_for != pure_refinement, ExcAlreadyPrepForRef());
561 Assert(prepared_for != coarsening_and_refinement,
562 ExcAlreadyPrepForCoarseAndRef());
574 const ::internal::parallel::shared::
575 TemporarilyRestoreSubdomainIds<dim, spacedim>
576 subdomain_modifier(dof_handler->get_triangulation());
578 const unsigned int n_active_cells =
579 dof_handler->get_triangulation().n_active_cells();
580 n_dofs_old = dof_handler->n_dofs();
583 std::vector<std::vector<types::global_dof_index>>(n_active_cells)
584 .
swap(indices_on_cell);
586 for (
const auto &cell : dof_handler->active_cell_iterators())
588 const unsigned int i = cell->active_cell_index();
589 indices_on_cell[i].resize(cell->get_fe().n_dofs_per_cell());
596 cell->get_dof_indices(indices_on_cell[i]);
597 cell_map[std::make_pair(cell->level(), cell->index())] =
600 prepared_for = pure_refinement;
605 template <
int dim,
typename VectorType,
int spacedim>
608 const VectorType &in,
609 VectorType &out)
const
611 Assert(prepared_for == pure_refinement, ExcNotPrepared());
612 Assert(in.size() == n_dofs_old,
614 Assert(out.size() == dof_handler->n_dofs(),
617 ExcMessage(
"Vectors cannot be used as input and output"
618 " at the same time!"));
628 const ::internal::parallel::shared::
629 TemporarilyRestoreSubdomainIds<dim, spacedim>
630 subdomain_modifier(dof_handler->get_triangulation());
634 typename std::map<std::pair<unsigned int, unsigned int>,
636 cell_map_end = cell_map.end();
638 for (
const auto &cell : dof_handler->cell_iterators())
641 cell_map.find(std::make_pair(cell->level(), cell->index()));
643 if (pointerstruct != cell_map_end)
651 const unsigned int this_fe_index =
653 const unsigned int dofs_per_cell =
654 cell->get_dof_handler().get_fe(this_fe_index).n_dofs_per_cell();
655 local_values.
reinit(dofs_per_cell,
true);
660 Assert(dofs_per_cell == (*pointerstruct->second.indices_ptr).size(),
662 for (
unsigned int i = 0; i < dofs_per_cell; ++i)
665 in, (*pointerstruct->second.indices_ptr)[i]);
666 cell->set_dof_values_by_interpolation(local_values,
691 template <
int dim,
int spacedim>
700 const ::hp::FECollection<dim, spacedim> &fe =
702 matrices.reinit(fe.size(), fe.size());
703 for (
unsigned int i = 0; i < fe.size(); ++i)
704 for (
unsigned int j = 0; j < fe.size(); ++j)
707 matrices(i, j).reinit(fe[i].n_dofs_per_cell(),
708 fe[j].n_dofs_per_cell());
719 fe[i].get_interpolation_matrix(fe[j], matrices(i, j));
722 ExcInterpolationNotImplemented &)
724 matrices(i, j).reinit(0, 0);
730 template <
int dim,
int spacedim>
733 std::vector<std::vector<bool>> &)
736 template <
int dim,
int spacedim>
739 const ::hp::FECollection<dim, spacedim> &fe,
740 std::vector<std::vector<bool>> &restriction_is_additive)
742 restriction_is_additive.resize(fe.size());
743 for (
unsigned int f = 0; f < fe.size(); ++f)
745 restriction_is_additive[f].resize(fe[f].n_dofs_per_cell());
746 for (
unsigned int i = 0; i < fe[f].n_dofs_per_cell(); ++i)
747 restriction_is_additive[f][i] = fe[f].restriction_is_additive(i);
754 template <
int dim,
typename VectorType,
int spacedim>
759 Assert(prepared_for != pure_refinement, ExcAlreadyPrepForRef());
760 Assert(prepared_for != coarsening_and_refinement,
761 ExcAlreadyPrepForCoarseAndRef());
764 n_dofs_old = dof_handler->n_dofs();
765 const unsigned int in_size = all_in.size();
769 ExcMessage(
"The array of input vectors you pass to this "
770 "function has no elements. This is not useful."));
771 for (
unsigned int i = 0; i < in_size; ++i)
786 const ::internal::parallel::shared::
787 TemporarilyRestoreSubdomainIds<dim, spacedim>
788 subdomain_modifier(dof_handler->get_triangulation());
793 unsigned int n_cells_to_coarsen = 0;
794 unsigned int n_cells_to_stay_or_refine = 0;
795 for (
const auto &act_cell : dof_handler->active_cell_iterators())
797 if (act_cell->coarsen_flag_set())
798 ++n_cells_to_coarsen;
800 ++n_cells_to_stay_or_refine;
802 Assert((n_cells_to_coarsen + n_cells_to_stay_or_refine) ==
803 dof_handler->get_triangulation().n_active_cells(),
806 unsigned int n_coarsen_fathers = 0;
807 for (
const auto &cell : dof_handler->cell_iterators())
808 if (!cell->is_active() && cell->child(0)->coarsen_flag_set())
811 (void)n_cells_to_coarsen;
816 std::vector<std::vector<types::global_dof_index>>(n_cells_to_stay_or_refine)
817 .
swap(indices_on_cell);
819 std::vector<std::vector<Vector<typename VectorType::value_type>>>(
821 std::vector<Vector<typename VectorType::value_type>>(in_size))
822 .swap(dof_values_on_cell);
825 std::vector<std::vector<bool>> restriction_is_additive;
829 restriction_is_additive);
834 unsigned int n_sr = 0, n_cf = 0;
835 for (
const auto &cell : dof_handler->cell_iterators())
838 if (cell->is_active() && !cell->coarsen_flag_set())
840 const unsigned int dofs_per_cell = cell->get_fe().n_dofs_per_cell();
841 indices_on_cell[n_sr].resize(dofs_per_cell);
846 cell->get_dof_indices(indices_on_cell[n_sr]);
847 cell_map[std::make_pair(cell->level(), cell->index())] =
848 Pointerstruct(&indices_on_cell[n_sr], cell->active_fe_index());
853 else if (cell->has_children() && cell->child(0)->coarsen_flag_set())
861 std::set<unsigned int> fe_indices_children;
862 for (
const auto &child : cell->child_iterators())
864 Assert(child->is_active() && child->coarsen_flag_set(),
865 typename ::Triangulation<
866 dim>::ExcInconsistentCoarseningFlags());
868 fe_indices_children.insert(child->active_fe_index());
872 const unsigned int target_fe_index =
873 dof_handler->get_fe_collection().find_dominated_fe_extended(
874 fe_indices_children, 0);
877 ::internal::hp::DoFHandlerImplementation::
878 ExcNoDominatedFiniteElementOnChildren());
880 const unsigned int dofs_per_cell =
881 dof_handler->get_fe(target_fe_index).n_dofs_per_cell();
883 std::vector<Vector<typename VectorType::value_type>>(
885 .
swap(dof_values_on_cell[n_cf]);
893 for (
unsigned int j = 0; j < in_size; ++j)
894 cell->get_interpolated_dof_values(all_in[j],
895 dof_values_on_cell[n_cf][j],
897 cell_map[std::make_pair(cell->level(), cell->index())] =
905 prepared_for = coarsening_and_refinement;
910 template <
int dim,
typename VectorType,
int spacedim>
915 std::vector<VectorType> all_in(1, in);
916 prepare_for_coarsening_and_refinement(all_in);
921 template <
int dim,
typename VectorType,
int spacedim>
924 const std::vector<VectorType> &all_in,
925 std::vector<VectorType> &all_out)
const
927 const unsigned int size = all_in.size();
929 Assert(prepared_for == coarsening_and_refinement, ExcNotPrepared());
931 for (
unsigned int i = 0; i <
size; ++i)
934 for (
unsigned int i = 0; i < all_out.size(); ++i)
935 Assert(all_out[i].
size() == dof_handler->n_dofs(),
937 for (
unsigned int i = 0; i <
size; ++i)
938 for (
unsigned int j = 0; j <
size; ++j)
939 Assert(&all_in[i] != &all_out[j],
940 ExcMessage(
"Vectors cannot be used as input and output"
941 " at the same time!"));
952 const ::internal::parallel::shared::
953 TemporarilyRestoreSubdomainIds<dim, spacedim>
954 subdomain_modifier(dof_handler->get_triangulation());
957 std::vector<types::global_dof_index> dofs;
959 typename std::map<std::pair<unsigned int, unsigned int>,
961 cell_map_end = cell_map.end();
967 for (
const auto &cell : dof_handler->cell_iterators())
970 cell_map.find(std::make_pair(cell->level(), cell->index()));
972 if (pointerstruct != cell_map_end)
974 const std::vector<types::global_dof_index> *
const indexptr =
975 pointerstruct->second.indices_ptr;
977 const std::vector<Vector<typename VectorType::value_type>>
978 *
const valuesptr = pointerstruct->second.dof_values_ptr;
981 if (indexptr !=
nullptr)
985 const unsigned int old_fe_index =
986 pointerstruct->second.active_fe_index;
990 unsigned int in_size = indexptr->size();
991 for (
unsigned int j = 0; j <
size; ++j)
993 tmp.
reinit(in_size,
true);
994 for (
unsigned int i = 0; i < in_size; ++i)
996 all_in[j], (*indexptr)[i]);
998 cell->set_dof_values_by_interpolation(tmp,
1010 const unsigned int dofs_per_cell =
1011 cell->get_fe().n_dofs_per_cell();
1012 dofs.resize(dofs_per_cell);
1015 cell->get_dof_indices(dofs);
1018 for (
unsigned int j = 0; j <
size; ++j)
1026 const unsigned int active_fe_index =
1027 cell->active_fe_index();
1028 if (active_fe_index !=
1029 pointerstruct->second.active_fe_index)
1031 const unsigned int old_index =
1032 pointerstruct->second.active_fe_index;
1034 interpolation_hp(active_fe_index, old_index);
1037 if (interpolation_matrix.empty())
1038 tmp.
reinit(dofs_per_cell,
false);
1041 tmp.
reinit(dofs_per_cell,
true);
1043 interpolation_matrix.
n());
1045 interpolation_matrix.
m());
1046 interpolation_matrix.
vmult(tmp, (*valuesptr)[j]);
1051 data = &(*valuesptr)[j];
1054 for (
unsigned int i = 0; i < dofs_per_cell; ++i)
1056 (*
data)(i), dofs[i], all_out[j]);
1067 for (
auto &vec : all_out)
1073 template <
int dim,
typename VectorType,
int spacedim>
1076 const VectorType &in,
1077 VectorType &out)
const
1079 Assert(in.size() == n_dofs_old,
1081 Assert(out.size() == dof_handler->n_dofs(),
1084 std::vector<VectorType> all_in = {in};
1085 std::vector<VectorType> all_out = {out};
1087 interpolate(all_in, all_out);
1094 template <
int dim,
typename VectorType,
int spacedim>
1104 sizeof(prepared_for) +
1111 template <
int dim,
typename VectorType,
int spacedim>
1116 return sizeof(*this);
1123#define SPLIT_INSTANTIATIONS_COUNT 4
1124#ifndef SPLIT_INSTANTIATIONS_INDEX
1125# define SPLIT_INSTANTIATIONS_INDEX 0
1127#include "solution_transfer.inst"
@ children_will_be_coarsened
const hp::FECollection< dim, spacedim > & get_fe_collection() const
bool has_hp_capabilities() const
void vmult(Vector< number2 > &w, const Vector< number2 > &v, const bool adding=false) const
void refine_interpolate(const VectorType &in, VectorType &out) const
void interpolate(const std::vector< VectorType > &all_in, std::vector< VectorType > &all_out) const
std::size_t memory_consumption() const
ObserverPointer< const DoFHandler< dim, spacedim >, SolutionTransfer< dim, VectorType, spacedim > > dof_handler
void prepare_for_coarsening_and_refinement(const std::vector< VectorType > &all_in)
void prepare_for_pure_refinement()
SolutionTransfer(const DoFHandler< dim, spacedim > &dof)
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)
void register_data_attach()
void deserialize(VectorType &in)
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)
virtual size_type size() const override
virtual void reinit(const size_type N, const bool omit_zeroing_entries=false)
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcMessage(std::string arg1)
typename ActiveSelector::cell_iterator cell_iterator
std::vector< index_type > data
void restriction_additive(const FiniteElement< dim, spacedim > &, std::vector< std::vector< bool > > &)
void extract_interpolation_matrices(const DoFHandler< dim, spacedim > &dof, ::Table< 2, FullMatrix< double > > &matrices)
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)
static const unsigned int invalid_unsigned_int
void swap(ObserverPointer< T, P > &t1, ObserverPointer< T, Q > &t2)
std::size_t memory_consumption() const
unsigned int active_fe_index
static VectorType::value_type get(const VectorType &V, const types::global_dof_index i)