From 8a1a1d33a50581c2ba587a6ff24bf9c90b16c3ce Mon Sep 17 00:00:00 2001 From: Martin Kronbichler Date: Thu, 10 Jun 2021 10:25:22 +0200 Subject: [PATCH] Simplify members of ParticleAccessor --- include/deal.II/particles/particle_accessor.h | 57 +++++++------------ source/particles/particle_handler.cc | 27 ++++----- 2 files changed, 34 insertions(+), 50 deletions(-) diff --git a/include/deal.II/particles/particle_accessor.h b/include/deal.II/particles/particle_accessor.h index e1f805097b..c2f24c8ae9 100644 --- a/include/deal.II/particles/particle_accessor.h +++ b/include/deal.II/particles/particle_accessor.h @@ -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::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 inline ParticleAccessor::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 &property_pool, const typename Triangulation::active_cell_iterator &cell, const unsigned int particle_index_within_cell) - : particles(const_cast(&particles)) - , property_pool(const_cast *>(&property_pool)) + : property_pool(const_cast *>(&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(&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:: operator==(const ParticleAccessor &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::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::Handle & ParticleAccessor::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::Handle & ParticleAccessor::get_handle() const { - return (*particles)[active_cell_index][particle_index_within_cell]; + return (*particles_on_cell)[particle_index_within_cell]; } } // namespace Particles diff --git a/source/particles/particle_handler.cc b/source/particles/particle_handler.cc index b76330f305..8b1d941d40 100644 --- a/source/particles/particle_handler.cc +++ b/source/particles/particle_handler.cc @@ -334,9 +334,7 @@ namespace Particles ParticleHandler::remove_particle( const ParticleHandler::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::invalid_handle; } -- 2.39.5