Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
insertion_volume.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2020-2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#include <core/utilities.h>
5
7
8using namespace DEM;
9
10// The constructor of the volume insertion class. In the constructor, we
11// investigate if the insertion box is adequately large to handle the desired
12// number of inserted particles. The number of insertion points in each
13// direction (number_of_particles_x_direction, number_of_particles_y_direction
14// and number_of_particles_z_direction) are also obtained
15template <int dim, typename PropertiesIndex>
17 const std::vector<std::shared_ptr<Distribution>>
18 &size_distribution_object_container,
19 const parallel::distributed::Triangulation<dim> &triangulation,
20 const DEMSolverParameters<dim> &dem_parameters,
21 const double maximum_particle_diameter)
22 : Insertion<dim, PropertiesIndex>(size_distribution_object_container,
23 triangulation,
24 dem_parameters)
26 dem_parameters.lagrangian_physical_properties.number.at(0))
27 , acceptance_fct(dem_parameters.insertion_info.insertion_acceptance_fct)
28 , maximum_diameter(maximum_particle_diameter)
29{
30 // Initializing the current inserting particle type
32
33 // We need to fill the vector filted_box_index.
34 // To do this, we loop of every insertion point inside the box considering
35 // that the acceptance_fct accepts every point.
36 // For each point, we check if it respects the condition. If so, we insert
37 // the index associated with this point inside the vector.
38 set_filtered_index(dem_parameters.insertion_info);
39}
40
41
42template <int dim, typename PropertiesIndex>
43void
45 Particles::ParticleHandler<dim> &particle_handler,
46 const parallel::distributed::Triangulation<dim> &triangulation,
47 const DEMSolverParameters<dim> &dem_parameters)
48{
52 {
54 dem_parameters.lagrangian_physical_properties.number.at(
56 }
57
58 // Check if the remaining number of uninserted particles is equal to zero
60 {
61 // Remove the particle if the remove box region feature is activated
63 {
64 if (this->mark_for_update)
65 {
66 this->find_cells_in_removing_box(triangulation);
67 this->mark_for_update = false;
68 }
69 this->remove_particles_in_box(particle_handler);
70 }
71
72 // Get the MPI communicator and the parallel cout
73 MPI_Comm communicator = triangulation.get_mpi_communicator();
74 ConditionalOStream pcout(
75 std::cout, Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0);
76
77 // Compute the number of particle to insert during this step.
78 unsigned int inserted_this_step;
80 dem_parameters.insertion_info, pcout, inserted_this_step);
81
82 // Get global bounding boxes
83 const auto my_bounding_box =
84 GridTools::compute_mesh_predicate_bounding_box(
85 triangulation, IteratorFilters::LocallyOwnedCell());
86 const auto global_bounding_boxes =
87 Utilities::MPI::all_gather(communicator, my_bounding_box);
88
89 // Call the random number generator for the offsets
90 std::vector<double> random_number_vector;
91 random_number_vector.reserve(filted_box_index.size());
93 random_number_vector,
94 filted_box_index.size(),
95 dem_parameters.insertion_info.insertion_maximum_offset,
96 dem_parameters.insertion_info.seed_for_insertion);
97
98 Point<dim> insertion_location;
99 std::vector<Point<dim>> insertion_points_on_proc;
100 insertion_points_on_proc.reserve(filted_box_index.size());
101
102 // Looping through the particles on each process and finding their
103 // insertion location
104 unsigned int particle_counter = 0;
105 for (unsigned int global_index = first_index_this_proc;
106 global_index < last_index_this_proc &&
107 global_index < inserted_this_step;
108 ++global_index, ++particle_counter)
109 {
110 find_insertion_location(insertion_location,
111 filted_box_index.at(particle_counter),
112 random_number_vector.at(particle_counter),
113 random_number_vector.at(
114 filted_box_index.size() - particle_counter -
115 1),
116 dem_parameters.insertion_info);
117
118 insertion_points_on_proc.push_back(insertion_location);
119 }
120
121 std::vector<std::vector<double>> particle_properties;
122
123 // Assigning inserted particles properties using
124 // assign_particle_properties function
125 this->assign_particle_properties(dem_parameters,
126 particle_counter,
128 insertion_points_on_proc,
129 particle_properties);
130
131 // Insert the particles using the points and assigned properties
132 particle_handler.insert_global_particles(insertion_points_on_proc,
133 global_bounding_boxes,
134 particle_properties);
135
136 // Updating remaining particles
137 particles_of_each_type_remaining -= inserted_this_step;
138
139 this->print_insertion_info(inserted_this_step,
142 pcout);
143 }
144}
145
146// This function assigns the insertion points of the inserted particles
147template <int dim, typename PropertiesIndex>
148void
150 Point<dim> &insertion_location,
151 const unsigned int id,
152 const double random_number1,
153 const double random_number2,
154 const Parameters::Lagrangian::InsertionInfo<dim> &insertion_information)
155{
156 std::vector<int> insertion_index;
157 insertion_index.resize(dim);
158
159 // First direction (axis) to have particles inserted
160 unsigned int axis_0 = insertion_information.direction_sequence[0];
161 unsigned int number_of_particles_0 = number_of_particles_directions[axis_0];
162 insertion_index[axis_0] = id % number_of_particles_0;
163 insertion_location[axis_0] =
164 axis_min[axis_0] + ((insertion_index[axis_0] + 0.5) *
165 insertion_information.distance_threshold -
166 random_number1) *
168
169 // Second direction (axis) to have particles inserted
170 unsigned int axis_1 = insertion_information.direction_sequence.at(1);
171
172 if constexpr (dim == 2)
173 {
174 insertion_index[axis_1] = static_cast<int>(id / number_of_particles_0);
175 insertion_location[axis_1] =
176 axis_min[axis_1] + ((insertion_index[axis_1] + 0.5) *
177 insertion_information.distance_threshold -
178 random_number2) *
180 }
181 else
182 {
183 unsigned int number_of_particles_1 =
185 insertion_index[axis_1] =
186 static_cast<int>(id % (number_of_particles_0 * number_of_particles_1)) /
187 (number_of_particles_0);
188 insertion_location[axis_1] =
189 axis_min[axis_1] + ((insertion_index[axis_1] + 0.5) *
190 insertion_information.distance_threshold -
191 random_number2) *
193
194 // Third direction (axis) to have particles inserted
195 unsigned int axis_2 = insertion_information.direction_sequence[2];
196 insertion_index[axis_2] =
197 static_cast<int>(id / (number_of_particles_0 * number_of_particles_1));
198 insertion_location[axis_2] =
199 axis_min[axis_2] + ((insertion_index[axis_2] + 0.5) *
200 insertion_information.distance_threshold -
201 random_number1) *
203 }
204}
205
206
207template <int dim, typename PropertiesIndex>
208void
210 const InsertionInfo<dim> &insertion_information)
211{
212 // If you don't insert particles in this simulation, we don't need to build
213 // the container.
214 if (insertion_information.inserted_this_step == 0)
215 return;
216
217 // This variable is used to compute the maximum number of particles
218 // that can fit in the chosen insertion box before the acceptance function
219 // is applied.
220 unsigned int maximum_number_of_points = 1;
221
223 axis_min.resize(dim);
224 axis_max.resize(dim);
225 std::vector<unsigned int> axis_list = {
226 insertion_information.direction_sequence[0],
227 insertion_information.direction_sequence[1]};
228
229 if constexpr (dim == 3)
230 {
231 axis_list.push_back(insertion_information.direction_sequence[2]);
232 }
233
234 // Assigning the minimum and maximum positions of the insertion box in respect
235 // to the axis order.
236 for (const int axis : axis_list)
237 {
238 AssertThrow(axis < dim,
239 ExcMessage("Insertion direction must be 0, 1 or 2"));
240
241 axis_min[axis] = insertion_information.insertion_box_point_1(axis);
242 axis_max[axis] = insertion_information.insertion_box_point_2(axis);
243
244 // Assign the maximum number of insertion points according to the
245 // direction and calculate the total number of points that fit in the box
246 // (maximum_number_of_points = max_x * max_y * max_z)
247 const unsigned int number_of_points = static_cast<unsigned int>(
248 (axis_max[axis] - axis_min[axis]) /
249 (insertion_information.distance_threshold * maximum_diameter));
250
251 number_of_particles_directions[axis] = number_of_points;
252
253 maximum_number_of_points *= number_of_points;
254 }
255
256 // We split the number of points evenly on every process.
257 // The last process needs to be adjusted to match the maximum number of points
258 // in the box.
259 MPI_Comm communicator = MPI_COMM_WORLD;
260 auto this_mpi_process = Utilities::MPI::this_mpi_process(communicator);
261 auto n_mpi_process = Utilities::MPI::n_mpi_processes(communicator);
262
263 unsigned int n_points_this_proc = maximum_number_of_points / n_mpi_process;
264 if (this_mpi_process == (n_mpi_process - 1))
265 n_points_this_proc =
266 maximum_number_of_points -
267 (n_mpi_process - 1) * (maximum_number_of_points / n_mpi_process);
268
269 // First and last index of the unfiltered points that were assigned to this
270 // process.
271 unsigned int first_index_unfiltered;
272 unsigned int last_index_unfiltered;
273 if (this_mpi_process == (n_mpi_process - 1)) // For the last process
274 {
275 first_index_unfiltered = maximum_number_of_points - n_points_this_proc;
276 last_index_unfiltered = maximum_number_of_points;
277 }
278 else // For processes 1 to n-1
279 {
280 first_index_unfiltered = this_mpi_process * n_points_this_proc;
281 last_index_unfiltered = (this_mpi_process + 1) * n_points_this_proc;
282 }
283
284 // Now, we know that the indexes before applying the acceptance function
285 // will go from first_index_unfiltered to last_index_unfiltered.
286 // We count the number of insertion points that respect the acceptance
287 // function and store the index of those points
288 Point<dim> insertion_location;
289 for (unsigned int index = first_index_unfiltered;
290 index < last_index_unfiltered;
291 ++index)
292 {
293 // Create the insertion point, associated with the current index, with
294 // no offset
296 insertion_location, index, 0., 0., insertion_information);
297
298 // If the point respects the acceptance function, we insert the index in
299 // the vector.
300 if (acceptance_fct->value(insertion_location) > 0.)
301 filted_box_index.push_back(index);
302 }
303
304 // Each process needs to know what is its first and last filtered indexes.
305 // To do so, every process needs to know how many insertion point were kept
306 // valid by the previous process.
307 // The numeration used for the filtered indexes follows the same principal as
308 // the one used before filtering. The only difference is that we skip the
309 // rejected points.
310 std::vector<unsigned int> starting_index_on_every_proc(n_mpi_process);
311 starting_index_on_every_proc.resize(n_mpi_process);
312
313 // The number of insertion points assigned to this process that respect the
314 // acceptance function.
315 const unsigned int n_valid_insertion_point_this_proc =
316 filted_box_index.size();
317
318 // Every process sends to process 0 the number of valid insertion points it
319 // has.
320 auto number_of_insertion_point_per_core =
321 Utilities::MPI::gather(communicator, n_valid_insertion_point_this_proc, 0);
322
323 if (this_mpi_process == 0)
324 {
325 starting_index_on_every_proc[0] = 0;
326 for (unsigned int i = 1; i < n_mpi_process; ++i)
327 starting_index_on_every_proc[i] =
328 starting_index_on_every_proc[i - 1] +
329 number_of_insertion_point_per_core[i - 1];
330
332 std::accumulate(number_of_insertion_point_per_core.begin(),
333 number_of_insertion_point_per_core.end(),
334 0);
335 }
336
337 // This scatters the number of particles to insert
339 Utilities::MPI::scatter(communicator, starting_index_on_every_proc, 0);
340
341 // We find the last index with the size of the vector.
343 first_index_this_proc + n_valid_insertion_point_this_proc;
344
345 // We need this for the adjust_insertion_count_by_insertion_box_capacity
346 // function
348 Utilities::MPI::broadcast(communicator,
350 0);
351}
352
353template <int dim, typename PropertiesIndex>
354void
357 const InsertionInfo<dim> &insertion_information,
358 const ConditionalOStream &pcout,
359 unsigned int &inserted_this_step)
360{
361 // If the inserted number of particles at this step exceeds the maximum
362 // number, a warning is printed
363 if (insertion_information.inserted_this_step >
365 {
366 pcout << "Warning: the requested number of particles for insertion ("
367 << insertion_information.inserted_this_step
368 << ") is higher than maximum expected number of particles ("
369 << number_of_valid_insertion_point_global << ")" << std::endl;
370
371 // Updating the number of inserted particles at each step
372 inserted_this_step = number_of_valid_insertion_point_global;
373 }
374 else
375 {
376 inserted_this_step = insertion_information.inserted_this_step;
377 }
378
379 // The inserted_this_step value is the minimum of
380 // particles_of_each_type_remaining and inserted_this_step
381 inserted_this_step =
382 std::min(particles_of_each_type_remaining, inserted_this_step);
383}
384
Handles all the parameters declared in the parameter handler file.
Parameters::Lagrangian::LagrangianPhysicalProperties lagrangian_physical_properties
Parameters::Lagrangian::InsertionInfo< dim > insertion_info
void set_filtered_index(const InsertionInfo< dim > &insertion_information)
Fills the filted_box_index vector. Also finds how many insertion points are valid inside the insertio...
unsigned int current_inserting_particle_type
virtual void insert(Particles::ParticleHandler< dim > &particle_handler, const parallel::distributed::Triangulation< dim > &triangulation, const DEMSolverParameters< dim > &dem_parameters) override
Carries out the volume insertion of particles.
std::vector< double > axis_min
unsigned int last_index_this_proc
std::vector< double > axis_max
unsigned int number_of_valid_insertion_point_global
void find_insertion_location(Point< dim > &insertion_location, const unsigned int id, const double random_number1, const double random_number2, const InsertionInfo< dim > &insertion_information)
Converts id of particles to volume insertion location.
const double maximum_diameter
Maximum particle diameter based on the values defined in the parameter.
std::vector< int > number_of_particles_directions
std::shared_ptr< Function< dim > > acceptance_fct
Function used to insert particle inside our outside an arbitrary shape.
unsigned int first_index_this_proc
unsigned int particles_of_each_type_remaining
std::vector< unsigned int > filted_box_index
void adjust_insertion_count_by_insertion_box_capacity(const InsertionInfo< dim > &insertion_information, const ConditionalOStream &pcout, unsigned int &inserted_this_step)
Adjust the number of inserted particles for this insertion time step if the number required by the us...
InsertionVolume(const std::vector< std::shared_ptr< Distribution > > &size_distribution_object_container, const parallel::distributed::Triangulation< dim > &triangulation, const DEMSolverParameters< dim > &dem_parameters, const double maximum_particle_diameter)
The constructor investigates if the insertion box is large enough to handle the desired number of par...
void remove_particles_in_box(Particles::ParticleHandler< dim > &particle_handler)
Remove every particle located inside a predefined zone. Currently, the zone is defined by a box.
Definition insertion.cc:186
bool mark_for_update
Definition insertion.h:181
void find_cells_in_removing_box(const parallel::distributed::Triangulation< dim > &triangulation)
Find all cells that are completely and partially inside de the clearing box. For the cell completely ...
Definition insertion.cc:134
void assign_particle_properties(const DEMSolverParameters< dim > &dem_parameters, const unsigned int &inserted_this_step_this_proc, const unsigned int &current_inserting_particle_type, const std::vector< Point< dim > > &insertion_points, std::vector< std::vector< double > > &particle_properties)
Carries out assigning the properties of inserted particles.
Definition insertion.cc:63
const bool removing_particles_in_region
Definition insertion.h:170
Insertion(const std::vector< std::shared_ptr< Distribution > > &size_distribution_object_container, const parallel::distributed::Triangulation< dim > &triangulation, const DEMSolverParameters< dim > &dem_parameters)
Carries out the insertion of particles. This is the base class of volume_insertion,...
Definition insertion.cc:10
void print_insertion_info(const unsigned int &inserted_this_step, const unsigned int &remained_particles, const unsigned int &particle_type, const ConditionalOStream &pcout)
Print information about the particles that have been inserted during an insertion time step.
Definition insertion.cc:44
Parameters controlling particle insertion in DEM simulations.
Point< 3 > insertion_box_point_1
First corner of the insertion box (volume method).
unsigned int inserted_this_step
Number of particles inserted at each insertion step.
double distance_threshold
Minimum distance threshold between inserted particles.
std::vector< unsigned int > direction_sequence
Direction sequence for particle insertion (1st, 2nd, 3rd).
Point< 3 > insertion_box_point_2
Second corner of the insertion box (volume method).
void create_random_number_container(std::vector< double > &random_container, const unsigned int number_of_elements, const double maximum_range, const unsigned int prn_seed)
Creates a vector of random doubles between 0 and a maximum value using a pseudo random seed.
Definition utilities.h:1302
This namespace defines all the data containers and required operations on these containers for the DE...
unsigned int particle_type_number
Number of particle types.
std::vector< int > number
Number particles of each particle type.