From fd10c71860961fc5e8e774780dc2739439505f8a Mon Sep 17 00:00:00 2001 From: Rene Gassmoeller Date: Thu, 8 Aug 2019 12:10:45 -0600 Subject: [PATCH] Implement pack_particles functions --- include/deal.II/particles/particle_handler.h | 14 ++++ source/particles/particle_handler.cc | 67 ++++++++++++++++++-- source/particles/particle_handler.inst.in | 15 +++++ 3 files changed, 90 insertions(+), 6 deletions(-) diff --git a/include/deal.II/particles/particle_handler.h b/include/deal.II/particles/particle_handler.h index cac550d4e0..75797dd875 100644 --- a/include/deal.II/particles/particle_handler.h +++ b/include/deal.II/particles/particle_handler.h @@ -38,6 +38,20 @@ DEAL_II_NAMESPACE_OPEN namespace Particles { + namespace internal + { + template + std::vector + pack_particles(std::vector> &particles); + + template + std::vector> + unpack_particles( + const boost::iterator_range::const_iterator> + & data_range, + PropertyPool &property_pool = PropertyPool(0)); + } // namespace internal + /** * This class manages the storage and handling of particles. It provides * the data structures necessary to store particles efficiently, accessor diff --git a/source/particles/particle_handler.cc b/source/particles/particle_handler.cc index 98a9f046fa..27f101d6f6 100644 --- a/source/particles/particle_handler.cc +++ b/source/particles/particle_handler.cc @@ -28,6 +28,65 @@ DEAL_II_NAMESPACE_OPEN namespace Particles { + namespace internal + { + template + std::vector + pack_particles(std::vector> &particles) + { + std::vector buffer; + + if (particles.size() == 0) + return buffer; + + buffer.resize(particles.size() * + particles.front().serialized_size_in_bytes()); + void *current_data = buffer.data(); + + for (const auto &particle : particles) + { + particle.write_data(current_data); + } + + return buffer; + } + + template + std::vector> + unpack_particles( + const boost::iterator_range::const_iterator> + & data_range, + PropertyPool &property_pool) + { + std::vector> particles; + + if (data_range.empty()) + return particles; + + Particle particle; + particle.set_property_pool(property_pool); + const unsigned int particle_size = particle.serialized_size_in_bytes(); + + particles.reserve(data_range.size() / particle_size); + + const void *data = static_cast(&(*data_range.begin())); + + while (data < &(*data_range.end())) + { + particles.push_back(Particle(data, &property_pool)); + } + + Assert( + data == &(*data_range.end()), + ExcMessage( + "The particle data could not be deserialized successfully. " + "Check that when deserializing the particles you expect the same " + "number of properties that were serialized.")); + + return particles; + } + } // namespace internal + template ParticleHandler::ParticleHandler() : triangulation() @@ -1182,8 +1241,7 @@ namespace Particles break; } - return Utilities::pack(stored_particles_on_cell, - /*allow_compression=*/true); + return internal::pack_particles(stored_particles_on_cell); } template @@ -1196,10 +1254,7 @@ namespace Particles // We leave this container non-const to be able to `std::move` // its contents directly into the particles multimap later. std::vector> loaded_particles_on_cell = - Utilities::unpack>>( - data_range.begin(), - data_range.end(), - /*allow_compression=*/true); + internal::unpack_particles(data_range, *property_pool); // Update the reference to the current property pool for all particles. // This was not stored, because they might be transported across process diff --git a/source/particles/particle_handler.inst.in b/source/particles/particle_handler.inst.in index 022b8568d2..8973ca70e9 100644 --- a/source/particles/particle_handler.inst.in +++ b/source/particles/particle_handler.inst.in @@ -19,6 +19,21 @@ for (deal_II_dimension : DIMENSIONS; deal_II_space_dimension : SPACE_DIMENSIONS) #if deal_II_dimension <= deal_II_space_dimension namespace Particles \{ + namespace internal + \{ + template std::vector + pack_particles( + std::vector> + &particles); + + template std::vector< + Particle> + unpack_particles( + const boost::iterator_range::const_iterator> + & data_range, + PropertyPool &property_pool); + \} + template class ParticleHandler; \} -- 2.39.5