From 7f9be12dcf10e59062b5c0e17977e587924beb5f Mon Sep 17 00:00:00 2001
From: Marc Fehling <mafehling.git@gmail.com>
Date: Wed, 10 Mar 2021 22:13:39 -0700
Subject: [PATCH] Simplify DoFHandler::prepare_coarsening_and_refinement().

---
 source/dofs/dof_handler.cc | 34 +++++++++++++++-------------------
 1 file changed, 15 insertions(+), 19 deletions(-)

diff --git a/source/dofs/dof_handler.cc b/source/dofs/dof_handler.cc
index 2e36ac73e2..cf63d8d2c9 100644
--- a/source/dofs/dof_handler.cc
+++ b/source/dofs/dof_handler.cc
@@ -2672,21 +2672,30 @@ DoFHandler<dim, spacedim>::prepare_coarsening_and_refinement(
     // nothing to do
     return false;
 
+
   //
   // establish hierarchy
   //
   // - create bimap between hierarchy levels and FE indices
 
+  // there can be as many levels in the hierarchy as active FE indices are
+  // possible
+  using level_type                      = active_fe_index_type;
+  static const level_type invalid_level = invalid_active_fe_index;
+
   // map from FE index to level in hierarchy
   // FE indices that are not covered in the hierarchy are not in the map
   const std::vector<unsigned int> fe_index_for_hierarchy_level =
     fe_collection.get_hierarchy_sequence(contains_fe_index);
 
   // map from level in hierarchy to FE index
-  // FE indices that are not covered in the hierarchy are not in the map
-  std::map<unsigned int, unsigned int> hierarchy_level_for_fe_index;
-  for (unsigned int i = 0; i < fe_index_for_hierarchy_level.size(); ++i)
-    hierarchy_level_for_fe_index[fe_index_for_hierarchy_level[i]] = i;
+  // FE indices that are not covered in the hierarchy will be mapped to
+  // invalid_level
+  std::vector<unsigned int> hierarchy_level_for_fe_index(fe_collection.size(),
+                                                         invalid_level);
+  for (unsigned int l = 0; l < fe_index_for_hierarchy_level.size(); ++l)
+    hierarchy_level_for_fe_index[fe_index_for_hierarchy_level[l]] = l;
+
 
   //
   // parallelization
@@ -2696,11 +2705,6 @@ DoFHandler<dim, spacedim>::prepare_coarsening_and_refinement(
   // - no need to compress, since the owning processor will have the correct
   //   level index
 
-  // there can be as many levels in the hierarchy as active FE indices are
-  // possible
-  using level_type                      = active_fe_index_type;
-  static const level_type invalid_level = invalid_active_fe_index;
-
   // HOTFIX: dealii::Vector does not accept integral types
   LinearAlgebra::distributed::Vector<float> future_levels;
   if (const auto parallel_tria =
@@ -2720,16 +2724,8 @@ DoFHandler<dim, spacedim>::prepare_coarsening_and_refinement(
 
   for (const auto &cell : active_cell_iterators())
     if (cell->is_locally_owned())
-      {
-        // set level if FE is part of hierarchy
-        const auto cell_fe_and_level =
-          hierarchy_level_for_fe_index.find(cell->future_fe_index());
-
-        future_levels[cell->global_active_cell_index()] =
-          (cell_fe_and_level != hierarchy_level_for_fe_index.end()) ?
-            cell_fe_and_level->second :
-            invalid_level;
-      }
+      future_levels[cell->global_active_cell_index()] =
+        hierarchy_level_for_fe_index[cell->future_fe_index()];
 
 
   //
-- 
2.39.5