https://mooseframework.inl.gov
ParsedGenerateSideset.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
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 "MeshTraversingUtils.h"
13 #include "MooseMeshUtils.h"
14 #include "CastUniquePointer.h"
15 
16 #include "libmesh/fparser_ad.hh"
17 #include "libmesh/distributed_mesh.h"
18 #include "libmesh/elem.h"
19 #include "libmesh/fe_base.h"
20 
21 #include <typeinfo>
22 
24 
27 {
30 
31  params.addRequiredParam<std::string>("combinatorial_geometry",
32  "Function expression encoding a combinatorial geometry");
33  params.addRequiredParam<BoundaryName>("new_sideset_name", "The name of the new sideset");
34 
35  params.addParam<std::vector<std::string>>(
36  "constant_names", {}, "Vector of constants used in the parsed function");
37  params.addParam<std::vector<std::string>>(
38  "constant_expressions",
39  {},
40  "Vector of values for the constants in constant_names (can be an FParser expression)");
41 
42  // This sideset generator can only handle a single new sideset name, not a vector of names
43  params.suppressParameter<std::vector<BoundaryName>>("new_boundary");
44 
45  params.addClassDescription(
46  "A MeshGenerator that adds element sides to a sideset if the centroid of the side satisfies "
47  "the `combinatorial_geometry` expression.");
48 
49  return params;
50 }
51 
53  : SideSetsGeneratorBase(parameters),
54  FunctionParserUtils<false>(parameters),
55  _function(parameters.get<std::string>("combinatorial_geometry"))
56 {
57  _boundary_names.push_back(getParam<BoundaryName>("new_sideset_name"));
58 
59  // Create parsed function
60  _func_F = std::make_shared<SymFunction>();
62  _function,
63  "x,y,z",
64  getParam<std::vector<std::string>>("constant_names"),
65  getParam<std::vector<std::string>>("constant_expressions"),
66  comm());
67 
68  _func_params.resize(3);
69 }
70 
71 std::unique_ptr<MeshBase>
73 {
74  std::unique_ptr<MeshBase> mesh = std::move(_input);
75  if (!mesh->is_replicated())
77  "ParsedGenerateSideset is not implemented for distributed meshes. Make sure the "
78  "parsed sideset does NOT cross any mesh distribution boundaries, using the ProcessorAux");
79 
80  setup(*mesh);
81 
82  // Get a reference to our BoundaryInfo object for later use
83  BoundaryInfo & boundary_info = mesh->get_boundary_info();
84 
85  // Get the BoundaryIDs from the mesh
86  std::vector<boundary_id_type> boundary_ids =
88  mooseAssert(boundary_ids.size() == 1, "Length of boundary_ids should be one");
89 
90  for (const auto & elem : mesh->active_element_ptr_range())
91  {
92  // check if the element is included
93  if (_check_subdomains &&
95  continue;
96 
97  for (const auto side : make_range(elem->n_sides()))
98  {
99  _fe_face->reinit(elem, side);
100  // We'll just use the normal of the first qp
101  const Point & face_normal = _fe_face->get_normals()[0];
102 
103  if (!elemSideSatisfiesRequirements(elem, side, *mesh, _normal, face_normal))
104  continue;
105 
106  // check expression
107  std::unique_ptr<Elem> curr_side = elem->side_ptr(side);
108  _func_params[0] = curr_side->vertex_average()(0);
109  _func_params[1] = curr_side->vertex_average()(1);
110  _func_params[2] = curr_side->vertex_average()(2);
111  if (evaluate(_func_F))
112  {
113  if (_replace)
114  boundary_info.remove_side(elem, side);
115  boundary_info.add_side(elem, side, boundary_ids[0]);
116  }
117  }
118  }
119  finalize();
120  boundary_info.sideset_name(boundary_ids[0]) = _boundary_names[0];
121 
122  mesh->unset_is_prepared();
123  return dynamic_pointer_cast<MeshBase>(mesh);
124 }
bool elementSubdomainIdInList(const Elem *const elem, const std::vector< subdomain_id_type > &subdomain_id_list)
Determines whether the given element&#39;s subdomain id is in the given subdomain_id_list.
GenericReal< is_ad > evaluate(SymFunctionPtr &, const std::string &object_name="")
Evaluate FParser object and check EvalError.
SymFunctionPtr _func_F
function parser object describing the combinatorial geometry
bool elemSideSatisfiesRequirements(const Elem *const elem, const unsigned int side, const MeshBase &mesh, const Point &normal, const Point &face_normal)
Determines whether the given element&#39;s side satisfies the following parameters: include_only_external...
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
std::unique_ptr< MeshBase > & _input
the mesh to add the sidesets to
ParsedGenerateSideset(const InputParameters &parameters)
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseBase.h:416
std::string _function
function expression
Point _normal
if specified, then faces are only added if their normal is close to this
static InputParameters validParams()
MeshBase & mesh
const bool _replace
Whether or not to remove the old sidesets (all of them, if any) when adding sidesets.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const Parallel::Communicator & comm() const
std::vector< BoundaryName > _boundary_names
The list of new boundary names.
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 considering additional ...
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...
void suppressParameter(const std::string &name)
This method suppresses an inherited parameter so that it isn&#39;t required or valid in the derived class...
void mooseWarning(Args &&... args) const
std::vector< subdomain_id_type > _included_subdomain_ids
A list of included subdomain ids that the side has to be part of, extracted from the included_subdoma...
std::vector< BoundaryID > getBoundaryIDs(const libMesh::MeshBase &mesh, const std::vector< BoundaryName > &boundary_name, bool generate_unknown, const std::set< BoundaryID > &mesh_boundary_ids)
Gets the boundary IDs with their names.
void parsedFunctionSetup(SymFunctionPtr &function, const std::string &expression, const std::string &variables, const std::vector< std::string > &constant_names, const std::vector< std::string > &constant_expressions, const libMesh::Parallel::Communicator &comm) const
Performs setup steps on a SymFunction.
void finalize()
This method finalizes the object, setting names back in the boundary_info object and releasing memory...
static InputParameters validParams()
const bool _check_subdomains
whether to check subdomain ids of the element in the (element, side, boundary id) tuple when adding s...
std::vector< GenericReal< is_ad > > _func_params
Array to stage the parameters passed to the functions when calling Eval.
IntRange< T > make_range(T beg, T end)
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...
static InputParameters validParams()
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object...
registerMooseObject("MooseApp", ParsedGenerateSideset)
std::unique_ptr< libMesh::FEBase > _fe_face
void setup(MeshBase &mesh)
This method is used to construct the FE object so we can compute normals of faces.
const Elem & get(const ElemType type_in)