From 56b3236ea300e0268d37d87353504a5885a9545c Mon Sep 17 00:00:00 2001 From: David Wells Date: Sat, 5 Nov 2022 10:12:37 -0400 Subject: [PATCH] multigrid tools: Remove template on sparsity pattern type. --- include/deal.II/multigrid/mg_tools.h | 34 +++--- source/multigrid/mg_tools.cc | 158 ++++++++++++++------------- source/multigrid/mg_tools.inst.in | 37 +++---- 3 files changed, 111 insertions(+), 118 deletions(-) diff --git a/include/deal.II/multigrid/mg_tools.h b/include/deal.II/multigrid/mg_tools.h index a78c53b32c..2ea75eacfb 100644 --- a/include/deal.II/multigrid/mg_tools.h +++ b/include/deal.II/multigrid/mg_tools.h @@ -76,8 +76,8 @@ namespace MGTools /** * Write the sparsity structure of the matrix belonging to the specified @p * level. The sparsity pattern is not compressed, so before creating the - * actual matrix you have to compress the matrix yourself, using - * SparsityPatternType::compress(). + * actual matrix you have to compress the matrix yourself, either using + * SparsityPattern::compress() or by copying to a SparsityPattern. * * The optional AffineConstraints argument allows to define constraints of * the level matrices like Dirichlet boundary conditions. Note that there is @@ -85,14 +85,11 @@ namespace MGTools * one level is considered. See DoFTools::make_sparsity_pattern() for more * details about the arguments. */ - template + template void make_sparsity_pattern( const DoFHandler &dof_handler, - SparsityPatternType & sparsity, + SparsityPatternBase & sparsity, const unsigned int level, const AffineConstraints &constraints = AffineConstraints(), const bool keep_constrained_dofs = true); @@ -105,14 +102,11 @@ namespace MGTools * and * @ref DoFTools */ - template + template void make_flux_sparsity_pattern( const DoFHandler &dof_handler, - SparsityPatternType & sparsity, + SparsityPatternBase & sparsity, const unsigned int level, const AffineConstraints &constraints = AffineConstraints(), const bool keep_constrained_dofs = true); @@ -124,10 +118,10 @@ namespace MGTools * * make_flux_sparsity_pattern() */ - template + template void make_flux_sparsity_pattern_edge(const DoFHandler &dof_handler, - SparsityPatternType & sparsity, + SparsityPatternBase & sparsity, const unsigned int level); /** * This function does the same as the other with the same name, but it gets @@ -138,10 +132,10 @@ namespace MGTools * There is one matrix for couplings in a cell and one for the couplings * occurring in fluxes. */ - template + template void make_flux_sparsity_pattern(const DoFHandler & dof, - SparsityPatternType & sparsity, + SparsityPatternBase & sparsity, const unsigned int level, const Table<2, DoFTools::Coupling> &int_mask, const Table<2, DoFTools::Coupling> &flux_mask); @@ -154,11 +148,11 @@ namespace MGTools * * make_flux_sparsity_pattern() */ - template + template void make_flux_sparsity_pattern_edge( const DoFHandler & dof_handler, - SparsityPatternType & sparsity, + SparsityPatternBase & sparsity, const unsigned int level, const Table<2, DoFTools::Coupling> &flux_mask); @@ -169,11 +163,11 @@ namespace MGTools * degrees of freedom on a refinement edge to those not on the refinement edge * of a certain level. */ - template + template void make_interface_sparsity_pattern(const DoFHandler &dof_handler, const MGConstrainedDoFs &mg_constrained_dofs, - SparsityPatternType & sparsity, + SparsityPatternBase & sparsity, const unsigned int level); diff --git a/source/multigrid/mg_tools.cc b/source/multigrid/mg_tools.cc index abc4b66614..568fb029ab 100644 --- a/source/multigrid/mg_tools.cc +++ b/source/multigrid/mg_tools.cc @@ -573,13 +573,10 @@ namespace MGTools - template + template void make_sparsity_pattern(const DoFHandler &dof, - SparsityPatternType & sparsity, + SparsityPatternBase & sparsity, const unsigned int level, const AffineConstraints &constraints, const bool keep_constrained_dofs) @@ -606,13 +603,10 @@ namespace MGTools - template + template void make_flux_sparsity_pattern(const DoFHandler &dof, - SparsityPatternType & sparsity, + SparsityPatternBase & sparsity, const unsigned int level, const AffineConstraints &constraints, const bool keep_constrained_dofs) @@ -683,10 +677,10 @@ namespace MGTools - template + template void make_flux_sparsity_pattern_edge(const DoFHandler &dof, - SparsityPatternType & sparsity, + SparsityPatternBase & sparsity, const unsigned int level) { Assert((level >= 1) && (level < dof.get_triangulation().n_global_levels()), @@ -733,16 +727,11 @@ namespace MGTools cell->neighbor_or_periodic_neighbor(face); neighbor->get_mg_dof_indices(dofs_on_other_cell); + std::sort(dofs_on_this_cell.begin(), dofs_on_this_cell.end()); for (unsigned int i = 0; i < dofs_per_cell; ++i) - { - for (unsigned int j = 0; j < dofs_per_cell; ++j) - { - sparsity.add(dofs_on_other_cell[i], - dofs_on_this_cell[j]); - sparsity.add(dofs_on_other_cell[j], - dofs_on_this_cell[i]); - } - } + sparsity.add_row_entries(dofs_on_other_cell[i], + make_array_view(dofs_on_this_cell), + true); } } } @@ -750,10 +739,10 @@ namespace MGTools - template + template void make_flux_sparsity_pattern(const DoFHandler & dof, - SparsityPatternType & sparsity, + SparsityPatternBase & sparsity, const unsigned int level, const Table<2, DoFTools::Coupling> &int_mask, const Table<2, DoFTools::Coupling> &flux_mask) @@ -780,7 +769,10 @@ namespace MGTools const unsigned int total_dofs = fe.n_dofs_per_cell(); std::vector dofs_on_this_cell(total_dofs); std::vector dofs_on_other_cell(total_dofs); - Table<2, bool> support_on_face(total_dofs, + std::vector> + cell_entries; + + Table<2, bool> support_on_face(total_dofs, GeometryInfo::faces_per_cell); const Table<2, DoFTools::Coupling> @@ -807,7 +799,8 @@ namespace MGTools for (unsigned int i = 0; i < total_dofs; ++i) for (unsigned int j = 0; j < total_dofs; ++j) if (int_dof_mask[i][j] != DoFTools::none) - sparsity.add(dofs_on_this_cell[i], dofs_on_this_cell[j]); + cell_entries.emplace_back(dofs_on_this_cell[i], + dofs_on_this_cell[j]); // Loop over all interior neighbors for (const unsigned int face : GeometryInfo::face_indices()) @@ -827,12 +820,12 @@ namespace MGTools const bool j_non_zero_i = support_on_face(j, face); if (flux_dof_mask(i, j) == DoFTools::always) - sparsity.add(dofs_on_this_cell[i], - dofs_on_this_cell[j]); + cell_entries.emplace_back(dofs_on_this_cell[i], + dofs_on_this_cell[j]); if (flux_dof_mask(i, j) == DoFTools::nonzero && i_non_zero_i && j_non_zero_i) - sparsity.add(dofs_on_this_cell[i], - dofs_on_this_cell[j]); + cell_entries.emplace_back(dofs_on_this_cell[i], + dofs_on_this_cell[j]); } } } @@ -861,71 +854,73 @@ namespace MGTools support_on_face(j, neighbor_face); if (flux_dof_mask(i, j) == DoFTools::always) { - sparsity.add(dofs_on_this_cell[i], - dofs_on_other_cell[j]); - sparsity.add(dofs_on_other_cell[i], - dofs_on_this_cell[j]); - sparsity.add(dofs_on_this_cell[i], - dofs_on_this_cell[j]); - sparsity.add(dofs_on_other_cell[i], - dofs_on_other_cell[j]); + cell_entries.emplace_back(dofs_on_this_cell[i], + dofs_on_other_cell[j]); + cell_entries.emplace_back(dofs_on_other_cell[i], + dofs_on_this_cell[j]); + cell_entries.emplace_back(dofs_on_this_cell[i], + dofs_on_this_cell[j]); + cell_entries.emplace_back(dofs_on_other_cell[i], + dofs_on_other_cell[j]); } if (flux_dof_mask(i, j) == DoFTools::nonzero) { if (i_non_zero_i && j_non_zero_e) - sparsity.add(dofs_on_this_cell[i], - dofs_on_other_cell[j]); + cell_entries.emplace_back(dofs_on_this_cell[i], + dofs_on_other_cell[j]); if (i_non_zero_e && j_non_zero_i) - sparsity.add(dofs_on_other_cell[i], - dofs_on_this_cell[j]); + cell_entries.emplace_back(dofs_on_other_cell[i], + dofs_on_this_cell[j]); if (i_non_zero_i && j_non_zero_i) - sparsity.add(dofs_on_this_cell[i], - dofs_on_this_cell[j]); + cell_entries.emplace_back(dofs_on_this_cell[i], + dofs_on_this_cell[j]); if (i_non_zero_e && j_non_zero_e) - sparsity.add(dofs_on_other_cell[i], - dofs_on_other_cell[j]); + cell_entries.emplace_back(dofs_on_other_cell[i], + dofs_on_other_cell[j]); } if (flux_dof_mask(j, i) == DoFTools::always) { - sparsity.add(dofs_on_this_cell[j], - dofs_on_other_cell[i]); - sparsity.add(dofs_on_other_cell[j], - dofs_on_this_cell[i]); - sparsity.add(dofs_on_this_cell[j], - dofs_on_this_cell[i]); - sparsity.add(dofs_on_other_cell[j], - dofs_on_other_cell[i]); + cell_entries.emplace_back(dofs_on_this_cell[j], + dofs_on_other_cell[i]); + cell_entries.emplace_back(dofs_on_other_cell[j], + dofs_on_this_cell[i]); + cell_entries.emplace_back(dofs_on_this_cell[j], + dofs_on_this_cell[i]); + cell_entries.emplace_back(dofs_on_other_cell[j], + dofs_on_other_cell[i]); } if (flux_dof_mask(j, i) == DoFTools::nonzero) { if (j_non_zero_i && i_non_zero_e) - sparsity.add(dofs_on_this_cell[j], - dofs_on_other_cell[i]); + cell_entries.emplace_back(dofs_on_this_cell[j], + dofs_on_other_cell[i]); if (j_non_zero_e && i_non_zero_i) - sparsity.add(dofs_on_other_cell[j], - dofs_on_this_cell[i]); + cell_entries.emplace_back(dofs_on_other_cell[j], + dofs_on_this_cell[i]); if (j_non_zero_i && i_non_zero_i) - sparsity.add(dofs_on_this_cell[j], - dofs_on_this_cell[i]); + cell_entries.emplace_back(dofs_on_this_cell[j], + dofs_on_this_cell[i]); if (j_non_zero_e && i_non_zero_e) - sparsity.add(dofs_on_other_cell[j], - dofs_on_other_cell[i]); + cell_entries.emplace_back(dofs_on_other_cell[j], + dofs_on_other_cell[i]); } } } face_touched[neighbor->face(neighbor_face)->index()] = true; } } + sparsity.add_entries(make_array_view(cell_entries)); + cell_entries.clear(); } } - template + template void make_flux_sparsity_pattern_edge(const DoFHandler & dof, - SparsityPatternType & sparsity, + SparsityPatternBase & sparsity, const unsigned int level, const Table<2, DoFTools::Coupling> &flux_mask) { @@ -955,7 +950,10 @@ namespace MGTools const unsigned int dofs_per_cell = dof.get_fe().n_dofs_per_cell(); std::vector dofs_on_this_cell(dofs_per_cell); std::vector dofs_on_other_cell(dofs_per_cell); - Table<2, bool> support_on_face(dofs_per_cell, + std::vector> + cell_entries; + + Table<2, bool> support_on_face(dofs_per_cell, GeometryInfo::faces_per_cell); const Table<2, DoFTools::Coupling> flux_dof_mask = @@ -997,25 +995,27 @@ namespace MGTools { if (flux_dof_mask(i, j) != DoFTools::none) { - sparsity.add(dofs_on_other_cell[i], - dofs_on_this_cell[j]); - sparsity.add(dofs_on_other_cell[j], - dofs_on_this_cell[i]); + cell_entries.emplace_back(dofs_on_other_cell[i], + dofs_on_this_cell[j]); + cell_entries.emplace_back(dofs_on_other_cell[j], + dofs_on_this_cell[i]); } } } } } + sparsity.add_entries(make_array_view(cell_entries)); + cell_entries.clear(); } } - template + template void make_interface_sparsity_pattern(const DoFHandler &dof, const MGConstrainedDoFs &mg_constrained_dofs, - SparsityPatternType & sparsity, + SparsityPatternBase & sparsity, const unsigned int level) { const types::global_dof_index n_dofs = dof.n_dofs(level); @@ -1028,15 +1028,25 @@ namespace MGTools const unsigned int dofs_per_cell = dof.get_fe().n_dofs_per_cell(); std::vector dofs_on_this_cell(dofs_per_cell); + std::vector cols; + cols.reserve(dofs_per_cell); + for (const auto &cell : dof.cell_iterators_on_level(level)) if (cell->is_locally_owned_on_level()) { cell->get_mg_dof_indices(dofs_on_this_cell); + std::sort(dofs_on_this_cell.begin(), dofs_on_this_cell.end()); for (unsigned int i = 0; i < dofs_per_cell; ++i) - for (unsigned int j = 0; j < dofs_per_cell; ++j) - if (mg_constrained_dofs.is_interface_matrix_entry( - level, dofs_on_this_cell[i], dofs_on_this_cell[j])) - sparsity.add(dofs_on_this_cell[i], dofs_on_this_cell[j]); + { + for (unsigned int j = 0; j < dofs_per_cell; ++j) + if (mg_constrained_dofs.is_interface_matrix_entry( + level, dofs_on_this_cell[i], dofs_on_this_cell[j])) + cols.push_back(dofs_on_this_cell[j]); + sparsity.add_row_entries(dofs_on_this_cell[i], + make_array_view(cols), + true); + cols.clear(); + } } } diff --git a/source/multigrid/mg_tools.inst.in b/source/multigrid/mg_tools.inst.in index b679e52ecb..3ef092a5c5 100644 --- a/source/multigrid/mg_tools.inst.in +++ b/source/multigrid/mg_tools.inst.in @@ -15,31 +15,24 @@ -for (PATTERN : SPARSITY_PATTERNS; deal_II_dimension : DIMENSIONS; - deal_II_space_dimension : SPACE_DIMENSIONS) +for (deal_II_dimension : DIMENSIONS; deal_II_space_dimension : SPACE_DIMENSIONS) { namespace MGTools \{ #if deal_II_dimension <= deal_II_space_dimension template void - make_sparsity_pattern( + make_sparsity_pattern( const DoFHandler &, - PATTERN &, + SparsityPatternBase &, const unsigned int, const AffineConstraints &, const bool); template void - make_sparsity_pattern( + make_sparsity_pattern( const DoFHandler &, - PATTERN &, + SparsityPatternBase &, const unsigned int, const AffineConstraints &, const bool); @@ -49,8 +42,7 @@ for (PATTERN : SPARSITY_PATTERNS; deal_II_dimension : DIMENSIONS; } -for (PATTERN : SPARSITY_PATTERNS; deal_II_dimension : DIMENSIONS; - deal_II_space_dimension : SPACE_DIMENSIONS) +for (deal_II_dimension : DIMENSIONS; deal_II_space_dimension : SPACE_DIMENSIONS) { namespace MGTools \{ @@ -58,11 +50,10 @@ for (PATTERN : SPARSITY_PATTERNS; deal_II_dimension : DIMENSIONS; #if deal_II_dimension <= deal_II_space_dimension template void make_interface_sparsity_pattern( + deal_II_space_dimension>( const DoFHandler &, const MGConstrainedDoFs &, - PATTERN &, + SparsityPatternBase &, const unsigned int); #endif @@ -70,9 +61,8 @@ for (PATTERN : SPARSITY_PATTERNS; deal_II_dimension : DIMENSIONS; template void make_flux_sparsity_pattern(const DoFHandler &, - PATTERN &, + SparsityPatternBase &, const unsigned int, const AffineConstraints &, const bool); @@ -80,9 +70,8 @@ for (PATTERN : SPARSITY_PATTERNS; deal_II_dimension : DIMENSIONS; template void make_flux_sparsity_pattern(const DoFHandler &, - PATTERN &, + SparsityPatternBase &, const unsigned int, const AffineConstraints &, const bool); @@ -90,7 +79,7 @@ for (PATTERN : SPARSITY_PATTERNS; deal_II_dimension : DIMENSIONS; template void make_flux_sparsity_pattern_edge( const DoFHandler &, - PATTERN &, + SparsityPatternBase &, const unsigned int); # if deal_II_dimension > 1 @@ -98,7 +87,7 @@ for (PATTERN : SPARSITY_PATTERNS; deal_II_dimension : DIMENSIONS; template void make_flux_sparsity_pattern( const DoFHandler &, - PATTERN &, + SparsityPatternBase &, const unsigned int, const Table<2, DoFTools::Coupling> &, const Table<2, DoFTools::Coupling> &); @@ -106,7 +95,7 @@ for (PATTERN : SPARSITY_PATTERNS; deal_II_dimension : DIMENSIONS; template void make_flux_sparsity_pattern_edge( const DoFHandler &, - PATTERN &, + SparsityPatternBase &, const unsigned int, const Table<2, DoFTools::Coupling> &); # endif -- 2.39.5