https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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");
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
78std::unique_ptr<MeshBase>
80{
81 std::unique_ptr<MeshBase> mesh = std::move(_input);
82
83 // Make sure subdomain caches are up to date
84 if (!mesh->preparation().has_cached_elem_data)
85 mesh->cache_elem_data();
86
87 // the extra element ids would not have existed at construction so we only do this now
88 for (const auto & eeid_name : _eeid_names)
89 _eeid_indices.push_back(mesh->get_elem_integer_index(eeid_name));
90
91 bool has_restriction = isParamValid("restricted_subdomains");
92 std::set<SubdomainID> restricted_blocks;
93 if (has_restriction)
94 {
95 auto names = getParam<std::vector<SubdomainName>>("restricted_subdomains");
96 for (auto & name : names)
97 {
98 // check that the subdomain exists in the mesh
100 paramError("restricted_subdomains", "The block '", name, "' was not found in the mesh");
101
102 restricted_blocks.insert(MooseMeshUtils::getSubdomainID(name, *mesh));
103 }
104 }
105
106 if (!mesh->has_elem_integer(_elem_id_name))
107 _extra_elem_id = mesh->add_elem_integer(_elem_id_name);
108 else
109 _extra_elem_id = mesh->get_elem_integer_index(_elem_id_name);
110
111 // Loop over the elements
112 for (const auto & elem : mesh->active_element_ptr_range())
113 {
114 if (has_restriction && restricted_blocks.count(elem->subdomain_id()) == 0)
115 continue;
116
117 const auto centroid = elem->true_centroid();
118 _func_params[0] = centroid(0);
119 _func_params[1] = centroid(1);
120 _func_params[2] = centroid(2);
121 for (const auto i : index_range(_eeid_indices))
122 _func_params[3 + i] = elem->get_extra_integer(_eeid_indices[i]);
123 const auto id_real = evaluate(_func_F);
124
125 dof_id_type id = id_real;
126 // this is to ensure a more robust conversion between Real and dof_id_type
127 if (id_real == (Real)DofObject::invalid_id)
128 id = DofObject::invalid_id;
129
130 elem->set_extra_integer(_extra_elem_id, id);
131 }
132
133 return mesh;
134}
registerMooseObject("MooseApp", ParsedExtraElementIDGenerator)
std::vector< GenericReal< is_ad > > _func_params
Array to stage the parameters passed to the functions when calling Eval.
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.
GenericReal< is_ad > evaluate(SymFunctionPtr &, const std::string &object_name="")
Evaluate FParser object and check EvalError.
static InputParameters validParams()
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
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition MooseBase.h:199
MeshGenerator for adding an extra element integer uses a parsed expression.
ParsedExtraElementIDGenerator(const InputParameters &parameters)
unsigned int _extra_elem_id
index of the extra elem id
const std::vector< ExtraElementIDName > _eeid_names
Names of the extra element ids used in the parsed expression.
const std::string & _elem_id_name
extra elem id name
std::vector< unsigned int > _eeid_indices
Indices of the extra element ids used in the parsed expression.
std::unique_ptr< MeshBase > & _input
mesh to add the subdomain to
SymFunctionPtr _func_F
function parser object describing the combinatorial geometry
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
const std::string _function
function expression
const Parallel::Communicator & comm() const
MeshBase & mesh
bool hasSubdomainName(const MeshBase &input_mesh, const SubdomainName &name)
Whether a particular subdomain name exists in the mesh.
SubdomainID getSubdomainID(const SubdomainName &subdomain_name, const MeshBase &mesh)
Gets the subdomain ID associated with the given SubdomainName.
const Real pi