www.mooseframework.org
MooseMeshUtils.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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/replicated_mesh.h"
16 #include "libmesh/mesh_base.h"
17 
18 namespace MooseMeshUtils
19 {
20 void
21 changeBoundaryId(MeshBase & mesh,
22  const libMesh::boundary_id_type old_id,
23  const libMesh::boundary_id_type new_id,
24  bool delete_prev)
25 {
26  // Get a reference to our BoundaryInfo object, we will use it several times below...
27  libMesh::BoundaryInfo & boundary_info = mesh.get_boundary_info();
28 
29  // Container to catch ids passed back from BoundaryInfo
30  std::vector<libMesh::boundary_id_type> old_ids;
31 
32  // Only level-0 elements store BCs. Loop over them.
33  for (auto & elem : as_range(mesh.level_elements_begin(0), mesh.level_elements_end(0)))
34  {
35  unsigned int n_sides = elem->n_sides();
36  for (unsigned int s = 0; s != n_sides; ++s)
37  {
38  boundary_info.boundary_ids(elem, s, old_ids);
39  if (std::find(old_ids.begin(), old_ids.end(), old_id) != old_ids.end())
40  {
41  std::vector<libMesh::boundary_id_type> new_ids(old_ids);
42  std::replace(new_ids.begin(), new_ids.end(), old_id, new_id);
43  if (delete_prev)
44  {
45  boundary_info.remove_side(elem, s);
46  boundary_info.add_side(elem, s, new_ids);
47  }
48  else
49  boundary_info.add_side(elem, s, new_ids);
50  }
51  }
52  }
53 
54  // Remove any remaining references to the old ID from the
55  // BoundaryInfo object. This prevents things like empty sidesets
56  // from showing up when printing information, etc.
57  if (delete_prev)
58  boundary_info.remove_id(old_id);
59 }
60 
61 std::vector<libMesh::boundary_id_type>
62 getBoundaryIDs(const libMesh::MeshBase & mesh,
63  const std::vector<BoundaryName> & boundary_name,
64  bool generate_unknown)
65 {
66  const libMesh::BoundaryInfo & boundary_info = mesh.get_boundary_info();
67  const std::map<libMesh::boundary_id_type, std::string> & sideset_map =
68  boundary_info.get_sideset_name_map();
69  const std::map<libMesh::boundary_id_type, std::string> & nodeset_map =
70  boundary_info.get_nodeset_name_map();
71 
72  std::set<libMesh::boundary_id_type> boundary_ids = boundary_info.get_boundary_ids();
73 
74  // On a distributed mesh we may have boundary ids that only exist on
75  // other processors.
76  if (!mesh.is_replicated())
77  mesh.comm().set_union(boundary_ids);
78 
79  libMesh::boundary_id_type max_boundary_id = boundary_ids.empty() ? 0 : *(boundary_ids.rbegin());
80 
81  std::vector<libMesh::boundary_id_type> ids(boundary_name.size());
82  for (unsigned int i = 0; i < boundary_name.size(); i++)
83  {
84  if (boundary_name[i] == "ANY_BOUNDARY_ID")
85  {
86  ids.assign(boundary_ids.begin(), boundary_ids.end());
87  if (i)
88  mooseWarning("You passed \"ANY_BOUNDARY_ID\" in addition to other boundary_names. This "
89  "may be a logic error.");
90  break;
91  }
92 
93  libMesh::boundary_id_type id;
94  std::istringstream ss(boundary_name[i]);
95 
96  if (!(ss >> id) || !ss.eof())
97  {
103  if (generate_unknown &&
104  !MooseUtils::doesMapContainValue(sideset_map, std::string(boundary_name[i])) &&
105  !MooseUtils::doesMapContainValue(nodeset_map, std::string(boundary_name[i])))
106  id = ++max_boundary_id;
107  else
108  id = boundary_info.get_id_by_name(boundary_name[i]);
109  }
110 
111  ids[i] = id;
112  }
113 
114  return ids;
115 }
116 
117 std::vector<subdomain_id_type>
118 getSubdomainIDs(const libMesh::MeshBase & mesh, const std::vector<SubdomainName> & subdomain_name)
119 {
120  std::vector<subdomain_id_type> ids(subdomain_name.size());
121  std::set<subdomain_id_type> mesh_subdomains;
122  mesh.subdomain_ids(mesh_subdomains);
123 
124  for (unsigned int i = 0; i < subdomain_name.size(); i++)
125  {
126  if (subdomain_name[i] == "ANY_BLOCK_ID")
127  {
128  ids.assign(mesh_subdomains.begin(), mesh_subdomains.end());
129  if (i)
130  mooseWarning("You passed \"ANY_BLOCK_ID\" in addition to other block names. This may be a "
131  "logic error.");
132  break;
133  }
134 
135  subdomain_id_type id = Moose::INVALID_BLOCK_ID;
136  std::istringstream ss(subdomain_name[i]);
137 
138  if (!(ss >> id) || !ss.eof())
139  id = mesh.get_id_by_name(subdomain_name[i]);
140 
141  ids[i] = id;
142  }
143 
144  return ids;
145 }
146 }
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:219
bool doesMapContainValue(const std::map< T1, T2 > &the_map, const T2 &value)
This routine is a simple helper function for searching a map by values instead of keys...
Definition: MooseUtils.h:208
std::vector< subdomain_id_type > getSubdomainIDs(const libMesh::MeshBase &mesh, const std::vector< SubdomainName > &subdomain_name)
const SubdomainID INVALID_BLOCK_ID
Definition: MooseTypes.C:16
std::vector< libMesh::boundary_id_type > getBoundaryIDs(const libMesh::MeshBase &mesh, const std::vector< BoundaryName > &boundary_name, bool generate_unknown)
void changeBoundaryId(MeshBase &mesh, const libMesh::boundary_id_type old_id, const libMesh::boundary_id_type new_id, bool delete_prev)