https://mooseframework.inl.gov
SideSetsFromNodeSetsGenerator.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 
11 
12 #include "CastUniquePointer.h"
13 #include "libmesh/boundary_info.h"
14 #include "libmesh/elem_side_builder.h"
15 
17 
20 {
22 
23  params.addClassDescription("Mesh generator which constructs side sets from node sets");
24  params.addRequiredParam<MeshGeneratorName>("input",
25  "Input mesh the operation will be applied to");
26  params.addParam<std::vector<BoundaryName>>(
27  "nodesets_to_convert",
28  "If specified, list of nodesets to convert. If not specified, all nodesets are converted");
29  return params;
30 }
31 
33  : MeshGenerator(parameters), _input(getMesh("input"))
34 {
35 }
36 
37 std::unique_ptr<MeshBase>
39 {
40  if (!isParamValid("nodesets_to_convert"))
41  _input->get_boundary_info().build_side_list_from_node_list();
42  else
43  {
44  const auto & nodeset_names = getParam<std::vector<BoundaryName>>("nodesets_to_convert");
45  auto & binfo = _input->get_boundary_info();
46 
47  std::map<BoundaryName, BoundaryID> new_nodeset_ids;
48  std::set<BoundaryID> nodeset_ids;
49 
50  for (const auto & nodeset_name : nodeset_names)
51  {
52  // Look through the nodeset map.
54  for (const auto & [id, name] : binfo.get_nodeset_name_map())
55  if (name == nodeset_name)
56  nodeset_id = id;
57  if (MooseUtils::isDigits(nodeset_name))
58  nodeset_id = std::stoi(nodeset_name);
59  if (nodeset_id == std::numeric_limits<BoundaryID>::max())
60  paramError("nodesets_to_convert",
61  "Nodeset '" + nodeset_name + "' does not exist in the input mesh");
62  nodeset_ids.insert(nodeset_id);
63 
64  // Find the target ID if there is already a sideset with that name
65  if (MooseUtils::isDigits(nodeset_name))
66  new_nodeset_ids[nodeset_name] = nodeset_id;
67  else if (binfo.get_sideset_name(nodeset_id) == nodeset_name)
68  new_nodeset_ids[nodeset_name] = binfo.get_id_by_name(nodeset_name);
69  else
70  new_nodeset_ids[nodeset_name] = nodeset_id;
71  }
72 
73  // Copy pasted from libMesh's build_side_list_from_node_list
74  // For avoiding extraneous element side construction
75  libMesh::ElemSideBuilder side_builder;
76  // Pull objects out of the loop to reduce heap operations
77  const Elem * side_elem;
78  const auto & boundary_node_ids = binfo.get_nodeset_map();
79 
80  for (const auto & elem : _input->active_element_ptr_range())
81  for (auto side : elem->side_index_range())
82  {
83  side_elem = &side_builder(*elem, side);
84 
85  // map from nodeset_id to count for that ID
86  std::map<boundary_id_type, unsigned> nodesets_node_count;
87 
88  // For each nodeset that this node is a member of, increment the associated
89  // nodeset ID count
90  for (const auto & node : side_elem->node_ref_range())
91  for (const auto & pr : as_range(boundary_node_ids.equal_range(&node)))
92  // Single added new line
93  if (nodeset_ids.count(pr.second))
94  nodesets_node_count[pr.second]++;
95 
96  // Now check to see what nodeset_counts have the correct
97  // number of nodes in them. For any that do, add this side to
98  // the sideset, making sure the sideset inherits the
99  // nodeset's name, if there is one.
100  for (const auto & pr : nodesets_node_count)
101  if (pr.second == side_elem->n_nodes())
102  binfo.add_side(elem, side, pr.first);
103 
104  // Add nodeset name in case it does not exist yet
105  for (const auto & [nodeset_name, new_nodeset_id] : new_nodeset_ids)
106  binfo.sideset_name(new_nodeset_id) = nodeset_name;
107  }
108  }
109 
110  return dynamic_pointer_cast<MeshBase>(_input);
111 }
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition: MooseBase.h:439
MeshGenerator for constructing side Sets from node sets.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
std::unique_ptr< T_DEST, T_DELETER > dynamic_pointer_cast(std::unique_ptr< T_SRC, T_DELETER > &src)
These are reworked from https://stackoverflow.com/a/11003103.
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
auto max(const L &left, const R &right)
const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:103
boundary_id_type BoundaryID
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
static InputParameters validParams()
Definition: MeshGenerator.C:23
registerMooseObject("MooseApp", SideSetsFromNodeSetsGenerator)
std::unique_ptr< MeshBase > & _input
Input mesh the operation will be applied to.
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object...
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseBase.h:199
SideSetsFromNodeSetsGenerator(const InputParameters &parameters)
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
MeshGenerators are objects that can modify or add to an existing mesh.
Definition: MeshGenerator.h:32
for(PetscInt i=0;i< nvars;++i)