Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
insertion.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 <dem/insertion.h>
5
6#include <cmath>
7#include <sstream>
8
9template <int dim, typename PropertiesIndex>
11 const std::vector<std::shared_ptr<Distribution>>
12 &size_distribution_object_container,
13 const parallel::distributed::Triangulation<dim> &triangulation,
14 const DEMSolverParameters<dim> &dem_parameters)
16 dem_parameters.insertion_info.removing_particles_in_region)
17{
18 distributions_objects = size_distribution_object_container;
19
20 // Boost signal for load balancing
21 this->mark_for_update = true;
22 this->change_to_triangulation = triangulation.signals.any_change.connect(
23 [&] { this->mark_for_update = true; });
24
25 for (int i = 0; i < dim; ++i)
26 {
27 if (dem_parameters.insertion_info.clear_box_point_1[i] <=
28 dem_parameters.insertion_info.clear_box_point_2[i])
29 {
30 p_min[i] = dem_parameters.insertion_info.clear_box_point_1[i];
31 p_max[i] = dem_parameters.insertion_info.clear_box_point_2[i];
32 }
33 else
34 {
35 p_min[i] = dem_parameters.insertion_info.clear_box_point_2[i];
36 p_max[i] = dem_parameters.insertion_info.clear_box_point_1[i];
37 }
38 }
39}
40
41// Prints the insertion information
42template <int dim, typename PropertiesIndex>
43void
45 const unsigned int &inserted_this_step,
46 const unsigned int &remained_particles,
47 const unsigned int &particle_type,
48 const ConditionalOStream &pcout)
49{
50 std::stringstream ss;
51
52 ss << inserted_this_step << " particles of type " << particle_type
53 << " were inserted, " << remained_particles << " particles of type "
54 << particle_type << " remaining";
55
56 announce_string(pcout, ss.str(), '*');
57}
58
59// Carries out assigning the properties of inserted particles. The output vector
60// is used in insert_global_particles as input argument
61template <int dim, typename PropertiesIndex>
62void
64 const DEMSolverParameters<dim> &dem_parameters,
65 const unsigned int &inserted_this_step_this_proc,
66 const unsigned int &current_inserting_particle_type,
67 const std::vector<Point<dim>> &insertion_points,
68 std::vector<std::vector<double>> &particle_properties)
69{
70 // Clearing and resizing particle_properties
71 particle_properties.reserve(inserted_this_step_this_proc);
72
73 // Getting properties as local parameters
74 // TODO: MAYBE CHANGE THE INPUT TO PHYSICAL PROPERTIES DIRECTLY
75 auto physical_properties = dem_parameters.lagrangian_physical_properties;
76
77 // Creating the particle diameter that will get inserted.
78 const std::vector<double> particle_sizes =
79 distributions_objects[current_inserting_particle_type]
80 ->particle_size_sampling(inserted_this_step_this_proc);
81
82 // A loop is defined over the number of particles which are going to be
83 // inserted at this step
84 for (unsigned int particle_counter = 0;
85 particle_counter < inserted_this_step_this_proc;
86 ++particle_counter)
87 {
88 double type = current_inserting_particle_type;
89 // We make sure that the diameter is positive
90 double diameter = std::abs(particle_sizes[particle_counter]);
91 double density =
92 physical_properties.density_particle[current_inserting_particle_type];
93 double vel_x = dem_parameters.insertion_info.initial_vel[0];
94 double vel_y = dem_parameters.insertion_info.initial_vel[1];
95 double vel_z = dem_parameters.insertion_info.initial_vel[2];
96 double omega_x = dem_parameters.insertion_info.initial_omega[0];
97 double omega_y = dem_parameters.insertion_info.initial_omega[1];
98 double omega_z = dem_parameters.insertion_info.initial_omega[2];
99 double mass = density * 4. / 3. * M_PI *
100 Utilities::fixed_power<3, double>(diameter * 0.5);
101
102 // Every property which is not set explicitly below, such as the CFD-DEM
103 // coupling properties, is left to zero.
104 std::vector<double> properties_of_one_particle(
105 PropertiesIndex::n_properties, 0.);
106
107 properties_of_one_particle[PropertiesIndex::type] = type;
108 properties_of_one_particle[PropertiesIndex::dp] = diameter;
109 properties_of_one_particle[PropertiesIndex::mass] = mass;
110 properties_of_one_particle[PropertiesIndex::v_x] = vel_x;
111 properties_of_one_particle[PropertiesIndex::v_y] = vel_y;
112 properties_of_one_particle[PropertiesIndex::v_z] = vel_z;
113 properties_of_one_particle[PropertiesIndex::omega_x] = omega_x;
114 properties_of_one_particle[PropertiesIndex::omega_y] = omega_y;
115 properties_of_one_particle[PropertiesIndex::omega_z] = omega_z;
116
118 {
119 properties_of_one_particle[PropertiesIndex::T] =
120 dem_parameters.insertion_info.initial_temperature_function->value(
121 insertion_points[particle_counter]);
122 properties_of_one_particle[PropertiesIndex::specific_heat] =
123 physical_properties
124 .specific_heat_particle[current_inserting_particle_type];
125 }
126
127 particle_properties.push_back(properties_of_one_particle);
128 properties_of_one_particle.clear();
129 }
130}
131
132template <int dim, typename PropertiesIndex>
133void
135 const parallel::distributed::Triangulation<dim> &triangulation)
136{
137 // Clearing the containers
138 in_removal_box.clear();
139 edge_of_removal_box.clear();
140 bool partially_inside, completely_inside;
141 // Looping through cells
142 for (const auto &cell : triangulation.active_cell_iterators())
143 {
144 if (cell->is_locally_owned())
145 {
146 partially_inside = false;
147 completely_inside = true;
148 // Loop through its vertices
149 for (unsigned int vertex_id = 0; vertex_id < cell->n_vertices();
150 ++vertex_id)
151 {
152 Point<3> vertex = point_nd_to_3d(cell->vertex(vertex_id));
153
154 // Check if the n_th vertex is in the box
155 if (p_min[0] <= vertex[0] && vertex[0] <= p_max[0] &&
156 p_min[1] <= vertex[1] && vertex[1] <= p_max[1] &&
157 p_min[2] <= vertex[2] && vertex[2] <= p_max[2])
158 {
159 partially_inside = true;
160 }
161 else
162 {
163 completely_inside = false;
164 }
165 }
166 // If the cell is completely inside the clearing box, both bool will
167 // be true. We need to add the cell iterator to the
168 // in_removal_box container.
169
170 // If the cell is partially inside, at least one of the "if" will have
171 // failed, thus the second bool will be false but the first one true.
172 // We need to add the cell iterator to the edge_of_removal_box
173 // container.
174
175 // If the cell is outside, both bool will be false, and we do nothing.
176 if (completely_inside)
177 in_removal_box.insert(cell);
178 else if (!completely_inside && partially_inside)
179 edge_of_removal_box.insert(cell);
180 }
181 }
182}
183
184template <int dim, typename PropertiesIndex>
185void
187 Particles::ParticleHandler<dim> &particle_handler)
188{
189 std::vector<
190 typename dealii::Particles::ParticleHandler<dim>::particle_iterator>
191 to_remove_iterators;
192
193 // Reserve to the maximum number of particle on this processor.
194 to_remove_iterators.reserve(particle_handler.n_locally_owned_particles());
195
196 // Loop over the first container
197 for (const auto &cell_in_box : in_removal_box)
198 {
199 // Check if this cell has particles
200 auto particles_in_cell = particle_handler.particles_in_cell(cell_in_box);
201
202 if (!particles_in_cell.empty())
203 {
204 // Loop over the particle in the cell
205 for (auto particle_in_cell = particles_in_cell.begin();
206 particle_in_cell != particles_in_cell.end();
207 ++particle_in_cell)
208 {
209 // Since we know the cell is fully inside the box, we can
210 // remove every particles in it.
211
212 to_remove_iterators.push_back(particle_in_cell);
213 }
214 }
215 }
216
217 // Loop over the second container
218 for (auto cell_edge_of_removal_box = edge_of_removal_box.begin();
219 cell_edge_of_removal_box != edge_of_removal_box.end();
220 ++cell_edge_of_removal_box)
221 {
222 // Check if this cell has particle
223 auto particles_in_cell =
224 particle_handler.particles_in_cell(*cell_edge_of_removal_box);
225 const bool particles_exist_in_cell = !particles_in_cell.empty();
226
227 if (particles_exist_in_cell)
228 {
229 // Loop over the particles in the cell
230 for (auto particle_in_cell = particles_in_cell.begin();
231 particle_in_cell != particles_in_cell.end();
232 ++particle_in_cell)
233 {
234 // We need to check if the particle is inside since the cell
235 // is at the edge.
236 Point<3> particle_position =
237 point_nd_to_3d(particle_in_cell->get_location());
238 if (p_min[0] <= particle_position[0] &&
239 p_max[0] >= particle_position[0] &&
240 p_min[1] <= particle_position[1] &&
241 p_max[1] >= particle_position[1] &&
242 p_min[2] <= particle_position[2] &&
243 p_max[2] >= particle_position[2])
244 {
245 to_remove_iterators.push_back(particle_in_cell);
246 }
247 }
248 }
249 }
250 particle_handler.remove_particles(to_remove_iterators);
251}
252
Handles all the parameters declared in the parameter handler file.
Parameters::Lagrangian::LagrangianPhysicalProperties lagrangian_physical_properties
Parameters::Lagrangian::InsertionInfo< dim > insertion_info
Base interface for classes that carry out the insertion of particles in the system.
Definition insertion.h:33
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
boost::signals2::connection change_to_triangulation
Definition insertion.h:182
std::vector< std::shared_ptr< Distribution > > distributions_objects
Definition insertion.h:167
std::set< typename Triangulation< dim >::active_cell_iterator > in_removal_box
Definition insertion.h:176
Point< dim > p_min
Definition insertion.h:173
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
std::set< typename Triangulation< dim >::active_cell_iterator > edge_of_removal_box
Definition insertion.h:178
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
Point< dim > p_max
Definition insertion.h:173
void announce_string(const ConditionalOStream &pcout, const std::string &expression, const char delimiter='-')
Prints a string and then adds a line above and below made with dashes containing as many dashes as th...
Definition utilities.h:500
constexpr bool has_thermal_properties
Whether a properties index set carries thermal properties.
Point< 3 > point_nd_to_3d(const Point< dim > &point)