https://mooseframework.inl.gov
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 "libmesh/replicated_mesh.h"
13 #include "libmesh/boundary_info.h"
14 
15 #include "MooseUtils.h"
16 #include "MooseTypes.h"
17 #include "FaceInfo.h"
18 #include "MeshGenerator.h"
19 
20 // Utilities for MeshBase
21 // Many of these utilities could live in libMesh, and in fact, before adding a new one here
22 // you should also check mesh_tools.h in libMesh to see if it does not exist there already.
23 
24 namespace MooseMeshUtils
25 {
26 
27 // Used to temporarily store information about which lower-dimensional
28 // sides to add and what subdomain id to use for the added sides.
30 {
31  ElemSidePair(Elem * elem_in, unsigned short int side_in) : elem(elem_in), side(side_in) {}
32 
33  Elem * elem;
34  unsigned short int side;
35 };
36 
42 void mergeBoundaryIDsWithSameName(MeshBase & mesh);
43 
52 void changeBoundaryId(MeshBase & mesh,
53  const libMesh::boundary_id_type old_id,
54  const libMesh::boundary_id_type new_id,
55  bool delete_prev);
56 
64 void
65 changeSubdomainId(MeshBase & mesh, const subdomain_id_type old_id, const subdomain_id_type new_id);
66 
76 std::vector<BoundaryID> getBoundaryIDs(const libMesh::MeshBase & mesh,
77  const std::vector<BoundaryName> & boundary_name,
78  bool generate_unknown,
79  const std::set<BoundaryID> & mesh_boundary_ids);
80 
90 std::vector<BoundaryID> getBoundaryIDs(const libMesh::MeshBase & mesh,
91  const std::vector<BoundaryName> & boundary_name,
92  bool generate_unknown);
93 
104 std::set<BoundaryID> getBoundaryIDSet(const libMesh::MeshBase & mesh,
105  const std::vector<BoundaryName> & boundary_name,
106  bool generate_unknown);
107 
114 BoundaryID getBoundaryID(const BoundaryName & boundary_name, const MeshBase & mesh);
115 
122 SubdomainID getSubdomainID(const SubdomainName & subdomain_name, const MeshBase & mesh);
123 
131 std::vector<subdomain_id_type> getSubdomainIDs(const libMesh::MeshBase & mesh,
132  const std::vector<SubdomainName> & subdomain_name);
133 std::set<subdomain_id_type> getSubdomainIDs(const libMesh::MeshBase & mesh,
134  const std::set<SubdomainName> & subdomain_name);
135 
141 Point meshCentroidCalculator(const MeshBase & mesh);
142 
149 Point boundaryCentroidCalculator(const BoundaryName & boundary, MeshBase & mesh);
150 
157 RealVectorValue boundaryWeightedNormal(const BoundaryName & boundary, MeshBase & mesh);
158 
169 template <typename P, typename C>
170 void
172  C & factor,
173  const Moose::CoordinateSystemType coord_type,
174  const unsigned int rz_radial_coord = libMesh::invalid_uint)
175 {
176  switch (coord_type)
177  {
178  case Moose::COORD_XYZ:
179  factor = 1.0;
180  break;
181  case Moose::COORD_RZ:
182  {
183  mooseAssert(rz_radial_coord != libMesh::invalid_uint,
184  "Must pass in a valid rz radial coordinate");
185  factor = 2 * M_PI * point(rz_radial_coord);
186  break;
187  }
189  factor = 4 * M_PI * point(0) * point(0);
190  break;
191  default:
192  mooseError("Unknown coordinate system");
193  }
194 }
195 
203 template <typename P, typename C>
204 C
205 computeDistanceToAxis(const P & point, const Point & origin, const RealVectorValue & direction)
206 {
207  return (point - origin).cross(direction).norm();
208 }
209 
217 Real computeMaxDistanceToAxis(const MeshBase & mesh,
218  const Point & origin,
219  const RealVectorValue & direction);
220 
229 template <typename P, typename C>
230 void
232  const std::pair<Point, RealVectorValue> & axis,
233  C & factor)
234 {
235  factor = 2 * M_PI * computeDistanceToAxis<P, C>(point, axis.first, axis.second);
236 }
237 
238 inline void
240  const Moose::CoordinateSystemType coord_type,
241  const unsigned int rz_radial_coord = libMesh::invalid_uint)
242 {
243  coordTransformFactor(fi.faceCentroid(), fi.faceCoord(), coord_type, rz_radial_coord);
244 }
245 
260 std::unordered_map<dof_id_type, dof_id_type>
261 getExtraIDUniqueCombinationMap(const MeshBase & mesh,
262  const std::set<SubdomainID> & block_ids,
263  std::vector<ExtraElementIDName> extra_ids);
264 
273 bool isCoPlanar(const std::vector<Point> & vec_pts, const Point plane_nvec, const Point fixed_pt);
274 
281 bool isCoPlanar(const std::vector<Point> & vec_pts, const Point plane_nvec);
282 
288 bool isCoPlanar(const std::vector<Point> & vec_pts);
289 
295 SubdomainID getNextFreeSubdomainID(MeshBase & input_mesh);
296 
302 BoundaryID getNextFreeBoundaryID(MeshBase & input_mesh);
303 
309 bool hasSubdomainID(const MeshBase & input_mesh, const SubdomainID & id);
310 
316 bool hasSubdomainName(const MeshBase & input_mesh, const SubdomainName & name);
317 
323 bool hasBoundaryID(const MeshBase & input_mesh, const BoundaryID id);
324 
330 bool hasBoundaryName(const MeshBase & input_mesh, const BoundaryName & name);
331 
342 void makeOrderedNodeList(std::vector<std::pair<dof_id_type, dof_id_type>> & node_assm,
343  std::vector<dof_id_type> & elem_id_list,
344  std::vector<dof_id_type> & midpoint_node_list,
345  std::vector<dof_id_type> & ordered_node_list,
346  std::vector<dof_id_type> & ordered_elem_id_list);
347 
356 void makeOrderedNodeList(std::vector<std::pair<dof_id_type, dof_id_type>> & node_assm,
357  std::vector<dof_id_type> & elem_id_list,
358  std::vector<dof_id_type> & ordered_node_list,
359  std::vector<dof_id_type> & ordered_elem_id_list);
360 
368 template <typename T, typename Q>
369 Q
370 getIDFromName(const T & name)
371 {
372  if (!MooseUtils::isDigits(name))
373  mooseError(
374  "'name' ", name, " should only contain digits that can be converted to a numerical type.");
375  long long id = std::stoll(name);
376  Q id_Q = Q(id);
378  mooseError(MooseUtils::prettyCppType<T>(&name),
379  " ",
380  name,
381  " is not within the numeric limits of the expected ID type ",
382  MooseUtils::prettyCppType<Q>(&id_Q),
383  ".");
384 
385  return id_Q;
386 }
387 
394 void swapNodesInElem(Elem & elem, const unsigned int nd1, const unsigned int nd2);
395 
405 template <typename T>
406 void
407 idSwapParametersProcessor(const std::string & class_name,
408  const std::string & id_name,
409  const std::vector<std::vector<T>> & id_swaps,
410  std::vector<std::unordered_map<T, T>> & id_swap_pairs,
411  const unsigned int row_index_shift = 0)
412 {
413  id_swap_pairs.resize(id_swaps.size());
414  for (const auto i : index_range(id_swaps))
415  {
416  const auto & swaps = id_swaps[i];
417  auto & swap_pairs = id_swap_pairs[i];
418 
419  if (swaps.size() % 2)
420  throw MooseException("Row ",
421  row_index_shift + i + 1,
422  " of ",
423  id_name,
424  " in ",
425  class_name,
426  " does not contain an even number of entries! Num entries: ",
427  swaps.size());
428 
429  swap_pairs.reserve(swaps.size() / 2);
430  for (unsigned int j = 0; j < swaps.size(); j += 2)
431  swap_pairs[swaps[j]] = swaps[j + 1];
432  }
433 }
434 
444  const std::string & class_name,
445  const unsigned int num_sections,
446  const unsigned int num_integers,
447  const std::vector<std::vector<std::vector<dof_id_type>>> & elem_integers_swaps,
448  std::vector<std::unordered_map<dof_id_type, dof_id_type>> & elem_integers_swap_pairs);
449 
459 std::unique_ptr<ReplicatedMesh> buildBoundaryMesh(const MeshBase & input_mesh,
460  const boundary_id_type boundary_id);
461 
470 std::unique_ptr<ReplicatedMesh> buildLoopBoundaryOf2DMesh(const MeshBase & input_mesh,
471  const boundary_id_type boundary_id);
472 
479 std::unordered_map<dof_id_type, std::unordered_set<dof_id_type>>
480 buildBoundaryNodeToElemMap(const MeshBase & input_mesh, const boundary_id_type boundary_id);
481 
489 std::set<dof_id_type> getBoundaryNodes(const MeshBase & mesh, const BoundaryID boundary_id);
490 
500 void createSubdomainFromSidesets(MeshBase & mesh,
501  std::vector<BoundaryName> boundary_names,
502  const SubdomainID new_subdomain_id,
503  const SubdomainName new_subdomain_name,
504  const std::string type_name);
505 
512 void convertBlockToMesh(MeshBase & source_mesh,
513  MeshBase & target_mesh,
514  const std::vector<SubdomainName> & target_blocks);
515 
527 void copyIntoMesh(MeshGenerator & mg,
528  UnstructuredMesh & destination,
529  const UnstructuredMesh & source,
530  const bool avoid_merging_subdomains,
531  const bool avoid_merging_boundaries,
532  const Parallel::Communicator & communicator);
533 
546 void buildPolyLineMesh(MeshBase & mesh,
547  const std::vector<Point> & points,
548  const bool loop,
549  const BoundaryName & start_boundary,
550  const BoundaryName & end_boundary,
551  const std::vector<unsigned int> & nums_edges_between_points);
552 
570 void buildPolyLineMesh(MeshBase & mesh,
571  const std::vector<Point> & points,
572  const std::vector<Point> & mid_points,
573  const bool loop,
574  const BoundaryName & start_boundary,
575  const BoundaryName & end_boundary,
576  const std::vector<unsigned int> & nums_edges_between_points);
577 
591 void buildPolyLineMesh(MeshBase & mesh,
592  const std::vector<Point> & points,
593  const bool loop,
594  const BoundaryName & start_boundary,
595  const BoundaryName & end_boundary,
596  const Real max_elem_size);
597 
605 void addExternalBoundary(MeshBase & mesh, const BoundaryID extern_bid, bool & has_external_bid);
606 }
void swapNodesInElem(Elem &elem, const unsigned int nd1, const unsigned int nd2)
Swap two nodes within an element.
std::string name(const ElemQuality q)
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 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...
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...
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.
const unsigned int invalid_uint
RealVectorValue boundaryWeightedNormal(const BoundaryName &boundary, MeshBase &mesh)
Calculates the side-volume weighted (side-vertex) average normal of a boundary on a mesh...
bool hasBoundaryName(const MeshBase &input_mesh, const BoundaryName &name)
Whether a particular boundary name exists in the mesh.
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.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
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.
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...
const Point & faceCentroid() const
Returns the coordinates of the face centroid.
Definition: FaceInfo.h:75
void coordTransformFactorRZGeneral(const P &point, const std::pair< Point, RealVectorValue > &axis, C &factor)
Computes a coordinate transformation factor for a general axisymmetric axis.
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...
bool hasBoundaryID(const MeshBase &input_mesh, const BoundaryID id)
Whether a particular boundary ID exists in the mesh.
Real & faceCoord()
Sets/gets the coordinate transformation factor (for e.g.
Definition: FaceInfo.h:68
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 > 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...
SubdomainID getSubdomainID(const SubdomainName &subdomain_name, const MeshBase &mesh)
Gets the subdomain ID associated with the given SubdomainName.
auto max(const L &left, const R &right)
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.
bool hasSubdomainID(const MeshBase &input_mesh, const SubdomainID &id)
Whether a particular subdomain ID exists in the mesh.
C computeDistanceToAxis(const P &point, const Point &origin, const RealVectorValue &direction)
Computes the distance to a general axis.
BoundaryID getBoundaryID(const BoundaryName &boundary_name, const MeshBase &mesh)
Gets the boundary ID associated with the given BoundaryName.
void computeFiniteVolumeCoords(FaceInfo &fi, const Moose::CoordinateSystemType coord_type, const unsigned int rz_radial_coord=libMesh::invalid_uint)
This data structure is used to store geometric and variable related metadata about each cell face in ...
Definition: FaceInfo.h:37
ElemSidePair(Elem *elem_in, unsigned short int side_in)
int8_t boundary_id_type
boundary_id_type BoundaryID
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 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...
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.
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.
bool hasSubdomainName(const MeshBase &input_mesh, const SubdomainName &name)
Whether a particular subdomain name exists in the mesh.
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.
void createSubdomainFromSidesets(MeshBase &mesh, std::vector< BoundaryName > boundary_names, const SubdomainID new_subdomain_id, const SubdomainName new_subdomain_name, const std::string type_name)
Create a new subdomain by generating new side elements from a list of sidesets in a given mesh...
Provides a way for users to bail out of the current solve.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
CoordinateSystemType
Definition: MooseTypes.h:858
Point boundaryCentroidCalculator(const BoundaryName &boundary, MeshBase &mesh)
Calculates the centroid of a boundary on a 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.
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.
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.
void mergeBoundaryIDsWithSameName(MeshBase &mesh)
Merges the boundary IDs of boundaries that have the same names but different IDs. ...
Q getIDFromName(const T &name)
Converts a given name (BoundaryName or SubdomainName) that is known to only contain digits into a cor...
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 getNextFreeBoundaryID(MeshBase &input_mesh)
Checks input mesh and returns the largest boundary ID in the mesh plus one, which is a boundary ID in...
auto min(const L &left, const R &right)
MeshGenerators are objects that can modify or add to an existing mesh.
Definition: MeshGenerator.h:33
auto index_range(const T &sizable)
void addExternalBoundary(MeshBase &mesh, const BoundaryID extern_bid, bool &has_external_bid)
Adds a sideset for the external boundary of the mesh (e.g.