Skip to content

Commit f9275b3

Browse files
make KDE sum all neighboring cell's particles, not just active cells
1 parent 7d23134 commit f9275b3

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

source/particle/manager.cc

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ namespace aspect
265265
// If any load balancing technique is selected that creates/destroys particles
266266
if (particle_load_balancing & ParticleLoadBalancing::remove_and_add_particles)
267267
{
268+
GridTools::Cache<dim>grid_cache(this->get_triangulation(), this->get_mapping());
268269
// First do some preparation for particle generation in poorly
269270
// populated areas. For this we need to know which particle ids to
270271
// generate so that they are globally unique.
@@ -369,15 +370,20 @@ namespace aspect
369370
*/
370371
std::vector<typename Particles::ParticleHandler<dim>::particle_iterator_range> particle_ranges_to_sum_over;
371372

372-
std::vector<typename Triangulation<dim>::active_cell_iterator> neighboring_cells;
373-
GridTools::get_active_neighbors<Triangulation<dim>>(cell, neighboring_cells);
373+
std::set<typename Triangulation<dim>::active_cell_iterator> neighboring_cells;
374+
const auto &vertex_to_cell_map = grid_cache.get_vertex_to_cell_map();
375+
for (const auto v : cell->vertex_indices())
376+
{
377+
const unsigned int vertex_index = cell->vertex_index(v);
378+
neighboring_cells.insert(vertex_to_cell_map[vertex_index].begin(),
379+
vertex_to_cell_map[vertex_index].end());
380+
}
381+
374382
for (const auto &neighbor_cell: neighboring_cells)
375-
{
376-
particle_ranges_to_sum_over.push_back(particle_handler->particles_in_cell(neighbor_cell));
377-
}
383+
{
384+
particle_ranges_to_sum_over.push_back(particle_handler->particles_in_cell(neighbor_cell));
385+
}
378386

379-
//std::vector<typename Particles::ParticleHandler<dim>::particle_iterator_range> particle_ranges_to_sum_over = {particle_handler->particles_in_cell(cell)};
380-
381387
pdf.fill_from_particle_range(particle_handler->particles_in_cell(cell),
382388
particle_ranges_to_sum_over,
383389
current_n_particles_in_cell);

source/postprocess/particle_distribution_statistics.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ namespace aspect
6363
const auto &particle_handler = this->get_particle_manager(particle_manager_index).get_particle_handler();
6464

6565
Particle::ParticlePDF<dim> pdf(granularity,bandwidth,kernel_function);
66+
// This should be filled with this cell's particles and neighboring cells particles
67+
std::vector<typename Particles::ParticleHandler<dim>::particle_iterator_range> particle_ranges_to_sum_over = {particle_handler.particles_in_cell(cell)};
6668
pdf.fill_from_particle_range(particle_handler.particles_in_cell(cell),
69+
particle_ranges_to_sum_over,
6770
particle_handler.n_particles_in_cell(cell));
6871
pdf.compute_statistical_values();
6972

@@ -79,12 +82,32 @@ namespace aspect
7982
}
8083
else
8184
{
85+
GridTools::Cache<dim>grid_cache(this->get_triangulation(), this->get_mapping());
8286
for (unsigned int particle_manager_index = 0; particle_manager_index < this->n_particle_managers(); ++particle_manager_index)
8387
{
8488
const auto &particle_handler = this->get_particle_manager(particle_manager_index).get_particle_handler();
8589

8690
Particle::ParticlePDF<dim> pdf(bandwidth,kernel_function);
91+
92+
// This should be filled with this cell's particles and neighboring cells particles
93+
std::vector<typename Particles::ParticleHandler<dim>::particle_iterator_range> particle_ranges_to_sum_over = {particle_handler.particles_in_cell(cell)};
94+
95+
std::set<typename Triangulation<dim>::active_cell_iterator> neighboring_cells;
96+
const auto &vertex_to_cell_map = grid_cache.get_vertex_to_cell_map();
97+
for (const auto v : cell->vertex_indices())
98+
{
99+
const unsigned int vertex_index = cell->vertex_index(v);
100+
neighboring_cells.insert(vertex_to_cell_map[vertex_index].begin(),
101+
vertex_to_cell_map[vertex_index].end());
102+
}
103+
104+
for (const auto &neighbor_cell: neighboring_cells)
105+
{
106+
particle_ranges_to_sum_over.push_back(particle_handler.particles_in_cell(neighbor_cell));
107+
}
108+
87109
pdf.fill_from_particle_range(particle_handler.particles_in_cell(cell),
110+
particle_ranges_to_sum_over,
88111
particle_handler.n_particles_in_cell(cell));
89112
pdf.compute_statistical_values();
90113

0 commit comments

Comments
 (0)