Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
grid_pipe_and_waveguide.cc
Go to the documentation of this file.
2
3#include <deal.II/grid/grid_generator.h>
4#include <deal.II/grid/grid_out.h>
5#include <deal.II/grid/grid_tools.h>
6#include <deal.II/grid/manifold_lib.h>
7#include <deal.II/grid/tria.h>
8#include <deal.II/grid/tria_description.h>
9
10#include <algorithm>
11#include <cmath>
12#include <fstream>
13#include <iostream>
14#include <numbers>
15
16using namespace dealii;
17
18/*
19This file creates a mesh that represents a rectangular volume entering a
20cylinder laterally, perpendicular to its axis.
21
22The program is divided into two parts:
23
241)The function "detailed_circle" is first defined, inspired by
25hyper_ball in the deal.II library. This function creates a 2D coarse mesh of a
26circle which is adapted to be connected with a rectangle on its side.
27
282)The core of the file creates the 3D pipe assembled with the waveguide
29*/
30
31// See the doxygen documentation to learn more about this function
32template <int dim, int spacedim>
33void
35 const Point<2> &p,
36 const double &a,
37 const double &b,
38 const double r,
39 const double R,
40 const bool inner_circle)
41{
42 const auto embed_point = [](const double x, const double y) -> Point<2> {
43 return Point<2>(x, y);
44 };
45
46 const auto vertices = [&]() -> std::vector<Point<2>> {
47 if (inner_circle)
48 // Inner circle
49 return {
50 p + embed_point(-1., -1.) * (r / std::numbers::sqrt2),
51 p + embed_point(+1., -1.) * (r / std::numbers::sqrt2),
52 p + embed_point(-1., +1.) * (r / std::numbers::sqrt2),
53 p + embed_point(+1., +1.) * (r / std::numbers::sqrt2),
54 // The four points which make the inner square
55 p + embed_point(-1., +1.) * (R / std::numbers::sqrt2),
56 p + embed_point(+1., +1.) * (R / std::numbers::sqrt2),
57 p + embed_point(-1., -1.) * (R / std::numbers::sqrt2),
58 p + embed_point(+1., -1.) * (R / std::numbers::sqrt2),
59 // Two points linked with the rectangle on is left
60 embed_point(b, 0),
61 embed_point(b, a),
62 // The two points (0, r) and (0, -r) from the center of the circle
63 p + embed_point(0, -r),
64 p + embed_point(0, +r),
65 // The two points on the right side of the outer circle that lie along
66 // the width of the rectangle
67 p + embed_point(std::sqrt(r * r - a * a / 4), -a / 2),
68 p + embed_point(std::sqrt(r * r - a * a / 4), +a / 2),
69 // The other points where the width of the rectangle intersects the
70 // central square
71 p + embed_point(-(std::sqrt(R * R - a * a / 4)), +a / 2),
72 p + embed_point(-(std::sqrt(R * R - a * a / 4)), -a / 2),
73 p + embed_point(+(std::sqrt(R * R - a * a / 4)), -a / 2),
74 p + embed_point(+(std::sqrt(R * R - a * a / 4)), +a / 2),
75 // The two points of the central square on the circle's axis of symmetry
76 p + embed_point(0, -R),
77 p + embed_point(0, +R),
78 // The two points where the width of the rectangle intersects the axis
79 // of symmetry of the circle
80 p + embed_point(0, +a / 2),
81 p + embed_point(0, -a / 2)};
82
83 else
84 // Inner square
85 return {
86 p + embed_point(-1., -1.) * (r / std::numbers::sqrt2),
87 p + embed_point(+1., -1.) * (r / std::numbers::sqrt2),
88 p + embed_point(-1., +1.) * (r / std::numbers::sqrt2),
89 p + embed_point(+1., +1.) * (r / std::numbers::sqrt2),
90 // The other points that form the inner square
91 p + embed_point(-1., -1.) * R,
92 p + embed_point(+1., -1.) * R,
93 p + embed_point(-1., +1.) * R,
94 p + embed_point(+1., +1.) * R,
95 // The two points attached to the rectangle
96 embed_point(b, 0),
97 embed_point(b, a),
98 // The two points (0, r) and (0, -r) from the center of the circle
99 p + embed_point(0, -r),
100 p + embed_point(0, +r),
101 // The two points on the right side of the outer circle that lie along
102 // the width of the rectangle
103 p + embed_point(std::sqrt(r * r - a * a / 4), -a / 2),
104 p + embed_point(std::sqrt(r * r - a * a / 4), +a / 2),
105 // The other points where the width of the rectangle intersects the
106 // central square
107 p + embed_point(-R, +a / 2),
108 p + embed_point(-R, -a / 2),
109 p + embed_point(+R, -a / 2),
110 p + embed_point(+R, +a / 2),
111 // The two points of the central square on the circle's axis of symmetry
112 p + embed_point(0, -R),
113 p + embed_point(0, +R),
114 // The two points where the width of the rectangle intersects the axis
115 // of symmetry of the circle
116 p + embed_point(0, +a / 2),
117 p + embed_point(0, -a / 2)};
118 }();
119
120 std::vector<CellData<2>> cells(16, CellData<2>());
121 static constexpr int circle_cell_vertices[16][4] = {
122 // We start with the 6 squares in the center square
123 {6, 18, 15, 21},
124 {15, 21, 14, 20},
125 {14, 20, 4, 19},
126 {18, 7, 21, 16},
127 {21, 16, 20, 17},
128 {20, 17, 19, 5},
129 // We fill in the outer cells, starting from the top left
130 {9, 14, 2, 4},
131 {8, 15, 9, 14},
132 {0, 6, 8, 15},
133 {0, 10, 6, 18},
134 {10, 1, 18, 7},
135 {7, 1, 16, 12},
136 {16, 12, 17, 13},
137 {17, 13, 5, 3},
138 {19, 5, 11, 3},
139 {4, 19, 2, 11}};
140
141 for (unsigned int i = 0; i < 16; ++i)
142 {
143 for (unsigned int j = 0; j < 4; ++j)
144 cells[i].vertices[j] = circle_cell_vertices[i][j];
145 }
146
147 tria.create_triangulation(std::vector<Point<2>>(std::begin(vertices),
148 std::end(vertices)),
149 cells,
150 SubCellData()); // no boundary information
151 tria.set_all_manifold_ids_on_boundary(0);
152 tria.set_manifold(0, SphericalManifold<2, 2>(p));
153 tria.set_manifold(1, FlatManifold<2, 2>());
154}
155
156
157
158// The core of the file begins here
159
160template <int dim, int spacedim>
162 const std::string &grid_arguments)
163{
164 const std::vector<std::string> arguments =
165 Utilities::split_string_list(grid_arguments, ':');
166
167 rectangle_width = std::stod(arguments[0]);
168 rectangle_length = std::stod(arguments[1]);
169 outer_radius = std::stod(arguments[2]);
170 inner_length = std::stod(arguments[3]);
171 bottom_height = std::stod(arguments[4]);
172 middle_height = std::stod(arguments[5]);
173 top_height = std::stod(arguments[6]);
174 bottom_resolution = std::stoi(arguments[7]);
175 middle_resolution = std::stoi(arguments[8]);
176 top_resolution = std::stoi(arguments[9]);
177 inner_circle = (arguments[10] == "true");
178}
179
180template <int dim, int spacedim>
181void
183 Triangulation<dim, spacedim> &triangulation)
184{
185 /*
186 Two geometric conditions that need to be verified to prevent the generation of
187 inverted cells.
188
189 The first condition concerns the inner shape. To improve mesh quality, a
190 central rectangle of width equal to the waveguide width is introduced. After
191 extrusion, the cells within this rectangle are nearly hexahedral, which
192 minimizes aspect ratio. However, this rectangle imposes a geometric
193 constraint: the inner shape must be large enough to fully contain it,
194 otherwise inverted cells are generated.
195
196 Then, the inner shape have to be strictly inscribed in the outer shape.
197 */
198
199 // Tests when the inner shape is a circle
200 if (inner_circle)
201 {
202 AssertThrow(
203 inner_length > rectangle_width / 2,
204 ExcMessage(
205 "The inner shape is not bigger than the rectangle that crosses the cylinder along the x-axis. This situations leads to the generation of inverted cells"));
206
207 AssertThrow(
208 inner_length < outer_radius,
209 ExcMessage(
210 "The internal radius is greater than the external radius. This situations leads to the generation of inverted cells"));
211 }
212
213 // Tests when the inner shape is a square
214 else
215 {
216 AssertThrow(
217 inner_length > rectangle_width / 2,
218 ExcMessage(
219 "The square side is too small compared to the width of the waveguide. This situations leads to the generation of inverted cells"));
220
221 AssertThrow(
222 inner_length < outer_radius / (std::sqrt(2)),
223 ExcMessage(
224 "The inner square length is greater than the external radius. This situations leads to the generation of inverted cells"));
225 }
226
227 // Dimension Test
228 if constexpr (!(dim == 3 && spacedim == 3))
229 {
230 AssertThrow(false,
231 ExcMessage("GridPipeAndWaveguide is only supported in 3D."));
232 return;
233 }
234
235 else
236 {
237 // COARSE 2D PARTS OF THE MESH
238
239 // PART -I-:Creating the Shape of the Medium: The paving stone embedded in
240 // the Cylinder
241
242 // Creating the first shape: a rectangle (after extrusion, this rectangle
243 // will represent the paving stone). This rectangle is the reason why we
244 // divide the volume into three different pieces.
245 Triangulation<2> rect;
246 GridGenerator::hyper_rectangle(
247 rect, Point<2>(0.0, 0.0), Point<2>(rectangle_length, rectangle_width));
248
249 // Creating the second shape: a rough circle with two points located
250 // exactly at the right corners of the rectangle
251 Triangulation<2> disk;
252 // Position of the center of the circle
253 const Point<2> center(rectangle_length +
254 sqrt(outer_radius * outer_radius -
255 rectangle_width * rectangle_width / 4),
256 rectangle_width / 2);
257 detailed_circle(disk,
258 center,
259 rectangle_width,
260 rectangle_length,
261 outer_radius,
262 inner_length,
263 inner_circle);
264
265 Triangulation<2> merged;
266 // The last argument of the following function is
267 // false since it is useless to copy the manifolds ids
268 // because we will set them after
269 GridGenerator::merge_triangulations(rect, disk, merged, 1e-12, false);
270
271 // PART -II- Creating the bottom cylinder
272 Triangulation<2, 2> base;
273 detailed_circle(base,
274 center,
275 rectangle_width,
276 rectangle_length,
277 outer_radius,
278 inner_length,
279 inner_circle);
280
281 // PART -III- Creating the top cylinder
282 Triangulation<2, 2> top;
283 detailed_circle(top,
284 center,
285 rectangle_width,
286 rectangle_length,
287 outer_radius,
288 inner_length,
289 inner_circle);
290
291 // EXTRUDE
292
293 Triangulation<3> extruded_base;
294 Triangulation<3> extruded_middle;
295 Triangulation<3> extruded_top;
296 GridGenerator::extrude_triangulation(base,
297 bottom_resolution,
298 bottom_height,
299 extruded_base);
300 GridGenerator::extrude_triangulation(merged,
301 middle_resolution,
302 middle_height,
303 extruded_middle);
304 GridGenerator::extrude_triangulation(top,
305 top_resolution,
306 top_height,
307 extruded_top);
308
309 // MANIFOLD
310
311 // Lambda function that places the id of the cylindrical manifold in the
312 // inside of the cylinder
313 auto attach_cylindrical_manifold_id = [&](Triangulation<3> &tria,
314 double height) {
315 for (const auto &cell : tria.active_cell_iterators())
316 if (cell->center()[0] >= rectangle_length - 1e-6)
317 // Concerns only the cylinder because every cell of the
318 // waveguide verifies the condition : x >= rectangle_length
319 for (const auto f : cell->face_indices())
320 if (cell->face(f)->at_boundary())
321 {
322 const Point<3> face_center = cell->face(f)->center();
323 if (std::abs(face_center[2]) > 1e-10 &&
324 std::abs(face_center[2] - height) > 1e-10)
325 // Does not take into account the the flat faces
326 // at z=0 and z=height because we do not want a manifold
327 // applied there.
328 cell->set_all_manifold_ids(1);
329 }
330 };
331
332 attach_cylindrical_manifold_id(extruded_base, bottom_height);
333 attach_cylindrical_manifold_id(extruded_middle, middle_height);
334 attach_cylindrical_manifold_id(extruded_top, top_height);
335
336 // Now that we have the IDs in the right place, we can install the
337 // manifolds
338 const Point<3> center_3d(center[0], center[1], 0.0);
339 const Tensor<1, 3> axis({0.0, 0.0, 1.0});
340 extruded_base.set_manifold(1, CylindricalManifold<3>(axis, center_3d));
341 extruded_middle.set_manifold(1, CylindricalManifold<3>(axis, center_3d));
342 extruded_top.set_manifold(1, CylindricalManifold<3>(axis, center_3d));
343
344 // MERGING THE 3D MESHES
345
346 // Move the cylinders in the right positions to prepare the merge
347 Tensor<1, 3> shift_down;
348 shift_down[0] = 0.0;
349 shift_down[1] = 0.0;
350 shift_down[2] = -bottom_height; // The base_cylinder must have its base at
351 // -bottom_height
352 GridTools::shift(shift_down, extruded_base);
353
354
355 Tensor<1, 3> shift_up;
356 shift_up[0] = 0.0;
357 shift_up[1] = 0.0;
358 shift_up[2] = +middle_height; // The top_cylinder must have its base at
359 // +middle_height
360 GridTools::shift(shift_up, extruded_top);
361
362
363 // Merge the 3 parts of the mesh
364 std::vector<const Triangulation<3, 3> *> parts = {&extruded_base,
365 &extruded_middle,
366 &extruded_top};
367
368 GridGenerator::merge_triangulations(
369 parts,
370 triangulation,
371 1e-12,
372 true); // “true” is used here to preserve the cell IDs; this is useful
373 // for attaching the manifold right after
374
375 // Attach the manifolds that we previously set on each part
376 triangulation.set_manifold(1, CylindricalManifold<3>(axis, center_3d));
377
378
379 // BOUNDARY AND MATERIAL IDs
380
381 /*
382 //Boundary
383 0: MW Inlet (x=0)
384 1: Fluid Inlet (y = -bottom_height)
385 2: Fluid Outlet (y = middle_height + top_height)
386 3: PEC walls (every other wall)
387
388 //Material
389 0: Fluid part (x> rectangle_length)
390 1: Waveguide paving stone (x<= rectangle_length)
391 */
392
393 for (auto &cell : triangulation.active_cell_iterators())
394 {
395 // Boundary ids
396 for (const auto f : cell->face_indices())
397 {
398 if (cell->face(f)->at_boundary())
399 {
400 const Point<3> face_center = cell->face(f)->center();
401 if (face_center[0] <= 1e-6)
402 cell->face(f)->set_boundary_id(0); // MW Inlet
403 else if (face_center[2] <= -bottom_height + 1e-6)
404 cell->face(f)->set_boundary_id(1); // Fluid Inlet
405 else if (face_center[2] >= middle_height + top_height - 1e-6)
406 cell->face(f)->set_boundary_id(2); // Fluid Outlet
407 else
408 cell->face(f)->set_boundary_id(
409 3); // PEC Walls (Perfect Electric Conductors)
410 }
411 }
412 // Material ids
413 if (cell->center()[0] <= rectangle_length)
414 cell->set_material_id(1); // waveguide (Paving stone)
415 else
416 cell->set_material_id(0); // Fluid (cylinder)
417 }
418 }
419}
420
421// Explicit instantiations
422template class GridPipeAndWaveguide<2, 2>;
423template class GridPipeAndWaveguide<2, 3>;
424template class GridPipeAndWaveguide<3, 3>;
Generates a 3D mesh of a waveguide embedded in a cylindrical cavity in which a liquid flows.