https://mooseframework.inl.gov
Loading...
Searching...
No Matches
UniqueExtraIDMeshGenerator.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
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 "MooseMeshUtils.h"
12#include "libmesh/elem.h"
13
15
18{
20 params.addRequiredParam<MeshGeneratorName>(
21 "input", "Name of an existing mesh generator to which we assign extra element IDs");
22 params.addRequiredParam<std::vector<ExtraElementIDName>>(
23 "id_name",
24 "Existing extra integer ID names that is used to generate a new extra integer ID by finding "
25 "unique combinations of their values");
26 params.addRequiredParam<ExtraElementIDName>("new_id_name", "New extra integer ID name");
27 params.addParam<std::vector<unsigned int>>(
28 "new_id_rule",
29 "Vector of unsigned integers to determine new integer ID values by multiplying the provided "
30 "integers to the corresponding existing ID values and then summing the resulting values");
31 params.addParam<std::vector<SubdomainName>>(
32 "restricted_subdomains", "Only set new extra element id for elements in given subdomains");
33 params.addClassDescription("Add a new extra element integer ID by finding unique combinations of "
34 "the existing extra element integer ID values");
35 return params;
36}
37
39 : MeshGenerator(params),
40 _input(getMesh("input")),
41 _extra_ids(getParam<std::vector<ExtraElementIDName>>("id_name")),
42 _use_new_id_rule(isParamValid("new_id_rule")),
43 _has_restriction(isParamValid("restricted_subdomains"))
44{
45 if (_use_new_id_rule &&
46 getParam<std::vector<unsigned int>>("new_id_rule").size() != _extra_ids.size())
47 paramError("new_id_rule",
48 "This parameter, if provided, must have a length equal to length of id_name.");
49}
50
51std::unique_ptr<MeshBase>
53{
54 std::unique_ptr<MeshBase> mesh = std::move(_input);
55
56 // Make sure subdomain caches are up to date
57 if (!mesh->preparation().has_cached_elem_data)
58 mesh->cache_elem_data();
59
60 std::set<SubdomainID> restricted_ids;
62 {
63 auto names = getParam<std::vector<SubdomainName>>("restricted_subdomains");
64 for (auto & name : names)
65 {
66 // check that the subdomain exists in the mesh
68 paramError("restricted_subdomains", "The block '", name, "' was not found in the mesh");
69
70 restricted_ids.insert(MooseMeshUtils::getSubdomainID(name, *mesh));
71 }
72 }
73
74 auto parsed_ids =
76
77 // override the extra ID values from MooseMeshUtils::getExtraIDUniqueCombinationMap by using
78 // new_id_rule
80 {
81 std::vector<unsigned int> new_id_rule = getParam<std::vector<unsigned int>>("new_id_rule");
82 std::vector<unsigned int> existing_extra_id_index;
83 for (const auto & id_name : _extra_ids)
84 existing_extra_id_index.push_back(mesh->get_elem_integer_index(id_name));
85 for (auto & elem : mesh->active_local_element_ptr_range())
86 {
87 dof_id_type new_id_value = 0;
88 for (unsigned int i = 0; i < _extra_ids.size(); ++i)
89 new_id_value += new_id_rule[i] * elem->get_extra_integer(existing_extra_id_index[i]);
90 parsed_ids[elem->id()] = new_id_value;
91 }
92 }
93 auto new_id_name = getParam<ExtraElementIDName>("new_id_name");
94 unsigned int extra_id_index;
95 if (!mesh->has_elem_integer(new_id_name))
96 extra_id_index = mesh->add_elem_integer(new_id_name);
97 else
98 {
99 extra_id_index = mesh->get_elem_integer_index(new_id_name);
101 "new_id_name", "An element integer with the name '", new_id_name, "' already exists");
102 }
103
104 for (auto & elem : mesh->active_element_ptr_range())
105 {
106 if (_has_restriction && restricted_ids.count(elem->subdomain_id()) == 0)
107 continue;
108 elem->set_extra_integer(extra_id_index, parsed_ids.at(elem->id()));
109 }
110 return mesh;
111}
registerMooseObject("MooseApp", UniqueExtraIDMeshGenerator)
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object.
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...
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.
MeshGenerators are objects that can modify or add to an existing mesh.
static InputParameters validParams()
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition MooseBase.h:457
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition MooseBase.h:406
void paramWarning(const std::string &param, Args... args) const
Add a new extra ID by finding unique combinations of existing extra ID values.
virtual std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
UniqueExtraIDMeshGenerator(const InputParameters &parameters)
const bool _has_restriction
Whether or not we add the new extra ID for elements in certain subdomains.
static InputParameters validParams()
const bool _use_new_id_rule
lag to indicate if new_id_rule is defined
std::unique_ptr< MeshBase > & _input
input mesh for adding element IDs
const std::vector< ExtraElementIDName > _extra_ids
existing extra ID names to be used for adding a new extra ID
MeshBase & mesh
bool hasSubdomainName(const MeshBase &input_mesh, const SubdomainName &name)
Whether a particular subdomain name exists in the mesh.
std::unordered_map< dof_id_type, dof_id_type > getExtraIDUniqueCombinationMap(const MeshBase &mesh, const std::set< SubdomainID > &block_ids, std::vector< ExtraElementIDName > extra_ids)
Create a new set of element-wise IDs by finding unique combinations of existing extra ID values.
SubdomainID getSubdomainID(const SubdomainName &subdomain_name, const MeshBase &mesh)
Gets the subdomain ID associated with the given SubdomainName.