From b7e135eda0deb3f8e19bf1fab79a08e788b23dcf Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Wed, 22 Dec 2021 19:11:55 -0700 Subject: [PATCH] Simplify initialization of block objects in the tutorial. --- examples/step-22/step-22.cc | 32 +++++------------------------ examples/step-31/step-31.cc | 19 +++-------------- examples/step-43/step-43.cc | 32 +++++++---------------------- examples/step-44/step-44.cc | 22 +------------------- examples/step-79/step-79.cc | 6 +----- source/lac/trilinos_block_vector.cc | 3 +++ 6 files changed, 20 insertions(+), 94 deletions(-) diff --git a/examples/step-22/step-22.cc b/examples/step-22/step-22.cc index 1ab4c866c7..c24fa9e096 100644 --- a/examples/step-22/step-22.cc +++ b/examples/step-22/step-22.cc @@ -535,17 +535,9 @@ namespace Step22 // dsp will be released once the information has been copied to // sparsity_pattern. { - BlockDynamicSparsityPattern dsp(2, 2); - - dsp.block(0, 0).reinit(n_u, n_u); - dsp.block(1, 0).reinit(n_p, n_u); - dsp.block(0, 1).reinit(n_u, n_p); - dsp.block(1, 1).reinit(n_p, n_p); - - dsp.collect_sizes(); + BlockDynamicSparsityPattern dsp(dofs_per_block, dofs_per_block); Table<2, DoFTools::Coupling> coupling(dim + 1, dim + 1); - for (unsigned int c = 0; c < dim + 1; ++c) for (unsigned int d = 0; d < dim + 1; ++d) if (!((c == dim) && (d == dim))) @@ -560,17 +552,10 @@ namespace Step22 } { - BlockDynamicSparsityPattern preconditioner_dsp(2, 2); - - preconditioner_dsp.block(0, 0).reinit(n_u, n_u); - preconditioner_dsp.block(1, 0).reinit(n_p, n_u); - preconditioner_dsp.block(0, 1).reinit(n_u, n_p); - preconditioner_dsp.block(1, 1).reinit(n_p, n_p); - - preconditioner_dsp.collect_sizes(); + BlockDynamicSparsityPattern preconditioner_dsp(dofs_per_block, + dofs_per_block); Table<2, DoFTools::Coupling> preconditioner_coupling(dim + 1, dim + 1); - for (unsigned int c = 0; c < dim + 1; ++c) for (unsigned int d = 0; d < dim + 1; ++d) if (((c == dim) && (d == dim))) @@ -593,15 +578,8 @@ namespace Step22 system_matrix.reinit(sparsity_pattern); preconditioner_matrix.reinit(preconditioner_sparsity_pattern); - solution.reinit(2); - solution.block(0).reinit(n_u); - solution.block(1).reinit(n_p); - solution.collect_sizes(); - - system_rhs.reinit(2); - system_rhs.block(0).reinit(n_u); - system_rhs.block(1).reinit(n_p); - system_rhs.collect_sizes(); + solution.reinit(dofs_per_block); + system_rhs.reinit(dofs_per_block); } diff --git a/examples/step-31/step-31.cc b/examples/step-31/step-31.cc index 7d6463e33c..380e918519 100644 --- a/examples/step-31/step-31.cc +++ b/examples/step-31/step-31.cc @@ -870,6 +870,7 @@ namespace Step31 const unsigned int n_u = stokes_dofs_per_block[0], n_p = stokes_dofs_per_block[1], n_T = temperature_dof_handler.n_dofs(); + const std::vector stokes_block_sizes = {n_u, n_p}; std::cout << "Number of active cells: " << triangulation.n_active_cells() << " (on " << triangulation.n_levels() << " levels)" << std::endl @@ -919,14 +920,7 @@ namespace Step31 { stokes_matrix.clear(); - BlockDynamicSparsityPattern dsp(2, 2); - - dsp.block(0, 0).reinit(n_u, n_u); - dsp.block(0, 1).reinit(n_u, n_p); - dsp.block(1, 0).reinit(n_p, n_u); - dsp.block(1, 1).reinit(n_p, n_p); - - dsp.collect_sizes(); + BlockDynamicSparsityPattern dsp(stokes_block_sizes, stokes_block_sizes); Table<2, DoFTools::Coupling> coupling(dim + 1, dim + 1); @@ -948,14 +942,7 @@ namespace Step31 Mp_preconditioner.reset(); stokes_preconditioner_matrix.clear(); - BlockDynamicSparsityPattern dsp(2, 2); - - dsp.block(0, 0).reinit(n_u, n_u); - dsp.block(0, 1).reinit(n_u, n_p); - dsp.block(1, 0).reinit(n_p, n_u); - dsp.block(1, 1).reinit(n_p, n_p); - - dsp.collect_sizes(); + BlockDynamicSparsityPattern dsp(stokes_block_sizes, stokes_block_sizes); Table<2, DoFTools::Coupling> coupling(dim + 1, dim + 1); for (unsigned int c = 0; c < dim + 1; ++c) diff --git a/examples/step-43/step-43.cc b/examples/step-43/step-43.cc index 7af90f333e..e29f03443c 100644 --- a/examples/step-43/step-43.cc +++ b/examples/step-43/step-43.cc @@ -695,6 +695,7 @@ namespace Step43 const unsigned int n_u = darcy_dofs_per_block[0], n_p = darcy_dofs_per_block[1], n_s = saturation_dof_handler.n_dofs(); + const std::vector darcy_block_sizes = {n_u, n_p}; std::cout << "Number of active cells: " << triangulation.n_active_cells() << " (on " << triangulation.n_levels() << " levels)" << std::endl @@ -705,17 +706,9 @@ namespace Step43 { darcy_matrix.clear(); - BlockDynamicSparsityPattern dsp(2, 2); - - dsp.block(0, 0).reinit(n_u, n_u); - dsp.block(0, 1).reinit(n_u, n_p); - dsp.block(1, 0).reinit(n_p, n_u); - dsp.block(1, 1).reinit(n_p, n_p); - - dsp.collect_sizes(); + BlockDynamicSparsityPattern dsp(darcy_block_sizes, darcy_block_sizes); Table<2, DoFTools::Coupling> coupling(dim + 1, dim + 1); - for (unsigned int c = 0; c < dim + 1; ++c) for (unsigned int d = 0; d < dim + 1; ++d) if (!((c == dim) && (d == dim))) @@ -735,14 +728,7 @@ namespace Step43 Mp_preconditioner.reset(); darcy_preconditioner_matrix.clear(); - BlockDynamicSparsityPattern dsp(2, 2); - - dsp.block(0, 0).reinit(n_u, n_u); - dsp.block(0, 1).reinit(n_u, n_p); - dsp.block(1, 0).reinit(n_p, n_u); - dsp.block(1, 1).reinit(n_p, n_p); - - dsp.collect_sizes(); + BlockDynamicSparsityPattern dsp(darcy_block_sizes, darcy_block_sizes); Table<2, DoFTools::Coupling> coupling(dim + 1, dim + 1); for (unsigned int c = 0; c < dim + 1; ++c) @@ -773,23 +759,19 @@ namespace Step43 saturation_matrix.reinit(dsp); } - std::vector darcy_partitioning(2); - darcy_partitioning[0] = complete_index_set(n_u); - darcy_partitioning[1] = complete_index_set(n_p); + const std::vector darcy_partitioning = {complete_index_set(n_u), + complete_index_set(n_p)}; + darcy_solution.reinit(darcy_partitioning, MPI_COMM_WORLD); - darcy_solution.collect_sizes(); last_computed_darcy_solution.reinit(darcy_partitioning, MPI_COMM_WORLD); - last_computed_darcy_solution.collect_sizes(); second_last_computed_darcy_solution.reinit(darcy_partitioning, MPI_COMM_WORLD); - second_last_computed_darcy_solution.collect_sizes(); darcy_rhs.reinit(darcy_partitioning, MPI_COMM_WORLD); - darcy_rhs.collect_sizes(); - IndexSet saturation_partitioning = complete_index_set(n_s); + const IndexSet saturation_partitioning = complete_index_set(n_s); saturation_solution.reinit(saturation_partitioning, MPI_COMM_WORLD); old_saturation_solution.reinit(saturation_partitioning, MPI_COMM_WORLD); old_old_saturation_solution.reinit(saturation_partitioning, MPI_COMM_WORLD); diff --git a/examples/step-44/step-44.cc b/examples/step-44/step-44.cc index 0a5e7c9e4a..082ddf6fed 100644 --- a/examples/step-44/step-44.cc +++ b/examples/step-44/step-44.cc @@ -1449,24 +1449,7 @@ namespace Step44 // Setup the sparsity pattern and tangent matrix tangent_matrix.clear(); { - const types::global_dof_index n_dofs_u = dofs_per_block[u_dof]; - const types::global_dof_index n_dofs_p = dofs_per_block[p_dof]; - const types::global_dof_index n_dofs_J = dofs_per_block[J_dof]; - - BlockDynamicSparsityPattern dsp(n_blocks, n_blocks); - - dsp.block(u_dof, u_dof).reinit(n_dofs_u, n_dofs_u); - dsp.block(u_dof, p_dof).reinit(n_dofs_u, n_dofs_p); - dsp.block(u_dof, J_dof).reinit(n_dofs_u, n_dofs_J); - - dsp.block(p_dof, u_dof).reinit(n_dofs_p, n_dofs_u); - dsp.block(p_dof, p_dof).reinit(n_dofs_p, n_dofs_p); - dsp.block(p_dof, J_dof).reinit(n_dofs_p, n_dofs_J); - - dsp.block(J_dof, u_dof).reinit(n_dofs_J, n_dofs_u); - dsp.block(J_dof, p_dof).reinit(n_dofs_J, n_dofs_p); - dsp.block(J_dof, J_dof).reinit(n_dofs_J, n_dofs_J); - dsp.collect_sizes(); + BlockDynamicSparsityPattern dsp(dofs_per_block, dofs_per_block); // The global system matrix initially has the following structure // @f{align*} @@ -1511,10 +1494,7 @@ namespace Step44 // We then set up storage vectors system_rhs.reinit(dofs_per_block); - system_rhs.collect_sizes(); - solution_n.reinit(dofs_per_block); - solution_n.collect_sizes(); // ...and finally set up the quadrature // point history: diff --git a/examples/step-79/step-79.cc b/examples/step-79/step-79.cc index 8018bd81cf..e1dd2e4dc5 100644 --- a/examples/step-79/step-79.cc +++ b/examples/step-79/step-79.cc @@ -441,11 +441,7 @@ namespace SAND const std::vector::size_type> block_sizes = { n_p, n_u, n_p, n_u, n_p, n_p, n_p, n_p, n_p}; - BlockDynamicSparsityPattern dsp(9, 9); - for (unsigned int k = 0; k < 9; ++k) - for (unsigned int j = 0; j < 9; ++j) - dsp.block(j, k).reinit(block_sizes[j], block_sizes[k]); - dsp.collect_sizes(); + BlockDynamicSparsityPattern dsp(block_sizes, block_sizes); // The bulk of the function is in setting up which of these diff --git a/source/lac/trilinos_block_vector.cc b/source/lac/trilinos_block_vector.cc index 628e5da397..25b25c1d62 100644 --- a/source/lac/trilinos_block_vector.cc +++ b/source/lac/trilinos_block_vector.cc @@ -91,6 +91,8 @@ namespace TrilinosWrappers collect_sizes(); } + + void BlockVector::reinit(const std::vector ¶llel_partitioning, const std::vector &ghost_values, @@ -119,6 +121,7 @@ namespace TrilinosWrappers } + void BlockVector::reinit(const BlockVector &v, const bool omit_zeroing_entries) { -- 2.39.5