15 #include "libmesh/mesh_modification.h" 24 params.
addRequiredParam<MeshGeneratorName>(
"input",
"The mesh we want to modify");
28 "Elements with this block number will be given the new_block_number or " 29 "new_block_name. You must supply either old_block_id or old_block_name. " 30 "You may supply a vector of old_block_id, in which case the new_block " 31 "information must also be a vector.",
32 "Use 'old_block' instead of 'old_block_id'.");
35 "Elements with this block name will be given the new_block_number or " 36 "new_block_name. You must supply either old_block_id or old_block_name. " 37 "You may supply a vector of old_block_name, in which case the new_block " 38 "information must also be a vector.",
39 "Use 'old_block' instead of 'old_block_name'.");
42 "Elements with the old block number (or name) will be given this block " 43 "number. If the old blocks are named, their names will be passed onto the " 44 "newly numbered blocks.",
45 "Use 'new_block' instead of 'new_block_id'.");
48 "Elements with the old block number (or name) will be given this block " 49 "name. No change of block ID is performed, unless multiple old blocks are " 50 "given the same name, in which case they are all given the first old block " 52 "Use 'new_block' instead of 'new_block_name'.");
54 params.
addParam<std::vector<SubdomainName>>(
56 "Elements with these block ID(s)/name(s) will be given the new block information specified " 58 params.
addParam<std::vector<SubdomainName>>(
60 "The new block ID(s)/name(s) to be given by the elements defined in 'old_block'.");
62 params.
addClassDescription(
"Changes the block IDs and/or block names for a given set of blocks " 63 "defined by either block ID or block name. The changes are " 64 "independent of ordering. The merging of blocks is supported.");
74 "Cannot use in combination with 'old_block_name'. Please use 'old_block' " 75 "instead; 'old_block_id' and 'old_block_name' are deprecated.");
78 "Cannot use in combination with 'new_block_name'. Please use 'new_block' " 79 "instead; 'new_block_id' and 'new_block_name' are deprecated.");
85 "Cannot use with 'old_block'. Use only 'old_block'; 'old_block_id' is " 89 "Cannot use with 'old_block'. Use only 'old_block'; 'old_block_name' is " 91 _old_block = getParam<std::vector<SubdomainName>>(
"old_block");
96 for (
const auto id :
getParam<std::vector<SubdomainID>>(
"old_block_id"))
103 ? getParam<std::vector<SubdomainName>>(
"old_block_name")
104 : std::vector<SubdomainName>{};
108 std::string new_block_param_name;
113 "Cannot use with 'new_block'. Use only 'new_block'; 'new_block_id' is " 117 "Cannot use with 'new_block'. Use only 'new_block'; 'new_block_name' is " 119 _new_block = getParam<std::vector<SubdomainName>>(
"new_block");
120 new_block_param_name =
"new_block";
124 for (
const auto id :
getParam<std::vector<SubdomainID>>(
"new_block_id"))
126 new_block_param_name =
"new_block_id";
131 ? getParam<std::vector<SubdomainName>>(
"new_block_name")
132 : std::vector<SubdomainName>{};
133 new_block_param_name =
"new_block_name";
140 std::unique_ptr<MeshBase>
143 std::unique_ptr<MeshBase>
mesh = std::move(
_input);
146 const MeshBase & const_mesh = *
mesh;
149 std::set<subdomain_id_type> block_ids;
150 mesh->subdomain_ids(block_ids);
154 auto get_unused_block_id = [
this, &block_ids, &const_mesh]()
157 if (!block_ids.count(
id) && !const_mesh.get_subdomain_name_map().count(
id))
159 block_ids.insert(
id);
170 const auto is_subdomain_id = [](
const SubdomainName & subdomain_name)
175 std::vector<SubdomainName> old_block_names(num_blocks);
176 std::stringstream missing_block;
183 old_block_ids[i] = id;
186 if (!block_ids.count(
id))
187 missing_block <<
name <<
" ";
191 if (is_subdomain_id(
name))
192 old_block_names[i] = const_mesh.subdomain_name(
id);
195 old_block_names[i] =
name;
197 if (missing_block.str().size())
199 "The following blocks were requested to be renamed, but do not exist: ",
200 missing_block.str());
204 std::map<SubdomainID, std::string> new_names;
210 if (is_subdomain_id(
name))
213 new_block_ids[i] = id;
217 block_ids.insert(
id);
220 if (old_block_names[i].size())
221 new_names[id] = old_block_names[i];
228 bool name_already_exists =
false;
232 for (
const auto map : {&const_mesh.get_subdomain_name_map(),
233 const_cast<const std::map<SubdomainID, std::string> *
>(&new_names)})
234 for (
const auto & id_name_pair : *map)
235 if (!name_already_exists && id_name_pair.second ==
name)
237 new_block_ids[i] = id_name_pair.first;
238 new_names[id_name_pair.first] =
name;
239 name_already_exists =
true;
243 if (!name_already_exists)
245 new_block_ids[i] = old_block_ids[i];
246 new_names[new_block_ids[i]] =
name;
262 auto temp_new_block_ids = new_block_ids;
263 std::vector<std::pair<SubdomainID, SubdomainID>> temp_change_ids;
265 for (
const auto new_i :
make_range(num_blocks))
270 for (
const auto old_i :
make_range(new_i + 1, num_blocks))
271 if (new_block_ids[new_i] == old_block_ids[old_i])
273 const auto temp_id = get_unused_block_id();
274 temp_change_ids.emplace_back(temp_id, new_block_ids[new_i]);
275 temp_new_block_ids[new_i] = temp_id;
282 MeshTools::Modification::change_subdomain_id(*
mesh, old_block_ids[i], temp_new_block_ids[i]);
284 for (
const auto & pair : temp_change_ids)
285 MeshTools::Modification::change_subdomain_id(*
mesh, pair.first, pair.second);
289 if (
mesh->get_subdomain_name_map().count(old_block_ids[i]))
290 mesh->set_subdomain_name_map().erase(old_block_ids[i]);
292 for (
const auto & pair : new_names)
293 mesh->subdomain_name(pair.first) = pair.second;
295 mesh->set_isnt_prepared();
std::vector< SubdomainName > _old_block
The old blocks.
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.
RenameBlockGenerator(const InputParameters ¶meters)
virtual const std::string & name() const
Get the name of the class.
SubdomainID getSubdomainID(const SubdomainName &subdomain_name, const MeshBase &mesh)
Gets the subdomain ID associated with the given SubdomainName.
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
const SubdomainID INVALID_BLOCK_ID
std::unique_ptr< MeshBase > & _input
static InputParameters validParams()
std::string _old_block_param_name
The name of the parameter that specifies the old blocks.
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
void paramError(const std::string ¶m, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
static InputParameters validParams()
std::vector< SubdomainName > _new_block
The new blocks.
IntRange< T > make_range(T beg, T end)
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
registerMooseObject("MooseApp", RenameBlockGenerator)
bool isDigits(const std::string &str)
Courtesy https://stackoverflow.com/a/8889045 and https://en.cppreference.com/w/cpp/string/byte/isdigi...
MeshGenerators are objects that can modify or add to an existing mesh.