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 
19 namespace MooseMeshUtils
20 {
21 
22 // Used to temporarily store information about which lower-dimensional
23 // sides to add and what subdomain id to use for the added sides.
25 {
26  ElemSidePair(Elem * elem_in, unsigned short int side_in) : elem(elem_in), side(side_in) {}
27 
28  Elem * elem;
29  unsigned short int side;
30 };
31 
37 void mergeBoundaryIDsWithSameName(MeshBase & mesh);
38 
47 void changeBoundaryId(MeshBase & mesh,
48  const libMesh::boundary_id_type old_id,
49  const libMesh::boundary_id_type new_id,
50  bool delete_prev);
51 
59 void
60 changeSubdomainId(MeshBase & mesh, const subdomain_id_type old_id, const subdomain_id_type new_id);
61 
71 std::vector<BoundaryID> getBoundaryIDs(const libMesh::MeshBase & mesh,
72  const std::vector<BoundaryName> & boundary_name,
73  bool generate_unknown,
74  const std::set<BoundaryID> & mesh_boundary_ids);
75 
85 std::vector<BoundaryID> getBoundaryIDs(const libMesh::MeshBase & mesh,
86  const std::vector<BoundaryName> & boundary_name,
87  bool generate_unknown);
88 
99 std::set<BoundaryID> getBoundaryIDSet(const libMesh::MeshBase & mesh,
100  const std::vector<BoundaryName> & boundary_name,
101  bool generate_unknown);
102 
109 BoundaryID getBoundaryID(const BoundaryName & boundary_name, const MeshBase & mesh);
110 
117 SubdomainID getSubdomainID(const SubdomainName & subdomain_name, const MeshBase & mesh);
118 
126 std::vector<subdomain_id_type> getSubdomainIDs(const libMesh::MeshBase & mesh,
127  const std::vector<SubdomainName> & subdomain_name);
128 std::set<subdomain_id_type> getSubdomainIDs(const libMesh::MeshBase & mesh,
129  const std::set<SubdomainName> & subdomain_name);
130 
136 Point meshCentroidCalculator(const MeshBase & mesh);
137 
148 template <typename P, typename C>
149 void
151  C & factor,
152  const Moose::CoordinateSystemType coord_type,
153  const unsigned int rz_radial_coord = libMesh::invalid_uint)
154 {
155  switch (coord_type)
156  {
157  case Moose::COORD_XYZ:
158  factor = 1.0;
159  break;
160  case Moose::COORD_RZ:
161  {
162  mooseAssert(rz_radial_coord != libMesh::invalid_uint,
163  "Must pass in a valid rz radial coordinate");
164  factor = 2 * M_PI * point(rz_radial_coord);
165  break;
166  }
168  factor = 4 * M_PI * point(0) * point(0);
169  break;
170  default:
171  mooseError("Unknown coordinate system");
172  }
173 }
174 
182 template <typename P, typename C>
183 C
184 computeDistanceToAxis(const P & point, const Point & origin, const RealVectorValue & direction)
185 {
186  return (point - origin).cross(direction).norm();
187 }
188 
197 template <typename P, typename C>
198 void
200  const std::pair<Point, RealVectorValue> & axis,
201  C & factor)
202 {
203  factor = 2 * M_PI * computeDistanceToAxis<P, C>(point, axis.first, axis.second);
204 }
205 
206 inline void
208  const Moose::CoordinateSystemType coord_type,
209  const unsigned int rz_radial_coord = libMesh::invalid_uint)
210 {
211  coordTransformFactor(fi.faceCentroid(), fi.faceCoord(), coord_type, rz_radial_coord);
212 }
213 
228 std::unordered_map<dof_id_type, dof_id_type>
229 getExtraIDUniqueCombinationMap(const MeshBase & mesh,
230  const std::set<SubdomainID> & block_ids,
231  std::vector<ExtraElementIDName> extra_ids);
232 
241 bool isCoPlanar(const std::vector<Point> vec_pts, const Point plane_nvec, const Point fixed_pt);
242 
249 bool isCoPlanar(const std::vector<Point> vec_pts, const Point plane_nvec);
250 
256 bool isCoPlanar(const std::vector<Point> vec_pts);
257 
263 SubdomainID getNextFreeSubdomainID(MeshBase & input_mesh);
264 
271 BoundaryID getNextFreeBoundaryID(MeshBase & input_mesh);
277 bool hasSubdomainID(const MeshBase & input_mesh, const SubdomainID & id);
278 
284 bool hasSubdomainName(const MeshBase & input_mesh, const SubdomainName & name);
285 
291 bool hasBoundaryID(const MeshBase & input_mesh, const BoundaryID id);
292 
298 bool hasBoundaryName(const MeshBase & input_mesh, const BoundaryName & name);
299 
310 void makeOrderedNodeList(std::vector<std::pair<dof_id_type, dof_id_type>> & node_assm,
311  std::vector<dof_id_type> & elem_id_list,
312  std::vector<dof_id_type> & midpoint_node_list,
313  std::vector<dof_id_type> & ordered_node_list,
314  std::vector<dof_id_type> & ordered_elem_id_list);
315 
324 void makeOrderedNodeList(std::vector<std::pair<dof_id_type, dof_id_type>> & node_assm,
325  std::vector<dof_id_type> & elem_id_list,
326  std::vector<dof_id_type> & ordered_node_list,
327  std::vector<dof_id_type> & ordered_elem_id_list);
328 
336 template <typename T, typename Q>
337 Q
338 getIDFromName(const T & name)
339 {
341  mooseError(
342  "'name' ", name, " should only contain digits that can be converted to a numerical type.");
343  long long id = std::stoll(name);
344  Q id_Q = Q(id);
346  mooseError(MooseUtils::prettyCppType<T>(&name),
347  " ",
348  name,
349  " is not within the numeric limits of the expected ID type ",
350  MooseUtils::prettyCppType<Q>(&id_Q),
351  ".");
352 
353  return id_Q;
354 }
355 
362 void swapNodesInElem(Elem & elem, const unsigned int nd1, const unsigned int nd2);
363 
373 template <typename T>
374 void
375 idSwapParametersProcessor(const std::string & class_name,
376  const std::string & id_name,
377  const std::vector<std::vector<T>> & id_swaps,
378  std::vector<std::unordered_map<T, T>> & id_swap_pairs,
379  const unsigned int row_index_shift = 0)
380 {
381  id_swap_pairs.resize(id_swaps.size());
382  for (const auto i : index_range(id_swaps))
383  {
384  const auto & swaps = id_swaps[i];
385  auto & swap_pairs = id_swap_pairs[i];
386 
387  if (swaps.size() % 2)
388  throw MooseException("Row ",
389  row_index_shift + i + 1,
390  " of ",
391  id_name,
392  " in ",
393  class_name,
394  " does not contain an even number of entries! Num entries: ",
395  swaps.size());
396 
397  swap_pairs.reserve(swaps.size() / 2);
398  for (unsigned int j = 0; j < swaps.size(); j += 2)
399  swap_pairs[swaps[j]] = swaps[j + 1];
400  }
401 }
402 
412  const std::string & class_name,
413  const unsigned int num_sections,
414  const unsigned int num_integers,
415  const std::vector<std::vector<std::vector<dof_id_type>>> & elem_integers_swaps,
416  std::vector<std::unordered_map<dof_id_type, dof_id_type>> & elem_integers_swap_pairs);
417 
427 std::unique_ptr<ReplicatedMesh> buildBoundaryMesh(const ReplicatedMesh & input_mesh,
428  const boundary_id_type boundary_id);
429 
439 void createSubdomainFromSidesets(std::unique_ptr<MeshBase> & mesh,
440  std::vector<BoundaryName> boundary_names,
441  const SubdomainID new_subdomain_id,
442  const SubdomainName new_subdomain_name,
443  const std::string type_name);
444 
451 void convertBlockToMesh(std::unique_ptr<MeshBase> & source_mesh,
452  std::unique_ptr<MeshBase> & target_mesh,
453  const std::vector<SubdomainName> & target_blocks);
454 }
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 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)
Crate 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.
std::unique_ptr< ReplicatedMesh > buildBoundaryMesh(const ReplicatedMesh &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...
const unsigned int invalid_uint
bool hasBoundaryName(const MeshBase &input_mesh, const BoundaryName &name)
Whether a particular boundary name exists in the mesh.
void createSubdomainFromSidesets(std::unique_ptr< 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...
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:323
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 factor
const Point & faceCentroid() const
Returns the coordinates of the face centroid.
Definition: FaceInfo.h:71
void coordTransformFactorRZGeneral(const P &point, const std::pair< Point, RealVectorValue > &axis, C &factor)
Computes a coordinate transformation factor for a general axisymmetric axis.
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:64
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.
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)
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:36
ElemSidePair(Elem *elem_in, unsigned short int side_in)
int8_t boundary_id_type
boundary_id_type BoundaryID
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.
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...
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.
Provides a way for users to bail out of the current solve.
CoordinateSystemType
Definition: MooseTypes.h:810
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.
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. ...
void convertBlockToMesh(std::unique_ptr< MeshBase > &source_mesh, std::unique_ptr< MeshBase > &target_mesh, const std::vector< SubdomainName > &target_blocks)
Convert a list of blocks in a given mesh to a standalone new mesh.
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...
bool isDigits(const std::string &str)
Courtesy https://stackoverflow.com/a/8889045 and https://en.cppreference.com/w/cpp/string/byte/isdigi...
Definition: MooseUtils.h:1186
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)
auto index_range(const T &sizable)