From: Wolfgang Bangerth Date: Fri, 22 Sep 2023 14:23:22 +0000 (-0600) Subject: Avoid deprecated AffineConstraints functions in the tutorials. X-Git-Tag: relicensing~475^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cd49087796e01305723215561e6949102fa35a70;p=dealii.git Avoid deprecated AffineConstraints functions in the tutorials. --- diff --git a/examples/step-16/step-16.cc b/examples/step-16/step-16.cc index 2002e66b56..09c4302d7d 100644 --- a/examples/step-16/step-16.cc +++ b/examples/step-16/step-16.cc @@ -433,9 +433,9 @@ namespace Step16 std::vector> boundary_constraints(n_levels); for (unsigned int level = 0; level < n_levels; ++level) { - const IndexSet dofset = - DoFTools::extract_locally_relevant_level_dofs(dof_handler, level); - boundary_constraints[level].reinit(dofset); + boundary_constraints[level].reinit( + dof_handler.locally_owned_mg_dofs(level), + DoFTools::extract_locally_relevant_level_dofs(dof_handler, level)); boundary_constraints[level].add_lines( mg_constrained_dofs.get_refinement_edge_indices(level)); boundary_constraints[level].add_lines( diff --git a/examples/step-32/step-32.cc b/examples/step-32/step-32.cc index f193a5a56c..4ff165b3c1 100644 --- a/examples/step-32/step-32.cc +++ b/examples/step-32/step-32.cc @@ -1852,26 +1852,28 @@ namespace Step32 // of each matrix or vector will be stored where, then call the functions // that actually set up the matrices, and at the end also resize the // various vectors we keep around in this program. - std::vector stokes_partitioning, stokes_relevant_partitioning; - IndexSet temperature_partitioning(n_T), - temperature_relevant_partitioning(n_T); - IndexSet stokes_relevant_set; - { - IndexSet stokes_index_set = stokes_dof_handler.locally_owned_dofs(); - stokes_partitioning.push_back(stokes_index_set.get_view(0, n_u)); - stokes_partitioning.push_back(stokes_index_set.get_view(n_u, n_u + n_p)); - - stokes_relevant_set = - DoFTools::extract_locally_relevant_dofs(stokes_dof_handler); - stokes_relevant_partitioning.push_back( - stokes_relevant_set.get_view(0, n_u)); - stokes_relevant_partitioning.push_back( - stokes_relevant_set.get_view(n_u, n_u + n_p)); - - temperature_partitioning = temperature_dof_handler.locally_owned_dofs(); - temperature_relevant_partitioning = - DoFTools::extract_locally_relevant_dofs(temperature_dof_handler); - } + + const IndexSet &stokes_locally_owned_index_set = + stokes_dof_handler.locally_owned_dofs(); + const IndexSet stokes_locally_relevant_set = + DoFTools::extract_locally_relevant_dofs(stokes_dof_handler); + + std::vector stokes_partitioning; + stokes_partitioning.push_back( + stokes_locally_owned_index_set.get_view(0, n_u)); + stokes_partitioning.push_back( + stokes_locally_owned_index_set.get_view(n_u, n_u + n_p)); + + std::vector stokes_relevant_partitioning; + stokes_relevant_partitioning.push_back( + stokes_locally_relevant_set.get_view(0, n_u)); + stokes_relevant_partitioning.push_back( + stokes_locally_relevant_set.get_view(n_u, n_u + n_p)); + + const IndexSet temperature_partitioning = + temperature_dof_handler.locally_owned_dofs(); + const IndexSet temperature_relevant_partitioning = + DoFTools::extract_locally_relevant_dofs(temperature_dof_handler); // Following this, we can compute constraints for the solution vectors, // including hanging node constraints and homogeneous and inhomogeneous @@ -1886,7 +1888,8 @@ namespace Step32 // objects. { stokes_constraints.clear(); - stokes_constraints.reinit(stokes_relevant_set); + stokes_constraints.reinit(stokes_locally_owned_index_set, + stokes_locally_relevant_set); DoFTools::make_hanging_node_constraints(stokes_dof_handler, stokes_constraints); @@ -1910,7 +1913,8 @@ namespace Step32 } { temperature_constraints.clear(); - temperature_constraints.reinit(temperature_relevant_partitioning); + temperature_constraints.reinit(temperature_partitioning, + temperature_relevant_partitioning); DoFTools::make_hanging_node_constraints(temperature_dof_handler, temperature_constraints); diff --git a/examples/step-37/step-37.cc b/examples/step-37/step-37.cc index 2c5a38a832..2b2c089b69 100644 --- a/examples/step-37/step-37.cc +++ b/examples/step-37/step-37.cc @@ -780,51 +780,50 @@ namespace Step37 { Timer time; setup_time = 0; + { + system_matrix.clear(); + mg_matrices.clear_elements(); - system_matrix.clear(); - mg_matrices.clear_elements(); - - dof_handler.distribute_dofs(fe); - dof_handler.distribute_mg_dofs(); - - pcout << "Number of degrees of freedom: " << dof_handler.n_dofs() - << std::endl; + dof_handler.distribute_dofs(fe); + dof_handler.distribute_mg_dofs(); - const IndexSet locally_relevant_dofs = - DoFTools::extract_locally_relevant_dofs(dof_handler); + pcout << "Number of degrees of freedom: " << dof_handler.n_dofs() + << std::endl; - constraints.clear(); - constraints.reinit(locally_relevant_dofs); - DoFTools::make_hanging_node_constraints(dof_handler, constraints); - VectorTools::interpolate_boundary_values( - mapping, dof_handler, 0, Functions::ZeroFunction(), constraints); - constraints.close(); + constraints.clear(); + constraints.reinit(dof_handler.locally_owned_dofs(), + DoFTools::extract_locally_relevant_dofs(dof_handler)); + DoFTools::make_hanging_node_constraints(dof_handler, constraints); + VectorTools::interpolate_boundary_values( + mapping, dof_handler, 0, Functions::ZeroFunction(), constraints); + constraints.close(); + } setup_time += time.wall_time(); time_details << "Distribute DoFs & B.C. (CPU/wall) " << time.cpu_time() << "s/" << time.wall_time() << 's' << std::endl; time.restart(); - { - typename MatrixFree::AdditionalData additional_data; - additional_data.tasks_parallel_scheme = - MatrixFree::AdditionalData::none; - additional_data.mapping_update_flags = - (update_gradients | update_JxW_values | update_quadrature_points); - std::shared_ptr> system_mf_storage( - new MatrixFree()); - system_mf_storage->reinit(mapping, - dof_handler, - constraints, - QGauss<1>(fe.degree + 1), - additional_data); - system_matrix.initialize(system_mf_storage); - } - - system_matrix.evaluate_coefficient(Coefficient()); + { + typename MatrixFree::AdditionalData additional_data; + additional_data.tasks_parallel_scheme = + MatrixFree::AdditionalData::none; + additional_data.mapping_update_flags = + (update_gradients | update_JxW_values | update_quadrature_points); + std::shared_ptr> system_mf_storage( + new MatrixFree()); + system_mf_storage->reinit(mapping, + dof_handler, + constraints, + QGauss<1>(fe.degree + 1), + additional_data); + system_matrix.initialize(system_mf_storage); + } - system_matrix.initialize_dof_vector(solution); - system_matrix.initialize_dof_vector(system_rhs); + system_matrix.evaluate_coefficient(Coefficient()); + system_matrix.initialize_dof_vector(solution); + system_matrix.initialize_dof_vector(system_rhs); + } setup_time += time.wall_time(); time_details << "Setup matrix-free system (CPU/wall) " << time.cpu_time() << "s/" << time.wall_time() << 's' << std::endl; @@ -839,43 +838,44 @@ namespace Step37 // closely the construction of the system matrix on the original mesh, // except the slight difference in naming when accessing information on // the levels rather than the active cells. - const unsigned int nlevels = triangulation.n_global_levels(); - mg_matrices.resize(0, nlevels - 1); - - const std::set dirichlet_boundary_ids = {0}; - mg_constrained_dofs.initialize(dof_handler); - mg_constrained_dofs.make_zero_boundary_constraints(dof_handler, - dirichlet_boundary_ids); - - for (unsigned int level = 0; level < nlevels; ++level) - { - const IndexSet relevant_dofs = - DoFTools::extract_locally_relevant_level_dofs(dof_handler, level); - AffineConstraints level_constraints; - level_constraints.reinit(relevant_dofs); - level_constraints.add_lines( - mg_constrained_dofs.get_boundary_indices(level)); - level_constraints.close(); - - typename MatrixFree::AdditionalData additional_data; - additional_data.tasks_parallel_scheme = - MatrixFree::AdditionalData::none; - additional_data.mapping_update_flags = - (update_gradients | update_JxW_values | update_quadrature_points); - additional_data.mg_level = level; - std::shared_ptr> mg_mf_storage_level( - new MatrixFree()); - mg_mf_storage_level->reinit(mapping, - dof_handler, - level_constraints, - QGauss<1>(fe.degree + 1), - additional_data); - - mg_matrices[level].initialize(mg_mf_storage_level, - mg_constrained_dofs, - level); - mg_matrices[level].evaluate_coefficient(Coefficient()); - } + { + const unsigned int nlevels = triangulation.n_global_levels(); + mg_matrices.resize(0, nlevels - 1); + + const std::set dirichlet_boundary_ids = {0}; + mg_constrained_dofs.initialize(dof_handler); + mg_constrained_dofs.make_zero_boundary_constraints( + dof_handler, dirichlet_boundary_ids); + + for (unsigned int level = 0; level < nlevels; ++level) + { + AffineConstraints level_constraints( + dof_handler.locally_owned_mg_dofs(level), + DoFTools::extract_locally_relevant_level_dofs(dof_handler, level)); + level_constraints.add_lines( + mg_constrained_dofs.get_boundary_indices(level)); + level_constraints.close(); + + typename MatrixFree::AdditionalData additional_data; + additional_data.tasks_parallel_scheme = + MatrixFree::AdditionalData::none; + additional_data.mapping_update_flags = + (update_gradients | update_JxW_values | update_quadrature_points); + additional_data.mg_level = level; + std::shared_ptr> mg_mf_storage_level( + new MatrixFree()); + mg_mf_storage_level->reinit(mapping, + dof_handler, + level_constraints, + QGauss<1>(fe.degree + 1), + additional_data); + + mg_matrices[level].initialize(mg_mf_storage_level, + mg_constrained_dofs, + level); + mg_matrices[level].evaluate_coefficient(Coefficient()); + } + } setup_time += time.wall_time(); time_details << "Setup matrix-free levels (CPU/wall) " << time.cpu_time() << "s/" << time.wall_time() << 's' << std::endl; diff --git a/examples/step-40/step-40.cc b/examples/step-40/step-40.cc index b30e3ced21..dfce3d21a0 100644 --- a/examples/step-40/step-40.cc +++ b/examples/step-40/step-40.cc @@ -282,15 +282,23 @@ namespace Step40 // expect any information to store. In our case, as explained in the // @ref distributed module, the degrees of freedom we need to care about on // each processor are the locally relevant ones, so we pass this to the - // AffineConstraints::reinit function. As a side note, if you forget to - // pass this argument, the AffineConstraints class will allocate an array + // AffineConstraints::reinit() function as a second argument. A further + // optimization, AffineConstraint can avoid certain operations if you also + // provide it with the set of locally owned degrees of freedom -- the + // first argument to AffineConstraints::reinit(). + // + // (What would happen if we didn't pass this information to + // AffineConstraints, for example if we called the argument-less version of + // AffineConstraints::reinit() typically used in non-parallel codes? In that + // case, the AffineConstraints class will allocate an array // with length equal to the largest DoF index it has seen so far. For - // processors with high MPI process number, this may be very large -- + // processors with large numbers of MPI processes, this may be very large -- // maybe on the order of billions. The program would then allocate more // memory than for likely all other operations combined for this single - // array. + // array. Fortunately, recent versions of deal.II would trigger an assertion + // that tells you that this is considered a bug.) constraints.clear(); - constraints.reinit(locally_relevant_dofs); + constraints.reinit(locally_owned_dofs, locally_relevant_dofs); DoFTools::make_hanging_node_constraints(dof_handler, constraints); VectorTools::interpolate_boundary_values(dof_handler, 0, diff --git a/examples/step-42/step-42.cc b/examples/step-42/step-42.cc index 8246694fdc..3fcd08064d 100644 --- a/examples/step-42/step-42.cc +++ b/examples/step-42/step-42.cc @@ -979,7 +979,8 @@ namespace Step42 /* setup hanging nodes and Dirichlet constraints */ { TimerOutput::Scope t(computing_timer, "Setup: constraints"); - constraints_hanging_nodes.reinit(locally_relevant_dofs); + constraints_hanging_nodes.reinit(locally_owned_dofs, + locally_relevant_dofs); DoFTools::make_hanging_node_constraints(dof_handler, constraints_hanging_nodes); constraints_hanging_nodes.close(); @@ -1059,7 +1060,8 @@ namespace Step42 template void PlasticityContactProblem::compute_dirichlet_constraints() { - constraints_dirichlet_and_hanging_nodes.reinit(locally_relevant_dofs); + constraints_dirichlet_and_hanging_nodes.reinit(locally_owned_dofs, + locally_relevant_dofs); constraints_dirichlet_and_hanging_nodes.merge(constraints_hanging_nodes); if (base_mesh == "box") @@ -1197,7 +1199,7 @@ namespace Step42 diag_mass_matrix_vector_relevant = diag_mass_matrix_vector; - all_constraints.reinit(locally_relevant_dofs); + all_constraints.reinit(locally_owned_dofs, locally_relevant_dofs); active_set.clear(); // The second part is a loop over all cells in which we look at each diff --git a/examples/step-45/step-45.cc b/examples/step-45/step-45.cc index 69120c2149..7acdfa3f9f 100644 --- a/examples/step-45/step-45.cc +++ b/examples/step-45/step-45.cc @@ -366,7 +366,7 @@ namespace Step45 { owned_partitioning.clear(); - IndexSet locally_owned_dofs = dof_handler.locally_owned_dofs(); + const IndexSet &locally_owned_dofs = dof_handler.locally_owned_dofs(); owned_partitioning.push_back(locally_owned_dofs.get_view(0, n_u)); owned_partitioning.push_back(locally_owned_dofs.get_view(n_u, n_u + n_p)); @@ -378,7 +378,7 @@ namespace Step45 locally_relevant_dofs.get_view(n_u, n_u + n_p)); constraints.clear(); - constraints.reinit(locally_relevant_dofs); + constraints.reinit(locally_owned_dofs, locally_relevant_dofs); const FEValuesExtractors::Vector velocities(0); diff --git a/examples/step-48/step-48.cc b/examples/step-48/step-48.cc index ea993a5800..7d6a7e8609 100644 --- a/examples/step-48/step-48.cc +++ b/examples/step-48/step-48.cc @@ -409,9 +409,9 @@ namespace Step48 // We generate hanging node constraints for ensuring continuity of the // solution. As in step-40, we need to equip the constraint matrix with - // the IndexSet of locally relevant degrees of freedom to avoid it to - // consume too much memory for big problems. Next, the MatrixFree - // object for the problem is set up. Note that we specify a + // the IndexSet of locally active and locally relevant degrees of freedom + // to avoid it consuming too much memory for big problems. Next, the + // MatrixFree object for the problem is set up. Note that we specify a // particular scheme for shared-memory parallelization (hence one would // use multithreading for intra-node parallelism and not MPI; we here // choose the standard option — if we wanted to disable shared @@ -428,7 +428,7 @@ namespace Step48 locally_relevant_dofs = DoFTools::extract_locally_relevant_dofs(dof_handler); constraints.clear(); - constraints.reinit(locally_relevant_dofs); + constraints.reinit(dof_handler.locally_owned_dofs(), locally_relevant_dofs); DoFTools::make_hanging_node_constraints(dof_handler, constraints); constraints.close(); diff --git a/examples/step-50/step-50.cc b/examples/step-50/step-50.cc index 032663879b..6492d805cf 100644 --- a/examples/step-50/step-50.cc +++ b/examples/step-50/step-50.cc @@ -490,7 +490,7 @@ void LaplaceProblem::setup_system() solution.reinit(locally_owned_dofs, mpi_communicator); right_hand_side.reinit(locally_owned_dofs, mpi_communicator); - constraints.reinit(locally_relevant_dofs); + constraints.reinit(locally_owned_dofs, locally_relevant_dofs); DoFTools::make_hanging_node_constraints(dof_handler, constraints); VectorTools::interpolate_boundary_values( @@ -592,11 +592,10 @@ void LaplaceProblem::setup_multigrid() for (unsigned int level = 0; level < n_levels; ++level) { - const IndexSet relevant_dofs = + AffineConstraints level_constraints( + dof_handler.locally_owned_mg_dofs(level), DoFTools::extract_locally_relevant_level_dofs(dof_handler, - level); - AffineConstraints level_constraints; - level_constraints.reinit(relevant_dofs); + level)); level_constraints.add_lines( mg_constrained_dofs.get_boundary_indices(level)); level_constraints.close(); @@ -824,9 +823,9 @@ void LaplaceProblem::assemble_multigrid() triangulation.n_global_levels()); for (unsigned int level = 0; level < triangulation.n_global_levels(); ++level) { - const IndexSet dof_set = - DoFTools::extract_locally_relevant_level_dofs(dof_handler, level); - boundary_constraints[level].reinit(dof_set); + boundary_constraints[level].reinit( + dof_handler.locally_owned_mg_dofs(level), + DoFTools::extract_locally_relevant_level_dofs(dof_handler, level)); boundary_constraints[level].add_lines( mg_constrained_dofs.get_refinement_edge_indices(level)); boundary_constraints[level].add_lines( diff --git a/examples/step-55/step-55.cc b/examples/step-55/step-55.cc index 583cabf5f1..89d64d1839 100644 --- a/examples/step-55/step-55.cc +++ b/examples/step-55/step-55.cc @@ -382,10 +382,10 @@ namespace Step55 // We split up the IndexSet for locally owned and locally relevant DoFs // into two IndexSets based on how we want to create the block matrices // and vectors. + const IndexSet &locally_owned_dofs = dof_handler.locally_owned_dofs(); owned_partitioning.resize(2); - owned_partitioning[0] = dof_handler.locally_owned_dofs().get_view(0, n_u); - owned_partitioning[1] = - dof_handler.locally_owned_dofs().get_view(n_u, n_u + n_p); + owned_partitioning[0] = locally_owned_dofs.get_view(0, n_u); + owned_partitioning[1] = locally_owned_dofs.get_view(n_u, n_u + n_p); const IndexSet locally_relevant_dofs = DoFTools::extract_locally_relevant_dofs(dof_handler); @@ -399,7 +399,7 @@ namespace Step55 // to put this function call in, in case adaptive refinement gets // introduced later. { - constraints.reinit(locally_relevant_dofs); + constraints.reinit(locally_owned_dofs, locally_relevant_dofs); const FEValuesExtractors::Vector velocities(0); DoFTools::make_hanging_node_constraints(dof_handler, constraints); diff --git a/examples/step-63/step-63.cc b/examples/step-63/step-63.cc index 16bbfb689f..bfead4ce5f 100644 --- a/examples/step-63/step-63.cc +++ b/examples/step-63/step-63.cc @@ -814,9 +814,9 @@ namespace Step63 for (unsigned int level = 0; level < triangulation.n_global_levels(); ++level) { - const IndexSet locally_owned_level_dof_indices = - DoFTools::extract_locally_relevant_level_dofs(dof_handler, level); - boundary_constraints[level].reinit(locally_owned_level_dof_indices); + boundary_constraints[level].reinit( + dof_handler.locally_owned_mg_dofs(level), + DoFTools::extract_locally_relevant_level_dofs(dof_handler, level)); boundary_constraints[level].add_lines( mg_constrained_dofs.get_refinement_edge_indices(level)); boundary_constraints[level].add_lines( diff --git a/examples/step-64/step-64.cc b/examples/step-64/step-64.cc index 8466cdca28..5b14e2b24c 100644 --- a/examples/step-64/step-64.cc +++ b/examples/step-64/step-64.cc @@ -415,7 +415,7 @@ namespace Step64 system_rhs_dev.reinit(locally_owned_dofs, mpi_communicator); constraints.clear(); - constraints.reinit(locally_relevant_dofs); + constraints.reinit(locally_owned_dofs, locally_relevant_dofs); DoFTools::make_hanging_node_constraints(dof_handler, constraints); VectorTools::interpolate_boundary_values(dof_handler, 0, diff --git a/examples/step-66/step-66.cc b/examples/step-66/step-66.cc index e407742ed4..d5c2585063 100644 --- a/examples/step-66/step-66.cc +++ b/examples/step-66/step-66.cc @@ -544,11 +544,9 @@ namespace Step66 dof_handler.distribute_dofs(fe); dof_handler.distribute_mg_dofs(); - const IndexSet locally_relevant_dofs = - DoFTools::extract_locally_relevant_dofs(dof_handler); - constraints.clear(); - constraints.reinit(locally_relevant_dofs); + constraints.reinit(dof_handler.locally_owned_dofs(), + DoFTools::extract_locally_relevant_dofs(dof_handler)); DoFTools::make_hanging_node_constraints(dof_handler, constraints); VectorTools::interpolate_boundary_values(dof_handler, 0, @@ -592,11 +590,9 @@ namespace Step66 for (unsigned int level = 0; level < nlevels; ++level) { - const IndexSet relevant_dofs = - DoFTools::extract_locally_relevant_level_dofs(dof_handler, level); - - AffineConstraints level_constraints; - level_constraints.reinit(relevant_dofs); + AffineConstraints level_constraints( + dof_handler.locally_owned_mg_dofs(level), + DoFTools::extract_locally_relevant_level_dofs(dof_handler, level)); level_constraints.add_lines( mg_constrained_dofs.get_boundary_indices(level)); level_constraints.close(); diff --git a/examples/step-70/step-70.cc b/examples/step-70/step-70.cc index 5e494ea190..82904e58fe 100644 --- a/examples/step-70/step-70.cc +++ b/examples/step-70/step-70.cc @@ -1250,7 +1250,7 @@ namespace Step70 fluid_relevant_dofs[1] = locally_relevant_dofs.get_view(n_u, n_u + n_p); { - constraints.reinit(locally_relevant_dofs); + constraints.reinit(fluid_dh.locally_owned_dofs(), locally_relevant_dofs); const FEValuesExtractors::Vector velocities(0); DoFTools::make_hanging_node_constraints(fluid_dh, constraints); @@ -1263,7 +1263,7 @@ namespace Step70 constraints.close(); } - auto locally_owned_dofs_per_processor = + const auto locally_owned_dofs_per_processor = Utilities::MPI::all_gather(mpi_communicator, fluid_dh.locally_owned_dofs()); { diff --git a/examples/step-75/step-75.cc b/examples/step-75/step-75.cc index 7ff8fda19f..417819f0e9 100644 --- a/examples/step-75/step-75.cc +++ b/examples/step-75/step-75.cc @@ -325,11 +325,9 @@ namespace Step75 // operator is applied to a vector with only the Dirichlet values set. The // result is the negative right-hand-side vector. { - AffineConstraints constraints_without_dbc; - - const IndexSet locally_relevant_dofs = - DoFTools::extract_locally_relevant_dofs(dof_handler); - constraints_without_dbc.reinit(locally_relevant_dofs); + AffineConstraints constraints_without_dbc( + dof_handler.locally_owned_dofs(), + DoFTools::extract_locally_relevant_dofs(dof_handler)); DoFTools::make_hanging_node_constraints(dof_handler, constraints_without_dbc); @@ -779,9 +777,8 @@ namespace Step75 const auto &dof_handler = dof_handlers[level]; auto &constraint = constraints[level]; - const IndexSet locally_relevant_dofs = - DoFTools::extract_locally_relevant_dofs(dof_handler); - constraint.reinit(locally_relevant_dofs); + constraint.reinit(dof_handler.locally_owned_dofs(), + DoFTools::extract_locally_relevant_dofs(dof_handler)); DoFTools::make_hanging_node_constraints(dof_handler, constraint); VectorTools::interpolate_boundary_values(mapping_collection, @@ -1117,7 +1114,7 @@ namespace Step75 system_rhs.reinit(locally_owned_dofs, mpi_communicator); constraints.clear(); - constraints.reinit(locally_relevant_dofs); + constraints.reinit(locally_owned_dofs, locally_relevant_dofs); DoFTools::make_hanging_node_constraints(dof_handler, constraints); VectorTools::interpolate_boundary_values( mapping_collection, dof_handler, 0, Solution(), constraints);