Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
insertion_plane.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2023-2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
5
7
8#include <algorithm>
9
10using namespace DEM;
11
12// The constructor of plane insertion class. In the constructor, we find which
13// cells are going to be used for the insertion, and we also find the centers of
14// those cells.
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 : Insertion<dim, PropertiesIndex>(size_distribution_object_container,
22 triangulation,
23 dem_parameters)
25 dem_parameters.lagrangian_physical_properties.number.at(0))
26{
27 // Initializing current inserting particle type
29
30 // Finding which cells are inplane
32 triangulation,
33 dem_parameters.insertion_info.insertion_plane_point,
34 dem_parameters.insertion_info.insertion_plane_normal_vector);
35
36 // Finding the center of those cells
38
39 // Initializing the variable for the random position of particle
41 dem_parameters.insertion_info.insertion_maximum_offset /
42 static_cast<double>(RAND_MAX);
43}
44
45template <int dim, typename PropertiesIndex>
46void
48 const parallel::distributed::Triangulation<dim> &triangulation,
49 const Point<3> &plane_point,
50 const Tensor<1, 3> &plane_normal_vector)
51{
53
54 // Looping through cells
55 for (const auto &cell : triangulation.active_cell_iterators())
56 {
57 // If the cell is owned by the processor
58 if (cell->is_locally_owned())
59 {
60 // Vector that goes from the plane reference point to the vertex 0.
61 Tensor<1, 3> connecting_vector_ref =
62 point_nd_to_3d(cell->vertex(0)) - plane_point;
63
64 // Normal distance between vertex 0 and the plane.
65 double vertex_wall_distance_ref =
66 connecting_vector_ref * plane_normal_vector;
67
68 // Loop over all the vertices of the cell
69 for (unsigned int vertex_id = 1; vertex_id < cell->n_vertices();
70 ++vertex_id)
71 {
72 // Vector that goes from the plane reference point to the vertex
73 // n-th
74 Tensor<1, 3> connecting_vector =
75 point_nd_to_3d(cell->vertex(vertex_id)) - plane_point;
76
77 // Normal distance between vertex n-th and the plane
78 double vertex_wall_distance =
79 connecting_vector * plane_normal_vector;
80
81 // If the multiplication of the two distances is negative, then at
82 // least one of vertex of the cell is on both sides of the plane.
83 // If the multiplication is equal to zero, then one vertex is on
84 // the plane.
85 if (vertex_wall_distance_ref * vertex_wall_distance <= 0)
86 {
87 plane_cells_for_insertion.insert(cell);
88 break; // If so, we break the loop, and go to the next cell
89 }
90 }
91 }
92 }
93}
94
95template <int dim, typename PropertiesIndex>
96void
98{
99 cells_centers.clear();
100 for (const auto &cell : plane_cells_for_insertion)
101 {
102 cells_centers.insert({cell->active_cell_index(), cell->center()});
103 }
104}
105
106// The main insertion function, insert_particle, is utilized to insert the
107// particle at the cell center with a random shifts particles
108template <int dim, typename PropertiesIndex>
109void
111 Particles::ParticleHandler<dim> &particle_handler,
112 const parallel::distributed::Triangulation<dim> &triangulation,
113 const DEMSolverParameters<dim> &dem_parameters)
114{
118 {
120 dem_parameters.lagrangian_physical_properties.number.at(
122 }
124 {
125 if (this->mark_for_update)
126 {
128 triangulation,
129 dem_parameters.insertion_info.insertion_plane_point,
130 dem_parameters.insertion_info.insertion_plane_normal_vector);
132
134 {
135 this->find_cells_in_removing_box(triangulation);
136 }
137 this->mark_for_update = false;
138 }
140 {
141 this->remove_particles_in_box(particle_handler);
142 }
143
144 MPI_Comm communicator = triangulation.get_mpi_communicator();
145 ConditionalOStream pcout(
146 std::cout, Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0);
147
148 unsigned int this_mpi_process =
149 Utilities::MPI::this_mpi_process(communicator);
150 unsigned int total_mpi_process =
151 Utilities::MPI::n_mpi_processes(communicator);
152 // List of the empty cells at this time step
153 std::set<typename Triangulation<dim>::active_cell_iterator>
154 empty_cells_on_proc;
155
156 // Loop over the cell inplane
157 for (const auto &cell : plane_cells_for_insertion)
158 {
159 // if the cell is empty ...
160 if (particle_handler.n_particles_in_cell(cell) == 0)
161 {
162 // ... we add the cell in std::set
163 empty_cells_on_proc.insert(cell);
164 }
165 }
166
167 // Processor zero knows how many particles every processor wants to insert
168 auto number_of_particles_to_insert_per_core =
169 Utilities::MPI::gather(communicator, empty_cells_on_proc.size(), 0);
170
171 std::vector<int> starting_IDs_on_every_proc(total_mpi_process);
172 std::vector<int> remained_particles_for_every_proc(total_mpi_process);
173 // Only processor zero does this
174 if (this_mpi_process == 0)
175 {
176 int starting_id_on_proc =
177 particle_handler.get_next_free_particle_index();
178
179 // we loop over the vector that we have created using the gather.
180 // Every integer in that vector gives the number of empty_cell of a
181 // certain processor.
182 for (unsigned int i = 0; i < total_mpi_process; ++i)
183 {
184 // We subtract those integer from the following variable
186 number_of_particles_to_insert_per_core[i];
187
188 // If the particles_of_each_type_remaining variable become
189 // negative, this means that we are trying to insert too many
190 // particle in the simulation. Thus, we need to decrease the
191 // number of empty_cell we want to insert in at this time step.
193 {
194 // At this point, we know the current processor have to many
195 // available cell to insert in (or just the right amount). The
196 // excess number of available cell on this processor is equal
197 // to the positive value of the
198 // particles_of_each_type_remaining variable since that
199 // variable became negative on this processor.
200
201 // Thus, we decrease the number of particle to insert on the
202 // current processor. To do so, we add the negative variable
203 // to the number of available cells which guarantee that the
204 // number of inserted particles in the simulation will reach
205 // its maximum on this processor.
206
207 number_of_particles_to_insert_per_core[i] +=
209
210 // We put the particles_of_each_type_remaining variable back
211 // to zero since we have just lower the number of cell
212 // available on this processor by the exceeding amount.
214
215 starting_IDs_on_every_proc[i] = starting_id_on_proc;
216 starting_id_on_proc +=
217 number_of_particles_to_insert_per_core[i];
218
219 // Loop over the remaining processor and put their number of
220 // available cell to zero.
221 for (unsigned int j = i + 1; j < (total_mpi_process); ++j)
222 {
223 number_of_particles_to_insert_per_core[j] = 0;
224 starting_IDs_on_every_proc[j] = 0;
225 }
226 break;
227 }
228 starting_IDs_on_every_proc[i] = starting_id_on_proc;
229 starting_id_on_proc += number_of_particles_to_insert_per_core[i];
230 }
231 std::ranges::fill(remained_particles_for_every_proc,
233 }
234
235 // Now, processor zero knows how many particles will be inserted by every
236 // processor without exceeding the maximum number of particles.
237
238 // We now need to scatter 3 information per processor, first how
239 // many particles it needs to insert. Second what is the ID of the
240 // first particle it will insert at this insertion step. The starting ID
241 // is mandatory, since we don't want two particles to have the same ID,
242 // and we don't want to use the global_insertion method because it uses an
243 // all_gather, which has a high computational cost. Third, the
244 // particles_of_each_type_remaining variable needs to be updated on
245 // processors other than processor 0 otherwise the insert function will
246 // become incoherent.
247
248 // This scatters the number of particles to insert
249 unsigned int number_of_particles_to_insert_on_this_core =
250 Utilities::MPI::scatter(communicator,
251 number_of_particles_to_insert_per_core,
252 0);
253
254 // This scatters the starting ID on every proc for the insertion
255 unsigned int starting_ID_on_proc =
256 Utilities::MPI::scatter(communicator, starting_IDs_on_every_proc, 0);
257
258 // This scatters particles_of_each_type_remaining on every proc
260 Utilities::MPI::scatter(communicator,
261 remained_particles_for_every_proc,
262 0);
263
264 // We yet didn't choose which empty cells won't be used anymore.
265
266 while (empty_cells_on_proc.size() >
267 number_of_particles_to_insert_on_this_core)
268 {
269 auto it = empty_cells_on_proc
270 .begin(); // Get the iterator of the first element
271 empty_cells_on_proc.erase(it); // Erase the first element
272 }
273
274 std::vector<Point<dim>> insertion_points_on_proc;
275 insertion_points_on_proc.reserve(
276 number_of_particles_to_insert_on_this_core);
277
278 // Loop over the empty cells we have kept.
279 for (const auto &cell : empty_cells_on_proc)
280 {
281 // Position of the center of the cell
282 Point<dim> insertion_location =
283 cells_centers.at(cell->active_cell_index());
284
285 // Generate the random point of insertion
286 insertion_location(0) +=
287 static_cast<double>(rand()) * maximum_range_for_randomness;
288
289 insertion_location(1) +=
290 static_cast<double>(rand()) * maximum_range_for_randomness;
291
292 if constexpr (dim == 3)
293 {
294 insertion_location(2) +=
295 static_cast<double>(rand()) * maximum_range_for_randomness;
296 }
297
298 insertion_points_on_proc.push_back(insertion_location);
299 }
300
301 // A vector of vectors, which contains all the properties of all inserted
302 // particles at each insertion step
303 std::vector<std::vector<double>> particle_properties;
304
306 dem_parameters,
307 number_of_particles_to_insert_on_this_core,
309 insertion_points_on_proc,
310 particle_properties);
311
312 // This is to iterate over the particle_properties vector
313 unsigned int i = 0;
314
315 // Insert the particles using the points and assigned properties
316 for (const auto &cell : empty_cells_on_proc)
317 {
318 // Use the pre-calculated insertion positions
319 Point<dim> insertion_location = insertion_points_on_proc[i];
320
321 // Insertion
322 Point<dim> ref_point;
323 particle_handler.insert_particle(insertion_location,
324 ref_point,
325 starting_ID_on_proc++,
326 cell,
327 particle_properties.at(i++));
328 }
329 }
330}
331
Handles all the parameters declared in the parameter handler file.
Parameters::Lagrangian::LagrangianPhysicalProperties lagrangian_physical_properties
Parameters::Lagrangian::InsertionInfo< dim > insertion_info
Insertion of particles using cells cut by a plane. Particle insertion using cells cut by a plane....
std::unordered_map< unsigned int, Point< dim > > cells_centers
unsigned int current_inserting_particle_type
InsertionPlane(const std::vector< std::shared_ptr< Distribution > > &size_distribution_object_container, const parallel::distributed::Triangulation< dim > &triangulation, const DEMSolverParameters< dim > &dem_parameters)
virtual void insert(Particles::ParticleHandler< dim > &particle_handler, const parallel::distributed::Triangulation< dim > &triangulation, const DEMSolverParameters< dim > &dem_parameters) override
void find_centers_of_inplane_cells()
Store the location of the centers of all the cells that are in the plane.
std::set< typename Triangulation< dim >::active_cell_iterator > plane_cells_for_insertion
void find_inplane_cells(const parallel::distributed::Triangulation< dim > &triangulation, const Point< 3 > &plane_point, const Tensor< 1, 3 > &plane_normal_vector)
Store the cells that are cut by the plane.
double maximum_range_for_randomness
int particles_of_each_type_remaining
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
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.
Point< 3 > point_nd_to_3d(const Point< dim > &point)