https://mooseframework.inl.gov
ParsedSubdomainGeneratorBase.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 "Conversion.h"
12 #include "MooseMeshUtils.h"
13 #include "CastUniquePointer.h"
14 
15 #include "libmesh/fparser_ad.hh"
16 #include "libmesh/elem.h"
17 
20 {
23 
24  params.addRequiredParam<MeshGeneratorName>("input", "The mesh we want to modify");
25  params.addRequiredParam<std::string>(
26  "expression", "Parsed expression to determine the subdomain id of each involved element");
27  params.addParam<std::vector<SubdomainName>>(
28  "excluded_subdomains",
29  "A set of subdomain names that will not changed even if "
30  "they are inside/outside the combinatorial geometry");
31  params.addDeprecatedParam<std::vector<subdomain_id_type>>(
32  "excluded_subdomain_ids",
33  "A set of subdomain ids that will not changed even if "
34  "they are inside/outside the combinatorial geometry",
35  "excluded_subdomain_ids is deprecated, use excluded_subdomains (ids or names accepted)");
36  params.addParam<std::vector<std::string>>(
37  "constant_names", {}, "Vector of constants used in the parsed function");
38  params.addParam<std::vector<std::string>>(
39  "constant_expressions",
40  {},
41  "Vector of values for the constants in constant_names (can be an FParser expression)");
42  params.addParam<std::vector<ExtraElementIDName>>(
43  "extra_element_id_names", {}, "Extra element integers used in the parsed expression");
44  params.addClassDescription("A base class for mesh generators that Use a parsed expression to "
45  "assign new subdomain id(s).");
46 
47  return params;
48 }
49 
51  : MeshGenerator(parameters),
52  FunctionParserUtils<false>(parameters),
53  _input(getMesh("input")),
54  _function(parameters.get<std::string>("expression")),
55  _excluded_ids(isParamValid("excluded_subdomain_ids")
56  ? parameters.get<std::vector<subdomain_id_type>>("excluded_subdomain_ids")
57  : std::vector<subdomain_id_type>()),
58  _eeid_names(getParam<std::vector<ExtraElementIDName>>("extra_element_id_names"))
59 {
61 }
62 
63 std::unique_ptr<MeshBase>
65 {
66  std::unique_ptr<MeshBase> mesh = std::move(_input);
67 
68  // the extra element ids would not have existed at construction so we only do this now
69  for (const auto & eeid_name : _eeid_names)
70  _eeid_indices.push_back(mesh->get_elem_integer_index(eeid_name));
71 
72  if (isParamValid("excluded_subdomains"))
73  {
74  auto excluded_subdomains = parameters().get<std::vector<SubdomainName>>("excluded_subdomains");
75 
76  // check that the subdomains exist in the mesh
77  for (const auto & name : excluded_subdomains)
79  paramError("excluded_subdomains", "The block '", name, "' was not found in the mesh");
80 
81  _excluded_ids = MooseMeshUtils::getSubdomainIDs(*mesh, excluded_subdomains);
82  }
83 
84  // Loop over the elements
85  for (const auto & elem : mesh->active_element_ptr_range())
87 
88  // Assign block name, if applicable
90 
91  mesh->set_isnt_prepared();
92  return dynamic_pointer_cast<MeshBase>(mesh);
93 }
94 
95 void
96 ParsedSubdomainGeneratorBase::functionInitialize(const std::string & function_expression)
97 {
98  // add the extra element integers
99  std::string symbol_str = "x,y,z";
100  for (const auto & eeid_name : _eeid_names)
101  symbol_str += "," + eeid_name;
102 
103  // Create parsed function
104  _func_F = std::make_shared<SymFunction>();
106  function_expression,
107  symbol_str,
108  getParam<std::vector<std::string>>("constant_names"),
109  getParam<std::vector<std::string>>("constant_expressions"),
110  comm());
111 
112  _func_params.resize(3 + _eeid_names.size());
113 }
std::vector< unsigned int > _eeid_indices
Indices of the extra element ids used in the parsed expression.
const std::vector< ExtraElementIDName > _eeid_names
Names of the extra element ids used in the parsed expression.
void addDeprecatedParam(const std::string &name, const T &value, const std::string &doc_string, const std::string &deprecation_message)
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
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
const std::string _function
function expression
MeshBase & mesh
std::vector< subdomain_id_type > _excluded_ids
A list of excluded subdomain ids that will not be changed even if they are in the combinatorial geome...
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const Parallel::Communicator & comm() const
std::unique_ptr< T_DEST, T_DELETER > dynamic_pointer_cast(std::unique_ptr< T_SRC, T_DELETER > &src)
These are reworked from https://stackoverflow.com/a/11003103.
void functionInitialize(const std::string &function_expression)
Initialize the function parser object.
std::vector< subdomain_id_type > getSubdomainIDs(const libMesh::MeshBase &mesh, const std::vector< SubdomainName > &subdomain_name)
Get the associated subdomainIDs for the subdomain names that are passed in.
virtual const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:57
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...
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
ParsedSubdomainGeneratorBase(const InputParameters &parameters)
virtual void setBlockName(std::unique_ptr< MeshBase > &)
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
virtual void assignElemSubdomainID(Elem *elem)=0
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.
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 ...
static InputParameters validParams()
Definition: MeshGenerator.C:23
std::unique_ptr< MeshBase > & _input
mesh to set the subdomains on
SymFunctionPtr _func_F
function parser object describing the combinatorial geometry
bool hasSubdomainName(const MeshBase &input_mesh, const SubdomainName &name)
Whether a particular subdomain name exists in the mesh.
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...
const InputParameters & parameters() const
Get the parameters of the object.
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...
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
MeshGenerators are objects that can modify or add to an existing mesh.
Definition: MeshGenerator.h:32