]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Simplify members of ParticleAccessor
authorMartin Kronbichler <kronbichler@lnm.mw.tum.de>
Thu, 10 Jun 2021 08:25:22 +0000 (10:25 +0200)
committerMartin Kronbichler <kronbichler@lnm.mw.tum.de>
Thu, 10 Jun 2021 09:08:18 +0000 (11:08 +0200)
include/deal.II/particles/particle_accessor.h
source/particles/particle_handler.cc

index e1f805097bd23362552839c80ca513738e01e745..c2f24c8ae90f40cd64ef02b279e4101b48b430f1 100644 (file)
@@ -320,9 +320,9 @@ namespace Particles
 
     /**
      * A pointer to the container that stores the particles. Obviously,
-     * this accessor is invalidated if the container changes.
+     * this accessor becomes invalid if the container changes.
      */
-    particle_container *particles;
+    typename particle_container::iterator particles_on_cell;
 
     /**
      * A pointer to the property pool that stores the actual particle data.
@@ -334,13 +334,6 @@ namespace Particles
      */
     typename Triangulation<dim, spacedim>::active_cell_iterator cell;
 
-    /**
-     * Index to the cell this particle is stored in at the moment. This could be
-     * read from the member variable cell, but is used in many performance
-     * critical functions and is therefore stored and updated separately.
-     */
-    unsigned int active_cell_index;
-
     /**
      * Local index of the particle within its current cell.
      */
@@ -416,10 +409,9 @@ namespace Particles
 
   template <int dim, int spacedim>
   inline ParticleAccessor<dim, spacedim>::ParticleAccessor()
-    : particles(nullptr)
+    : particles_on_cell({})
     , property_pool(nullptr)
     , cell()
-    , active_cell_index(numbers::invalid_unsigned_int)
     , particle_index_within_cell(numbers::invalid_unsigned_int)
   {}
 
@@ -431,17 +423,16 @@ namespace Particles
     const PropertyPool<dim, spacedim> &property_pool,
     const typename Triangulation<dim, spacedim>::active_cell_iterator &cell,
     const unsigned int particle_index_within_cell)
-    : particles(const_cast<particle_container *>(&particles))
-    , property_pool(const_cast<PropertyPool<dim, spacedim> *>(&property_pool))
+    : property_pool(const_cast<PropertyPool<dim, spacedim> *>(&property_pool))
     , cell(cell)
     , particle_index_within_cell(particle_index_within_cell)
   {
     if (cell.state() == IteratorState::valid)
-      active_cell_index = cell->active_cell_index();
-    else if (cell.state() == IteratorState::past_the_end)
-      active_cell_index = particles.size();
+      particles_on_cell =
+        const_cast<particle_container *>(&particles)->begin() +
+        cell->active_cell_index();
     else
-      active_cell_index = numbers::invalid_unsigned_int;
+      particles_on_cell = {};
   }
 
 
@@ -713,7 +704,7 @@ namespace Particles
 
     ++particle_index_within_cell;
 
-    if (particle_index_within_cell >= (*particles)[active_cell_index].size())
+    if (particle_index_within_cell >= particles_on_cell->size())
       {
         const bool particle_is_locally_owned = cell->is_locally_owned();
 
@@ -722,10 +713,10 @@ namespace Particles
         do
           {
             ++cell;
-            ++active_cell_index;
+            ++particles_on_cell;
           }
         while (cell.state() == IteratorState::valid &&
-               ((*particles)[active_cell_index].empty() ||
+               (particles_on_cell->empty() ||
                 cell->is_locally_owned() != particle_is_locally_owned));
       }
   }
@@ -747,23 +738,22 @@ namespace Particles
         do
           {
             --cell;
-            --active_cell_index;
+            --particles_on_cell;
 
             if (cell.state() != IteratorState::valid)
               {
-                active_cell_index = particles->size();
+                particles_on_cell = {};
                 break;
               }
 
-            if ((*particles)[active_cell_index].size() > 0 &&
+            if (!particles_on_cell->empty() &&
                 cell->is_locally_owned() == particle_is_locally_owned)
               {
-                particle_index_within_cell =
-                  (*particles)[active_cell_index].size() - 1;
+                particle_index_within_cell = particles_on_cell->size() - 1;
                 break;
               }
           }
-        while ((*particles)[active_cell_index].size() == 0 ||
+        while (particles_on_cell->size() == 0 ||
                cell->is_locally_owned() != particle_is_locally_owned);
       }
   }
