/**
* 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.
*/
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.
*/
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)
{}
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 = {};
}
++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();
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));
}
}
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);
}
}
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);
}
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
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];
}
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
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
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;
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();
++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)
{
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;
}