https://mooseframework.inl.gov
Loading...
Searching...
No Matches
MooseMeshUtils.h
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#pragma once
11
12#include <functional>
13
14#include "libmesh/replicated_mesh.h"
15#include "libmesh/boundary_info.h"
16
17#include "MooseUtils.h"
18#include "MooseTypes.h"
19#include "FaceInfo.h"
20#include "MeshGenerator.h"
21
22// Utilities for MeshBase
23// Many of these utilities could live in libMesh, and in fact, before adding a new one here
24// you should also check mesh_tools.h in libMesh to see if it does not exist there already.
25
27{
28
29// Used to temporarily store information about which lower-dimensional
30// sides to add and what subdomain id to use for the added sides.
32{
33 ElemSidePair(Elem * elem_in, unsigned short int side_in) : elem(elem_in), side(side_in) {}
34
35 Elem * elem;
36 unsigned short int side;
37};
38
44void mergeBoundaryIDsWithSameName(MeshBase & mesh);
45
54void changeBoundaryId(MeshBase & mesh,
55 const libMesh::boundary_id_type old_id,
56 const libMesh::boundary_id_type new_id,
57 bool delete_prev);
58
66void
67changeSubdomainId(MeshBase & mesh, const subdomain_id_type old_id, const subdomain_id_type new_id);
68
82std::vector<BoundaryID> getBoundaryIDs(const libMesh::MeshBase & mesh,
83 const std::vector<BoundaryName> & boundary_name,
84 bool generate_unknown,
85 const std::set<BoundaryID> & mesh_boundary_ids);
86
100std::vector<BoundaryID> getBoundaryIDs(const libMesh::MeshBase & mesh,
101 const std::vector<BoundaryName> & boundary_name,
102 bool generate_unknown);
103
118std::set<BoundaryID> getBoundaryIDSet(const libMesh::MeshBase & mesh,
119 const std::vector<BoundaryName> & boundary_name,
120 bool generate_unknown);
121
128BoundaryID getBoundaryID(const BoundaryName & boundary_name, const MeshBase & mesh);
129
135std::set<std::pair<BoundaryName, BoundaryID>>
137
144SubdomainID getSubdomainID(const SubdomainName & subdomain_name, const MeshBase & mesh);
145
153std::vector<subdomain_id_type> getSubdomainIDs(const libMesh::MeshBase & mesh,
154 const std::vector<SubdomainName> & subdomain_name);
155std::set<subdomain_id_type> getSubdomainIDs(const libMesh::MeshBase & mesh,
156 const std::set<SubdomainName> & subdomain_name);
157
163std::set<std::pair<SubdomainName, SubdomainID>>
165
172Point meshCentroidCalculator(const MeshBase & mesh);
173
180Point boundaryCentroidCalculator(const BoundaryName & boundary, MeshBase & mesh);
181
188RealVectorValue boundaryWeightedNormal(const BoundaryName & boundary, MeshBase & mesh);
189
200template <typename P, typename C>
201void
203 C & factor,
204 const Moose::CoordinateSystemType coord_type,
205 const unsigned int rz_radial_coord = libMesh::invalid_uint)
206{
207 switch (coord_type)
208 {
209 case Moose::COORD_XYZ:
210 factor = 1.0;
211 break;
212 case Moose::COORD_RZ:
213 {
214 mooseAssert(rz_radial_coord != libMesh::invalid_uint,
215 "Must pass in a valid rz radial coordinate");
216 factor = 2 * M_PI * point(rz_radial_coord);
217 break;
218 }
220 factor = 4 * M_PI * point(0) * point(0);
221 break;
222 default:
223 mooseError("Unknown coordinate system");
224 }
225}
226
234template <typename P, typename C>
235C
236computeDistanceToAxis(const P & point, const Point & origin, const RealVectorValue & direction)
237{
238 return (point - origin).cross(direction).norm();
239}
240
248Real computeMaxDistanceToAxis(const MeshBase & mesh,
249 const Point & origin,
250 const RealVectorValue & direction);
251
260template <typename P, typename C>
261void
263 const std::pair<Point, RealVectorValue> & axis,
264 C & factor)
265{
266 factor = 2 * M_PI * computeDistanceToAxis<P, C>(point, axis.first, axis.second);
267}
268
269inline void
271 const Moose::CoordinateSystemType coord_type,
272 const unsigned int rz_radial_coord = libMesh::invalid_uint)
273{
274 coordTransformFactor(fi.faceCentroid(), fi.faceCoord(), coord_type, rz_radial_coord);
275}
276
291std::unordered_map<dof_id_type, dof_id_type>
292getExtraIDUniqueCombinationMap(const MeshBase & mesh,
293 const std::set<SubdomainID> & block_ids,
294 std::vector<ExtraElementIDName> extra_ids);
295
304bool isCoPlanar(const std::vector<Point> & vec_pts, const Point plane_nvec, const Point fixed_pt);
305
312bool isCoPlanar(const std::vector<Point> & vec_pts, const Point plane_nvec);
313
319bool isCoPlanar(const std::vector<Point> & vec_pts);
320
326SubdomainID getNextFreeSubdomainID(MeshBase & input_mesh);
327
341std::unordered_map<subdomain_id_type, subdomain_id_type> remapSecondarySubdomainIDsForStitching(
342 MeshBase & mesh, MeshBase & secondary_mesh, bool merge_same_name_across_ids = true);
343
355std::unordered_map<boundary_id_type, boundary_id_type>
357 MeshBase & secondary_mesh,
358 const std::set<boundary_id_type> & ids_to_preserve = {});
359
365BoundaryID getNextFreeBoundaryID(MeshBase & input_mesh);
366
372bool hasSubdomainID(const MeshBase & input_mesh, const SubdomainID & id);
373
379bool hasSubdomainName(const MeshBase & input_mesh, const SubdomainName & name);
380
386bool hasBoundaryID(const MeshBase & input_mesh, const BoundaryID id);
387
396bool hasBoundaryName(const MeshBase & mesh, const BoundaryName & name);
397
406bool hasBoundaryNameOrID(const MeshBase & mesh, const BoundaryName & name_or_id);
407
418void makeOrderedNodeList(std::vector<std::pair<dof_id_type, dof_id_type>> & node_assm,
419 std::vector<dof_id_type> & elem_id_list,
420 std::vector<dof_id_type> & midpoint_node_list,
421 std::vector<dof_id_type> & ordered_node_list,
422 std::vector<dof_id_type> & ordered_elem_id_list);
423
432void makeOrderedNodeList(std::vector<std::pair<dof_id_type, dof_id_type>> & node_assm,
433 std::vector<dof_id_type> & elem_id_list,
434 std::vector<dof_id_type> & ordered_node_list,
435 std::vector<dof_id_type> & ordered_elem_id_list);
436
444template <typename T, typename Q>
445Q
446getIDFromName(const T & name)
447{
448 if (!MooseUtils::isDigits(name))
450 "'name' ", name, " should only contain digits that can be converted to a numerical type.");
451 long long id = std::stoll(name);
452 Q id_Q = Q(id);
453 if (id < std::numeric_limits<Q>::min() || id > std::numeric_limits<Q>::max())
454 mooseError(MooseUtils::prettyCppType<T>(&name),
455 " ",
456 name,
457 " is not within the numeric limits of the expected ID type ",
458 MooseUtils::prettyCppType<Q>(&id_Q),
459 ".");
460
461 return id_Q;
462}
463
470void swapNodesInElem(Elem & elem, const unsigned int nd1, const unsigned int nd2);
471
481template <typename T>
482void
483idSwapParametersProcessor(const std::string & class_name,
484 const std::string & id_name,
485 const std::vector<std::vector<T>> & id_swaps,
486 std::vector<std::unordered_map<T, T>> & id_swap_pairs,
487 const unsigned int row_index_shift = 0)
488{
489 id_swap_pairs.resize(id_swaps.size());
490 for (const auto i : index_range(id_swaps))
491 {
492 const auto & swaps = id_swaps[i];
493 auto & swap_pairs = id_swap_pairs[i];
494
495 if (swaps.size() % 2)
496 throw MooseException("Row ",
497 row_index_shift + i + 1,
498 " of ",
499 id_name,
500 " in ",
501 class_name,
502 " does not contain an even number of entries! Num entries: ",
503 swaps.size());
504
505 swap_pairs.reserve(swaps.size() / 2);
506 for (unsigned int j = 0; j < swaps.size(); j += 2)
507 swap_pairs[swaps[j]] = swaps[j + 1];
508 }
509}
510
520 const std::string & class_name,
521 const unsigned int num_sections,
522 const unsigned int num_integers,
523 const std::vector<std::vector<std::vector<dof_id_type>>> & elem_integers_swaps,
524 std::vector<std::unordered_map<dof_id_type, dof_id_type>> & elem_integers_swap_pairs);
525
535std::unique_ptr<ReplicatedMesh> buildBoundaryMesh(const MeshBase & input_mesh,
536 const boundary_id_type boundary_id);
537
546std::unique_ptr<ReplicatedMesh> buildLoopBoundaryOf2DMesh(const MeshBase & input_mesh,
547 const boundary_id_type boundary_id);
548
555std::unordered_map<dof_id_type, std::unordered_set<dof_id_type>>
556buildBoundaryNodeToElemMap(const MeshBase & input_mesh, const boundary_id_type boundary_id);
557
565std::set<dof_id_type> getBoundaryNodes(const MeshBase & mesh, const BoundaryID boundary_id);
566
576void createSubdomainFromSidesets(MeshBase & mesh,
577 std::vector<BoundaryName> boundary_names,
578 const SubdomainID new_subdomain_id,
579 const SubdomainName & new_subdomain_name);
580
587void convertBlockToMesh(MeshBase & source_mesh,
588 MeshBase & target_mesh,
589 const std::vector<SubdomainName> & target_blocks);
590
606 UnstructuredMesh & destination,
607 const UnstructuredMesh & source,
608 const bool avoid_merging_subdomains,
609 const bool avoid_merging_boundaries,
610 const Parallel::Communicator & communicator,
611 const bool skip_preparation = false);
612
625void buildPolyLineMesh(MeshBase & mesh,
626 const std::vector<Point> & points,
627 const bool loop,
628 const BoundaryName & start_boundary,
629 const BoundaryName & end_boundary,
630 const std::vector<unsigned int> & nums_edges_between_points);
631
649void buildPolyLineMesh(MeshBase & mesh,
650 const std::vector<Point> & points,
651 const std::vector<Point> & mid_points,
652 const bool loop,
653 const BoundaryName & start_boundary,
654 const BoundaryName & end_boundary,
655 const std::vector<unsigned int> & nums_edges_between_points);
656
670void buildPolyLineMesh(MeshBase & mesh,
671 const std::vector<Point> & points,
672 const bool loop,
673 const BoundaryName & start_boundary,
674 const BoundaryName & end_boundary,
675 const Real max_elem_size);
676
684void addExternalBoundary(MeshBase & mesh, const BoundaryID extern_bid, bool & has_external_bid);
685
696std::vector<unsigned int> nodalQuadraturePointToNodeMap(const Elem & elem,
697 const std::vector<Point> & q_points);
698
716std::unique_ptr<ReplicatedMesh>
717stitchMeshesPairwise(std::vector<std::unique_ptr<ReplicatedMesh>> meshes,
718 const std::function<BoundaryID(const MeshBase &)> & left_boundary_id,
719 const std::function<BoundaryID(const MeshBase &)> & right_boundary_id,
720 const bool clear_stitched_boundary_ids,
721 const bool verbose);
722}
boundary_id_type BoundaryID
subdomain_id_type SubdomainID
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
This data structure is used to store geometric and variable related metadata about each cell face in ...
Definition FaceInfo.h:38
Real & faceCoord()
Sets/gets the coordinate transformation factor (for e.g.
Definition FaceInfo.h:68
const Point & faceCentroid() const
Returns the coordinates of the face centroid.
Definition FaceInfo.h:75
MeshGenerators are objects that can modify or add to an existing mesh.
Provides a way for users to bail out of the current solve.
std::unique_ptr< ReplicatedMesh > stitchMeshesPairwise(std::vector< std::unique_ptr< ReplicatedMesh > > meshes, const std::function< BoundaryID(const MeshBase &)> &left_boundary_id, const std::function< BoundaryID(const MeshBase &)> &right_boundary_id, const bool clear_stitched_boundary_ids, const bool verbose)
Stitch a list of meshes into one by repeatedly stitching neighboring pairs, so that the cost is O(N l...
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.
std::unordered_map< boundary_id_type, boundary_id_type > remapSecondaryBoundaryIDsForStitching(MeshBase &mesh, MeshBase &secondary_mesh, const std::set< boundary_id_type > &ids_to_preserve={})
Renumber the boundaries of a mesh about to be stitched into another so that the boundary names of bot...
void computeFiniteVolumeCoords(FaceInfo &fi, const Moose::CoordinateSystemType coord_type, const unsigned int rz_radial_coord=libMesh::invalid_uint)
void coordTransformFactorRZGeneral(const P &point, const std::pair< Point, RealVectorValue > &axis, C &factor)
Computes a coordinate transformation factor for a general axisymmetric axis.
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.
Q getIDFromName(const T &name)
Converts a given name (BoundaryName or SubdomainName) that is known to only contain digits into a cor...
std::unordered_map< subdomain_id_type, subdomain_id_type > remapSecondarySubdomainIDsForStitching(MeshBase &mesh, MeshBase &secondary_mesh, bool merge_same_name_across_ids=true)
Renumber the subdomains of a mesh about to be stitched into another so that the subdomain names of bo...
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 copyIntoMesh(MeshGenerator &mg, UnstructuredMesh &destination, const UnstructuredMesh &source, const bool avoid_merging_subdomains, const bool avoid_merging_boundaries, const Parallel::Communicator &communicator, const bool skip_preparation=false)
Helper function for copying one mesh into another.
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.
C computeDistanceToAxis(const P &point, const Point &origin, const RealVectorValue &direction)
Computes the distance to a general axis.
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...
void coordTransformFactor(const P &point, C &factor, const Moose::CoordinateSystemType coord_type, const unsigned int rz_radial_coord=libMesh::invalid_uint)
Compute a coordinate transformation volume integration factor.
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.
CoordinateSystemType
Definition MooseTypes.h:869
@ COORD_RZ
Definition MooseTypes.h:871
@ COORD_RSPHERICAL
Definition MooseTypes.h:872
@ COORD_XYZ
Definition MooseTypes.h:870
int8_t boundary_id_type
const unsigned int invalid_uint
ElemSidePair(Elem *elem_in, unsigned short int side_in)