Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
grid_uniform_channel_with_meshed_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#include <core/utilities.h>
6
7#include <deal.II/grid/grid_generator.h>
8
9#include <numbers>
10#include <set>
11
12template <int dim, int spacedim>
14 GridUniformChannelWithMeshedCylinder(const std::string &grid_arguments)
15{
16 if constexpr (dim == 1 || spacedim == 1)
17 {
18 AssertThrow(
19 false,
20 ExcMessage(
21 "The uniform_channel_with_meshed_cylinder mesh is only supported in 2D and 3D space."));
22 return;
23 }
24 else if constexpr (dim == 2 && spacedim == 3)
25 {
26 AssertThrow(
27 false,
28 ExcMessage(
29 "The uniform_channel_with_meshed_cylinder mesh is only supported in 3D space with 3D elements."));
30 return;
31 }
32
33 const std::vector<std::string> arguments =
34 Utilities::split_string_list(grid_arguments, ':');
35
36 if (arguments.size() < 5)
37 {
38 AssertThrow(
39 false,
40 ExcMessage(
41 "Mandatory uniform_channel_with_meshed_cylinder parameters are (bottom left point : top right point : center of the cylinder : inner radius : outer radius). The points should be given as x,y and the radii should be given as a single number. The optional parameters are (pad bottom : pad top : pad left : pad right : height : n_slices : use_transfinite_region : mesh_obstacle : colorize). The padding parameters should be given as a single integer, the height should be given as a single double, the n_slices should be given as a single integer, use_transfinite_region should be given as true/false, mesh_obstacle should be given as true/false, and the colorize parameter should be given as true/false."));
42 }
43 // Parse bottom_left point
44 try
45 {
46 Tensor<1, 2> bottom_left_coords = value_string_to_tensor<2>(arguments[0]);
47 this->bottom_left =
48 (dim == 2) ?
49 Point<dim>(bottom_left_coords[0], bottom_left_coords[1]) :
50 Point<dim>(bottom_left_coords[0], bottom_left_coords[1], 0.0);
51 }
52 catch (const std::exception &e)
53 {
54 AssertThrow(
55 false,
56 ExcMessage(
57 "The bottom left point should have 2 components coordinates (x,y format) because the channel is extruded in the z direction."));
58 }
59
60 // Parse top_right point
61 try
62 {
63 Tensor<1, 2> top_right_coords = value_string_to_tensor<2>(arguments[1]);
64 this->top_right =
65 (dim == 2) ? Point<dim>(top_right_coords[0], top_right_coords[1]) :
66 Point<dim>(top_right_coords[0], top_right_coords[1], 0.0);
67 }
68 catch (const std::exception &e)
69 {
70 AssertThrow(
71 false,
72 ExcMessage(
73 "The top right point should have 2 components coordinates (x,y format) because the channel is extruded in the z direction."));
74 }
75
76
77 // Parse center point
78 try
79 {
80 Tensor<1, 2> center_coords = value_string_to_tensor<2>(arguments[2]);
81 this->center = (dim == 2) ?
82 Point<dim>(center_coords[0], center_coords[1]) :
83 Point<dim>(center_coords[0], center_coords[1], 0.0);
84 }
85 catch (const std::exception &e)
86 {
87 AssertThrow(
88 false,
89 ExcMessage(
90 "The center point should have 2 components coordinates (x,y format) because the channel is extruded in the z direction."));
91 }
92
93 // Parse inner_radius
94 this->inner_radius = Utilities::string_to_double(arguments[3]);
95
96 // Parse outer_radius
97 this->outer_radius = Utilities::string_to_double(arguments[4]);
98
99 // Check if the cylinder fits in the rectangle
100 AssertThrow((center[0] - outer_radius >= bottom_left[0]) &&
101 (center[0] + outer_radius <= top_right[0]) &&
102 (center[1] - outer_radius >= bottom_left[1]) &&
103 (center[1] + outer_radius <= top_right[1]),
104 ExcMessage(
105 "The cylinder outer radius does not fit in the channel."));
106
107 // Parse optional parameters if provided. If not provided, they will be set to
108 // default values.
109 this->pad_bottom =
110 (arguments.size() > 5) ? Utilities::string_to_int(arguments[5]) : 0;
111 this->pad_top =
112 (arguments.size() > 6) ? Utilities::string_to_int(arguments[6]) : 0;
113 this->pad_left =
114 (arguments.size() > 7) ? Utilities::string_to_int(arguments[7]) : 0;
115 this->pad_right =
116 (arguments.size() > 8) ? Utilities::string_to_int(arguments[8]) : 0;
117
118 // Padding is required in each direction where the transition square does not
119 // reach the channel boundary (i.e., there is a gap to fill with rectangles).
120 AssertThrow(!((pad_bottom == 0) && (std::abs((center[1] - outer_radius) -
121 bottom_left[1]) > 1e-12)),
122 ExcMessage("Bottom padding is required because the transition "
123 "region does not reach the bottom channel boundary."));
124 AssertThrow(!((pad_top == 0) &&
125 (std::abs((center[1] + outer_radius) - top_right[1]) > 1e-12)),
126 ExcMessage("Top padding is required because the transition "
127 "region does not reach the top channel boundary."));
128 AssertThrow(!((pad_left == 0) && (std::abs((center[0] - outer_radius) -
129 bottom_left[0]) > 1e-12)),
130 ExcMessage("Left padding is required because the transition "
131 "region does not reach the left channel boundary."));
132 AssertThrow(!((pad_right == 0) &&
133 (std::abs((center[0] + outer_radius) - top_right[0]) > 1e-12)),
134 ExcMessage("Right padding is required because the transition "
135 "region does not reach the right channel boundary."));
136
137 this->height =
138 (arguments.size() > 9) ? Utilities::string_to_double(arguments[9]) : 1.0;
139 this->n_slices =
140 (arguments.size() > 10) ? Utilities::string_to_int(arguments[10]) : 2.;
141 AssertThrow(n_slices >= 2, ExcMessage("The minimum number of slices is 2."));
143 (arguments.size() > 11 && arguments[11] == "true") ? true : false;
144 this->mesh_obstacle =
145 (arguments.size() > 12 && arguments[12] == "true") ? true : false;
146 this->colorize =
147 (arguments.size() > 13 && arguments[13] == "true") ? true : false;
148}
149
150
151template <int dim, int spacedim>
152void
154 Triangulation<2> &triangulation,
155 const Point<2> &bottom_left,
156 const Point<2> &top_right,
157 const Point<2> &center,
158 const double inner_radius,
159 const double outer_radius,
160 const unsigned int pad_bottom,
161 const unsigned int pad_top,
162 const unsigned int pad_left,
163 const unsigned int pad_right,
164 const bool colorize)
165{
166 const types::material_id fluid_material_id = 0;
167 const types::material_id solid_material_id = 1;
168 const types::manifold_id tfi_manifold_id = 0;
169 const types::manifold_id polar_manifold_id = 1;
170
171 // Create the inner cylinder (balanced disk)
172 Triangulation<2> cylinder_tria;
173 GridGenerator::hyper_ball_balanced(cylinder_tria, center, inner_radius);
174
175 // Create the transition region between the cylinder and the rectangular
176 // channel. A hyper_shell with 8 segments produces vertices at 45-degree
177 // intervals. The diagonal vertices are moved outward by a factor of sqrt(2)
178 // so that the outer boundary forms a square of half-side outer_radius.
179 Triangulation<2> box_tria;
180 GridGenerator::hyper_shell(box_tria, center, inner_radius, outer_radius, 8);
181 for (const auto &cell : box_tria.active_cell_iterators())
182 {
183 for (unsigned int v = 0; v < GeometryInfo<2>::vertices_per_cell; ++v)
184 {
185 Point<2> &vertex = cell->vertex(v);
186 const double dist = vertex.distance(center);
187 // Check if the vertex is on the outer ring AND on a diagonal
188 if ((std::abs(dist - outer_radius) < 1e-12 * outer_radius) &&
189 (std::abs(std::abs(vertex[0] - center[0]) -
190 std::abs(vertex[1] - center[1]))) <
191 1e-12 * outer_radius)
192 {
193 vertex = center + std::numbers::sqrt2 * (vertex - center);
194 }
195 }
196 }
197
198 // Create the padding with up to 8 rectangles that fill the gap between the
199 // transition square and the channel boundary. The four sides and four
200 // corners are meshed independently.
201 Triangulation<2> pad_bottom_tria;
202 if (pad_bottom > 0)
203 {
204 GridGenerator::subdivided_hyper_rectangle(
205 pad_bottom_tria,
206 {2, pad_bottom},
207 Point<2>(center[0] - outer_radius, bottom_left[1]),
208 Point<2>(center[0] + outer_radius, center[1] - outer_radius));
209 }
210
211 Triangulation<2> pad_top_tria;
212 if (pad_top > 0)
213 {
214 GridGenerator::subdivided_hyper_rectangle(
215 pad_top_tria,
216 {2, pad_top},
217 Point<2>(center[0] - outer_radius, center[1] + outer_radius),
218 Point<2>(center[0] + outer_radius, top_right[1]));
219 }
220
221 Triangulation<2> pad_left_tria;
222 if (pad_left > 0)
223 {
224 GridGenerator::subdivided_hyper_rectangle(
225 pad_left_tria,
226 {pad_left, 2},
227 Point<2>(bottom_left[0], center[1] - outer_radius),
228 Point<2>(center[0] - outer_radius, center[1] + outer_radius));
229 }
230
231 Triangulation<2> pad_right_tria;
232 if (pad_right > 0)
233 {
234 GridGenerator::subdivided_hyper_rectangle(
235 pad_right_tria,
236 {pad_right, 2},
237 Point<2>(center[0] + outer_radius, center[1] - outer_radius),
238 Point<2>(top_right[0], center[1] + outer_radius));
239 }
240
241 Triangulation<2> pad_bottom_left_corner_tria;
242 if (pad_bottom > 0 && pad_left > 0)
243 {
244 GridGenerator::subdivided_hyper_rectangle(
245 pad_bottom_left_corner_tria,
248 Point<2>(center[0] - outer_radius, center[1] - outer_radius));
249 }
250
251 Triangulation<2> pad_bottom_right_corner_tria;
252 if (pad_bottom > 0 && pad_right > 0)
253 {
254 GridGenerator::subdivided_hyper_rectangle(
255 pad_bottom_right_corner_tria,
257 Point<2>(center[0] + outer_radius, bottom_left[1]),
258 Point<2>(top_right[0], center[1] - outer_radius));
259 }
260
261 Triangulation<2> pad_top_left_corner_tria;
262 if (pad_top > 0 && pad_left > 0)
263 {
264 GridGenerator::subdivided_hyper_rectangle(
265 pad_top_left_corner_tria,
266 {pad_left, pad_top},
267 Point<2>(bottom_left[0], center[1] + outer_radius),
268 Point<2>(center[0] - outer_radius, top_right[1]));
269 }
270
271 Triangulation<2> pad_top_right_corner_tria;
272 if (pad_top > 0 && pad_right > 0)
273 {
274 GridGenerator::subdivided_hyper_rectangle(
275 pad_top_right_corner_tria,
277 Point<2>(center[0] + outer_radius, center[1] + outer_radius),
278 top_right);
279 }
280
281 // Merge only non-empty triangulations
282 std::vector<const Triangulation<2> *> trias = {&cylinder_tria, &box_tria};
283 if (pad_bottom > 0)
284 trias.push_back(&pad_bottom_tria);
285 if (pad_top > 0)
286 trias.push_back(&pad_top_tria);
287 if (pad_left > 0)
288 trias.push_back(&pad_left_tria);
289 if (pad_right > 0)
290 trias.push_back(&pad_right_tria);
291 if (pad_bottom > 0 && pad_left > 0)
292 trias.push_back(&pad_bottom_left_corner_tria);
293 if (pad_bottom > 0 && pad_right > 0)
294 trias.push_back(&pad_bottom_right_corner_tria);
295 if (pad_top > 0 && pad_left > 0)
296 trias.push_back(&pad_top_left_corner_tria);
297 if (pad_top > 0 && pad_right > 0)
298 trias.push_back(&pad_top_right_corner_tria);
299 GridGenerator::merge_triangulations(trias, triangulation);
300
301
302 // Assign material and manifold IDs:
303 // - id 0 (TFI) on all cells inside the transition region and on all faces
304 // not on the inner cylinder boundary
305 // - id 1 (polar/cylindrical) on inner-cylinder boundary faces
306 triangulation.reset_all_manifolds();
307 triangulation.set_all_manifold_ids(
308 use_transfinite_region ? tfi_manifold_id : numbers::flat_manifold_id);
309
310 for (const auto &cell : triangulation.active_cell_iterators())
311 {
312 // If cell center is within the inner_radius, it's in the cylinder.
313 bool cell_in_cylinder = cell->center().distance(center) < inner_radius;
314
315 // The inner cylinder is marked with the polar manifold ID for the
316 // material and all faces that have all vertices on the inner circle are
317 // marked with the polar manifold ID for the manifold.
318 if (cell_in_cylinder)
319 {
320 cell->set_material_id(solid_material_id);
321 }
322
323 // Every other cells stay with the flat manifold and gets the fluid
324 // material ID.
325 else
326 {
327 cell->set_material_id(fluid_material_id);
328 }
329 }
330
331 // Remove the cells of the inner cylinder if the mesh_obstacle flag is false.
332 if (!mesh_obstacle)
333 {
334 std::set<typename Triangulation<2>::active_cell_iterator> obstacle_cells;
335 for (const auto &cell : triangulation.active_cell_iterators())
336 if (cell->material_id() == solid_material_id)
337 obstacle_cells.insert(cell);
338
339 Triangulation<2> temp_tria;
340 GridGenerator::create_triangulation_with_removed_cells(triangulation,
341 obstacle_cells,
342 temp_tria);
343 triangulation.clear();
344 triangulation.copy_triangulation(temp_tria);
345 }
346
347 // Assign boundary IDs following the subdivided_hyper_rectangle
348 // convention:
349 // 0: obstacle, 1: left (-x), 2: right (+x), 3: bottom (-y), 4: top (+y)
350 if (colorize)
351 {
352 const double tol_x = 1e-10 * (top_right[0] - bottom_left[0]);
353 const double tol_y = 1e-10 * (top_right[1] - bottom_left[1]);
354
355 for (const auto &face : triangulation.active_face_iterators())
356 {
357 if (!face->at_boundary())
358 continue;
359
360 const Point<2> face_center = face->center();
361
362 if (std::abs(face_center[0] - bottom_left[0]) < tol_x)
363 face->set_boundary_id(mesh_obstacle ? 0 : 1);
364 else if (std::abs(face_center[0] - top_right[0]) < tol_x)
365 face->set_boundary_id(mesh_obstacle ? 1 : 2);
366 else if (std::abs(face_center[1] - bottom_left[1]) < tol_y)
367 face->set_boundary_id(mesh_obstacle ? 2 : 3);
368 else if (std::abs(face_center[1] - top_right[1]) < tol_y)
369 face->set_boundary_id(mesh_obstacle ? 3 : 4);
370 else
371 face->set_boundary_id(0);
372 }
373 }
374
375 // Attach manifold objects for proper refinement behavior
376 for (const auto &cell : triangulation.active_cell_iterators())
377 {
378 for (const auto &face : cell->face_iterators())
379 {
380 bool all_vertices_on_circle = true;
381 for (unsigned int v = 0; v < GeometryInfo<2>::vertices_per_face; ++v)
382 {
383 if (std::abs(face->vertex(v).distance(center) - inner_radius) >
384 1e-10 * inner_radius)
385 {
386 all_vertices_on_circle = false;
387 break;
388 }
389 }
390 if (all_vertices_on_circle)
391 face->set_all_manifold_ids(polar_manifold_id);
392 }
393 }
394 PolarManifold<2, 2> polar_manifold(center);
395 triangulation.set_manifold(1, polar_manifold);
396
397 // If transfinite interpolation is enabled for the transition region, attach a
398 // TFI, else deal.II will use a default flat manifold.
400 {
401 TransfiniteInterpolationManifold<2> tfi_manifold;
402 tfi_manifold.initialize(triangulation);
403 triangulation.set_manifold(0, tfi_manifold);
404 }
405}
406
407
408
409template <>
410void
412 Triangulation<2, 2> &triangulation)
413{
414 generate_2d_channel_mesh(triangulation,
416 top_right,
417 center,
421 pad_top,
422 pad_left,
423 pad_right,
424 colorize);
425}
426
427
428template <>
429void
431 Triangulation<3, 3> &triangulation)
432{
433 // Generate the 2D cross-section (geometry + manifold IDs + boundary IDs)
434 Triangulation<2> tria_2D;
436 Point<2>(bottom_left[0], bottom_left[1]),
437 Point<2>(top_right[0], top_right[1]),
438 Point<2>(center[0], center[1]),
442 pad_top,
443 pad_left,
444 pad_right,
445 colorize);
446
447 // Extrude the 2D cross-section along the z-axis. Manifold and material IDs
448 // from the 2D mesh are copied to the lateral faces of the 3D mesh.
449 GridGenerator::extrude_triangulation(
450 tria_2D, n_slices, height, triangulation, true);
451
452 // Attach 3D manifold objects. The manifold IDs (0 for TFI, 1 for
453 // cylindrical) were inherited from the 2D mesh during extrusion.
454 const Tensor<1, 3> direction{{0.0, 0.0, 1.0}};
455 const CylindricalManifold<3> cylindrical_manifold(direction, center);
456 triangulation.set_manifold(1, cylindrical_manifold);
457
459 {
460 TransfiniteInterpolationManifold<3> tfi_manifold;
461 tfi_manifold.initialize(triangulation);
462 triangulation.set_manifold(0, tfi_manifold);
463 }
464
465 // Set the boundary ids for the extruded top and bottom faces.
466 if (colorize)
467 {
468 for (const auto &face : triangulation.active_face_iterators())
469 {
470 if (!face->at_boundary())
471 continue;
472
473 const Point<3> face_center = face->center();
474
475 if (std::abs(face_center[2] - bottom_left[2]) < 1e-10 * height)
476 face->set_boundary_id(mesh_obstacle ? 4 : 5); // bottom
477 else if (std::abs(face_center[2] - (bottom_left[2] + height)) <
478 1e-10 * height)
479 face->set_boundary_id(mesh_obstacle ? 5 : 6); // top
480 }
481 }
482}
483
484// Fallback make_grid definition for unsupported template parameters. This
485// provides a linker-visible symbol and a clear runtime error when the
486// class is instantiated for dim/spacedim combinations that are not
487// specialized above.
488template <int dim, int spacedim>
489void
491 Triangulation<dim, spacedim> & /*triangulation*/)
492{
493 AssertThrow(
494 false,
495 ExcMessage(
496 "GridUniformChannelWithMeshedCylinder is only supported for <2,2> and <3,3> <dim,spacedim> specializations."));
497}
498
499// Explicit template instantiations
Generates a rectangular channel mesh with a meshed cylinder obstacle. By default, this geometry defin...
void make_grid(Triangulation< dim, spacedim > &triangulation)
Generate the 2D or 3D channel mesh with a meshed cylinder and attach manifold objects for proper mesh...
Point< dim > center
Point that define the center of the cylinder in the xy-plane.
Point< dim > top_right
Point that define the top-right of the channel in the xy-plane.
Point< dim > bottom_left
Point that define the bottom-left of the channel in the xy-plane.
double outer_radius
Radius of the transition region between the cylinder and the channel.
GridUniformChannelWithMeshedCylinder(const std::string &grid_arguments)
Constructor that parses geometry parameters from a colon-separated string.
void generate_2d_channel_mesh(Triangulation< 2 > &triangulation, const Point< 2 > &bottom_left, const Point< 2 > &top_right, const Point< 2 > &center, const double inner_radius, const double outer_radius, const unsigned int pad_bottom, const unsigned int pad_top, const unsigned int pad_left, const unsigned int pad_right, const bool colorize)
Generate the 2D channel mesh geometry with a meshed cylinder.
double height
Extrusion height in the z direction, 3D only.
Tensor< 1, spacedim > value_string_to_tensor(const std::string &value_string)
Return the tensor corresponding to the value_string. If the dimension correspondence of the value_str...
Definition utilities.h:956