]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Make access to list of particles more robust
authorMartin Kronbichler <martin.kronbichler@it.uu.se>
Mon, 8 Nov 2021 12:24:02 +0000 (13:24 +0100)
committerMartin Kronbichler <martin.kronbichler@it.uu.se>
Mon, 8 Nov 2021 12:24:02 +0000 (13:24 +0100)
include/deal.II/particles/particle_handler.h
source/particles/particle_handler.cc

index e84ea81a4b3c15d82b43a447840ce61ca5b85570..66f95133411785b385c9a70205bac893c774cc8d 100644 (file)
@@ -930,9 +930,10 @@ namespace Particles
 
     /**
      * Iterator to the end of the list elements of particle_container which
-     * belong to locally owned elements.
+     * belong to locally owned elements. Made const to avoid accidental
+     * modification.
      */
-    typename particle_container::iterator owned_particles_end;
+    const typename particle_container::iterator owned_particles_end;
 
     /**
      * List from the active cells on the present MPI process to positions in
@@ -1152,6 +1153,34 @@ namespace Particles
       const typename Triangulation<dim, spacedim>::CellStatus     status,
       const boost::iterator_range<std::vector<char>::const_iterator>
         &data_range);
+
+    /**
+     * Internal function returning an iterator to the begin of the container
+     * for owned particles.
+     */
+    typename particle_container::iterator
+    particle_container_owned_begin() const;
+
+    /**
+     * Internal function returning an iterator to the end of the container for
+     * owned particles.
+     */
+    typename particle_container::iterator
+    particle_container_owned_end() const;
+
+    /**
+     * Internal function returning an iterator to the begin of the container
+     * for ghost particles.
+     */
+    typename particle_container::iterator
+    particle_container_ghost_begin() const;
+
+    /**
+     * Internal function returning an iterator to the end of the container for
+     * ghost particles.
+     */
+    typename particle_container::iterator
+    particle_container_ghost_end() const;
   };
 
 
@@ -1172,12 +1201,9 @@ namespace Particles
   inline typename ParticleHandler<dim, spacedim>::particle_iterator
   ParticleHandler<dim, spacedim>::begin()
   {
-    // We should always have at least the three anchor entries in the list of
-    // particles
-    Assert(!particles.empty(), ExcInternalError());
-
-    // jump over the first entry which is used for book-keeping
-    return particle_iterator(++particles.begin(), *property_pool, 0);
+    return particle_iterator(particle_container_owned_begin(),
+                             *property_pool,
+                             0);
   }
 
 
@@ -1195,7 +1221,7 @@ namespace Particles
   inline typename ParticleHandler<dim, spacedim>::particle_iterator
   ParticleHandler<dim, spacedim>::end()
   {
-    return particle_iterator(owned_particles_end, *property_pool, 0);
+    return particle_iterator(particle_container_owned_end(), *property_pool, 0);
   }
 
 
@@ -1213,13 +1239,9 @@ namespace Particles
   inline typename ParticleHandler<dim, spacedim>::particle_iterator
   ParticleHandler<dim, spacedim>::begin_ghost()
   {
-    Assert(!particles.empty(), ExcInternalError());
-    // find the start of ghost particles as one past the end of owned indices;
-    // the index in between is used for book-keeping
-    typename particle_container::iterator ghost_particles_begin =
-      owned_particles_end;
-    ++ghost_particles_begin;
-    return particle_iterator(ghost_particles_begin, *property_pool, 0);
+    return particle_iterator(particle_container_ghost_begin(),
+                             *property_pool,
+                             0);
   }
 
 
@@ -1237,8 +1259,54 @@ namespace Particles
   inline typename ParticleHandler<dim, spacedim>::particle_iterator
   ParticleHandler<dim, spacedim>::end_ghost()
   {
-    // skip the last entry which is used for book-keeping
-    return particle_iterator(--particles.end(), *property_pool, 0);
+    return particle_iterator(particle_container_ghost_end(), *property_pool, 0);
+  }
+
+
+
+  template <int dim, int spacedim>
+  inline typename ParticleHandler<dim, spacedim>::particle_container::iterator
+  ParticleHandler<dim, spacedim>::particle_container_owned_begin() const
+  {
+    // We should always have at least the three anchor entries in the list of
+    // particles
+    Assert(!particles.empty(), ExcInternalError());
+    typename particle_container::iterator begin =
+      const_cast<particle_container &>(particles).begin();
+    return ++begin;
+  }
+
+
+
+  template <int dim, int spacedim>
+  inline typename ParticleHandler<dim, spacedim>::particle_container::iterator
+  ParticleHandler<dim, spacedim>::particle_container_owned_end() const
+  {
+    Assert(!particles.empty(), ExcInternalError());
+    return owned_particles_end;
+  }
+
+
+
+  template <int dim, int spacedim>
+  inline typename ParticleHandler<dim, spacedim>::particle_container::iterator
+  ParticleHandler<dim, spacedim>::particle_container_ghost_begin() const
+  {
+    Assert(!particles.empty(), ExcInternalError());
+    typename particle_container::iterator begin = owned_particles_end;
+    return ++begin;
+  }
+
+
+
+  template <int dim, int spacedim>
+  inline typename ParticleHandler<dim, spacedim>::particle_container::iterator
+  ParticleHandler<dim, spacedim>::particle_container_ghost_end() const
+  {
+    Assert(!particles.empty(), ExcInternalError());
+    typename particle_container::iterator end =
+      const_cast<particle_container &>(particles).end();
+    return --end;
   }
 
 
