www.mooseframework.org
RenameBlock.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 "RenameBlock.h"
11 #include "MooseMesh.h"
12 
13 #include "libmesh/elem.h"
14 
15 registerMooseObject("MooseApp", RenameBlock);
16 
17 template <>
20 {
22  params.addParam<std::vector<SubdomainID>>(
23  "old_block_id",
24  "Elements with this block number will be given the new_block_number or "
25  "new_block_name. You must supply either old_block_id or old_block_name. "
26  "You may supply a vector of old_block_id, in which case the new_block "
27  "information must also be a vector.");
28  params.addParam<std::vector<SubdomainName>>(
29  "old_block_name",
30  "Elements with this block name will be given the new_block_number or "
31  "new_block_name. You must supply either old_block_id or old_block_name. "
32  "You may supply a vector of old_block_name, in which case the new_block "
33  "information must also be a vector.");
34  params.addParam<std::vector<SubdomainID>>(
35  "new_block_id",
36  "Elements with the old block number (or name) will be given this block "
37  "number. If the old blocks are named, their names will be passed onto the "
38  "newly numbered blocks.");
39  params.addParam<std::vector<SubdomainName>>(
40  "new_block_name",
41  "Elements with the old block number (or name) will be given this block "
42  "name. No change of block ID is performed, unless multiple old blocks are "
43  "given the same name, in which case they are all given the first old block "
44  "number.");
45  params.addClassDescription(
46  "RenameBlock re-numbers or re-names an old_block_id or old_block_name "
47  "with a new_block_id or new_block_name. If using RenameBlock to "
48  "merge blocks (by giving them the same name, for instance) it is "
49  "advisable to specify all your blocks in old_blocks to avoid inconsistencies");
50  return params;
51 }
52 
53 RenameBlock::RenameBlock(const InputParameters & parameters) : MeshModifier(parameters)
54 {
55  // error checking. Must have exactly one of old_block_id or old_block_name
56  if (isParamValid("old_block_id") && isParamValid("old_block_name"))
57  mooseError("RenameBlock: You must supply exactly one of old_block_id or old_block_name\n");
58  else if (!isParamValid("old_block_id") && !isParamValid("old_block_name"))
59  mooseError("RenameBlock: You must supply exactly one of old_block_id or old_block_name\n");
60 
61  // error checking. Must have exactly one of new_block_id or new_block_name
62  // In principal we could have both (the old block would then be given a new ID and a new name)
63  // but i feel that could lead to confusion for the user. If the user wants to do that they
64  // should use two of these RenameBlock MeshModifiers.
65  if (isParamValid("new_block_id") && isParamValid("new_block_name"))
66  mooseError("RenameBlock: You must supply exactly one of new_block_id or new_block_name\n");
67  else if (!isParamValid("new_block_id") && !isParamValid("new_block_name"))
68  mooseError("RenameBlock: You must supply exactly one of new_block_id or new_block_name\n");
69 }
70 
71 void
73 {
74  // Check that we have access to the mesh
75  if (!_mesh_ptr)
76  mooseError("_mesh_ptr must be initialized before calling RenameBlock::modify()");
77 
78  // grab the user input. Can't do all this in the constructor as some things may not
79  // have been put into the mesh yet, eg old_block_name might have been inserted by
80  // another MeshModifier
81  if (isParamValid("old_block_id"))
82  {
83  // user must have supplied old_block_id
84  _old_block_id = getParam<std::vector<SubdomainID>>("old_block_id");
85  _old_block_name.reserve(_old_block_id.size());
86  for (const auto & block_id : _old_block_id)
87  _old_block_name.emplace_back(_mesh_ptr->getMesh().subdomain_name(block_id));
88  }
89  else
90  {
91  // user must have supplied old_block_name
92  _old_block_name = getParam<std::vector<SubdomainName>>("old_block_name");
94  }
95 
96  // construct new_block_id and new_block_name
97  if (isParamValid("new_block_id"))
98  {
99  _new_block_id = getParam<std::vector<SubdomainID>>("new_block_id");
100  if (_new_block_id.size() != _old_block_id.size())
101  mooseError("RenameBlock: The vector of old_block information must have the same length as "
102  "the vector of new_block information\n");
103 
104  // construct the _new_block_name
105  _new_block_name.reserve(_new_block_id.size());
106  for (const auto & block_id : _new_block_id)
107  _new_block_name.emplace_back(newBlockName(block_id));
108  }
109  else // must have supplied new_block_name
110  {
111  _new_block_name = getParam<std::vector<SubdomainName>>("new_block_name");
112  if (_new_block_name.size() != _old_block_id.size())
113  mooseError("RenameBlock: The vector of old_block information must have the same length as "
114  "the vector of new_block information\n");
115 
116  _new_block_id.reserve(_new_block_name.size());
117  for (const auto & block_name : _new_block_name)
118  _new_block_id.emplace_back(newBlockID(block_name));
119  }
120 
121  for (const auto & elem : _mesh_ptr->getMesh().active_element_ptr_range())
122  for (unsigned i = 0; i < _old_block_id.size(); ++i)
123  if (elem->subdomain_id() == _old_block_id[i])
124  elem->subdomain_id() = _new_block_id[i];
125  for (unsigned i = 0; i < _old_block_id.size(); ++i)
126  _mesh_ptr->getMesh().subdomain_name(_old_block_id[i]) = _new_block_name[i];
127 }
128 
129 SubdomainName
131 {
132  for (unsigned i = 0; i < _new_block_id.size(); ++i)
133  if (_new_block_id[i] == new_block_id)
134  return _old_block_name[i];
135  mooseError("RenameBlock: error in code");
136 }
137 
139 RenameBlock::newBlockID(const SubdomainName & new_block_name)
140 {
141  for (unsigned i = 0; i < _new_block_name.size(); ++i)
142  if (_new_block_name[i] == new_block_name)
143  return _old_block_id[i];
144  mooseError("RenameBlock: error in code");
145 }
RenameBlock(const InputParameters &parameters)
Class constructor.
Definition: RenameBlock.C:53
SubdomainName newBlockName(const SubdomainID &new_block_id)
Given a new_block_id, provide a block name, based on the old block names provided (or deduced from th...
Definition: RenameBlock.C:130
MeshModifier for re-numbering or re-naming blocks.
Definition: RenameBlock.h:24
registerMooseObject("MooseApp", RenameBlock)
std::vector< SubdomainID > _old_block_id
Definition: RenameBlock.h:36
MeshModifiers are objects that can modify or add to an existing mesh.
Definition: MeshModifier.h:25
std::vector< SubdomainName > _old_block_name
Definition: RenameBlock.h:38
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
virtual void modify() override
Pure virtual modify function MUST be overridden by children classes.
Definition: RenameBlock.C:72
MooseMesh * _mesh_ptr
Pointer to the mesh.
Definition: MeshModifier.h:68
std::vector< SubdomainName > _new_block_name
Definition: RenameBlock.h:42
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:2567
std::vector< SubdomainID > _new_block_id
Definition: RenameBlock.h:40
subdomain_id_type SubdomainID
InputParameters validParams< RenameBlock >()
Definition: RenameBlock.C:19
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 ...
Definition: RenameBlock.C:139
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< SubdomainID > getSubdomainIDs(const std::vector< SubdomainName > &subdomain_name) const
Get the associated subdomainIDs for the subdomain names that are passed in.
Definition: MooseMesh.C:1090
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:89
InputParameters validParams< MeshModifier >()
Definition: MeshModifier.C:15