Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
velocity_verlet_integrator.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
6
9
10using namespace DEM;
11
12template <int dim, typename PropertiesIndex>
13void
15 Particles::ParticleHandler<dim> &particle_handler,
16 const Tensor<1, 3> &g,
17 const double dt,
18 std::vector<Tensor<1, 3>> &torque,
19 std::vector<Tensor<1, 3>> &force,
20 const std::vector<double> &MOI)
21{
22 const Tensor<1, 3> half_dt_g = 0.5 * g * dt;
23
24 for (auto particle = particle_handler.begin();
25 particle != particle_handler.end();
26 ++particle)
27 {
28 // Get the total array view to the particle properties once to improve
29 // efficiency
30 auto particle_properties = particle->get_properties();
31 auto particle_position = particle->get_location();
32 types::particle_index particle_id = particle->get_local_index();
33
34 Tensor<1, 3> &particle_torque = torque[particle_id];
35 Tensor<1, 3> &particle_force = force[particle_id];
36
37 const double half_dt_mass_inverse =
38 0.5 * dt / particle_properties[PropertiesIndex::mass];
39 const double half_dt_MOI_inverse = 0.5 * dt / MOI[particle_id];
40
41 // The velocities are advanced over all three components, even in 2D,
42 // since the third component is the one that carries the rotation in 2D
43 for (unsigned int d = 0; d < 3; ++d)
44 {
45 // Half-step velocity
46 particle_properties[PropertiesIndex::v_x + d] +=
47 half_dt_g[d] + particle_force[d] * half_dt_mass_inverse;
48
49 // Half-step angular velocity
50 particle_properties[PropertiesIndex::omega_x + d] +=
51 particle_torque[d] * half_dt_MOI_inverse;
52 }
53
54 // Update particle position over a full time step using the half-step
55 // velocity
56 for (int d = 0; d < dim; ++d)
57 particle_position[d] +=
58 particle_properties[PropertiesIndex::v_x + d] * dt;
59
60 // Reinitialize force and torque of particle
61 particle_force = 0.;
62 particle_torque = 0.;
63
64 particle->set_location(particle_position);
65 }
66}
67
68template <int dim, typename PropertiesIndex>
69void
71 Particles::ParticleHandler<dim> &particle_handler,
72 const Tensor<1, 3> &g,
73 const double dt,
74 std::vector<Tensor<1, 3>> &torque,
75 std::vector<Tensor<1, 3>> &force,
76 const std::vector<double> &MOI)
77{
78 const Tensor<1, 3> half_dt_g = 0.5 * g * dt;
79
80 for (auto &particle : particle_handler)
81 {
82 // Get the total array view to the particle properties once to improve
83 // efficiency
84 types::particle_index particle_id = particle.get_local_index();
85
86 auto particle_properties = particle.get_properties();
87 Tensor<1, 3> &particle_torque = torque[particle_id];
88 Tensor<1, 3> &particle_force = force[particle_id];
89
90 const double half_dt_mass_inverse =
91 0.5 * dt / particle_properties[PropertiesIndex::mass];
92 const double half_dt_MOI_inverse = 0.5 * dt / MOI[particle_id];
93
94 // Loop is manually unrolled for performance
95 // Particle velocity integration
96 particle_properties[PropertiesIndex::v_x] +=
97 half_dt_g[0] + particle_force[0] * half_dt_mass_inverse;
98 particle_properties[PropertiesIndex::v_y] +=
99 half_dt_g[1] + particle_force[1] * half_dt_mass_inverse;
100 particle_properties[PropertiesIndex::v_z] +=
101 half_dt_g[2] + particle_force[2] * half_dt_mass_inverse;
102
103 // Updating angular velocity
104 particle_properties[PropertiesIndex::omega_x] +=
105 particle_torque[0] * half_dt_MOI_inverse;
106 particle_properties[PropertiesIndex::omega_y] +=
107 particle_torque[1] * half_dt_MOI_inverse;
108 particle_properties[PropertiesIndex::omega_z] +=
109 particle_torque[2] * half_dt_MOI_inverse;
110
111 // Reinitialize force and torque of particle
112 particle_force = 0.;
113 particle_torque = 0.;
114 }
115}
116
117template <int dim, typename PropertiesIndex>
118void
120 Particles::ParticleHandler<dim> &particle_handler,
121 const Tensor<1, 3> &g,
122 const double dt,
123 std::vector<Tensor<1, 3>> &torque,
124 std::vector<Tensor<1, 3>> &force,
125 const std::vector<double> &MOI,
126 const parallel::distributed::Triangulation<dim> &triangulation,
127 AdaptiveSparseContacts<dim, PropertiesIndex> &sparse_contacts_object)
128{
129 auto *action_manager = DEMActionManager::get_action_manager();
130
131 const bool use_default_function =
132 !action_manager->check_sparse_contacts_enabled() ||
133 action_manager->check_mobility_status_reset();
134
135 // Regular synchronization process if sparse contacts are not enabled or if
136 // this is the first iteration.
137 if (use_default_function)
138 {
139 integrate_end(particle_handler, g, dt, torque, force, MOI);
140 return;
141 }
142
143 const Tensor<1, 3> half_dt_g = 0.5 * g * dt;
144
145 // Get the map of mobility status of cells
146 auto &cell_mobility_status_map = sparse_contacts_object.get_mobility_status();
147
148 // Contrary to the regular integration, the particles of the advected cells do
149 // not need any special treatment here since no position is updated by the
150 // closing half step.
151 for (auto &cell : triangulation.active_cell_iterators())
152 {
153 if (!cell->is_locally_owned())
154 continue;
155
156 // Get the mobility status of the cell
157 const unsigned int mobility_status =
158 cell_mobility_status_map.at(cell->active_cell_index());
159
160 // We loop over the particles, even if cell is not mobile, to reset the
161 // force and torques value of the particle with the particle id
162 auto particles_in_cell = particle_handler.particles_in_cell(cell);
163
164 if (particle_handler.n_particles_in_cell(cell) == 0)
165 continue;
166
167 if (mobility_status ==
169 {
170 for (auto &particle : particles_in_cell)
171 {
172 types::particle_index particle_id = particle.get_local_index();
173 auto particle_properties = particle.get_properties();
174
175 Tensor<1, 3> &particle_torque = torque[particle_id];
176 Tensor<1, 3> &particle_force = force[particle_id];
177
178 const double half_dt_mass_inverse =
179 0.5 * dt / particle_properties[PropertiesIndex::mass];
180 const double half_dt_MOI_inverse = 0.5 * dt / MOI[particle_id];
181
182 for (unsigned int d = 0; d < 3; ++d)
183 {
184 // Particle velocity integration
185 particle_properties[PropertiesIndex::v_x + d] +=
186 half_dt_g[d] + particle_force[d] * half_dt_mass_inverse;
187
188 // Updating angular velocity
189 particle_properties[PropertiesIndex::omega_x + d] +=
190 particle_torque[d] * half_dt_MOI_inverse;
191 }
192
193 // Reinitialize force and torque of particle
194 particle_force = 0.;
195 particle_torque = 0.;
196 }
197 }
198 else // Active and inactive cells
199 {
200 for (auto &particle : particles_in_cell)
201 {
202 types::particle_index particle_id = particle.get_local_index();
203
204 // Reset forces
205 force[particle_id] = 0.;
206 torque[particle_id] = 0.;
207 }
208 }
209 }
210}
211
212template <int dim, typename PropertiesIndex>
213void
215 Particles::ParticleHandler<dim> &particle_handler,
216 const Tensor<1, 3> &g,
217 const double dt,
218 std::vector<Tensor<1, 3>> &torque,
219 std::vector<Tensor<1, 3>> &force,
220 const std::vector<double> &MOI)
221{
222 Point<3> particle_position;
223 const Tensor<1, 3> dt_g = g * dt;
224
225 for (auto &particle : particle_handler)
226 {
227 // Get the total array view to the particle properties once to improve
228 // efficiency
229 types::particle_index particle_id = particle.get_local_index();
230
231 auto particle_properties = particle.get_properties();
232 Tensor<1, 3> &particle_torque = torque[particle_id];
233 Tensor<1, 3> &particle_force = force[particle_id];
234
235 double dt_mass_inverse = dt / particle_properties[PropertiesIndex::mass];
236 double dt_MOI_inverse = dt / MOI[particle_id];
237
238 particle_position = [&] {
239 if constexpr (dim == 3)
240 {
241 return particle.get_location();
242 }
243 else
244 {
245 return (point_nd_to_3d(particle.get_location()));
246 }
247 }();
248
249 // Loop is manually unrolled for performance reason.
250 // for (int d = 0; d < 3; ++d)
251 {
252 // Particle velocity integration
253 particle_properties[PropertiesIndex::v_x] +=
254 dt_g[0] + particle_force[0] * dt_mass_inverse;
255 particle_properties[PropertiesIndex::v_y] +=
256 dt_g[1] + particle_force[1] * dt_mass_inverse;
257 particle_properties[PropertiesIndex::v_z] +=
258 dt_g[2] + particle_force[2] * dt_mass_inverse;
259
260
261 // Particle location integration
262 particle_position[0] += particle_properties[PropertiesIndex::v_x] * dt;
263 particle_position[1] += particle_properties[PropertiesIndex::v_y] * dt;
264 particle_position[2] += particle_properties[PropertiesIndex::v_z] * dt;
265
266 // Updating angular velocity
267 particle_properties[PropertiesIndex::omega_x] +=
268 particle_torque[0] * dt_MOI_inverse;
269 particle_properties[PropertiesIndex::omega_y] +=
270 particle_torque[1] * dt_MOI_inverse;
271 particle_properties[PropertiesIndex::omega_z] +=
272 particle_torque[2] * dt_MOI_inverse;
273 }
274
275 // Reinitialize force and torque of particle
276 particle_force = 0;
277 particle_torque = 0;
278
279 if constexpr (dim == 3)
280 particle.set_location(particle_position);
281
282 if constexpr (dim == 2)
283 {
284 Point<2> position_2d;
285 position_2d[0] = particle_position[0];
286 position_2d[1] = particle_position[1];
287 particle.set_location(position_2d);
288 }
289 }
290}
291
292template <int dim, typename PropertiesIndex>
293void
295 Particles::ParticleHandler<dim> &particle_handler,
296 const Tensor<1, 3> &g,
297 const double dt,
298 std::vector<Tensor<1, 3>> &torque,
299 std::vector<Tensor<1, 3>> &force,
300 const std::vector<double> &MOI,
301 const parallel::distributed::Triangulation<dim> &triangulation,
302 AdaptiveSparseContacts<dim, PropertiesIndex> &sparse_contacts_object)
303{
304 auto *action_manager = DEMActionManager::get_action_manager();
305
306 bool use_default_function =
307 !action_manager->check_sparse_contacts_enabled() ||
308 action_manager->check_mobility_status_reset();
309
310 // Regular integration process if sparse contacts are not enabled or if this
311 // is the first iteration.
312 if (use_default_function)
313 {
314 integrate(particle_handler, g, dt, torque, force, MOI);
315 return;
316 }
317
318 // If there are advected particles, we use another function since the average
319 // velocity and acceleration of cells are computed for mobile cells
320 if (sparse_contacts_object.has_advected_particles())
321 {
322 integrate_with_advected_particles(particle_handler,
323 g,
324 dt,
325 torque,
326 force,
327 MOI,
328 triangulation,
329 sparse_contacts_object);
330 }
331 else
332 {
333 Point<3> particle_position;
334 const Tensor<1, 3> dt_g = g * dt;
335
336 // Get the map of mobility status of cells
337 auto &cell_mobility_status_map =
338 sparse_contacts_object.get_mobility_status();
339
340 for (auto &cell : triangulation.active_cell_iterators())
341 {
342 if (cell->is_locally_owned())
343 {
344 // Get the mobility status of the cell
345 unsigned int mobility_status =
346 cell_mobility_status_map.at(cell->active_cell_index());
347
348 // We loop over the particles, even if cell is not mobile, to
349 // reset the force and torques value of the particle with the
350 // particle id
351 auto particles_in_cell = particle_handler.particles_in_cell(cell);
352 unsigned int n_particles_in_cell =
353 particle_handler.n_particles_in_cell(cell);
354
355 if (n_particles_in_cell > 0)
356 {
357 if (mobility_status ==
359 {
360 for (auto &particle : particles_in_cell)
361 {
362 types::particle_index particle_id =
363 particle.get_local_index();
364 auto particle_properties = particle.get_properties();
365
366 Tensor<1, 3> &particle_torque = torque[particle_id];
367 Tensor<1, 3> &particle_force = force[particle_id];
368
369 double dt_mass_inverse =
370 dt / particle_properties[PropertiesIndex::mass];
371 double dt_MOI_inverse = dt / MOI[particle_id];
372
373 particle_position = [&] {
374 if constexpr (dim == 3)
375 {
376 return particle.get_location();
377 }
378 else
379 {
380 return (
381 point_nd_to_3d(particle.get_location()));
382 }
383 }();
384
385 for (unsigned int d = 0; d < 3; ++d)
386 {
387 // Particle velocity integration
388 particle_properties[PropertiesIndex::v_x + d] +=
389 dt_g[d] + particle_force[d] * dt_mass_inverse;
390
391 // Particle location integration
392 particle_position[d] +=
393 particle_properties[PropertiesIndex::v_x + d] *
394 dt;
395
396 // Updating angular velocity
397 particle_properties[PropertiesIndex::omega_x +
398 d] +=
399 particle_torque[d] * dt_MOI_inverse;
400 }
401
402 // Reinitialize force and torque of particle
403 particle_force = 0.0;
404 particle_torque = 0.0;
405
406 // Update particle location
407 if constexpr (dim == 3)
408 particle.set_location(particle_position);
409
410 if constexpr (dim == 2)
411 {
412 Point<2> position_2d;
413 position_2d[0] = particle_position[0];
414 position_2d[1] = particle_position[1];
415 particle.set_location(position_2d);
416 }
417 }
418 }
419 else // Active and inactive cells
420 {
421 for (auto &particle : particles_in_cell)
422 {
423 types::particle_index particle_id =
424 particle.get_local_index();
425
426 // Reset forces
427 force[particle_id] = 0.0;
428 torque[particle_id] = 0.0;
429 }
430 }
431 }
432 }
433 }
434 }
435}
436
437template <int dim, typename PropertiesIndex>
438void
441 Particles::ParticleHandler<dim> &particle_handler,
442 const Tensor<1, 3> &g,
443 const double dt,
444 std::vector<Tensor<1, 3>> &torque,
445 std::vector<Tensor<1, 3>> &force,
446 const std::vector<double> &MOI,
447 const parallel::distributed::Triangulation<dim> &triangulation,
448 AdaptiveSparseContacts<dim, PropertiesIndex> &sparse_contacts_object)
449{
450 Point<3> particle_position;
451 const Tensor<1, 3> dt_g = g * dt;
452
453 // Get the map of average velocities and accelerations of cells
454 auto cell_velocities_accelerations_map =
455 sparse_contacts_object.get_velocities_accelerations();
456
457 // Get the map of mobility status of cells
458 auto cell_mobility_status_map = sparse_contacts_object.get_mobility_status();
459
460 for (auto &cell : triangulation.active_cell_iterators())
461 {
462 if (cell->is_locally_owned())
463 {
464 // Get the mobility status of the cell
465 unsigned int mobility_status =
466 cell_mobility_status_map.at(cell->active_cell_index());
467
468 // Get the average velocity and acceleration * dt of the cell
469 auto &[velocity_cell_average, acc_dt_cell_average] =
470 cell_velocities_accelerations_map[cell];
471
472 // We loop over the particles, even if cell is not mobile, to reset
473 // the force and torques value of the particle with the particle id
474 auto particles_in_cell = particle_handler.particles_in_cell(cell);
475 unsigned int n_particles_in_cell =
476 particle_handler.n_particles_in_cell(cell);
477
478 if (n_particles_in_cell > 0)
479 {
480 if (mobility_status ==
482 {
483 // When mobile, the average velocity of the cell and the
484 // acceleration is updated at the same step that the particles
485 // are integrated of performance reasons
486 velocity_cell_average.clear();
487 acc_dt_cell_average.clear();
488
489 for (auto &particle : particles_in_cell)
490 {
491 types::particle_index particle_id =
492 particle.get_local_index();
493 auto particle_properties = particle.get_properties();
494
495 Tensor<1, 3> &particle_torque = torque[particle_id];
496 Tensor<1, 3> &particle_force = force[particle_id];
497
498 double dt_mass_inverse =
499 dt / particle_properties[PropertiesIndex::mass];
500 double dt_MOI_inverse = dt / MOI[particle_id];
501
502 particle_position = [&] {
503 if constexpr (dim == 3)
504 {
505 return particle.get_location();
506 }
507 else
508 {
509 return (point_nd_to_3d(particle.get_location()));
510 }
511 }();
512
513 // Calculate acceleration * dt and add velocity value for
514 // average acceleration computation
515 Tensor<1, 3> acc_dt_particle =
516 dt_g + particle_force * dt_mass_inverse;
517 acc_dt_cell_average += acc_dt_particle;
518
519 for (unsigned int d = 0; d < 3; ++d)
520 {
521 // Particle velocity integration
522 particle_properties[PropertiesIndex::v_x + d] +=
523 acc_dt_particle[d];
524
525 // Add velocity value for average velocity computation
526 velocity_cell_average[d] +=
527 particle_properties[PropertiesIndex::v_x + d];
528
529 // Particle location integration
530 particle_position[d] +=
531 particle_properties[PropertiesIndex::v_x + d] * dt;
532
533 // Updating angular velocity
534 particle_properties[PropertiesIndex::omega_x + d] +=
535 particle_torque[d] * dt_MOI_inverse;
536 }
537
538 // Reinitialize force and torque of particle
539 particle_force = 0.0;
540 particle_torque = 0.0;
541
542 // Update particle location
543 if constexpr (dim == 3)
544 particle.set_location(particle_position);
545
546 if constexpr (dim == 2)
547 {
548 Point<2> position_2d;
549 position_2d[0] = particle_position[0];
550 position_2d[1] = particle_position[1];
551 particle.set_location(position_2d);
552 }
553 }
554
555 // Compute average velocity and acceleration
556 acc_dt_cell_average /= n_particles_in_cell;
557 velocity_cell_average /= n_particles_in_cell;
558 }
559 else if (mobility_status ==
561 PropertiesIndex>::advected ||
562 mobility_status ==
564 advected_active)
565 {
566 for (auto &particle : particles_in_cell)
567 {
568 types::particle_index particle_id =
569 particle.get_local_index();
570
571 particle_position = [&] {
572 if constexpr (dim == 3)
573 {
574 return particle.get_location();
575 }
576 else
577 {
578 return (point_nd_to_3d(particle.get_location()));
579 }
580 }();
581
582 // Update the new average velocity of the cell : v + a*dt
583 velocity_cell_average += acc_dt_cell_average;
584
585 // Particle location integration with the average velocity
586 for (unsigned int d = 0; d < 3; ++d)
587 {
588 particle_position[d] += velocity_cell_average[d] * dt;
589 }
590
591 // Reset forces
592 force[particle_id] = 0.0;
593 torque[particle_id] = 0.0;
594
595 // Update particle location
596 if constexpr (dim == 3)
597 particle.set_location(particle_position);
598
599 if constexpr (dim == 2)
600 {
601 Point<2> position_2d;
602 position_2d[0] = particle_position[0];
603 position_2d[1] = particle_position[1];
604 particle.set_location(position_2d);
605 }
606 }
607 }
608 else // Static active and inactive cells (no particles
609 // integration)
610 {
611 for (auto &particle : particles_in_cell)
612 {
613 types::particle_index particle_id =
614 particle.get_local_index();
615
616 // Reset forces
617 force[particle_id] = 0.0;
618 torque[particle_id] = 0.0;
619 }
620 }
621 }
622 }
623 }
624}
625
626
628template class VelocityVerletIntegrator<2,
630template class VelocityVerletIntegrator<2,
632
633template class VelocityVerletIntegrator<
634 2,
637template class VelocityVerletIntegrator<3,
639template class VelocityVerletIntegrator<3,
641
642template class VelocityVerletIntegrator<
643 3,
Mechanism that disables contacts computation through mobility status of cells based on granular tempe...
bool has_advected_particles() const
Give the advected particles flag.
std::map< typename Triangulation< dim >::active_cell_iterator, std::pair< Tensor< 1, 3 >, Tensor< 1, 3 > > > & get_velocities_accelerations()
Give the map of the cell-averaged velocities and accelerations * dt.
DEM::dem_data_structures< dim >::cell_index_int_map & get_mobility_status()
Give the map of the mobility status of the cells.
static DEMActionManager * get_action_manager()
Getter of the unique instance of the DEMActionManager.
Implementation of a classical velocity verlet scheme for the integration of the particle motion....
void integrate_with_advected_particles(Particles::ParticleHandler< dim > &particle_handler, const Tensor< 1, 3 > &body_force, const double time_step, std::vector< Tensor< 1, 3 > > &torque, std::vector< Tensor< 1, 3 > > &force, const std::vector< double > &MOI, const parallel::distributed::Triangulation< dim > &triangulation, AdaptiveSparseContacts< dim, PropertiesIndex > &sparse_contacts_object)
virtual void integrate_start(Particles::ParticleHandler< dim > &particle_handler, const Tensor< 1, 3 > &body_force, const double time_step, std::vector< Tensor< 1, 3 > > &torque, std::vector< Tensor< 1, 3 > > &force, const std::vector< double > &MOI) override
Integrate the opening half step of the Velocity Verlet scheme, which starts the staggering....
virtual void integrate_end(Particles::ParticleHandler< dim > &particle_handler, const Tensor< 1, 3 > &body_force, const double time_step, std::vector< Tensor< 1, 3 > > &torque, std::vector< Tensor< 1, 3 > > &force, const std::vector< double > &MOI) override
Integrate the closing half step of the Velocity Verlet scheme. The velocities are advanced by half a ...
virtual void integrate(Particles::ParticleHandler< dim > &particle_handler, const Tensor< 1, 3 > &body_force, const double time_step, std::vector< Tensor< 1, 3 > > &torque, std::vector< Tensor< 1, 3 > > &force, const std::vector< double > &MOI) override
Carry out a regular step of the Velocity Verlet scheme, that is, a full velocity step followed by a f...
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.
PropertiesIndex
Particle property indices for DEM multiphysics simulations.
This namespace defines all the data containers and required operations on these containers for the DE...
Point< 3 > point_nd_to_3d(const Point< dim > &point)