Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
grid_cylinder.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 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/grid/grid_generator.h>
7#include <deal.II/grid/grid_tools.h>
8
9template <int dim, int spacedim>
10GridCylinder<dim, spacedim>::GridCylinder(const std::string &grid_type,
11 const std::string &grid_arguments)
12{
13 if constexpr (!(dim == 3 && spacedim == 3))
14 {
15 AssertThrow(
16 false,
17 ExcMessage(
18 "Custom cylinder mesh is only supported in 3D space with 3D elements."));
19 return;
20 }
21
22 this->grid_arguments = grid_arguments;
23 if (grid_type == "cylinder_classic")
24 this->cylinder_type = CylinderType::classic;
25 else if (grid_type == "cylinder_balanced")
26 this->cylinder_type = CylinderType::balanced;
27 else if (grid_type == "cylinder_squared")
28 this->cylinder_type = CylinderType::squared;
29 else if (grid_type == "cylinder_regularized")
30 this->cylinder_type = CylinderType::regularized;
31 else
32 AssertThrow(
33 false,
34 ExcMessage(
35 "Unsupported custom cylinder, the choices are: cylinder_classic|cylinder_balanced|cylinder_squared|cylinder_regularized"));
36
37
38 // Separate arguments of the string
39 std::vector<std::string> arguments;
40 std::stringstream s_stream(grid_arguments);
41 while (s_stream.good())
42 {
43 std::string substr;
44 getline(s_stream, substr, ':');
45 arguments.push_back(substr);
46 }
47
48 // Arguments declaration
49 if (arguments.size() != 3)
50 {
51 AssertThrow(
52 false,
53 ExcMessage(
54 "Mandatory cylinder parameters are (x subdivisions: radius : half height)"));
55 }
56 else
57 {
58 std::vector<double> arguments_double =
59 Utilities::string_to_double(arguments);
60 this->subdivisions = static_cast<int>(arguments_double[0]);
61 this->radius = arguments_double[1];
62 this->half_height = arguments_double[2];
63 }
64}
65
66
67template <>
68void
69GridCylinder<3, 3>::make_grid(Triangulation<3, 3> &triangulation)
70{
71 if (cylinder_type == CylinderType::classic)
72 {
73 // Create a subdivided cylinder from deal.ii
74 GridGenerator::subdivided_cylinder(triangulation,
75 subdivisions,
76 radius,
77 half_height);
78 }
79 else
80 {
81 // Create a temporary 2D mesh
82 Triangulation<2, 2> temporary_triangulation;
83
84 // Create a spherical manifold for 2D mesh
85 Point<2> center(0.0, 0.0);
86 const SphericalManifold<2, 2> m0(center);
87
88 if (cylinder_type == CylinderType::regularized ||
89 cylinder_type == CylinderType::squared)
90 {
91 // Create a square mesh
92 double real_radius = radius * std::sin(M_PI_4);
93 GridGenerator::hyper_cube(temporary_triangulation,
94 -real_radius,
95 real_radius,
96 true);
97
98 // Assign boundary 0 to perimeter as for cylinder
99 for (const auto &cell :
100 temporary_triangulation.active_cell_iterators())
101 {
102 if (cell->is_locally_owned())
103 {
104 // Looping through all the faces of the cell
105 for (const auto &face : cell->face_iterators())
106 {
107 // Check to see if the face is located at boundary
108 if (face->at_boundary())
109 {
110 face->set_boundary_id(0);
111 }
112 }
113 }
114 }
115 }
116 else if (cylinder_type == CylinderType::balanced)
117 {
118 GridGenerator::hyper_ball_balanced(temporary_triangulation,
119 center,
120 radius);
121 }
122
123 temporary_triangulation.reset_all_manifolds();
124 temporary_triangulation.set_all_manifold_ids_on_boundary(0);
125 temporary_triangulation.set_manifold(0, m0);
126
127 if (cylinder_type == CylinderType::regularized)
128 {
129 // Pre-refinement to reduce mesh size at corners before
130 // reinitialization
131 temporary_triangulation.refine_global(2);
132 GridTools::regularize_corner_cells(temporary_triangulation);
133
134 // Flatten the triangulation
135 Triangulation<2, 2> flat_temporary_triangulation;
136 flat_temporary_triangulation.copy_triangulation(
137 temporary_triangulation);
138 temporary_triangulation.clear();
139 GridGenerator::flatten_triangulation(flat_temporary_triangulation,
140 temporary_triangulation);
141 }
142
143 // Extrude the 2D temporary mesh to 3D cylinder
144 GridGenerator::extrude_triangulation(temporary_triangulation,
145 subdivisions + 1,
146 2.0 * half_height,
147 triangulation,
148 true);
149
150 // Rotate mesh in x-axis and set the (0,0,0) at the barycenter
151 // to be comparable to dealii cylinder meshes
152 Tensor<1, 3> axis_vector({0.0, 1.0, 0.0});
153 GridTools::rotate(axis_vector, M_PI_2, triangulation);
154 Tensor<1, 3> shift_vector({-half_height, 0.0, 0.0});
155 GridTools::shift(shift_vector, triangulation);
156
157 // Force the manifold id to be zero in the case of the balanced
158 // cylinder
159 if (cylinder_type == CylinderType::balanced)
160 triangulation.reset_manifold(1);
161
162 // Add a cylindrical manifold on the final unrefined mesh
163 const CylindricalManifold<3, 3> m1(0);
164 triangulation.set_manifold(0, m1);
165 }
166}
167
168// Fallback make_grid definition for unsupported template parameters. This
169// provides a linker-visible symbol and a clear runtime error when the
170// class is instantiated for dim/spacedim combinations that are not
171// specialized above.
172template <int dim, int spacedim>
173void
174GridCylinder<dim, spacedim>::make_grid(
175 Triangulation<dim, spacedim> & /*triangulation*/)
176{
177 AssertThrow(
178 false,
179 ExcMessage(
180 "GridCylinder is only implemented for dim = 3 and spacedim = 3."));
181}
182
183// Explicit template instantiations
184template class GridCylinder<2, 2>;
185template class GridCylinder<2, 3>;
186template class GridCylinder<3, 3>;