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 
20 template <>
23 {
26 
27  params.addRequiredParam<MeshGeneratorName>("input", "The mesh we want to modify");
28  params.addRequiredParam<std::string>("combinatorial_geometry",
29  "Function expression encoding a combinatorial geometry");
30  params.addRequiredParam<subdomain_id_type>("block_id",
31  "Subdomain id to set for inside of the combinatorial");
32  params.addParam<SubdomainName>("block_name",
33  "Subdomain name to set for inside of the combinatorial");
34  params.addParam<std::vector<subdomain_id_type>>(
35  "excluded_subdomain_ids",
36  "A set of subdomain ids that will not changed even if "
37  "they are inside/outside the combinatorial geometry");
38  params.addParam<std::vector<std::string>>(
39  "constant_names", "Vector of constants used in the parsed function (use this for kB etc.)");
40  params.addParam<std::vector<std::string>>(
41  "constant_expressions",
42  "Vector of values for the constants in constant_names (can be an FParser expression)");
43  params.addClassDescription("MeshModifier that uses a parsed expression (combinatorial_geometry) "
44  "to determine if an element (aka its centroid) is inside the "
45  "combinatorial geometry and "
46  "assigns a new block id.");
47 
48  return params;
49 }
50 
52  : MeshGenerator(parameters),
53  FunctionParserUtils(parameters),
54  _input(getMesh("input")),
55  _function(parameters.get<std::string>("combinatorial_geometry")),
56  _block_id(parameters.get<SubdomainID>("block_id")),
57  _excluded_ids(parameters.get<std::vector<SubdomainID>>("excluded_subdomain_ids"))
58 {
59  // base function object
61 
62  // set FParser internal feature flags
64 
65  // add the constant expressions
67  getParam<std::vector<std::string>>("constant_names"),
68  getParam<std::vector<std::string>>("constant_expressions"));
69 
70  // parse function
71  if (_func_F->Parse(_function, "x,y,z") >= 0)
72  mooseError("Invalid function\n",
73  _function,
74  "\nin ParsedSubdomainMeshModifier ",
75  name(),
76  ".\n",
77  _func_F->ErrorMsg());
78 
79  _func_params.resize(3);
80 }
81 
82 std::unique_ptr<MeshBase>
84 {
85  std::unique_ptr<MeshBase> mesh = std::move(_input);
86 
87  // Loop over the elements
88  for (const auto & elem : mesh->active_element_ptr_range())
89  {
90  _func_params[0] = elem->centroid()(0);
91  _func_params[1] = elem->centroid()(1);
92  _func_params[2] = elem->centroid()(2);
93  bool contains = evaluate(_func_F);
94 
95  if (contains && std::find(_excluded_ids.begin(), _excluded_ids.end(), elem->subdomain_id()) ==
96  _excluded_ids.end())
97  elem->subdomain_id() = _block_id;
98  }
99 
100  // Assign block name, if provided
101  if (isParamValid("block_name"))
102  mesh->subdomain_name(_block_id) = getParam<SubdomainName>("block_name");
103 
104  return dynamic_pointer_cast<MeshBase>(mesh);
105 }
Real evaluate(ADFunctionPtr &)
Evaluate FParser object and check EvalError.
void addFParserConstants(ADFunctionPtr &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
std::unique_ptr< MeshBase > & _input
FunctionParserADBase< Real > ADFunction
Shorthand for an autodiff function parser object.
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
registerMooseObject("MooseApp", ParsedSubdomainMeshGenerator)
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::shared_ptr< ADFunction > ADFunctionPtr
Shorthand for an smart pointer to an autodiff function parser object.
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 mooseError(Args &&... args) const
Definition: MooseObject.h:147
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseObject.h:191
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...
InputParameters validParams< ParsedSubdomainMeshGenerator >()
subdomain_id_type SubdomainID
const 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 std::string _function
function expression
ParsedSubdomainMeshGenerator(const InputParameters &parameters)
InputParameters validParams< FunctionParserUtils >()
std::vector< Real > _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.
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:59
void setParserFeatureFlags(ADFunctionPtr &)
apply input paramters to internal feature flags of the parser object
ADFunctionPtr _func_F
function parser object describing the combinatorial geometry
const subdomain_id_type _block_id
Block ID to assign to the region.
InputParameters validParams< MeshGenerator >()
Definition: MeshGenerator.C:16
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:89
MeshGenerators are objects that can modify or add to an existing mesh.
Definition: MeshGenerator.h:30