www.mooseframework.org
RenameBoundaryGenerator.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 
11 #include "CastUniquePointer.h"
12 
13 #include "libmesh/mesh_modification.h"
14 
16 
17 template <>
20 {
22 
23  params.addRequiredParam<MeshGeneratorName>("input", "The mesh we want to modify");
24  params.addParam<std::vector<boundary_id_type>>(
25  "old_boundary_id",
26  "Elements with this boundary number will be given the new_boundary_number or "
27  "new_boundary_name. You must supply either old_boundary_id or old_boundary_name. "
28  "You may supply a vector of old_boundary_id, in which case the new_boundary "
29  "information must also be a vector.");
30  params.addParam<std::vector<BoundaryName>>(
31  "old_boundary_name",
32  "Elements with this boundary name will be given the new_boundary_number or "
33  "new_boundary_name. You must supply either old_boundary_id or old_boundary_name. "
34  "You may supply a vector of old_boundary_name, in which case the new_boundary "
35  "information must also be a vector.");
36  params.addParam<std::vector<boundary_id_type>>(
37  "new_boundary_id",
38  "Elements with the old boundary number (or name) will be given this boundary "
39  "number. If the old boundaries are named, their names will be passed onto the "
40  "newly numbered boundarys.");
41  params.addParam<std::vector<BoundaryName>>(
42  "new_boundary_name",
43  "Elements with the old boundary number (or name) will be given this boundary "
44  "name. No change of boundary ID is performed, unless multiple old boundaries are "
45  "given the same name, in which case they are all given the first old boundary "
46  "number.");
47  params.addClassDescription(
48  "RenameBoundaryGenerator re-numbers or re-names an old_boundary_id or old_boundary_name "
49  "with a new_boundary_id or new_boundary_name. If using RenameBoundaryGenerator to "
50  "merge boundaries (by giving them the same name, for instance) it is "
51  "advisable to specify all your boundaries in old_boundaries to avoid inconsistencies");
52 
53  return params;
54 }
55 
57  : MeshGenerator(parameters), _input(getMesh("input"))
58 {
59  // error checking. Must have exactly one of old_boundary_id or old_boundary_name
60  if (isParamValid("old_boundary_id") && isParamValid("old_boundary_name"))
61  mooseError("RenameBoundaryGenerator: You must supply exactly one of old_boundary_id or "
62  "old_boundary_name\n");
63  else if (!isParamValid("old_boundary_id") && !isParamValid("old_boundary_name"))
64  mooseError("RenameBoundaryGenerator: You must supply exactly one of old_boundary_id or "
65  "old_boundary_name\n");
66 
67  // error checking. Must have exactly one of new_boundary_id or new_boundary_name
68  // In principal we could have both (the old boundary would then be given a new ID and a new name)
69  // but i feel that could lead to confusion for the user. If the user wants to do that they
70  // should use two of these RenameBoundary MeshModifiers.
71  if (isParamValid("new_boundary_id") && isParamValid("new_boundary_name"))
72  mooseError("RenameBoundaryGenerator: You must supply exactly one of new_boundary_id or "
73  "new_boundary_name\n");
74  else if (!isParamValid("new_boundary_id") && !isParamValid("new_boundary_name"))
75  mooseError("RenameBoundaryGenerator: You must supply exactly one of new_boundary_id or "
76  "new_boundary_name\n");
77 }
78 
79 std::unique_ptr<MeshBase>
81 {
82  std::unique_ptr<MeshBase> mesh = std::move(_input);
83 
84  auto & boundary_info = mesh->get_boundary_info();
85 
86  // grab the user input. Can't do all this in the constructor as some things may not
87  // have been put into the mesh yet, eg old_boundary_name might have been inserted by
88  // another MeshGenerator
89  if (isParamValid("old_boundary_id"))
90  {
91  // user must have supplied old_boundary_id
92  _old_boundary_id = getParam<std::vector<boundary_id_type>>("old_boundary_id");
93  }
94 
95  if (isParamValid("old_boundary_name"))
96  {
97  _old_boundary_name = getParam<std::vector<BoundaryName>>("old_boundary_id");
98 
99  _old_boundary_id.reserve(_old_boundary_name.size());
100 
101  for (auto & old_name : _old_boundary_name)
102  _old_boundary_id.emplace_back(boundary_info.get_id_by_name(old_name));
103  }
104 
105  if (isParamValid("new_boundary_id"))
106  {
107  _new_boundary_id = getParam<std::vector<boundary_id_type>>("new_boundary_id");
108 
109  if (_new_boundary_id.size() != _old_boundary_id.size())
110  mooseError(
111  "RenameBoundaryGenerator: The vector of old_boundary information must have the same"
112  " length as the vector of new_boundary information\n");
113 
114  for (unsigned int i = 0; i < _new_boundary_id.size(); i++)
115  MeshTools::Modification::change_boundary_id(*mesh, _old_boundary_id[i], _new_boundary_id[i]);
116 
117  return dynamic_pointer_cast<MeshBase>(mesh);
118  }
119 
120  if (isParamValid("new_boundary_name"))
121  {
122  _new_boundary_name = getParam<std::vector<BoundaryName>>("new_boundary_name");
123 
124  if (_new_boundary_name.size() != _old_boundary_id.size())
125  mooseError(
126  "RenameBoundaryGenerator: The vector of old_boundary information must have the same"
127  " length as the vector of new_boundary information\n");
128 
129  for (unsigned int i = 0; i < _new_boundary_name.size(); i++)
130  {
131  boundary_info.sideset_name(_old_boundary_id[i]) = _new_boundary_name[i];
132  boundary_info.nodeset_name(_old_boundary_id[i]) = _new_boundary_name[i];
133  }
134  return dynamic_pointer_cast<MeshBase>(mesh);
135  }
136 
137  mooseError("Must supply one of either new_boundary_id or new_boundary_name");
138 }
std::unique_ptr< MeshBase > & _input
MeshGenerator for re-numbering or re-naming boundaries.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::vector< boundary_id_type > _old_boundary_id
std::vector< BoundaryName > _old_boundary_name
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 mooseError(Args &&... args) const
Definition: MooseObject.h:147
registerMooseObject("MooseApp", RenameBoundaryGenerator)
InputParameters validParams< RenameBoundaryGenerator >()
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...
std::vector< BoundaryName > _new_boundary_name
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
RenameBoundaryGenerator(const InputParameters &parameters)
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 option parameter and a documentation string to the InputParameters object...
std::vector< boundary_id_type > _new_boundary_id
InputParameters validParams< MeshGenerator >()
Definition: MeshGenerator.C:16
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:89
MeshGenerators are objects that can modify or add to an existing mesh.
Definition: MeshGenerator.h:30