45 ProbabilityFunctionNegative,
47 <<
"Your probability density function in the particle generator "
48 "returned a negative value for the following position: "
49 << arg1 <<
". Please check your function expression.");
62 template <
int dim,
int spacedim = dim>
64 compute_local_cumulative_cell_weights(
69 std::vector<double> cumulative_cell_weights(
71 double cumulative_weight = 0.0;
87 for (
const auto &cell :
triangulation.active_cell_iterators())
89 if (cell->is_locally_owned())
91 fe_values.reinit(cell);
92 const double quadrature_point_weight =
93 probability_density_function.
value(
94 fe_values.get_quadrature_points()[0]);
97 ProbabilityFunctionNegative<dim>(
98 quadrature_formula.
point(0)));
101 cumulative_weight += quadrature_point_weight * fe_values.JxW(0);
103 cumulative_cell_weights[cell->active_cell_index()] =
107 return cumulative_cell_weights;
117 template <
int dim,
int spacedim>
119 random_location_in_cell(
122 std::mt19937 & random_number_generator)
124 Assert(cell->reference_cell().is_hyper_cube() ==
true,
129 std::uniform_real_distribution<double> uniform_distribution_01(0, 1);
133 cell_bounding_box.get_boundary_points());
137 const unsigned int n_attempts = 100;
138 for (
unsigned int i = 0; i < n_attempts; ++i)
141 for (
unsigned int d = 0;
d < spacedim; ++
d)
143 position[
d] = uniform_distribution_01(random_number_generator) *
144 (cell_bounds.second[
d] - cell_bounds.first[
d]) +
145 cell_bounds.first[d];
153 if (cell->reference_cell().contains_point(reference_position))
154 return {position, reference_position};
168 for (
unsigned int d = 0;
d < dim; ++
d)
169 reference_position[d] =
170 uniform_distribution_01(random_number_generator);
175 return {position, reference_position};
181 template <
int dim,
int spacedim>
185 const std::vector<
Point<dim>> & particle_reference_locations,
191 triangulation.n_active_cells() * particle_reference_locations.size();
193#ifdef DEAL_II_WITH_MPI
194 if (
const auto tria =
198 n_particles_to_generate =
tria->n_locally_owned_active_cells() *
199 particle_reference_locations.size();
203 const int ierr = MPI_Exscan(&n_particles_to_generate,
214 n_particles_to_generate);
216 for (
const auto &cell :
triangulation.active_cell_iterators() |
219 for (
const auto &reference_location : particle_reference_locations)
237 template <
int dim,
int spacedim>
242 std::mt19937 & random_number_generator,
245 const auto position_and_reference_position =
246 random_location_in_cell(cell, mapping, random_number_generator);
248 position_and_reference_position.second,
254 template <
int dim,
int spacedim>
259 std::mt19937 & random_number_generator,
263 const auto position_and_reference_position =
264 random_location_in_cell(cell, mapping, random_number_generator);
266 position_and_reference_position.first,
267 position_and_reference_position.second,
274 template <
int dim,
int spacedim>
279 const bool random_cell_selection,
283 const unsigned int random_number_seed)
285 unsigned int combined_seed = random_number_seed;
286 if (
const auto tria =
290 const unsigned int my_rank =
292 combined_seed += my_rank;
294 std::mt19937 random_number_generator(combined_seed);
299 std::vector<types::particle_index> particles_per_cell(
305 const std::vector<double> cumulative_cell_weights =
308 probability_density_function);
311 double local_weight_integral = (cumulative_cell_weights.size() > 0) ?
312 cumulative_cell_weights.back() :
315 double global_weight_integral;
317 if (
const auto tria =
321 global_weight_integral =
327 global_weight_integral = local_weight_integral;
330 AssertThrow(global_weight_integral > std::numeric_limits<double>::min(),
332 "The integral of the user prescribed probability "
333 "density function over the domain equals zero, "
334 "deal.II has no way to determine the cell of "
335 "generated particles. Please ensure that the "
336 "provided function is positive in at least a "
337 "part of the domain; also check the syntax of "
342 double local_start_weight = 0.0;
344#ifdef DEAL_II_WITH_MPI
345 if (
const auto tria =
349 const int ierr = MPI_Exscan(&local_weight_integral,
361 std::llround(
static_cast<double>(n_particles_to_create) *
362 local_start_weight / global_weight_integral);
366 std::llround(
static_cast<double>(n_particles_to_create) *
367 ((local_start_weight + local_weight_integral) /
368 global_weight_integral));
369 n_local_particles = end_particle_id - start_particle_id;
371 if (random_cell_selection)
376 std::uniform_real_distribution<double> uniform_distribution(
377 0.0, local_weight_integral);
381 current_particle_index < n_local_particles;
382 ++current_particle_index)
386 const double random_weight =
387 uniform_distribution(random_number_generator);
389 const std::vector<double>::const_iterator selected_cell =
390 std::lower_bound(cumulative_cell_weights.begin(),
391 cumulative_cell_weights.end(),
394 std::distance(cumulative_cell_weights.begin(), selected_cell);
405 for (
const auto &cell :
triangulation.active_cell_iterators() |
410 static_cast<double>(n_local_particles) *
411 cumulative_cell_weights[cell->active_cell_index()] /
412 local_weight_integral);
417 particles_per_cell[cell->active_cell_index()] =
418 cumulative_particles_to_create - particles_created;
420 particles_per_cell[cell->active_cell_index()];
429 unsigned int current_particle_index = start_particle_id;
431 for (
const auto &cell :
triangulation.active_cell_iterators() |
434 for (
unsigned int i = 0;
435 i < particles_per_cell[cell->active_cell_index()];
439 current_particle_index,
440 random_number_generator,
444 ++current_particle_index;
454 template <
int dim,
int spacedim>
458 & global_bounding_boxes,
462 const std::vector<std::vector<double>> &properties)
464 const auto &fe = dof_handler.
get_fe();
471 std::map<types::global_dof_index, Point<spacedim>> support_points_map;
480 std::vector<Point<spacedim>> support_points_vec;
481 support_points_vec.reserve(support_points_map.size());
482 for (
auto const &element : support_points_map)
483 support_points_vec.push_back(element.second);
486 global_bounding_boxes,
491 template <
int dim,
int spacedim>
498 & global_bounding_boxes,
501 const std::vector<std::vector<double>> &properties)
503 const std::vector<Point<dim>> &particle_reference_locations =
505 std::vector<Point<spacedim>> points_to_generate;
506 points_to_generate.reserve(particle_reference_locations.size() *
510 for (
const auto &cell :
triangulation.active_cell_iterators() |
513 for (
const auto &reference_location : particle_reference_locations)
517 points_to_generate.push_back(position_real);
521 global_bounding_boxes,
527#include "generators.inst"
unsigned int size() const
const FiniteElement< dim, spacedim > & get_fe(const unsigned int 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 Point< dim > & point(const unsigned int i) const
const std::vector< Point< dim > > & get_points() const
virtual MPI_Comm get_communicator() const
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
@ update_JxW_values
Transformed quadrature weights.
@ update_quadrature_points
Transformed quadrature points.
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)
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)
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=ComponentMask(), const std::vector< std::vector< double > > &properties={})
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 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)
unsigned int this_mpi_process(const MPI_Comm &mpi_communicator)
T sum(const T &t, const MPI_Comm &mpi_communicator)
const ::parallel::distributed::Triangulation< dim, spacedim > * triangulation
#define DEAL_II_PARTICLE_INDEX_MPI_TYPE
const ::Triangulation< dim, spacedim > & tria