www.mooseframework.org
RenameBlock.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "RenameBlock.h"
16 #include "MooseMesh.h"
17 
18 template <>
21 {
23  params.addParam<std::vector<SubdomainID>>(
24  "old_block_id",
25  "Elements with this block number will be given the new_block_number or "
26  "new_block_name. You must supply either old_block_id or old_block_name. "
27  "You may supply a vector of old_block_id, in which case the new_block "
28  "information must also be a vector.");
29  params.addParam<std::vector<SubdomainName>>(
30  "old_block_name",
31  "Elements with this block name will be given the new_block_number or "
32  "new_block_name. You must supply either old_block_id or old_block_name. "
33  "You may supply a vector of old_block_name, in which case the new_block "
34  "information must also be a vector.");
35  params.addParam<std::vector<SubdomainID>>(
36  "new_block_id",
37  "Elements with the old block number (or name) will be given this block "
38  "number. You must supply either new_block_id or new_block_name. You may "
39  "supply a vector of new_block_id, in which case the old_block information "
40  "must also be a vector.");
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. You must supply either new_block_id or new_block_name. You may "
45  "supply a vector of new_block_id, in which case the old_block information "
46  "must also be a vector.");
47  params.addClassDescription("RenameBlock re-numbers or re-names an old_block_id or old_block_name "
48  "with a new_block_id or new_block_name");
49  return params;
50 }
51 
52 RenameBlock::RenameBlock(const InputParameters & parameters) : MeshModifier(parameters)
53 {
54  // error checking. Must have exactly one of old_block_id or old_block_name
55  if (isParamValid("old_block_id") && isParamValid("old_block_name"))
56  mooseError("RenameBlock: You must supply exactly one of old_block_id or old_block_name\n");
57  else if (!isParamValid("old_block_id") && !isParamValid("old_block_name"))
58  mooseError("RenameBlock: You must supply exactly one of old_block_id or old_block_name\n");
59 
60  // error checking. Must have exactly one of new_block_id or new_block_name
61  // In principal we could have both (the old block would then be given a new ID and a new name)
62  // but i feel that could lead to confusion for the user. If the user wants to do that they
63  // should use two of these RenameBlock MeshModifiers.
64  if (isParamValid("new_block_id") && isParamValid("new_block_name"))
65  mooseError("RenameBlock: You must supply exactly one of new_block_id or new_block_name\n");
66  else if (!isParamValid("new_block_id") && !isParamValid("new_block_name"))
67  mooseError("RenameBlock: You must supply exactly one of new_block_id or new_block_name\n");
68 }
69 
70 void
72 {
73  // Check that we have access to the mesh
74  if (!_mesh_ptr)
75  mooseError("_mesh_ptr must be initialized before calling RenameBlock::modify()");
76 
77  // grab the user input. Can't do all this in the constructor as some things may not
78  // have been put into the mesh yet, eg old_block_name might have been inserted by
79  // another MeshModifier
80  if (isParamValid("old_block_id"))
81  _old_block_id = getParam<std::vector<SubdomainID>>("old_block_id");
82  else // must have supplied old_block_name
84  _mesh_ptr->getSubdomainIDs(getParam<std::vector<SubdomainName>>("old_block_name"));
85 
86  if (isParamValid("new_block_id"))
87  {
88  _new_block_id = getParam<std::vector<SubdomainID>>("new_block_id");
89  if (_new_block_id.size() != _old_block_id.size())
90  mooseError("RenameBlock: The vector of old_block information must have the same length as "
91  "the vector of new_block information\n");
92  for (const auto & elem : _mesh_ptr->getMesh().active_element_ptr_range())
93  for (unsigned i = 0; i < _old_block_id.size(); ++i)
94  if (elem->subdomain_id() == _old_block_id[i])
95  elem->subdomain_id() = _new_block_id[i];
96  }
97  else // must have supplied new_block_name
98  {
99  _new_block_name = getParam<std::vector<SubdomainName>>("new_block_name");
100  if (_new_block_name.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  for (unsigned i = 0; i < _old_block_id.size(); ++i)
104  // libmesh appears to check that _old_block_id[i] isn't too big or too small
105  _mesh_ptr->getMesh().subdomain_name(_old_block_id[i]) = _new_block_name[i];
106  }
107 }
RenameBlock(const InputParameters &parameters)
Class constructor.
Definition: RenameBlock.C:52
std::vector< SubdomainID > _old_block_id
Definition: RenameBlock.h:42
MeshModifiers are objects that can modify or add to an existing mesh.
Definition: MeshModifier.h:31
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
virtual void modify() override
Pure virtual modify function MUST be overridden by children classes.
Definition: RenameBlock.C:71
MooseMesh * _mesh_ptr
Pointer to the mesh.
Definition: MeshModifier.h:74
std::vector< SubdomainName > _new_block_name
Definition: RenameBlock.h:46
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:2355
std::vector< SubdomainID > _new_block_id
Definition: RenameBlock.h:44
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:67
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseObject.h:122
InputParameters validParams< RenameBlock >()
Definition: RenameBlock.C:20
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...
void mooseError(Args &&...args) const
Definition: MooseObject.h:80
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:1012
InputParameters validParams< MeshModifier >()
Definition: MeshModifier.C:20