44 ProbabilityFunctionNegative,
46 <<
"Your probability density function in the particle generator "
47 "returned a negative value for the following position: "
48 << arg1 <<
". Please check your function expression.");
61 template <
int dim,
int spacedim = dim>
63 compute_local_cumulative_cell_weights(
68 std::vector<double> cumulative_cell_weights(
70 double cumulative_weight = 0.0;
86 for (
const auto &cell :
triangulation.active_cell_iterators())
88 if (cell->is_locally_owned())
90 fe_values.reinit(cell);
91 const double quadrature_point_weight =
92 probability_density_function.
value(
93 fe_values.get_quadrature_points()[0]);
96 ProbabilityFunctionNegative<dim>(
97 quadrature_formula.point(0)));
100 cumulative_weight += quadrature_point_weight * fe_values.JxW(0);
102 cumulative_cell_weights[cell->active_cell_index()] =
106 return cumulative_cell_weights;
116 template <
int dim,
int spacedim>
118 random_location_in_cell(
121 std::mt19937 &random_number_generator)
123 Assert(cell->reference_cell().is_hyper_cube() ==
true,
128 std::uniform_real_distribution<double> uniform_distribution_01(0, 1);
132 cell_bounding_box.get_boundary_points());
136 const unsigned int n_attempts = 100;
137 for (
unsigned int i = 0; i < n_attempts; ++i)
140 for (
unsigned int d = 0;
d < spacedim; ++
d)
142 position[
d] = uniform_distribution_01(random_number_generator) *
143 (cell_bounds.second[
d] - cell_bounds.first[
d]) +
144 cell_bounds.first[d];
152 if (cell->reference_cell().contains_point(reference_position))
153 return {position, reference_position};
167 for (
unsigned int d = 0;
d < dim; ++
d)
168 reference_position[d] =
169 uniform_distribution_01(random_number_generator);
174 return {position, reference_position};
180 template <
int dim,
int spacedim>
184 const std::vector<
Point<dim>> &particle_reference_locations,
190 triangulation.n_active_cells() * particle_reference_locations.size();
192#ifdef DEAL_II_WITH_MPI
193 if (
const auto tria =
197 n_particles_to_generate = tria->n_locally_owned_active_cells() *
198 particle_reference_locations.size();
202 const int ierr = MPI_Exscan(&n_particles_to_generate,
207 tria->get_mpi_communicator());
213 n_particles_to_generate);
215 for (
const auto &cell :
triangulation.active_cell_iterators() |
218 for (
const auto &reference_location : particle_reference_locations)
236 template <
int dim,
int spacedim>
241 std::mt19937 &random_number_generator,
244 const auto position_and_reference_position =
245 random_location_in_cell(cell, mapping, random_number_generator);
247 position_and_reference_position.second,
253 template <
int dim,
int spacedim>
258 std::mt19937 &random_number_generator,
262 const auto position_and_reference_position =
263 random_location_in_cell(cell, mapping, random_number_generator);
265 position_and_reference_position.first,
266 position_and_reference_position.second,
273 template <
int dim,
int spacedim>
278 const bool random_cell_selection,
282 const unsigned int random_number_seed)
284 unsigned int combined_seed = random_number_seed;
285 if (
const auto tria =
289 const unsigned int my_rank =
291 combined_seed += my_rank;
293 std::mt19937 random_number_generator(combined_seed);
298 std::vector<types::particle_index> particles_per_cell(
304 const std::vector<double> cumulative_cell_weights =
307 probability_density_function);
310 double local_weight_integral = (cumulative_cell_weights.size() > 0) ?
311 cumulative_cell_weights.back() :
315 double local_start_weight = numbers::signaling_nan<double>();
316 double global_weight_integral = numbers::signaling_nan<double>();
318 if (
const auto tria =
322 std::tie(local_start_weight, global_weight_integral) =
324 local_weight_integral, tria->get_mpi_communicator());
328 local_start_weight = 0;
329 global_weight_integral = local_weight_integral;
332 AssertThrow(global_weight_integral > std::numeric_limits<double>::min(),
334 "The integral of the user prescribed probability "
335 "density function over the domain equals zero, "
336 "deal.II has no way to determine the cell of "
337 "generated particles. Please ensure that the "
338 "provided function is positive in at least a "
339 "part of the domain; also check the syntax of "
344 std::llround(
static_cast<double>(n_particles_to_create) *
345 local_start_weight / global_weight_integral);
349 std::llround(
static_cast<double>(n_particles_to_create) *
350 ((local_start_weight + local_weight_integral) /
351 global_weight_integral));
352 n_local_particles = end_particle_id - start_particle_id;
354 if (random_cell_selection)
359 std::uniform_real_distribution<double> uniform_distribution(
360 0.0, local_weight_integral);
364 current_particle_index < n_local_particles;
365 ++current_particle_index)
369 const double random_weight =
370 uniform_distribution(random_number_generator);
372 const std::vector<double>::const_iterator selected_cell =
373 std::lower_bound(cumulative_cell_weights.begin(),
374 cumulative_cell_weights.end(),
377 std::distance(cumulative_cell_weights.begin(), selected_cell);
388 for (
const auto &cell :
triangulation.active_cell_iterators() |
393 static_cast<double>(n_local_particles) *
394 cumulative_cell_weights[cell->active_cell_index()] /
395 local_weight_integral);
400 particles_per_cell[cell->active_cell_index()] =
401 cumulative_particles_to_create - particles_created;
403 particles_per_cell[cell->active_cell_index()];
412 unsigned int current_particle_index = start_particle_id;
414 for (
const auto &cell :
triangulation.active_cell_iterators() |
417 for (
unsigned int i = 0;
418 i < particles_per_cell[cell->active_cell_index()];
422 current_particle_index,
423 random_number_generator,
427 ++current_particle_index;
437 template <
int dim,
int spacedim>
441 &global_bounding_boxes,
445 const std::vector<std::vector<double>> &properties)
447 const auto &fe = dof_handler.
get_fe();
454 const std::map<types::global_dof_index, Point<spacedim>>
460 std::vector<Point<spacedim>> support_points_vec;
461 support_points_vec.reserve(support_points_map.size());
462 for (
const auto &element : support_points_map)
463 support_points_vec.push_back(element.second);
466 global_bounding_boxes,
471 template <
int dim,
int spacedim>
478 &global_bounding_boxes,
481 const std::vector<std::vector<double>> &properties)
483 const std::vector<Point<dim>> &particle_reference_locations =
485 std::vector<Point<spacedim>> points_to_generate;
486 points_to_generate.reserve(particle_reference_locations.size() *
490 for (
const auto &cell :
triangulation.active_cell_iterators() |
493 for (
const auto &reference_location : particle_reference_locations)
497 points_to_generate.push_back(position_real);
501 global_bounding_boxes,
507#include "generators.inst"
unsigned int size() const
const FiniteElement< dim, spacedim > & get_fe(const types::fe_index index=0) const
virtual RangeNumberType value(const Point< dim > &p, const unsigned int component=0) const
Abstract base class for mapping classes.
virtual Point< spacedim > transform_unit_to_real_cell(const typename Triangulation< dim, spacedim >::cell_iterator &cell, const Point< dim > &p) const =0
virtual Point< dim > transform_real_to_unit_cell(const typename Triangulation< dim, spacedim >::cell_iterator &cell, const Point< spacedim > &p) const =0
void update_cached_numbers()
void reserve(const std::size_t n_particles)
std::map< unsigned int, IndexSet > insert_global_particles(const std::vector< Point< spacedim > > &positions, const std::vector< std::vector< BoundingBox< spacedim > > > &global_bounding_boxes, const std::vector< std::vector< double > > &properties={}, const std::vector< types::particle_index > &ids={})
types::particle_index n_locally_owned_particles() const
particle_iterator insert_particle(const Particle< dim, spacedim > &particle, const typename Triangulation< dim, spacedim >::active_cell_iterator &cell)
const std::vector< Point< dim > > & get_points() const
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcNotImplemented()
#define Assert(cond, exc)
#define AssertThrowMPI(error_code)
#define DeclException1(Exception1, type1, outsequence)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
@ update_JxW_values
Transformed quadrature weights.
@ update_quadrature_points
Transformed quadrature points.
void probabilistic_locations(const Triangulation< dim, spacedim > &triangulation, const Function< spacedim > &probability_density_function, const bool random_cell_selection, const types::particle_index n_particles_to_create, ParticleHandler< dim, spacedim > &particle_handler, const Mapping< dim, spacedim > &mapping=(ReferenceCells::get_hypercube< dim >() .template get_default_linear_mapping< dim, spacedim >()), const unsigned int random_number_seed=5432)
ParticleIterator< dim, spacedim > random_particle_in_cell_insert(const typename Triangulation< dim, spacedim >::active_cell_iterator &cell, const types::particle_index id, std::mt19937 &random_number_generator, ParticleHandler< dim, spacedim > &particle_handler, const Mapping< dim, spacedim > &mapping=(ReferenceCells::get_hypercube< dim >() .template get_default_linear_mapping< dim, spacedim >()))
void quadrature_points(const Triangulation< dim, spacedim > &triangulation, const Quadrature< dim > &quadrature, const std::vector< std::vector< BoundingBox< spacedim > > > &global_bounding_boxes, ParticleHandler< dim, spacedim > &particle_handler, const Mapping< dim, spacedim > &mapping=(ReferenceCells::get_hypercube< dim >() .template get_default_linear_mapping< dim, spacedim >()), const std::vector< std::vector< double > > &properties={})
Particle< dim, spacedim > random_particle_in_cell(const typename Triangulation< dim, spacedim >::active_cell_iterator &cell, const types::particle_index id, std::mt19937 &random_number_generator, const Mapping< dim, spacedim > &mapping=(ReferenceCells::get_hypercube< dim >() .template get_default_linear_mapping< dim, spacedim >()))
void dof_support_points(const DoFHandler< dim, spacedim > &dof_handler, const std::vector< std::vector< BoundingBox< spacedim > > > &global_bounding_boxes, ParticleHandler< dim, spacedim > &particle_handler, const Mapping< dim, spacedim > &mapping=(ReferenceCells::get_hypercube< dim >() .template get_default_linear_mapping< dim, spacedim >()), const ComponentMask &components={}, const std::vector< std::vector< double > > &properties={})
void regular_reference_locations(const Triangulation< dim, spacedim > &triangulation, const std::vector< Point< dim > > &particle_reference_locations, ParticleHandler< dim, spacedim > &particle_handler, const Mapping< dim, spacedim > &mapping=(ReferenceCells::get_hypercube< dim >() .template get_default_linear_mapping< dim, spacedim >()))
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
std::pair< T, T > partial_and_total_sum(const T &value, const MPI_Comm comm)
unsigned int this_mpi_process(const MPI_Comm mpi_communicator)
const ::parallel::distributed::Triangulation< dim, spacedim > * triangulation
#define DEAL_II_PARTICLE_INDEX_MPI_TYPE