Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
shape_parsing.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2023-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/base/exceptions.h>
7
8#include <algorithm>
9
10template <int dim>
13{
14 using ShapeType = typename Shape<dim>::ShapeType;
15 static const DeprecatedEnumNames<ShapeType> names = {
16 {"hyper rectangle", ShapeType::hyper_rectangle},
17 {"cylindrical tube", ShapeType::cylindrical_tube},
18 {"cylindrical helix", ShapeType::cylindrical_helix},
19 {"cut hollow sphere", ShapeType::cut_hollow_sphere},
20 {"death star", ShapeType::death_star}};
21 return names;
22}
23
24template <int dim>
27{
28 for (const auto &[deprecated_name, shape_type] :
30 if (type == deprecated_name)
31 return shape_type;
33}
34
35template <int dim>
36std::shared_ptr<Shape<dim>>
37ShapeGenerator::initialize_shape(const std::string &type,
38 const std::string &shape_arguments_str,
39 const Point<dim> &position,
40 const Tensor<1, 3> &orientation)
41{
42 using ShapeType = typename Shape<dim>::ShapeType;
43 const ShapeType shape_type = string_to_shape_type<dim>(type);
44
45 std::shared_ptr<Shape<dim>> shape;
46 std::vector<double> shape_arguments;
47 if (shape_type == ShapeType::rbf || shape_type == ShapeType::composite ||
48 shape_type == ShapeType::opencascade)
49 {
50 shape = initialize_shape_from_file(type,
51 shape_arguments_str,
52 position,
53 orientation);
54 }
55 else
56 {
57 std::vector<std::string> shape_arguments_str_list(
58 Utilities::split_string_list(shape_arguments_str, ";"));
59 shape_arguments = Utilities::string_to_double(shape_arguments_str_list);
61 shape_arguments,
62 position,
63 orientation);
64 }
65 return shape;
66}
67
68template <int dim>
69std::shared_ptr<Shape<dim>>
71 const std::string &type,
72 const std::vector<double> &shape_arguments,
73 const Point<dim> &position,
74 const Tensor<1, 3> &orientation)
75{
76 using ShapeType = typename Shape<dim>::ShapeType;
77 const ShapeType shape_type = string_to_shape_type<dim>(type);
78
79 std::shared_ptr<Shape<dim>> shape;
80 if (shape_type == ShapeType::sphere)
81 shape =
82 std::make_shared<Sphere<dim>>(shape_arguments[0], position, orientation);
83 else if (shape_type == ShapeType::hyper_rectangle)
84 {
85 Tensor<1, dim> half_lengths;
86 for (int i = 0; i < dim; ++i)
87 {
88 half_lengths[i] = shape_arguments[i];
89 }
90 shape = std::make_shared<HyperRectangle<dim>>(half_lengths,
91 position,
92 orientation);
93 }
94 else if (shape_type == ShapeType::ellipsoid)
95 {
96 Tensor<1, dim> radii;
97 for (int i = 0; i < dim; ++i)
98 {
99 radii[i] = shape_arguments[i];
100 }
101 shape = std::make_shared<Ellipsoid<dim>>(radii, position, orientation);
102 }
103 else if (shape_type == ShapeType::superquadric)
104 {
105 if constexpr (dim == 3)
106 {
107 Tensor<1, dim> half_lengths{};
108 Tensor<1, dim> exponents{};
109 for (int d = 0; d < dim; d++)
110 {
111 half_lengths[d] = shape_arguments[d];
112 exponents[d] = shape_arguments[d + dim];
113 }
114 double epsilon = 1e-8;
115 if (shape_arguments.size() == 7)
116 {
117 epsilon = shape_arguments[3 + dim];
118 }
119 shape = std::make_shared<Superquadric<dim>>(
120 half_lengths, exponents, epsilon, position, orientation);
121 }
122 }
123 else if (shape_type == ShapeType::torus)
124 {
125 if constexpr (dim == 3)
126 shape = std::make_shared<Torus<dim>>(shape_arguments[0],
127 shape_arguments[1],
128 position,
129 orientation);
130 }
131 else if (shape_type == ShapeType::cone)
132 {
133 if constexpr (dim == 3)
134 shape = std::make_shared<Cone<dim>>(shape_arguments[0],
135 shape_arguments[1],
136 position,
137 orientation);
138 }
139 else if (shape_type == ShapeType::cylinder)
140 {
141 if constexpr (dim == 3)
142 shape = std::make_shared<Cylinder<dim>>(shape_arguments[0],
143 shape_arguments[1],
144 position,
145 orientation);
146 }
147 else if (shape_type == ShapeType::cylindrical_tube)
148 {
149 if constexpr (dim == 3)
150 shape = std::make_shared<CylindricalTube<dim>>(shape_arguments[0],
151 shape_arguments[1],
152 shape_arguments[2],
153 position,
154 orientation);
155 }
156 else if (shape_type == ShapeType::cylindrical_helix)
157 {
158 if constexpr (dim == 3)
159 shape = std::make_shared<CylindricalHelix<dim>>(shape_arguments[0],
160 shape_arguments[1],
161 shape_arguments[2],
162 shape_arguments[3],
163 position,
164 orientation);
165 }
166 else if (shape_type == ShapeType::cut_hollow_sphere)
167 {
168 if constexpr (dim == 3)
169 shape = std::make_shared<CutHollowSphere<dim>>(shape_arguments[0],
170 shape_arguments[1],
171 shape_arguments[2],
172 position,
173 orientation);
174 }
175 else if (shape_type == ShapeType::death_star)
176 {
177 if constexpr (dim == 3)
178 shape = std::make_shared<DeathStar<dim>>(shape_arguments[0],
179 shape_arguments[1],
180 shape_arguments[2],
181 position,
182 orientation);
183 }
184 else if (shape_type == ShapeType::plane)
185 {
186 shape = std::make_shared<Plane<dim>>(position, orientation);
187 }
188 else
189 AssertThrow(false, ExcNotImplemented());
190 return shape;
191}
192
193template <int dim>
194std::shared_ptr<Shape<dim>>
196 const std::string &file_name,
197 const Point<dim> &position,
198 const Tensor<1, 3> &orientation)
199{
200 using ShapeType = typename Shape<dim>::ShapeType;
201 const ShapeType shape_type = string_to_shape_type<dim>(type);
202
203 std::shared_ptr<Shape<dim>> shape;
204 std::vector<double> shape_arguments;
205 if (shape_type == ShapeType::rbf)
206 {
207 shape = std::make_shared<RBFShape<dim>>(file_name, position, orientation);
208 }
209 else if (shape_type == ShapeType::composite)
210 {
211 // The following lines retrieve information regarding a
212 // composite shape.
213 std::map<unsigned int, std::shared_ptr<Shape<dim>>> components;
214 std::map<unsigned int,
215 std::tuple<typename CompositeShape<dim>::BooleanOperation,
216 unsigned int,
217 unsigned int>>
218 operations;
219 // In the file, we first loop over all component shapes, then
220 // we loop over operations
221 std::ifstream myfile(file_name);
222 // Read file line by line for section names or arguments
223 if (myfile)
224 {
225 std::string line;
226 std::vector<std::string> column_names;
227 std::vector<double> line_of_data;
228 bool parsing_shapes = false;
229 bool parsing_operations = false;
230 // comment marker
231 std::string marker = "#";
232 while (std::getline(myfile, line))
233 {
234 // Ignore comments in the line
235 size_t markerPos = line.find(marker);
236 if (markerPos != std::string::npos)
237 {
238 line = line.substr(0, markerPos);
239 }
240 if (line == "")
241 continue;
242 if (line == "shapes")
243 {
244 parsing_shapes = true;
245 parsing_operations = false;
246 }
247 else if (line == "operations")
248 {
249 parsing_shapes = false;
250 parsing_operations = true;
251 }
252 else
253 {
254 std::vector<std::string> list_of_words_base =
255 Utilities::split_string_list(line, ";");
256 std::vector<std::string> list_of_words_clean;
257 for (const auto &word : list_of_words_base)
258 {
259 if (word != "")
260 list_of_words_clean.emplace_back(word);
261 }
262 if (parsing_shapes)
263 {
264 unsigned int identifier = stoi(list_of_words_clean[0]);
265 const std::string &type_str = list_of_words_clean[1];
266 std::string arguments_str = list_of_words_clean[2];
267 std::ranges::replace(arguments_str, ':', ';');
268 const std::string &position_str = list_of_words_clean[3];
269 const std::string &orientation_str =
270 list_of_words_clean[4];
271
272 std::vector<std::string> position_str_component =
273 Utilities::split_string_list(position_str, ":");
274 std::vector<std::string> orientation_str_component =
275 Utilities::split_string_list(orientation_str, ":");
276
277 std::vector<double> temp_position_vec =
278 Utilities::string_to_double(position_str_component);
279 std::vector<double> temp_orientation_vec =
280 Utilities::string_to_double(orientation_str_component);
281
282 Point<dim> temp_position;
283 Point<3> temp_orientation =
284 Point<3>({temp_orientation_vec[0],
285 temp_orientation_vec[1],
286 temp_orientation_vec[2]});
287 temp_position[0] = temp_position_vec[0];
288 temp_position[1] = temp_position_vec[1];
289 if constexpr (dim == 3)
290 temp_position[2] = temp_position_vec[2];
291
292 std::shared_ptr<Shape<dim>> shape_temp;
294 type_str, arguments_str, Point<dim>(), Tensor<1, 3>());
295 shape_temp->set_position(temp_position);
296 shape_temp->set_orientation(temp_orientation);
297 components[identifier] = shape_temp->static_copy();
298 }
299 else if (parsing_operations)
300 {
301 unsigned int identifier = stoi(list_of_words_clean[0]);
302 const std::string &type_str = list_of_words_clean[1];
303 const std::string &arguments_str = list_of_words_clean[2];
304 std::vector<std::string> arguments_str_component =
305 Utilities::split_string_list(arguments_str, ":");
306
307 unsigned int first_shape =
308 stoi(arguments_str_component[0]);
309 unsigned int second_shape =
310 stoi(arguments_str_component[1]);
311 if (type_str == "union")
312 {
313 operations[identifier] = std::make_tuple(
315 first_shape,
316 second_shape);
317 }
318 else if (type_str == "difference")
319 {
320 operations[identifier] = std::make_tuple(
322 first_shape,
323 second_shape);
324 }
325 else if (type_str == "intersection")
326 {
327 operations[identifier] = std::make_tuple(
329 first_shape,
330 second_shape);
331 }
332 }
333 }
334 }
335 myfile.close();
336 shape = std::make_shared<CompositeShape<dim>>(components,
337 operations,
338 position,
339 orientation);
340 }
341 else
342 throw std::invalid_argument(file_name);
343 }
344 else if (shape_type == ShapeType::opencascade)
345 {
346 shape = std::make_shared<OpenCascadeShape<dim>>(file_name,
347 position,
348 orientation);
349 }
350 return shape;
351}
352
357template Shape<2>::ShapeType
358ShapeGenerator::string_to_shape_type<2>(const std::string &type);
359template Shape<3>::ShapeType
360ShapeGenerator::string_to_shape_type<3>(const std::string &type);
361template std::shared_ptr<Shape<2>>
362ShapeGenerator::initialize_shape(const std::string &type,
363 const std::string &arguments,
364 const Point<2> &position,
365 const Tensor<1, 3> &orientation);
366template std::shared_ptr<Shape<3>>
367ShapeGenerator::initialize_shape(const std::string &type,
368 const std::string &arguments,
369 const Point<3> &position,
370 const Tensor<1, 3> &orientation);
371template std::shared_ptr<Shape<2>>
373 const std::string &type,
374 const std::vector<double> &shape_arguments,
375 const Point<2> &position,
376 const Tensor<1, 3> &orientation);
377template std::shared_ptr<Shape<3>>
379 const std::string &type,
380 const std::vector<double> &shape_arguments,
381 const Point<3> &position,
382 const Tensor<1, 3> &orientation);
383template std::shared_ptr<Shape<2>>
384ShapeGenerator::initialize_shape_from_file(const std::string &type,
385 const std::string &file_name,
386 const Point<2> &position,
387 const Tensor<1, 3> &orientation);
388template std::shared_ptr<Shape<3>>
389ShapeGenerator::initialize_shape_from_file(const std::string &type,
390 const std::string &file_name,
391 const Point<3> &position,
392 const Tensor<1, 3> &orientation);
Definition shape.h:1896
ShapeType
enum class that associate an integer index tp each type of shape. The enumerator names are the values...
Definition shape.h:61
std::vector< std::pair< std::string, EnumType > > DeprecatedEnumNames
Deprecated strings of an enum parameter, each paired with the enumerator it stands for....
Definition utilities.h:765
EnumType string_to_enum(const std::string &name)
Return the enumerator whose identifier is name, e.g. to parse the value of a parameter entry into its...
Definition utilities.h:734
Shape< dim >::ShapeType string_to_shape_type(const std::string &type)
Return the shape type designated by a string, which is either the name of a Shape<dim>::ShapeType enu...
std::shared_ptr< Shape< dim > > initialize_shape_from_vector(const std::string &type, const std::vector< double > &shape_arguments, const Point< dim > &position, const Tensor< 1, 3 > &orientation)
const DeprecatedEnumNames< typename Shape< dim >::ShapeType > & deprecated_shape_type_names()
Return the deprecated strings of the shape types, which remain accepted in parameter files,...
std::shared_ptr< Shape< dim > > initialize_shape(const std::string &type, const std::string &shape_arguments_str, const Point< dim > &position, const Tensor< 1, 3 > &orientation)
std::shared_ptr< Shape< dim > > initialize_shape_from_file(const std::string &type, const std::string &file_name, const Point< dim > &position, const Tensor< 1, 3 > &orientation)