Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
log_collision_data.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2025-2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
5
6#include <deal.II/base/mpi.h>
7
8using namespace dealii;
9
10template <int dim, typename PropertiesIndex>
11void
13 const DEMSolverParameters<dim> &parameters,
15 &particle_wall_pairs_in_contact,
16 const double current_time,
17 OngoingCollisionLog<dim> &ongoing_collision_log,
18 CompletedCollisionLog<dim> &collision_event_log)
19{
20 // Looping over all the active particles in particle-wall pairs
21 for (auto &&pairs_in_contact_content :
22 particle_wall_pairs_in_contact | boost::adaptors::map_values)
23 {
24 // Iterating over a map which contains the required information for
25 // calculation of the contact force for each particle
26 for (auto &&contact_info :
27 pairs_in_contact_content | boost::adaptors::map_values)
28 {
29 // Defining local variables which will be used within the contact
30 // calculation
31 auto particle = contact_info.particle;
32 auto particle_properties = particle->get_properties();
33 Point<3> point_on_boundary = contact_info.point_on_boundary;
34 // Normal vector from the wall to the particle
35 Tensor<1, 3> normal_vector = contact_info.normal_vector;
36 // Getting particle 3d location
37 Point<3> particle_location_3d = get_location(particle);
38 // Defining a tensor which connects the point_on_boundary to the
39 // center of particle
40 Tensor<1, 3> point_to_particle_vector =
41 particle_location_3d - point_on_boundary;
42 Tensor<1, 3> projected_vector =
43 find_projection(point_to_particle_vector, normal_vector);
44 // Calculating the particle-wall distance using the projected vector
45 double normal_overlap =
46 ((particle_properties[PropertiesIndex::dp]) * 0.5) -
47 (projected_vector.norm());
48 types::boundary_id boundary_id = contact_info.boundary_id;
49
50 // If we log all walls or if the boundary ID is in the list of
51 // particle-wall collision boundary IDs, we log the collision
52 // information.
54 std::find(parameters.post_processing
56 parameters.post_processing
58 boundary_id) !=
60 .end())
61 {
62 unsigned int particle_id = particle->get_id();
63 if (normal_overlap > 0 &&
64 !ongoing_collision_log.is_in_collision(particle_id,
65 boundary_id))
66 {
67 collision_log<dim> start_log;
68 start_log.particle_id = particle_id;
69 start_log.dp = particle_properties[PropertiesIndex::dp];
70 start_log.mass = particle_properties[PropertiesIndex::mass];
71 start_log.velocity[0] =
72 particle_properties[PropertiesIndex::v_x];
73 start_log.velocity[1] =
74 particle_properties[PropertiesIndex::v_y];
75 start_log.velocity[2] =
76 particle_properties[PropertiesIndex::v_z];
77 start_log.omega[0] =
78 particle_properties[PropertiesIndex::omega_x];
79 start_log.omega[1] =
80 particle_properties[PropertiesIndex::omega_y];
81 start_log.omega[2] =
82 particle_properties[PropertiesIndex::omega_z];
83 start_log.time = current_time;
84 start_log.boundary_id = contact_info.boundary_id;
85 ongoing_collision_log.start_collision(
86 start_log); // Start logging the collision
87
88 // Print the start of the collision in the terminal if
89 // verbosity is set to verbose
90 if (parameters.post_processing.collision_verbosity ==
92 {
93 std::cout
94 << "Collision with boundary " << start_log.boundary_id
95 << " started for particle " << particle_id << std::endl;
96 }
97 }
98
99 // If the particle does not have a positive overlap anymore, the
100 // collision has ended. Consequently, it now needs to be
101 // removed from ongoing_collision_log since the contact has
102 // reached its end.
103 if (normal_overlap < 0 &&
104 ongoing_collision_log.is_in_collision(particle_id,
105 boundary_id))
106 {
107 collision_log<dim> end_log;
108 end_log.particle_id = particle_id;
109 end_log.dp = particle_properties[PropertiesIndex::dp];
110 end_log.mass = particle_properties[PropertiesIndex::mass];
111 end_log.velocity[0] =
112 particle_properties[PropertiesIndex::v_x];
113 end_log.velocity[1] =
114 particle_properties[PropertiesIndex::v_y];
115 end_log.velocity[2] =
116 particle_properties[PropertiesIndex::v_z];
117 end_log.omega[0] =
118 particle_properties[PropertiesIndex::omega_x];
119 end_log.omega[1] =
120 particle_properties[PropertiesIndex::omega_y];
121 end_log.omega[2] =
122 particle_properties[PropertiesIndex::omega_z];
123 end_log.time = current_time;
124 collision_log<dim> start_log;
125
126 // End the collision for the particle and retrieve the start
127 // log
128 ongoing_collision_log.end_collision(particle_id,
129 boundary_id,
130 start_log);
131 end_log.boundary_id = start_log.boundary_id;
133 event.particle_id = particle_id;
134 event.start_log = start_log;
135 event.end_log = end_log;
136
137 // Add the completed collision event to the collision event
138 // log
139 collision_event_log.add_event(event);
140
141 // Print the end of the collision in the terminal if verbosity
142 // is set to verbose
143 if (parameters.post_processing.collision_verbosity ==
145 {
146 std::cout << "Collision with boundary "
147 << end_log.boundary_id << " ended for particle "
148 << particle_id << std::endl;
149 }
150 }
151 }
152 }
153 }
154}
155
156template <int dim>
157void
159 const CompletedCollisionLog<dim> &collision_event_log,
160 const MPI_Comm &mpi_communicator)
161{
162 // MPI processes information
163 const unsigned int this_mpi_process =
164 Utilities::MPI::this_mpi_process(mpi_communicator);
165 const unsigned int n_mpi_processes =
166 Utilities::MPI::n_mpi_processes(mpi_communicator);
167
168 // Separator and filename for the output file
169 std::string sep;
170 std::string filename = parameters.post_processing.collision_stats_file_name;
171
172 // Check if a .csv or .dat extension is specified in the filename, if not add
173 // ".csv"
174 std::size_t csv_file = filename.find(".csv");
175 std::size_t dat_file = filename.find(".dat");
176
177 if ((csv_file == std::string::npos) && (dat_file == std::string::npos))
178 filename += ".csv";
179
180 // Open the file for writing or appending based on the MPI process.
181 // This forces a barrier after MPI processes and will undoubtedly be slow in
182 // large parallel simulations. If this becomes an issue, the function should
183 // be ported to use MPI I/O or a format like HDF5.
184 for (unsigned int i = 0; i < n_mpi_processes; ++i)
185 {
186 if (this_mpi_process == i)
187 {
188 std::ofstream my_file;
189
190 if (this_mpi_process == 0)
191 {
192 // If this is the first MPI process, we write the header
193 my_file.open(filename);
194 if (filename.substr(filename.find_last_of('.') + 1) == ".dat")
195 {
196 my_file
197 << "particle_id diameter mass boundary_id start_time end_time start_particle_velocity_x start_particle_velocity_y start_particle_velocity_z start_particle_angular_velocity_x start_particle_angular_velocity_y start_particle_angular_velocity_z end_particle_velocity_x end_particle_velocity_y end_particle_velocity_z end_particle_angular_velocity_x end_particle_angular_velocity_y end_particle_angular_velocity_z"
198 << std::endl;
199 sep = " ";
200 }
201 else // .csv is default
202 {
203 my_file
204 << "particle_id,diameter,mass,boundary_id,start_time,end_time,start_particle_velocity_x,start_particle_velocity_y,start_particle_velocity_z,start_particle_angular_velocity_x,start_particle_angular_velocity_y,start_particle_angular_velocity_z,end_particle_velocity_x,end_particle_velocity_y,end_particle_velocity_z,end_particle_angular_velocity_x,end_particle_angular_velocity_y,end_particle_angular_velocity_z"
205 << std::endl;
206 sep = ",";
207 }
208 }
209 else
210 {
211 // If this is not the first MPI process, we open the file for
212 // appending
213 my_file.open(filename, std::ios::app);
214 if (filename.substr(filename.find_last_of('.') + 1) == ".dat")
215 sep = " ";
216 else // .csv is default
217 sep = ",";
218 }
219
220 // Write the collision statistics
221 for (const auto &event : collision_event_log.get_events())
222 {
223 const auto &start = event.start_log;
224 const auto &end = event.end_log;
225
226 // Write the collision data to the file
227 my_file << start.particle_id << sep << start.dp << sep
228 << start.mass << sep
229 << static_cast<int>(start.boundary_id) << sep
230 << start.time << sep << end.time << sep
231 << start.velocity[0] << sep << start.velocity[1] << sep
232 << start.velocity[2] << sep << start.omega[0] << sep
233 << start.omega[1] << sep << start.omega[2] << sep
234 << end.velocity[0] << sep << end.velocity[1] << sep
235 << end.velocity[2] << sep << end.omega[0] << sep
236 << end.omega[1] << sep << end.omega[2] << std::endl;
237 }
238 my_file.close();
239 }
240 // Ensure all MPI processes reach this point before continuing.
241 // The barrier forces the synchronisation of the processes.
242 MPI_Barrier(mpi_communicator);
243 }
244}
245
246template void
248 const DEMSolverParameters<2> &parameters,
250 &particle_wall_pairs_in_contact,
251 const double current_time,
252 OngoingCollisionLog<2> &ongoing_collision_log,
253 CompletedCollisionLog<2> &collision_event_log);
254
255template void
257 const DEMSolverParameters<3> &parameters,
259 &particle_wall_pairs_in_contact,
260 const double current_time,
261 OngoingCollisionLog<3> &ongoing_collision_log,
262 CompletedCollisionLog<3> &collision_event_log);
263
264template void
266 const DEMSolverParameters<2> &parameters,
268 &particle_wall_pairs_in_contact,
269 const double current_time,
270 OngoingCollisionLog<2> &ongoing_collision_log,
271 CompletedCollisionLog<2> &collision_event_log);
272
273template void
275 const DEMSolverParameters<3> &parameters,
277 &particle_wall_pairs_in_contact,
278 const double current_time,
279 OngoingCollisionLog<3> &ongoing_collision_log,
280 CompletedCollisionLog<3> &collision_event_log);
281
282template void
284 const DEMSolverParameters<2> &parameters,
286 &particle_wall_pairs_in_contact,
287 const double current_time,
288 OngoingCollisionLog<2> &ongoing_collision_log,
289 CompletedCollisionLog<2> &collision_event_log);
290
291template void
293 const DEMSolverParameters<2> &parameters,
295 &particle_wall_pairs_in_contact,
296 const double current_time,
297 OngoingCollisionLog<2> &ongoing_collision_log,
298 CompletedCollisionLog<2> &collision_event_log);
299
300template void
302 const DEMSolverParameters<3> &parameters,
304 &particle_wall_pairs_in_contact,
305 const double current_time,
306 OngoingCollisionLog<3> &ongoing_collision_log,
307 CompletedCollisionLog<3> &collision_event_log);
308
309template void
311 const DEMSolverParameters<3> &parameters,
313 &particle_wall_pairs_in_contact,
314 const double current_time,
315 OngoingCollisionLog<3> &ongoing_collision_log,
316 CompletedCollisionLog<3> &collision_event_log);
317
318template void
320 const CompletedCollisionLog<2> &collision_event_log,
321 const MPI_Comm &mpi_communicator);
322
323template void
325 const CompletedCollisionLog<3> &collision_event_log,
326 const MPI_Comm &mpi_communicator);
Class that stores all the completed collision events.
void add_event(const collision_event< dim > &event)
Add a completed event to the log.
const std::list< collision_event< dim > > & get_events() const
Retrieve the list of all completed collision events.
Handles all the parameters declared in the parameter handler file.
Parameters::Lagrangian::LagrangianPostProcessing post_processing
Class managing the ongoing collisions.
bool is_in_collision(const unsigned int particle_id, const types::boundary_id boundary_id) const
Check whether the particle is currently in a collision.
void end_collision(const unsigned int particle_id, const types::boundary_id boundary_id, collision_log< dim > &start_log)
End a collision for a particle. Retrieve the start log and remove the particle from the ongoing colli...
void start_collision(const collision_log< dim > &log)
Start logging a collision for a particle. If the particle is already present in the log,...
template void log_collision_data< 3, DEM::DEMMPProperties::PropertiesIndex >(const DEMSolverParameters< 3 > &parameters, dem_data_structures< 3 >::particle_wall_in_contact &particle_wall_pairs_in_contact, const double current_time, OngoingCollisionLog< 3 > &ongoing_collision_log, CompletedCollisionLog< 3 > &collision_event_log)
template void log_collision_data< 3, DEM::CFDDEMProperties::PropertiesIndex >(const DEMSolverParameters< 3 > &parameters, dem_data_structures< 3 >::particle_wall_in_contact &particle_wall_pairs_in_contact, const double current_time, OngoingCollisionLog< 3 > &ongoing_collision_log, CompletedCollisionLog< 3 > &collision_event_log)
template void log_collision_data< 2, DEM::CFDDEMMPProperties::PropertiesIndex >(const DEMSolverParameters< 2 > &parameters, dem_data_structures< 2 >::particle_wall_in_contact &particle_wall_pairs_in_contact, const double current_time, OngoingCollisionLog< 2 > &ongoing_collision_log, CompletedCollisionLog< 2 > &collision_event_log)
void log_collision_data(const DEMSolverParameters< dim > &parameters, typename DEM::dem_data_structures< dim >::particle_wall_in_contact &particle_wall_pairs_in_contact, const double current_time, OngoingCollisionLog< dim > &ongoing_collision_log, CompletedCollisionLog< dim > &collision_event_log)
Logs the particle-wall contact statistics.
template void log_collision_data< 3, DEM::CFDDEMMPProperties::PropertiesIndex >(const DEMSolverParameters< 3 > &parameters, dem_data_structures< 3 >::particle_wall_in_contact &particle_wall_pairs_in_contact, const double current_time, OngoingCollisionLog< 3 > &ongoing_collision_log, CompletedCollisionLog< 3 > &collision_event_log)
template void write_collision_stats< 2 >(const DEMSolverParameters< 2 > &parameters, const CompletedCollisionLog< 2 > &collision_event_log, const MPI_Comm &mpi_communicator)
void write_collision_stats(const DEMSolverParameters< dim > &parameters, const CompletedCollisionLog< dim > &collision_event_log, const MPI_Comm &mpi_communicator)
Writes the collision statistics to a file.
template void log_collision_data< 2, DEM::DEMMPProperties::PropertiesIndex >(const DEMSolverParameters< 2 > &parameters, dem_data_structures< 2 >::particle_wall_in_contact &particle_wall_pairs_in_contact, const double current_time, OngoingCollisionLog< 2 > &ongoing_collision_log, CompletedCollisionLog< 2 > &collision_event_log)
template void log_collision_data< 2, DEM::CFDDEMProperties::PropertiesIndex >(const DEMSolverParameters< 2 > &parameters, dem_data_structures< 2 >::particle_wall_in_contact &particle_wall_pairs_in_contact, const double current_time, OngoingCollisionLog< 2 > &ongoing_collision_log, CompletedCollisionLog< 2 > &collision_event_log)
template void log_collision_data< 3, DEM::DEMProperties::PropertiesIndex >(const DEMSolverParameters< 3 > &parameters, dem_data_structures< 3 >::particle_wall_in_contact &particle_wall_pairs_in_contact, const double current_time, OngoingCollisionLog< 3 > &ongoing_collision_log, CompletedCollisionLog< 3 > &collision_event_log)
template void log_collision_data< 2, DEM::DEMProperties::PropertiesIndex >(const DEMSolverParameters< 2 > &parameters, dem_data_structures< 2 >::particle_wall_in_contact &particle_wall_pairs_in_contact, const double current_time, OngoingCollisionLog< 2 > &ongoing_collision_log, CompletedCollisionLog< 2 > &collision_event_log)
template void write_collision_stats< 3 >(const DEMSolverParameters< 3 > &parameters, const CompletedCollisionLog< 3 > &collision_event_log, const MPI_Comm &mpi_communicator)
Tensor< 1, 3 > find_projection(const Tensor< 1, 3 > &vector_a, const Tensor< 1, 3 > &vector_b)
This function is used to find the projection of vector_a on vector_b.
Point< 3 > get_location(const Particles::ParticleIterator< dim > &particle)
Get the 3d location of the particle.
ankerl::unordered_dense::map< types::particle_index, ankerl::unordered_dense::map< global_face_id, particle_wall_contact_info< dim > > > particle_wall_in_contact
Parameters::Verbosity collision_verbosity
Verbosity level for collision statistics output.
std::vector< int > particle_wall_collision_boundary_ids
Boundary IDs of the walls for which collisions are logged.
std::string collision_stats_file_name
File name for exporting collision statistics (CSV format).
bool log_collisions_with_all_walls
Log collisions with all walls (if false, only selected boundaries).
Struct storing a full collision event (start and end).
unsigned int particle_id
Struct to store information about a particle-wall collision moment.
types::boundary_id boundary_id
Tensor< 1, dim > omega
unsigned int particle_id
Tensor< 1, dim > velocity