https://mooseframework.inl.gov
Loading...
Searching...
No Matches
AdvancedExtruderGenerator.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
11#include "MooseUtils.h"
12#include "MooseMeshUtils.h"
13#include "MathUtils.h"
14#include "GeometryUtils.h"
15
16#include "libmesh/boundary_info.h"
17#include "libmesh/function_base.h"
18#include "libmesh/cell_prism6.h"
19#include "libmesh/cell_prism18.h"
20#include "libmesh/cell_prism21.h"
21#include "libmesh/cell_hex8.h"
22#include "libmesh/cell_hex20.h"
23#include "libmesh/cell_hex27.h"
24#include "libmesh/cell_c0polyhedron.h"
25#include "libmesh/edge_edge2.h"
26#include "libmesh/edge_edge3.h"
27#include "libmesh/edge_edge4.h"
28#include "libmesh/face_quad4.h"
29#include "libmesh/face_quad8.h"
30#include "libmesh/face_quad9.h"
31#include "libmesh/face_tri3.h"
32#include "libmesh/face_tri6.h"
33#include "libmesh/face_tri7.h"
34#include "libmesh/face_c0polygon.h"
35#include "libmesh/libmesh_logging.h"
36#include "libmesh/mesh_communication.h"
37#include "libmesh/mesh_modification.h"
38#include "libmesh/mesh_serializer.h"
39#include "libmesh/mesh_tools.h"
40#include "libmesh/parallel.h"
41#include "libmesh/remote_elem.h"
42#include "libmesh/string_to_enum.h"
43#include "libmesh/unstructured_mesh.h"
44#include "libmesh/point.h"
45#include "libmesh/fe_base.h"
46#include "libmesh/quadrature_nodal.h"
47
48#include <numeric>
49#include <cmath>
50
53 FancyExtruderGenerator,
54 "02/18/2023 24:00",
56
59{
61
63 "Extrudes a 1D mesh into 2D, or a 2D mesh into 3D, and supports a variable height for each "
64 "elevation, variable number of layers within each elevation, variable growth factors of "
65 "axial element sizes within each elevation and remap subdomain_ids, boundary_ids and element "
66 "extra integers within each elevation as well as interface boundaries between neighboring "
67 "elevation layers, as well as following a 1D curve and modifying the radial (normal to "
68 "the extrusion axis) extent of the geometry. It can also extrude each node along the "
69 "averaged normal of the elements it is connected to, which is useful to create boundary "
70 "layers.");
71
72 params.addRequiredParam<MeshGeneratorName>("input", "The mesh to extrude");
73
74 // User input of extrusion heights / axial discretization
75 params.addParam<std::vector<Real>>("heights", {}, "The height of each elevation");
76 params.addRangeCheckedParam<std::vector<Real>>(
77 "biases", "biases>0.0", "The axial growth factor used for mesh biasing for each elevation.");
78 params.addParam<std::vector<unsigned int>>(
79 "num_layers",
80 {},
81 "The number of layers for each elevation - must be num_elevations in length!");
82
83 // Swaps on every height
84 params.addParam<std::vector<std::vector<SubdomainName>>>(
85 "subdomain_swaps",
86 {},
87 "For each row, every two entries are interpreted as a pair of "
88 "'from' and 'to' to remap the subdomains for that elevation. Subdomains may be specified by "
89 "name or ID. A 'to' name absent from the input mesh creates a new subdomain");
90 params.addParam<std::vector<std::vector<boundary_id_type>>>(
91 "boundary_swaps",
92 {},
93 "For each row, every two entries are interpreted as a pair of "
94 "'from' and 'to' to remap the boundaries for that elevation");
95 params.addParam<std::vector<std::string>>(
96 "elem_integer_names_to_swap",
97 {},
98 "Array of element extra integer names that need to be swapped during extrusion.");
99 params.addParam<std::vector<std::vector<std::vector<dof_id_type>>>>(
100 "elem_integers_swaps",
101 {},
102 "For each row, every two entries are interpreted as a pair of 'from' and 'to' to remap the "
103 "element extra integer for that elevation. If multiple element extra integers need to be "
104 "swapped, the enties are stacked based on the order provided in "
105 "'elem_integer_names_to_swap' to form the third dimension.");
106
107 // Direction parameter
108 params.addParam<Point>("direction",
109 "A vector that points in the direction to extrude (note, this will be "
110 "normalized internally - so don't worry about it here)");
111 params.addParam<MeshGeneratorName>(
112 "extrusion_curve",
113 "Name of the mesh generator providing the line mesh curve to be extruded along. The "
114 "extrusion path follows the node order in the line mesh");
115 params.addParam<RealVectorValue>(
116 "start_extrusion_direction",
117 "A vector that points in the starting direction for extruding along a curve. This vector "
118 "should be the tangent vector at the FIRST node of the extrusion curve. Vector will be "
119 "normalized in code, so don't worry about it here.");
120 params.addParam<RealVectorValue>(
121 "end_extrusion_direction",
122 "A vector that points in the ending direction for extruding along a curve. This vector "
123 "should be the tangent vector at the LAST node of the extrusion curve. Vector will be "
124 "normalized in code, so don't worry about it here.");
125 params.addParam<bool>(
126 "extrude_along_node_normals",
127 false,
128 "If true, each node is extruded along the averaged normal of the elements it is connected "
129 "to, instead of along a fixed 'direction'. Only supported when extruding a 1D mesh in a "
130 "plane of constant z into 2D, or a 2D surface mesh into 3D. This is useful to create "
131 "boundary layers.");
132
133 // Boundaries and interfaces
134 params.addParam<BoundaryName>(
135 "top_boundary",
136 "The boundary name to set on the top boundary. If omitted an ID will be generated.");
137 params.addParam<BoundaryName>(
138 "bottom_boundary",
139 "The boundary name to set on the bottom boundary. If omitted an ID will be generated.");
140 params.addParam<BoundaryName>(
141 "side_boundary",
142 "The boundary name to add the extruded exterior sides of the input mesh, and their nodes, "
143 "to. If omitted, no side boundary is added.");
144 params.addParam<std::vector<std::vector<subdomain_id_type>>>(
145 "upward_boundary_source_blocks", "Block ids used to generate upward interface boundaries.");
146 params.addParam<std::vector<std::vector<boundary_id_type>>>("upward_boundary_ids",
147 "Upward interface boundary ids.");
148 params.addParam<std::vector<std::vector<subdomain_id_type>>>(
149 "downward_boundary_source_blocks",
150 "Block ids used to generate downward interface boundaries.");
151 params.addParam<std::vector<std::vector<boundary_id_type>>>("downward_boundary_ids",
152 "Downward interface boundary ids.");
153
154 // Radial transformations
155 params.addParam<Real>("twist_pitch",
156 0,
157 "Pitch for helicoidal extrusion around an axis going through the origin "
158 "following the direction vector");
159 params.addParam<Real>("end_radial_extent",
160 0,
161 "If modifying the radial extent of the extruded geometry, final radial "
162 "extent to reach at the end of the extrusion process");
163 MooseEnum radial_growth_methods("linear cubic", "linear");
164 params.addParam<MooseEnum>("radial_growth_method",
165 radial_growth_methods,
166 "Functional form to change radius while extruding along curve.");
167 params.addParam<Real>("start_radial_growth_rate", 1., "Starting rate of radial expansion.");
168 params.addParam<Real>("end_radial_growth_rate", 1., "Ending rate of radial expansion.");
169
170 params.addParamNamesToGroup("top_boundary bottom_boundary side_boundary "
171 "upward_boundary_source_blocks upward_boundary_ids "
172 "downward_boundary_source_blocks downward_boundary_ids",
173 "Boundary Assignment");
175 "subdomain_swaps boundary_swaps elem_integer_names_to_swap elem_integers_swaps", "ID Swap");
176 params.addParamNamesToGroup("extrusion_curve start_extrusion_direction end_extrusion_direction",
177 "Extrusion along curve");
178 params.addParamNamesToGroup("twist_pitch end_radial_extent radial_growth_method "
179 "start_radial_growth_rate end_radial_growth_rate",
180 "Radial transformation");
181
182 return params;
183}
184
186 : MeshGenerator(parameters),
187 _input(getMesh("input")),
188 _heights(getParam<std::vector<Real>>("heights")),
189 _biases(isParamValid("biases") ? getParam<std::vector<Real>>("biases")
190 : std::vector<Real>(_heights.size(), 1.0)),
191 _num_layers(getParam<std::vector<unsigned int>>("num_layers")),
192 _subdomain_swaps(getParam<std::vector<std::vector<SubdomainName>>>("subdomain_swaps")),
193 _boundary_swaps(getParam<std::vector<std::vector<boundary_id_type>>>("boundary_swaps")),
194 _elem_integer_names_to_swap(getParam<std::vector<std::string>>("elem_integer_names_to_swap")),
195 _elem_integers_swaps(
196 getParam<std::vector<std::vector<std::vector<dof_id_type>>>>("elem_integers_swaps")),
197 _direction(isParamValid("direction") ? getParam<Point>("direction") : Point(0, 0, 0)),
198 _extrusion_curve(getMesh("extrusion_curve", true)),
199 _start_extrusion_direction(isParamValid("start_extrusion_direction")
200 ? getParam<RealVectorValue>("start_extrusion_direction").unit()
201 : Point(0, 0, 0)),
202 _end_extrusion_direction(isParamValid("end_extrusion_direction")
203 ? getParam<RealVectorValue>("end_extrusion_direction").unit()
204 : Point(0, 0, 0)),
205 _extrude_along_curve(isParamValid("extrusion_curve")),
206 _extrude_along_node_normals(getParam<bool>("extrude_along_node_normals")),
207 _has_top_boundary(isParamValid("top_boundary")),
208 _top_boundary(isParamValid("top_boundary") ? getParam<BoundaryName>("top_boundary") : "0"),
209 _has_bottom_boundary(isParamValid("bottom_boundary")),
210 _bottom_boundary(isParamValid("bottom_boundary") ? getParam<BoundaryName>("bottom_boundary")
211 : "0"),
212 _has_side_boundary(isParamValid("side_boundary")),
213 _side_boundary(isParamValid("side_boundary") ? getParam<BoundaryName>("side_boundary") : ""),
214 _upward_boundary_source_blocks(
215 isParamValid("upward_boundary_source_blocks")
216 ? getParam<std::vector<std::vector<subdomain_id_type>>>("upward_boundary_source_blocks")
217 : std::vector<std::vector<subdomain_id_type>>(_heights.size(),
218 std::vector<subdomain_id_type>())),
219 _upward_boundary_ids(
220 isParamValid("upward_boundary_ids")
221 ? getParam<std::vector<std::vector<boundary_id_type>>>("upward_boundary_ids")
222 : std::vector<std::vector<boundary_id_type>>(_heights.size(),
223 std::vector<boundary_id_type>())),
224 _downward_boundary_source_blocks(isParamValid("downward_boundary_source_blocks")
225 ? getParam<std::vector<std::vector<subdomain_id_type>>>(
226 "downward_boundary_source_blocks")
227 : std::vector<std::vector<subdomain_id_type>>(
228 _heights.size(), std::vector<subdomain_id_type>())),
229 _downward_boundary_ids(
230 isParamValid("downward_boundary_ids")
231 ? getParam<std::vector<std::vector<boundary_id_type>>>("downward_boundary_ids")
232 : std::vector<std::vector<boundary_id_type>>(_heights.size(),
233 std::vector<boundary_id_type>())),
234 _twist_pitch(getParam<Real>("twist_pitch")),
235 _end_radial_extent(getParam<Real>("end_radial_extent")),
236 _radial_expansion_method(getParam<MooseEnum>("radial_growth_method")),
237 _start_radial_growth_rate(getParam<Real>("start_radial_growth_rate")),
238 _end_radial_growth_rate(getParam<Real>("end_radial_growth_rate"))
239{
241 {
242 if (isParamSetByUser("heights"))
243 paramError("heights", "heights cannot be set if extruding along curve!");
244 if (isParamValid("biases"))
245 paramError("biases", "biases cannot be set if extruding along curve!");
246 if (isParamSetByUser("num_layers"))
247 paramError("num_layers", "num_layers cannot be set if extruding along curve!");
248 if (isParamValid("direction"))
249 paramError("direction", "direction cannot be set if extruding along curve!");
251 paramError("extrude_along_node_normals",
252 "extrude_along_node_normals cannot be set if extruding along curve!");
253 }
255 {
256 if (isParamValid("direction"))
257 paramError("direction",
258 "direction cannot be set if 'extrude_along_node_normals' is set, as the extrusion "
259 "direction is computed from the element normals!");
260 if (!MooseUtils::absoluteFuzzyEqual(_twist_pitch, 0.))
261 paramError("twist_pitch",
262 "twist_pitch cannot be set if 'extrude_along_node_normals' is set!");
264 paramError("end_radial_extent",
265 "end_radial_extent cannot be set if 'extrude_along_node_normals' is set!");
266 }
267 else
268 {
269 if (!_direction.norm())
270 paramError("direction", "Must have some length!");
271 _direction /= _direction.norm();
272 }
273
274 unsigned int num_elevations;
276 num_elevations = 1;
277 else
278 {
279 num_elevations = _heights.size();
280 if (_num_layers.size() != num_elevations)
282 "heights", "The length of 'heights' and 'num_layers' must be the same in ", name());
283 }
284
285 if (_subdomain_swaps.size() && (_subdomain_swaps.size() != num_elevations))
286 {
288 paramError("subdomain_swaps",
289 "If specified, 'subdomain_swaps' (" + std::to_string(_subdomain_swaps.size()) +
290 ") must be equal to 1 when extruding along a curve.");
291 else
292 {
293 paramError("subdomain_swaps",
294 "If specified, 'subdomain_swaps' (" + std::to_string(_subdomain_swaps.size()) +
295 ") must be the same length as 'heights' (" + std::to_string(num_elevations) +
296 ") in ",
297 name());
298 }
299 }
300
301 if (_boundary_swaps.size() && (_boundary_swaps.size() != num_elevations))
302 {
304 {
305 paramError("boundary_swaps",
306 "If specified, 'boundary_swaps' (" + std::to_string(_boundary_swaps.size()) +
307 ") must be the same length as 'heights' (" + std::to_string(num_elevations) +
308 ") in ",
309 name());
310 }
311 else
312 {
313 paramError("boundary_swaps",
314 "If specified, 'boundary_swaps' (" + std::to_string(_boundary_swaps.size()) +
315 ") must be equal to 1 when extruding along a curve.");
316 }
317 }
318
319 try
320 {
322 name(), "boundary_swaps", _boundary_swaps, _boundary_swap_pairs);
323 }
324 catch (const MooseException & e)
325 {
326 paramError("boundary_swaps", e.what());
327 }
328
329 if (_elem_integers_swaps.size() &&
331 paramError("elem_integers_swaps",
332 "If specified, 'elem_integers_swaps' must have the same length as the length of "
333 "'elem_integer_names_to_swap'.");
334
335 for (const auto & unit_elem_integers_swaps : _elem_integers_swaps)
336 if (unit_elem_integers_swaps.size() != num_elevations)
337 paramError("elem_integers_swaps",
338 "If specified, each element of 'elem_integers_swaps' must have the same length as "
339 "the length of 'heights'.");
340
341 try
342 {
344 num_elevations,
348 }
349 catch (const MooseException & e)
350 {
351 paramError("elem_integers_swaps", e.what());
352 }
353
355 {
356 bool has_negative_entry = false;
357 bool has_positive_entry = false;
358 for (const auto & h : _heights)
359 {
360 if (h > 0.0)
361 has_positive_entry = true;
362 else
363 has_negative_entry = true;
364 }
365
366 if (has_negative_entry && has_positive_entry)
367 paramError("heights", "Cannot have both positive and negative heights!");
368 if (_biases.size() != _heights.size())
369 paramError("biases", "Size of this parameter, if provided, must be the same as heights.");
370 }
371
374 _upward_boundary_ids.size() != num_elevations))
375 {
378 "upward_boundary_ids",
379 "This parameter must have the same length (" +
380 std::to_string(_upward_boundary_ids.size()) + ") as upward_boundary_source_blocks (" +
381 std::to_string(_upward_boundary_source_blocks.size()) + ") and num_elevations (" +
382 std::to_string(num_elevations) +
383 "). Note that the number of heights is set to 1 when extruding along a curve.");
384 else
385 {
386 paramError("upward_boundary_ids",
387 "This parameter must have the same length (" +
388 std::to_string(_upward_boundary_ids.size()) +
389 ") as upward_boundary_source_blocks (" +
390 std::to_string(_upward_boundary_source_blocks.size()) + ") and heights (" +
391 std::to_string(num_elevations) + ")");
392 }
393 }
394
395 for (unsigned int i = 0; i < _upward_boundary_source_blocks.size(); i++)
396 if (_upward_boundary_source_blocks[i].size() != _upward_boundary_ids[i].size())
397 paramError("upward_boundary_ids",
398 "Every element of this parameter must have the same length as the corresponding "
399 "element of upward_boundary_source_blocks.");
400
403 _downward_boundary_ids.size() != num_elevations))
404 {
407 "downward_boundary_ids",
408 "This parameter must have the same length (" +
409 std::to_string(_downward_boundary_ids.size()) +
410 ") as downward_boundary_source_blocks (" +
411 std::to_string(_downward_boundary_source_blocks.size()) + ") and (" +
412 std::to_string(num_elevations) +
413 "). Note that the number of heights is set to 1 when extruding along a curve.");
414 else
415 {
416 paramError("downward_boundary_ids",
417 "This parameter must have the same length (" +
418 std::to_string(_downward_boundary_ids.size()) +
419 ") as downward_boundary_source_blocks (" +
420 std::to_string(_downward_boundary_source_blocks.size()) + ") and heights (" +
421 std::to_string(num_elevations) + ")");
422 }
423 }
424 for (unsigned int i = 0; i < _downward_boundary_source_blocks.size(); i++)
426 paramError("downward_boundary_ids",
427 "Every element of this parameter must have the same length as the corresponding "
428 "element of downward_boundary_source_blocks.");
429}
430
431std::unique_ptr<MeshBase>
433{
434 // Note: bulk of this code originally from libmesh mesh_modification.C
435 // Original copyright: Copyright (C) 2002-2019 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
436 // Original license is LGPL so it can be used here.
437
438 auto mesh = buildMeshBaseObject(_input->mesh_dimension() + 1);
439 mesh->set_mesh_dimension(_input->mesh_dimension() + 1);
440
441 // Check if the element integer names are existent in the input mesh.
442 for (const auto i : make_range(_elem_integer_names_to_swap.size()))
443 if (_input->has_elem_integer(_elem_integer_names_to_swap[i]))
445 _input->get_elem_integer_index(_elem_integer_names_to_swap[i]));
446 else
447 paramError("elem_integer_names_to_swap",
448 "Element ",
449 i + 1,
450 " of 'elem_integer_names_to_swap' in is not a valid extra element integer of the "
451 "input mesh.");
452
453 // prepare for transferring extra element integers from original mesh to the extruded mesh.
454 const unsigned int num_extra_elem_integers = _input->n_elem_integers();
455 std::vector<std::string> id_names;
456
457 for (unsigned int i = 0; i < num_extra_elem_integers; i++)
458 {
459 id_names.push_back(_input->get_elem_integer_name(i));
460 if (!mesh->has_elem_integer(id_names[i]))
461 mesh->add_elem_integer(id_names[i]);
462 }
463
464 // retrieve subdomain/sideset/nodeset name maps
465 if (!_input->preparation().has_cached_elem_data)
466 _input->cache_elem_data();
467 const auto & input_subdomain_map = _input->get_subdomain_name_map();
468 const auto & input_sideset_map = _input->get_boundary_info().get_sideset_name_map();
469 const auto & input_nodeset_map = _input->get_boundary_info().get_nodeset_name_map();
470
471 // Check that the swaps source blocks are present in the mesh
472 for (const auto & swap : _subdomain_swaps)
473 for (const auto i : index_range(swap))
474 if (i % 2 == 0 && !MooseMeshUtils::hasSubdomainName(*_input, swap[i]))
475 {
476 std::string blocks;
477 for (const auto & [name, id] : MooseMeshUtils::getAllSubdomainNamesAndIDs(*_input))
478 blocks += (blocks.empty() ? "" : ", ") + name + " (" + std::to_string(id) + ")";
479 paramError("subdomain_swaps",
480 "The block '",
481 swap[i],
482 "' was not found within the mesh.\nBlock names (ID) in the mesh: " + blocks);
483 }
484
485 // Convert the subdomain swaps from names to IDs. Target subdomains that are neither IDs nor
486 // names of subdomains in the input mesh are assigned new IDs, unused by the input mesh and by
487 // any other target ID
488 subdomain_id_type next_free_subdomain_id = MooseMeshUtils::getNextFreeSubdomainID(*_input);
489 for (const auto & swap : _subdomain_swaps)
490 for (const auto i : index_range(swap))
491 if (i % 2 == 1 && MooseUtils::isDigits(swap[i]))
492 next_free_subdomain_id = std::max(
493 next_free_subdomain_id,
494 cast_int<subdomain_id_type>(MooseMeshUtils::getSubdomainID(swap[i], *_input) + 1));
495 std::map<SubdomainName, subdomain_id_type> new_subdomain_ids;
496 std::vector<std::vector<subdomain_id_type>> subdomain_swap_ids(_subdomain_swaps.size());
497 for (const auto row : index_range(_subdomain_swaps))
498 for (const auto i : index_range(_subdomain_swaps[row]))
499 {
500 const auto & subdomain = _subdomain_swaps[row][i];
501 if (i % 2 == 0 || MooseUtils::isDigits(subdomain) ||
503 subdomain_swap_ids[row].push_back(MooseMeshUtils::getSubdomainID(subdomain, *_input));
504 else
505 {
506 if (!new_subdomain_ids.count(subdomain))
507 new_subdomain_ids[subdomain] = next_free_subdomain_id++;
508 subdomain_swap_ids[row].push_back(new_subdomain_ids[subdomain]);
509 }
510 }
511
512 try
513 {
515 name(), "subdomain_swaps", subdomain_swap_ids, _subdomain_swap_pairs);
516 }
517 catch (const MooseException & e)
518 {
519 paramError("subdomain_swaps", e.what());
520 }
521
522 // Check that the swaps source boundaries are present in the mesh
523 for (const auto & swap : _boundary_swaps)
524 for (const auto i : index_range(swap))
525 if (i % 2 == 0 && !MooseMeshUtils::hasBoundaryID(*_input, swap[i]))
526 paramError("boundary_swaps",
527 "The boundary '",
528 swap[i],
529 "' was not found within the mesh.\nBoundaries in the mesh: " +
531
532 // Check that the source blocks for layer top/bottom boundaries exist in the mesh
533 for (const auto & layer_vec : _upward_boundary_source_blocks)
534 for (const auto bid : layer_vec)
537 "upward_boundary_source_blocks", "The block '", bid, "' was not found within the mesh");
538 for (const auto & layer_vec : _downward_boundary_source_blocks)
539 for (const auto bid : layer_vec)
541 paramError("downward_boundary_source_blocks",
542 "The block '",
543 bid,
544 "' was not found within the mesh");
545
546 // Move the meshes as requested by the mesh generator system
547 std::unique_ptr<MeshBase> input = std::move(_input);
548 std::unique_ptr<MeshBase> extrusion_curve;
550 extrusion_curve = std::move(_extrusion_curve);
551
552 if (_extrude_along_node_normals && !input->is_serial())
553 paramError("extrude_along_node_normals",
554 "Extruding along node normals is not implemented for non-serialized distributed "
555 "meshes yet.");
556
557 // We must serialize the curve to know how to extrude across all ranks
558 std::unique_ptr<libMesh::MeshSerializer> serializer;
560 serializer = std::make_unique<libMesh::MeshSerializer>(*extrusion_curve);
561
562 // Exterior sides of the input mesh are identified by their lack of neighbor
563 if (_has_side_boundary && !input->preparation().has_neighbor_ptrs)
564 input->find_neighbors();
565
566 // If we're using a distributed mesh... then make sure we don't have any remote elements hanging
567 // around
568 if (!input->is_serial())
569 {
570 input->delete_remote_elements();
571 // This should be a no-op, and yet, it's needed for two THM tests
572 mesh->delete_remote_elements();
573 }
574
575 if (input->n_nodes() != input->max_node_id())
576 input->renumber_nodes_and_elements();
577
578 if (input->n_nodes() != input->max_node_id())
580 "You must allow renumbering, because the extruded mesh should be contiguously numbered. "
581 "Alternatively, you can use a separate mesh generator (MeshRepairGenerator with the "
582 "renumber_contiguously parameter for example) to renumber the nodes contiguously.");
583
584 unsigned int total_num_layers;
585 unsigned int total_num_elevations;
587 {
588 total_num_layers = std::accumulate(_num_layers.begin(), _num_layers.end(), 0);
589 total_num_elevations = _heights.size();
590 }
591 else
592 {
593 total_num_layers = extrusion_curve->n_elem();
594 total_num_elevations = 1;
595 }
596
597 dof_id_type orig_elem = input->n_elem();
598 dof_id_type orig_nodes = input->n_nodes();
599
600#ifdef LIBMESH_ENABLE_UNIQUE_ID
601 unique_id_type orig_unique_ids = input->parallel_max_unique_id();
602 bool has_poly_midnodes = false;
603#endif
604
605 bool has_polygons = false;
606 unsigned int order = 1;
607
608 BoundaryInfo & boundary_info = mesh->get_boundary_info();
609 const BoundaryInfo & input_boundary_info = input->get_boundary_info();
610
611 // Determine boundary IDs for the new user provided boundary names
612 std::vector<BoundaryName> new_boundary_names;
614 new_boundary_names.push_back(_bottom_boundary);
616 new_boundary_names.push_back(_top_boundary);
618 new_boundary_names.push_back(_side_boundary);
619 std::vector<boundary_id_type> new_boundary_ids =
620 MooseMeshUtils::getBoundaryIDs(*input, new_boundary_names, true);
621 // getBoundaryIDs generates a distinct ID for each unknown name, even a repeated one, so make
622 // boundaries given the same name share an ID
623 for (const auto i : index_range(new_boundary_names))
624 for (const auto j : make_range(i))
625 if (new_boundary_names[i] == new_boundary_names[j])
626 new_boundary_ids[i] = new_boundary_ids[j];
627 const auto user_bottom_boundary_id =
628 _has_bottom_boundary ? new_boundary_ids.front() : libMesh::BoundaryInfo::invalid_id;
629 const auto user_top_boundary_id = _has_top_boundary
630 ? new_boundary_ids[_has_bottom_boundary ? 1 : 0]
632 const auto user_side_boundary_id =
633 _has_side_boundary ? new_boundary_ids.back() : libMesh::BoundaryInfo::invalid_id;
634
635 // We know a priori how many elements we'll need
636 mesh->reserve_elem(total_num_layers * orig_elem);
637
638 // Look for higher order elements which introduce an extra layer
639 std::set<ElemType> higher_orders = {EDGE3, EDGE4, TRI6, TRI7, QUAD8, QUAD9};
640 bool extruding_quad_eights = false;
641 std::vector<ElemType> types;
642 MeshTools::elem_types(*input, types);
643 for (const auto elem_type : types)
644 {
645 if (higher_orders.count(elem_type))
646 order = 2;
647 if (elem_type == QUAD8)
648 extruding_quad_eights = true;
649 }
650 mesh->comm().max(order);
651 mesh->comm().max(extruding_quad_eights);
652
653 // Reserve for the max number possibly needed
654 mesh->reserve_nodes((order * total_num_layers + 1) * orig_nodes);
655
656 // Container to catch the boundary IDs handed back by the BoundaryInfo object
657 std::vector<boundary_id_type> ids_to_copy;
658
659 // We need to compute the distance to the axis
660 Real start_radial_extent = 0;
661 Point reference_point;
663 reference_point = *(extrusion_curve->node_ptr(0));
664 else if (!MooseUtils::absoluteFuzzyEqual(_twist_pitch, 0.))
665 // Backwards compatibility: we were twisting along an axis going through the origin
666 reference_point = Point(0, 0, 0);
667 else
668 reference_point = MooseMeshUtils::meshCentroidCalculator(*input);
669
671 {
672 // the center is at the curve if we are extruding along a curve, and the centroid of the mesh
673 // otherwise
674 RealVectorValue reference_direction;
676 reference_direction =
679 : RealVectorValue(
680 (*(extrusion_curve->node_ptr(1)) - *(extrusion_curve->node_ptr(0))).unit());
681 else
682 reference_direction = _direction;
683
684 // now we measure the initial radial extent
685 start_radial_extent =
686 MooseMeshUtils::computeMaxDistanceToAxis(*input, reference_point, reference_direction);
687 }
688
689 // Compute the total extrusion distance along the axis
690 Real total_extrusion_distance_at_axis;
692 {
693 if (!extrusion_curve->is_prepared())
694 extrusion_curve->prepare_for_use();
695 total_extrusion_distance_at_axis = MeshTools::volume(*extrusion_curve);
696 }
697 else
698 total_extrusion_distance_at_axis = std::accumulate(_heights.begin(), _heights.end(), 0);
699
700 // When extruding along node normals, precompute each node's extrusion direction from the
701 // normals of the elements connected to it
702 std::unordered_map<dof_id_type, Point> node_normals;
704 node_normals = computeNodeNormals(*input);
705
706 // Create translated layers of nodes in the direction of extrusion
707 for (const auto & node : input->node_ptr_range())
708 {
709 unsigned int current_node_layer = 0;
710 Point orig_node_to_previous;
711 Point orig_node_to_current;
712 Real sum_step_sizes = 0.;
713 Real sum_step_sizes_at_axis = 0.;
714 // Initial distance from the node to the extrusion axis
715 Real start_node_radius = (*node - reference_point).norm();
716
717 // Direction to extrude this node along: a fixed direction, or the averaged normal of the
718 // elements connected to it
719 Point node_extrude_direction = _direction;
721 {
722 auto it = node_normals.find(node->id());
723 if (it == node_normals.end())
724 mooseError("Node ",
725 node->id(),
726 " is not connected to any element with a well-defined normal; its extrusion "
727 "direction cannot be determined.");
728 node_extrude_direction = it->second;
729 }
730
731 // e is the elevation layer ordering
732 for (const auto e : make_range(total_num_elevations))
733 {
734 unsigned int num_layers = libMesh::invalid_uint;
735 Real height = std::numeric_limits<Real>::max(), bias = std::numeric_limits<Real>::max();
737 {
738 num_layers = extrusion_curve->n_elem();
739 }
740 else
741 {
742 num_layers = _num_layers[e];
743 height = _heights[e];
744 bias = _biases[e];
745 }
746
747 // In first layer we also add the base nodes, hence the "plus one"
748 unsigned int num_heights_at_elevation = order * num_layers + (e == 0 ? 1 : 0);
749 // Keep track of the plane normal to the extrusion
750 RealVectorValue prev_intersecting_plane_normal_vec = _start_extrusion_direction;
751
752 // k is the element layer ordering within each elevation layer
753 for (const auto k : make_range(num_heights_at_elevation))
754 {
755 // Compute 'orig_node_to_current', the update vector
756 // For the first layer we don't need to move
757 if (e == 0 && k == 0)
758 orig_node_to_current.zero();
759 else
760 {
761 // Shift the previous position by a certain fraction of 'height' along the extrusion
762 // direction to get the new position.
763
764 // Compute orig_node_to_current before any transformation
765 Real step_size = 0;
766 Real step_size_at_axis = 0;
768 {
769 // current point in extrusion curve
770 const Node * P_current = extrusion_curve->node_ptr(k);
771 // previous point in extrusion curve
772 const Node * P_prev = extrusion_curve->node_ptr(k - 1);
773
774 // Quantities for the previous position of the extruded node
775 const auto old_node = orig_node_to_previous + *node;
776 RealVectorValue b_vec = old_node - *P_prev;
777 Real node_distance_to_curve = b_vec.norm();
778
779 // Node does not lie exactly on the extrusion curve we are following
780 if (node_distance_to_curve > libMesh::TOLERANCE)
781 {
782 // normal vector to the plane to extrude the point into
783 RealVectorValue intersecting_plane_normal_vec;
784
785 // Select the extrusion direction based on the curve or the user parameters
786 if (k == 1)
787 {
788 const auto P_next = extrusion_curve->node_ptr(k + 1);
789 intersecting_plane_normal_vec =
790 0.5 * (_start_extrusion_direction + (*P_next - *P_current).unit());
791 }
792 else if (k < order * num_layers - 1)
793 {
794 const auto P_next = extrusion_curve->node_ptr(k + 1);
795 // this approximates the derivative at the spline point
796 intersecting_plane_normal_vec = *P_next - *P_prev;
797 }
798 else
799 {
800 intersecting_plane_normal_vec = (_end_extrusion_direction.norm_sq() > 0)
802 : RealVectorValue(*P_current - *P_prev);
803 }
804 intersecting_plane_normal_vec /= intersecting_plane_normal_vec.norm();
805
806 Point new_node_point;
807 // If the extrusion direction and the previous plane normal are aligned,
808 // we can't define a rotation axis. We simply translate the points
809 if (MooseUtils::absoluteFuzzyEqual(
810 prev_intersecting_plane_normal_vec.cross(intersecting_plane_normal_vec)
811 .norm_sq(),
812 0))
813 new_node_point = old_node + *P_current - *P_prev;
814 // We use a rotation from the previous extrusion plane normal to the current one
815 // We have tried in the past:
816 // - assuming (P_prev, P_current, old_node, current_node) are coplanar
817 // - assuming (P_prev, P_current) and (old_node, current_node) are parallel
818 // Both result in a slight deformation of the shape during extrusion.
819 else
820 {
821 auto axis = prev_intersecting_plane_normal_vec.cross(intersecting_plane_normal_vec);
822 const auto sin_th = axis.norm();
823 axis /= sin_th;
824 Real cos_th = prev_intersecting_plane_normal_vec * intersecting_plane_normal_vec;
825 // Rodrigues formula for rotation
826 const auto new_v = cos_th * b_vec + axis.cross(b_vec) * sin_th +
827 axis * (axis * b_vec) * (1. - cos_th);
828
829 mooseAssert(MooseUtils::absoluteFuzzyEqual(new_v.norm(), b_vec.norm()),
830 "Radial extent be conserved");
831 new_node_point = *P_current + new_v;
832 }
833
834 orig_node_to_current = new_node_point - *node;
835
836 step_size = (orig_node_to_current - orig_node_to_previous).norm();
837 step_size_at_axis = (*P_current - *P_prev).norm();
838 prev_intersecting_plane_normal_vec = intersecting_plane_normal_vec;
839 }
840 // Point is on the axis of the line
841 else
842 {
843 _direction = *P_current - *P_prev;
844 _direction /= _direction.norm();
845 mooseAssert(std::abs(_direction.norm() - 1.0) < libMesh::TOLERANCE,
846 "Norm of direction vector is not 1!");
847
848 // Calculate step size.
849 // Note: orig_node_to_previous + *node is the vector description of the
850 // previously-created node
851 step_size = ((*P_current - (orig_node_to_previous + *node)) * _direction);
852 step_size_at_axis = step_size;
853 orig_node_to_current = orig_node_to_previous + _direction * step_size;
854 }
855 }
856 // Extruding in a fixed direction (not along a curve)
857 else
858 {
859 // Divide by the order to avoid applying the bias to the nodes within a higher order
860 // element
861 auto layer_index = (k - (e == 0 ? 1 : 0)) / order + 1;
862 step_size = MooseUtils::absoluteFuzzyEqual(bias, 1.0)
863 ? height / (Real)num_layers / (Real)order
864 : height * std::pow(bias, (Real)(layer_index - 1)) * (1.0 - bias) /
865 (1.0 - std::pow(bias, (Real)(num_layers))) / (Real)order;
866 step_size_at_axis = step_size;
867 // update distance from starting node to new node
868 orig_node_to_current = orig_node_to_previous + node_extrude_direction * step_size;
869 }
870
871 // Keep track of the cumulative step size as an extrusion axis coordinate
872 sum_step_sizes += step_size;
873 sum_step_sizes_at_axis += step_size_at_axis;
874
875 // Handle radial expansion. No need to perform expansion at the centerline
876 if (_end_radial_extent && start_node_radius > libMesh::TOLERANCE)
877 {
878 // How far along we are in the extrusion, measured at the axis (=curve when extruding
879 // along a curve)
880 Real tm1 =
881 (sum_step_sizes_at_axis - step_size_at_axis) / total_extrusion_distance_at_axis;
882 Real t = sum_step_sizes_at_axis / total_extrusion_distance_at_axis;
883
884 // Direction of radial expansion.
885 RealVectorValue node_to_extrusion_axis;
887 node_to_extrusion_axis =
888 *node + orig_node_to_current - *(extrusion_curve->node_ptr(k));
889 // The radial expansion has to be performed in the rotated frame
890 else if (!MooseUtils::absoluteFuzzyEqual(_twist_pitch, 0.))
891 node_to_extrusion_axis =
892 *node + orig_node_to_current - (reference_point + _direction * sum_step_sizes);
893 // when extruding in a straight line with no twisting, we can use the original radial
894 // direction
895 else
896 node_to_extrusion_axis = *node - reference_point;
897
898 // Fraction of the total starting radius the node lives in
899 const auto radius_scaling = start_node_radius / start_radial_extent;
900 // Calculate weighting for expansion
901 const auto radial_ratio_m1 = AdvancedExtruderGenerator::radialExpansionRatio(tm1);
902 const auto radial_ratio = AdvancedExtruderGenerator::radialExpansionRatio(t);
903
904 // Compute change in step
905 orig_node_to_current += (start_radial_extent * (radial_ratio_m1 - radial_ratio) +
906 (radial_ratio - radial_ratio_m1) * _end_radial_extent) *
907 radius_scaling * node_to_extrusion_axis.unit();
908 }
909
910 // Handle helicoidal extrusion
911 if (!MooseUtils::absoluteFuzzyEqual(_twist_pitch, 0.))
912 {
913 // twist 1 should be 'normal' to the extruded shape
914 RealVectorValue twist1 = _direction.cross(*node);
915 // This happens for any node on the helicoidal extrusion axis
916 if (!MooseUtils::absoluteFuzzyEqual(twist1.norm(), .0))
917 twist1 /= twist1.norm();
918 const RealVectorValue twist2 = twist1.cross(_direction);
919
920 auto twist =
921 (cos(2. * libMesh::pi * current_node_layer * step_size / _twist_pitch) -
922 cos(2. * libMesh::pi * (current_node_layer - 1) * step_size / _twist_pitch)) *
923 twist2 +
924 (sin(2. * libMesh::pi * current_node_layer * step_size / _twist_pitch) -
925 sin(2. * libMesh::pi * (current_node_layer - 1) * step_size / _twist_pitch)) *
926 twist1;
927
928 // If we normalize twist, we must multiply by 2 * sin(libMesh::pi * step_size /
929 // _twist_pitch) if (!MooseUtils::absoluteFuzzyEqual(twist.norm(), .0))
930 // twist /= twist.norm();
931
932 // Get a point on the extrusion axis (around which we currently twist the geometry)
933 // at the local elevation height to be able to compute the distance to the axis
934 Point extrusion_axis_at_elevation;
935 Point prev_extrusion_axis_at_elevation;
937 {
938 extrusion_axis_at_elevation = *extrusion_curve->node_ptr(k);
939 prev_extrusion_axis_at_elevation = *extrusion_curve->node_ptr(k - 1);
940 }
941 else
942 {
943 // We can't use old and current step updates because they have been "twisted"
944 extrusion_axis_at_elevation = reference_point + _direction * sum_step_sizes;
945 prev_extrusion_axis_at_elevation =
946 reference_point + _direction * (sum_step_sizes - step_size);
947 }
948
949 // Scale with how far the node is from the extrusion axis, before twisting
950 twist *= (*node + orig_node_to_current - extrusion_axis_at_elevation).norm();
951
952 // No need to twist or expand the point on the axis
953 if (!MooseUtils::absoluteFuzzyEqual(twist1.norm(), .0))
954 orig_node_to_current += twist;
955 }
956 }
957
958 // Add the new node to the mesh
959 Node * new_node = mesh->add_point(*node + orig_node_to_current,
960 node->id() + (current_node_layer * orig_nodes),
961 node->processor_id());
962
963#ifdef LIBMESH_ENABLE_UNIQUE_ID
964 // Let's give the base of the extruded mesh the same
965 // unique_ids as the source mesh, in case anyone finds that
966 // a useful map to preserve.
967 const unique_id_type uid = (current_node_layer == 0)
968 ? node->unique_id()
969 : orig_unique_ids +
970 (current_node_layer - 1) * (orig_elem + orig_nodes) +
971 node->id();
972 new_node->set_unique_id(uid);
973#endif
974
975 // Add the new node to the extruded boundaries
976 input_boundary_info.boundary_ids(node, ids_to_copy);
977 if (_boundary_swap_pairs.empty())
978 boundary_info.add_node(new_node, ids_to_copy);
979 else
980 for (const auto & id_to_copy : ids_to_copy)
981 {
982 boundary_info.add_node(new_node,
983 _boundary_swap_pairs[e].count(id_to_copy)
984 ? _boundary_swap_pairs[e][id_to_copy]
985 : id_to_copy);
986 }
987
988 orig_node_to_previous = orig_node_to_current;
989 current_node_layer++;
990 }
991 }
992 }
993
994 const auto & side_ids = input_boundary_info.get_side_boundary_ids();
995
996 boundary_id_type next_side_id =
997 side_ids.empty() ? 0 : cast_int<boundary_id_type>(*side_ids.rbegin() + 1);
998
999 // side_ids may not include ids from remote elements, in which case
1000 // some processors may have underestimated the next_side_id; let's
1001 // fix that.
1002 input->comm().max(next_side_id);
1003
1004 // A generated side boundary ID starts after the largest input boundary ID, so it can coincide
1005 // with the default bottom (next_side_id) and top (next_side_id + 1) IDs
1007 while (user_side_boundary_id == next_side_id || user_side_boundary_id == next_side_id + 1)
1008 ++next_side_id;
1009
1010 // Map to keep track of polygon sides of polygonal prisms
1011 std::map<std::array<unsigned int, 3>, std::shared_ptr<libMesh::Polygon>> poly_extruded_sides;
1012
1013 // Build the extruded elements
1014 for (const auto & elem : input->element_ptr_range())
1015 {
1016 const ElemType etype = elem->type();
1017
1018 // build_extrusion currently only works on coarse meshes
1019 libmesh_assert(!elem->parent());
1020
1021 unsigned int current_layer = 0;
1022
1023 for (unsigned int e = 0; e != total_num_elevations; e++)
1024 {
1025 auto num_layers = !_extrude_along_curve ? _num_layers[e] : extrusion_curve->n_nodes() - 1;
1026
1027 for (unsigned int k = 0; k != num_layers; ++k)
1028 {
1029 std::unique_ptr<Elem> new_elem;
1030 bool is_flipped(false);
1031 switch (etype)
1032 {
1033 case EDGE2:
1034 {
1035 new_elem = std::make_unique<Quad4>();
1036 new_elem->set_node(
1037 0, mesh->node_ptr(elem->node_ptr(0)->id() + (current_layer * orig_nodes)));
1038 new_elem->set_node(
1039 1, mesh->node_ptr(elem->node_ptr(1)->id() + (current_layer * orig_nodes)));
1040 new_elem->set_node(
1041 2, mesh->node_ptr(elem->node_ptr(1)->id() + ((current_layer + 1) * orig_nodes)));
1042 new_elem->set_node(
1043 3, mesh->node_ptr(elem->node_ptr(0)->id() + ((current_layer + 1) * orig_nodes)));
1044
1045 if (elem->neighbor_ptr(0) == remote_elem)
1046 new_elem->set_neighbor(3, const_cast<RemoteElem *>(remote_elem));
1047 if (elem->neighbor_ptr(1) == remote_elem)
1048 new_elem->set_neighbor(1, const_cast<RemoteElem *>(remote_elem));
1049
1050 break;
1051 }
1052 case EDGE3:
1053 {
1054 new_elem = std::make_unique<Quad9>();
1055 new_elem->set_node(
1056 0, mesh->node_ptr(elem->node_ptr(0)->id() + (2 * current_layer * orig_nodes)));
1057 new_elem->set_node(
1058 1, mesh->node_ptr(elem->node_ptr(1)->id() + (2 * current_layer * orig_nodes)));
1059 new_elem->set_node(
1060 2,
1061 mesh->node_ptr(elem->node_ptr(1)->id() + ((2 * current_layer + 2) * orig_nodes)));
1062 new_elem->set_node(
1063 3,
1064 mesh->node_ptr(elem->node_ptr(0)->id() + ((2 * current_layer + 2) * orig_nodes)));
1065 new_elem->set_node(
1066 4, mesh->node_ptr(elem->node_ptr(2)->id() + (2 * current_layer * orig_nodes)));
1067 new_elem->set_node(
1068 5,
1069 mesh->node_ptr(elem->node_ptr(1)->id() + ((2 * current_layer + 1) * orig_nodes)));
1070 new_elem->set_node(
1071 6,
1072 mesh->node_ptr(elem->node_ptr(2)->id() + ((2 * current_layer + 2) * orig_nodes)));
1073 new_elem->set_node(
1074 7,
1075 mesh->node_ptr(elem->node_ptr(0)->id() + ((2 * current_layer + 1) * orig_nodes)));
1076 new_elem->set_node(
1077 8,
1078 mesh->node_ptr(elem->node_ptr(2)->id() + ((2 * current_layer + 1) * orig_nodes)));
1079
1080 if (elem->neighbor_ptr(0) == remote_elem)
1081 new_elem->set_neighbor(3, const_cast<RemoteElem *>(remote_elem));
1082 if (elem->neighbor_ptr(1) == remote_elem)
1083 new_elem->set_neighbor(1, const_cast<RemoteElem *>(remote_elem));
1084
1085 break;
1086 }
1087 case TRI3:
1088 {
1089 new_elem = std::make_unique<Prism6>();
1090 new_elem->set_node(
1091 0, mesh->node_ptr(elem->node_ptr(0)->id() + (current_layer * orig_nodes)));
1092 new_elem->set_node(
1093 1, mesh->node_ptr(elem->node_ptr(1)->id() + (current_layer * orig_nodes)));
1094 new_elem->set_node(
1095 2, mesh->node_ptr(elem->node_ptr(2)->id() + (current_layer * orig_nodes)));
1096 new_elem->set_node(
1097 3, mesh->node_ptr(elem->node_ptr(0)->id() + ((current_layer + 1) * orig_nodes)));
1098 new_elem->set_node(
1099 4, mesh->node_ptr(elem->node_ptr(1)->id() + ((current_layer + 1) * orig_nodes)));
1100 new_elem->set_node(
1101 5, mesh->node_ptr(elem->node_ptr(2)->id() + ((current_layer + 1) * orig_nodes)));
1102
1103 if (elem->neighbor_ptr(0) == remote_elem)
1104 new_elem->set_neighbor(1, const_cast<RemoteElem *>(remote_elem));
1105 if (elem->neighbor_ptr(1) == remote_elem)
1106 new_elem->set_neighbor(2, const_cast<RemoteElem *>(remote_elem));
1107 if (elem->neighbor_ptr(2) == remote_elem)
1108 new_elem->set_neighbor(3, const_cast<RemoteElem *>(remote_elem));
1109
1110 if (new_elem->volume() < 0.0)
1111 {
1112 MooseMeshUtils::swapNodesInElem(*new_elem, 0, 3);
1113 MooseMeshUtils::swapNodesInElem(*new_elem, 1, 4);
1114 MooseMeshUtils::swapNodesInElem(*new_elem, 2, 5);
1115 is_flipped = true;
1116 }
1117
1118 break;
1119 }
1120 case TRI6:
1121 {
1122 new_elem = std::make_unique<Prism18>();
1123 new_elem->set_node(
1124 0, mesh->node_ptr(elem->node_ptr(0)->id() + (2 * current_layer * orig_nodes)));
1125 new_elem->set_node(
1126 1, mesh->node_ptr(elem->node_ptr(1)->id() + (2 * current_layer * orig_nodes)));
1127 new_elem->set_node(
1128 2, mesh->node_ptr(elem->node_ptr(2)->id() + (2 * current_layer * orig_nodes)));
1129 new_elem->set_node(
1130 3,
1131 mesh->node_ptr(elem->node_ptr(0)->id() + ((2 * current_layer + 2) * orig_nodes)));
1132 new_elem->set_node(
1133 4,
1134 mesh->node_ptr(elem->node_ptr(1)->id() + ((2 * current_layer + 2) * orig_nodes)));
1135 new_elem->set_node(
1136 5,
1137 mesh->node_ptr(elem->node_ptr(2)->id() + ((2 * current_layer + 2) * orig_nodes)));
1138 new_elem->set_node(
1139 6, mesh->node_ptr(elem->node_ptr(3)->id() + (2 * current_layer * orig_nodes)));
1140 new_elem->set_node(
1141 7, mesh->node_ptr(elem->node_ptr(4)->id() + (2 * current_layer * orig_nodes)));
1142 new_elem->set_node(
1143 8, mesh->node_ptr(elem->node_ptr(5)->id() + (2 * current_layer * orig_nodes)));
1144 new_elem->set_node(
1145 9,
1146 mesh->node_ptr(elem->node_ptr(0)->id() + ((2 * current_layer + 1) * orig_nodes)));
1147 new_elem->set_node(
1148 10,
1149 mesh->node_ptr(elem->node_ptr(1)->id() + ((2 * current_layer + 1) * orig_nodes)));
1150 new_elem->set_node(
1151 11,
1152 mesh->node_ptr(elem->node_ptr(2)->id() + ((2 * current_layer + 1) * orig_nodes)));
1153 new_elem->set_node(
1154 12,
1155 mesh->node_ptr(elem->node_ptr(3)->id() + ((2 * current_layer + 2) * orig_nodes)));
1156 new_elem->set_node(
1157 13,
1158 mesh->node_ptr(elem->node_ptr(4)->id() + ((2 * current_layer + 2) * orig_nodes)));
1159 new_elem->set_node(
1160 14,
1161 mesh->node_ptr(elem->node_ptr(5)->id() + ((2 * current_layer + 2) * orig_nodes)));
1162 new_elem->set_node(
1163 15,
1164 mesh->node_ptr(elem->node_ptr(3)->id() + ((2 * current_layer + 1) * orig_nodes)));
1165 new_elem->set_node(
1166 16,
1167 mesh->node_ptr(elem->node_ptr(4)->id() + ((2 * current_layer + 1) * orig_nodes)));
1168 new_elem->set_node(
1169 17,
1170 mesh->node_ptr(elem->node_ptr(5)->id() + ((2 * current_layer + 1) * orig_nodes)));
1171
1172 if (elem->neighbor_ptr(0) == remote_elem)
1173 new_elem->set_neighbor(1, const_cast<RemoteElem *>(remote_elem));
1174 if (elem->neighbor_ptr(1) == remote_elem)
1175 new_elem->set_neighbor(2, const_cast<RemoteElem *>(remote_elem));
1176 if (elem->neighbor_ptr(2) == remote_elem)
1177 new_elem->set_neighbor(3, const_cast<RemoteElem *>(remote_elem));
1178
1179 if (new_elem->volume() < 0.0)
1180 {
1181 MooseMeshUtils::swapNodesInElem(*new_elem, 0, 3);
1182 MooseMeshUtils::swapNodesInElem(*new_elem, 1, 4);
1183 MooseMeshUtils::swapNodesInElem(*new_elem, 2, 5);
1184 MooseMeshUtils::swapNodesInElem(*new_elem, 6, 12);
1185 MooseMeshUtils::swapNodesInElem(*new_elem, 7, 13);
1186 MooseMeshUtils::swapNodesInElem(*new_elem, 8, 14);
1187 is_flipped = true;
1188 }
1189
1190 break;
1191 }
1192 case TRI7:
1193 {
1194 new_elem = std::make_unique<Prism21>();
1195 new_elem->set_node(
1196 0, mesh->node_ptr(elem->node_ptr(0)->id() + (2 * current_layer * orig_nodes)));
1197 new_elem->set_node(
1198 1, mesh->node_ptr(elem->node_ptr(1)->id() + (2 * current_layer * orig_nodes)));
1199 new_elem->set_node(
1200 2, mesh->node_ptr(elem->node_ptr(2)->id() + (2 * current_layer * orig_nodes)));
1201 new_elem->set_node(
1202 3,
1203 mesh->node_ptr(elem->node_ptr(0)->id() + ((2 * current_layer + 2) * orig_nodes)));
1204 new_elem->set_node(
1205 4,
1206 mesh->node_ptr(elem->node_ptr(1)->id() + ((2 * current_layer + 2) * orig_nodes)));
1207 new_elem->set_node(
1208 5,
1209 mesh->node_ptr(elem->node_ptr(2)->id() + ((2 * current_layer + 2) * orig_nodes)));
1210 new_elem->set_node(
1211 6, mesh->node_ptr(elem->node_ptr(3)->id() + (2 * current_layer * orig_nodes)));
1212 new_elem->set_node(
1213 7, mesh->node_ptr(elem->node_ptr(4)->id() + (2 * current_layer * orig_nodes)));
1214 new_elem->set_node(
1215 8, mesh->node_ptr(elem->node_ptr(5)->id() + (2 * current_layer * orig_nodes)));
1216 new_elem->set_node(
1217 9,
1218 mesh->node_ptr(elem->node_ptr(0)->id() + ((2 * current_layer + 1) * orig_nodes)));
1219 new_elem->set_node(
1220 10,
1221 mesh->node_ptr(elem->node_ptr(1)->id() + ((2 * current_layer + 1) * orig_nodes)));
1222 new_elem->set_node(
1223 11,
1224 mesh->node_ptr(elem->node_ptr(2)->id() + ((2 * current_layer + 1) * orig_nodes)));
1225 new_elem->set_node(
1226 12,
1227 mesh->node_ptr(elem->node_ptr(3)->id() + ((2 * current_layer + 2) * orig_nodes)));
1228 new_elem->set_node(
1229 13,
1230 mesh->node_ptr(elem->node_ptr(4)->id() + ((2 * current_layer + 2) * orig_nodes)));
1231 new_elem->set_node(
1232 14,
1233 mesh->node_ptr(elem->node_ptr(5)->id() + ((2 * current_layer + 2) * orig_nodes)));
1234 new_elem->set_node(
1235 15,
1236 mesh->node_ptr(elem->node_ptr(3)->id() + ((2 * current_layer + 1) * orig_nodes)));
1237 new_elem->set_node(
1238 16,
1239 mesh->node_ptr(elem->node_ptr(4)->id() + ((2 * current_layer + 1) * orig_nodes)));
1240 new_elem->set_node(
1241 17,
1242 mesh->node_ptr(elem->node_ptr(5)->id() + ((2 * current_layer + 1) * orig_nodes)));
1243 new_elem->set_node(
1244 18, mesh->node_ptr(elem->node_ptr(6)->id() + (2 * current_layer * orig_nodes)));
1245 new_elem->set_node(
1246 19,
1247 mesh->node_ptr(elem->node_ptr(6)->id() + ((2 * current_layer + 2) * orig_nodes)));
1248 new_elem->set_node(
1249 20,
1250 mesh->node_ptr(elem->node_ptr(6)->id() + ((2 * current_layer + 1) * orig_nodes)));
1251
1252 if (elem->neighbor_ptr(0) == remote_elem)
1253 new_elem->set_neighbor(1, const_cast<RemoteElem *>(remote_elem));
1254 if (elem->neighbor_ptr(1) == remote_elem)
1255 new_elem->set_neighbor(2, const_cast<RemoteElem *>(remote_elem));
1256 if (elem->neighbor_ptr(2) == remote_elem)
1257 new_elem->set_neighbor(3, const_cast<RemoteElem *>(remote_elem));
1258
1259 if (new_elem->volume() < 0.0)
1260 {
1261 MooseMeshUtils::swapNodesInElem(*new_elem, 0, 3);
1262 MooseMeshUtils::swapNodesInElem(*new_elem, 1, 4);
1263 MooseMeshUtils::swapNodesInElem(*new_elem, 2, 5);
1264 MooseMeshUtils::swapNodesInElem(*new_elem, 6, 12);
1265 MooseMeshUtils::swapNodesInElem(*new_elem, 7, 13);
1266 MooseMeshUtils::swapNodesInElem(*new_elem, 8, 14);
1267 MooseMeshUtils::swapNodesInElem(*new_elem, 18, 19);
1268 is_flipped = true;
1269 }
1270
1271 break;
1272 }
1273 case QUAD4:
1274 {
1275 new_elem = std::make_unique<Hex8>();
1276 new_elem->set_node(
1277 0, mesh->node_ptr(elem->node_ptr(0)->id() + (current_layer * orig_nodes)));
1278 new_elem->set_node(
1279 1, mesh->node_ptr(elem->node_ptr(1)->id() + (current_layer * orig_nodes)));
1280 new_elem->set_node(
1281 2, mesh->node_ptr(elem->node_ptr(2)->id() + (current_layer * orig_nodes)));
1282 new_elem->set_node(
1283 3, mesh->node_ptr(elem->node_ptr(3)->id() + (current_layer * orig_nodes)));
1284 new_elem->set_node(
1285 4, mesh->node_ptr(elem->node_ptr(0)->id() + ((current_layer + 1) * orig_nodes)));
1286 new_elem->set_node(
1287 5, mesh->node_ptr(elem->node_ptr(1)->id() + ((current_layer + 1) * orig_nodes)));
1288 new_elem->set_node(
1289 6, mesh->node_ptr(elem->node_ptr(2)->id() + ((current_layer + 1) * orig_nodes)));
1290 new_elem->set_node(
1291 7, mesh->node_ptr(elem->node_ptr(3)->id() + ((current_layer + 1) * orig_nodes)));
1292
1293 if (elem->neighbor_ptr(0) == remote_elem)
1294 new_elem->set_neighbor(1, const_cast<RemoteElem *>(remote_elem));
1295 if (elem->neighbor_ptr(1) == remote_elem)
1296 new_elem->set_neighbor(2, const_cast<RemoteElem *>(remote_elem));
1297 if (elem->neighbor_ptr(2) == remote_elem)
1298 new_elem->set_neighbor(3, const_cast<RemoteElem *>(remote_elem));
1299 if (elem->neighbor_ptr(3) == remote_elem)
1300 new_elem->set_neighbor(4, const_cast<RemoteElem *>(remote_elem));
1301
1302 if (new_elem->volume() < 0.0)
1303 {
1304 MooseMeshUtils::swapNodesInElem(*new_elem, 0, 4);
1305 MooseMeshUtils::swapNodesInElem(*new_elem, 1, 5);
1306 MooseMeshUtils::swapNodesInElem(*new_elem, 2, 6);
1307 MooseMeshUtils::swapNodesInElem(*new_elem, 3, 7);
1308 is_flipped = true;
1309 }
1310
1311 break;
1312 }
1313 case QUAD8:
1314 {
1315 new_elem = std::make_unique<Hex20>();
1316 new_elem->set_node(
1317 0, mesh->node_ptr(elem->node_ptr(0)->id() + (2 * current_layer * orig_nodes)));
1318 new_elem->set_node(
1319 1, mesh->node_ptr(elem->node_ptr(1)->id() + (2 * current_layer * orig_nodes)));
1320 new_elem->set_node(
1321 2, mesh->node_ptr(elem->node_ptr(2)->id() + (2 * current_layer * orig_nodes)));
1322 new_elem->set_node(
1323 3, mesh->node_ptr(elem->node_ptr(3)->id() + (2 * current_layer * orig_nodes)));
1324 new_elem->set_node(
1325 4,
1326 mesh->node_ptr(elem->node_ptr(0)->id() + ((2 * current_layer + 2) * orig_nodes)));
1327 new_elem->set_node(
1328 5,
1329 mesh->node_ptr(elem->node_ptr(1)->id() + ((2 * current_layer + 2) * orig_nodes)));
1330 new_elem->set_node(
1331 6,
1332 mesh->node_ptr(elem->node_ptr(2)->id() + ((2 * current_layer + 2) * orig_nodes)));
1333 new_elem->set_node(
1334 7,
1335 mesh->node_ptr(elem->node_ptr(3)->id() + ((2 * current_layer + 2) * orig_nodes)));
1336 new_elem->set_node(
1337 8, mesh->node_ptr(elem->node_ptr(4)->id() + (2 * current_layer * orig_nodes)));
1338 new_elem->set_node(
1339 9, mesh->node_ptr(elem->node_ptr(5)->id() + (2 * current_layer * orig_nodes)));
1340 new_elem->set_node(
1341 10, mesh->node_ptr(elem->node_ptr(6)->id() + (2 * current_layer * orig_nodes)));
1342 new_elem->set_node(
1343 11, mesh->node_ptr(elem->node_ptr(7)->id() + (2 * current_layer * orig_nodes)));
1344 new_elem->set_node(
1345 12,
1346 mesh->node_ptr(elem->node_ptr(0)->id() + ((2 * current_layer + 1) * orig_nodes)));
1347 new_elem->set_node(
1348 13,
1349 mesh->node_ptr(elem->node_ptr(1)->id() + ((2 * current_layer + 1) * orig_nodes)));
1350 new_elem->set_node(
1351 14,
1352 mesh->node_ptr(elem->node_ptr(2)->id() + ((2 * current_layer + 1) * orig_nodes)));
1353 new_elem->set_node(
1354 15,
1355 mesh->node_ptr(elem->node_ptr(3)->id() + ((2 * current_layer + 1) * orig_nodes)));
1356 new_elem->set_node(
1357 16,
1358 mesh->node_ptr(elem->node_ptr(4)->id() + ((2 * current_layer + 2) * orig_nodes)));
1359 new_elem->set_node(
1360 17,
1361 mesh->node_ptr(elem->node_ptr(5)->id() + ((2 * current_layer + 2) * orig_nodes)));
1362 new_elem->set_node(
1363 18,
1364 mesh->node_ptr(elem->node_ptr(6)->id() + ((2 * current_layer + 2) * orig_nodes)));
1365 new_elem->set_node(
1366 19,
1367 mesh->node_ptr(elem->node_ptr(7)->id() + ((2 * current_layer + 2) * orig_nodes)));
1368
1369 if (elem->neighbor_ptr(0) == remote_elem)
1370 new_elem->set_neighbor(1, const_cast<RemoteElem *>(remote_elem));
1371 if (elem->neighbor_ptr(1) == remote_elem)
1372 new_elem->set_neighbor(2, const_cast<RemoteElem *>(remote_elem));
1373 if (elem->neighbor_ptr(2) == remote_elem)
1374 new_elem->set_neighbor(3, const_cast<RemoteElem *>(remote_elem));
1375 if (elem->neighbor_ptr(3) == remote_elem)
1376 new_elem->set_neighbor(4, const_cast<RemoteElem *>(remote_elem));
1377
1378 if (new_elem->volume() < 0.0)
1379 {
1380 MooseMeshUtils::swapNodesInElem(*new_elem, 0, 4);
1381 MooseMeshUtils::swapNodesInElem(*new_elem, 1, 5);
1382 MooseMeshUtils::swapNodesInElem(*new_elem, 2, 6);
1383 MooseMeshUtils::swapNodesInElem(*new_elem, 3, 7);
1384 MooseMeshUtils::swapNodesInElem(*new_elem, 8, 16);
1385 MooseMeshUtils::swapNodesInElem(*new_elem, 9, 17);
1386 MooseMeshUtils::swapNodesInElem(*new_elem, 10, 18);
1387 MooseMeshUtils::swapNodesInElem(*new_elem, 11, 19);
1388 is_flipped = true;
1389 }
1390
1391 break;
1392 }
1393 case QUAD9:
1394 {
1395 new_elem = std::make_unique<Hex27>();
1396 new_elem->set_node(
1397 0, mesh->node_ptr(elem->node_ptr(0)->id() + (2 * current_layer * orig_nodes)));
1398 new_elem->set_node(
1399 1, mesh->node_ptr(elem->node_ptr(1)->id() + (2 * current_layer * orig_nodes)));
1400 new_elem->set_node(
1401 2, mesh->node_ptr(elem->node_ptr(2)->id() + (2 * current_layer * orig_nodes)));
1402 new_elem->set_node(
1403 3, mesh->node_ptr(elem->node_ptr(3)->id() + (2 * current_layer * orig_nodes)));
1404 new_elem->set_node(
1405 4,
1406 mesh->node_ptr(elem->node_ptr(0)->id() + ((2 * current_layer + 2) * orig_nodes)));
1407 new_elem->set_node(
1408 5,
1409 mesh->node_ptr(elem->node_ptr(1)->id() + ((2 * current_layer + 2) * orig_nodes)));
1410 new_elem->set_node(
1411 6,
1412 mesh->node_ptr(elem->node_ptr(2)->id() + ((2 * current_layer + 2) * orig_nodes)));
1413 new_elem->set_node(
1414 7,
1415 mesh->node_ptr(elem->node_ptr(3)->id() + ((2 * current_layer + 2) * orig_nodes)));
1416 new_elem->set_node(
1417 8, mesh->node_ptr(elem->node_ptr(4)->id() + (2 * current_layer * orig_nodes)));
1418 new_elem->set_node(
1419 9, mesh->node_ptr(elem->node_ptr(5)->id() + (2 * current_layer * orig_nodes)));
1420 new_elem->set_node(
1421 10, mesh->node_ptr(elem->node_ptr(6)->id() + (2 * current_layer * orig_nodes)));
1422 new_elem->set_node(
1423 11, mesh->node_ptr(elem->node_ptr(7)->id() + (2 * current_layer * orig_nodes)));
1424 new_elem->set_node(
1425 12,
1426 mesh->node_ptr(elem->node_ptr(0)->id() + ((2 * current_layer + 1) * orig_nodes)));
1427 new_elem->set_node(
1428 13,
1429 mesh->node_ptr(elem->node_ptr(1)->id() + ((2 * current_layer + 1) * orig_nodes)));
1430 new_elem->set_node(
1431 14,
1432 mesh->node_ptr(elem->node_ptr(2)->id() + ((2 * current_layer + 1) * orig_nodes)));
1433 new_elem->set_node(
1434 15,
1435 mesh->node_ptr(elem->node_ptr(3)->id() + ((2 * current_layer + 1) * orig_nodes)));
1436 new_elem->set_node(
1437 16,
1438 mesh->node_ptr(elem->node_ptr(4)->id() + ((2 * current_layer + 2) * orig_nodes)));
1439 new_elem->set_node(
1440 17,
1441 mesh->node_ptr(elem->node_ptr(5)->id() + ((2 * current_layer + 2) * orig_nodes)));
1442 new_elem->set_node(
1443 18,
1444 mesh->node_ptr(elem->node_ptr(6)->id() + ((2 * current_layer + 2) * orig_nodes)));
1445 new_elem->set_node(
1446 19,
1447 mesh->node_ptr(elem->node_ptr(7)->id() + ((2 * current_layer + 2) * orig_nodes)));
1448 new_elem->set_node(
1449 20, mesh->node_ptr(elem->node_ptr(8)->id() + (2 * current_layer * orig_nodes)));
1450 new_elem->set_node(
1451 21,
1452 mesh->node_ptr(elem->node_ptr(4)->id() + ((2 * current_layer + 1) * orig_nodes)));
1453 new_elem->set_node(
1454 22,
1455 mesh->node_ptr(elem->node_ptr(5)->id() + ((2 * current_layer + 1) * orig_nodes)));
1456 new_elem->set_node(
1457 23,
1458 mesh->node_ptr(elem->node_ptr(6)->id() + ((2 * current_layer + 1) * orig_nodes)));
1459 new_elem->set_node(
1460 24,
1461 mesh->node_ptr(elem->node_ptr(7)->id() + ((2 * current_layer + 1) * orig_nodes)));
1462 new_elem->set_node(
1463 25,
1464 mesh->node_ptr(elem->node_ptr(8)->id() + ((2 * current_layer + 2) * orig_nodes)));
1465 new_elem->set_node(
1466 26,
1467 mesh->node_ptr(elem->node_ptr(8)->id() + ((2 * current_layer + 1) * orig_nodes)));
1468
1469 if (elem->neighbor_ptr(0) == remote_elem)
1470 new_elem->set_neighbor(1, const_cast<RemoteElem *>(remote_elem));
1471 if (elem->neighbor_ptr(1) == remote_elem)
1472 new_elem->set_neighbor(2, const_cast<RemoteElem *>(remote_elem));
1473 if (elem->neighbor_ptr(2) == remote_elem)
1474 new_elem->set_neighbor(3, const_cast<RemoteElem *>(remote_elem));
1475 if (elem->neighbor_ptr(3) == remote_elem)
1476 new_elem->set_neighbor(4, const_cast<RemoteElem *>(remote_elem));
1477
1478 if (new_elem->volume() < 0.0)
1479 {
1480 MooseMeshUtils::swapNodesInElem(*new_elem, 0, 4);
1481 MooseMeshUtils::swapNodesInElem(*new_elem, 1, 5);
1482 MooseMeshUtils::swapNodesInElem(*new_elem, 2, 6);
1483 MooseMeshUtils::swapNodesInElem(*new_elem, 3, 7);
1484 MooseMeshUtils::swapNodesInElem(*new_elem, 8, 16);
1485 MooseMeshUtils::swapNodesInElem(*new_elem, 9, 17);
1486 MooseMeshUtils::swapNodesInElem(*new_elem, 10, 18);
1487 MooseMeshUtils::swapNodesInElem(*new_elem, 11, 19);
1488 MooseMeshUtils::swapNodesInElem(*new_elem, 20, 25);
1489 is_flipped = true;
1490 }
1491
1492 break;
1493 }
1494 case libMesh::C0POLYGON:
1495 {
1496 has_polygons = true;
1497 const auto num_sides = elem->n_sides();
1498 std::vector<std::shared_ptr<libMesh::Polygon>> sides;
1499 sides.reserve(2 + num_sides);
1500 if (2 * elem->n_nodes() > libMesh::C0Polygon::max_n_nodes)
1501 mooseError("Too many nodes in polygons to extrude it. Max number of the prism "
1502 "polyhedral nodes after extrusion: " +
1503 std::to_string(libMesh::C0Polygon::max_n_nodes));
1504 // Make a copy of the original element to use as a side
1505 auto new_ptr = std::make_shared<libMesh::C0Polygon>(num_sides);
1506 for (const auto node_i : make_range(elem->n_nodes()))
1507 {
1508 // This one will be oriented outwards, it should be OK though, polyhedron code does
1509 // not mind
1510 new_ptr->set_node(
1511 node_i,
1512 mesh->node_ptr(elem->node_ptr(node_i)->id() + (current_layer * orig_nodes)));
1513 }
1514 sides.push_back(new_ptr);
1515 // Form the next horizontal side
1516 auto translated_side = std::make_shared<libMesh::C0Polygon>(num_sides);
1517 for (const auto node_i : make_range(elem->n_nodes()))
1518 translated_side->set_node(node_i,
1519 mesh->node_ptr(elem->node_ptr(node_i)->id() +
1520 ((current_layer + 1) * orig_nodes)));
1521 sides.push_back(translated_side);
1522
1523 // Form the vertical sides
1524 for (const auto side_i : make_range(num_sides))
1525 {
1526 // If the side already exists, use that
1527 std::array<unsigned int, 3> side_key = {
1528 current_layer,
1529 static_cast<unsigned int>(
1530 std::min(elem->node_ptr(side_i)->id(),
1531 elem->node_ptr((side_i + 1) % num_sides)->id())),
1532 static_cast<unsigned int>(
1533 std::max(elem->node_ptr(side_i)->id(),
1534 elem->node_ptr((side_i + 1) % num_sides)->id()))};
1535 if (poly_extruded_sides.count(side_key))
1536 {
1537 sides.push_back(poly_extruded_sides[side_key]);
1538 continue;
1539 }
1540
1541 // They are all quads, but constructor expects polygons
1542 auto vert_side = std::make_shared<libMesh::C0Polygon>(4);
1543 vert_side->set_node(
1544 0, mesh->node_ptr(elem->node_ptr(side_i)->id() + (current_layer * orig_nodes)));
1545 vert_side->set_node(1,
1546 mesh->node_ptr(elem->node_ptr((side_i + 1) % num_sides)->id() +
1547 (current_layer * orig_nodes)));
1548 vert_side->set_node(2,
1549 mesh->node_ptr(elem->node_ptr((side_i + 1) % num_sides)->id() +
1550 ((current_layer + 1) * orig_nodes)));
1551 vert_side->set_node(3,
1552 mesh->node_ptr(elem->node_ptr(side_i)->id() +
1553 ((current_layer + 1) * orig_nodes)));
1554 sides.push_back(vert_side);
1555
1556 poly_extruded_sides.insert(std::make_pair(side_key, vert_side));
1557 }
1558 mooseAssert(sides.size() == 2 + num_sides, "Unexpected size of side vector");
1559
1560 // Create the element from the sides, let libMesh figure out the orientation
1561 std::unique_ptr<libMesh::Node> mid_elem_node;
1562 new_elem = std::make_unique<libMesh::C0Polyhedron>(sides, mid_elem_node);
1563 if (mid_elem_node)
1564 {
1565#ifdef LIBMESH_ENABLE_UNIQUE_ID
1566 // Number it at the end for convenience
1567 // use the element ID to be able to set in parallel
1568 unsigned int total_new_node_layers = total_num_layers * order;
1569 unsigned int last_uid = orig_unique_ids + (total_new_node_layers - 1) * orig_elem +
1570 total_new_node_layers * orig_nodes + elem->unique_id();
1571 mid_elem_node->set_unique_id(last_uid);
1572 has_poly_midnodes = true;
1573#endif
1574 mesh->add_node(std::move(mid_elem_node));
1575 }
1576
1577 break;
1578 }
1579 default:
1580 mooseError("Extrusion is not implemented for element type " + Moose::stringify(etype));
1581 }
1582
1583 new_elem->set_id(elem->id() + (current_layer * orig_elem));
1584 new_elem->processor_id() = elem->processor_id();
1585
1586#ifdef LIBMESH_ENABLE_UNIQUE_ID
1587 // Let's give the base of the extruded mesh the same
1588 // unique_ids as the source mesh, in case anyone finds that
1589 // a useful map to preserve.
1590 const unique_id_type uid = (current_layer == 0)
1591 ? elem->unique_id()
1592 : orig_unique_ids +
1593 (current_layer - 1) * (orig_elem + orig_nodes) +
1594 orig_nodes + elem->id();
1595
1596 new_elem->set_unique_id(uid);
1597#endif
1598
1599 // maintain the subdomain_id
1600 new_elem->subdomain_id() = elem->subdomain_id();
1601
1602 // define upward boundaries
1603 if (k == num_layers - 1)
1604 {
1605 // Identify the side index of the new element that is part of the upward boundary
1606 const unsigned short top_id =
1607 new_elem->dim() == 3 ? cast_int<unsigned short>(elem->n_sides() + 1) : 2;
1608 // Assign sideset id to the side if the element belongs to a specified
1609 // upward_boundary_source_blocks
1611 for (unsigned int i = 0; i < _upward_boundary_source_blocks[e].size(); i++)
1612 if (new_elem->subdomain_id() == _upward_boundary_source_blocks[e][i])
1613 boundary_info.add_side(
1614 new_elem.get(), is_flipped ? 0 : top_id, _upward_boundary_ids[e][i]);
1615 }
1616 // define downward boundaries
1617 if (k == 0)
1618 {
1619 const unsigned short top_id =
1620 new_elem->dim() == 3 ? cast_int<unsigned short>(elem->n_sides() + 1) : 2;
1622 for (unsigned int i = 0; i < _downward_boundary_source_blocks[e].size(); i++)
1623 if (new_elem->subdomain_id() == _downward_boundary_source_blocks[e][i])
1624 boundary_info.add_side(
1625 new_elem.get(), is_flipped ? top_id : 0, _downward_boundary_ids[e][i]);
1626 }
1627
1628 // perform subdomain swaps
1629 if (_subdomain_swap_pairs.size())
1630 {
1631 auto & elevation_swap_pairs = _subdomain_swap_pairs[e];
1632
1633 auto new_id_it = elevation_swap_pairs.find(elem->subdomain_id());
1634
1635 if (new_id_it != elevation_swap_pairs.end())
1636 new_elem->subdomain_id() = new_id_it->second;
1637 }
1638
1639 Elem * added_elem = mesh->add_elem(std::move(new_elem));
1640
1641 // maintain extra integers
1642 for (unsigned int i = 0; i < num_extra_elem_integers; i++)
1643 added_elem->set_extra_integer(i, elem->get_extra_integer(i));
1644
1645 if (_elem_integers_swap_pairs.size())
1646 {
1647 for (unsigned int i = 0; i < _elem_integer_indices_to_swap.size(); i++)
1648 {
1649 auto & elevation_extra_swap_pairs = _elem_integers_swap_pairs[i * _heights.size() + e];
1650
1651 auto new_extra_id_it = elevation_extra_swap_pairs.find(
1652 elem->get_extra_integer(_elem_integer_indices_to_swap[i]));
1653
1654 if (new_extra_id_it != elevation_extra_swap_pairs.end())
1655 added_elem->set_extra_integer(_elem_integer_indices_to_swap[i],
1656 new_extra_id_it->second);
1657 }
1658 }
1659
1660 // Copy any old boundary ids on all sides
1661 for (auto s : elem->side_index_range())
1662 {
1663 input_boundary_info.boundary_ids(elem, s, ids_to_copy);
1664
1665 if (added_elem->dim() == 3)
1666 {
1667 // For 2D->3D extrusion, we give the boundary IDs
1668 // for side s on the old element to side s+1 on the
1669 // new element. This is just a happy coincidence as
1670 // far as I can tell...
1671 if (_boundary_swap_pairs.empty())
1672 boundary_info.add_side(added_elem, cast_int<unsigned short>(s + 1), ids_to_copy);
1673 else
1674 for (const auto & id_to_copy : ids_to_copy)
1675 boundary_info.add_side(added_elem,
1676 cast_int<unsigned short>(s + 1),
1677 _boundary_swap_pairs[e].count(id_to_copy)
1678 ? _boundary_swap_pairs[e][id_to_copy]
1679 : id_to_copy);
1680 }
1681 else
1682 {
1683 // For 1D->2D extrusion, the boundary IDs map as:
1684 // Old elem -> New elem
1685 // 0 -> 3
1686 // 1 -> 1
1687 libmesh_assert_less(s, 2);
1688 const unsigned short sidemap[2] = {3, 1};
1689 if (_boundary_swap_pairs.empty())
1690 boundary_info.add_side(added_elem, sidemap[s], ids_to_copy);
1691 else
1692 for (const auto & id_to_copy : ids_to_copy)
1693 boundary_info.add_side(added_elem,
1694 sidemap[s],
1695 _boundary_swap_pairs[e].count(id_to_copy)
1696 ? _boundary_swap_pairs[e][id_to_copy]
1697 : id_to_copy);
1698 }
1699
1700 // Add the extruded exterior sides and their nodes to the side boundary
1701 if (_has_side_boundary && !elem->neighbor_ptr(s))
1702 {
1703 const unsigned short new_side =
1704 added_elem->dim() == 3 ? cast_int<unsigned short>(s + 1) : (s == 0 ? 3 : 1);
1705 boundary_info.add_side(added_elem, new_side, user_side_boundary_id);
1706 for (const auto n : added_elem->nodes_on_side(new_side))
1707 boundary_info.add_node(added_elem->node_ptr(n), user_side_boundary_id);
1708 }
1709 }
1710
1711 // Give new boundary ids to bottom and top
1712 if (current_layer == 0)
1713 {
1714 const unsigned short top_id =
1715 added_elem->dim() == 3 ? cast_int<unsigned short>(elem->n_sides() + 1) : 2;
1717 {
1718 mooseAssert(user_bottom_boundary_id != libMesh::BoundaryInfo::invalid_id,
1719 "We should have retrieved a proper boundary ID");
1720 boundary_info.add_side(added_elem, is_flipped ? top_id : 0, user_bottom_boundary_id);
1721 }
1722 else
1723 boundary_info.add_side(added_elem, is_flipped ? top_id : 0, next_side_id);
1724 }
1725
1726 if (current_layer == total_num_layers - 1)
1727 {
1728 // For 2D->3D extrusion, the "top" ID is 1+the original
1729 // element's number of sides. For 1D->2D extrusion, the
1730 // "top" ID is side 2.
1731 const unsigned short top_id =
1732 added_elem->dim() == 3 ? cast_int<unsigned short>(elem->n_sides() + 1) : 2;
1733
1735 {
1736 mooseAssert(user_top_boundary_id != libMesh::BoundaryInfo::invalid_id,
1737 "We should have retrieved a proper boundary ID");
1738 boundary_info.add_side(added_elem, is_flipped ? 0 : top_id, user_top_boundary_id);
1739 }
1740 else
1741 boundary_info.add_side(
1742 added_elem, is_flipped ? 0 : top_id, cast_int<boundary_id_type>(next_side_id + 1));
1743 }
1744
1745 current_layer++;
1746 }
1747 }
1748 }
1749
1750 if (has_polygons && !input->is_serial())
1751 mooseError("Distributed meshes are not supported when extruding polygons at this time.");
1752
1753#ifdef LIBMESH_ENABLE_UNIQUE_ID
1754 // Update the value of next_unique_id based on newly created nodes and elements
1755 // Note: Number of element layers is one less than number of node layers
1756 unsigned int total_new_node_layers = total_num_layers * order;
1757 unsigned int new_unique_ids = orig_unique_ids + (total_new_node_layers - 1) * orig_elem +
1758 total_new_node_layers * orig_nodes;
1759 // Maximum case for the unique ids: all poly elements have a midnode
1760 if (has_poly_midnodes)
1761 new_unique_ids += orig_elem;
1762 mesh->set_next_unique_id(new_unique_ids);
1763#endif
1764
1765 // Copy all the subdomain/sideset/nodeset name maps to the extruded mesh
1766 if (!input_subdomain_map.empty())
1767 mesh->set_subdomain_name_map().insert(input_subdomain_map.begin(), input_subdomain_map.end());
1768 for (const auto & [subdomain_name, subdomain_id] : new_subdomain_ids)
1769 mesh->set_subdomain_name(subdomain_id, subdomain_name, true);
1770 if (!input_sideset_map.empty())
1771 mesh->get_boundary_info().set_sideset_name_map().insert(input_sideset_map.begin(),
1772 input_sideset_map.end());
1773 if (!input_nodeset_map.empty())
1774 mesh->get_boundary_info().set_nodeset_name_map().insert(input_nodeset_map.begin(),
1775 input_nodeset_map.end());
1776
1777 for (const auto i : index_range(new_boundary_names))
1778 boundary_info.sideset_name(new_boundary_ids[i]) = new_boundary_names[i];
1780 boundary_info.nodeset_name(user_side_boundary_id) = _side_boundary;
1781
1782 mesh->unset_is_prepared();
1783 // Creating the layered meshes creates a lot of leftover nodes, notably in the boundary_info,
1784 // which will crash both paraview and trigger exodiff. Best to be safe.
1785 if (extruding_quad_eights)
1786 mesh->prepare_for_use();
1787
1788 return mesh;
1789}
1790
1791Real
1793{
1794 // NOTE: All functions added to this method must obey the following: f(0)=0, f(1)=1 for all t in
1795 // [0,1].
1797 {
1798 case 0:
1799 return t;
1800 // Only linear and cubic implemented
1801 default:
1804 MathUtils::pow(t, 2) +
1806 }
1807}
1808
1809std::unordered_map<dof_id_type, Point>
1811{
1812 // Evaluate the geometric map of each element at its nodes with a nodal quadrature rule, as done
1813 // for mortar nodal normals. On curved (higher-order) elements this captures the local surface
1814 // normal at each node rather than a single per-element normal.
1815 const auto dim = input.mesh_dimension();
1816 libMesh::QNodal qrule(dim);
1817 const auto & qweights = qrule.get_weights();
1818 std::unique_ptr<FEBase> fe(FEBase::build(dim, FEType(FIRST, LAGRANGE)));
1819 fe->attach_quadrature_rule(&qrule);
1820 const auto & dxyzdxi = fe->get_dxyzdxi();
1821 const auto & dxyzdeta = fe->get_dxyzdeta();
1822 const auto & q_points = fe->get_xyz();
1823 const Point z_axis(0, 0, 1);
1824
1825 // Sum the JxW-weighted normals of the elements connected to each node
1826 std::unordered_map<dof_id_type, Point> node_normals;
1827 for (const auto & elem : input.element_ptr_range())
1828 {
1829 fe->reinit(elem);
1830 const auto qpoint_to_node = MooseMeshUtils::nodalQuadraturePointToNodeMap(*elem, q_points);
1831 for (const auto qp : index_range(q_points))
1832 {
1833 // The weighted normal JxW * n is the quadrature weight times the area vector
1834 // dxyz/dxi x dxyz/deta in 2D, or the in-plane rotation z x dxyz/dxi of the tangent in 1D.
1835 // Both have the orientation of the element's node ordering, so that the extruded elements
1836 // are not inverted.
1837 Point weighted_normal;
1838 if (dim == 2)
1839 weighted_normal = dxyzdxi[qp].cross(dxyzdeta[qp]);
1840 else
1841 {
1842 // A curve only defines a unique normal within its plane
1843 if (std::abs(dxyzdxi[qp](2)) > libMesh::TOLERANCE * dxyzdxi[qp].norm())
1844 paramError("extrude_along_node_normals",
1845 "Extruding a 1D mesh along node normals requires the mesh to lie in a plane "
1846 "of constant z, but element ",
1847 elem->id(),
1848 " is not.");
1849 weighted_normal = z_axis.cross(dxyzdxi[qp]);
1850 }
1851 node_normals[elem->node_id(qpoint_to_node[qp])] += qweights[qp] * weighted_normal;
1852 }
1853 }
1854
1855 // The input surface mesh is expected to have consistently oriented elements.
1856 for (auto & [node_id, normal] : node_normals)
1857 {
1858 if (normal.norm_sq() == 0)
1859 mooseError("The averaged normal at node ",
1860 node_id,
1861 " is zero; an extrusion direction cannot be determined. This can happen if the "
1862 "connected elements do not have consistent orientations.");
1863 normal = normal.unit();
1864 }
1865
1866 return node_normals;
1867}
registerMooseObjectRenamed("MooseApp", FancyExtruderGenerator, "02/18/2023 24:00", AdvancedExtruderGenerator)
registerMooseObject("MooseApp", AdvancedExtruderGenerator)
unsigned int count
Definition MortarUtils.C:53
char ** sides
char ** blocks
unsigned int dim
void ErrorVector unsigned int
Extrudes a mesh to another dimension.
const Real _twist_pitch
Axial pitch for a full rotation.
const std::vector< Real > & _heights
Height of each elevation.
const std::vector< unsigned int > & _num_layers
Number of layers in each elevation.
std::unordered_map< dof_id_type, Point > computeNodeNormals(const MeshBase &input) const
Compute, for each node, the extrusion direction as the JxW-weighted average of the normals,...
const RealVectorValue _end_extrusion_direction
Extrusion direction for the final layer of the extrusion.
std::vector< std::unordered_map< subdomain_id_type, subdomain_id_type > > _subdomain_swap_pairs
Easier to work with version of _sudomain_swaps.
const std::vector< std::vector< subdomain_id_type > > _upward_boundary_source_blocks
The list of input mesh's blocks that need to be assigned upward boundary interfaces for each layer of...
const Real _start_radial_growth_rate
Derivative of the radial expansion function at the beginning of the extrusion.
const std::vector< std::vector< SubdomainName > > & _subdomain_swaps
Subdomains to swap out for each elevation.
const bool _has_side_boundary
Whether to add the lateral exterior sides (and their nodes) to a boundary.
const RealVectorValue _start_extrusion_direction
Extrusion direction to follow at the start (first layer) of the extrusion.
Point _direction
The direction of the extrusion.
const std::vector< std::vector< boundary_id_type > > & _boundary_swaps
Boundaries to swap out for each elevation.
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
AdvancedExtruderGenerator(const InputParameters &parameters)
const std::vector< Real > _biases
Bias growth factor of each elevation.
const std::vector< std::vector< boundary_id_type > > _upward_boundary_ids
Upward boundary interfaces for each layer of elevation.
const std::vector< std::vector< boundary_id_type > > _downward_boundary_ids
Downward boundary interfaces for each layer of elevation.
const std::vector< std::vector< std::vector< dof_id_type > > > & _elem_integers_swaps
Extra element integers to swap out for each elevation and each element interger name.
static InputParameters validParams()
std::vector< std::unordered_map< boundary_id_type, boundary_id_type > > _boundary_swap_pairs
Easier to work with version of _boundary_swaps.
const bool _extrude_along_node_normals
Whether to extrude each node along the averaged normal of the elements connected to it.
const Real _end_radial_extent
Radial extent of the extruded geometry at the end of the extrusion.
const std::vector< std::string > & _elem_integer_names_to_swap
Names and indices of extra element integers to swap.
bool _extrude_along_curve
Whether we are extruding along a curve.
const Real _end_radial_growth_rate
Derivative of the radial expansion function at the end of the extrusion.
const MooseEnum _radial_expansion_method
Function type for modifying the radial extent of the extruded shape.
Real radialExpansionRatio(const Real t) const
Calculate the share of the radial expansion to apply at the local node This share goes from 0 at the ...
const std::vector< std::vector< subdomain_id_type > > _downward_boundary_source_blocks
The list of input mesh's blocks that need to be assigned downward boundary interfaces for each layer ...
std::vector< unsigned int > _elem_integer_indices_to_swap
std::vector< std::unordered_map< dof_id_type, dof_id_type > > _elem_integers_swap_pairs
Easier to work with version of _elem_integers_swaps.
std::unique_ptr< MeshBase > & _input
Mesh that comes from another generator.
std::unique_ptr< MeshBase > & _extrusion_curve
Curve mesh to extrude along.
const BoundaryName _side_boundary
Boundary for the lateral exterior sides and their nodes.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
This method takes a space delimited list of parameter names and adds them to the specified group name...
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object.
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump.
void addRangeCheckedParam(const std::string &name, const T &value, const std::string &parsed_function, const std::string &doc_string)
MeshGenerators are objects that can modify or add to an existing mesh.
static InputParameters validParams()
std::unique_ptr< MeshBase > buildMeshBaseObject(unsigned int dim=libMesh::invalid_uint)
Build a MeshBase object whose underlying type will be determined by the Mesh input file block.
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition MooseBase.h:457
bool isParamSetByUser(const std::string &name) const
Test if the supplied parameter is set by a user, as opposed to not set or set to default.
Definition MooseBase.h:205
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition MooseBase.h:271
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition MooseBase.h:199
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition MooseEnum.h:55
Provides a way for users to bail out of the current solve.
virtual const char * what() const
Get out the error message.
static const boundary_id_type invalid_id
static const unsigned int max_n_nodes
const std::vector< Real > & get_weights() const
TypeVector< typename CompareTypes< T, T2 >::supertype > cross(const TypeVector< T2 > &v) const
LIBMESH_DEVICE_INLINE void zero()
auto norm_sq() const
MeshBase & mesh
T pow(T x, int e)
Definition MathUtils.h:92
std::set< std::pair< BoundaryName, BoundaryID > > getAllBoundaryNamesAndIDs(const libMesh::MeshBase &mesh)
Get all boundary names and IDs from a mesh.
bool hasBoundaryID(const MeshBase &input_mesh, const BoundaryID id)
Whether a particular boundary ID exists in the mesh.
void extraElemIntegerSwapParametersProcessor(const std::string &class_name, const unsigned int num_sections, const unsigned int num_integers, const std::vector< std::vector< std::vector< dof_id_type > > > &elem_integers_swaps, std::vector< std::unordered_map< dof_id_type, dof_id_type > > &elem_integers_swap_pairs)
Reprocess the elem_integers_swaps into maps so they are easier to use.
bool hasSubdomainName(const MeshBase &input_mesh, const SubdomainName &name)
Whether a particular subdomain name exists in the mesh.
std::set< std::pair< SubdomainName, SubdomainID > > getAllSubdomainNamesAndIDs(const libMesh::MeshBase &mesh)
Get all subdomain names and ids from a mesh.
bool hasSubdomainID(const MeshBase &input_mesh, const SubdomainID &id)
Whether a particular subdomain ID exists in the mesh.
std::vector< BoundaryID > getBoundaryIDs(const libMesh::MeshBase &mesh, const std::vector< BoundaryName > &boundary_name, bool generate_unknown, const std::set< BoundaryID > &mesh_boundary_ids)
Gets the boundary IDs with their names.
std::vector< unsigned int > nodalQuadraturePointToNodeMap(const Elem &elem, const std::vector< Point > &q_points)
Match the points of a nodal quadrature rule (e.g.
void swapNodesInElem(Elem &elem, const unsigned int nd1, const unsigned int nd2)
Swap two nodes within an element.
void idSwapParametersProcessor(const std::string &class_name, const std::string &id_name, const std::vector< std::vector< T > > &id_swaps, std::vector< std::unordered_map< T, T > > &id_swap_pairs, const unsigned int row_index_shift=0)
Reprocess the swap related input parameters to make pairs out of them to ease further processing.
SubdomainID getNextFreeSubdomainID(MeshBase &input_mesh)
Checks input mesh and returns max(block ID) + 1, which represents a block ID that is not currently in...
SubdomainID getSubdomainID(const SubdomainName &subdomain_name, const MeshBase &mesh)
Gets the subdomain ID associated with the given SubdomainName.
Point meshCentroidCalculator(const MeshBase &mesh)
Calculates the centroid of a MeshBase.
Real computeMaxDistanceToAxis(const MeshBase &mesh, const Point &origin, const RealVectorValue &direction)
Computes the maximum distance from all nodes of a mesh to a general axis.
std::string stringify(const T &t)
conversion to string
Definition Conversion.h:65
void elem_types(const MeshBase &mesh, std::vector< ElemType > &et)
Real volume(const MeshBase &mesh, unsigned int dim=libMesh::invalid_uint)
const unsigned int invalid_uint
const Real pi
static constexpr Real TOLERANCE
MooseUnits pow(const MooseUnits &, int)
Definition Units.C:537
const boundary_id_type top_id