From 57f1c177dd2eabaa6341e1b00626c036f890b7c6 Mon Sep 17 00:00:00 2001 From: Martin Kronbichler Date: Mon, 1 Nov 2021 19:06:48 +0100 Subject: [PATCH] Apply some range-based for loops --- source/dofs/dof_tools.cc | 131 ++++++++---------------------- source/dofs/dof_tools_sparsity.cc | 35 ++------ 2 files changed, 41 insertions(+), 125 deletions(-) diff --git a/source/dofs/dof_tools.cc b/source/dofs/dof_tools.cc index c2bd778cae..7b6469bc52 100644 --- a/source/dofs/dof_tools.cc +++ b/source/dofs/dof_tools.cc @@ -347,13 +347,10 @@ namespace DoFTools // count how often we have added a value in the sum for each dof std::vector touch_count(dof_handler.n_dofs(), 0); - typename DoFHandler::active_cell_iterator - cell = dof_handler.begin_active(), - endc = dof_handler.end(); std::vector dof_indices; dof_indices.reserve(fe_collection.max_dofs_per_cell()); - for (unsigned int present_cell = 0; cell != endc; ++cell, ++present_cell) + for (const auto &cell : dof_handler.active_cell_iterators()) { const unsigned int dofs_per_cell = cell->get_fe().n_dofs_per_cell(); dof_indices.resize(dofs_per_cell); @@ -366,7 +363,7 @@ namespace DoFTools (cell->get_fe().system_to_component_index(i).first == component)) { // sum up contribution of the present_cell to this dof - dof_data(dof_indices[i]) += cell_data(present_cell); + dof_data(dof_indices[i]) += cell_data(cell->active_cell_index()); // note that we added another summand ++touch_count[dof_indices[i]]; @@ -521,10 +518,9 @@ namespace DoFTools // then loop over all cells and do work std::vector indices(fe.n_dofs_per_cell()); - typename DoFHandler::level_cell_iterator c; - for (c = dof.begin(level); c != dof.end(level); ++c) + for (const auto &cell : dof.cell_iterators_on_level(level)) { - c->get_mg_dof_indices(indices); + cell->get_mg_dof_indices(indices); for (unsigned int i = 0; i < fe.n_dofs_per_cell(); ++i) selected_dofs[indices[i]] = local_selected_dofs[i]; } @@ -822,10 +818,7 @@ namespace DoFTools // Get all the dofs that live on the subdomain: std::set predicate_dofs; - typename DoFHandler::active_cell_iterator - cell = dof_handler.begin_active(), - endc = dof_handler.end(); - for (; cell != endc; ++cell) + for (const auto &cell : dof_handler.active_cell_iterators()) if (!cell->is_artificial() && predicate(cell)) { local_dof_indices.resize(cell->get_fe().n_dofs_per_cell()); @@ -1041,10 +1034,7 @@ namespace DoFTools // this function is similar to the make_sparsity_pattern function, see // there for more information - typename DoFHandler::active_cell_iterator - cell = dof_handler.begin_active(), - endc = dof_handler.end(); - for (; cell != endc; ++cell) + for (const auto &cell : dof_handler.active_cell_iterators()) if (cell->subdomain_id() == subdomain_id) { const unsigned int dofs_per_cell = cell->get_fe().n_dofs_per_cell(); @@ -1071,10 +1061,7 @@ namespace DoFTools std::vector dof_indices; std::set global_dof_indices; - typename DoFHandler::active_cell_iterator - cell = dof_handler.begin_active(), - endc = dof_handler.end(); - for (; cell != endc; ++cell) + for (const auto &cell : dof_handler.active_cell_iterators()) if (cell->is_locally_owned()) { dof_indices.resize(cell->get_fe().n_dofs_per_cell()); @@ -1147,10 +1134,7 @@ namespace DoFTools std::vector dof_indices; std::vector dofs_on_ghosts; - typename DoFHandler::active_cell_iterator - cell = dof_handler.begin_active(), - endc = dof_handler.end(); - for (; cell != endc; ++cell) + for (const auto &cell : dof_handler.active_cell_iterators()) if (cell->is_ghost()) { dof_indices.resize(cell->get_fe().n_dofs_per_cell()); @@ -1191,11 +1175,7 @@ namespace DoFTools std::vector dof_indices; std::vector dofs_on_ghosts; - typename DoFHandler::cell_iterator cell = dof_handler.begin( - level), - endc = - dof_handler.end(level); - for (; cell != endc; ++cell) + for (const auto &cell : dof_handler.cell_iterators_on_level(level)) { const types::subdomain_id id = cell->level_subdomain_id(); @@ -1325,10 +1305,7 @@ namespace DoFTools AssertDimension(active_fe_indices.size(), dof_handler.get_triangulation().n_active_cells()); - typename DoFHandler::active_cell_iterator - cell = dof_handler.begin_active(), - endc = dof_handler.end(); - for (; cell != endc; ++cell) + for (const auto &cell : dof_handler.active_cell_iterators()) active_fe_indices[cell->active_cell_index()] = cell->active_fe_index(); } @@ -1546,10 +1523,7 @@ namespace DoFTools } else { - for (typename DoFHandler::active_cell_iterator cell = - dof_handler.begin_active(); - cell != dof_handler.end(); - cell++) + for (const auto &cell : dof_handler.active_cell_iterators()) if (cell->is_locally_owned()) cell_owners[cell->active_cell_index()] = cell->subdomain_id(); } @@ -1565,10 +1539,7 @@ namespace DoFTools // loop over all cells and record which subdomain a DoF belongs to. // give to the smaller subdomain_id in case it is on an interface - typename DoFHandler::active_cell_iterator - cell = dof_handler.begin_active(), - endc = dof_handler.end(); - for (; cell != endc; ++cell) + for (const auto &cell : dof_handler.active_cell_iterators()) { const types::subdomain_id subdomain_id = cell_owners[cell->active_cell_index()]; @@ -1646,10 +1617,7 @@ namespace DoFTools // more expensive than a vector std::vector subdomain_indices; - typename DoFHandler::active_cell_iterator - cell = dof_handler.begin_active(), - endc = dof_handler.end(); - for (; cell != endc; ++cell) + for (const auto &cell : dof_handler.active_cell_iterators()) if ((cell->is_artificial() == false) && (cell->subdomain_id() == subdomain)) { @@ -2047,10 +2015,7 @@ namespace DoFTools // do not support boundaries of dimension dim-2, and so every isolated // boundary line is also part of a boundary face which we will be // visiting sooner or later - typename DoFHandler::active_cell_iterator - cell = dof_handler.begin_active(), - endc = dof_handler.end(); - for (; cell != endc; ++cell) + for (const auto &cell : dof_handler.active_cell_iterators()) for (const unsigned int f : cell->face_indices()) if (cell->at_boundary(f)) { @@ -2092,10 +2057,7 @@ namespace DoFTools dofs_on_face.reserve(dof_handler.get_fe_collection().max_dofs_per_face()); types::global_dof_index next_boundary_index = 0; - typename DoFHandler::active_cell_iterator - cell = dof_handler.begin_active(), - endc = dof_handler.end(); - for (; cell != endc; ++cell) + for (const auto &cell : dof_handler.active_cell_iterators()) for (const unsigned int f : cell->face_indices()) if (boundary_ids.find(cell->face(f)->boundary_id()) != boundary_ids.end()) @@ -2158,12 +2120,9 @@ namespace DoFTools fe_collection, q_coll_dummy, update_quadrature_points); - typename DoFHandler::active_cell_iterator - cell = dof_handler.begin_active(), - endc = dof_handler.end(); std::vector local_dof_indices; - for (; cell != endc; ++cell) + for (const auto &cell : dof_handler.active_cell_iterators()) // only work on locally relevant cells if (cell->is_artificial() == false) { @@ -2316,30 +2275,23 @@ namespace DoFTools { AssertThrow(out, ExcIO()); - using dof_map_t = std::map>; - - using point_map_t = std::map, - std::vector, - typename internal::ComparisonHelper>; - - point_map_t point_map; + std::map, + std::vector, + typename internal::ComparisonHelper> + point_map; // convert to map point -> list of DoFs - for (typename dof_map_t::const_iterator it = support_points.begin(); - it != support_points.end(); - ++it) + for (const auto &it : support_points) { - std::vector &v = point_map[it->second]; - v.push_back(it->first); + std::vector &v = point_map[it.second]; + v.push_back(it.first); } // print the newly created map: - for (typename point_map_t::iterator it = point_map.begin(); - it != point_map.end(); - ++it) + for (const auto &it : point_map) { - out << it->first << " \""; - const std::vector &v = it->second; + out << it.first << " \""; + const std::vector &v = it.second; for (unsigned int i = 0; i < v.size(); ++i) { if (i > 0) @@ -2415,21 +2367,18 @@ namespace DoFTools const std::vector & selected_dofs, const types::global_dof_index offset) { - typename DoFHandler::level_cell_iterator cell; - typename DoFHandler::level_cell_iterator endc = - dof_handler.end(level); std::vector indices; unsigned int i = 0; - for (cell = dof_handler.begin(level); cell != endc; ++cell) + for (const auto &cell : dof_handler.cell_iterators_on_level(level)) if (cell->is_locally_owned_on_level()) ++i; block_list.reinit(i, dof_handler.n_dofs(), dof_handler.get_fe().n_dofs_per_cell()); i = 0; - for (cell = dof_handler.begin(level); cell != endc; ++cell) + for (const auto &cell : dof_handler.cell_iterators_on_level(level)) if (cell->is_locally_owned_on_level()) { indices.resize(cell->get_fe().n_dofs_per_cell()); @@ -2462,14 +2411,11 @@ namespace DoFTools { const FiniteElement &fe = dof_handler.get_fe(); block_list.reinit(1, dof_handler.n_dofs(level), dof_handler.n_dofs(level)); - typename DoFHandler::level_cell_iterator cell; - typename DoFHandler::level_cell_iterator endc = - dof_handler.end(level); std::vector indices; std::vector exclude; - for (cell = dof_handler.begin(level); cell != endc; ++cell) + for (const auto &cell : dof_handler.cell_iterators_on_level(level)) { indices.resize(cell->get_fe().n_dofs_per_cell()); cell->get_mg_dof_indices(indices); @@ -2509,23 +2455,18 @@ namespace DoFTools Assert(level > 0 && level < dof_handler.get_triangulation().n_levels(), ExcIndexRange(level, 1, dof_handler.get_triangulation().n_levels())); - typename DoFHandler::level_cell_iterator pcell = - dof_handler.begin(level - 1); - typename DoFHandler::level_cell_iterator endc = - dof_handler.end(level - 1); - std::vector indices; std::vector exclude; - for (unsigned int block = 0; pcell != endc; ++pcell) + unsigned int block = 0; + for (const auto &pcell : dof_handler.cell_iterators_on_level(level - 1)) { if (pcell->is_active()) continue; for (unsigned int child = 0; child < pcell->n_children(); ++child) { - const typename DoFHandler::level_cell_iterator cell = - pcell->child(child); + const auto cell = pcell->child(child); // For hp, only this line here would have to be replaced. const FiniteElement &fe = dof_handler.get_fe(); @@ -2600,10 +2541,6 @@ namespace DoFTools const bool single_cell_patches, const bool invert_vertex_mapping) { - typename DoFHandler::level_cell_iterator cell; - typename DoFHandler::level_cell_iterator endc = - dof_handler.end(level); - // Vector mapping from vertex index in the triangulation to consecutive // block indices on this level The number of cells at a vertex std::vector vertex_cell_count( @@ -2623,7 +2560,7 @@ namespace DoFTools // Identify all vertices active on this level and remember some data // about them - for (cell = dof_handler.begin(level); cell != endc; ++cell) + for (const auto &cell : dof_handler.cell_iterators_on_level(level)) for (const unsigned int v : cell->vertex_indices()) { const unsigned int vg = cell->vertex_index(v); @@ -2672,7 +2609,7 @@ namespace DoFTools std::vector indices; std::vector exclude; - for (cell = dof_handler.begin(level); cell != endc; ++cell) + for (const auto &cell : dof_handler.cell_iterators_on_level(level)) { const FiniteElement &fe = cell->get_fe(); indices.resize(fe.n_dofs_per_cell()); diff --git a/source/dofs/dof_tools_sparsity.cc b/source/dofs/dof_tools_sparsity.cc index 87c4df922f..810ecb9024 100644 --- a/source/dofs/dof_tools_sparsity.cc +++ b/source/dofs/dof_tools_sparsity.cc @@ -92,14 +92,11 @@ namespace DoFTools std::vector dofs_on_this_cell; dofs_on_this_cell.reserve(dof.get_fe_collection().max_dofs_per_cell()); - typename DoFHandler::active_cell_iterator - cell = dof.begin_active(), - endc = dof.end(); // In case we work with a distributed sparsity pattern of Trilinos // type, we only have to do the work if the current cell is owned by // the calling processor. Otherwise, just continue. - for (; cell != endc; ++cell) + for (const auto &cell : dof.active_cell_iterators()) if (((subdomain_id == numbers::invalid_subdomain_id) || (subdomain_id == cell->subdomain_id())) && cell->is_locally_owned()) @@ -183,14 +180,11 @@ namespace DoFTools std::vector dofs_on_this_cell( fe_collection.max_dofs_per_cell()); - typename DoFHandler::active_cell_iterator - cell = dof.begin_active(), - endc = dof.end(); // In case we work with a distributed sparsity pattern of Trilinos // type, we only have to do the work if the current cell is owned by // the calling processor. Otherwise, just continue. - for (; cell != endc; ++cell) + for (const auto &cell : dof.active_cell_iterators()) if (((subdomain_id == numbers::invalid_subdomain_id) || (subdomain_id == cell->subdomain_id())) && cell->is_locally_owned()) @@ -404,10 +398,7 @@ namespace DoFTools // @p{cell->has_boundary_lines}), since we do not support boundaries of // dimension dim-2, and so every boundary line is also part of a // boundary face. - typename DoFHandler::active_cell_iterator - cell = dof.begin_active(), - endc = dof.end(); - for (; cell != endc; ++cell) + for (const auto &cell : dof.active_cell_iterators()) for (const unsigned int f : cell->face_indices()) if (cell->at_boundary(f)) { @@ -501,10 +492,7 @@ namespace DoFTools std::vector dofs_on_this_face; dofs_on_this_face.reserve(dof.get_fe_collection().max_dofs_per_face()); - typename DoFHandler::active_cell_iterator - cell = dof.begin_active(), - endc = dof.end(); - for (; cell != endc; ++cell) + for (const auto &cell : dof.active_cell_iterators()) for (const unsigned int f : cell->face_indices()) if (boundary_ids.find(cell->face(f)->boundary_id()) != boundary_ids.end()) @@ -564,9 +552,6 @@ namespace DoFTools std::vector dofs_on_other_cell; dofs_on_this_cell.reserve(dof.get_fe_collection().max_dofs_per_cell()); dofs_on_other_cell.reserve(dof.get_fe_collection().max_dofs_per_cell()); - typename DoFHandler::active_cell_iterator - cell = dof.begin_active(), - endc = dof.end(); // TODO: in an old implementation, we used user flags before to tag // faces that were already touched. this way, we could reduce the work @@ -576,7 +561,7 @@ namespace DoFTools // In case we work with a distributed sparsity pattern of Trilinos // type, we only have to do the work if the current cell is owned by // the calling processor. Otherwise, just continue. - for (; cell != endc; ++cell) + for (const auto &cell : dof.active_cell_iterators()) if (((subdomain_id == numbers::invalid_subdomain_id) || (subdomain_id == cell->subdomain_id())) && cell->is_locally_owned()) @@ -820,10 +805,7 @@ namespace DoFTools if (int_dof_mask(i, j) != none) bool_int_dof_mask(i, j) = true; - typename DoFHandler::active_cell_iterator - cell = dof.begin_active(), - endc = dof.end(); - for (; cell != endc; ++cell) + for (const auto &cell : dof.active_cell_iterators()) if (((subdomain_id == numbers::invalid_subdomain_id) || (subdomain_id == cell->subdomain_id())) && cell->is_locally_owned()) @@ -1156,10 +1138,7 @@ namespace DoFTools } - typename dealii::DoFHandler::active_cell_iterator - cell = dof.begin_active(), - endc = dof.end(); - for (; cell != endc; ++cell) + for (const auto &cell : dof.active_cell_iterators()) if (((subdomain_id == numbers::invalid_subdomain_id) || (subdomain_id == cell->subdomain_id())) && cell->is_locally_owned()) -- 2.39.5