Skip to content

Commit 6478da7

Browse files
update KDE to sum particles from neighboring cells when generating PDF
1 parent 33ce954 commit 6478da7

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

include/aspect/particle/distribution.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <deal.II/particles/property_pool.h>
3030
#include <deal.II/particles/particle_handler.h>
3131
#include <deal.II/base/function_lib.h>
32+
#include <vector>
3233

3334
namespace aspect
3435
{
@@ -93,6 +94,7 @@ namespace aspect
9394
*/
9495
void
9596
fill_from_particle_range(const typename Particles::ParticleHandler<dim>::particle_iterator_range particle_range,
97+
const std::vector<typename Particles::ParticleHandler<dim>::particle_iterator_range> particle_ranges_to_sum_over,
9698
const unsigned int n_particles_in_cell);
9799

98100
/**

include/aspect/particle/manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
#include <boost/serialization/unique_ptr.hpp>
4848

4949
#include <random>
50-
50+
#include <vector>
5151

5252
namespace aspect
5353
{

source/particle/distribution.cc

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ namespace aspect
6767

6868
template <int dim>
6969
void
70-
ParticlePDF<dim>::fill_from_particle_range(const typename Particles::ParticleHandler<dim>::particle_iterator_range particle_range,
70+
ParticlePDF<dim>::fill_from_particle_range(const typename ParticleHandler<dim>::particle_iterator_range particle_range,
71+
const std::vector<typename Particles::ParticleHandler<dim>::particle_iterator_range> particle_ranges_to_sum_over,
7172
const unsigned int n_particles_in_cell)
7273
{
7374
if (is_defined_per_particle == false)
@@ -114,14 +115,25 @@ namespace aspect
114115
// Sum the value of the kernel function on that position from every other particle.
115116
for (const auto &reference_particle: particle_range)
116117
{
117-
const auto &reference_coordinates = reference_particle.get_reference_location();
118+
// Cell surrounding the particle whose PDF value is being calculated.
119+
const typename Triangulation<dim>::cell_iterator surrounding_cell = reference_particle.get_surrounding_cell();
120+
/*
121+
Cell diameter is used to normalize the distance value by the size of the cell containing the
122+
particle whose PDF value is being calculated.
123+
*/
124+
const double cell_diameter = surrounding_cell->diameter();
125+
const auto &reference_coordinates = reference_particle.get_location();
118126
double function_value = 0;
119127

120-
for (const auto &kernel_position_particle: particle_range)
128+
for (const auto &particle_range_to_sum: particle_ranges_to_sum_over)
121129
{
122-
const auto &kernel_coordinates = kernel_position_particle.get_reference_location();
123-
const double distance = reference_coordinates.distance(kernel_coordinates);
124-
function_value += apply_selected_kernel_function(distance);
130+
for (const auto &kernel_position_particle: particle_range_to_sum)
131+
{
132+
const auto &kernel_coordinates = kernel_position_particle.get_location();
133+
const double distance = reference_coordinates.distance(kernel_coordinates);
134+
const double distance_normalized = distance/cell_diameter;
135+
function_value += apply_selected_kernel_function(distance_normalized);
136+
}
125137
}
126138

127139
add_value_to_function_table(function_value/n_particles_in_cell,reference_particle.get_id());

source/particle/manager.cc

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,25 @@ namespace aspect
362362
if (deletion_algorithm == DeletionAlgorithm::point_density_function)
363363
{
364364
ParticlePDF<dim> pdf(0.3,kernel_function);
365-
pdf.fill_from_particle_range(particle_handler->particles_in_cell(cell),current_n_particles_in_cell);
365+
/*
366+
'particle_ranges_to_sum_over' includes this cell's and neighboring cell's particles.
367+
If neighboring cell's particles are not included in the KDE, particles at cell boundaries will
368+
have artificially low point density values.
369+
*/
370+
std::vector<typename Particles::ParticleHandler<dim>::particle_iterator_range> particle_ranges_to_sum_over;
371+
372+
std::vector<typename Triangulation<dim>::active_cell_iterator> neighboring_cells;
373+
GridTools::get_active_neighbors<Triangulation<dim>>(cell, neighboring_cells);
374+
for (const auto &neighbor_cell: neighboring_cells)
375+
{
376+
particle_ranges_to_sum_over.push_back(particle_handler->particles_in_cell(neighbor_cell));
377+
}
378+
379+
//std::vector<typename Particles::ParticleHandler<dim>::particle_iterator_range> particle_ranges_to_sum_over = {particle_handler->particles_in_cell(cell)};
380+
381+
pdf.fill_from_particle_range(particle_handler->particles_in_cell(cell),
382+
particle_ranges_to_sum_over,
383+
current_n_particles_in_cell);
366384
pdf.compute_statistical_values();
367385

368386
const types::particle_index index_max = pdf.get_max_particle();
@@ -834,13 +852,13 @@ namespace aspect
834852
"Strategy that is used to balance the computational "
835853
"load across processors for adaptive meshes.");
836854
prm.declare_entry ("Particle removal algorithm", "random",
837-
Patterns::MultipleSelection ("random|point density function"),
855+
Patterns::Selection ("random|point density function"),
838856
"Algorithm used to delete excess particles from cells. If point density function "
839857
"is chosen, the particle manager as the removal algorithm, the particle manager "
840858
"will generate a point density function from the locations of each particle and remove "
841859
"the particle whose position is at the maximum of the point density function.");
842860
prm.declare_entry ("Point density kernel function", "cutoff w1 dealii",
843-
Patterns::MultipleSelection ("cutoff w1 dealii|uniform|triangular|gaussian"),
861+
Patterns::Selection ("cutoff w1 dealii|uniform|triangular|gaussian"),
844862
"The kernel function is summed at each particle location to generate a point "
845863
"density function of the particle locations according to a process known as "
846864
"kernel density estimation. Because kernel density estimation sums the value of "
@@ -1024,7 +1042,7 @@ namespace aspect
10241042

10251043
if (kernel_function_string == "cutoff w1 dealii")
10261044
kernel_function = ParticlePDF<dim>::KernelFunction::cutoff_function_w1_dealii;
1027-
if (kernel_function_string == "uniform")
1045+
else if (kernel_function_string == "uniform")
10281046
kernel_function = ParticlePDF<dim>::KernelFunction::uniform;
10291047
else if (kernel_function_string == "triangular")
10301048
kernel_function = ParticlePDF<dim>::KernelFunction::triangular;

0 commit comments

Comments
 (0)