www.mooseframework.org
ParsedAddSideset.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
11 #include "ParsedAddSideset.h"
12 #include "Conversion.h"
13 #include "MooseMesh.h"
14 
15 #include "libmesh/fparser_ad.hh"
16 #include "libmesh/elem.h"
17 #include "libmesh/fe_base.h"
18 
20 
21 template <>
24 {
27  params.addRequiredParam<std::string>("combinatorial_geometry",
28  "Function expression encoding a combinatorial geometry");
29  params.addRequiredParam<BoundaryName>("new_sideset_name", "The name of the new sideset");
30  params.addParam<std::vector<SubdomainID>>(
31  "included_subdomain_ids",
32  "A set of subdomain ids whose sides will be included in the new sidesets");
33  params.addParam<Point>(
34  "normal",
35  Point(),
36  "If provided specifies the normal vector on sides that are added to the new ");
37  params.addParam<std::vector<std::string>>(
38  "constant_names", "Vector of constants used in the parsed function (use this for kB etc.)");
39  params.addParam<std::vector<std::string>>(
40  "constant_expressions",
41  "Vector of values for the constants in constant_names (can be an FParser expression)");
42  params.addClassDescription("A MeshModifier that adds element's sides to a sideset if the "
43  "centroid satisfies the combinatorial_geometry expression, (and "
44  "optionally) "
45  "if one of the side's elements is in included_subdomain_ids and if it "
46  "features the correct normal.");
47  return params;
48 }
49 
51  : AddSideSetsBase(parameters),
52  FunctionParserUtils(parameters),
53  _function(parameters.get<std::string>("combinatorial_geometry")),
54  _sideset_name(getParam<BoundaryName>("new_sideset_name")),
55  _check_subdomains(isParamValid("included_subdomain_ids")),
56  _check_normal(parameters.isParamSetByUser("normal")),
57  _included_ids(_check_subdomains
58  ? parameters.get<std::vector<SubdomainID>>("included_subdomain_ids")
59  : std::vector<SubdomainID>()),
60  _normal(getParam<Point>("normal"))
61 {
62  // base function object
64 
65  // set FParser internal feature flags
67 
68  // add the constant expressions
70  getParam<std::vector<std::string>>("constant_names"),
71  getParam<std::vector<std::string>>("constant_expressions"));
72 
73  // parse function
74  if (_func_F->Parse(_function, "x,y,z") >= 0)
75  mooseError("Invalid function\n",
76  _function,
77  "\nin ParsedAddSideset ",
78  name(),
79  ".\n",
80  _func_F->ErrorMsg());
81 
82  _func_params.resize(3);
83 }
84 
85 void
87 {
88  // this mesh modifier works only on replicated meshes
89  _mesh_ptr->errorIfDistributedMesh("ParsedAddSideset");
90 
91  setup();
92 
93  MeshBase & mesh = _mesh_ptr->getMesh();
94 
95  // Get a reference to our BoundaryInfo object for later use
96  BoundaryInfo & boundary_info = mesh.get_boundary_info();
97 
98  // Get the BoundaryIDs from the mesh
99  std::vector<BoundaryID> boundary_ids = _mesh_ptr->getBoundaryIDs({_sideset_name}, true);
100  mooseAssert(boundary_ids.size() == 1, "Length of boundary_ids should be one");
101 
102  for (const auto & elem : mesh.active_element_ptr_range())
103  {
104  SubdomainID curr_subdomain = elem->subdomain_id();
105 
106  // check if the element is included
107  if (_check_subdomains &&
108  std::find(_included_ids.begin(), _included_ids.end(), curr_subdomain) ==
109  _included_ids.end())
110  continue;
111 
112  for (unsigned int side = 0; side < elem->n_sides(); ++side)
113  {
114  _fe_face->reinit(elem, side);
115  const std::vector<Point> & normals = _fe_face->get_normals();
116 
117  // check normal if requested
118  if (_check_normal && std::abs(1.0 - _normal * normals[0]) > _variance)
119  continue;
120 
121  // check expression
122  std::unique_ptr<Elem> curr_side = elem->side_ptr(side);
123  _func_params[0] = curr_side->centroid()(0);
124  _func_params[1] = curr_side->centroid()(1);
125  _func_params[2] = curr_side->centroid()(2);
126  if (evaluate(_func_F))
127  boundary_info.add_side(elem, side, boundary_ids[0]);
128  }
129  }
130  finalize();
131  boundary_info.sideset_name(boundary_ids[0]) = _sideset_name;
132  boundary_info.nodeset_name(boundary_ids[0]) = _sideset_name;
133 }
ADFunctionPtr _func_F
function parser object describing the combinatorial geometry
MetaPhysicL::DualNumber< T, D > abs(const MetaPhysicL::DualNumber< T, D > &in)
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
BoundaryName _sideset_name
name of the new sideset
FunctionParserADBase< Real > ADFunction
Shorthand for an autodiff function parser object.
virtual void modify() override
Pure virtual modify function MUST be overridden by children classes.
std::unique_ptr< FEBase > _fe_face
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
InputParameters validParams< ParsedAddSideset >()
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
void finalize()
This method finalizes the object, setting names back in the boundary_info object and releasing memory...
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
MeshModifier for defining a Sideset by a parsed expression and optionally by looking at the subdomain...
void errorIfDistributedMesh(std::string name) const
Generate a unified error message if the underlying libMesh mesh is a DistributedMesh.
Definition: MooseMesh.C:2685
registerMooseObject("MooseApp", ParsedAddSideset)
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:2567
bool _check_subdomains
whether to check subdomain ids when adding sides or not
subdomain_id_type SubdomainID
ParsedAddSideset(const InputParameters &parameters)
std::vector< SubdomainID > _included_ids
A list of included subdomain ids that the side has to be part of.
InputParameters validParams< AddSideSetsBase >()
bool _check_normal
whether to check normals when adding sides or not
Point _normal
A normal vector that (if provided) is compared against side&#39;s normals.
InputParameters validParams< FunctionParserUtils >()
std::vector< Real > _func_params
Array to stage the parameters passed to the functions when calling Eval.
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
std::vector< BoundaryID > getBoundaryIDs(const Elem *const elem, const unsigned short int side) const
Returns a vector of boundary IDs for the requested element on the requested side. ...
Definition: MooseMesh.C:2153
std::string _function
function expression
void setup()
This method is used to construct the FE object so we can compute normals of faces.