@@ -785,8 +775,7 @@ namespace Particles
   ParticleAccessor<dim, spacedim>::
   operator==(const ParticleAccessor<dim, spacedim> &other) const
   {
-    return (particles == other.particles) &&
-           (property_pool == other.property_pool) && (cell == other.cell) &&
+    return (property_pool == other.property_pool) && (cell == other.cell) &&
            (particle_index_within_cell == other.particle_index_within_cell);
   }
 
@@ -796,12 +785,10 @@ namespace Particles
   inline IteratorState::IteratorStates
   ParticleAccessor<dim, spacedim>::state() const
   {
-    if (particles != nullptr && property_pool != nullptr &&
-        cell.state() == IteratorState::valid &&
-        particle_index_within_cell < (*particles)[active_cell_index].size())
+    if (property_pool != nullptr && cell.state() == IteratorState::valid &&
+        particle_index_within_cell < particles_on_cell->size())
       return IteratorState::valid;
-    else if (particles != nullptr &&
-             cell.state() == IteratorState::past_the_end &&
+    else if (cell.state() == IteratorState::past_the_end &&
              particle_index_within_cell == 0)
       return IteratorState::past_the_end;
     else
@@ -816,7 +803,7 @@ namespace Particles
   inline typename PropertyPool<dim, spacedim>::Handle &
   ParticleAccessor<dim, spacedim>::get_handle()
   {
-    return (*particles)[active_cell_index][particle_index_within_cell];
+    return (*particles_on_cell)[particle_index_within_cell];
   }
 
 
@@ -825,7 +812,7 @@ namespace Particles
   inline const typename PropertyPool<dim, spacedim>::Handle &
   ParticleAccessor<dim, spacedim>::get_handle() const
   {
-    return (*particles)[active_cell_index][particle_index_within_cell];
+    return (*particles_on_cell)[particle_index_within_cell];
   }
 
 } // namespace Particles
index b76330f30583bd4771d781b76cbc55a7df814879..8b1d941d400bf7b7e37c26e4f0c78e3dacef74bf 100644 (file)
@@ -334,9 +334,7 @@ namespace Particles
   ParticleHandler<dim, spacedim>::remove_particle(
     const ParticleHandler<dim, spacedim>::particle_iterator &particle)
   {
-    const unsigned int active_cell_index = particle->active_cell_index;
-
-    particle_container &container = *(particle->particles);
+    auto &container = *(particle->particles_on_cell);
 
     // if the particle has an invalid handle (e.g. because it has
     // been duplicated before calling this function) do not try
@@ -347,16 +345,15 @@ namespace Particles
         property_pool->deregister_particle(handle);
       }
 
-    if (container[active_cell_index].size() > 1)
+    if (container.size() > 1)
       {
-        container[active_cell_index][particle->particle_index_within_cell] =
-          std::move(particles[active_cell_index].back());
-        container[active_cell_index].resize(
-          particles[active_cell_index].size() - 1);
+        container[particle->particle_index_within_cell] =
+          std::move(particles[particle->cell->active_cell_index()].back());
+        container.resize(particles.size() - 1);
       }
     else
       {
-        container[active_cell_index].clear();
+        container.clear();
       }
 
     --local_number_of_particles;
@@ -380,8 +377,8 @@ namespace Particles
           break;
 
         // Skip cells where there is nothing to remove
-        if (particles_to_remove[n_particles_removed]->active_cell_index !=
-            cell_index)
+        if (particles_to_remove[n_particles_removed]
+              ->cell->active_cell_index() != cell_index)
           continue;
 
         const unsigned int n_particles_in_cell = particles[cell_index].size();
@@ -390,8 +387,8 @@ namespace Particles
              ++move_from)
           {
             if (n_particles_removed != particles_to_remove.size() &&
-                particles_to_remove[n_particles_removed]->active_cell_index ==
-                  cell_index &&
+                particles_to_remove[n_particles_removed]
+                    ->cell->active_cell_index() == cell_index &&
                 particles_to_remove[n_particles_removed]
                     ->particle_index_within_cell == move_from)
               {
@@ -1254,11 +1251,11 @@ namespace Particles
                 current_cell->active_cell_index();
 
               particles[active_cell_index].push_back(
-                particles[out_particle->active_cell_index]
+                particles[out_particle->cell->active_cell_index()]
                          [out_particle->particle_index_within_cell]);
 
               // Avoid deallocating the memory of this particle
-              particles[out_particle->active_cell_index]
+              particles[out_particle->cell->active_cell_index()]
                        [out_particle->particle_index_within_cell] =
                          PropertyPool<dim, spacedim>::invalid_handle;
             }

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.