Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
grid_uniform_channel_with_meshed_square_prism.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#include <deal.II/grid/grid_tools.h>
9#include <deal.II/grid/manifold_lib.h>
10
11#include <algorithm>
12#include <array>
13#include <cmath>
14#include <limits>
15#include <numbers>
16#include <set>
17
18template <int dim, int spacedim>
20 GridUniformChannelWithMeshedSquarePrism(const std::string &grid_arguments)
21{
22 if constexpr (dim == 1 || spacedim == 1)
23 {
24 AssertThrow(
25 false,
26 ExcMessage(
27 "The uniform_channel_with_meshed_square_prism is only supported for <2,2> and <3,3> <dim,spacedim> specializations."));
28 return;
29 }
30 else if constexpr (dim == 2 && spacedim == 3)
31 {
32 AssertThrow(
33 false,
34 ExcMessage(
35 "The uniform_channel_with_meshed_square_prism is only supported for <2,2> and <3,3> <dim,spacedim> specializations."));
36 return;
37 }
38
39 const std::vector<std::string> arguments =
40 Utilities::split_string_list(grid_arguments, ':');
41
42 if (arguments.size() < 5)
43 {
44 AssertThrow(
45 false,
46 ExcMessage(
47 "Mandatory uniform_channel_with_meshed_square_prism parameters are (bottom left point : top right point : center of the obstacle : inner half-side : outer half-side). The points should be given as x,y and the half-sides should be single numbers. The optional parameters are (rotation_deg : pad_bottom : pad_top : pad_left : pad_right : height : n_slices : mesh_obstacle : colorize )."));
48 }
49
50 // Parse bottom_left point
51 try
52 {
53 Tensor<1, 2> bottom_left_coords = value_string_to_tensor<2>(arguments[0]);
54 this->bottom_left =
55 (dim == 2) ?
56 Point<dim>(bottom_left_coords[0], bottom_left_coords[1]) :
57 Point<dim>(bottom_left_coords[0], bottom_left_coords[1], 0.0);
58 }
59 catch (const std::exception &e)
60 {
61 AssertThrow(
62 false,
63 ExcMessage(
64 "The bottom left point should have 2 components coordinates (x,y format) because the channel is extruded in the z direction."));
65 }
66
67 // Parse top_right point
68 try
69 {
70 Tensor<1, 2> top_right_coords = value_string_to_tensor<2>(arguments[1]);
71 this->top_right =
72 (dim == 2) ? Point<dim>(top_right_coords[0], top_right_coords[1]) :
73 Point<dim>(top_right_coords[0], top_right_coords[1], 0.0);
74 }
75 catch (const std::exception &e)
76 {
77 AssertThrow(
78 false,
79 ExcMessage(
80 "The top right point should have 2 components coordinates (x,y format) because the channel is extruded in the z direction."));
81 }
82
83
84 // Parse center point
85 try
86 {
87 Tensor<1, 2> center_coords = value_string_to_tensor<2>(arguments[2]);
88 this->center = (dim == 2) ?
89 Point<dim>(center_coords[0], center_coords[1]) :
90 Point<dim>(center_coords[0], center_coords[1], 0.0);
91 }
92 catch (const std::exception &e)
93 {
94 AssertThrow(
95 false,
96 ExcMessage(
97 "The center point should have 2 components coordinates (x,y format) because the channel is extruded in the z direction."));
98 }
99
100
101 // Parse half-sides of inner and outer squares
102 this->inner_half_side = Utilities::string_to_double(arguments[3]);
103 this->outer_half_side = Utilities::string_to_double(arguments[4]);
104
105 AssertThrow(
106 inner_half_side > 0.0,
107 ExcMessage(
108 " The inner_half_side must be greater than 0 to have an object in the channel."));
109
110 AssertThrow(
112 ExcMessage(
113 "The outer half-side must be at least 1.5 times the inner half-side, if not the elements in the transition region will be too distorted."));
114
115
116 // Check if the outer transition square fits in the channel
117 AssertThrow((center[0] - outer_half_side >= bottom_left[0]) &&
118 (center[0] + outer_half_side <= top_right[0]) &&
119 (center[1] - outer_half_side >= bottom_left[1]) &&
120 (center[1] + outer_half_side <= top_right[1]),
121 ExcMessage(
122 "The outer transition square does not fit in the channel."));
123
124 // Parse optional parameters if provided
126 (arguments.size() > 5) ? Utilities::string_to_double(arguments[5]) : 0.0;
127
128 AssertThrow(
129 (inner_rotation_angle >= 0.0) && (inner_rotation_angle < 90.0),
130 ExcMessage("The rotation angle needs to be in the range [0, 90) degrees."));
131
132 inner_rotation_angle = inner_rotation_angle * std::numbers::pi / 180.0;
133
134 this->pad_bottom =
135 (arguments.size() > 6) ? Utilities::string_to_int(arguments[6]) : 0;
136 this->pad_top =
137 (arguments.size() > 7) ? Utilities::string_to_int(arguments[7]) : 0;
138 this->pad_left =
139 (arguments.size() > 8) ? Utilities::string_to_int(arguments[8]) : 0;
140 this->pad_right =
141 (arguments.size() > 9) ? Utilities::string_to_int(arguments[9]) : 0;
142
143 // Padding is required in each direction where the transition square does not
144 // reach the channel boundary (i.e., there is a gap to fill with rectangles).
145 AssertThrow(!((pad_bottom == 0) && (std::abs((center[1] - outer_half_side) -
146 bottom_left[1]) > 1e-12)),
147 ExcMessage("Bottom padding is required because the transition "
148 "region does not reach the bottom channel boundary."));
149 AssertThrow(!((pad_top == 0) && (std::abs((center[1] + outer_half_side) -
150 top_right[1]) > 1e-12)),
151 ExcMessage("Top padding is required because the transition "
152 "region does not reach the top channel boundary."));
153 AssertThrow(!((pad_left == 0) && (std::abs((center[0] - outer_half_side) -
154 bottom_left[0]) > 1e-12)),
155 ExcMessage("Left padding is required because the transition "
156 "region does not reach the left channel boundary."));
157 AssertThrow(!((pad_right == 0) && (std::abs((center[0] + outer_half_side) -
158 top_right[0]) > 1e-12)),
159 ExcMessage("Right padding is required because the transition "
160 "region does not reach the right channel boundary."));
161
162 this->height =
163 (arguments.size() > 10) ? Utilities::string_to_double(arguments[10]) : 1.0;
164 this->n_slices =
165 (arguments.size() > 11) ? Utilities::string_to_int(arguments[11]) : 2;
166 AssertThrow(n_slices >= 2, ExcMessage("The minimum number of slices is 2."));
167 this->mesh_obstacle =
168 (arguments.size() > 12 && arguments[12] == "true") ? true : false;
169 this->colorize =
170 (arguments.size() > 13 && arguments[13] == "true") ? true : false;
171}
172
173template <int dim, int spacedim>
174void
176 generate_2d_channel_mesh(Triangulation<2> &triangulation,
177 const Point<2> &bottom_left,
178 const Point<2> &top_right,
179 const Point<2> &center,
180 const double inner_half_side,
181 const double outer_half_side,
182 const double inner_rotation_angle,
183 const unsigned int pad_bottom,
184 const unsigned int pad_top,
185 const unsigned int pad_left,
186 const unsigned int pad_right,
187 const bool colorize)
188{
189 const types::material_id fluid_id = 0;
190 const types::material_id obstacle_id = 1;
191 const double tol_inner = 1e-12 * inner_half_side;
192
193 Triangulation<2> obstacle_tria;
194 // Start with a circular obstacle mesh, which helps define the edges and
195 // vertices links. The circular shape is then transformed to the square
196 // obstacle by moving the vertices. The outer ring of the circular mesh is
197 // used to define the transition region and is attached to the outer square by
198 // vertex projection, the inner vertices are shaped to form the inner square.
199 // To ensure that the inner square vertices can be easily identified even
200 // when rotated, the initial circular mesh is generated using twice the
201 // inner_half_side as radius and every vertex can be identified using a
202 // distance criterion.
203 GridGenerator::hyper_ball_balanced(obstacle_tria,
204 center,
205 2 * inner_half_side);
206
207 // We apply a transformation to shape inner vertices to form the inner square
208 GridTools::transform(
209 [&](const Point<2> &p) {
210 const double dist = p.distance(center);
211
212 // If the vertex is the one in the center, we keep it fixed.
213 if (dist <= tol_inner)
214 return p;
215
216 // If the vertex is within sqrt(2)*inner_half_side, it cannot be on the
217 // outer ring as we defined it to be 2*inner_half_side, so it is an inner
218 // vertex. We move it to the inner square by scaling the vector from the
219 // center to the vertex. The second condition checks if the vertex is on
220 // the diagonal (within a tolerance) in which case we need to scale it to
221 // the corner instead of the face of the inner square.
222 if ((dist < std::numbers::sqrt2 * inner_half_side + tol_inner) &&
223 (std::abs(std::abs(p[0] - center[0]) - std::abs(p[1] - center[1])) <
224 tol_inner))
225 return center +
226 std::numbers::sqrt2 * inner_half_side * (p - center) / dist;
227 // This is for non corner inner vertices, we scale them to the face of the
228 // inner square.
229 if ((dist < std::numbers::sqrt2 * inner_half_side + tol_inner) &&
230 (std::abs(std::abs(p[0] - center[0]) - std::abs(p[1] - center[1])) >=
231 tol_inner))
232 return center + (inner_half_side / dist) * (p - center);
233 return p;
234 },
235 obstacle_tria);
236
237 // We apply the rotation to the whole mesh if a rotation angle is specified.
238 if (inner_rotation_angle != 0.0)
239 {
240 const double cos_a = std::cos(inner_rotation_angle);
241 const double sin_a = std::sin(inner_rotation_angle);
242 GridTools::transform(
243 [&](const Point<2> &p) {
244 const double x_rel = p[0] - center[0];
245 const double y_rel = p[1] - center[1];
246 return Point<2>(center[0] + cos_a * x_rel - sin_a * y_rel,
247 center[1] + sin_a * x_rel + cos_a * y_rel);
248 },
249 obstacle_tria);
250 }
251
252 // Here we pre-compute the 8 projected positions of vertices that we want for
253 // the outer square. The outer ring of hyper_ball_balanced has vertices at
254 // angles k*pi/4. After rotation they sit at inner_rotation_angle + k*pi/4.
255 // The projection maps each direction onto the outer square along the same
256 // line. To avoid having voids in the corners we choose the subset of the 4
257 // target points that are closest to corners and change the target projection
258 // to attach them there instead.
259 std::array<Point<2>, 8> outer_targets;
260 for (unsigned int k = 0; k < 8; ++k)
261 {
262 double theta = inner_rotation_angle + k * std::numbers::pi / 4.0;
263
264 // If the rotation angle is between pi/18 and 4*pi/9, we want to project
265 // the vertices at the square middle points to the corners.
266 if (inner_rotation_angle >= std::numbers::pi / 18.0 - tol_inner &&
267 inner_rotation_angle < 4 * std::numbers::pi / 9.0 - tol_inner)
268 {
269 // The even k are associated to the vertices at the square middle
270 // points.
271 if (k % 2 == 0)
272 {
273 theta = (k + 1) * std::numbers::pi / 4.0;
274 }
275 }
276 // If the rotation angle is between 0 and pi/18 or between 4*pi/9 and
277 // pi/2, we want to project the vertices at the square corners to the
278 // corners.
279 else if (inner_rotation_angle >= 0.0 - tol_inner &&
280 inner_rotation_angle < std::numbers::pi / 18.0 - tol_inner)
281 {
282 // The odd k are associated to the vertices at the square corners.
283 if (k % 2 == 1)
284 {
285 theta = k * std::numbers::pi / 4.0;
286 }
287 }
288 else if (inner_rotation_angle >= 4 * std::numbers::pi / 9.0 - tol_inner &&
289 inner_rotation_angle < std::numbers::pi / 2.0 - tol_inner)
290 {
291 if (k % 2 == 1)
292 {
293 theta = (k + 2) * std::numbers::pi / 4.0;
294 }
295 }
296
297 const double cx = std::cos(theta);
298 const double cy = std::sin(theta);
299 const double scale =
300 outer_half_side / std::max(std::abs(cx), std::abs(cy));
301 outer_targets[k] = center + Point<2>(scale * cx, scale * cy);
302 }
303
304 // Attach each outer ring vertex to its analytical target (closest by angle).
305 // Using angle difference avoids the Euclidean distance ambiguity and ensures
306 // exact vertex matching with the padding meshes.
307 GridTools::transform(
308 [&](const Point<2> &p) {
309 const double dist = p.distance(center);
310
311 // Check if the vertex is an inside vertex. The outer vertex still lies on
312 // the circle defined by the hyper_ball_balanced, so we can use the same
313 // distance criterion as before to identify them.
314 if (dist <= std::numbers::sqrt2 * inner_half_side + tol_inner)
315 return p;
316
317 double min_angle = -1.0;
318 Point<2> closest = outer_targets[0];
319 // We compute the unitary vector from the center to the vertex and do the
320 // dot product with the unitary vector from the center to each target. The
321 // one with the highest cosine is the closest by angle and we project to
322 // that one (when the orientation is superposed, the cosine is 1, when it
323 // is orthogonal, the cosine is 0, and when it is opposite, the cosine is
324 // -1).
325 const Tensor<1, 2> p_normalize = (p - center) / (p - center).norm();
326 for (const Point<2> &candidate : outer_targets)
327 {
328 const Tensor<1, 2> target_normalize =
329 (candidate - center) / (candidate - center).norm();
330 const double cos_angle = p_normalize * target_normalize;
331
332 if (cos_angle > min_angle)
333 {
334 min_angle = cos_angle;
335 closest = candidate;
336 }
337 }
338 return closest;
339 },
340 obstacle_tria);
341
342 // Collect the projected x/y coordinates per outer-square face.
343 // Vertices landing exactly on a corner (|cos θ| == |sin θ|) are skipped
344 // because they already coincide with the corner of the padding rectangle.
345 // This is used to get the padding meshes to have vertices at the exact same
346 // positions so that merge_triangulations can merge them without needing to
347 // introduce new vertices.
348 std::vector<double> top_x, bottom_x, left_y, right_y;
349 for (unsigned int k = 0; k < 8; ++k)
350 {
351 const Point<2> &q = outer_targets[k];
352 const double theta = std::atan2(q[1] - center[1], q[0] - center[0]);
353 const double abs_cx = std::abs(std::cos(theta));
354 const double abs_cy = std::abs(std::sin(theta));
355
356 // If the vertex lands on the corner, we do not need to add a breakpoint
357 // for the padding meshes because they already have a vertex there.
358 if (std::abs(abs_cx - abs_cy) < 1e-12)
359 continue;
360
361 // If cos θ is larger than sin θ, then the vertex is either on the left or
362 // right face.
363 if (abs_cx > abs_cy)
364 {
365 if (std::cos(theta) > 0.0)
366 right_y.push_back(q[1]);
367 else
368 left_y.push_back(q[1]);
369 }
370 // If sin θ is larger than cos θ, then the vertex is either on the top or
371 // bottom face.
372 else
373 {
374 if (std::sin(theta) > 0.0)
375 top_x.push_back(q[0]);
376 else
377 bottom_x.push_back(q[0]);
378 }
379 }
380 std::ranges::sort(top_x);
381 std::ranges::sort(bottom_x);
382 std::ranges::sort(left_y);
383 std::ranges::sort(right_y);
384
385 // Build a vector of step sizes from sorted breakpoints + outer bounds.
386 // This lets subdivided_hyper_rectangle place vertices exactly at the
387 // projected positions so merge_triangulations sees coincident nodes.
388 // Example:
389 // inner = {x1, x2}, lo = a, hi = b
390 // coords = {a, x1, x2, b}
391 // steps = {x1-a, x2-x1, b-x2}
392 auto make_steps = [](const std::vector<double> &inner_coords,
393 const double lo,
394 const double hi) {
395 // Create the coordinates list
396 std::vector<double> coords = {lo};
397 for (const double c : inner_coords)
398 coords.push_back(c);
399 coords.push_back(hi);
400
401 // Create the steps list
402 std::vector<double> steps;
403 for (std::size_t i = 1; i < coords.size(); ++i)
404 steps.push_back(coords[i] - coords[i - 1]);
405 return steps;
406 };
407
408 // Similar as above, this lambda function creates a vector of uniform step
409 // sizes given the number of steps n and the total length to cover.
410 auto uniform = [](const unsigned int n, const double length) {
411 return std::vector<double>(n, length / static_cast<double>(n));
412 };
413
414 // Bottom pad: columns split at bottom_x projected coords
415 Triangulation<2> pad_bottom_tria;
416 if (pad_bottom > 0)
417 GridGenerator::subdivided_hyper_rectangle(
418 pad_bottom_tria,
419 {make_steps(bottom_x,
422 uniform(pad_bottom, center[1] - outer_half_side - bottom_left[1])},
423 Point<2>(center[0] - outer_half_side, bottom_left[1]),
424 Point<2>(center[0] + outer_half_side, center[1] - outer_half_side));
425
426 // Top pad: columns split at top_x projected coords
427 Triangulation<2> pad_top_tria;
428 if (pad_top > 0)
429 GridGenerator::subdivided_hyper_rectangle(
430 pad_top_tria,
431 {make_steps(top_x,
434 uniform(pad_top, top_right[1] - (center[1] + outer_half_side))},
435 Point<2>(center[0] - outer_half_side, center[1] + outer_half_side),
436 Point<2>(center[0] + outer_half_side, top_right[1]));
437
438 // Left pad: rows split at left_y projected coords
439 Triangulation<2> pad_left_tria;
440 if (pad_left > 0)
441 GridGenerator::subdivided_hyper_rectangle(
442 pad_left_tria,
443 {uniform(pad_left, center[0] - outer_half_side - bottom_left[0]),
444 make_steps(left_y,
446 center[1] + outer_half_side)},
447 Point<2>(bottom_left[0], center[1] - outer_half_side),
448 Point<2>(center[0] - outer_half_side, center[1] + outer_half_side));
449
450 // Right pad: rows split at right_y projected coords
451 Triangulation<2> pad_right_tria;
452 if (pad_right > 0)
453 GridGenerator::subdivided_hyper_rectangle(
454 pad_right_tria,
455 {uniform(pad_right, top_right[0] - (center[0] + outer_half_side)),
456 make_steps(right_y,
458 center[1] + outer_half_side)},
459 Point<2>(center[0] + outer_half_side, center[1] - outer_half_side),
460 Point<2>(top_right[0], center[1] + outer_half_side));
461
462 // Corner pads never touch the obstacle tria boundary, so no special split
463 // needed
464 Triangulation<2> pad_bottom_left_corner_tria;
465 if (pad_bottom > 0 && pad_left > 0)
466 GridGenerator::subdivided_hyper_rectangle(
467 pad_bottom_left_corner_tria,
470 Point<2>(center[0] - outer_half_side, center[1] - outer_half_side));
471
472 Triangulation<2> pad_bottom_right_corner_tria;
473 if (pad_bottom > 0 && pad_right > 0)
474 GridGenerator::subdivided_hyper_rectangle(
475 pad_bottom_right_corner_tria,
477 Point<2>(center[0] + outer_half_side, bottom_left[1]),
478 Point<2>(top_right[0], center[1] - outer_half_side));
479
480 Triangulation<2> pad_top_left_corner_tria;
481 if (pad_top > 0 && pad_left > 0)
482 GridGenerator::subdivided_hyper_rectangle(
483 pad_top_left_corner_tria,
484 {pad_left, pad_top},
485 Point<2>(bottom_left[0], center[1] + outer_half_side),
486 Point<2>(center[0] - outer_half_side, top_right[1]));
487
488 Triangulation<2> pad_top_right_corner_tria;
489 if (pad_top > 0 && pad_right > 0)
490 GridGenerator::subdivided_hyper_rectangle(
491 pad_top_right_corner_tria,
493 Point<2>(center[0] + outer_half_side, center[1] + outer_half_side),
494 top_right);
495
496 // Merge only non-empty triangulations
497 std::vector<const Triangulation<2> *> trias = {&obstacle_tria};
498 if (pad_bottom > 0)
499 trias.push_back(&pad_bottom_tria);
500 if (pad_top > 0)
501 trias.push_back(&pad_top_tria);
502 if (pad_left > 0)
503 trias.push_back(&pad_left_tria);
504 if (pad_right > 0)
505 trias.push_back(&pad_right_tria);
506 if (pad_bottom > 0 && pad_left > 0)
507 trias.push_back(&pad_bottom_left_corner_tria);
508 if (pad_bottom > 0 && pad_right > 0)
509 trias.push_back(&pad_bottom_right_corner_tria);
510 if (pad_top > 0 && pad_left > 0)
511 trias.push_back(&pad_top_left_corner_tria);
512 if (pad_top > 0 && pad_right > 0)
513 trias.push_back(&pad_top_right_corner_tria);
514 GridGenerator::merge_triangulations(trias, triangulation);
515
516 triangulation.reset_all_manifolds();
517 triangulation.set_all_manifold_ids(0);
518
519 // Here we mark obstacle cells with the obstacle material ID. This is done in
520 // the unrotated frame by rotating the cell center back to the axis-aligned
521 // frame temporarily and checking if it is within the inner square dimensions.
522 for (const auto &cell : triangulation.active_cell_iterators())
523 {
524 const Point<2> c = cell->center();
525
526 // Translate to obstacle-centered coordinates
527 const double dx = c[0] - center[0];
528 const double dy = c[1] - center[1];
529
530 // Rotate point back to axis-aligned frame
531 const double cos_a = std::cos(inner_rotation_angle);
532 const double sin_a = std::sin(inner_rotation_angle);
533
534 const double x_local = cos_a * dx + sin_a * dy;
535 const double y_local = -sin_a * dx + cos_a * dy;
536
537 // Axis-aligned square test
538 const bool inside_obstacle =
539 (std::abs(x_local) <= inner_half_side + tol_inner) &&
540 (std::abs(y_local) <= inner_half_side + tol_inner);
541
542 cell->set_material_id(inside_obstacle ? obstacle_id : fluid_id);
543 }
544
545 // If the obstacle is not meshed, we remove the cells with the obstacle
546 // material
547 if (!mesh_obstacle)
548 {
549 std::set<typename Triangulation<2>::active_cell_iterator> obstacle_cells;
550 // We loop over the cells to find the ones with the obstacle material ID
551 // that we want to remove.
552 for (const auto &cell : triangulation.active_cell_iterators())
553 {
554 if (cell->material_id() == obstacle_id)
555 obstacle_cells.insert(cell);
556 }
557 Triangulation<2> temp_tria;
558 GridGenerator::create_triangulation_with_removed_cells(triangulation,
559 obstacle_cells,
560 temp_tria);
561 triangulation.clear();
562 triangulation.copy_triangulation(temp_tria);
563 }
564
565 // Assign boundary IDs following the subdivided_hyper_rectangle
566 // convention:
567 // 0: obstacle, 1: left (-x), 2: right (+x), 3: bottom (-y), 4: top
568 // (+y), if no obstacle the boundary ids are shifted by -1
569 if (colorize)
570 {
571 const double tol_x = 1e-10 * (top_right[0] - bottom_left[0]);
572 const double tol_y = 1e-10 * (top_right[1] - bottom_left[1]);
573 for (const auto &face : triangulation.active_face_iterators())
574 {
575 if (!face->at_boundary())
576 continue;
577 const Point<2> fc = face->center();
578 if (std::abs(fc[0] - bottom_left[0]) < tol_x)
579 face->set_boundary_id(mesh_obstacle ? 0 : 1);
580 else if (std::abs(fc[0] - top_right[0]) < tol_x)
581 face->set_boundary_id(mesh_obstacle ? 1 : 2);
582 else if (std::abs(fc[1] - bottom_left[1]) < tol_y)
583 face->set_boundary_id(mesh_obstacle ? 2 : 3);
584 else if (std::abs(fc[1] - top_right[1]) < tol_y)
585 face->set_boundary_id(mesh_obstacle ? 3 : 4);
586 else
587 face->set_boundary_id(0);
588 }
589 }
590}
591
592template <>
593void
595 Triangulation<2, 2> &triangulation)
596{
597 generate_2d_channel_mesh(triangulation,
599 top_right,
600 center,
605 pad_top,
606 pad_left,
607 pad_right,
608 colorize);
609
610 // Attach manifold for id 0 required by deal.II
611 FlatManifold<2, 2> FlatManifold;
612 triangulation.set_manifold(0, FlatManifold);
613}
614
615
616template <>
617void
619 Triangulation<3, 3> &triangulation)
620{
621 // Generate the 2D cross-section (geometry + material IDs + boundary IDs)
622 Triangulation<2> tria_2D;
624 Point<2>(bottom_left[0], bottom_left[1]),
625 Point<2>(top_right[0], top_right[1]),
626 Point<2>(center[0], center[1]),
631 pad_top,
632 pad_left,
633 pad_right,
634 colorize);
635
636 // Attach manifold id 0 on the 2D mesh before extrusion.
637 // extrude_triangulation queries get_manifold() on the input 2D triangulation.
638 FlatManifold<2, 2> FlatManifold_2d;
639 tria_2D.set_manifold(0, FlatManifold_2d);
640
641 // Extrude the 2D cross-section along the z-axis. Manifold and material IDs
642 // from the 2D mesh are copied to the lateral faces of the 3D mesh.
643 GridGenerator::extrude_triangulation(
644 tria_2D, n_slices, height, triangulation, true);
645
646 // Attach manifold for id 0 required by deal.II
647 FlatManifold<3, 3> FlatManifold;
648 triangulation.set_manifold(0, FlatManifold);
649
650 // Set the boundary ids for the extruded top and bottom faces.
651 if (colorize)
652 {
653 for (const auto &face : triangulation.active_face_iterators())
654 {
655 if (!face->at_boundary())
656 continue;
657
658 const Point<3> face_center = face->center();
659
660 if (std::abs(face_center[2] - bottom_left[2]) < 1e-10 * height)
661 face->set_boundary_id(mesh_obstacle ? 4 : 5); // bottom
662 else if (std::abs(face_center[2] - (bottom_left[2] + height)) <
663 1e-10 * height)
664 face->set_boundary_id(mesh_obstacle ? 5 : 6); // top
665 }
666 }
667}
668
669// Fallback make_grid definition for unsupported template parameters.
670template <int dim, int spacedim>
671void
673 Triangulation<dim, spacedim> & /*triangulation*/)
674{
675 AssertThrow(
676 false,
677 ExcMessage(
678 "GridUniformChannelWithMeshedSquarePrism is only supported for <2,2> and <3,3> <dim,spacedim> specializations."));
679}
680
681// Explicit template instantiations
Generates a rectangular channel mesh with a meshed square obstacle in 2D (extruded to a square prism ...
double inner_rotation_angle
In-plane rotation angle in radians applied to the inner mesh pattern.
double outer_half_side
Half-side of the outer transition square.
double inner_half_side
Half-side of the inner square obstacle.
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_half_side, const double outer_half_side, const double inner_rotation_angle, 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 square obstacle.
Point< dim > bottom_left
Point that define the bottom-left of the channel in the xy-plane.
double height
Extrusion height in the z direction, 3D only.
Point< dim > top_right
Point that define the top-right of the channel in the xy-plane.
void make_grid(Triangulation< dim, spacedim > &triangulation)
Generate the 2D/3D channel mesh with a meshed square obstacle.
bool mesh_obstacle
Whether to mesh the inner square obstacle or leave it as a hole.
GridUniformChannelWithMeshedSquarePrism(const std::string &grid_arguments)
Constructor that parses geometry parameters from a colon-separated string.
Point< dim > center
Point that define the center of the obstacle in the xy-plane.
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