From: Marc Fehling <mafehling.git@gmail.com>
Date: Sat, 12 Mar 2022 03:18:17 +0000 (-0700)
Subject: cell_weight: fix bug with AMR in step-68.
X-Git-Tag: v9.4.0-rc1~352^2~4
X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cd0c52b12298c12a11331ba0be7870b8fd10744a;p=dealii.git

cell_weight: fix bug with AMR in step-68.
---

diff --git a/examples/step-68/step-68.cc b/examples/step-68/step-68.cc
index a79f2f106f..bde1d7d5a4 100644
--- a/examples/step-68/step-68.cc
+++ b/examples/step-68/step-68.cc
@@ -350,27 +350,28 @@ namespace Step68
     // should have the status `CELL_PERSIST`. However this function can also
     // be used to distribute load during refinement, therefore we consider
     // refined or coarsened cells as well.
-    if (status == parallel::distributed::Triangulation<dim>::CELL_PERSIST ||
-        status == parallel::distributed::Triangulation<dim>::CELL_REFINE)
+    unsigned int n_particles_in_cell = 0;
+    switch (status)
       {
-        const unsigned int n_particles_in_cell =
-          particle_handler.n_particles_in_cell(cell);
-        return n_particles_in_cell * particle_weight;
-      }
-    else if (status == parallel::distributed::Triangulation<dim>::CELL_COARSEN)
-      {
-        unsigned int n_particles_in_cell = 0;
-
-        for (unsigned int child_index = 0; child_index < cell->n_children();
-             ++child_index)
-          n_particles_in_cell +=
-            particle_handler.n_particles_in_cell(cell->child(child_index));
-
-        return n_particles_in_cell * particle_weight;
+        case parallel::distributed::Triangulation<dim>::CELL_PERSIST:
+        case parallel::distributed::Triangulation<dim>::CELL_REFINE:
+          n_particles_in_cell = particle_handler.n_particles_in_cell(cell);
+          break;
+
+        case parallel::distributed::Triangulation<dim>::CELL_INVALID:
+          break;
+
+        case parallel::distributed::Triangulation<dim>::CELL_COARSEN:
+          for (const auto &child : cell->child_iterators())
+            n_particles_in_cell += particle_handler.n_particles_in_cell(child);
+          break;
+
+        default:
+          Assert(false, ExcInternalError());
+          break;
       }
 
-    Assert(false, ExcInternalError());
-    return 0;
+    return base_weight + particle_weight * n_particles_in_cell;
   }
 
 
diff --git a/tests/particles/step-68.cc b/tests/particles/step-68.cc
index 72713fbc65..544f59feed 100644
--- a/tests/particles/step-68.cc
+++ b/tests/particles/step-68.cc
@@ -278,27 +278,28 @@ namespace Step68
     // should have the status `CELL_PERSIST`. However this function can also
     // be used to distribute load during refinement, therefore we consider
     // refined or coarsened cells as well.
-    if (status == parallel::distributed::Triangulation<dim>::CELL_PERSIST ||
-        status == parallel::distributed::Triangulation<dim>::CELL_REFINE)
+    unsigned int n_particles_in_cell = 0;
+    switch (status)
       {
-        const unsigned int n_particles_in_cell =
-          particle_handler.n_particles_in_cell(cell);
-        return n_particles_in_cell * particle_weight;
-      }
-    else if (status == parallel::distributed::Triangulation<dim>::CELL_COARSEN)
-      {
-        unsigned int n_particles_in_cell = 0;
-
-        for (unsigned int child_index = 0; child_index < cell->n_children();
-             ++child_index)
-          n_particles_in_cell +=
-            particle_handler.n_particles_in_cell(cell->child(child_index));
-
-        return n_particles_in_cell * particle_weight;
+        case parallel::distributed::Triangulation<dim>::CELL_PERSIST:
+        case parallel::distributed::Triangulation<dim>::CELL_REFINE:
+          n_particles_in_cell = particle_handler.n_particles_in_cell(cell);
+          break;
+
+        case parallel::distributed::Triangulation<dim>::CELL_INVALID:
+          break;
+
+        case parallel::distributed::Triangulation<dim>::CELL_COARSEN:
+          for (const auto &child : cell->child_iterators())
+            n_particles_in_cell += particle_handler.n_particles_in_cell(child);
+          break;
+
+        default:
+          Assert(false, ExcInternalError());
+          break;
       }
 
-    Assert(false, ExcInternalError());
-    return 0;
+    return base_weight + particle_weight * n_particles_in_cell;
   }