Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
insertion_packed.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 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
8#include <deal.II/base/tensor.h>
9
10#include <deal.II/grid/grid_tools.h>
11
12#include <algorithm>
13
14using namespace DEM;
15
16template <int dim, typename PropertiesIndex>
18 const std::vector<std::shared_ptr<Distribution>>
19 &size_distribution_object_container,
20 const parallel::distributed::Triangulation<dim> &triangulation,
21 const DEMSolverParameters<dim> &dem_parameters)
22 : Insertion<dim, PropertiesIndex>(size_distribution_object_container,
23 triangulation,
24 dem_parameters)
25 , acceptance_fct(dem_parameters.insertion_info.insertion_acceptance_fct)
26{
27 // Initializing the current inserting particle type
28 // Getting properties as local parameters
29 const InsertionInfo<dim> insertion_information =
30 dem_parameters.insertion_info;
31
32 std::vector<unsigned int> axis_list;
33 axis_list.reserve(dim);
34 axis_list = {insertion_information.direction_sequence[0],
35 insertion_information.direction_sequence[1]};
36 if constexpr (dim == 3)
37 {
38 axis_list.push_back(insertion_information.direction_sequence[2]);
39 }
40
41 axis_min.resize(3);
42 axis_max.resize(3);
43 for (int axis : axis_list)
44 {
45 AssertThrow(axis < dim,
46 ExcMessage("Insertion direction must be 0, 1 or 2"));
47
48 axis_min[axis] = insertion_information.insertion_box_point_1(axis);
49 axis_max[axis] = insertion_information.insertion_box_point_2(axis);
50 }
51}
52
53
54template <int dim, typename PropertiesIndex>
55void
57 Particles::ParticleHandler<dim> &particle_handler,
58 const parallel::distributed::Triangulation<dim> &triangulation,
59 const DEMSolverParameters<dim> &dem_parameters)
60{
61 // We only insert once with this method. Thus, if the particle handler already
62 // possesses particles, we should not insert again.
63 if (particle_handler.n_global_particles() != 0)
64 return;
65
66
67 // Message passing interface requirements.
68 MPI_Comm communicator = triangulation.get_mpi_communicator();
69 ConditionalOStream pcout(std::cout,
70 Utilities::MPI::this_mpi_process(communicator) == 0);
71
72 auto this_mpi_process = Utilities::MPI::this_mpi_process(communicator);
73 auto n_mpi_process = Utilities::MPI::n_mpi_processes(communicator);
74
75 // Obtaining global bounding boxes
76 const auto my_bounding_box = GridTools::compute_mesh_predicate_bounding_box(
77 triangulation, IteratorFilters::LocallyOwnedCell());
78 const auto global_bounding_boxes =
79 Utilities::MPI::all_gather(communicator, my_bounding_box);
80
81 // Vector that will get filled with the insertion locations.
82 std::vector<Point<dim>> insertion_points_on_proc;
83
84 // Initialize the Random Number Generator using the configured seed
85 std::mt19937 rng(dem_parameters.insertion_info.seed_for_insertion +
86 this_mpi_process);
87
88 // We insert all the particle for every type all at once, thus we loop on each
89 // particle type.
90 for (unsigned int particle_type = 0;
91 particle_type <
93 particle_type++)
94 {
95 const unsigned int n_particle_current_type =
96 dem_parameters.lagrangian_physical_properties.number.at(particle_type);
97
98 if (n_particle_current_type == 0)
99 continue;
100
101 // Distributing particles between processors
102 unsigned int n_particle_current_type_this_proc =
103 static_cast<unsigned int>(
104 floor(n_particle_current_type / n_mpi_process));
105 if (this_mpi_process == (n_mpi_process - 1))
106 n_particle_current_type_this_proc = static_cast<unsigned int>(
107 n_particle_current_type -
108 (n_mpi_process - 1) * floor(n_particle_current_type / n_mpi_process));
109
110 // Clear and reserve space for insertion points for this particle type.
111 // We clear, since if this is the second particle type, this vector is
112 // already filled.
113 insertion_points_on_proc.clear();
114 insertion_points_on_proc.reserve(n_particle_current_type_this_proc);
115
116 // Find the first and the last particle id for each process
117 unsigned int first_id;
118 unsigned int last_id;
119 if (this_mpi_process == (n_mpi_process - 1))
120 { // Last proc
121 first_id =
122 n_particle_current_type - n_particle_current_type_this_proc;
123 last_id = n_particle_current_type;
124 }
125 else
126 { // Every other proc
127 first_id = this_mpi_process * n_particle_current_type_this_proc;
128 last_id = (this_mpi_process + 1) * n_particle_current_type_this_proc;
129 }
130
131 // Loop and generate valid insertion points.
132 Point<dim> insertion_location;
133 for (unsigned int id = first_id; id < last_id; ++id)
134 {
135 // Call updated rejection sampling function passing the RNG state
136 generate_insertion_location(insertion_location,
137 rng,
138 dem_parameters.insertion_info);
139
140 insertion_points_on_proc.push_back(insertion_location);
141 }
142
143 // Assigning particle properties
144 std::vector<std::vector<double>> particle_properties;
145 this->assign_particle_properties(dem_parameters,
146 n_particle_current_type_this_proc,
147 particle_type,
148 insertion_points_on_proc,
149 particle_properties);
150
151 // Insert the particles using the points and assigned properties
152 particle_handler.insert_global_particles(insertion_points_on_proc,
153 global_bounding_boxes,
154 particle_properties);
155
156 this->print_insertion_info(n_particle_current_type,
157 0,
158 particle_type,
159 pcout);
160 }
161
162 // Loop on every locally owned particle
163 double this_proc_inserted_volume = 0.;
164 for (auto &particle : particle_handler)
165 {
166 const double volume = numbers::PI *
167 Utilities::fixed_power<3>(
168 particle.get_properties()[PropertiesIndex::dp]) /
169 6.;
170 this_proc_inserted_volume += volume;
171 }
172
173 // Volume of the entire triangulation
174 const double total_triangulation_volume = GridTools::volume(triangulation);
175
176 // Total volume of inserted particle.
177 double total_inserted_volume =
178 Utilities::MPI::sum(this_proc_inserted_volume, communicator);
179
180 pcout << " The particle volume to triangulation volume ratio is: "
181 << total_inserted_volume / total_triangulation_volume << std::endl;
182}
183
184template <int dim, typename PropertiesIndex>
185void
187 Particles::ParticleHandler<dim> &particle_handler,
188 const double max_displacement,
190 std::vector<double> &displacements)
191{
192 for (auto &particle : particle_handler)
193 {
194 const types::particle_index particle_id = particle.get_local_index();
195
196 // The shift contact models accumulate the displacement of the particle
197 // in the force container.
198 const Tensor<1, 3> &accumulated_displacement =
199 contact_outcome.force[particle_id];
200
201 Tensor<1, dim> particle_displacement;
202 if constexpr (dim == 2)
203 particle_displacement = tensor_nd_to_2d(accumulated_displacement);
204 else if constexpr (dim == 3)
205 particle_displacement = accumulated_displacement;
206
207
208 // Particles without contact are not displaced
209 const double displacement_norm = particle_displacement.norm();
210 if (displacement_norm == 0.)
211 continue;
212
213 // Limit the displacement to prevent particles from crossing each other
214 if (displacement_norm > max_displacement)
215 particle_displacement *= max_displacement / displacement_norm;
216
217 particle.set_location(particle.get_location() + particle_displacement);
218 displacements[particle_id] += particle_displacement.norm();
219 }
220
221 // Reset the accumulated displacements for the next pseudo-time step
222 std::ranges::fill(contact_outcome.force, Tensor<1, 3>());
223}
224
225
226template <int dim, typename PropertiesIndex>
227void
229 Point<dim> &insertion_location,
230 std::mt19937 &rng,
231 const InsertionInfo<dim> &insertion_information)
232{
233 // A continuous random distribution on the range [min, max) with equal
234 // probability throughout the range. Thus the generated points are equally
235 // likely to be located in the middle of the box or on its edges.
236 std::uniform_real_distribution<double> distribution(0.0, 1.0);
237
238 bool accepted = false;
239 while (!accepted)
240 {
241 // Generate a new candidate point within the bounding box
242 for (int d = 0; d < dim; ++d)
243 {
244 // Get the actual coordinate axis (e.g., 0 for X, 1 for Y, 2 for Z)
245 // based on the configured insertion sequence
246 const unsigned int axis =
247 insertion_information.direction_sequence.at(d);
248
249 // Pull a fresh 0.0 to 1.0 random value
250 double random_value = distribution(rng);
251
252 // Map it uniformly across the specific axis span
253 insertion_location[axis] =
254 this->axis_min[axis] +
255 random_value * (this->axis_max[axis] - this->axis_min[axis]);
256 }
257 // Reject the point if the value is negative.
258 accepted = acceptance_fct->value(insertion_location) > 0.;
259 }
260}
261
262
Handles all the parameters declared in the parameter handler file.
Parameters::Lagrangian::LagrangianPhysicalProperties lagrangian_physical_properties
Parameters::Lagrangian::InsertionInfo< dim > insertion_info
std::vector< double > axis_max
void generate_insertion_location(Point< dim > &insertion_location, std::mt19937 &rng, const InsertionInfo< dim > &insertion_information)
Generate an insertion point inside an insertion box.
static void apply_shift_displacement(Particles::ParticleHandler< dim > &particle_handler, const double max_displacement, ParticleInteractionOutcomes< PropertiesIndex > &contact_outcome, std::vector< double > &displacements)
Displace the locally owned particles by the displacements accumulated by the shift contact models.
std::vector< double > axis_min
virtual void insert(Particles::ParticleHandler< dim > &particle_handler, const parallel::distributed::Triangulation< dim > &triangulation, const DEMSolverParameters< dim > &dem_parameters) override
Carries out the packed insertion of particles.
InsertionPacked(const std::vector< std::shared_ptr< Distribution > > &size_distribution_object_container, const parallel::distributed::Triangulation< dim > &triangulation, const DEMSolverParameters< dim > &dem_parameters)
Insert particles using the packed insertion method.
std::shared_ptr< Function< dim > > acceptance_fct
Function used to insert particles inside or outside an arbitrary shape.
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
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).
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).
Class where the outcomes of particle-particle interactions are stored.
std::vector< Tensor< 1, 3 > > force
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.
Tensor< 1, 2 > tensor_nd_to_2d(const Tensor< 1, dim > &tensor)