Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
adaptive_sparse_contacts.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 <deal.II/dofs/dof_tools.h>
9
10#include <deal.II/fe/fe_q.h>
11
12template <int dim, typename PropertiesIndex>
17
18template <int dim, typename PropertiesIndex>
19void
21 const DoFHandler<dim> &background_dh)
22{
24 return;
25
27 for (const auto &cell : background_dh.active_cell_iterators())
28 {
29 if (cell->is_locally_owned() || cell->is_ghost())
30 local_and_ghost_cells.insert(cell);
31 }
32}
33
34template <int dim, typename PropertiesIndex>
35void
38 const Particles::ParticleHandler<dim> &particle_handler,
39 const std::set<typename DoFHandler<dim>::active_cell_iterator>
40 &local_and_ghost_cells_with_particles,
41 Vector<double> &granular_temperature_average,
42 Vector<double> &solid_fractions)
43{
44 // Iterating through the active cells in cell set with particles
45 for (const auto &cell : local_and_ghost_cells_with_particles)
46 {
47 // Particles in the cell
48 auto particles_in_cell = particle_handler.particles_in_cell(cell);
49 const unsigned int n_particles_in_cell =
50 particle_handler.n_particles_in_cell(cell);
51
52 // Initialize variables for solid fraction computation
53 double solid_fraction = 0.0;
54 double solid_volume = 0.0;
55 const double cell_volume = cell->measure();
56
57 // Initialize variables for granular temperature computation
58 double granular_temperature_cell = 0.0;
59 Tensor<1, dim> velocity_cell_average;
60 Tensor<1, dim> cell_velocity_fluctuation_squared_average;
61
62 // First loop over particles in cell to compute the sum of
63 // particle velocity and the solid volume of the current cell
64 for (auto particles_in_cell_iterator = particles_in_cell.begin();
65 particles_in_cell_iterator != particles_in_cell.end();
66 ++particles_in_cell_iterator)
67 {
68 // Get particle properties
69 auto particle_properties =
70 particles_in_cell_iterator->get_properties();
71 const double dp = particle_properties[PropertiesIndex::dp];
72
73 for (int d = 0; d < dim; ++d)
74 {
75 // Get the particle velocity component (v_x, v_y & v_z if dim = 3)
76 int v_axis = PropertiesIndex::v_x + d;
77
78 // Add the velocity component value
79 velocity_cell_average[d] += particle_properties[v_axis];
80 }
81
82 solid_volume += M_PI * Utilities::fixed_power<dim>(dp) / (2.0 * dim);
83 }
84
85 // Calculate average velocity in the cell (sum/n_particles)
86 velocity_cell_average /= n_particles_in_cell;
87
88 // Calculate solid fraction of cell
89 solid_fraction = solid_volume / cell_volume;
90
91 // Second loop over particle to compute the sum of the cell velocity
92 // fluctuations
93 for (auto particles_in_cell_iterator = particles_in_cell.begin();
94 particles_in_cell_iterator != particles_in_cell.end();
95 ++particles_in_cell_iterator)
96 {
97 auto particle_properties =
98 particles_in_cell_iterator->get_properties();
99
100 for (int d = 0; d < dim; ++d)
101 {
102 // Get the particle velocity component (v_x, v_y & v_z if dim = 3)
103 int v_axis = PropertiesIndex::v_x + d;
104
105 cell_velocity_fluctuation_squared_average[d] +=
106 Utilities::fixed_power<2>(particle_properties[v_axis] -
107 velocity_cell_average[d]);
108 }
109 }
110
111 // Calculate average granular temperature in the cell
112 for (int d = 0; d < dim; ++d)
113 {
114 cell_velocity_fluctuation_squared_average[d] /= n_particles_in_cell;
115 granular_temperature_cell +=
116 cell_velocity_fluctuation_squared_average[d] / dim;
117 }
118
119 // Store the average granular temperature and solid fraction with
120 // active cell index
121 granular_temperature_average[cell->active_cell_index()] =
122 granular_temperature_cell;
123 solid_fractions[cell->active_cell_index()] = solid_fraction;
124 }
125}
126
127template <int dim, typename PropertiesIndex>
128void
130 const DoFHandler<dim> &background_dh,
131 const Particles::ParticleHandler<dim> &particle_handler,
132 const unsigned int n_active_cells,
133 MPI_Comm mpi_communicator)
134{
135 // If the sparse contacts are not enabled, exit the function
137 return;
138
139 // Reset cell status containers
140 cell_mobility_status.clear();
141
142 // Reset all status to mobile if mobility status needs to be reset
143 if (DEMActionManager::get_action_manager()->check_mobility_status_reset())
144 {
145 for (auto cell : local_and_ghost_cells)
146 {
147 assign_mobility_status(cell->active_cell_index(),
149 }
150
151 return;
152 }
153
154 // Get a copy of the active & ghost cells set to iterate over and remove cell
155 // of the set when the mobility status is known. It avoids unnecessary
156 // iteration for next loops. We don't want to modify the original set since
157 // it is only updated when there's load balancing or reading of checkpoints.
158 auto local_and_ghost_cells_copy = local_and_ghost_cells;
159
160 // Create dummy dofs for background dof handler for the mobility_at_nodes
161 const FE_Q<dim> fe(1);
162 const unsigned int dofs_per_cell = fe.dofs_per_cell;
163
164 // Get locally owned and relevant dofs
165 const IndexSet &locally_owned_dofs = background_dh.locally_owned_dofs();
166 IndexSet locally_relevant_dofs =
167 DoFTools::extract_locally_relevant_dofs(background_dh);
168
169 // Reinit all value of mobility at nodes as inactive
170 mobility_at_nodes.reinit(locally_owned_dofs,
171 locally_relevant_dofs,
172 mpi_communicator);
174
175 // If the advection of particles setting is enabled (useful for CFD-DEM),
176 // mobility status are different: inactive and static_active status are
177 // advected and advected_active since they are not handled in the same way as
178 // in DEM only. They have a special handling in the velocity integration step
179 // because cell averaged velocity and acceleration are applied to particles,
180 // so they need this different status. The criteria for those status are the
181 // same as for DEM.
182 mobility_status inactive_status = (!advect_particles_enabled) ?
185 mobility_status active_status = (!advect_particles_enabled) ?
188
189 // Check if the cell is empty (n_particle = 0), if so, nodes and cells are
190 // flagged as empty mobility status
191 for (auto cell = local_and_ghost_cells_copy.begin();
192 cell != local_and_ghost_cells_copy.end();)
193 {
194 // Check if the cell has any particles
195 if (particle_handler.n_particles_in_cell(*cell) == 0)
196 {
197 std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
198 (*cell)->get_dof_indices(local_dof_indices);
199
200 // Assign inactive status to cell and empty status to nodes
201 assign_mobility_status((*cell)->active_cell_index(),
202 local_dof_indices,
205
206 // Remove cell from cell set and iterate to the following cell,
207 // erase() function returns the next iterator
208 cell = local_and_ghost_cells_copy.erase(cell);
209 }
210 else
211 {
212 // Since erase() is not called, iterator is incremented
213 ++cell;
214 }
215 }
216
217 // Update ghost values of mobility_at_nodes
218 mobility_at_nodes.update_ghost_values();
219
220 // Calculate the average granular temperature and solid fraction for each
221 // cell currently in the local_and_ghost_cells_copy set (empty cells are
222 // already removed)
223 Vector<double> granular_temperature_average(n_active_cells);
224 Vector<double> solid_fractions(n_active_cells);
226 particle_handler,
227 local_and_ghost_cells_copy,
228 granular_temperature_average,
229 solid_fractions);
230
231 // Check if the cell is mobile by criteria:
232 // * granular temperature > threshold or
233 // * solid fraction of cell < threshold or
234 // * is next to an empty cell
235 // If so, nodes are flagged and cells are stored with mobile status
236 for (auto cell = local_and_ghost_cells_copy.begin();
237 cell != local_and_ghost_cells_copy.end();)
238 {
239 const unsigned int cell_id = (*cell)->active_cell_index();
240 std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
241 (*cell)->get_dof_indices(local_dof_indices);
242
243 // Check if the cell has any empty neighbor cell by the value at nodes
244 bool has_empty_node = false;
245 for (auto node_id : local_dof_indices)
246 {
248 {
249 has_empty_node = true;
250 break; // No need to check other nodes
251 }
252 }
253
254 // Check if the cell is mobile by criteria (granular temperature, solid
255 // fraction, next to empty cell)
256 if (granular_temperature_average[cell_id] >
258 solid_fractions[cell_id] < solid_fraction_threshold || has_empty_node)
259 {
260 // Assign mobile status to cell and nodes
262 local_dof_indices,
265
266 // Remove cell from cell set
267 cell = local_and_ghost_cells_copy.erase(cell);
268 }
269 else
270 {
271 ++cell;
272 }
273 }
274
275 mobility_at_nodes.update_ghost_values();
276
277 // Check if the cell is mobile by neighbor (at least a node is flagged as
278 // mobile from previous check), this is the additional mobile layer.
279 // If so, cells are stored in map as mobile status and nodes that are not
280 // mobile are flagged as active
281 for (auto cell = local_and_ghost_cells_copy.begin();
282 cell != local_and_ghost_cells_copy.end();)
283 {
284 std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
285 (*cell)->get_dof_indices(local_dof_indices);
286
287 // Check if the cell has any mobile nodes by the value at nodes
288 bool has_mobile_node = false;
289 for (auto node_id : local_dof_indices)
290 {
292 {
293 has_mobile_node = true;
294 break;
295 }
296 }
297
298 if (has_mobile_node)
299 {
300 // Assign mobile status to cell and active to nodes
301 assign_mobility_status((*cell)->active_cell_index(),
302 local_dof_indices,
304 active_status);
305
306 // Remove cell from cell set and iterate to the following cell
307 cell = local_and_ghost_cells_copy.erase(cell);
308 }
309 else
310 {
311 ++cell;
312 }
313 }
314
315 mobility_at_nodes.update_ghost_values();
316
317 // Check if the cell is active (at least a node is flagged as active from
318 // previous check), this is the layer of active cells
319 // If so, cells are stored with active status in the map
320 for (auto cell = local_and_ghost_cells_copy.begin();
321 cell != local_and_ghost_cells_copy.end();)
322 {
323 std::vector<types::global_dof_index> local_dofs_indices(dofs_per_cell);
324 (*cell)->get_dof_indices(local_dofs_indices);
325
326 // Check if cell has active nodes
327 bool has_active_nodes = false;
328 for (auto node_id : local_dofs_indices)
329 {
330 if (mobility_at_nodes(node_id) == (int)active_status)
331 {
332 has_active_nodes = true;
333 break;
334 }
335 }
336
337 if (has_active_nodes)
338 {
339 // Assign active status to cell (no need to assign active to nodes)
340 assign_mobility_status((*cell)->active_cell_index(), active_status);
341 cell = local_and_ghost_cells_copy.erase(cell);
342 }
343 else
344 {
345 ++cell;
346 }
347 }
348
349 // Store the inactive cells in the map, those are all the remaining cells
350 // in the local & ghost cells set
351 for (auto cell = local_and_ghost_cells_copy.begin();
352 cell != local_and_ghost_cells_copy.end();
353 ++cell)
354 {
355 // Assign inactive status to cell in map
356 assign_mobility_status((*cell)->active_cell_index(), inactive_status);
357 }
358}
359
360template <int dim, typename PropertiesIndex>
361void
364 Particles::ParticleHandler<dim> &particle_handler,
365 const Tensor<1, 3> &g,
366 const std::vector<Tensor<1, 3>> &force,
367 const double dt)
368{
369 // If the sparse contacts is disabled, exit the function.
370 // Also, if the mobility status do not need to be reset, which only happens
371 // at first dem iteration of the cfd iteration
373 !DEMActionManager::get_action_manager()->check_mobility_status_reset())
374 return;
375
377
378 // Tensors for velocity and acceleration * dt computation
379 Tensor<1, 3> velocity_cell_average;
380 Tensor<1, 3> acc_dt_cell_average;
381
382 const Tensor<1, 3> dt_g = g * dt;
383
384 // Loop over all the cells even if they are not mobile to reset the force
385 // and torque values
386 for (auto cell : local_and_ghost_cells)
387 {
388 if (cell->is_locally_owned())
389 {
390 const unsigned int n_particles_in_cell =
391 particle_handler.n_particles_in_cell(cell);
392
393 velocity_cell_average.clear();
394 acc_dt_cell_average.clear();
395
396 if (n_particles_in_cell > 0)
397 {
398 auto particles_in_cell = particle_handler.particles_in_cell(cell);
399 for (auto &particle : particles_in_cell)
400 {
401 // Get particle properties
402 auto particle_properties = particle.get_properties();
403 types::particle_index particle_id =
404 particle.get_local_index();
405
406 double dt_mass_inverse =
407 dt / particle_properties[PropertiesIndex::mass];
408
409 // Calculate the acceleration of the particle times the time
410 // step
411 Tensor<1, 3> acc_dt_particle =
412 dt_g + force[particle_id] * dt_mass_inverse;
413
414 // Add up the acc. * dt for the average
415 acc_dt_cell_average += acc_dt_particle;
416
417 for (int d = 0; d < dim; ++d)
418 {
419 // Add up the current velocity for the average velocity
420 velocity_cell_average[d] +=
421 particle_properties[PropertiesIndex::v_x + d] +
422 acc_dt_particle[d];
423 }
424 }
425
426 // Compute the average velocity and acceleration * dt
427 velocity_cell_average /= n_particles_in_cell;
428 acc_dt_cell_average /= n_particles_in_cell;
429 }
430
431 // Insert the average pair in map
432 cell_velocities_accelerations.insert(std::make_pair(
433 cell, std::make_pair(velocity_cell_average, acc_dt_cell_average)));
434 }
435 }
436}
437
439template class AdaptiveSparseContacts<2,
441
442template class AdaptiveSparseContacts<2,
446template class AdaptiveSparseContacts<3,
448
449template class AdaptiveSparseContacts<3,
Mechanism that disables contacts computation through mobility status of cells based on granular tempe...
double solid_fraction_threshold
Threshold value for solid fraction.
bool advect_particles_enabled
Flag for the advection of particles.
void calculate_granular_temperature_and_solid_fraction(const Particles::ParticleHandler< dim > &particle_handler, const std::set< typename DoFHandler< dim >::active_cell_iterator > &local_and_ghost_cells_with_particles, Vector< double > &cell_granular_temperature, Vector< double > &cell_solid_fraction)
Calculate the granular temperature and solid fraction approximation (pcm method). Those values are cr...
double granular_temperature_threshold
Threshold value for granular temperature.
bool sparse_contacts_enabled
Flag for adaptive sparse contacts enabling. Useful to exit functions.
std::set< typename DoFHandler< dim >::active_cell_iterator > local_and_ghost_cells
Set of locally owned and ghost cells: <local/ghost cells> Used to loop over only the locally owned an...
std::map< typename Triangulation< dim >::active_cell_iterator, std::pair< Tensor< 1, 3 >, Tensor< 1, 3 > > > cell_velocities_accelerations
Map of cell velocities and accelerations * dt: <cell iterator: <velocity, acceleration * dt>>.
LinearAlgebra::distributed::Vector< int > mobility_at_nodes
Vector of mobility status at nodes: [mobility status] Used to check the value at node to determine th...
DEM::dem_data_structures< dim >::cell_index_int_map cell_mobility_status
Map of cell mobility status: <cell index: mobility status>.
void identify_mobility_status(const DoFHandler< dim > &background_dh, const Particles::ParticleHandler< dim > &particle_handler, const unsigned int n_active_cells, MPI_Comm mpi_communicator)
Identify of the mobility status of each cell through a node-based identification and check....
void update_local_and_ghost_cell_set(const DoFHandler< dim > &background_dh)
Create or update a set of the active and ghost cells so that there is no loop over all the cells of t...
void update_average_velocities_acceleration(Particles::ParticleHandler< dim > &particle_handler, const Tensor< 1, 3 > &g, const std::vector< Tensor< 1, 3 > > &force, const double dt)
Calculate or update the cell-averaged velocities and accelerations of the cells. This is used for the...
void assign_mobility_status(unsigned int cell_id, const std::vector< types::global_dof_index > &dof_indices, const int cell_status, const int node_status)
Assign the mobility status to the cell, its nodes, and its periodic coinciding nodes from the periodi...
static DEMActionManager * get_action_manager()
Getter of the unique instance of the DEMActionManager.
DEM particle property definitions and output name management.
PropertiesIndex
Particle property indices for CFD-DEM multiphysics simulations.
PropertiesIndex
Particle property indices for CFD-DEM simulations.