https://mooseframework.inl.gov
ParsedExtraElementIDGenerator.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 
12 #include "Conversion.h"
13 #include "MooseMeshUtils.h"
14 
15 #include "libmesh/fparser_ad.hh"
16 #include "libmesh/elem.h"
17 
19 
22 {
25 
26  params.addRequiredParam<MeshGeneratorName>("input", "The mesh we want to modify");
27  params.addRequiredParam<std::string>(
28  "expression", "Function expression to return the extra element ID based on element centroid");
29  params.addRequiredParam<std::string>(
30  "extra_elem_integer_name", "Name of the extra element integer to be added by this generator");
31  params.addParam<std::vector<SubdomainName>>("restricted_subdomains",
32  "Only set ids for elements within these restricted "
33  "subdomains (empty means the entire domain)");
34  params.addParam<std::vector<std::string>>(
35  "constant_names", {}, "Vector of constants used in the parsed function");
36  params.addParam<std::vector<std::string>>(
37  "constant_expressions",
38  {},
39  "Vector of values for the constants in constant_names (can be an FParser expression)");
40  params.addParam<std::vector<ExtraElementIDName>>(
41  "extra_element_id_names", {}, "Extra element integers used in the parsed expression");
42  params.addClassDescription(
43  "Uses a parsed expression to set an extra element id for elements (via their centroids).");
44 
45  return params;
46 }
47 
49  : MeshGenerator(parameters),
50  FunctionParserUtils<false>(parameters),
51  _input(getMesh("input")),
52  _function(parameters.get<std::string>("expression")),
53  _eeid_names(getParam<std::vector<ExtraElementIDName>>("extra_element_id_names")),
54  _elem_id_name(getParam<std::string>("extra_elem_integer_name"))
55 {
56  // Form the vectors of constants names (symbols in the expression) and expression (values)
57  auto c_names = getParam<std::vector<std::string>>("constant_names");
58  auto c_defs = getParam<std::vector<std::string>>("constant_expressions");
59  c_names.push_back("invalid_elem_id");
60  c_defs.push_back(std::to_string(DofObject::invalid_id));
61  c_names.push_back("pi");
62  c_defs.push_back(std::to_string(libMesh::pi));
63  c_names.push_back("e");
64  c_defs.push_back(std::to_string(std::exp(Real(1))));
65 
66  // add the extra element integers
67  std::string symbol_str = "x,y,z";
68  for (const auto & eeid_name : _eeid_names)
69  symbol_str += "," + eeid_name;
70 
71  // base function object
72  _func_F = std::make_shared<SymFunction>();
73  parsedFunctionSetup(_func_F, _function, symbol_str, c_names, c_defs, comm());
74 
75  _func_params.resize(3 + _eeid_names.size());
76 }
77 
78 std::unique_ptr<MeshBase>
80 {
81  std::unique_ptr<MeshBase> mesh = std::move(_input);
82 
83  // the extra element ids would not have existed at construction so we only do this now
84  for (const auto & eeid_name : _eeid_names)
85  _eeid_indices.push_back(mesh->get_elem_integer_index(eeid_name));
86 
87  bool has_restriction = isParamValid("restricted_subdomains");
88  std::set<SubdomainID> restricted_blocks;
89  if (has_restriction)
90  {
91  auto names = getParam<std::vector<SubdomainName>>("restricted_subdomains");
92  for (auto & name : names)
93  {
94  // check that the subdomain exists in the mesh
96  paramError("restricted_subdomains", "The block '", name, "' was not found in the mesh");
97 
98  restricted_blocks.insert(MooseMeshUtils::getSubdomainID(name, *mesh));
99  }
100  }
101 
102  if (!mesh->has_elem_integer(_elem_id_name))
103  _extra_elem_id = mesh->add_elem_integer(_elem_id_name);
104  else
105  _extra_elem_id = mesh->get_elem_integer_index(_elem_id_name);
106 
107  // Loop over the elements
108  for (const auto & elem : mesh->active_element_ptr_range())
109  {
110  if (has_restriction && restricted_blocks.count(elem->subdomain_id()) == 0)
111  continue;
112 
113  const auto centroid = elem->true_centroid();
114  _func_params[0] = centroid(0);
115  _func_params[1] = centroid(1);
116  _func_params[2] = centroid(2);
117  for (const auto i : index_range(_eeid_indices))
118  _func_params[3 + i] = elem->get_extra_integer(_eeid_indices[i]);
119  const auto id_real = evaluate(_func_F);
120 
121  dof_id_type id = id_real;
122  // this is to ensure a more robust conversion between Real and dof_id_type
123  if (id_real == (Real)DofObject::invalid_id)
124  id = DofObject::invalid_id;
125 
126  elem->set_extra_integer(_extra_elem_id, id);
127  }
128 
129  return mesh;
130 }
GenericReal< is_ad > evaluate(SymFunctionPtr &, const std::string &object_name="")
Evaluate FParser object and check EvalError.
unsigned int _extra_elem_id
index of the extra elem id
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:435
auto exp(const T &)
T * get(const std::unique_ptr< T > &u)
The MooseUtils::get() specializations are used to support making forwards-compatible code changes fro...
Definition: MooseUtils.h:1155
MeshBase & mesh
ParsedExtraElementIDGenerator(const InputParameters &parameters)
SymFunctionPtr _func_F
function parser object describing the combinatorial geometry
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const Parallel::Communicator & comm() const
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
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...
SubdomainID getSubdomainID(const SubdomainName &subdomain_name, const MeshBase &mesh)
Gets the subdomain ID associated with the given SubdomainName.
const std::string _function
function expression
std::vector< unsigned int > _eeid_indices
Indices of the extra element ids used in the parsed expression.
const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:99
std::unique_ptr< MeshBase > & _input
mesh to add the subdomain to
registerMooseObject("MooseApp", ParsedExtraElementIDGenerator)
const std::string & _elem_id_name
extra elem id name
void parsedFunctionSetup(SymFunctionPtr &function, const std::string &expression, const std::string &variables, const std::vector< std::string > &constant_names, const std::vector< std::string > &constant_expressions, const libMesh::Parallel::Communicator &comm) const
Performs setup steps on a SymFunction.
static InputParameters validParams()
Definition: MeshGenerator.C:23
bool hasSubdomainName(const MeshBase &input_mesh, const SubdomainName &name)
Whether a particular subdomain name exists in the mesh.
MeshGenerator for adding an extra element integer uses a parsed expression.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const std::vector< ExtraElementIDName > _eeid_names
Names of the extra element ids used in the parsed expression.
std::vector< GenericReal< is_ad > > _func_params
Array to stage the parameters passed to the functions when calling Eval.
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...
static InputParameters validParams()
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...
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseBase.h:195
MeshGenerators are objects that can modify or add to an existing mesh.
Definition: MeshGenerator.h:32
auto index_range(const T &sizable)
uint8_t dof_id_type
const Real pi