]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Hide particle container from property pool
authorRene Gassmoeller <rene.gassmoeller@mailbox.org>
Fri, 11 Jun 2021 17:21:33 +0000 (13:21 -0400)
committerRene Gassmoeller <rene.gassmoeller@mailbox.org>
Sat, 12 Jun 2021 03:28:18 +0000 (23:28 -0400)
include/deal.II/particles/property_pool.h
source/particles/particle_handler.cc
source/particles/property_pool.cc
tests/particles/property_pool_03.cc

index 8d26b445e590729dde936b649b2f04c6e82c311f..3f74a8167c4b8d7b83448fc8e33179a477f1d3d6 100644 (file)
@@ -215,13 +215,12 @@ namespace Particles
      * This function makes sure that all internally stored memory blocks
      * are sorted in the same order as one would loop over the @p handles_to_sort
      * container. This makes sure memory access is contiguous with actual
-     * memory location. Because the ordering is given in the input argumet
-     * the complexity of this function is $O(N)$ where N is the number of
-     * elements in the input argument. This function creates a temporary copy of
-     * all currently registered memory blocks.
+     * memory location. Because the ordering is given in the input argument
+     * the complexity of this function is $O(N)$ where $N$ is the number of
+     * elements in the input argument.
      */
     void
-    sort_memory_slots(std::vector<std::vector<Handle>> &handles_to_sort);
+    sort_memory_slots(const std::vector<Handle> &handles_to_sort);
 
   private:
     /**
index 52d47fc513d2a782fa143275092352273c2fd6a7..cdf59073e1944b65edb6154cb233d1aebd1e4968 100644 (file)
@@ -1291,7 +1291,21 @@ namespace Particles
 
     // remove_particles also calls update_cached_numbers()
     remove_particles(particles_out_of_cell);
-    property_pool->sort_memory_slots(particles);
+
+    // now make sure particle data is sorted in order of iteration
+    std::vector<typename PropertyPool<dim, spacedim>::Handle> unsorted_handles;
+    unsorted_handles.reserve(local_number_of_particles);
+
+    typename PropertyPool<dim, spacedim>::Handle sorted_handle = 0;
+    for (auto &particles_in_cell : particles)
+      for (auto &particle : particles_in_cell)
+        {
+          unsorted_handles.push_back(particle);
+          particle = sorted_handle++;
+        }
+
+    property_pool->sort_memory_slots(unsorted_handles);
+
   } // namespace Particles
 
 
index 20e48d1f96ae571d7964e93d2d927633fc0a2692..1189a664b1982848d609d4b3ecc6a2d1e71f23b4 100644 (file)
@@ -170,7 +170,7 @@ namespace Particles
   template <int dim, int spacedim>
   void
   PropertyPool<dim, spacedim>::sort_memory_slots(
-    std::vector<std::vector<Handle>> &handles_to_sort)
+    const std::vector<Handle> &handles_to_sort)
   {
     std::vector<Point<spacedim>>       sorted_locations;
     std::vector<Point<dim>>            sorted_reference_locations;
@@ -182,24 +182,19 @@ namespace Particles
     sorted_ids.reserve(ids.size());
     sorted_properties.reserve(properties.size());
 
-    Handle i = 0;
-    for (auto &handles_in_cell : handles_to_sort)
-      for (auto &handle : handles_in_cell)
-        {
-          Assert(handle != invalid_handle,
-                 ExcMessage(
-                   "Invalid handle detected during sorting particle memory."));
-
-          sorted_locations.push_back(locations[handle]);
-          sorted_reference_locations.push_back(reference_locations[handle]);
-          sorted_ids.push_back(ids[handle]);
+    for (auto &handle : handles_to_sort)
+      {
+        Assert(handle != invalid_handle,
+               ExcMessage(
+                 "Invalid handle detected during sorting particle memory."));
 
-          for (unsigned int j = 0; j < n_properties; ++j)
-            sorted_properties.push_back(properties[handle * n_properties + j]);
+        sorted_locations.push_back(locations[handle]);
+        sorted_reference_locations.push_back(reference_locations[handle]);
+        sorted_ids.push_back(ids[handle]);
 
-          handle = i;
-          ++i;
-        }
+        for (unsigned int j = 0; j < n_properties; ++j)
+          sorted_properties.push_back(properties[handle * n_properties + j]);
+      }
 
     Assert(sorted_locations.size() ==
              locations.size() - currently_available_handles.size(),
@@ -207,10 +202,10 @@ namespace Particles
              "Number of sorted property handles is not equal to number "
              "of currently registered handles."));
 
-    locations.swap(sorted_locations);
-    reference_locations.swap(sorted_reference_locations);
-    ids.swap(sorted_ids);
-    properties.swap(sorted_properties);
+    locations           = std::move(sorted_locations);
+    reference_locations = std::move(sorted_reference_locations);
+    ids                 = std::move(sorted_ids);
+    properties          = std::move(sorted_properties);
 
     currently_available_handles.clear();
   }
index 64ada7b132552e12afaec812e0616fd59cbcdaf6..532e745112c3c0cc2511e580fb5865e407607959 100644 (file)
@@ -35,60 +35,62 @@ test()
     const unsigned int                     n_properties = 3;
     Particles::PropertyPool<dim, spacedim> pool(n_properties);
 
-    std::vector<
-      std::vector<typename Particles::PropertyPool<dim, spacedim>::Handle>>
+    std::vector<typename Particles::PropertyPool<dim, spacedim>::Handle>
       particle_handles;
-    particle_handles.resize(2);
 
     // Allocate some space in non-contigous locations
-    particle_handles[1].push_back(pool.register_particle());
-    particle_handles[0].push_back(pool.register_particle());
-    particle_handles[1].push_back(pool.register_particle());
-    particle_handles[0].push_back(pool.register_particle());
-    particle_handles[1].push_back(pool.register_particle());
-    particle_handles[0].push_back(pool.register_particle());
+    particle_handles.push_back(pool.register_particle());
+    particle_handles.insert(particle_handles.begin(), pool.register_particle());
+    particle_handles.push_back(pool.register_particle());
+    particle_handles.insert(particle_handles.begin(), pool.register_particle());
+    particle_handles.push_back(pool.register_particle());
+    particle_handles.insert(particle_handles.begin(), pool.register_particle());
 
     unsigned int i = 0;
-    for (auto &particles_in_cell : particle_handles)
-      for (auto &particle : particles_in_cell)
-        pool.set_id(particle, i++);
+    for (auto &particle : particle_handles)
+      pool.set_id(particle, i++);
 
-    for (const auto &particles_in_cell : particle_handles)
-      for (const auto &particle : particles_in_cell)
-        deallog << "Before removal unsorted ID: " << pool.get_id(particle)
-                << ". Handle: " << particle << std::endl;
+    for (const auto &particle : particle_handles)
+      deallog << "Before removal unsorted ID: " << pool.get_id(particle)
+              << ". Handle: " << particle << std::endl;
 
     pool.sort_memory_slots(particle_handles);
 
-    for (const auto &particles_in_cell : particle_handles)
-      for (const auto &particle : particles_in_cell)
-        deallog << "Before removal sorted ID: " << pool.get_id(particle)
-                << ". Handle: " << particle << std::endl;
+    typename Particles::PropertyPool<dim, spacedim>::Handle h = 0;
+    for (auto &particle : particle_handles)
+      particle = h++;
+
+    for (const auto &particle : particle_handles)
+      deallog << "Before removal sorted ID: " << pool.get_id(particle)
+              << ". Handle: " << particle << std::endl;
 
     // Deallocate some space in the same way the particle handler would
     // remove particles
-    pool.deregister_particle(particle_handles[1][1]);
-    pool.deregister_particle(particle_handles[0][2]);
+    pool.deregister_particle(particle_handles[2]);
+    pool.deregister_particle(particle_handles[3]);
+
+    particle_handles[2] = particle_handles.back();
+    particle_handles.resize(particle_handles.size() - 1);
 
-    particle_handles[1][1] = particle_handles[1].back();
-    particle_handles[1].resize(particle_handles[1].size() - 1);
-    particle_handles[0].resize(particle_handles[0].size() - 1);
+    particle_handles[3] = particle_handles.back();
+    particle_handles.resize(particle_handles.size() - 1);
 
-    for (const auto &particles_in_cell : particle_handles)
-      for (const auto &particle : particles_in_cell)
-        deallog << "After removal unsorted ID: " << pool.get_id(particle)
-                << ". Handle: " << particle << std::endl;
+    for (const auto &particle : particle_handles)
+      deallog << "After removal unsorted ID: " << pool.get_id(particle)
+              << ". Handle: " << particle << std::endl;
 
     pool.sort_memory_slots(particle_handles);
 
-    for (const auto &particles_in_cell : particle_handles)
-      for (const auto &particle : particles_in_cell)
-        deallog << "After removal sorted ID: " << pool.get_id(particle)
-                << ". Handle: " << particle << std::endl;
+    h = 0;
+    for (auto &particle : particle_handles)
+      particle = h++;
+
+    for (const auto &particle : particle_handles)
+      deallog << "After removal sorted ID: " << pool.get_id(particle)
+              << ". Handle: " << particle << std::endl;
 
-    for (auto &particles_in_cell : particle_handles)
-      for (auto &particle : particles_in_cell)
-        pool.deregister_particle(particle);
+    for (auto &particle : particle_handles)
+      pool.deregister_particle(particle);
   }
 
   deallog << "OK" << std::endl;

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.