Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
grid_periodic_hills.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>
11 const std::string &grid_arguments)
12{
13 this->grid_arguments = grid_arguments;
14
15 // Separate arguments of the string
16 std::vector<std::string> arguments =
17 Utilities::split_string_list(grid_arguments, ':');
18
19 std::vector<double> arguments_double =
20 dealii::Utilities::string_to_double(arguments);
21 spacing_y = arguments_double[0];
22 alpha = arguments_double[1];
23 repetitions_x = static_cast<int>(arguments_double[2]);
24 repetitions_y = static_cast<int>(arguments_double[3]);
25 if (dim == 3)
26 repetitions_z = static_cast<int>(arguments_double[4]);
27
28 if (abs(alpha - 1) < 1e-6)
29 alpha = int(alpha);
30}
31
32
33template <int dim, int spacedim>
34void
36 const Point<spacedim> &op,
37 Vector<double> &values) const
38{
39 const Point<spacedim> np =
41
42 values(0) = np[0];
43 values(1) = np[1];
44
45 if (spacedim == 3)
46 values(2) = np[2];
47}
48
49
50template <int dim, int spacedim>
51double
53 const Point<spacedim> &op,
54 const unsigned int component) const
55{
56 const Point<spacedim> np =
58 return np[component];
59}
60
61
62template <int dim, int spacedim>
63void
65 Vector<double> &values) const
66{
67 double x = np[0];
68 double max_y = 3.035;
69 double max_x = 9.0;
70 double flat_region_length = 5.142;
71 double left_hill = 1.929;
72 double right_hill = 7.071;
73 double min_y;
74
75 // Reversing elongation and shifting of x lines
76 if (alpha != 1)
77 {
78 if (x < left_hill * alpha)
79 x = (x / alpha);
80 else if (x > alpha * left_hill + flat_region_length)
81 x = (x - flat_region_length - alpha * left_hill) / alpha + right_hill;
82 else
83 x = x - (alpha * left_hill) + left_hill;
84 }
85
86 if (alpha > 1)
87 {
88 if (x < max_x / 2)
89 x = (-(1 - 0.5) +
90 std::sqrt(std::pow((1 - 0.5), 2) - (4 * (1 / max_x) * -x))) /
91 (2 / max_x);
92 else if (x > max_x / 2 && x < max_x)
93 x = (-(1 + 1.5) + std::sqrt(std::pow((1 + 1.5), 2) -
94 (4 * (-1 / max_x) * (-0.5 * max_x - x)))) /
95 (2 * -1 / max_x);
96 }
97
98 // Reversing polynomial transformation and shifting of y lines
99 if (spacedim == 2)
100 {
101 min_y =
103 spacing_y,
104 alpha)[1];
105 }
106 else if (spacedim == 3)
107 {
109 Point<spacedim>(x, 0, np[2]), spacing_y, alpha)[1];
110 values(2) = np[2];
111 }
112
113 double y = (np[1] - min_y) / (1 - min_y / max_y);
114
115 if (y < max_y / 2 && spacing_y != 0)
116 y = (-(1 - 0.5 * spacing_y) + std::sqrt(std::pow((1 - 0.5 * spacing_y), 2) -
117 (4 * (spacing_y / max_y) * -y))) /
118 (2 * spacing_y / max_y);
119 else if (y > max_y / 2 && y < max_y && spacing_y != 0)
120 y =
121 (-(1 + 1.5 * spacing_y) +
122 std::sqrt(std::pow((1 + 1.5 * spacing_y), 2) -
123 (4 * (-spacing_y / max_y) * (-0.5 * spacing_y * max_y - y)))) /
124 (2 * -spacing_y / max_y);
125
126 values(0) = x;
127 values(1) = y;
128}
129
130
131template <int dim, int spacedim>
132double
134 const unsigned int component) const
135{
136 double x = np[0];
137 double max_y = 3.035;
138 double max_x = 9.0;
139 double flat_region_length = 5.142;
140 double left_hill = 1.929;
141 double right_hill = 7.071;
142 double min_y;
143
144 if (alpha != 1)
145 {
146 if (x < left_hill * alpha)
147 x = (x / alpha);
148 else if (x > alpha * left_hill + flat_region_length)
149 x = (x - flat_region_length - alpha * left_hill) / alpha + right_hill;
150 else
151 x = x - (alpha * left_hill) + left_hill;
152 }
153
154 if (alpha > 1)
155 {
156 if (x < max_x / 2)
157 x = (-(1 - 0.5) +
158 std::sqrt(std::pow((1 - 0.5), 2) - (4 * (1 / max_x) * -x))) /
159 (2 / max_x);
160 else if (x > max_x / 2 && x <= max_x)
161 x = (-(1 + 1.5) + std::sqrt(std::pow((1 + 1.5), 2) -
162 (4 * (-1 / max_x) * (-0.5 * max_x - x)))) /
163 (2 * -1 / max_x);
164 }
165
166 if (spacedim == 2)
167 min_y =
169 spacing_y,
170 alpha)[1];
171 else if (spacedim == 3)
173 Point<spacedim>(x, 0, np[2]), spacing_y, alpha)[1];
174
175
176 double y = (np[1] - min_y) / (1 - min_y / max_y);
177
178 if (y < max_y / 2 && spacing_y != 0)
179 y = (-(1 - 0.5 * spacing_y) + std::sqrt(std::pow((1 - 0.5 * spacing_y), 2) -
180 (4 * (spacing_y / max_y) * -y))) /
181 (2 * spacing_y / max_y);
182 else if (y > max_y / 2 && y < max_y && spacing_y != 0)
183 y =
184 (-(1 + 1.5 * spacing_y) +
185 std::sqrt(std::pow((1 + 1.5 * spacing_y), 2) -
186 (4 * (-spacing_y / max_y) * (-0.5 * spacing_y * max_y - y)))) /
187 (2 * -spacing_y / max_y);
188
189 Point<spacedim> op;
190 if (spacedim == 2)
191 op = {x, y};
192 else if (spacedim == 3)
193 op = {x, y, np[2]};
194
195 return op[component];
196}
197
198
199template <int dim, int spacedim>
200Point<spacedim>
202 double spacing_y,
203 double alpha)
204{
205 double H = 28; // Height dimension to use with polynomials
206 double x = p[0] * H, y = p[1] * H;
207 double max_y = 3.035 * H;
208 double max_x = 9.0 * H;
209 double flat_region_length = 5.142 * H;
210 double left_hill = 1.929 * H;
211 double right_hill = 7.071 * H;
212 double pos_x_left;
213 double pos_x_right;
214 double pos_y_bottom;
215 double pos_y_top;
216 double pos_y;
217 double y_0;
218 double diff;
219
220 // Gradual spacing and swifting depending on x position if the geometry
221 // is elongated
222 if (alpha > 1)
223 {
224 if (x < max_x / 2)
225 {
226 pos_x_left = x / -max_x + 0.5;
227 x -= pos_x_left * x;
228 }
229 else if (x > max_x / 2 && x < max_x)
230 {
231 pos_x_right = x / max_x - 0.5;
232 x += pos_x_right * (max_x - x);
233 }
234 }
235
236 // Polynomial coefficients :
237 const double a1 = 2.800000000000E+01, b1 = 0.000000000000E+00,
238 c1 = 6.775070969851E-03, d1 = -2.124527775800E-03;
239 const double a2 = 2.507355893131E+01, b2 = 9.754803562315E-01,
240 c2 = -1.016116352781E-01, d2 = 1.889794677828E-03;
241 const double a3 = 2.579601052357E+01, b3 = +8.206693007457E-01,
242 c3 = -9.055370274339E-02, d3 = 1.626510569859E-03;
243 const double a4 = 4.046435022819E+01, b4 = -1.379581654948E+00,
244 c4 = 1.945884504128E-02, d4 = -2.070318932190E-04;
245 const double a5 = 1.792461334664E+01, b5 = +8.743920332081E-01,
246 c5 = -5.567361123058E-02, d5 = 6.277731764683E-04;
247 const double a6 = 5.639011190988E+01, b6 = -2.010520359035E+00,
248 c6 = 1.644919857549E-02, d6 = 2.674976141766E-05;
249
250 double new_x = (max_x - x); // x for the left side of the geometry
251
252 // Gradual spacing and swifting depending on y position
253 if (y < max_y / 2 && (std::abs(spacing_y) > 0))
254 {
255 pos_y_bottom = y / -max_y + 0.5;
256 y -= spacing_y * pos_y_bottom * y;
257 }
258 else if (y > max_y / 2 && y < max_y && (std::abs(spacing_y) > 0))
259 {
260 pos_y_top = y / max_y - 0.5;
261 y += spacing_y * pos_y_top * (max_y - y);
262 }
263
264 pos_y = y / -max_y + 1;
265
266 // Polynomial equations :
267 if (x >= 0 && x < 9)
268 {
269 y += pos_y * (a1 + b1 * x + c1 * std::pow(x, 2) + d1 * std::pow(x, 3));
270
271 // Checking if y is under 28 and correction
272 y_0 = (a1 + b1 * x + c1 * std::pow(x, 2) + d1 * std::pow(x, 3));
273 diff = y_0 - 28.0;
274 if (diff > 0)
275 y -= pos_y * diff;
276 }
277 else if (x >= 9 && x < 14)
278 y += pos_y * (a2 + b2 * x + c2 * std::pow(x, 2) + d2 * std::pow(x, 3));
279 else if (x >= 14 && x < 20)
280 y += pos_y * (a3 + b3 * x + c3 * std::pow(x, 2) + d3 * std::pow(x, 3));
281 else if (x >= 20 && x < 30)
282 y += pos_y * (a4 + b4 * x + c4 * std::pow(x, 2) + d4 * std::pow(x, 3));
283 else if (x >= 30 && x < 40)
284 y += pos_y * (a5 + b5 * x + c5 * std::pow(x, 2) + d5 * std::pow(x, 3));
285 else if (x >= 40 && x < 54)
286 {
287 y += pos_y * (a6 + b6 * x + c6 * std::pow(x, 2) + d6 * std::pow(x, 3));
288 if (y < 0)
289 y = 0;
290 }
291 else if (x <= 252 && x > 243)
292 {
293 y += pos_y * (a1 + b1 * new_x + c1 * std::pow(new_x, 2) +
294 d1 * std::pow(new_x, 3));
295 y_0 = (a1 + b1 * x + c1 * std::pow(x, 2) + d1 * std::pow(x, 3));
296 diff = y_0 - 28.0;
297 if (diff > 0)
298 y -= pos_y * diff;
299 }
300 else if (x <= 243 && x > 238)
301 y += pos_y *
302 (a2 + b2 * new_x + c2 * std::pow(new_x, 2) + d2 * std::pow(new_x, 3));
303 else if (x <= 238 && x > 232)
304 y += pos_y *
305 (a3 + b3 * new_x + c3 * std::pow(new_x, 2) + d3 * std::pow(new_x, 3));
306 else if (x <= 232 && x > 222)
307 y += pos_y *
308 (a4 + b4 * new_x + c4 * std::pow(new_x, 2) + d4 * std::pow(new_x, 3));
309 else if (x <= 222 && x > 212)
310 y += pos_y *
311 (a5 + b5 * new_x + c5 * std::pow(new_x, 2) + d5 * std::pow(new_x, 3));
312 else if (x <= 212 && x > 198)
313 {
314 y += pos_y * (a6 + b6 * new_x + c6 * std::pow(new_x, 2) +
315 d6 * std::pow(new_x, 3));
316 if (y < 0)
317 y = 0;
318 }
319 else
320 y += 0;
321
322 // Elongation of the geometry with the alpha factor
323 // Note : The length of the flat region is always the same length
324 if (x < left_hill)
325 x = alpha * x;
326 else if (x > right_hill)
327 x = alpha * (x - right_hill) + flat_region_length + alpha * left_hill;
328 else
329 x = (x - left_hill) + (alpha * left_hill);
330
331 Point<spacedim> q;
332 q[0] = (x / H);
333 q[1] = (y / H);
334
335 if (spacedim == 3)
336 q[2] = p[2];
337
338 return q;
339}
340
341
342template <int dim, int spacedim>
343void
345 Triangulation<dim, spacedim> &triangulation)
346{
347 std::vector<unsigned int> repetitions(2);
348 repetitions[0] = repetitions_x;
349 repetitions[1] = repetitions_y;
350
351 if (dim == 2)
352 {
353 if (alpha != 1 && (repetitions_x > 1 || repetitions_y > 1))
354 throw std::logic_error(
355 "When alpha parameter is not 1, repetition parameters should be all set to 1");
356
357 GridGenerator::subdivided_hyper_rectangle(triangulation,
358 repetitions,
359 Point<dim>(0.0, 0.0),
360 Point<dim>(9.0, 3.035),
361 true);
362 }
363 else if (dim == 3)
364 {
365 if (alpha != 1 &&
366 (repetitions_x > 1 || repetitions_y > 1 || repetitions_z > 1))
367 throw std::logic_error(
368 "When alpha parameter is not 1, repetition parameters should be all set to 1");
369 repetitions.push_back(repetitions_z);
370 GridGenerator::subdivided_hyper_rectangle(triangulation,
371 repetitions,
372 Point<dim>(0.0, 0.0, 0.0),
373 Point<dim>(9.0, 3.035, 4.5),
374 true);
375 }
376
377 // Transformation of the geometry with the hill geometry
378 GridTools::transform(
379 [this](const Point<spacedim> &p) {
380 return this->hill_geometry(p, spacing_y, alpha);
381 },
382 triangulation);
383
384 // Manifold construction
385 static const FunctionManifold<dim, spacedim, spacedim> manifold_func(
388 triangulation.set_manifold(1, manifold_func);
389 triangulation.set_all_manifold_ids(1);
390}
391
392// Explicit template instantiations
393template class GridPeriodicHills<2, 2>;
394template class GridPeriodicHills<2, 3>;
395template class GridPeriodicHills<3, 3>;
Class that creates a hyper_rectangle and transforms it to obtain the hill geometry with the hill_geom...
GridPeriodicHills(const std::string &grid_arguments)
Constructor for the PeriodicHillsGrid.
static Point< spacedim > hill_geometry(const Point< spacedim > &p, double spacing_y, double alpha)
The hill_geometry function calculates all the domain of the geometry with 6 polynomials depending the...
void make_grid(Triangulation< dim, spacedim > &triangulation)
make_grid. The make_grid function generates a hyper rectangle of the size of the domain and then tran...
virtual double value(const Point< spacedim > &np, const unsigned int component) const override
value. The value function does the same thing than vector_value for one component....
virtual void vector_value(const Point< spacedim > &np, Vector< double > &values) const override
vector_value. This vector_value function is used to construct the geometry manifold....
The push_forward & the pull_back classes create the vector_value functions gradient function and beca...
virtual double value(const Point< spacedim > &p, const unsigned int component) const override
value. The value function does the same thing than vector_value for one component....
virtual void vector_value(const Point< spacedim > &p, Vector< double > &values) const override
vector_value. This function is used to construct the geometry manifold. It changes the original point...