Skip to content

Commit

Permalink
Move setup to build()
Browse files Browse the repository at this point in the history
  • Loading branch information
peterrum committed Jan 7, 2022
1 parent 5c77f46 commit eb4d4d5
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 53 deletions.
9 changes: 4 additions & 5 deletions doc/news/changes/minor/20210309Munch
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
New: The classes MGLevelGlobalTransfer and MGTransferMatrixFree now
accept in the constructor an optional function for initializing
the internal level vectors. This is useful if one uses the the
transfer operators in the context of smoothers that are build
around MatrixFree objects.
New: The classes MGTransferMatrixFree::build() now also
accepts an optional function for initializing the internal level vectors.
This is useful if one uses the the transfer operators in the context of
smoothers that are build around MatrixFree objects.
<br>
(Peter Munch, 2021/03/09)

8 changes: 0 additions & 8 deletions include/deal.II/multigrid/mg_transfer.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,14 +413,6 @@ class MGLevelGlobalTransfer<LinearAlgebra::distributed::Vector<Number>>
: public MGTransferBase<LinearAlgebra::distributed::Vector<Number>>
{
public:
/**
* Constructor.
*/
MGLevelGlobalTransfer(
const std::function<void(const unsigned int,
LinearAlgebra::distributed::Vector<Number> &)>
&initialize_dof_vector = {});

/**
* Reset the object to the state it had right after the default constructor.
*/
Expand Down
11 changes: 0 additions & 11 deletions include/deal.II/multigrid/mg_transfer.templates.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,16 +395,6 @@ MGLevelGlobalTransfer<VectorType>::assert_built(
/* --------- MGLevelGlobalTransfer<LinearAlgebra::distributed::Vector> -------
*/


template <typename Number>
MGLevelGlobalTransfer<LinearAlgebra::distributed::Vector<Number>>::
MGLevelGlobalTransfer(
const std::function<void(const unsigned int,
LinearAlgebra::distributed::Vector<Number> &)>
&initialize_dof_vector)
: initialize_dof_vector(initialize_dof_vector)
{}

template <typename Number>
template <int dim, typename Number2, int spacedim>
void
Expand Down Expand Up @@ -584,7 +574,6 @@ MGLevelGlobalTransfer<LinearAlgebra::distributed::Vector<Number>>::copy_from_mg(
if (ghosted_level_vector[level].size() > 0)
ghosted_vector = src[level];


const auto ghosted_vector_ptr = (ghosted_level_vector[level].size() > 0) ?
&ghosted_vector :
&src[level];
Expand Down
21 changes: 12 additions & 9 deletions include/deal.II/multigrid/mg_transfer_matrix_free.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,13 @@ class MGTransferMatrixFree
* Constructor without constraint matrices. Use this constructor only with
* discontinuous finite elements or with no local refinement.
*/
MGTransferMatrixFree(
const std::function<void(const unsigned int,
LinearAlgebra::distributed::Vector<Number> &)>
&initialize_dof_vector = {});
MGTransferMatrixFree();

/**
* Constructor with constraints. Equivalent to the default constructor
* followed by initialize_constraints().
*/
MGTransferMatrixFree(
const MGConstrainedDoFs &mg_constrained_dofs,
const std::function<void(const unsigned int,
LinearAlgebra::distributed::Vector<Number> &)>
&initialize_dof_vector = {});
MGTransferMatrixFree(const MGConstrainedDoFs &mg_constrained_dofs);

/**
* Destructor.
Expand Down Expand Up @@ -112,6 +105,16 @@ class MGTransferMatrixFree
&external_partitioners =
std::vector<std::shared_ptr<const Utilities::MPI::Partitioner>>());

/**
* Same as above but taking a lambda for initializing vector instead of
* partitioners.
*/
void
build(const DoFHandler<dim, dim> &dof_handler,
const std::function<void(const unsigned int,
LinearAlgebra::distributed::Vector<Number> &)>
&initialize_dof_vector);

/**
* Prolongate a vector from level <tt>to_level-1</tt> to level
* <tt>to_level</tt> using the embedding matrices of the underlying finite
Expand Down
54 changes: 39 additions & 15 deletions source/multigrid/mg_transfer_matrix_free.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,8 @@ DEAL_II_NAMESPACE_OPEN


template <int dim, typename Number>
MGTransferMatrixFree<dim, Number>::MGTransferMatrixFree(
const std::function<void(const unsigned int,
LinearAlgebra::distributed::Vector<Number> &)>
&initialize_dof_vector)
: MGLevelGlobalTransfer<LinearAlgebra::distributed::Vector<Number>>(
initialize_dof_vector)
, fe_degree(0)
MGTransferMatrixFree<dim, Number>::MGTransferMatrixFree()
: fe_degree(0)
, element_is_continuous(false)
, n_components(0)
, n_child_cell_dofs(0)
Expand All @@ -57,13 +52,8 @@ MGTransferMatrixFree<dim, Number>::MGTransferMatrixFree(

template <int dim, typename Number>
MGTransferMatrixFree<dim, Number>::MGTransferMatrixFree(
const MGConstrainedDoFs &mg_c,
const std::function<void(const unsigned int,
LinearAlgebra::distributed::Vector<Number> &)>
&initialize_dof_vector)
: MGLevelGlobalTransfer<LinearAlgebra::distributed::Vector<Number>>(
initialize_dof_vector)
, fe_degree(0)
const MGConstrainedDoFs &mg_c)
: fe_degree(0)
, element_is_continuous(false)
, n_components(0)
, n_child_cell_dofs(0)
Expand Down Expand Up @@ -103,6 +93,7 @@ MGTransferMatrixFree<dim, Number>::clear()
}



template <int dim, typename Number>
void
MGTransferMatrixFree<dim, Number>::build(
Expand All @@ -124,7 +115,7 @@ MGTransferMatrixFree<dim, Number>::build(
"A initialize_dof_vector function has already been registered in the constructor!"));

this->initialize_dof_vector =
[&external_partitioners](
[external_partitioners](
const unsigned int level,
LinearAlgebra::distributed::Vector<Number> &vec) {
vec.reinit(external_partitioners[level]);
Expand Down Expand Up @@ -210,6 +201,39 @@ MGTransferMatrixFree<dim, Number>::build(



template <int dim, typename Number>
void
MGTransferMatrixFree<dim, Number>::build(
const DoFHandler<dim, dim> &dof_handler,
const std::function<void(const unsigned int,
LinearAlgebra::distributed::Vector<Number> &)>
&initialize_dof_vector)
{
if (initialize_dof_vector)
{
const unsigned int n_levels =
dof_handler.get_triangulation().n_global_levels();

std::vector<std::shared_ptr<const Utilities::MPI::Partitioner>>
external_partitioners(n_levels);

for (unsigned int level = 0; level < n_levels; ++level)
{
LinearAlgebra::distributed::Vector<Number> vector;
initialize_dof_vector(level, vector);
external_partitioners[level] = vector.get_partitioner();
}

build(dof_handler, external_partitioners);
}
else
{
build(dof_handler);
}
}



template <int dim, typename Number>
void
MGTransferMatrixFree<dim, Number>::prolongate(
Expand Down
9 changes: 4 additions & 5 deletions tests/matrix_free/multigrid_dg_sip_01.cc
Original file line number Diff line number Diff line change
Expand Up @@ -596,12 +596,11 @@ do_test(const DoFHandler<dim> &dof, const unsigned n_q_points_1d)
mg_constrained_dofs.initialize(dof);
mg_constrained_dofs.make_zero_boundary_constraints(dof, {0});

MGTransferMatrixFree<dim, number> mg_transfer(
mg_constrained_dofs, [&](const unsigned int level, auto &vec) {
mg_matrices[level].initialize_dof_vector(vec);
});
MGTransferMatrixFree<dim, number> mg_transfer(mg_constrained_dofs);

mg_transfer.build(dof);
mg_transfer.build(dof, [&](const unsigned int level, auto &vec) {
mg_matrices[level].initialize_dof_vector(vec);
});

mg::Matrix<LinearAlgebra::distributed::Vector<double>> mg_matrix(mg_matrices);

Expand Down

0 comments on commit eb4d4d5

Please sign in to comment.