www.mooseframework.org
ParsedSubdomainMeshGenerator.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 
19 
22 {
25 
26  params.addRequiredParam<MeshGeneratorName>("input", "The mesh we want to modify");
27  params.addRequiredParam<std::string>("combinatorial_geometry",
28  "Function expression encoding a combinatorial geometry");
29  params.addRequiredParam<subdomain_id_type>("block_id",
30  "Subdomain id to set for inside of the combinatorial");
31  params.addParam<SubdomainName>("block_name",
32  "Subdomain name to set for inside of the combinatorial");
33  params.addParam<std::vector<SubdomainName>>(
34  "excluded_subdomains",
35  "A set of subdomain names that will not changed even if "
36  "they are inside/outside the combinatorial geometry");
37  params.addDeprecatedParam<std::vector<subdomain_id_type>>(
38  "excluded_subdomain_ids",
39  "A set of subdomain ids that will not changed even if "
40  "they are inside/outside the combinatorial geometry",
41  "excluded_subdomain_ids is deprecated, use excluded_subdomains (ids or names accepted)");
42  params.addParam<std::vector<std::string>>(
43  "constant_names", {}, "Vector of constants used in the parsed function");
44  params.addParam<std::vector<std::string>>(
45  "constant_expressions",
46  {},
47  "Vector of values for the constants in constant_names (can be an FParser expression)");
48  params.addParam<std::vector<ExtraElementIDName>>(
49  "extra_element_id_names", {}, "Extra element integers used in the parsed expression");
50  params.addClassDescription(
51  "Uses a parsed expression (`combinatorial_geometry`) to determine if an "
52  "element (via its centroid) is inside the region defined by the expression and "
53  "assigns a new block ID.");
54 
55  return params;
56 }
57 
59  : MeshGenerator(parameters),
60  FunctionParserUtils<false>(parameters),
61  _input(getMesh("input")),
62  _function(parameters.get<std::string>("combinatorial_geometry")),
63  _block_id(parameters.get<SubdomainID>("block_id")),
64  _excluded_ids(isParamValid("excluded_subdomain_ids")
65  ? parameters.get<std::vector<subdomain_id_type>>("excluded_subdomain_ids")
66  : std::vector<subdomain_id_type>()),
67  _eeid_names(getParam<std::vector<ExtraElementIDName>>("extra_element_id_names"))
68 {
69  // base function object
70  _func_F = std::make_shared<SymFunction>();
71 
72  // set FParser internal feature flags
74 
75  // add the constant expressions
77  getParam<std::vector<std::string>>("constant_names"),
78  getParam<std::vector<std::string>>("constant_expressions"));
79 
80  // add the extra element integers
81  std::string symbol_str = "x,y,z";
82  for (const auto & eeid_name : _eeid_names)
83  symbol_str += "," + eeid_name;
84 
85  // parse function
86  if (_func_F->Parse(_function, symbol_str) >= 0)
87  mooseError("Invalid function\n",
88  _function,
89  "\nin ParsedSubdomainMeshGenerator ",
90  name(),
91  ".\n",
92  _func_F->ErrorMsg());
93 
94  _func_params.resize(3 + _eeid_names.size());
95 }
96 
97 std::unique_ptr<MeshBase>
99 {
100  std::unique_ptr<MeshBase> mesh = std::move(_input);
101 
102  // the extra element ids would not have existed at construction so we only do this now
103  for (const auto & eeid_name : _eeid_names)
104  _eeid_indices.push_back(mesh->get_elem_integer_index(eeid_name));
105 
106  if (isParamValid("excluded_subdomains"))
107  {
108  auto excluded_subdomains = parameters().get<std::vector<SubdomainName>>("excluded_subdomains");
109 
110  // check that the subdomains exist in the mesh
111  for (const auto & name : excluded_subdomains)
113  paramError("excluded_subdomains", "The block '", name, "' was not found in the mesh");
114 
115  _excluded_ids = MooseMeshUtils::getSubdomainIDs(*mesh, excluded_subdomains);
116  }
117 
118  // Loop over the elements
119  for (const auto & elem : mesh->active_element_ptr_range())
120  {
121  _func_params[0] = elem->vertex_average()(0);
122  _func_params[1] = elem->vertex_average()(1);
123  _func_params[2] = elem->vertex_average()(2);
124  for (const auto i : index_range(_eeid_indices))
125  _func_params[3 + i] = elem->get_extra_integer(_eeid_indices[i]);
126  bool contains = evaluate(_func_F);
127 
128  if (contains && std::find(_excluded_ids.begin(), _excluded_ids.end(), elem->subdomain_id()) ==
129  _excluded_ids.end())
130  elem->subdomain_id() = _block_id;
131  }
132 
133  // Assign block name, if provided
134  if (isParamValid("block_name"))
135  mesh->subdomain_name(_block_id) = getParam<SubdomainName>("block_name");
136 
137  mesh->set_isnt_prepared();
138  return dynamic_pointer_cast<MeshBase>(mesh);
139 }
GenericReal< is_ad > evaluate(SymFunctionPtr &, const std::string &object_name="")
Evaluate FParser object and check EvalError.
void addDeprecatedParam(const std::string &name, const T &value, const std::string &doc_string, const std::string &deprecation_message)
std::unique_ptr< MeshBase > & _input
mesh to add the subdomain to
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
std::vector< unsigned int > _eeid_indices
Indices of the extra element ids used in the parsed expression.
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.
registerMooseObject("MooseApp", ParsedSubdomainMeshGenerator)
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:1147
MeshBase & mesh
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
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.
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.
void addFParserConstants(SymFunctionPtr &parser, const std::vector< std::string > &constant_names, const std::vector< std::string > &constant_expressions)
add constants (which can be complex expressions) to the parser object
virtual const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:56
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.
const std::vector< ExtraElementIDName > _eeid_names
Names of the extra element ids used in the parsed expression.
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...
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
bool hasSubdomainName(MeshBase &input_mesh, const SubdomainName &name)
Whether a particular subdomain name exists in the mesh.
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
const std::string _function
function expression
ParsedSubdomainMeshGenerator(const InputParameters &parameters)
std::vector< GenericReal< is_ad > > _func_params
Array to stage the parameters passed to the functions when calling Eval.
MeshGenerator for defining a Subdomain inside or outside of combinatorial geometry.
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
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 option parameter and a documentation string to the InputParameters object...
const subdomain_id_type _block_id
Block ID to assign to the region.
SymFunctionPtr _func_F
function parser object describing the combinatorial geometry
MeshGenerators are objects that can modify or add to an existing mesh.
Definition: MeshGenerator.h:32
auto index_range(const T &sizable)
void setParserFeatureFlags(SymFunctionPtr &)
apply input paramters to internal feature flags of the parser object