www.mooseframework.org
ParsedSubdomainMeshModifier.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 
10 // MOOSE includes
12 #include "Conversion.h"
13 #include "MooseMesh.h"
14 
15 #include "libmesh/fparser_ad.hh"
16 #include "libmesh/elem.h"
17 
19 
20 template <>
23 {
26  params.addRequiredParam<std::string>("combinatorial_geometry",
27  "Function expression encoding a combinatorial geometry");
28  params.addRequiredParam<SubdomainID>("block_id",
29  "Subdomain id to set for inside of the combinatorial");
30  params.addParam<SubdomainName>("block_name",
31  "Subdomain name to set for inside of the combinatorial");
32  params.addParam<std::vector<SubdomainID>>("excluded_subdomain_ids",
33  "A set of subdomain ids that will not changed even if "
34  "they are inside/outside the combinatorial geometry");
35  params.addParam<std::vector<std::string>>(
36  "constant_names", "Vector of constants used in the parsed function (use this for kB etc.)");
37  params.addParam<std::vector<std::string>>(
38  "constant_expressions",
39  "Vector of values for the constants in constant_names (can be an FParser expression)");
40  params.addClassDescription("MeshModifier that uses a parsed expression (combinatorial_geometry) "
41  "to determine if an element (aka its centroid) is inside the "
42  "combinatorial geometry and "
43  "assigns a new block id.");
44  return params;
45 }
46 
48  : MeshModifier(parameters),
49  FunctionParserUtils(parameters),
50  _function(parameters.get<std::string>("combinatorial_geometry")),
51  _block_id(parameters.get<SubdomainID>("block_id")),
52  _excluded_ids(parameters.get<std::vector<SubdomainID>>("excluded_subdomain_ids"))
53 {
54  // base function object
56 
57  // set FParser internal feature flags
59 
60  // add the constant expressions
62  getParam<std::vector<std::string>>("constant_names"),
63  getParam<std::vector<std::string>>("constant_expressions"));
64 
65  // parse function
66  if (_func_F->Parse(_function, "x,y,z") >= 0)
67  mooseError("Invalid function\n",
68  _function,
69  "\nin ParsedSubdomainMeshModifier ",
70  name(),
71  ".\n",
72  _func_F->ErrorMsg());
73 
74  _func_params.resize(3);
75 }
76 
77 void
79 {
80  // Check that we have access to the mesh
81  if (!_mesh_ptr)
82  mooseError(
83  "_mesh_ptr must be initialized before calling ParsedSubdomainMeshModifier::modify()");
84 
85  // Loop over the elements
86  for (const auto & elem : _mesh_ptr->getMesh().active_element_ptr_range())
87  {
88  _func_params[0] = elem->centroid()(0);
89  _func_params[1] = elem->centroid()(1);
90  _func_params[2] = elem->centroid()(2);
91  bool contains = evaluate(_func_F);
92 
93  if (contains && std::find(_excluded_ids.begin(), _excluded_ids.end(), elem->subdomain_id()) ==
94  _excluded_ids.end())
95  elem->subdomain_id() = _block_id;
96  }
97 
98  // Assign block name, if provided
99  if (isParamValid("block_name"))
100  _mesh_ptr->getMesh().subdomain_name(_block_id) = getParam<SubdomainName>("block_name");
101 }
registerMooseObject("MooseApp", ParsedSubdomainMeshModifier)
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
FunctionParserADBase< Real > ADFunction
Shorthand for an autodiff function parser object.
const SubdomainID _block_id
Block ID to assign to the region.
virtual void modify() override
Pure virtual modify function MUST be overridden by children classes.
MeshModifiers are objects that can modify or add to an existing mesh.
Definition: MeshModifier.h:25
ADFunctionPtr _func_F
function parser object describing the combinatorial geometry
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.
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...
MooseMesh * _mesh_ptr
Pointer to the mesh.
Definition: MeshModifier.h:68
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:2567
subdomain_id_type SubdomainID
const std::vector< SubdomainID > _excluded_ids
A list of excluded subdomain ids that will not be changed even if they are in the combinatorial geome...
InputParameters validParams< FunctionParserUtils >()
std::vector< Real > _func_params
Array to stage the parameters passed to the functions when calling Eval.
MeshModifier for defining a Subdomain inside or outside of combinatorial geometry.
ParsedSubdomainMeshModifier(const InputParameters &parameters)
Class constructor.
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
InputParameters validParams< ParsedSubdomainMeshModifier >()
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:89
const std::string _function
function expression
InputParameters validParams< MeshModifier >()
Definition: MeshModifier.C:15