Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
load_balancing.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2022-2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
7
8using namespace dealii;
9
10template <int dim, typename PropertiesIndex>
16
17template <int dim, typename PropertiesIndex>
18inline void
24
25template <int dim, typename PropertiesIndex>
26inline void
28 const
29{
30 if ((simulation_control->get_iteration_number() % load_balance_frequency) ==
31 0)
33}
34
35template <int dim, typename PropertiesIndex>
36inline void
38{
39 if (simulation_control->get_iteration_number() % dynamic_check_frequency == 0)
40 {
41 unsigned int maximum_particle_number_on_proc =
42 Utilities::MPI::max(particle_handler->n_locally_owned_particles(),
44 unsigned int minimum_particle_number_on_proc =
45 Utilities::MPI::min(particle_handler->n_locally_owned_particles(),
47 unsigned int average_particle_number_on_proc =
48 particle_handler->n_locally_owned_particles() / n_mpi_processes;
49
50 // Execute load balancing if difference of load between processors is
51 // larger than threshold of the load per processor
52 if ((maximum_particle_number_on_proc - minimum_particle_number_on_proc) >
53 load_threshold * average_particle_number_on_proc)
55 }
56}
57
58template <int dim, typename PropertiesIndex>
59inline void
62{
63 if (simulation_control->get_iteration_number() % dynamic_check_frequency == 0)
64 {
65 // Process to accumulate the load of each process regards the number
66 // of cells and particles with their selected weight and with a factor
67 // related to the mobility status of the cells
68 double load_weight = 0.0;
69
70 for (const auto &cell : triangulation->active_cell_iterators())
71 {
72 if (cell->is_locally_owned())
73 {
74 // Apply the cell weight
75 Point<dim> cell_barycenter = cell->center();
76 load_weight +=
77 static_cast<int>(cell_weight_function->value(cell_barycenter));
78
79 // Get the mobility status of the cell and the number of particles
80 const unsigned int cell_mobility_status =
81 adaptive_sparse_contacts->check_cell_mobility(cell);
82 const unsigned int n_particles_in_cell =
83 particle_handler->n_particles_in_cell(cell);
84
85 // Apply a factor on the particle weight regards the
86 // mobility status. alpha = 1 by default for mobile cell, but
87 // is modified if cell is active or inactive
88 double alpha = 1.0;
89 if (cell_mobility_status ==
91 PropertiesIndex>::static_active ||
92 cell_mobility_status ==
94 PropertiesIndex>::advected_active)
95 {
97 }
98 else if (cell_mobility_status ==
100 PropertiesIndex>::inactive ||
101 cell_mobility_status ==
103 {
105 }
106
107 // Add the particle weight time the number of particles in the
108 // cell to the processor load
109 load_weight += alpha * n_particles_in_cell * particle_weight;
110 }
111 }
112
113 // Find the minimum load on a processor
114 double maximum_load_on_proc =
115 Utilities::MPI::max(load_weight, mpi_communicator);
116
117 // Find the minimum load on a processor
118 double minimum_load_on_proc =
119 Utilities::MPI::min(load_weight, mpi_communicator);
120
121 // Get the total load
122 double total_load = Utilities::MPI::sum(load_weight, mpi_communicator);
123
124 if ((maximum_load_on_proc - minimum_load_on_proc) >
125 load_threshold * (total_load / n_mpi_processes))
126 {
127 // Clear and connect a new cell weight function
129
131 }
132 }
133
134 // Clear and connect a new cell weight function with new mobility status
136}
138template <int dim, typename PropertiesIndex>
139unsigned int
141 const typename parallel::distributed::Triangulation<dim>::cell_iterator &cell,
142 const CellStatus status) const
143{
144 // Assign no weight to cells we do not own.
145 if (!cell->is_locally_owned())
146 return 0;
148 switch (status)
149 {
150 case CellStatus::cell_will_persist:
151 case CellStatus::cell_will_be_refined:
152 // If CELL_PERSIST, do as CELL_REFINE
153 {
154 const unsigned int n_particles_in_cell =
155 particle_handler->n_particles_in_cell(cell);
156 return n_particles_in_cell * particle_weight;
157 }
158 case CellStatus::cell_invalid:
159 break;
160
161 case CellStatus::children_will_be_coarsened:
162 {
163 unsigned int n_particles_in_cell = 0;
164
165 for (unsigned int child_index = 0;
166 child_index < GeometryInfo<dim>::max_children_per_cell;
167 ++child_index)
168 n_particles_in_cell +=
169 particle_handler->n_particles_in_cell(cell->child(child_index));
170
171 return n_particles_in_cell * particle_weight;
172 }
173 default:
174 Assert(false, ExcInternalError());
175 break;
176 }
177
178 return 0;
179}
180
181template <int dim, typename PropertiesIndex>
182unsigned int
185 const typename parallel::distributed::Triangulation<dim>::cell_iterator
186 &cell,
187 const CellStatus status) const
188{
189 // Assign no weight to cells we do not own.
190 if (!cell->is_locally_owned())
191 return 0;
192
193 // Get mobility status of the cell
194 const unsigned int cell_mobility_status =
195 adaptive_sparse_contacts->check_cell_mobility(cell);
196
197 // Applied a factor on the particle weight regards the mobility status
198 // Factor of 1 when mobile cell
199 double alpha = 1.0;
200 if (cell_mobility_status ==
202 cell_mobility_status ==
204 {
205 alpha = active_status_factor;
206 }
207 else if (cell_mobility_status ==
209 cell_mobility_status ==
211 {
213 }
214
215 switch (status)
216 {
217 case dealii::CellStatus::cell_will_persist:
218 case dealii::CellStatus::cell_will_be_refined:
219 {
220 const unsigned int n_particles_in_cell =
221 particle_handler->n_particles_in_cell(cell);
222 return static_cast<int>(alpha * n_particles_in_cell *
224 }
225 case dealii::CellStatus::cell_invalid:
226 break;
227 case dealii::CellStatus::children_will_be_coarsened:
228 {
229 unsigned int n_particles_in_cell = 0;
230
231 for (unsigned int child_index = 0;
232 child_index < GeometryInfo<dim>::max_children_per_cell;
233 ++child_index)
234 n_particles_in_cell +=
235 particle_handler->n_particles_in_cell(cell->child(child_index));
236
237 return static_cast<int>(alpha * n_particles_in_cell *
239 }
240 default:
241 Assert(false, ExcInternalError());
242 break;
243 }
244
245 return 0;
246}
247
249template class LagrangianLoadBalancing<2,
251template class LagrangianLoadBalancing<2,
253
254template class LagrangianLoadBalancing<
Mechanism that disables contacts computation through mobility status of cells based on granular tempe...
static DEMActionManager * get_action_manager()
Getter of the unique instance of the DEMActionManager.
void load_balance_step()
Set triggers for actions to be performed in the current iteration because it is a load balancing step...
Manages the load balancing (repartitioning), of the domain according to the computational load of the...
Particles::ParticleHandler< dim > * particle_handler
Pointer to the particle handler object.
AdaptiveSparseContacts< dim, PropertiesIndex > * adaptive_sparse_contacts
Pointer to the adaptive sparse contacts object.
void connect_mobility_status_weight_signals()
Connects the weight signals of the cells to the triangulation with the mobility status....
const unsigned int this_mpi_process
Rank of the current MPI process.
std::shared_ptr< SimulationControl > simulation_control
Pointer to the simulation control object.
double active_status_factor
Load weight factor of particle weight in a cell with an active mobility status (only with ASC).
const unsigned int n_mpi_processes
Number of MPI processes.
unsigned int load_balance_step
Iteration number for load balancing execution, only when method is once.
parallel::distributed::Triangulation< dim > * triangulation
Pointer to the triangulation object.
double inactive_status_factor
Load weight factor of particle weight in a cell with an inactive mobility status (only with ASC).
std::shared_ptr< Function< dim > > cell_weight_function
void check_load_balance_once() const
Determines whether the present iteration is the load balance step when load balance method is once.
unsigned int dynamic_check_frequency
Frequency of check for the load balancing execution, only when method is dynamic.
unsigned int particle_weight
Load weight of a particle, the default parameters is 10000.
void check_load_balance_with_sparse_contacts()
Determines whether the present is the load balance step when load balance method is dynamic_with_spar...
unsigned int calculate_total_cell_weight(const typename parallel::distributed::Triangulation< dim >::cell_iterator &cell, const CellStatus status) const
Indicates to the triangulation how much computational work is expected to happen on this cell,...
double load_threshold
Threshold (\beta) for the check load balancing execution, only when method is dynamic.
unsigned int calculate_total_cell_weight_with_mobility_status(const typename parallel::distributed::Triangulation< dim >::cell_iterator &cell, const CellStatus status) const
Indicates to the triangulation how much computational work is expected to happen on this cell,...
void check_load_balance_dynamic()
Determines whether the present iteration is the load balance step when load balance method is dynamic...
MPI_Comm mpi_communicator
MPI communicator.
void check_load_balance_frequent() const
Determines whether the present iteration is the load balance step when load balance method is frequen...
PropertiesIndex
Particle property indices for CFD-DEM multiphysics simulations.
PropertiesIndex
Particle property indices for CFD-DEM simulations.
PropertiesIndex
Particle property indices for DEM multiphysics simulations.