35#include <deal.II/multigrid/mg_transfer_matrix_free.templates.h>
42template <
int dim,
typename Number>
45 , element_is_continuous(false)
47 , n_child_cell_dofs(0)
52template <
int dim,
typename Number>
56 , element_is_continuous(false)
58 , n_child_cell_dofs(0)
65template <
int dim,
typename Number>
70 this->mg_constrained_dofs = &mg_c;
75template <
int dim,
typename Number>
82 element_is_continuous =
false;
84 n_child_cell_dofs = 0;
85 level_dof_indices.clear();
86 parent_child_connect.clear();
87 dirichlet_indices.clear();
88 n_owned_level_cells.clear();
89 prolongation_matrix_1d.clear();
90 evaluation_data.clear();
91 weights_on_refined.clear();
96template <
int dim,
typename Number>
100 const std::vector<std::shared_ptr<const Utilities::MPI::Partitioner>>
101 &external_partitioners)
105 "The underlying DoFHandler object has not had its "
106 "distribute_mg_dofs() function called, but this is a prerequisite "
107 "for multigrid transfers. You will need to call this function, "
108 "probably close to where you already call distribute_dofs()."));
109 if (external_partitioners.size() > 0)
111 Assert(this->initialize_dof_vector ==
nullptr,
112 ExcMessage(
"An initialize_dof_vector function has already "
113 "been registered in the constructor!"));
115 this->initialize_dof_vector =
116 [external_partitioners](
117 const unsigned int level,
119 vec.reinit(external_partitioners[
level]);
123 this->fill_and_communicate_copy_indices(dof_handler);
125 vector_partitioners.resize(0,
128 for (
unsigned int level = 0;
level <= this->ghosted_level_vector.max_level();
130 vector_partitioners[
level] =
131 this->ghosted_level_vector[
level].get_partitioner();
133 std::vector<std::vector<Number>> weights_unvectorized;
139 this->mg_constrained_dofs,
140 external_partitioners,
143 parent_child_connect,
146 weights_unvectorized,
147 this->copy_indices_global_mine,
148 vector_partitioners);
152 this->ghosted_level_vector.resize(0, vector_partitioners.max_level());
153 for (
unsigned int level = 0;
level <= vector_partitioners.max_level();
155 if (external_partitioners.size() == vector_partitioners.max_level() + 1 &&
156 external_partitioners[
level].get() == vector_partitioners[
level].get())
157 this->ghosted_level_vector[
level].reinit(0);
159 this->ghosted_level_vector[
level].reinit(vector_partitioners[
level]);
174 const unsigned int n_levels =
178 weights_on_refined.resize(n_levels - 1);
181 weights_on_refined[
level - 1].resize(
182 ((n_owned_level_cells[
level - 1] + vec_size - 1) / vec_size) *
185 for (
unsigned int c = 0; c < n_owned_level_cells[
level - 1]; ++c)
187 const unsigned int comp = c / vec_size;
188 const unsigned int v = c % vec_size;
189 for (
unsigned int i = 0; i < n_weights_per_cell; ++i)
191 weights_on_refined[
level - 1][comp * n_weights_per_cell + i][v] =
192 weights_unvectorized[
level - 1][c * n_weights_per_cell + i];
197 evaluation_data.resize(n_child_cell_dofs);
202template <
int dim,
typename Number>
206 const std::function<
void(
const unsigned int,
208 &initialize_dof_vector)
210 if (initialize_dof_vector)
212 const unsigned int n_levels =
215 std::vector<std::shared_ptr<const Utilities::MPI::Partitioner>>
216 external_partitioners(n_levels);
221 initialize_dof_vector(
level, vector);
225 build(dof_handler, external_partitioners);
235template <
int dim,
typename Number>
238 const unsigned int to_level,
243 prolongate_and_add(to_level, dst, src);
248template <
int dim,
typename Number>
251 const unsigned int to_level,
255 Assert((to_level >= 1) && (to_level <= level_dof_indices.size()),
259 this->vector_partitioners[to_level - 1].get();
260 if (src_inplace ==
false)
262 if (this->ghosted_level_vector[to_level - 1].get_partitioner().get() !=
263 this->vector_partitioners[to_level - 1].get())
264 this->ghosted_level_vector[to_level - 1].reinit(
265 this->vector_partitioners[to_level - 1]);
266 this->ghosted_level_vector[to_level - 1].copy_locally_owned_data_from(
270 const bool dst_inplace =
271 dst.
get_partitioner().get() == this->vector_partitioners[to_level].get();
272 if (dst_inplace ==
false)
274 if (this->ghosted_level_vector[to_level].get_partitioner().get() !=
275 this->vector_partitioners[to_level].get())
276 this->ghosted_level_vector[to_level].reinit(
277 this->vector_partitioners[to_level]);
278 AssertDimension(this->ghosted_level_vector[to_level].locally_owned_size(),
280 this->ghosted_level_vector[to_level] = 0.;
284 src_inplace ? src : this->ghosted_level_vector[to_level - 1];
286 dst_inplace ? dst : this->ghosted_level_vector[to_level];
293 do_prolongate_add<0>(to_level, dst_vec, src_vec);
294 else if (fe_degree == 1)
295 do_prolongate_add<1>(to_level, dst_vec, src_vec);
296 else if (fe_degree == 2)
297 do_prolongate_add<2>(to_level, dst_vec, src_vec);
298 else if (fe_degree == 3)
299 do_prolongate_add<3>(to_level, dst_vec, src_vec);
300 else if (fe_degree == 4)
301 do_prolongate_add<4>(to_level, dst_vec, src_vec);
302 else if (fe_degree == 5)
303 do_prolongate_add<5>(to_level, dst_vec, src_vec);
304 else if (fe_degree == 6)
305 do_prolongate_add<6>(to_level, dst_vec, src_vec);
306 else if (fe_degree == 7)
307 do_prolongate_add<7>(to_level, dst_vec, src_vec);
308 else if (fe_degree == 8)
309 do_prolongate_add<8>(to_level, dst_vec, src_vec);
310 else if (fe_degree == 9)
311 do_prolongate_add<9>(to_level, dst_vec, src_vec);
312 else if (fe_degree == 10)
313 do_prolongate_add<10>(to_level, dst_vec, src_vec);
315 do_prolongate_add<-1>(to_level, dst_vec, src_vec);
318 if (dst_inplace ==
false)
321 if (src_inplace ==
true)
327template <
int dim,
typename Number>
330 const unsigned int from_level,
334 Assert((from_level >= 1) && (from_level <= level_dof_indices.size()),
337 const bool src_inplace =
338 src.
get_partitioner().get() == this->vector_partitioners[from_level].get();
339 if (src_inplace ==
false)
341 if (this->ghosted_level_vector[from_level].get_partitioner().get() !=
342 this->vector_partitioners[from_level].get())
343 this->ghosted_level_vector[from_level].reinit(
344 this->vector_partitioners[from_level]);
345 this->ghosted_level_vector[from_level].copy_locally_owned_data_from(src);
349 this->vector_partitioners[from_level - 1].get();
350 if (dst_inplace ==
false)
352 if (this->ghosted_level_vector[from_level - 1].get_partitioner().get() !=
353 this->vector_partitioners[from_level - 1].get())
354 this->ghosted_level_vector[from_level - 1].reinit(
355 this->vector_partitioners[from_level - 1]);
357 this->ghosted_level_vector[from_level - 1].locally_owned_size(),
359 this->ghosted_level_vector[from_level - 1] = 0.;
363 src_inplace ? src : this->ghosted_level_vector[from_level];
365 dst_inplace ? dst : this->ghosted_level_vector[from_level - 1];
370 do_restrict_add<0>(from_level, dst_vec, src_vec);
371 else if (fe_degree == 1)
372 do_restrict_add<1>(from_level, dst_vec, src_vec);
373 else if (fe_degree == 2)
374 do_restrict_add<2>(from_level, dst_vec, src_vec);
375 else if (fe_degree == 3)
376 do_restrict_add<3>(from_level, dst_vec, src_vec);
377 else if (fe_degree == 4)
378 do_restrict_add<4>(from_level, dst_vec, src_vec);
379 else if (fe_degree == 5)
380 do_restrict_add<5>(from_level, dst_vec, src_vec);
381 else if (fe_degree == 6)
382 do_restrict_add<6>(from_level, dst_vec, src_vec);
383 else if (fe_degree == 7)
384 do_restrict_add<7>(from_level, dst_vec, src_vec);
385 else if (fe_degree == 8)
386 do_restrict_add<8>(from_level, dst_vec, src_vec);
387 else if (fe_degree == 9)
388 do_restrict_add<9>(from_level, dst_vec, src_vec);
389 else if (fe_degree == 10)
390 do_restrict_add<10>(from_level, dst_vec, src_vec);
393 do_restrict_add<-1>(from_level, dst_vec, src_vec);
396 if (dst_inplace ==
false)
399 if (src_inplace ==
true)
405template <
int dim,
typename Number>
409 const unsigned int to_level,
414 const unsigned int degree_size = (degree > -1 ? degree : fe_degree) + 1;
415 const unsigned int n_child_dofs_1d = 2 * degree_size - element_is_continuous;
416 const unsigned int n_scalar_cell_dofs =
425 if (this->mg_constrained_dofs !=
nullptr &&
426 this->mg_constrained_dofs->get_user_constraint_matrix(to_level - 1)
433 this->mg_constrained_dofs->get_user_constraint_matrix(to_level - 1)
434 .distribute(copy_src);
444 for (
unsigned int cell = 0; cell < n_owned_level_cells[to_level - 1];
447 const unsigned int n_lanes =
448 (cell + vec_size > n_owned_level_cells[to_level - 1]) ?
449 (n_owned_level_cells[to_level - 1] - cell) :
453 for (
unsigned int v = 0; v < n_lanes; ++v)
455 const unsigned int shift =
457 parent_child_connect[to_level - 1][cell + v].
second,
458 fe_degree + 1 - element_is_continuous,
460 const unsigned int *indices =
461 &level_dof_indices[to_level - 1]
462 [parent_child_connect[to_level - 1][cell + v]
466 for (
unsigned int c = 0, m = 0; c < n_components; ++c)
468 for (
unsigned int k = 0; k < (dim > 2 ? degree_size : 1); ++k)
469 for (
unsigned int j = 0; j < (dim > 1 ? degree_size : 1); ++j)
470 for (
unsigned int i = 0; i < degree_size; ++i, ++m)
472 indices[c * n_scalar_cell_dofs +
473 k * n_child_dofs_1d * n_child_dofs_1d +
474 j * n_child_dofs_1d + i]);
477 for (std::vector<unsigned short>::const_iterator i =
478 dirichlet_indices[to_level - 1][cell + v].begin();
479 i != dirichlet_indices[to_level - 1][cell + v].end();
481 evaluation_data[*i][v] = 0.;
486 degree_size * n_child_dofs_1d);
488 if (element_is_continuous)
492 for (
int c = n_components - 1; c >= 0; --c)
498 2 * degree + 1>::do_forward(1,
499 prolongation_matrix_1d,
500 evaluation_data.begin() +
503 evaluation_data.begin() +
504 c * n_scalar_cell_dofs,
508 degree != -1 ? 2 * degree + 1 :
511 &weights_on_refined[to_level - 1][(cell / vec_size) * three_to_dim],
514 evaluation_data.begin());
518 for (
int c = n_components - 1; c >= 0; --c)
524 2 * degree + 2>::do_forward(1,
525 prolongation_matrix_1d,
526 evaluation_data.begin() +
529 evaluation_data.begin() +
530 c * n_scalar_cell_dofs,
536 const unsigned int *indices =
537 &level_dof_indices[to_level][cell * n_child_cell_dofs];
538 for (
unsigned int v = 0; v < n_lanes; ++v)
540 for (
unsigned int i = 0; i < n_child_cell_dofs; ++i)
542 indices += n_child_cell_dofs;
549template <
int dim,
typename Number>
553 const unsigned int from_level,
558 const unsigned int degree_size = (degree > -1 ? degree : fe_degree) + 1;
559 const unsigned int n_child_dofs_1d = 2 * degree_size - element_is_continuous;
560 const unsigned int n_scalar_cell_dofs =
564 for (
unsigned int cell = 0; cell < n_owned_level_cells[from_level - 1];
567 const unsigned int n_lanes =
568 (cell + vec_size > n_owned_level_cells[from_level - 1]) ?
569 (n_owned_level_cells[from_level - 1] - cell) :
574 const unsigned int *indices =
575 &level_dof_indices[from_level][cell * n_child_cell_dofs];
576 for (
unsigned int v = 0; v < n_lanes; ++v)
578 for (
unsigned int i = 0; i < n_child_cell_dofs; ++i)
580 indices += n_child_cell_dofs;
585 degree_size * n_child_dofs_1d);
587 if (element_is_continuous)
590 degree != -1 ? 2 * degree + 1 :
593 &weights_on_refined[from_level - 1]
594 [(cell / vec_size) * three_to_dim],
597 evaluation_data.data());
598 for (
unsigned int c = 0; c < n_components; ++c)
604 2 * degree + 1>::do_backward(1,
605 prolongation_matrix_1d,
607 evaluation_data.begin() +
608 c * n_scalar_cell_dofs,
609 evaluation_data.begin() +
617 for (
unsigned int c = 0; c < n_components; ++c)
623 2 * degree + 2>::do_backward(1,
624 prolongation_matrix_1d,
626 evaluation_data.begin() +
627 c * n_scalar_cell_dofs,
628 evaluation_data.begin() +
636 for (
unsigned int v = 0; v < n_lanes; ++v)
638 const unsigned int shift =
640 parent_child_connect[from_level - 1][cell + v].
second,
641 fe_degree + 1 - element_is_continuous,
644 parent_child_connect[from_level - 1][cell + v].
first *
646 n_child_cell_dofs - 1,
647 level_dof_indices[from_level - 1].size());
648 const unsigned int *indices =
649 &level_dof_indices[from_level - 1]
650 [parent_child_connect[from_level - 1][cell + v]
654 for (
unsigned int c = 0, m = 0; c < n_components; ++c)
657 for (std::vector<unsigned short>::const_iterator i =
658 dirichlet_indices[from_level - 1][cell + v].begin();
659 i != dirichlet_indices[from_level - 1][cell + v].end();
661 evaluation_data[*i][v] = 0.;
663 for (
unsigned int k = 0; k < (dim > 2 ? degree_size : 1); ++k)
664 for (
unsigned int j = 0; j < (dim > 1 ? degree_size : 1); ++j)
665 for (
unsigned int i = 0; i < degree_size; ++i, ++m)
667 indices[c * n_scalar_cell_dofs +
668 k * n_child_dofs_1d * n_child_dofs_1d +
669 j * n_child_dofs_1d + i]) +=
670 evaluation_data[m][v];
678template <
int dim,
typename Number>
696template <
int dim,
typename Number>
708template <
int dim,
typename Number>
710 const std::vector<MGConstrainedDoFs> &mg_c)
715 for (
const auto &constrained_block_dofs : mg_c)
721template <
int dim,
typename Number>
726 Assert(this->same_for_all,
727 ExcMessage(
"This object was initialized with support for usage with "
728 "one DoFHandler for each block, but this method assumes "
729 "that the same DoFHandler is used for all the blocks!"));
732 this->matrix_free_transfer_vector[0].initialize_constraints(mg_c);
737template <
int dim,
typename Number>
740 const std::vector<MGConstrainedDoFs> &mg_c)
742 Assert(!this->same_for_all,
743 ExcMessage(
"This object was initialized with support for using "
744 "the same DoFHandler for all the blocks, but this "
745 "method assumes that there is a separate DoFHandler "
747 AssertDimension(this->matrix_free_transfer_vector.size(), mg_c.size());
749 for (
unsigned int i = 0; i < mg_c.size(); ++i)
750 this->matrix_free_transfer_vector[i].initialize_constraints(mg_c[i]);
755template <
int dim,
typename Number>
761 this->matrix_free_transfer_vector[0].build(dof_handler);
766template <
int dim,
typename Number>
771 AssertDimension(this->matrix_free_transfer_vector.size(), dof_handler.size());
772 for (
unsigned int i = 0; i < dof_handler.size(); ++i)
773 this->matrix_free_transfer_vector[i].build(*dof_handler[i]);
778template <
int dim,
typename Number>
782 std::size_t total_memory_consumption = 0;
783 for (
const auto &el : this->matrix_free_transfer_vector)
784 total_memory_consumption += el.memory_consumption();
785 return total_memory_consumption;
790template <
int dim,
typename Number>
793 const unsigned int b)
const
795 return matrix_free_transfer_vector[b];
800template <
int dim,
typename Number>
804 this->matrix_free_transfer_vector.clear();
810#include "mg_transfer_matrix_free.inst"
const Triangulation< dim, spacedim > & get_triangulation() const
bool has_level_dofs() const
void zero_out_ghost_values() const
Number local_element(const size_type local_index) const
void update_ghost_values() const
size_type locally_owned_size() const
void compress(VectorOperation::values operation)
const std::shared_ptr< const Utilities::MPI::Partitioner > & get_partitioner() const
void reinit(const size_type size, const bool omit_zeroing_entries=false)
SmartPointer< const MGConstrainedDoFs > mg_constrained_dofs
std::vector< MGTransferMatrixFree< dim, Number > > matrix_free_transfer_vector
void build(const DoFHandler< dim > &dof_handler)
std::size_t memory_consumption() const
MGTransferBlockMatrixFree()=default
const MGTransferMatrixFree< dim, Number > & get_matrix_free_transfer(const unsigned int b) const override
void initialize_constraints(const MGConstrainedDoFs &mg_constrained_dofs)
void do_restrict_add(const unsigned int from_level, LinearAlgebra::distributed::Vector< Number > &dst, const LinearAlgebra::distributed::Vector< Number > &src) const
void do_prolongate_add(const unsigned int to_level, LinearAlgebra::distributed::Vector< Number > &dst, const LinearAlgebra::distributed::Vector< Number > &src) const
virtual void prolongate(const unsigned int to_level, LinearAlgebra::distributed::Vector< Number > &dst, const LinearAlgebra::distributed::Vector< Number > &src) const override
void initialize_constraints(const MGConstrainedDoFs &mg_constrained_dofs)
void build(const DoFHandler< dim > &dof_handler, const std::vector< std::shared_ptr< const Utilities::MPI::Partitioner > > &external_partitioners=std::vector< std::shared_ptr< const Utilities::MPI::Partitioner > >())
virtual void prolongate_and_add(const unsigned int to_level, LinearAlgebra::distributed::Vector< Number > &dst, const LinearAlgebra::distributed::Vector< Number > &src) const override
std::size_t memory_consumption() const
virtual void restrict_and_add(const unsigned int from_level, LinearAlgebra::distributed::Vector< Number > &dst, const LinearAlgebra::distributed::Vector< Number > &src) const override
virtual unsigned int n_global_levels() const
static constexpr std::size_t size()
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcIndexRange(std::size_t arg1, std::size_t arg2, std::size_t arg3)
static ::ExceptionBase & ExcMessage(std::string arg1)
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)
constexpr T fixed_power(const T t)
constexpr T pow(const T base, const int iexp)
unsigned int compute_shift_within_children(const unsigned int child, const unsigned int fe_shift_1d, const unsigned int fe_degree)
void setup_transfer(const DoFHandler< dim > &dof_handler, const MGConstrainedDoFs *mg_constrained_dofs, const std::vector< std::shared_ptr< const Utilities::MPI::Partitioner > > &external_partitioners, ElementInfo< Number > &elem_info, std::vector< std::vector< unsigned int > > &level_dof_indices, std::vector< std::vector< std::pair< unsigned int, unsigned int > > > &parent_child_connect, std::vector< unsigned int > &n_owned_level_cells, std::vector< std::vector< std::vector< unsigned short > > > &dirichlet_indices, std::vector< std::vector< Number > > &weights_on_refined, std::vector< Table< 2, unsigned int > > ©_indices_global_mine, MGLevelObject< std::shared_ptr< const Utilities::MPI::Partitioner > > &vector_partitioners)
void weight_fe_q_dofs_by_entity(const Number *weights, const unsigned int n_components, const int n_points_1d_non_template, Number *data)
std::vector< Number > prolongation_matrix_1d
unsigned int n_child_cell_dofs
bool element_is_continuous
unsigned int n_components