Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
serial_solid.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
6#include <core/utilities.h>
7
8#include <deal.II/base/point.h>
9
10#include <deal.II/fe/fe_q.h>
11#include <deal.II/fe/fe_simplex_p.h>
12#include <deal.II/fe/fe_system.h>
13
14#include <deal.II/grid/grid_generator.h>
15#include <deal.II/grid/grid_in.h>
16#include <deal.II/grid/grid_tools.h>
17
18#include <deal.II/numerics/data_out.h>
19#include <deal.II/numerics/vector_tools.h>
20
21#include <fstream>
22
23template <int dim, int spacedim>
26 const unsigned int id)
27 : mpi_communicator(MPI_COMM_WORLD)
30 , param(param)
31 , id(id)
38{
39 if (param->solid_mesh.simplex)
40 {
41 // for simplex meshes
42 fe = std::make_shared<FE_SimplexP<dim, spacedim>>(1);
43 solid_mapping = std::make_shared<MappingFE<dim, spacedim>>(*fe);
44 solid_tria = std::make_shared<Triangulation<dim, spacedim>>();
45
46 displacement_fe =
47 std::make_shared<FESystem<dim, spacedim>>(FE_SimplexP<dim, spacedim>(1),
48 spacedim);
49 }
50 else
51 {
52 // Usual case, for quad/hex meshes
53 fe = std::make_shared<FE_Q<dim, spacedim>>(1);
54 solid_mapping = std::make_shared<MappingQGeneric<dim, spacedim>>(1);
55 solid_tria = std::make_shared<Triangulation<dim, spacedim>>(
56 typename Triangulation<dim, spacedim>::MeshSmoothing(
57 Triangulation<dim, spacedim>::smoothing_on_refinement |
58 Triangulation<dim, spacedim>::smoothing_on_coarsening));
59
60 displacement_fe =
61 std::make_shared<FESystem<dim, spacedim>>(FE_Q<dim, spacedim>(1),
62 spacedim);
63 }
64
65 // Dof handler associated with mesh displacement
66 displacement_dh.clear();
67 displacement_dh.reinit(*this->solid_tria);
68
69 // Load triangulation
71
72 if constexpr (dim == 2)
73 {
74 // Set-up containers
75 if (param->solid_mesh.simplex)
77 }
78}
79
80template <int dim, int spacedim>
81std::vector<
82 std::pair<typename Triangulation<spacedim>::active_cell_iterator,
83 typename Triangulation<dim, spacedim>::active_cell_iterator>>
85 const parallel::TriangulationBase<spacedim> &background_tr)
86{
87 std::vector<
88 std::pair<typename Triangulation<spacedim>::active_cell_iterator,
89 typename Triangulation<dim, spacedim>::active_cell_iterator>>
90 mapped_solid;
91
92 if constexpr (dim == 2)
93 {
94 // Gather the amount of vertices per cell in the solid cells once to have
95 // a static memory allocation of the vector. This prevents reallocating
96 // the vector dynamically using push_back if the solid triangulation
97 // has many triangles
98 auto temporary_solid_cell = solid_tria->begin();
99 std::vector<Point<spacedim>> triangle(temporary_solid_cell->n_vertices());
100
101 // Calculate distance from cell center to solid_cell
102 for (const auto &background_cell : background_tr.active_cell_iterators())
103 {
104 // If the cell is owned by the processor
105 if (background_cell->is_locally_owned())
106 {
107 // Calculate the characteristic size of the background cell
108 const double bg_cell_length = background_cell->diameter();
109
110 // Calculate the center of the cell
111 Point<spacedim> bg_cell_center = background_cell->center();
112
113 // Calculate distance from center of the cell to triangle
114 for (auto &solid_cell : solid_tria->active_cell_iterators())
115 {
116 // Gather triangle vertices
117 for (unsigned int v = 0; v < solid_cell->n_vertices(); ++v)
118 {
119 triangle[v] = solid_cell->vertex(v);
120 }
121
122 // Calculate distance between triangle and center
123 const double distance =
125 triangle, bg_cell_center);
126
127 if (distance < bg_cell_length)
128 {
129 mapped_solid.push_back(
130 std::make_pair(background_cell, solid_cell));
131 }
132 }
133 }
134 }
135 }
136 else if constexpr (dim == 3)
137 {
138 throw std::runtime_error(
139 "Solid volumes are not yet implemented in Lethe. Please, remove the whole \"solid volumes\" subsection from your parameter file.");
140 }
141
142
144
145 return mapped_solid;
146}
147
148
149
150template <int dim, int spacedim>
151void
153{
154 // initial_setup is called if the simulation is not a restarted one
155 // Set-up of solid triangulation
156 setup_triangulation(false);
158}
159
160
161
162template <int dim, int spacedim>
163void
165{
167 {
168 check_file_exists(param->solid_mesh.file_name,
169 "gmsh mesh file of the solid object, given by "
170 "'subsection mesh' - 'set file name'");
171
172 // Grid creation
173 GridIn<dim, spacedim> grid_in;
174 // Attach triangulation
175 grid_in.attach_triangulation(*solid_tria);
176 // Read input gmsh file
177 std::ifstream input_file(param->solid_mesh.file_name);
178
179 grid_in.read_msh(input_file);
180 }
181 else if (param->solid_mesh.type ==
183 {
184 if (param->solid_mesh.simplex)
185 {
186 Triangulation<dim, spacedim> temporary_quad_triangulation;
187 GridGenerator::generate_from_name_and_arguments(
188 temporary_quad_triangulation,
189 param->solid_mesh.grid_type,
190 param->solid_mesh.grid_arguments);
191
192 // initial refinement
193 const int initial_refinement = param->solid_mesh.initial_refinement;
194 temporary_quad_triangulation.refine_global(initial_refinement);
195 // flatten the triangulation
196 Triangulation<dim, spacedim> flat_temp_quad_triangulation;
197 GridGenerator::flatten_triangulation(temporary_quad_triangulation,
198 flat_temp_quad_triangulation);
199
200 GridGenerator::convert_hypercube_to_simplex_mesh(
201 flat_temp_quad_triangulation, *solid_tria);
202 }
203 else
204 { // deal.ii creates grid and attaches the solid triangulation
205 GridGenerator::generate_from_name_and_arguments(
206 *solid_tria,
207 param->solid_mesh.grid_type,
208 param->solid_mesh.grid_arguments);
209 }
210 }
211 else
212 throw std::runtime_error(
213
214 "Unsupported mesh type - solid mesh will not be created");
215
216 // Rotate the triangulation
217 rotate_grid(param->solid_mesh.rotation_angle,
218 param->solid_mesh.rotation_axis);
219
220 // Translate the triangulation
221 translate_grid(param->solid_mesh.translation);
222
223 // Refine the solid triangulation to its initial size
224 // NB: solid_tria should not be refined if loaded from a restart file
225 // afterward
226 if (!restart)
227 {
228 if (param->solid_mesh.simplex)
229 {
230 // Simplex triangulation refinement isn't possible yet
231 }
232 else
233 {
234 solid_tria->refine_global(param->solid_mesh.initial_refinement);
235 }
236 }
237}
238
239
240template <int dim, int spacedim>
241DoFHandler<dim, spacedim> &
246
247
248template <int dim, int spacedim>
249Vector<double> &
254
255template <int dim, int spacedim>
256double
261
262template <int dim, int spacedim>
263std::shared_ptr<Triangulation<dim, spacedim>>
268
269template <int dim, int spacedim>
270void
272 const double angle,
273 [[maybe_unused]] const Tensor<1, spacedim> &axis)
274{
275 if constexpr (spacedim == 2)
276 GridTools::rotate(angle, *solid_tria);
277 else
278 GridTools::rotate(axis, angle, *solid_tria);
279}
280
281template <int dim, int spacedim>
282void
284 const Tensor<1, spacedim> &translation)
285{
286 GridTools::shift(translation, *solid_tria);
287}
288
289template <int dim, int spacedim>
290void
292{
295 return;
296
297 solid_temperature->set_time(initial_time);
298 current_solid_temperature = solid_temperature->value(Point<spacedim>());
299}
300
301template <int dim, int spacedim>
302void
304 const double initial_time)
305{
306 // First we calculate the displacement that the solid triangulation
307 // will feel during this iteration.
308 // Then, we apply this displacement to the triangulation
309 const unsigned int dofs_per_cell = displacement_fe->dofs_per_cell;
310 const std::vector<bool> locally_owned_vertices =
311 GridTools::get_locally_owned_vertices(*this->solid_tria);
312 std::vector<bool> vertex_moved(this->solid_tria->n_vertices(), false);
313 std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
314
315 translational_velocity->set_time(initial_time);
316 angular_velocity->set_time(initial_time);
317
318 for (int comp_i = 0; comp_i < spacedim; ++comp_i)
319 {
323 angular_velocity->value(center_of_rotation, comp_i);
324 }
325
326 for (const auto &cell : displacement_dh.active_cell_iterators())
327 {
328 if (cell->is_locally_owned())
329 {
330 for (unsigned int vertex = 0;
331 vertex < cell->reference_cell().n_vertices();
332 ++vertex)
333 {
334 const auto dof_index = cell->vertex_dof_index(vertex, 0);
335 Point<spacedim> &vertex_position = cell->vertex(vertex);
336 const unsigned global_vertex_no = cell->vertex_index(vertex);
337 if (vertex_moved[global_vertex_no] ||
338 !locally_owned_vertices[global_vertex_no])
339 continue;
340
341 Tensor<1, spacedim> distance_vector =
342 vertex_position - center_of_rotation;
343
344 Tensor<1, spacedim> local_velocity =
346 if constexpr (spacedim == 2)
347 {
348 local_velocity[0] +=
349 -distance_vector[1] * current_angular_velocity[2];
350 local_velocity[1] +=
351 distance_vector[0] * current_angular_velocity[2];
352 }
353 if constexpr (spacedim == 3)
354 {
355 local_velocity +=
356 cross_product_3d(current_angular_velocity, distance_vector);
357 }
358
359 auto vertex_displacement = time_step * local_velocity;
360
361 vertex_position += vertex_displacement;
362
363 for (int d = 0; d < spacedim; ++d)
364 {
365 displacement[dof_index + d] =
366 displacement[dof_index + d] + vertex_displacement[d];
367 displacement_since_mapped[dof_index + d] +=
368 vertex_displacement[d];
369 }
370
371 vertex_moved[global_vertex_no] = true;
372 }
373 }
374 }
375
377}
378
379
380template <int dim, int spacedim>
381void
383{
384 const unsigned int dofs_per_cell = displacement_fe->dofs_per_cell;
385 std::set<unsigned int> dof_vertex_displaced;
386 std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
387
388 for (const auto &cell : displacement_dh.active_cell_iterators())
389 {
390 if (cell->is_locally_owned())
391 {
392 for (unsigned int vertex = 0;
393 vertex < cell->reference_cell().n_vertices();
394 ++vertex)
395 {
396 if (!dof_vertex_displaced.contains(cell->vertex_index(vertex)))
397 {
398 const auto dof_index = cell->vertex_dof_index(vertex, 0);
399 Point<spacedim> &vertex_position = cell->vertex(vertex);
400
401 for (int d = 0; d < spacedim; ++d)
402 {
403 vertex_position[d] =
404 vertex_position[d] + displacement[dof_index + d];
405 }
406
407 dof_vertex_displaced.insert(cell->vertex_index(vertex));
408 }
409 }
410 }
411 }
412}
413
414template <int dim, int spacedim>
415void
417{
418 const unsigned int dofs_per_cell = displacement_fe->dofs_per_cell;
419
420 // Set of vertices which were displaced already. A set is used
421 // for efficiency when searching over it.
422 std::set<unsigned int> dof_vertex_displaced;
423 std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
424
425 // Loop over all cells
426 for (const auto &cell : displacement_dh.active_cell_iterators())
427 {
428 for (unsigned int vertex = 0;
429 vertex < cell->reference_cell().n_vertices();
430 ++vertex)
431 {
432 if (!dof_vertex_displaced.contains(cell->vertex_index(vertex)))
433 {
434 const auto dof_index = cell->vertex_dof_index(vertex, 0);
435 Point<spacedim> &vertex_position = cell->vertex(vertex);
436
437 for (int d = 0; d < spacedim; ++d)
438 {
439 vertex_position[d] += displacement[dof_index + d];
440 }
441
442 dof_vertex_displaced.insert(cell->vertex_index(vertex));
443 }
444 }
445 }
446}
447
448
449template <int dim, int spacedim>
450void
452 const std::shared_ptr<SimulationControl> &simulation_control)
453{
454 if (!output_bool)
455 return;
456
457 DataOut<dim, spacedim> data_out;
458 data_out.attach_dof_handler(displacement_dh);
459
460 {
461 std::vector<std::string> displacement_names(spacedim, "displacement");
462 std::vector<DataComponentInterpretation::DataComponentInterpretation>
463 data_component_interpretation(
464 spacedim, DataComponentInterpretation::component_is_part_of_vector);
465
466 data_out.add_data_vector(displacement,
467 displacement_names,
468 DataOut<dim, spacedim>::type_dof_data,
469 data_component_interpretation);
470 }
471
472 {
473 std::vector<std::string> displacement_names(spacedim,
474 "displacement_since_mapped");
475 std::vector<DataComponentInterpretation::DataComponentInterpretation>
476 data_component_interpretation(
477 spacedim, DataComponentInterpretation::component_is_part_of_vector);
478
479 data_out.add_data_vector(displacement_since_mapped,
480 displacement_names,
481 DataOut<dim, spacedim>::type_dof_data,
482 data_component_interpretation);
483 }
484
485 data_out.build_patches();
486
487 const std::string folder = simulation_control->get_output_path();
488 const std::string solution_name = [&] {
489 if constexpr (dim == 1)
490 {
491 return simulation_control->get_output_name() + ".solid_line." +
492 Utilities::int_to_string(id, 2);
493 }
494 if constexpr (dim == 2)
495 {
496 return simulation_control->get_output_name() + ".solid_surface." +
497 Utilities::int_to_string(id, 2);
498 }
499 if constexpr (dim == 3)
500 {
501 return simulation_control->get_output_name() + ".solid_volume." +
502 Utilities::int_to_string(id, 2);
503 }
504 }();
505 const unsigned int iter = simulation_control->get_iteration_number();
506 const double time = simulation_control->get_current_time();
507 const unsigned int group_files = simulation_control->get_group_files();
508
510 data_out,
511 folder,
512 solution_name,
513 time,
514 iter,
515 group_files,
516 this->mpi_communicator);
517}
518
519template <int dim, int spacedim>
520void
522{
523 // Checkpoint the DOF Handler
524 {
525 std::string file_name =
526 prefix + ".solid_object." + Utilities::int_to_string(id, 2) + ".dof";
527 std::ofstream ofs(file_name);
528 boost::archive::text_oarchive oa(ofs);
529 displacement_dh.save(oa, 0);
530 }
531 // Checkpoint the displacement vector which we will use to shift the
532 // triangulation
533 {
534 std::string file_name = prefix + ".solid_object." +
535 Utilities::int_to_string(id, 2) + ".displacement";
536 std::ofstream ofs(file_name);
537 boost::archive::text_oarchive oa(ofs);
538 displacement.save(oa, 0);
539 }
540
541 // Write pvd handler
542 pvdhandler.save(prefix + ".solid_object." + Utilities::int_to_string(id, 2));
543}
544
545template <int dim, int spacedim>
546void
548{
549 // Import dof handler
550 {
551 // displacement_dh.distribute_dofs(fe);
552 std::string file_name =
553 prefix + ".solid_object." + Utilities::int_to_string(id, 2) + ".dof";
554 check_file_exists(file_name,
555 "checkpoint file of the degrees of freedom of the solid "
556 "object, given by 'subsection restart' - 'set filename'");
557 std::ifstream ifs(file_name);
558 boost::archive::text_iarchive ia(ifs);
559 displacement_dh.load(ia, 0);
560 }
561 // Import nodal counts
562 {
563 std::string file_name = prefix + ".solid_object." +
564 Utilities::int_to_string(id, 2) + ".displacement";
565 check_file_exists(file_name,
566 "checkpoint file of the displacement of the solid "
567 "object, given by 'subsection restart' - 'set filename'");
568 std::ifstream ifs(file_name);
569 boost::archive::text_iarchive ia(ifs);
570 displacement.load(ia, 0);
571 }
572
573 // Re-read pvd handler from output files
574 pvdhandler.read(prefix + ".solid_object." + Utilities::int_to_string(id, 2));
575
576 // Reset triangulation position using displacement vector
578}
579
580
581template <int dim, int spacedim>
582void
584{
585 displacement_dh.distribute_dofs(*this->displacement_fe);
586 displacement.reinit(displacement_dh.n_locally_owned_dofs());
587 displacement_since_mapped.reinit(displacement_dh.n_locally_owned_dofs());
588
589 displacement = 0;
591}
592
593template <int dim, int spacedim>
594void
596{
597 // Map : Vertices ID --> Cell iterator vector
598 std::map<
599 unsigned int,
600 std::set<typename Triangulation<dim, spacedim>::active_cell_iterator>>
601 vertices_cell_map;
603
604 // Loop on every cell
605 for (const auto &main_cell : solid_tria->active_cell_iterators())
606 {
607 // Initializing the containers associated with the main cell
608 std::vector<typename Triangulation<dim, spacedim>::active_cell_iterator>
609 es_main_cell, vs_main_cell;
610
611 // Number of vertices of the main cell
612 const unsigned int n_vertices_main_cell = main_cell->n_vertices();
613
614 // Store the vertices index of the main cell in a vector
615 std::vector<unsigned int> main_cell_vertex_indices;
616 main_cell_vertex_indices.reserve(n_vertices_main_cell);
617 for (unsigned int v = 0; v < n_vertices_main_cell; ++v)
618 main_cell_vertex_indices.push_back(main_cell->vertex_index(v));
619
620 // Find the cell iterators around the main cell. This includes the main
621 // cell itself.
622 auto neighboring_cells =
624 main_cell);
625
626 // Loop on the neighboring cells of the main cell
627 for (const auto &neigh_cell : neighboring_cells)
628 {
629 // Skip the first iterator since it is the main cell itself
630 if (neigh_cell == main_cell)
631 continue;
632
633 // Number of sharing vertices between main cell and neighboring
634 // cell. We know it is high either 1 or 2.
635 unsigned int n_sharing_vertices = 0;
636
637 // Loop on the vertices of that of the neighboring cell.
638 const unsigned int n_vertices_neigh_cell = neigh_cell->n_vertices();
639
640 for (unsigned int v = 0; v < n_vertices_neigh_cell; ++v)
641 {
642 unsigned int neigh_cell_vertex_index =
643 neigh_cell->vertex_index(v);
644 if (std::ranges::find(main_cell_vertex_indices,
645 neigh_cell_vertex_index) !=
646 main_cell_vertex_indices.end())
647 n_sharing_vertices++;
648 }
649 // Sharing 1 vertex
650 if (n_sharing_vertices == 1)
651 vs_main_cell.push_back(neigh_cell);
652 else // Sharing 2 vertex
653 es_main_cell.push_back(neigh_cell);
654 }
655 es_neighbors.insert(std::make_pair(main_cell, es_main_cell));
656 vs_neighbors.insert(std::make_pair(main_cell, vs_main_cell));
657 }
658}
659// Pre-compile the 2D, 3D and the 2D in 3D versions with the types that can
660// occur
661template class SerialSolid<1, 2>;
662template class SerialSolid<2, 3>;
663template class SerialSolid<2>;
664template class SerialSolid<3>;
Parameters for a single rigid solid object used in DEM simulations.
std::shared_ptr< Function< spacedim > > angular_velocity
void setup_containers()
Builds the neighboring cells information for each cell of the solid object. These containers store fo...
const unsigned int n_mpi_processes
unsigned int id
PVDHandler pvdhandler
Tensor< 1, 3 > current_angular_velocity
std::shared_ptr< Triangulation< dim, spacedim > > solid_tria
void setup_dof_handler()
Set-ups the displacement vector to store the motion of the triangulation.
MPI_Comm mpi_communicator
void write_output_results(const std::shared_ptr< SimulationControl > &simulation_control)
Write the output file of the solid object triangulation.
std::shared_ptr< Parameters::RigidSolidObject< dim, spacedim > > & param
double current_solid_temperature
std::shared_ptr< FESystem< dim, spacedim > > displacement_fe
triangulation_cell_neighbors_map vs_neighbors
Vector< double > displacement_since_mapped
std::shared_ptr< Triangulation< dim, spacedim > > get_triangulation()
Returns the shared pointer to the solid object triangulation.
Tensor< 1, spacedim > current_translational_velocity
triangulation_cell_neighbors_map es_neighbors
void move_solid_triangulation(double time_step, double initial_time)
Moves the vertices of the solid triangulation. This function uses explicitEuler scheme time integrato...
void initial_setup()
Manages solid triangulation setup.
void reset_displacement_monitoring()
Reset displacements since intersection for contact detection.
void read_checkpoint(const std::string &prefix_name)
read solid base triangulation checkpoint and replaces the triangulation at the location where it was ...
DoFHandler< dim, spacedim > displacement_dh
void rotate_grid(const double angle, const Tensor< 1, spacedim > &axis)
Rotates the grid by a given angle around an axis.
void write_checkpoint(const std::string &prefix_name)
write solid base triangulation checkpoint
const bool output_bool
std::shared_ptr< Function< spacedim > > translational_velocity
Vector< double > displacement
DoFHandler< dim, spacedim > & get_dof_handler()
Returns the dof_handler associated with the displacement of the solid object.
void update_solid_temperature(const double initial_time)
Update the temperature of the solid object, which is a function of time.
double get_max_displacement_since_mapped() const
Calculates the max displacement of the solid object. The max displacement is defined as the L^\infty ...
Parameters::SolidObjectThermalBoundaryType thermal_boundary_type
std::vector< std::pair< typename Triangulation< spacedim >::active_cell_iterator, typename Triangulation< dim, spacedim >::active_cell_iterator > > map_solid_in_background_triangulation(const parallel::TriangulationBase< spacedim > &background_tr)
Maps the solid object in the background triangulation.
void translate_grid(const Tensor< 1, spacedim > &translate)
Translate the grid. In spacedim=2, the third component is ignored.
std::shared_ptr< Function< spacedim > > solid_temperature
const unsigned int this_mpi_process
void move_solid_triangulation_with_displacement()
Moves the vertices of the solid triangulation using the displacement vector. This is mostly used when...
Point< spacedim > center_of_rotation
void displace_solid_triangulation()
Moves the dofs of the solid_triangulation by using the displacement vector. This is only used to move...
void setup_triangulation(const bool restart)
Generates a solid triangulation from a dealii or gmsh mesh.
SerialSolid(std::shared_ptr< Parameters::RigidSolidObject< dim, spacedim > > &param, unsigned int id)
Vector< double > & get_displacement_vector()
Returns the displacement vector of all the vertices of the solid object.
void check_file_exists(const std::string &file_name, const std::string &file_description)
Verify that a file that Lethe must read exists and can be opened, and raise an error stated in the us...
Definition utilities.cc:225
double find_point_triangle_distance(const std::vector< Point< dim > > &triangle_vertices, const Point< dim > &point)
Calculates the distance between points and a triangle (defined using three vertices)....
void vertices_cell_mapping(const DoFHandler< dim > &dof_handler, std::map< unsigned int, std::set< typename DoFHandler< dim >::active_cell_iterator > > &vertices_cell_map)
Map the vertex index to the cells that includes that vertex.
std::vector< typename DoFHandler< dim >::active_cell_iterator > find_cells_around_cell(std::map< unsigned int, std::set< typename DoFHandler< dim >::active_cell_iterator > > &vertices_cell_map, const typename DoFHandler< dim >::active_cell_iterator &cell)
Return a vector of cells around a cell. The vector of cells includes all the cells that share a verte...
@ adiabatic
No heat flux through the solid boundary.
void write_vtu_and_pvd(PVDHandler &pvd_handler, const DataOutInterface< dim, spacedim > &data_out, const std::string &folder, const std::string &file_prefix, const double time, const unsigned int iter, const unsigned int group_files, const MPI_Comm &mpi_communicator, const unsigned int digits=5)
Output the data out to "group_files" vtu files, with a pvtu file and a pvd to store the timing This f...