https://mooseframework.inl.gov
Loading...
Searching...
No Matches
MooseMeshUtils.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
10// MOOSE includes
11#include "MooseMeshUtils.h"
12
13#include "libmesh/elem.h"
14#include "libmesh/boundary_info.h"
15#include "libmesh/id_types.h"
16#include "libmesh/int_range.h"
17#include "libmesh/parallel.h"
18#include "libmesh/parallel_algebra.h"
19#include "libmesh/utility.h"
20
21#include "libmesh/distributed_mesh.h"
22#include "libmesh/parallel_elem.h"
23#include "libmesh/parallel_node.h"
24#include "libmesh/compare_elems_by_level.h"
25#include "libmesh/mesh_communication.h"
26#include "libmesh/edge_edge3.h"
27#include "libmesh/enum_to_string.h"
28#include "libmesh/unstructured_mesh.h"
29
30#include "timpi/parallel_sync.h"
31
32namespace MooseMeshUtils
33{
34
35void
37{
38 // We check if we have the same boundary name with different IDs. If we do, we assign the
39 // first ID to every occurrence.
40 const auto & side_bd_name_map = mesh.get_boundary_info().get_sideset_name_map();
41 const auto & node_bd_name_map = mesh.get_boundary_info().get_nodeset_name_map();
42 std::map<boundary_id_type, boundary_id_type> same_name_ids;
43
44 auto populate_map = [](const std::map<boundary_id_type, std::string> & map,
45 std::map<boundary_id_type, boundary_id_type> & same_ids)
46 {
47 for (const auto & pair_outer : map)
48 for (const auto & pair_inner : map)
49 // The last condition is needed to make sure we only store one combination
50 if (pair_outer.second == pair_inner.second && pair_outer.first != pair_inner.first &&
51 same_ids.find(pair_inner.first) == same_ids.end())
52 same_ids[pair_outer.first] = pair_inner.first;
53 };
54
55 populate_map(side_bd_name_map, same_name_ids);
56 populate_map(node_bd_name_map, same_name_ids);
57
58 for (const auto & [id1, id2] : same_name_ids)
59 mesh.get_boundary_info().renumber_id(id2, id1);
60}
61
62void
63changeBoundaryId(MeshBase & mesh,
64 const boundary_id_type old_id,
65 const boundary_id_type new_id,
66 bool delete_prev)
67{
68 // Get a reference to our BoundaryInfo object, we will use it several times below...
69 BoundaryInfo & boundary_info = mesh.get_boundary_info();
70
71 // Container to catch ids passed back from BoundaryInfo
72 std::vector<boundary_id_type> old_ids;
73
74 // Only level-0 elements store BCs. Loop over them.
75 for (auto & elem : as_range(mesh.level_elements_begin(0), mesh.level_elements_end(0)))
76 {
77 unsigned int n_sides = elem->n_sides();
78 for (const auto s : make_range(n_sides))
79 {
80 boundary_info.boundary_ids(elem, s, old_ids);
81 if (std::find(old_ids.begin(), old_ids.end(), old_id) != old_ids.end())
82 {
83 std::vector<boundary_id_type> new_ids(old_ids);
84 std::replace(new_ids.begin(), new_ids.end(), old_id, new_id);
85 if (delete_prev)
86 {
87 boundary_info.remove_side(elem, s);
88 boundary_info.add_side(elem, s, new_ids);
89 }
90 else
91 boundary_info.add_side(elem, s, new_ids);
92 }
93 }
94 }
95
96 // Remove any remaining references to the old ID from the
97 // BoundaryInfo object. This prevents things like empty sidesets
98 // from showing up when printing information, etc.
99 if (delete_prev)
100 boundary_info.remove_id(old_id);
101
102 // global information may now be out of sync
103 mesh.unset_is_prepared();
104}
105
106std::vector<boundary_id_type>
107getBoundaryIDs(const MeshBase & mesh,
108 const std::vector<BoundaryName> & boundary_name,
109 bool generate_unknown)
110{
111 return getBoundaryIDs(
112 mesh, boundary_name, generate_unknown, mesh.get_boundary_info().get_boundary_ids());
113}
114
115std::vector<boundary_id_type>
116getBoundaryIDs(const MeshBase & mesh,
117 const std::vector<BoundaryName> & boundary_name,
118 bool generate_unknown,
119 const std::set<BoundaryID> & mesh_boundary_ids)
120{
121 const BoundaryInfo & boundary_info = mesh.get_boundary_info();
122 const std::map<BoundaryID, std::string> & sideset_map = boundary_info.get_sideset_name_map();
123 const std::map<BoundaryID, std::string> & nodeset_map = boundary_info.get_nodeset_name_map();
124
125 /* It is required to generate a new ID for a given name. It is used often in mesh modifiers such
126 * as SideSetsBetweenSubdomains. Then we need to check the current boundary ids since they are
127 * changing during "mesh modify()", and figure out the right max boundary ID. Most of mesh
128 * modifiers are running in serial, and we won't involve a global communication.
129 */
130 BoundaryID max_boundary_id = 0;
131 if (generate_unknown)
132 {
133 bool has_boundary_id_sets = mesh.preparation().has_boundary_id_sets;
134
135 // Ideally, this requirement should be able to be enforced earlier when we
136 // get the name maps. However, that preparedness check on sideset and
137 // nodeset maps doesn't exist yet.
138 if (!has_boundary_id_sets)
139 libmesh_parallel_only(mesh.comm());
140
141 const auto & bids = has_boundary_id_sets ? boundary_info.get_global_boundary_ids()
142 : boundary_info.get_boundary_ids();
143 if (bids.size())
144 max_boundary_id = *(bids.rbegin());
145 if (!has_boundary_id_sets)
146 mesh.comm().max(max_boundary_id);
147 }
148
149 std::vector<BoundaryID> ids(boundary_name.size());
150 for (const auto i : index_range(boundary_name))
151 {
152 if (boundary_name[i] == "ANY_BOUNDARY_ID")
153 {
154 ids.assign(mesh_boundary_ids.begin(), mesh_boundary_ids.end());
155 if (i)
156 mooseWarning("You passed \"ANY_BOUNDARY_ID\" in addition to other boundary_names. This "
157 "may be a logic error.");
158 break;
159 }
160
161 if (boundary_name[i].empty() && !generate_unknown)
162 mooseError("Incoming boundary name is empty and we are not generating unknown boundary IDs. "
163 "This is invalid.");
164
165 BoundaryID id;
166
167 if (boundary_name[i].empty() || !MooseUtils::isDigits(boundary_name[i]))
168 {
174 if (generate_unknown &&
175 !MooseUtils::doesMapContainValue(sideset_map, std::string(boundary_name[i])) &&
176 !MooseUtils::doesMapContainValue(nodeset_map, std::string(boundary_name[i])))
177 id = ++max_boundary_id;
178 else
179 id = boundary_info.get_id_by_name(boundary_name[i]);
180 }
181 else
182 id = getIDFromName<BoundaryName, BoundaryID>(boundary_name[i]);
183
184 ids[i] = id;
185 }
186
187 return ids;
188}
189
190std::set<BoundaryID>
191getBoundaryIDSet(const MeshBase & mesh,
192 const std::vector<BoundaryName> & boundary_name,
193 bool generate_unknown)
194{
195 auto boundaries = getBoundaryIDs(mesh, boundary_name, generate_unknown);
196 return std::set<BoundaryID>(boundaries.begin(), boundaries.end());
197}
198
199std::set<std::pair<BoundaryName, BoundaryID>>
200getAllBoundaryNamesAndIDs(const MeshBase & mesh)
201{
202 std::set<std::pair<BoundaryName, BoundaryID>> boundaries;
203 const auto & binfo = mesh.get_boundary_info();
204 const auto & boundary_ids = binfo.get_boundary_ids();
205 for (const auto id : boundary_ids)
206 {
207 // if neither a sideset or nodeset, then empty
208 if (binfo.get_sideset_name(id).size())
209 boundaries.insert(std::make_pair(binfo.get_sideset_name(id), id));
210 else
211 boundaries.insert(std::make_pair(binfo.get_nodeset_name(id), id));
212 }
213 return boundaries;
214}
215
216std::vector<subdomain_id_type>
217getSubdomainIDs(const MeshBase & mesh, const std::vector<SubdomainName> & subdomain_names)
218{
219 std::vector<subdomain_id_type> ids;
220
221 // shortcut for "ANY_BLOCK_ID"
222 if (subdomain_names.size() == 1 && subdomain_names[0] == "ANY_BLOCK_ID")
223 {
224 // since get_mesh_subdomains() requires a prepared mesh, we need to check that here
225 mooseAssert(mesh.is_prepared(),
226 "getSubdomainIDs() should only be called on a prepared mesh if ANY_BLOCK_ID is "
227 "used to query all block IDs");
228 ids.assign(mesh.get_mesh_subdomains().begin(), mesh.get_mesh_subdomains().end());
229 return ids;
230 }
231
232 // loop through subdomain names and get IDs (this preserves the order of subdomain_names)
233 ids.resize(subdomain_names.size());
234 for (auto i : index_range(subdomain_names))
235 {
236 if (subdomain_names[i] == "ANY_BLOCK_ID")
237 mooseError("getSubdomainIDs() accepts \"ANY_BLOCK_ID\" if and only if it is the only "
238 "subdomain name being queried.");
239 ids[i] = MooseMeshUtils::getSubdomainID(subdomain_names[i], mesh);
240 }
241
242 return ids;
243}
244
245std::set<subdomain_id_type>
246getSubdomainIDs(const MeshBase & mesh, const std::set<SubdomainName> & subdomain_names)
247{
248 const auto blk_ids = getSubdomainIDs(
249 mesh, std::vector<SubdomainName>(subdomain_names.begin(), subdomain_names.end()));
250 return {blk_ids.begin(), blk_ids.end()};
251}
252
253std::set<std::pair<SubdomainName, SubdomainID>>
254getAllSubdomainNamesAndIDs(const MeshBase & mesh)
255{
256 std::set<std::pair<SubdomainName, SubdomainID>> subdomains;
257 std::set<subdomain_id_type> subdomain_ids;
258 mesh.subdomain_ids(subdomain_ids, /*global*/ true);
259 for (const auto id : subdomain_ids)
260 subdomains.insert(std::make_pair(mesh.subdomain_name(id), id));
261 return subdomains;
262}
263
265getBoundaryID(const BoundaryName & boundary_name, const MeshBase & mesh)
266{
268 if (boundary_name.empty())
269 return id;
270
271 if (!MooseUtils::isDigits(boundary_name))
272 id = mesh.get_boundary_info().get_id_by_name(boundary_name);
273 else
274 id = getIDFromName<BoundaryName, BoundaryID>(boundary_name);
275
276 return id;
277}
278
280getSubdomainID(const SubdomainName & subdomain_name, const MeshBase & mesh)
281{
282 if (subdomain_name == "ANY_BLOCK_ID")
283 mooseError("getSubdomainID() does not work with \"ANY_BLOCK_ID\"");
284
286 if (subdomain_name.empty())
287 return id;
288
289 if (!MooseUtils::isDigits(subdomain_name))
290 id = mesh.get_id_by_name(subdomain_name);
291 else
292 id = getIDFromName<SubdomainName, SubdomainID>(subdomain_name);
293
294 return id;
295}
296
297void
298changeSubdomainId(MeshBase & mesh, const subdomain_id_type old_id, const subdomain_id_type new_id)
299{
300 for (const auto & elem : mesh.element_ptr_range())
301 if (elem->subdomain_id() == old_id)
302 elem->subdomain_id() = new_id;
303
304 // global cached information may now be out of sync
305 mesh.unset_is_prepared();
306}
307
308Point
309meshCentroidCalculator(const MeshBase & mesh)
310{
311 Point centroid_pt = Point(0.0, 0.0, 0.0);
312 Real vol_tmp = 0.0;
313 for (const auto & elem : mesh.active_local_element_ptr_range())
314 {
315 Real elem_vol = elem->volume();
316 centroid_pt += (elem->true_centroid()) * elem_vol;
317 vol_tmp += elem_vol;
318 }
319 mesh.comm().sum(centroid_pt);
320 mesh.comm().sum(vol_tmp);
321 centroid_pt /= vol_tmp;
322 return centroid_pt;
323}
324
325Point
326boundaryCentroidCalculator(const BoundaryName & boundary, MeshBase & mesh)
327{
328 // Need boundaries to be synchronized
329 if (!mesh.preparation().has_boundary_id_sets)
330 mesh.get_boundary_info().synchronize_global_id_set();
331 BoundaryInfo & mesh_boundary_info = mesh.get_boundary_info();
332 boundary_id_type boundary_id = mesh_boundary_info.get_id_by_name(boundary);
333 const auto side_list = mesh_boundary_info.build_side_list();
334
335 // Initialize sums
336 Real volume_sum = 0;
337 Point volume_weighted_centroid_sum(0, 0, 0);
338
339 for (const auto & [eid, side_i, bid] : side_list)
340 {
341 if (bid != boundary_id)
342 continue;
343
344 // Get the side
345 const auto elem = mesh.elem_ptr(eid);
346 const auto side = elem->side_ptr(side_i);
347
348 volume_sum += side->volume();
349 volume_weighted_centroid_sum += side->volume() * side->true_centroid();
350 }
351 // Sum across processes
352 mesh.comm().sum(volume_weighted_centroid_sum);
353 mesh.comm().sum(volume_sum);
354
355 if (volume_sum == 0)
356 {
357 mooseWarning("Boundary centroid was requested but boundary '" + boundary + "' is empty.");
358 return Point(0.);
359 }
360 return volume_weighted_centroid_sum / volume_sum;
361}
362
363RealVectorValue
364boundaryWeightedNormal(const BoundaryName & boundary, MeshBase & mesh)
365{
366 // Need boundaries to be synchronized
367 if (!mesh.preparation().has_boundary_id_sets)
368 mesh.get_boundary_info().synchronize_global_id_set();
369 BoundaryInfo & mesh_boundary_info = mesh.get_boundary_info();
370 boundary_id_type boundary_id = mesh_boundary_info.get_id_by_name(boundary);
371 const auto side_list = mesh_boundary_info.build_side_list();
372
373 // Initialize sums
374 Real volume_sum = 0;
375 RealVectorValue volume_weighted_normal_sum(0, 0, 0);
376
377 for (const auto & [eid, side_i, bid] : side_list)
378 {
379 if (bid != boundary_id)
380 continue;
381
382 // Get the side
383 const auto elem = mesh.elem_ptr(eid);
384 const auto side = elem->side_ptr(side_i);
385
386 volume_sum += side->volume();
387 volume_weighted_normal_sum += side->volume() * elem->side_vertex_average_normal(side_i);
388 }
389 // Sum across processes
390 mesh.comm().sum(volume_weighted_normal_sum);
391 mesh.comm().sum(volume_sum);
392
393 return volume_weighted_normal_sum / volume_sum;
394}
395
396Real
397computeMaxDistanceToAxis(const MeshBase & mesh,
398 const Point & origin,
399 const RealVectorValue & direction)
400{
401 Real distance = 0;
402 mooseAssert(MooseUtils::absoluteFuzzyEqual(direction.norm_sq(), 1),
403 "Direction should be normalized");
404 for (const auto & node : mesh.node_ptr_range())
405 if (const auto dist_node = (*node - origin).cross(direction).norm(); dist_node > distance)
406 distance = dist_node;
407 mesh.comm().max(distance);
408 return distance;
409}
410
411std::unordered_map<dof_id_type, dof_id_type>
412getExtraIDUniqueCombinationMap(const MeshBase & mesh,
413 const std::set<SubdomainID> & block_ids,
414 std::vector<ExtraElementIDName> extra_ids)
415{
416 // check block restriction
417 const bool block_restricted = !block_ids.empty();
418 // get element id name of interest in recursive parsing algorithm
419 ExtraElementIDName id_name = extra_ids.back();
420 extra_ids.pop_back();
421 const auto id_index = mesh.get_elem_integer_index(id_name);
422
423 // create base parsed id set
424 if (extra_ids.empty())
425 {
426 // get set of extra id values;
427 std::vector<dof_id_type> ids;
428 {
429 std::set<dof_id_type> ids_set;
430 for (const auto & elem : mesh.active_element_ptr_range())
431 {
432 if (block_restricted && block_ids.find(elem->subdomain_id()) == block_ids.end())
433 continue;
434 const auto id = elem->get_extra_integer(id_index);
435 ids_set.insert(id);
436 }
437 mesh.comm().set_union(ids_set);
438 ids.assign(ids_set.begin(), ids_set.end());
439 }
440
441 // determine new extra id values;
442 std::unordered_map<dof_id_type, dof_id_type> parsed_ids;
443 for (auto & elem : mesh.active_element_ptr_range())
444 {
445 if (block_restricted && block_ids.find(elem->subdomain_id()) == block_ids.end())
446 continue;
447 parsed_ids[elem->id()] = std::distance(
448 ids.begin(), std::lower_bound(ids.begin(), ids.end(), elem->get_extra_integer(id_index)));
449 }
450 return parsed_ids;
451 }
452
453 // if extra_ids is not empty, recursively call getExtraIDUniqueCombinationMap
454 const auto base_parsed_ids =
456 // parsing extra ids based on ref_parsed_ids
457 std::vector<std::pair<dof_id_type, dof_id_type>> unique_ids;
458 {
459 std::set<std::pair<dof_id_type, dof_id_type>> unique_ids_set;
460 for (const auto & elem : mesh.active_element_ptr_range())
461 {
462 if (block_restricted && block_ids.find(elem->subdomain_id()) == block_ids.end())
463 continue;
464 const dof_id_type id1 = libmesh_map_find(base_parsed_ids, elem->id());
465 const dof_id_type id2 = elem->get_extra_integer(id_index);
466 const std::pair<dof_id_type, dof_id_type> ids = std::make_pair(id1, id2);
467 unique_ids_set.insert(ids);
468 }
469 mesh.comm().set_union(unique_ids_set);
470 unique_ids.assign(unique_ids_set.begin(), unique_ids_set.end());
471 }
472
473 std::unordered_map<dof_id_type, dof_id_type> parsed_ids;
474
475 for (const auto & elem : mesh.active_element_ptr_range())
476 {
477 if (block_restricted && block_ids.find(elem->subdomain_id()) == block_ids.end())
478 continue;
479 const dof_id_type id1 = libmesh_map_find(base_parsed_ids, elem->id());
480 const dof_id_type id2 = elem->get_extra_integer(id_index);
481 const dof_id_type new_id = std::distance(
482 unique_ids.begin(),
483 std::lower_bound(unique_ids.begin(), unique_ids.end(), std::make_pair(id1, id2)));
484 parsed_ids[elem->id()] = new_id;
485 }
486
487 return parsed_ids;
488}
489
490bool
491isCoPlanar(const std::vector<Point> & vec_pts, const Point plane_nvec, const Point fixed_pt)
492{
493 for (const auto & pt : vec_pts)
494 if (!MooseUtils::absoluteFuzzyEqual((pt - fixed_pt) * plane_nvec, 0.0))
495 return false;
496 return true;
497}
498
499bool
500isCoPlanar(const std::vector<Point> & vec_pts, const Point plane_nvec)
501{
502 return isCoPlanar(vec_pts, plane_nvec, vec_pts.front());
503}
504
505bool
506isCoPlanar(const std::vector<Point> & vec_pts)
507{
508 // Assuming that overlapped Points are allowed, the Points that are overlapped with vec_pts[0] are
509 // removed before further calculation.
510 std::vector<Point> vec_pts_nonzero{vec_pts[0]};
511 for (const auto i : index_range(vec_pts))
512 if (!MooseUtils::absoluteFuzzyEqual((vec_pts[i] - vec_pts[0]).norm(), 0.0))
513 vec_pts_nonzero.push_back(vec_pts[i]);
514 // 3 or fewer points are always coplanar
515 if (vec_pts_nonzero.size() <= 3)
516 return true;
517 else
518 {
519 for (const auto i : make_range(vec_pts_nonzero.size() - 1))
520 {
521 const Point tmp_pt = (vec_pts_nonzero[i] - vec_pts_nonzero[0])
522 .cross(vec_pts_nonzero[i + 1] - vec_pts_nonzero[0]);
523 // if the three points are not collinear, use cross product as the normal vector of the plane
524 if (!MooseUtils::absoluteFuzzyEqual(tmp_pt.norm(), 0.0))
525 return isCoPlanar(vec_pts_nonzero, tmp_pt.unit());
526 }
527 }
528 // If all the points are collinear, they are also coplanar
529 return true;
530}
531
533getNextFreeSubdomainID(MeshBase & input_mesh)
534{
535 // Call this to get most up to date block id information
536 input_mesh.cache_elem_data();
537
538 std::set<SubdomainID> preexisting_subdomain_ids;
539 input_mesh.subdomain_ids(preexisting_subdomain_ids);
540 if (preexisting_subdomain_ids.empty())
541 return 0;
542 else
543 {
544 const auto highest_subdomain_id =
545 *std::max_element(preexisting_subdomain_ids.begin(), preexisting_subdomain_ids.end());
546 mooseAssert(highest_subdomain_id < std::numeric_limits<SubdomainID>::max(),
547 "A SubdomainID with max possible value was found");
548 return highest_subdomain_id + 1;
549 }
550}
551
553getNextFreeBoundaryID(MeshBase & input_mesh)
554{
555 if (!input_mesh.preparation().has_boundary_id_sets)
556 input_mesh.get_boundary_info().regenerate_id_sets();
557
558 auto boundary_ids = input_mesh.get_boundary_info().get_boundary_ids();
559 if (boundary_ids.empty())
560 return 0;
561 return (*boundary_ids.rbegin() + 1);
562}
563
564bool
565hasSubdomainID(const MeshBase & input_mesh, const SubdomainID & id)
566{
567 std::set<SubdomainID> mesh_blocks;
568 input_mesh.subdomain_ids(mesh_blocks);
569
570 // On a distributed mesh we may have sideset IDs that only exist on
571 // other processors
572 if (!input_mesh.is_replicated())
573 input_mesh.comm().set_union(mesh_blocks);
574
575 return mesh_blocks.count(id) && (id != Moose::INVALID_BLOCK_ID);
576}
577
578bool
579hasSubdomainName(const MeshBase & input_mesh, const SubdomainName & name)
580{
581 const auto id = getSubdomainID(name, input_mesh);
582 return hasSubdomainID(input_mesh, id);
583}
584
585bool
586hasBoundaryID(const MeshBase & input_mesh, const BoundaryID id)
587{
588 const BoundaryInfo & boundary_info = input_mesh.get_boundary_info();
589 std::set<boundary_id_type> boundary_ids = boundary_info.get_boundary_ids();
590
591 // On a distributed mesh we may have boundary IDs that only exist on
592 // other processors
593 if (!input_mesh.is_replicated())
594 input_mesh.comm().set_union(boundary_ids);
595
596 return boundary_ids.count(id) && (id != Moose::INVALID_BOUNDARY_ID);
597}
598
599bool
600hasBoundaryName(const MeshBase & mesh, const BoundaryName & name)
601{
602 const BoundaryInfo & boundary_info = mesh.get_boundary_info();
603 const BoundaryID id = boundary_info.get_id_by_name(name);
604 return id != Moose::INVALID_BOUNDARY_ID;
605}
606
607bool
608hasBoundaryNameOrID(const MeshBase & mesh, const BoundaryName & name_or_id)
609{
610 const auto id = getBoundaryID(name_or_id, mesh);
611 return hasBoundaryID(mesh, id);
612}
613
614void
615makeOrderedNodeList(std::vector<std::pair<dof_id_type, dof_id_type>> & node_assm,
616 std::vector<dof_id_type> & elem_id_list,
617 std::vector<dof_id_type> & midpoint_node_list,
618 std::vector<dof_id_type> & ordered_node_list,
619 std::vector<dof_id_type> & ordered_elem_id_list)
620{
621 // a flag to indicate if the ordered_node_list has been reversed
622 bool is_flipped = false;
623 // Start from the first element, try to find a chain of nodes
624 mooseAssert(node_assm.size(), "Node list must not be empty");
625 ordered_node_list.push_back(node_assm.front().first);
626 if (midpoint_node_list.front() != DofObject::invalid_id)
627 ordered_node_list.push_back(midpoint_node_list.front());
628 ordered_node_list.push_back(node_assm.front().second);
629 ordered_elem_id_list.push_back(elem_id_list.front());
630 // Remove the element that has just been added to ordered_node_list
631 node_assm.erase(node_assm.begin());
632 midpoint_node_list.erase(midpoint_node_list.begin());
633 elem_id_list.erase(elem_id_list.begin());
634 const unsigned int node_assm_size_0 = node_assm.size();
635 for (unsigned int i = 0; i < node_assm_size_0; i++)
636 {
637 // Find nodes to expand the chain
638 dof_id_type end_node_id = ordered_node_list.back();
639 auto isMatch1 = [end_node_id](std::pair<dof_id_type, dof_id_type> old_id_pair)
640 { return old_id_pair.first == end_node_id; };
641 auto isMatch2 = [end_node_id](std::pair<dof_id_type, dof_id_type> old_id_pair)
642 { return old_id_pair.second == end_node_id; };
643 auto result = std::find_if(node_assm.begin(), node_assm.end(), isMatch1);
644 bool match_first;
645 if (result == node_assm.end())
646 {
647 match_first = false;
648 result = std::find_if(node_assm.begin(), node_assm.end(), isMatch2);
649 }
650 else
651 {
652 match_first = true;
653 }
654 // If found, add the node to boundary_ordered_node_list
655 if (result != node_assm.end())
656 {
657 const auto elem_index = std::distance(node_assm.begin(), result);
658 if (midpoint_node_list[elem_index] != DofObject::invalid_id)
659 ordered_node_list.push_back(midpoint_node_list[elem_index]);
660 ordered_node_list.push_back(match_first ? (*result).second : (*result).first);
661 node_assm.erase(result);
662 midpoint_node_list.erase(midpoint_node_list.begin() + elem_index);
663 ordered_elem_id_list.push_back(elem_id_list[elem_index]);
664 elem_id_list.erase(elem_id_list.begin() + elem_index);
665 }
666 // If there are still elements in node_assm and result ==
667 // node_assm.end(), this means the curve is not a loop, the
668 // ordered_node_list is flipped and try the other direction that has not
669 // been examined yet.
670 else
671 {
672 if (is_flipped)
673 // Flipped twice; this means the node list has at least two segments.
674 throw MooseException("The node list provided has more than one segments.");
675
676 // mark the first flip event.
677 is_flipped = true;
678 std::reverse(ordered_node_list.begin(), ordered_node_list.end());
679 std::reverse(midpoint_node_list.begin(), midpoint_node_list.end());
680 std::reverse(ordered_elem_id_list.begin(), ordered_elem_id_list.end());
681 // As this iteration is wasted, set the iterator backward
682 i--;
683 }
684 }
685}
686
687void
688makeOrderedNodeList(std::vector<std::pair<dof_id_type, dof_id_type>> & node_assm,
689 std::vector<dof_id_type> & elem_id_list,
690 std::vector<dof_id_type> & ordered_node_list,
691 std::vector<dof_id_type> & ordered_elem_id_list)
692{
693 std::vector<dof_id_type> dummy_midpoint_node_list(node_assm.size(), DofObject::invalid_id);
695 node_assm, elem_id_list, dummy_midpoint_node_list, ordered_node_list, ordered_elem_id_list);
696}
697
698void
699swapNodesInElem(Elem & elem, const unsigned int nd1, const unsigned int nd2)
700{
701 Node * n_temp = elem.node_ptr(nd1);
702 elem.set_node(nd1, elem.node_ptr(nd2));
703 elem.set_node(nd2, n_temp);
704}
705
706void
708 const std::string & class_name,
709 const unsigned int num_sections,
710 const unsigned int num_integers,
711 const std::vector<std::vector<std::vector<dof_id_type>>> & elem_integers_swaps,
712 std::vector<std::unordered_map<dof_id_type, dof_id_type>> & elem_integers_swap_pairs)
713{
714 elem_integers_swap_pairs.reserve(num_sections * num_integers);
715 for (const auto i : make_range(num_integers))
716 {
717 const auto & elem_integer_swaps = elem_integers_swaps[i];
718 std::vector<std::unordered_map<dof_id_type, dof_id_type>> elem_integer_swap_pairs;
719 try
720 {
722 "elem_integers_swaps",
723 elem_integer_swaps,
724 elem_integer_swap_pairs,
725 i * num_sections);
726 }
727 catch (const MooseException & e)
728 {
729 throw MooseException(e.what());
730 }
731
732 elem_integers_swap_pairs.insert(elem_integers_swap_pairs.end(),
733 elem_integer_swap_pairs.begin(),
734 elem_integer_swap_pairs.end());
735 }
736}
737
738std::unique_ptr<ReplicatedMesh>
739buildBoundaryMesh(const MeshBase & input_mesh, const boundary_id_type boundary_id)
740{
741 if (!input_mesh.is_serial())
742 ::mooseError("Input mesh should be serialized for extracting the boundary mesh.\nInput mesh:" +
743 input_mesh.get_info());
744 auto poly_mesh = std::make_unique<ReplicatedMesh>(input_mesh.comm());
745
746 auto side_list = input_mesh.get_boundary_info().build_side_list();
747
748 std::unordered_map<dof_id_type, dof_id_type> old_new_node_map;
749 for (const auto & [eid, side_i, bid] : side_list)
750 {
751 if (bid != boundary_id)
752 continue;
753
754 // Get the side
755 const auto elem = input_mesh.elem_ptr(eid);
756 const auto side = elem->side_ptr(side_i);
757 auto side_elem = elem->build_side_ptr(side_i);
758 auto copy = side_elem->build(side_elem->type());
759
760 for (const auto i : side_elem->node_index_range())
761 {
762 auto & n = side_elem->node_ref(i);
763
764 if (old_new_node_map.count(n.id()))
765 copy->set_node(i, poly_mesh->node_ptr(old_new_node_map[n.id()]));
766 else
767 {
768 Node * node = poly_mesh->add_point(side_elem->point(i));
769 copy->set_node(i, node);
770 old_new_node_map[n.id()] = node->id();
771 }
772 }
773 poly_mesh->add_elem(copy.release());
774 }
775 poly_mesh->skip_partitioning(true);
776 poly_mesh->prepare_for_use();
777 if (poly_mesh->n_elem() == 0)
778 mooseError("The input mesh to extract the boundary from does not have a boundary with id ",
779 boundary_id,
780 ".\n",
781 input_mesh);
782
783 return poly_mesh;
784}
785
786std::unique_ptr<ReplicatedMesh>
787buildLoopBoundaryOf2DMesh(const MeshBase & input_mesh, const boundary_id_type boundary_id)
788{
789 if (!input_mesh.is_serial())
791 "Input 2D mesh should be serialized for extracting the loop boundary mesh.\nInput mesh:" +
792 input_mesh.get_info());
793 auto edge_mesh = std::make_unique<ReplicatedMesh>(input_mesh.comm());
794 auto side_list = input_mesh.get_boundary_info().build_side_list();
795 std::set<BoundaryInfo::BCTuple> visited;
796 bool already_seen_this_side_tuple = false;
797 BoundaryInfo::BCTuple first_side_visited = {libMesh::invalid_uint, 0, 0};
798
799 // Helps move elem to elem at a given node
800 const auto node_to_elem_map = buildBoundaryNodeToElemMap(input_mesh, boundary_id);
801 // Helps check if a node is part of a boundary
802 const auto & node_to_bids = input_mesh.get_boundary_info().get_nodeset_map();
803
804 // Traverse from the first side (edge) in the side_list that matches the boundary_id
805 for (const auto & bside : side_list)
806 {
807 if (std::get<2>(bside) != boundary_id)
808 continue;
809
810 // Check that we are not starting 'another' loop
811 if (bside != first_side_visited)
812 {
813 if (visited.size() && !visited.count(bside))
815 "Boundary " + std::to_string(boundary_id) +
816 " is not a (contiguous) loop. Boundary side: (" + Moose::stringify(bside) +
817 ") was not visited after a single pass around the boundary. Boundary sides visited: " +
818 Moose::stringify(visited));
819 else if (visited.empty())
820 first_side_visited = bside;
821 else
822 continue;
823 }
824
825 // Form the element to be able to find the side
826 // These three variables will be updated while traversing the loop boundary
827 const Elem * elem = input_mesh.elem_ptr(std::get<0>(bside));
828 auto current_side = std::get<1>(bside);
829 auto side_elem = elem->build_side_ptr(current_side);
830
831 // 3D elements should not be part of this boundary
832 if (elem->dim() != 2)
834 "Finding the loop boundary of a 2D mesh cannot be done with non-2D elements such as ",
835 *elem);
836
837 // Start from node 0 of the side (on the boundary), set the next node as the other node
838 // one that side, and keep going from tht next node
839 bool looped_back = false;
840 const Node * starting_node = side_elem->node_ptr(0);
841 const auto new_mesh_starting_node = edge_mesh->add_point(side_elem->point(0));
842 Node * new_first_node = new_mesh_starting_node;
843 [[maybe_unused]] dof_id_type first_node_index = starting_node->id();
844 dof_id_type second_node_index = input_mesh.node_ptr(side_elem->node_id(1))->id();
845
846 while (!looped_back && !already_seen_this_side_tuple)
847 {
848 if (MooseUtils::absoluteFuzzyEqual(input_mesh.point(second_node_index),
849 Point(*starting_node)))
850 looped_back = true;
851
852 // Get the opposite node (the next node) and add it to the edge mesh
853 Node * new_second_node = looped_back
854 ? new_mesh_starting_node
855 : edge_mesh->add_point(input_mesh.point(second_node_index));
856
857 // Add a copy of the edge side element to the mesh
858 side_elem = elem->build_side_ptr(current_side);
859 auto copy = side_elem->build(side_elem->type());
860 copy->set_node(0, new_first_node);
861 copy->set_node(1, new_second_node);
862 edge_mesh->add_elem(copy.release());
863
864 // Make this side as 'visited'
865 std::tuple<dof_id_type, unsigned short int, boundary_id_type> bc_tuple = {
866 elem->id(), current_side, boundary_id};
867 const auto & visit_iter = visited.insert(bc_tuple);
868 if (!looped_back && !visit_iter.second)
869 already_seen_this_side_tuple = true;
870
871 // Find the next element and side_elem
872 auto & connected_elems = libmesh_map_find(node_to_elem_map, second_node_index);
873 bool found_match = false;
874 const auto current_eid = elem->id();
875
876 for (const auto eid : connected_elems)
877 {
878 mooseAssert(!found_match,
879 "We should only find one node on a connected element on this boundary");
880 if (eid != current_eid)
881 {
882 // Update the element (on the input mesh)
883 elem = input_mesh.elem_ptr(eid);
884
885 // Find the side and the opposite node index in that side
886 for (const auto si : elem->side_index_range())
887 {
888 // Check that second node is on the side
889 const auto local_second_node_index =
890 elem->get_node_index(input_mesh.node_ptr(second_node_index));
891 // 2 sides should match this
892 if (elem->is_node_on_side(local_second_node_index, si))
893 {
894 // Only one side should be on the same boundary (node is connected to two elements)
895 // Form a bc_tuple and check the list of boundary sides
896 std::tuple<dof_id_type, unsigned short int, boundary_id_type> side_bc_tuple = {
897 elem->id(), si, boundary_id};
898
899 if (std::find(side_list.begin(), side_list.end(), side_bc_tuple) == side_list.end())
900 continue;
901
902 // We are on the right boundary, just need to get the other node
903 for (const auto local_side_node_id : elem->nodes_on_side(si))
904 {
905 const auto side_node_id = elem->node_id(local_side_node_id);
906
907 // Skip current node (use global index to compare)
908 if (side_node_id == second_node_index)
909 continue;
910 mooseAssert(side_node_id != first_node_index,
911 "Somehow looped back in a single element");
912
913 current_side = si;
914 second_node_index = side_node_id;
915 found_match = true;
916 break;
917 }
918 }
919 // No need to examine more sides
920 if (found_match)
921 break;
922 }
923
924 // No need to examine more elements
925 if (found_match)
926 break;
927 }
928 // next node could be on the same element, just moving on to the next side
929 else if (connected_elems.size() == 1)
930 {
931 elem = input_mesh.elem_ptr(eid);
932 const auto local_second_node_index =
933 elem->get_node_index(input_mesh.node_ptr(second_node_index));
934
935 // Move on to the next side
936 for (const auto si : elem->side_index_range())
937 if (si != current_side && elem->is_node_on_side(local_second_node_index, si))
938 {
939 // Check all nodes on that next side
940 for (const auto local_side_node_id : elem->nodes_on_side(si))
941 {
942 const auto side_node_id = elem->node_id(local_side_node_id);
943 // Skip current node
944 if (side_node_id == second_node_index)
945 continue;
946 mooseAssert((side_node_id != first_node_index) ||
947 (side_list.size() == elem->n_sides()),
948 "Somehow looped back in a single element");
949
950 // Check all the boundaries the other node (on the edge side) is part of
951 const auto bids_range = node_to_bids.equal_range(input_mesh.node_ptr(side_node_id));
952
953 for (auto iter = bids_range.first; iter != bids_range.second; iter++)
954 if (iter->second == boundary_id)
955 {
956 current_side = si;
957 second_node_index = side_node_id;
958 found_match = true;
959 }
960 }
961
962 // no need to examine other sides
963 if (found_match)
964 break;
965 }
966 }
967 }
968
969 // Set current node to opposite node of new element
970 // NOTE: do not use new_first_node or new_second_node to search in the input mesh!
971 new_first_node = new_second_node;
972 first_node_index = second_node_index;
973
974 // Handle loop ending criterion
975 if (!found_match)
976 {
977 mooseWarning("Search for next element in loop boundary failed. Is boundary '" +
978 std::to_string(boundary_id) + "' of mesh ",
979 input_mesh,
980 " a loop boundary?");
981 break;
982 }
983 }
984 }
985
986 if (already_seen_this_side_tuple)
987 mooseWarning("Boundary " + std::to_string(boundary_id) +
988 " seems to have cycles. A single-cycle loop should be used");
989
990 edge_mesh->skip_partitioning(true);
991 edge_mesh->prepare_for_use();
992 if (edge_mesh->n_elem() == 0)
993 mooseError("The input mesh to extract the boundary from does not have a boundary with id ",
994 boundary_id,
995 "\n",
996 input_mesh);
997
998 return edge_mesh;
999}
1000
1001std::unordered_map<dof_id_type, std::unordered_set<dof_id_type>>
1002buildBoundaryNodeToElemMap(const MeshBase & input_mesh, const boundary_id_type boundary_id)
1003{
1004 if (!input_mesh.is_serial())
1006 "Input 2D mesh should be serialized for extracting the loop boundary mesh.\nInput mesh:" +
1007 input_mesh.get_info());
1008
1009 // Get all nodes on that boundary
1010 // Boundary ID might be a sideset or a nodeset, get nodes regardless
1011 const auto particular_node_ids = getBoundaryNodes(input_mesh, boundary_id);
1012
1013 std::unordered_map<dof_id_type, std::unordered_set<dof_id_type>> nid_to_eids_map;
1014 // Fill the map from looping over elements
1015 for (const auto & elem :
1016 as_range(input_mesh.active_elements_begin(), input_mesh.active_elements_end()))
1017 {
1018 for (const auto & nd : elem->node_ref_range())
1019 {
1020 // Only add the element id if the node is on the boundary
1021 if (!particular_node_ids.count(nd.id()))
1022 continue;
1023
1024 auto & elem_ids = nid_to_eids_map[nd.id()];
1025 elem_ids.insert(elem->id());
1026 }
1027 }
1028 return nid_to_eids_map;
1029}
1030
1031std::set<dof_id_type>
1032getBoundaryNodes(const MeshBase & mesh, const BoundaryID boundary_id)
1033{
1034 std::set<dof_id_type> boundary_node_ids;
1035 const BoundaryInfo & boundary_info = mesh.get_boundary_info();
1036
1037 // Get all nodes from the sideset with ID of boundary_id
1038 const auto & bc_sides =
1039 boundary_info.build_side_list(libMesh::BoundaryInfo::BCTupleSortBy::BOUNDARY_ID);
1040 for (const auto & [elem_id, side, bc_id] : bc_sides)
1041 {
1042 if (bc_id == boundary_id)
1043 {
1044 const auto elem = mesh.elem_ptr(elem_id);
1045 for (const auto ni : elem->nodes_on_side(side))
1046 boundary_node_ids.insert(elem->node_id(ni));
1047 }
1048 }
1049
1050 // Get all nodes from nodeset with ID of boundary_id
1051 const auto & bc_nodes = boundary_info.build_node_list();
1052 for (const auto & [n_id, bc_id] : bc_nodes)
1053 if (bc_id == boundary_id)
1054 boundary_node_ids.insert(n_id);
1055
1056 return boundary_node_ids;
1057}
1058
1059void
1061 std::vector<BoundaryName> boundary_names,
1062 const SubdomainID new_subdomain_id,
1063 const SubdomainName & new_subdomain_name)
1064{
1065 // Generate a new block id if one isn't supplied.
1066 SubdomainID new_block_id = new_subdomain_id;
1067
1068 // Make sure our boundary info and parallel counts are setup
1069 if (!mesh.is_prepared())
1070 {
1071 const bool allow_remote_element_removal = mesh.allow_remote_element_removal();
1072 // We want all of our boundary elements available, so avoid removing them if they haven't
1073 // already been so
1074 mesh.allow_remote_element_removal(false);
1075 mesh.complete_preparation();
1076 mesh.allow_remote_element_removal(allow_remote_element_removal);
1077 }
1078
1079 // Check that the sidesets are present in the mesh
1080 for (const auto & sideset : boundary_names)
1082 mooseException("The sideset '", sideset, "' was not found within the mesh");
1083
1084 auto sideset_ids = MooseMeshUtils::getBoundaryIDs(mesh, boundary_names, true);
1085 std::set<boundary_id_type> sidesets(sideset_ids.begin(), sideset_ids.end());
1086 auto side_list = mesh.get_boundary_info().build_side_list();
1087 if (!mesh.is_serial() && mesh.comm().size() > 1)
1088 {
1089 std::vector<Elem *> elements_to_send;
1090 unsigned short i_need_boundary_elems = 0;
1091 for (const auto & [elem_id, side, bc_id] : side_list)
1092 {
1093 libmesh_ignore(side);
1094 if (sidesets.count(bc_id))
1095 {
1096 // Whether we have this boundary information through our locally owned element or a ghosted
1097 // element, we'll need the boundary elements for parallel consistent addition
1098 i_need_boundary_elems = 1;
1099 auto * elem = mesh.elem_ptr(elem_id);
1100 if (elem->processor_id() == mesh.processor_id())
1101 elements_to_send.push_back(elem);
1102 }
1103 }
1104
1105 std::set<const Elem *, libMesh::CompareElemIdsByLevel> connected_elements(
1106 elements_to_send.begin(), elements_to_send.end());
1107 std::set<const Node *> connected_nodes;
1108 reconnect_nodes(connected_elements, connected_nodes);
1109 std::set<dof_id_type> connected_node_ids;
1110 for (auto * nd : connected_nodes)
1111 connected_node_ids.insert(nd->id());
1112
1113 std::vector<unsigned short> need_boundary_elems(mesh.comm().size());
1114 mesh.comm().allgather(i_need_boundary_elems, need_boundary_elems);
1115 std::unordered_map<processor_id_type, decltype(elements_to_send)> push_element_data;
1116 std::unordered_map<processor_id_type, decltype(connected_nodes)> push_node_data;
1117
1118 for (const auto pid : index_range(mesh.comm()))
1119 // Don't need to send to self
1120 if (pid != mesh.processor_id() && need_boundary_elems[pid])
1121 {
1122 if (elements_to_send.size())
1123 push_element_data[pid] = elements_to_send;
1124 if (connected_nodes.size())
1125 push_node_data[pid] = connected_nodes;
1126 }
1127
1128 auto node_action_functor = [](processor_id_type, const auto &)
1129 {
1130 // Node packing specialization already has unpacked node into mesh, so nothing to do
1131 };
1132 Parallel::push_parallel_packed_range(mesh.comm(), push_node_data, &mesh, node_action_functor);
1133 auto elem_action_functor = [](processor_id_type, const auto &)
1134 {
1135 // Elem packing specialization already has unpacked elem into mesh, so nothing to do
1136 };
1137 TIMPI::push_parallel_packed_range(mesh.comm(), push_element_data, &mesh, elem_action_functor);
1138
1139 // now that we've gathered everything, we need to rebuild the side list
1140 side_list = mesh.get_boundary_info().build_side_list();
1141 }
1142
1143 // Use libmesh's BoundaryInfo::add_elements to create lower-d elements with interior_parent
1144 // pointers and subdomain IDs set in one pass.
1145 auto & unstr_mesh = libMesh::cast_ref<UnstructuredMesh &>(mesh);
1146 unstr_mesh.get_boundary_info().add_elements(sidesets, unstr_mesh, false, {new_block_id});
1147
1148 // Assign block name, if provided
1149 if (new_subdomain_name.size())
1150 mesh.set_subdomain_name(new_block_id, new_subdomain_name);
1151
1152 const bool skip_partitioning_old = mesh.skip_partitioning();
1153 mesh.skip_partitioning(true);
1154 mesh.prepare_for_use();
1155 mesh.skip_partitioning(skip_partitioning_old);
1156}
1157
1158void
1159convertBlockToMesh(MeshBase & source_mesh,
1160 MeshBase & target_mesh,
1161 const std::vector<SubdomainName> & target_blocks)
1162{
1163 if (!source_mesh.is_replicated())
1164 mooseError("This generator does not support distributed meshes.");
1165
1166 const auto target_block_ids = MooseMeshUtils::getSubdomainIDs(source_mesh, target_blocks);
1167
1168 // Our source is non-const, so we can fix any missing caches
1169 if (!source_mesh.preparation().has_cached_elem_data)
1170 source_mesh.cache_elem_data();
1171
1172 // Check that the block ids/names exist in the mesh
1173 std::set<SubdomainID> mesh_blocks;
1174 source_mesh.subdomain_ids(mesh_blocks);
1175
1176 for (const auto i : index_range(target_block_ids))
1177 if (target_block_ids[i] == Moose::INVALID_BLOCK_ID || !mesh_blocks.count(target_block_ids[i]))
1178 {
1179 mooseException("The target_block '", target_blocks[i], "' was not found within the mesh.");
1180 }
1181
1182 // know which nodes have already been inserted, by tracking the old mesh's node's ids'
1183 std::unordered_map<dof_id_type, dof_id_type> old_new_node_map;
1184
1185 for (const auto target_block_id : target_block_ids)
1186 {
1187
1188 for (auto elem : source_mesh.active_subdomain_elements_ptr_range(target_block_id))
1189 {
1190 if (elem->level() != 0)
1191 mooseError("Refined blocks are not supported by this generator. "
1192 "Can you re-organize mesh generators to refine after converting the block?");
1193
1194 // make a deep copy so that mutiple meshes' destructors don't segfault at program termination
1195 auto copy = elem->build(elem->type());
1196
1197 // Keep the subdomain id
1198 copy->subdomain_id() = elem->subdomain_id();
1199
1200 // index of node in the copy element must be managed manually as there is no intelligent
1201 // insert method
1202 dof_id_type copy_n_index = 0;
1203
1204 // correctly assign new copies of nodes, loop over nodes
1205 for (dof_id_type i : elem->node_index_range())
1206 {
1207 auto & n = elem->node_ref(i);
1208
1209 if (old_new_node_map.count(n.id()))
1210 {
1211 // case where we have already inserted this particular point before
1212 // then we need to find the already-inserted one and hook it up right
1213 // to it's respective element
1214 copy->set_node(copy_n_index++, target_mesh.node_ptr(old_new_node_map[n.id()]));
1215 }
1216 else
1217 {
1218 // case where we've NEVER inserted this particular point before
1219 // add them both to the element and the mesh
1220
1221 // Nodes' IDs are their indexes in the nodes' respective mesh
1222 // If we set them as invalid they are automatically assigned
1223 // Add to mesh, auto-assigning a new id.
1224 Node * node = target_mesh.add_point(elem->point(i));
1225
1226 // Add to element copy (manually)
1227 copy->set_node(copy_n_index++, node);
1228
1229 // remember the (old) ID
1230 old_new_node_map[n.id()] = node->id();
1231 }
1232 }
1233
1234 // it is ok to release the copy element into the mesh because derived meshes class
1235 // (ReplicatedMesh, DistributedMesh) manage their own elements, will delete them
1236 target_mesh.add_elem(copy.release());
1237 }
1238 }
1239
1240 // Move subdomain names
1241 for (const auto sbd_id : target_block_ids)
1242 target_mesh.set_subdomain_name(sbd_id, source_mesh.subdomain_name(sbd_id));
1243}
1244
1245void
1247 UnstructuredMesh & destination,
1248 const UnstructuredMesh & source,
1249 const bool avoid_merging_subdomains,
1250 const bool avoid_merging_boundaries,
1251 const Parallel::Communicator & communicator)
1252{
1253 dof_id_type node_delta = destination.max_node_id();
1254 dof_id_type elem_delta = destination.max_elem_id();
1255
1256 unique_id_type unique_delta =
1257#ifdef LIBMESH_ENABLE_UNIQUE_ID
1258 destination.parallel_max_unique_id();
1259#else
1260 0;
1261#endif
1262
1263 // Prevent overlaps by offsetting the subdomains in
1264 std::unordered_map<subdomain_id_type, subdomain_id_type> id_remapping;
1265 unsigned int block_offset = 0;
1266 if (avoid_merging_subdomains)
1267 {
1268 // Note: if performance becomes an issue, this is overkill for just getting the max node id
1269 std::set<subdomain_id_type> source_ids;
1270 std::set<subdomain_id_type> dest_ids;
1271
1272 // We need source subdomain ids already cached; libMesh will
1273 // scream otherwise
1274 source.subdomain_ids(source_ids, true);
1275
1276 // Our destination is non-const, so we can fix any missing caches
1277 if (!destination.preparation().has_cached_elem_data)
1278 destination.cache_elem_data();
1279
1280 destination.subdomain_ids(dest_ids, true);
1281
1282 mooseAssert(source_ids.size(), "Should have a subdomain");
1283 mooseAssert(dest_ids.size(), "Should have a subdomain");
1284 unsigned int max_dest_bid = *dest_ids.rbegin();
1285 unsigned int min_source_bid = *source_ids.begin();
1286 communicator.max(max_dest_bid);
1287 communicator.min(min_source_bid);
1288 block_offset = 1 + max_dest_bid - min_source_bid;
1289 for (const auto bid : source_ids)
1290 id_remapping[bid] = block_offset + bid;
1291 }
1292
1293 // Copy mesh data over from the other mesh
1294 destination.copy_nodes_and_elements(source,
1295 // Skipping this should cause the neighbors
1296 // to simply be copied from the other mesh
1297 // (which makes sense and is way faster)
1298 /*skip_find_neighbors = */ true,
1299 elem_delta,
1300 node_delta,
1301 unique_delta,
1302 avoid_merging_subdomains ? &id_remapping : nullptr);
1303
1304 // Get an offset to prevent overlaps / wild merging between boundaries
1305 BoundaryInfo & boundary = destination.get_boundary_info();
1306 const BoundaryInfo & other_boundary = source.get_boundary_info();
1307
1308 unsigned int bid_offset = 0;
1309 if (avoid_merging_boundaries)
1310 {
1311 const auto boundary_ids = boundary.get_boundary_ids();
1312 const auto other_boundary_ids = other_boundary.get_boundary_ids();
1313 unsigned int max_dest_bid = boundary_ids.size() ? *boundary_ids.rbegin() : 0;
1314 unsigned int min_source_bid = other_boundary_ids.size() ? *other_boundary_ids.begin() : 0;
1315 communicator.max(max_dest_bid);
1316 communicator.min(min_source_bid);
1317 bid_offset = 1 + max_dest_bid - min_source_bid;
1318 }
1319
1320 // Note: the code below originally came from ReplicatedMesh::stitch_mesh_helper()
1321 // in libMesh replicated_mesh.C around line 1203
1322
1323 // Copy BoundaryInfo from other_mesh too. We do this via the
1324 // list APIs rather than element-by-element for speed.
1325 for (const auto & t : other_boundary.build_node_list())
1326 boundary.add_node(std::get<0>(t) + node_delta, bid_offset + std::get<1>(t));
1327
1328 for (const auto & t : other_boundary.build_side_list())
1329 boundary.add_side(std::get<0>(t) + elem_delta, std::get<1>(t), bid_offset + std::get<2>(t));
1330
1331 for (const auto & t : other_boundary.build_edge_list())
1332 boundary.add_edge(std::get<0>(t) + elem_delta, std::get<1>(t), bid_offset + std::get<2>(t));
1333
1334 for (const auto & t : other_boundary.build_shellface_list())
1335 boundary.add_shellface(
1336 std::get<0>(t) + elem_delta, std::get<1>(t), bid_offset + std::get<2>(t));
1337
1338 // Check for the case with two block ids sharing the same name
1339 if (avoid_merging_subdomains)
1340 {
1341 mooseAssert(mg.parameters().isParamDefined("avoid_merging_subdomains"),
1342 "Missing parameter in the mesh generator calling this function: "
1343 "avoid_merging_subdomains. Considering setting avoid_merging_subdomains to true.");
1344 for (const auto & [block_id, block_name] : destination.get_subdomain_name_map())
1345 for (const auto & [source_id, source_name] : source.get_subdomain_name_map())
1346 if (block_name == source_name)
1347 mg.paramWarning(
1348 "avoid_merging_subdomains",
1349 "Not merging subdomains is creating two subdomains with the same name '" +
1350 block_name + "' but different ids: " + std::to_string(source_id) + " & " +
1351 std::to_string(block_id + block_offset) +
1352 ".\n We recommend using a RenameBlockGenerator to prevent this as you "
1353 "will get errors reading the Exodus output later.");
1354 }
1355
1356 for (const auto & [block_id, block_name] : source.get_subdomain_name_map())
1357 destination.set_subdomain_name_map().insert(
1358 std::make_pair<SubdomainID, SubdomainName>(block_id + block_offset, block_name));
1359
1360 // Check for the case with two boundary ids sharing the same name
1361 if (avoid_merging_boundaries)
1362 {
1363 mooseAssert(mg.parameters().isParamDefined("avoid_merging_boundaries"),
1364 "Missing parameter in the mesh generator calling this function: "
1365 "avoid_merging_boundaries. Considering setting avoid_merging_boundaries to true.");
1366 for (const auto & [b_id, b_name] : other_boundary.get_sideset_name_map())
1367 for (const auto & [source_id, source_name] : boundary.get_sideset_name_map())
1368 if (b_name == source_name)
1369 mg.paramWarning(
1370 "avoid_merging_boundaries",
1371 "Not merging boundaries is creating two sidesets with the same name '" + b_name +
1372 "' but different ids: " + std::to_string(source_id) + " & " +
1373 std::to_string(b_id + bid_offset) +
1374 ".\n We recommend using a RenameBoundaryGenerator to prevent this as you "
1375 "will get errors reading the Exodus output later.");
1376 for (const auto & [b_id, b_name] : other_boundary.get_nodeset_name_map())
1377 for (const auto & [source_id, source_name] : boundary.get_nodeset_name_map())
1378 if (b_name == source_name)
1379 mg.paramWarning(
1380 "avoid_merging_boundaries",
1381 "Not merging boundaries is creating two nodesets with the same name '" + b_name +
1382 "' but different ids: " + std::to_string(source_id) + " & " +
1383 std::to_string(b_id + bid_offset) +
1384 ".\n We recommend using a RenameBoundaryGenerator to prevent this as you "
1385 "will get errors reading the Exodus output later.");
1386 }
1387
1388 for (const auto & [nodeset_id, nodeset_name] : other_boundary.get_nodeset_name_map())
1389 boundary.set_nodeset_name_map().insert(
1390 std::make_pair<BoundaryID, BoundaryName>(nodeset_id + bid_offset, nodeset_name));
1391
1392 for (const auto & [sideset_id, sideset_name] : other_boundary.get_sideset_name_map())
1393 boundary.set_sideset_name_map().insert(
1394 std::make_pair<BoundaryID, BoundaryName>(sideset_id + bid_offset, sideset_name));
1395
1396 for (const auto & [edgeset_id, edgeset_name] : other_boundary.get_edgeset_name_map())
1397 boundary.set_edgeset_name_map().insert(
1398 std::make_pair<BoundaryID, BoundaryName>(edgeset_id + bid_offset, edgeset_name));
1399}
1400
1401void
1402buildPolyLineMesh(MeshBase & mesh,
1403 const std::vector<Point> & points,
1404 const std::vector<Point> & mid_points,
1405 const bool loop,
1406 const BoundaryName & start_boundary,
1407 const BoundaryName & end_boundary,
1408 const std::vector<unsigned int> & nums_edges_between_points)
1409{
1410 mooseAssert(nums_edges_between_points.size() == 1 ||
1411 nums_edges_between_points.size() == points.size() - 1 + loop,
1412 "nums_edges_between_points must be either a single value or have the same number of "
1413 "entries as segments defined by the points.");
1414 mooseAssert(
1415 mid_points.size() == 0 || mid_points.size() == points.size() - (loop ? 0 : 1),
1416 "mid_points must be either empty or have the consistent number of entries as points.");
1417 mooseAssert(
1418 mid_points.size() == 0 ||
1419 (nums_edges_between_points.size() == 1 && nums_edges_between_points.front() == 1) ||
1420 (nums_edges_between_points.size() == points.size() - 1 + loop &&
1421 std::all_of(nums_edges_between_points.begin(),
1422 nums_edges_between_points.end(),
1423 [](unsigned int n) { return n == 1; })),
1424 "mid_points can only be provided if each segment has exactly one edge.");
1425
1426 const auto n_points = points.size();
1427 for (auto i : make_range(n_points))
1428 {
1429 const auto & num_edges_between_points =
1430 (nums_edges_between_points.size() == 1)
1431 ? nums_edges_between_points[0]
1432 : (i == nums_edges_between_points.size() ? 0 : nums_edges_between_points[i]);
1433
1434 Point p = points[i];
1435 const auto pt_counter = (nums_edges_between_points.size() == 1)
1436 ? i
1437 : std::accumulate(nums_edges_between_points.begin(),
1438 nums_edges_between_points.begin() + i,
1439 0);
1440 mesh.add_point(
1441 p, nums_edges_between_points.size() == 1 ? (i * num_edges_between_points) : pt_counter);
1442
1443 if (num_edges_between_points > 1)
1444 {
1445 if (!loop && (i + 1) == n_points)
1446 break;
1447
1448 const auto ip1 = (i + 1) % n_points;
1449 const Point pvec = (points[ip1] - p) / num_edges_between_points;
1450
1451 for (auto j : make_range(1u, num_edges_between_points))
1452 {
1453 p += pvec;
1454 mesh.add_point(
1455 p,
1456 (nums_edges_between_points.size() == 1 ? (i * num_edges_between_points) : pt_counter) +
1457 j);
1458 }
1459 }
1460 }
1461 // Add mid points if applicable. When mid points are provided, each segment has exactly one edge,
1462 // so the midpoint node ids follow the vertex node ids.
1463 for (const auto & i : make_range(mid_points.size()))
1464 mesh.add_point(mid_points[i], n_points + i);
1465
1466 const auto n_segments = loop ? n_points : (n_points - 1);
1467 const auto n_elem =
1468 nums_edges_between_points.size() == 1
1469 ? n_segments * nums_edges_between_points[0]
1470 : std::accumulate(nums_edges_between_points.begin(), nums_edges_between_points.end(), 0);
1471 const auto max_nodes =
1472 (nums_edges_between_points.size() == 1 ? n_segments * nums_edges_between_points[0]
1473 : std::accumulate(nums_edges_between_points.begin(),
1474 nums_edges_between_points.end(),
1475 0)) +
1476 (loop ? 0 : 1);
1477 for (auto i : make_range(n_elem))
1478 {
1479 std::unique_ptr<Elem> elem;
1480 if (mid_points.size())
1481 {
1482 elem = std::make_unique<Edge3>();
1483 elem->set_node(2, mesh.node_ptr(n_points + i));
1484 }
1485 else
1486 elem = Elem::build(EDGE2);
1487 const auto ip1 = (i + 1) % max_nodes;
1488 elem->set_node(0, mesh.node_ptr(i));
1489 elem->set_node(1, mesh.node_ptr(ip1));
1490 elem->set_id() = i;
1491 mesh.add_elem(std::move(elem));
1492 }
1493
1494 if (!loop)
1495 {
1496 BoundaryInfo & bi = mesh.get_boundary_info();
1497 std::vector<BoundaryName> bdy_names{start_boundary, end_boundary};
1498 std::vector<boundary_id_type> ids = MooseMeshUtils::getBoundaryIDs(mesh, bdy_names, true);
1499 bi.add_side(mesh.elem_ptr(0), 0, ids[0]);
1500 bi.add_side(mesh.elem_ptr(n_elem - 1), 1, ids[1]);
1501 }
1502 else
1503 mooseAssert(start_boundary.empty() && end_boundary.empty(),
1504 "Cannot assign start/end boundaries on a looped polyline.");
1505
1506 mesh.prepare_for_use();
1507}
1508
1509void
1510buildPolyLineMesh(MeshBase & mesh,
1511 const std::vector<Point> & points,
1512 const bool loop,
1513 const BoundaryName & start_boundary,
1514 const BoundaryName & end_boundary,
1515 const std::vector<unsigned int> & nums_edges_between_points)
1516{
1518 mesh, points, {}, loop, start_boundary, end_boundary, nums_edges_between_points);
1519}
1520
1521void
1522buildPolyLineMesh(MeshBase & mesh,
1523 const std::vector<Point> & points,
1524 const bool loop,
1525 const BoundaryName & start_boundary,
1526 const BoundaryName & end_boundary,
1527 const Real max_elem_size)
1528{
1529 std::vector<unsigned int> nums_edges_between_points;
1530 const auto n_points = points.size();
1531 for (auto i : make_range(n_points))
1532 {
1533 if (!loop && (i + 1) == n_points)
1534 break;
1535
1536 const auto ip1 = (i + 1) % n_points;
1537 const Real length = (points[ip1] - points[i]).norm();
1538 const unsigned int n_elems = std::max(
1539 static_cast<unsigned int>(std::ceil(length / max_elem_size)), static_cast<unsigned int>(1));
1540 nums_edges_between_points.push_back(n_elems);
1541 }
1542
1544 mesh, points, {}, loop, start_boundary, end_boundary, nums_edges_between_points);
1545}
1546
1547void
1548addExternalBoundary(MeshBase & mesh, const BoundaryID extern_bid, bool & has_external_bid)
1549{
1550 auto & binfo = mesh.get_boundary_info();
1551 for (const auto & elem : mesh.active_element_ptr_range())
1552 for (const auto & i_side : elem->side_index_range())
1553 if (elem->neighbor_ptr(i_side) == nullptr)
1554 {
1555 has_external_bid = true;
1556 binfo.add_side(elem, i_side, extern_bid);
1557 }
1558}
1559
1560std::vector<unsigned int>
1561nodalQuadraturePointToNodeMap(const Elem & elem, const std::vector<Point> & q_points)
1562{
1563 const auto n_nodes = elem.n_nodes();
1564 if (q_points.size() != n_nodes)
1565 mooseError("Nodal quadrature produced ",
1566 q_points.size(),
1567 " points for element ",
1568 elem.id(),
1569 " of type ",
1570 libMesh::Utility::enum_to_string<ElemType>(elem.type()),
1571 ", but the element has ",
1572 n_nodes,
1573 " nodes.");
1574
1575 const auto invalid_node = std::numeric_limits<unsigned int>::max();
1576 std::vector<unsigned int> qpoint_to_node(n_nodes, invalid_node);
1577 std::vector<bool> node_used(n_nodes, false);
1578
1579 const Real element_size = elem.hmax();
1580 mooseAssert(
1581 element_size > 0,
1582 "Element " << elem.id() << " of type "
1583 << libMesh::Utility::enum_to_string<ElemType>(elem.type())
1584 << " has a non-positive hmax and cannot be used for nodal quadrature point "
1585 "matching.");
1586
1587 // The nodal quadrature locations and the generated nodes are two floating-point
1588 // reconstructions of the same physical points. Scale the tolerance by element size so the
1589 // matching is insensitive to coordinate magnitude; the 100*TOLERANCE factor allows roundoff
1590 // from FE reinitialization and mesh generation while remaining far below a valid node spacing.
1591 const Real matching_tol = 100 * TOLERANCE * element_size;
1592 const Real matching_tol_sq = matching_tol * matching_tol;
1593
1594 // Each nodal quadrature point should coincide with exactly one still-unused
1595 // node. The unused-node search makes the mapping one-to-one and
1596 // avoids silently assigning two quadrature entries to the same node.
1597 for (const auto qp : make_range(q_points.size()))
1598 {
1599 unsigned int closest_node = invalid_node;
1600 Real closest_dist_sq = std::numeric_limits<Real>::max();
1601 Real second_closest_dist_sq = std::numeric_limits<Real>::max();
1602
1603 for (const auto n : make_range(n_nodes))
1604 {
1605 if (node_used[n])
1606 continue;
1607
1608 const Real dist_sq = (q_points[qp] - elem.point(n)).norm_sq();
1609 if (dist_sq < closest_dist_sq)
1610 {
1611 second_closest_dist_sq = closest_dist_sq;
1612 closest_dist_sq = dist_sq;
1613 closest_node = n;
1614 }
1615 else if (dist_sq < second_closest_dist_sq)
1616 second_closest_dist_sq = dist_sq;
1617 }
1618
1619 if (closest_node == invalid_node || closest_dist_sq > matching_tol_sq)
1620 mooseError("Could not match nodal quadrature point ",
1621 qp,
1622 " at ",
1623 q_points[qp],
1624 " to a node on element ",
1625 elem.id(),
1626 " of type ",
1627 libMesh::Utility::enum_to_string<ElemType>(elem.type()),
1628 ". The nearest unmatched node distance is ",
1629 std::sqrt(closest_dist_sq),
1630 ", which exceeds the tolerance ",
1631 matching_tol,
1632 ".");
1633
1634 if (second_closest_dist_sq <= matching_tol_sq)
1635 mooseError("Nodal quadrature point ",
1636 qp,
1637 " at ",
1638 q_points[qp],
1639 " does not map uniquely to element ",
1640 elem.id(),
1641 " of type ",
1642 libMesh::Utility::enum_to_string<ElemType>(elem.type()),
1643 ". Two unmatched nodes are within the matching tolerance ",
1644 matching_tol,
1645 ".");
1646
1647 qpoint_to_node[qp] = closest_node;
1648 node_used[closest_node] = true;
1649 }
1650
1651#ifdef DEBUG
1652 // In optimized builds the mapping above skips already matched nodes for speed. In debug builds,
1653 // audit the full candidate set to catch ambiguous geometry or accidental many-to-one matches.
1654 std::vector<unsigned int> node_to_qpoint(n_nodes, invalid_node);
1655 for (const auto qp : make_range(q_points.size()))
1656 {
1657 const auto mapped_node = qpoint_to_node[qp];
1658 mooseAssert(mapped_node != invalid_node && mapped_node < n_nodes,
1659 "Invalid node mapping for nodal quadrature point " << qp << ".");
1660 mooseAssert(node_to_qpoint[mapped_node] == invalid_node,
1661 "Node " << mapped_node << " on element " << elem.id()
1662 << " was matched to both nodal quadrature point "
1663 << node_to_qpoint[mapped_node] << " and " << qp << ".");
1664 node_to_qpoint[mapped_node] = qp;
1665
1666 // Check the qp -> node direction without excluding nodes already matched by previous qps.
1667 unsigned int candidate_count = 0;
1668 unsigned int candidate_node = invalid_node;
1669 for (const auto n : make_range(n_nodes))
1670 if ((q_points[qp] - elem.point(n)).norm_sq() <= matching_tol_sq)
1671 {
1672 ++candidate_count;
1673 candidate_node = n;
1674 }
1675
1676 mooseAssert(candidate_count == 1,
1677 "Nodal quadrature point " << qp << " on element " << elem.id() << " has "
1678 << candidate_count << " node candidates within tolerance "
1679 << matching_tol << ".");
1680 mooseAssert(candidate_node == mapped_node,
1681 "Nodal quadrature point "
1682 << qp << " on element " << elem.id() << " was matched to node " << mapped_node
1683 << ", but the full candidate search found node " << candidate_node << ".");
1684 }
1685
1686 for (const auto n : make_range(n_nodes))
1687 {
1688 mooseAssert(node_to_qpoint[n] != invalid_node,
1689 "Node " << n << " on element " << elem.id()
1690 << " was not matched to a nodal quadrature point.");
1691
1692 // Check the node -> qp direction so every node is also uniquely represented.
1693 unsigned int candidate_count = 0;
1694 unsigned int candidate_qp = invalid_node;
1695 for (const auto qp : make_range(q_points.size()))
1696 if ((q_points[qp] - elem.point(n)).norm_sq() <= matching_tol_sq)
1697 {
1698 ++candidate_count;
1699 candidate_qp = qp;
1700 }
1701
1702 mooseAssert(candidate_count == 1,
1703 "Node " << n << " on element " << elem.id() << " has " << candidate_count
1704 << " nodal quadrature point candidates within tolerance " << matching_tol
1705 << ".");
1706 mooseAssert(candidate_qp == node_to_qpoint[n],
1707 "Node " << n << " on element " << elem.id()
1708 << " was matched to nodal quadrature point " << node_to_qpoint[n]
1709 << ", but the full candidate search found point " << candidate_qp << ".");
1710 }
1711#endif
1712
1713 return qpoint_to_node;
1714}
1715}
boundary_id_type BoundaryID
subdomain_id_type SubdomainID
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition MooseError.h:345
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
std::set< std::string > sideset
bool isParamDefined(const std::string &name) const
Method returns true if the parameter is defined for any type.
MeshGenerators are objects that can modify or add to an existing mesh.
const InputParameters & parameters() const
Get the parameters of the object.
Definition MooseBase.h:131
Provides a way for users to bail out of the current solve.
virtual const char * what() const
Get out the error message.
void paramWarning(const std::string &param, Args... args) const
MeshBase & mesh
void copyIntoMesh(MeshGenerator &mg, UnstructuredMesh &destination, const UnstructuredMesh &source, const bool avoid_merging_subdomains, const bool avoid_merging_boundaries, const Parallel::Communicator &communicator)
Helper function for copying one mesh into another.
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.
Point boundaryCentroidCalculator(const BoundaryName &boundary, MeshBase &mesh)
Calculates the centroid of a boundary on a mesh.
bool hasBoundaryName(const MeshBase &mesh, const BoundaryName &name)
Whether a particular boundary name 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.
void buildPolyLineMesh(MeshBase &mesh, const std::vector< Point > &points, const bool loop, const BoundaryName &start_boundary, const BoundaryName &end_boundary, const std::vector< unsigned int > &nums_edges_between_points)
Generates meshes from edges connecting a list of points.
void changeBoundaryId(MeshBase &mesh, const libMesh::boundary_id_type old_id, const libMesh::boundary_id_type new_id, bool delete_prev)
Changes the old boundary ID to a new ID in the mesh.
bool hasSubdomainID(const MeshBase &input_mesh, const SubdomainID &id)
Whether a particular subdomain ID exists in the mesh.
std::vector< subdomain_id_type > getSubdomainIDs(const libMesh::MeshBase &mesh, const std::vector< SubdomainName > &subdomain_name)
Get the associated subdomainIDs for the subdomain names that are passed in.
std::unique_ptr< ReplicatedMesh > buildBoundaryMesh(const MeshBase &input_mesh, const boundary_id_type boundary_id)
Build a lower-dimensional mesh from a boundary of an input mesh Note: The lower-dimensional mesh will...
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::unordered_map< dof_id_type, std::unordered_set< dof_id_type > > buildBoundaryNodeToElemMap(const MeshBase &input_mesh, const boundary_id_type boundary_id)
Build a map from the node ids to all element ids they are part of for the nodes on a particular nodes...
void changeSubdomainId(MeshBase &mesh, const subdomain_id_type old_id, const subdomain_id_type new_id)
Changes the old subdomain ID to a new ID in the mesh.
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 createSubdomainFromSidesets(MeshBase &mesh, std::vector< BoundaryName > boundary_names, const SubdomainID new_subdomain_id, const SubdomainName &new_subdomain_name)
Create a new subdomain by generating new side elements from a list of sidesets in a given mesh.
std::set< dof_id_type > getBoundaryNodes(const MeshBase &mesh, const BoundaryID boundary_id)
Get all the nodes on that particular boundary, whether a nodeset or a sideset.
BoundaryID getNextFreeBoundaryID(MeshBase &input_mesh)
Checks input mesh and returns the largest boundary ID in the mesh plus one, which is a boundary ID in...
void addExternalBoundary(MeshBase &mesh, const BoundaryID extern_bid, bool &has_external_bid)
Adds a sideset for the external boundary of the mesh (e.g.
void swapNodesInElem(Elem &elem, const unsigned int nd1, const unsigned int nd2)
Swap two nodes within an element.
void convertBlockToMesh(MeshBase &source_mesh, MeshBase &target_mesh, const std::vector< SubdomainName > &target_blocks)
Convert a list of blocks in a given mesh to a standalone new mesh.
std::set< BoundaryID > getBoundaryIDSet(const libMesh::MeshBase &mesh, const std::vector< BoundaryName > &boundary_name, bool generate_unknown)
Gets the boundary IDs into a set with their names.
std::unordered_map< dof_id_type, dof_id_type > getExtraIDUniqueCombinationMap(const MeshBase &mesh, const std::set< SubdomainID > &block_ids, std::vector< ExtraElementIDName > extra_ids)
Create a new set of element-wise IDs by finding unique combinations of existing extra ID values.
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.
void mergeBoundaryIDsWithSameName(MeshBase &mesh)
Merges the boundary IDs of boundaries that have the same names but different IDs.
SubdomainID getNextFreeSubdomainID(MeshBase &input_mesh)
Checks input mesh and returns max(block ID) + 1, which represents a block ID that is not currently in...
BoundaryID getBoundaryID(const BoundaryName &boundary_name, const MeshBase &mesh)
Gets the boundary ID associated with the given BoundaryName.
bool isCoPlanar(const std::vector< Point > &vec_pts, const Point plane_nvec, const Point fixed_pt)
Decides whether all the Points of a vector of Points are in a plane that is defined by a normal vecto...
std::unique_ptr< ReplicatedMesh > buildLoopBoundaryOf2DMesh(const MeshBase &input_mesh, const boundary_id_type boundary_id)
Build a loop mesh of edges from the contiguous 2D boundary of 2D input mesh Note: The lower-dimension...
RealVectorValue boundaryWeightedNormal(const BoundaryName &boundary, MeshBase &mesh)
Calculates the side-volume weighted (side-vertex) average normal of a boundary on a mesh.
void makeOrderedNodeList(std::vector< std::pair< dof_id_type, dof_id_type > > &node_assm, std::vector< dof_id_type > &elem_id_list, std::vector< dof_id_type > &midpoint_node_list, std::vector< dof_id_type > &ordered_node_list, std::vector< dof_id_type > &ordered_elem_id_list)
Convert a list of sides in the form of a vector of pairs of node ids into a list of ordered nodes bas...
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.
bool hasBoundaryNameOrID(const MeshBase &mesh, const BoundaryName &name_or_id)
Whether a particular boundary name or ID exists in the mesh.
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
const BoundaryID INVALID_BOUNDARY_ID
Definition MooseTypes.C:22
const SubdomainID INVALID_BLOCK_ID
Definition MooseTypes.C:20
void push_parallel_packed_range(const Communicator &comm, MapToContainers &&data, Context *context, const ActionFunctor &act_on_data)
const unsigned int invalid_uint
Real distance(const Point &p)
const dof_id_type n_nodes