www.mooseframework.org
RenameBlockGenerator.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 #include "RenameBlockGenerator.h"
11 #include "CastUniquePointer.h"
12 
13 #include "libmesh/elem.h"
14 
16 
17 template <>
20 {
22 
23  params.addRequiredParam<MeshGeneratorName>("input", "The mesh we want to modify");
24  params.addParam<std::vector<subdomain_id_type>>(
25  "old_block_id",
26  "Elements with this block number will be given the new_block_number or "
27  "new_block_name. You must supply either old_block_id or old_block_name. "
28  "You may supply a vector of old_block_id, in which case the new_block "
29  "information must also be a vector.");
30  params.addParam<std::vector<SubdomainName>>(
31  "old_block_name",
32  "Elements with this block name will be given the new_block_number or "
33  "new_block_name. You must supply either old_block_id or old_block_name. "
34  "You may supply a vector of old_block_name, in which case the new_block "
35  "information must also be a vector.");
36  params.addParam<std::vector<subdomain_id_type>>(
37  "new_block_id",
38  "Elements with the old block number (or name) will be given this block "
39  "number. If the old blocks are named, their names will be passed onto the "
40  "newly numbered blocks.");
41  params.addParam<std::vector<SubdomainName>>(
42  "new_block_name",
43  "Elements with the old block number (or name) will be given this block "
44  "name. No change of block ID is performed, unless multiple old blocks are "
45  "given the same name, in which case they are all given the first old block "
46  "number.");
47  params.addClassDescription(
48  "RenameBlock re-numbers or re-names an old_block_id or old_block_name "
49  "with a new_block_id or new_block_name. If using RenameBlock to "
50  "merge blocks (by giving them the same name, for instance) it is "
51  "advisable to specify all your blocks in old_blocks 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_block_id or old_block_name
60  if (isParamValid("old_block_id") && isParamValid("old_block_name"))
61  mooseError(
62  "RenameBlockGenerator: You must supply exactly one of old_block_id or old_block_name\n");
63  else if (!isParamValid("old_block_id") && !isParamValid("old_block_name"))
64  mooseError(
65  "RenameBlockGenerator: You must supply exactly one of old_block_id or old_block_name\n");
66 
67  // error checking. Must have exactly one of new_block_id or new_block_name
68  // In principal we could have both (the old block 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 RenameBlock MeshModifiers.
71  if (isParamValid("new_block_id") && isParamValid("new_block_name"))
72  mooseError(
73  "RenameBlockGenerator: You must supply exactly one of new_block_id or new_block_name\n");
74  else if (!isParamValid("new_block_id") && !isParamValid("new_block_name"))
75  mooseError(
76  "RenameBlockGenerator: You must supply exactly one of new_block_id or new_block_name\n");
77 }
78 
79 std::unique_ptr<MeshBase>
81 {
82  std::unique_ptr<MeshBase> mesh = std::move(_input);
83 
84  // grab the user input. Can't do all this in the constructor as some things may not
85  // have been put into the mesh yet, eg old_block_name might have been inserted by
86  // another MeshModifier
87  if (isParamValid("old_block_id"))
88  {
89  // user must have supplied old_block_id
90  _old_block_id = getParam<std::vector<subdomain_id_type>>("old_block_id");
91  _old_block_name.reserve(_old_block_id.size());
92  for (const auto & block_id : _old_block_id)
93  _old_block_name.emplace_back(mesh->subdomain_name(block_id));
94  }
95  else
96  {
97  // user must have supplied old_block_name
98  _old_block_name = getParam<std::vector<SubdomainName>>("old_block_name");
99  _old_block_id.reserve(_old_block_name.size());
100  for (const auto & block_name : _old_block_name)
101  _old_block_id.emplace_back(mesh->get_id_by_name(block_name));
102  }
103 
104  // construct new_block_id and new_block_name
105  if (isParamValid("new_block_id"))
106  {
107  _new_block_id = getParam<std::vector<subdomain_id_type>>("new_block_id");
108  if (_new_block_id.size() != _old_block_id.size())
109  mooseError("RenameBlockGenerator: The vector of old_block information must have the same"
110  " length as the vector of new_block information\n");
111 
112  // construct the _new_block_name
113  _new_block_name.reserve(_new_block_id.size());
114  for (const auto & block_id : _new_block_id)
115  _new_block_name.emplace_back(newBlockName(block_id));
116  }
117  else // must have supplied new_block_name
118  {
119  _new_block_name = getParam<std::vector<SubdomainName>>("new_block_name");
120  if (_new_block_name.size() != _old_block_id.size())
121  mooseError("RenameBlockGenerator: The vector of old_block information must have the same"
122  " length as the vector of new_block information\n");
123 
124  _new_block_id.reserve(_new_block_name.size());
125  for (const auto & block_name : _new_block_name)
126  _new_block_id.emplace_back(newBlockID(block_name));
127  }
128 
129  for (const auto & elem : mesh->active_element_ptr_range())
130  for (unsigned i = 0; i < _old_block_id.size(); ++i)
131  if (elem->subdomain_id() == _old_block_id[i])
132  elem->subdomain_id() = _new_block_id[i];
133  for (unsigned i = 0; i < _old_block_id.size(); ++i)
134  mesh->subdomain_name(_old_block_id[i]) = _new_block_name[i];
135 
136  return dynamic_pointer_cast<MeshBase>(mesh);
137 }
138 
139 SubdomainName
140 RenameBlockGenerator::newBlockName(const subdomain_id_type & new_block_id)
141 {
142  for (unsigned i = 0; i < _new_block_id.size(); ++i)
143  if (_new_block_id[i] == new_block_id)
144  return _old_block_name[i];
145  mooseError("RenameBlock: error in code");
146 }
147 
148 subdomain_id_type
149 RenameBlockGenerator::newBlockID(const SubdomainName & new_block_name)
150 {
151  for (unsigned i = 0; i < _new_block_name.size(); ++i)
152  if (_new_block_name[i] == new_block_name)
153  return _old_block_id[i];
154  mooseError("RenameBlock: error in code");
155 }
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::vector< subdomain_id_type > _new_block_id
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.
MeshGenerator for re-numbering or re-naming blocks.
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
RenameBlockGenerator(const InputParameters &parameters)
std::vector< SubdomainName > _new_block_name
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...
InputParameters validParams< RenameBlockGenerator >()
SubdomainID newBlockID(const SubdomainName &new_block_name)
Given a new_block_name, provide a block ID, based on the old block IDs provided (or deduced from the ...
std::vector< SubdomainName > _old_block_name
std::unique_ptr< MeshBase > & _input
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
SubdomainName newBlockName(const subdomain_id_type &new_block_id)
Given a new_block_id, provide a block name, based on the old block names provided (or deduced from th...
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...
registerMooseObject("MooseApp", RenameBlockGenerator)
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...
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
std::vector< subdomain_id_type > _old_block_id