Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
grid_cube_merged.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
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 <numbers>
12
13template <int dim, int spacedim>
15{
16 if constexpr (!(dim == 3 && spacedim == 3))
17 {
18 AssertThrow(
19 false,
20 ExcMessage(
21 "Custom cube merged mesh is only supported in 3D space with 3D elements."));
22 return;
23 }
24
25 this->grid_arguments = grid_arguments;
26
27 // Separate arguments of the string
28 std::vector<std::string> arguments =
29 Utilities::split_string_list(grid_arguments, ':');
30
31 // Arguments declaration
32 if (arguments.size() != 4)
33 {
34 AssertThrow(
35 false,
36 ExcMessage(
37 "Mandatory cylinder parameters are (R_cylinder : L_plate : H : n_subdivisions)"));
38 }
39 else
40 {
41 this->cylinder_radius = Utilities::string_to_double(arguments[0]);
42 this->plate_half_length = Utilities::string_to_double(arguments[1]);
43 this->height = Utilities::string_to_double(arguments[2]);
44 this->n_subdivisions = Utilities::string_to_int(arguments[3]);
45
46 // To match the number of cells at the cylindrical interface, the cylinder
47 // geometry is refined once before being merged into the extruded plate.
48 // The prescribed number of subdivisions, therefore, needs to be divided
49 // by half to create the initial cylinder. Hence, he number of
50 // subdivisions needs to be an even number
51 AssertThrow(this->n_subdivisions % 2 == 0,
52 ExcMessage(
53 "The number of subdivisions needs to be an even number."));
54 }
55}
56
57
58template <>
59void
60GridCubeMerged<3, 3>::make_grid(Triangulation<3, 3> &triangulation)
61{
62 // Create grid of extruded plate with hole
63 Triangulation<3, 3> tria_plate;
64 GridGenerator::hyper_cube_with_cylindrical_hole(tria_plate,
67 height,
69 false);
70
71 // Create grid of cylinder
72 Triangulation<3, 3> tria_cylinder;
73 GridGenerator::subdivided_cylinder(tria_cylinder,
76 height / 2);
77
78 // Rotate the cylinder so that it is aligned with the z axis
79 GridTools::rotate(Tensor<1, 3>({0, 1, 0}),
80 std::numbers::pi / 2,
81 tria_cylinder);
82 // Shift the cylinder so that its 2D extrusion plane is the same as tria_plate
83 GridTools::shift(Tensor<1, 3>({0, 0, 1}), tria_cylinder);
84 // Remove cylindrical manifold in x direction
85 tria_cylinder.reset_all_manifolds();
86 // Set new manifold on z direction (manifold ID for the hull of the cylinder
87 // is 0)
88 tria_cylinder.set_manifold(0,
89 CylindricalManifold<3>(Tensor<1, 3>({0, 0, 1}),
90 Point<3>()));
91
92 // Refine the cylinder mesh once so that the number of cells at the interface
93 // match with tria_plate
94 tria_cylinder.refine_global(1);
95
96 // Flatten cylinder triangulation so that it can be merged with the extruded
97 // plate with hole
98 Triangulation<3, 3> tria_cylinder_flat;
99 GridGenerator::flatten_triangulation(tria_cylinder, tria_cylinder_flat);
100
101 // Merge triangulations
102 GridGenerator::merge_triangulations(tria_plate,
103 tria_cylinder_flat,
104 triangulation,
105 1e-8);
106
107 // Re-assign cylindrical manifold
108 double tol = 1e-10;
109 for (const auto &cell : triangulation.active_cell_iterators())
110 {
111 for (unsigned int f = 0; f < GeometryInfo<3>::faces_per_cell; ++f)
112 {
113 bool is_cylindrical_boundary = true;
114 for (unsigned int v = 0; v < GeometryInfo<3>::vertices_per_face; ++v)
115 {
116 const auto vertex = cell->face(f)->vertex(v);
117
118 // Check if vertex lies on the former cylindrical boundary
120 Point<3>(), Tensor<1, 3>({0, 0, 1}), vertex);
121 if (std::abs(distance - this->cylinder_radius) > tol)
122 {
123 is_cylindrical_boundary = false;
124 break;
125 }
126 }
127
128 if (is_cylindrical_boundary)
129 {
130 cell->face(f)->set_manifold_id(1);
131 for (unsigned int l = 0; l < GeometryInfo<3>::lines_per_face; ++l)
132 cell->face(f)->line(l)->set_manifold_id(1);
133 }
134 }
135 }
136
137 triangulation.set_manifold(0, FlatManifold<3>());
138 triangulation.set_manifold(1,
139 CylindricalManifold<3>(Tensor<1, 3>({0, 0, 1}),
140 Point<3>()));
141}
142
143// Fallback make_grid definition for unsupported template parameters. This
144// provides a linker-visible symbol and a clear runtime error when the
145// class is instantiated for dim/spacedim combinations that are not
146// specialized above.
147template <int dim, int spacedim>
148void
150 Triangulation<dim, spacedim> & /*triangulation*/)
151{
152 AssertThrow(
153 false,
154 ExcMessage(
155 "GridCubeMerged is only implemented for dim = 3 and spacedim = 3."));
156}
157
158// Explicit template instantiations
159template class GridCubeMerged<2, 2>;
160template class GridCubeMerged<2, 3>;
161template class GridCubeMerged<3, 3>;
Class that creates a custom geometry that combines an extruded plate with a hole and an inner cylinde...
unsigned int n_subdivisions
Number of subdivisions in the extruded direction (z axis).
void make_grid(Triangulation< dim, spacedim > &triangulation)
Generate the merged cube mesh.
double plate_half_length
Half edge length of the square in the extruded plate with hole mesh.
std::string grid_arguments
Arguments used to generate the grid.
GridCubeMerged(const std::string &grid_arguments)
Constructor for the GridCubeMerged.
double cylinder_radius
Radius of the cylindrical mesh.
double height
Height in the extruded direction (z axis).
double find_point_line_distance(const Point< dim > &line_origin, const Tensor< 1, dim > &line_direction, const Point< dim > &point)
Calculate the minimum distance between a point and a line (defined using its orign and direction)....