index d1ea72c6ec6680ac9249f106536a24e211bfe9c0..57e17a8002e58686679c12a51d5bf36ad1a2859d 100644 (file)
@@ -165,12 +165,15 @@ namespace Particles
     global_max_particles_per_cell =
       particle_handler.global_max_particles_per_cell;
     next_free_particle_index = particle_handler.next_free_particle_index;
-    particles                = particle_handler.particles;
-    owned_particles_end      = particles.begin();
-    for (auto it = particle_handler.particles.begin();
-         it != particle_handler.owned_particles_end;
-         ++it)
-      ++owned_particles_end;
+
+    // Manually copy over the particles because we do not want to touch the
+    // anchor iterators set by initialize()
+    particles.insert(particle_container_owned_end(),
+                     particle_handler.particle_container_owned_begin(),
+                     particle_handler.particle_container_owned_end());
+    particles.insert(particle_container_ghost_end(),
+                     particle_handler.particle_container_ghost_begin(),
+                     particle_handler.particle_container_ghost_end());
 
     for (auto it = particles.begin(); it != particles.end(); ++it)
       if (!it->particles.empty())
@@ -228,19 +231,19 @@ namespace Particles
     const Triangulation<dim, spacedim> *triangulation,
     particle_container &                given_particles)
   {
-    TriaActiveIterator<CellAccessor<dim, spacedim>> past_the_end_iterator(
-      triangulation, -1, -1);
     given_particles.clear();
     for (unsigned int i = 0; i < 3; ++i)
       given_particles.emplace_back(
         std::vector<typename PropertyPool<dim, spacedim>::Handle>(),
-        past_the_end_iterator);
+        triangulation->end());
 
     // Set the end of owned particles to the middle of the three elements
-    owned_particles_end = ++given_particles.begin();
+    const_cast<typename particle_container::iterator &>(owned_particles_end) =
+      ++given_particles.begin();
   }
 
 
+
   template <int dim, int spacedim>
   void
   ParticleHandler<dim, spacedim>::update_cached_numbers()
@@ -248,8 +251,8 @@ namespace Particles
     // first sort the owned particles by the active cell index
     bool sort_is_necessary = false;
     {
-      auto previous = ++particles.begin();
-      for (auto next = previous; next != owned_particles_end; ++next)
+      auto previous = particle_container_owned_begin();
+      for (auto next = previous; next != particle_container_owned_end(); ++next)
         {
           if (previous->cell.state() == IteratorState::valid &&
               next->cell.state() == IteratorState::valid &&
@@ -280,11 +283,9 @@ namespace Particles
             if (cells_to_particle_cache[cell->active_cell_index()] !=
                 particles.end())
               {
-                // observe that owned_particles_end was already set to point
-                // into the new sorted_particles array
                 typename particle_container::iterator insert_position =
-                  cell->is_locally_owned() ? owned_particles_end :
-                                             --sorted_particles.end();
+                  cell->is_locally_owned() ? particle_container_owned_end() :
+                                             particle_container_ghost_end();
                 typename particle_container::iterator new_entry =
                   sorted_particles.insert(
                     insert_position, typename particle_container::value_type());
@@ -324,7 +325,9 @@ namespace Particles
 
     // check that we only have locally owned particles in the first region of
     // cells; note that we skip the very first anchor element
-    for (auto it = ++particles.begin(); it != owned_particles_end; ++it)
+    for (auto it = particle_container_owned_begin();
+         it != particle_container_owned_end();
+         ++it)
       Assert(it->cell->is_locally_owned(), ExcInternalError());
 
     // check that the cache is consistent with the iterators
@@ -577,7 +580,8 @@ namespace Particles
     if (cache == particles.end())
       {
         const typename particle_container::iterator insert_position =
-          cell->is_locally_owned() ? owned_particles_end : --particles.end();
+          cell->is_locally_owned() ? particle_container_owned_end() :
+                                     particle_container_ghost_end();
         cache = particles.emplace(
           insert_position,
           std::vector<typename PropertyPool<dim, spacedim>::Handle>{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.