www.mooseframework.org
ParsedGenerateSideset.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 #include "ParsedGenerateSideset.h"
11 #include "Conversion.h"
12 #include "MooseMeshUtils.h"
13 #include "CastUniquePointer.h"
14 
15 #include "libmesh/fparser_ad.hh"
16 #include "libmesh/distributed_mesh.h"
17 #include "libmesh/elem.h"
18 #include "libmesh/fe_base.h"
19 
20 #include <typeinfo>
21 
23 
24 template <>
27 {
30 
31  params.addRequiredParam<MeshGeneratorName>("input", "The mesh we want to modify");
32  params.addRequiredParam<std::string>("combinatorial_geometry",
33  "Function expression encoding a combinatorial geometry");
34  params.addRequiredParam<BoundaryName>("new_sideset_name", "The name of the new sideset");
35  params.addParam<std::vector<subdomain_id_type>>(
36  "included_subdomain_ids",
37  "A set of subdomain ids whose sides will be included in the new sidesets");
38  params.addParam<Point>(
39  "normal",
40  Point(),
41  "If provided specifies the normal vector on sides that are added to the new ");
42  params.addParam<std::vector<std::string>>(
43  "constant_names", "Vector of constants used in the parsed function (use this for kB etc.)");
44  params.addParam<std::vector<std::string>>(
45  "constant_expressions",
46  "Vector of values for the constants in constant_names (can be an FParser expression)");
47  params.addClassDescription("A MeshModifier that adds element's sides to a sideset if the "
48  "centroid satisfies the combinatorial_geometry expression, (and "
49  "optionally) "
50  "if one of the side's elements is in included_subdomain_ids and if it "
51  "features the correct normal.");
52 
53  return params;
54 }
55 
57  : SideSetsGeneratorBase(parameters),
58  FunctionParserUtils(parameters),
59  _input(getMesh("input")),
60  _function(parameters.get<std::string>("combinatorial_geometry")),
61  _sideset_name(getParam<BoundaryName>("new_sideset_name")),
62  _check_subdomains(isParamValid("included_subdomain_ids")),
63  _check_normal(parameters.isParamSetByUser("normal")),
64  _included_ids(_check_subdomains
65  ? parameters.get<std::vector<SubdomainID>>("included_subdomain_ids")
66  : std::vector<SubdomainID>()),
67  _normal(getParam<Point>("normal"))
68 {
69  if (typeid(_input).name() == typeid(std::unique_ptr<DistributedMesh>).name())
70  mooseError("GenerateAllSideSetsByNormals only works with ReplicatedMesh.");
71 
72  // base function object
74 
75  // set FParser internal feature flags
77 
78  // add the constant expressions
80  getParam<std::vector<std::string>>("constant_names"),
81  getParam<std::vector<std::string>>("constant_expressions"));
82 
83  // parse function
84  if (_func_F->Parse(_function, "x,y,z") >= 0)
85  mooseError("Invalid function\n",
86  _function,
87  "\nin ParsedAddSideset ",
88  name(),
89  ".\n",
90  _func_F->ErrorMsg());
91 
92  _func_params.resize(3);
93 }
94 
95 std::unique_ptr<MeshBase>
97 {
98  std::unique_ptr<MeshBase> mesh = std::move(_input);
99 
100  setup(*mesh);
101 
102  // Get a reference to our BoundaryInfo object for later use
103  BoundaryInfo & boundary_info = mesh->get_boundary_info();
104 
105  // Get the BoundaryIDs from the mesh
106  std::vector<boundary_id_type> boundary_ids =
108  mooseAssert(boundary_ids.size() == 1, "Length of boundary_ids should be one");
109 
110  for (const auto & elem : mesh->active_element_ptr_range())
111  {
112  subdomain_id_type curr_subdomain = elem->subdomain_id();
113 
114  // check if the element is included
115  if (_check_subdomains &&
116  std::find(_included_ids.begin(), _included_ids.end(), curr_subdomain) ==
117  _included_ids.end())
118  continue;
119 
120  for (unsigned int side = 0; side < elem->n_sides(); ++side)
121  {
122  _fe_face->reinit(elem, side);
123  const std::vector<Point> & normals = _fe_face->get_normals();
124 
125  // check normal if requested
126  if (_check_normal && std::abs(1.0 - _normal * normals[0]) > _variance)
127  continue;
128 
129  // check expression
130  std::unique_ptr<Elem> curr_side = elem->side_ptr(side);
131  _func_params[0] = curr_side->centroid()(0);
132  _func_params[1] = curr_side->centroid()(1);
133  _func_params[2] = curr_side->centroid()(2);
134  if (evaluate(_func_F))
135  boundary_info.add_side(elem, side, boundary_ids[0]);
136  }
137  }
138  finalize();
139  boundary_info.sideset_name(boundary_ids[0]) = _sideset_name;
140  boundary_info.nodeset_name(boundary_ids[0]) = _sideset_name;
141 
142  return dynamic_pointer_cast<MeshBase>(mesh);
143 }
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
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
ParsedGenerateSideset(const InputParameters &parameters)
FunctionParserADBase< Real > ADFunction
Shorthand for an autodiff function parser object.
InputParameters validParams< ParsedGenerateSideset >()
std::unique_ptr< MeshBase > & _input
std::string _function
function expression
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.
MeshGenerator for defining a Sideset by a parsed expression and optionally by looking at the subdomai...
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...
bool _check_normal
whether to check normals when adding sides or not
bool _check_subdomains
whether to check subdomain ids when adding sides or not
std::unique_ptr< FEBase > _fe_face
subdomain_id_type SubdomainID
void finalize()
This method finalizes the object, setting names back in the boundary_info object and releasing memory...
ADFunctionPtr _func_F
function parser object describing the combinatorial geometry
InputParameters validParams< SideSetsGeneratorBase >()
BoundaryName _sideset_name
name of the new sideset
std::vector< libMesh::boundary_id_type > getBoundaryIDs(const libMesh::MeshBase &mesh, const std::vector< BoundaryName > &boundary_name, bool generate_unknown)
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< subdomain_id_type > _included_ids
A list of included subdomain ids that the side has to be part of.
registerMooseObject("MooseApp", ParsedGenerateSideset)
Point _normal
A normal vector that (if provided) is compared against side&#39;s normals.
void setup(MeshBase &mesh)
This method is used to construct the FE object so we can compute normals of faces.