www.mooseframework.org
SideSetsFromNormalsGenerator.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 "Parser.h"
12 #include "InputParameters.h"
13 #include "MooseMeshUtils.h"
14 #include "CastUniquePointer.h"
15 #include "MooseApp.h"
16 #include "MeshGeneratorSystem.h"
17 
18 #include "libmesh/mesh_generation.h"
19 #include "libmesh/mesh.h"
20 #include "libmesh/string_to_enum.h"
21 #include "libmesh/quadrature_gauss.h"
22 #include "libmesh/point_locator_base.h"
23 #include "libmesh/distributed_mesh.h"
24 #include "libmesh/elem.h"
25 #include "libmesh/fe_base.h"
26 
27 #include <typeinfo>
28 
30 
33 {
35 
36  params.addRequiredParam<MeshGeneratorName>("input", "The mesh we want to modify");
37  params.addClassDescription(
38  "Adds a new named sideset to the mesh for all faces matching the specified normal.");
39  params.addRequiredParam<std::vector<BoundaryName>>("new_boundary",
40  "The names of the boundaries to create");
41  params.addRequiredParam<std::vector<Point>>(
42  "normals", "A list of normals for which to start painting sidesets");
43  params.addParam<Real>("tolerance", 1e-5, "Tolerance for comparing the face nornmal");
44 
45  return params;
46 }
47 
49  : SideSetsGeneratorBase(parameters),
50  _input(getMesh("input")),
51  _normals(getParam<std::vector<Point>>("normals")),
52  _boundary_to_normal_map(
53  declareMeshProperty<std::map<BoundaryID, RealVectorValue>>("boundary_normals")),
54  _tolerance(getParam<Real>("tolerance"))
55 {
56  // Get the BoundaryIDs from the mesh
57  _boundary_names = getParam<std::vector<BoundaryName>>("new_boundary");
58 
59  if (_normals.size() != _boundary_names.size())
60  mooseError("normal list and boundary list are not the same length");
61 
62  // Make sure that the normals are normalized
63  for (auto & normal : _normals)
64  {
65  if (normal.norm() < 1e-5)
66  mooseError("Normal is zero");
67  normal /= normal.norm();
68  }
69 }
70 
71 std::unique_ptr<MeshBase>
73 {
74  std::unique_ptr<MeshBase> mesh = std::move(_input);
75  if (!_fixed_normal && !mesh->is_replicated())
76  mooseError("SideSetsFromNormalsGenerator is not implemented for distributed meshes when "
77  "fixed_normal = false");
78 
79  std::vector<BoundaryID> boundary_ids =
81 
82  setup(*mesh);
83 
84  _visited.clear();
85 
86  // We'll need to loop over all of the elements to find ones that match this normal.
87  // We can't rely on flood catching them all here...
88  for (const auto & elem : mesh->element_ptr_range())
89  for (unsigned int side = 0; side < elem->n_sides(); ++side)
90  {
91  if (elem->neighbor_ptr(side))
92  continue;
93 
94  const std::vector<Point> & normals = _fe_face->get_normals();
95  _fe_face->reinit(elem, side);
96 
97  for (unsigned int i = 0; i < boundary_ids.size(); ++i)
98  {
99  if (std::abs(1.0 - _normals[i] * normals[0]) < _tolerance)
100  flood(elem, _normals[i], boundary_ids[i], *mesh);
101  }
102  }
103 
104  finalize();
105 
106  BoundaryInfo & boundary_info = mesh->get_boundary_info();
107  for (unsigned int i = 0; i < boundary_ids.size(); ++i)
108  {
109  boundary_info.sideset_name(boundary_ids[i]) = _boundary_names[i];
110  _boundary_to_normal_map[boundary_ids[i]] = _normals[i];
111  }
112 
113  // This is a terrible hack that we'll want to remove once BMBBG isn't terrible
115  mesh->set_isnt_prepared();
116  return dynamic_pointer_cast<MeshBase>(mesh);
117 }
std::unique_ptr< MeshBase > & _input
the mesh to add the sidesets to
MeshBase & mesh
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
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.
std::vector< Point > _normals
holds the normals used to generate sidesets
bool hasBreakMeshByBlockGenerator() const
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...
SideSetsFromNormalsGenerator(const InputParameters &parameters)
ADRealEigenVector< T, D, asd > abs(const ADRealEigenVector< T, D, asd > &)
boundary_id_type BoundaryID
const Real _tolerance
a tolerance for comparing normals
std::unique_ptr< FEBase > _fe_face
void flood(const Elem *elem, Point normal, boundary_id_type side_id, MeshBase &mesh)
This method implements a recursive flood routine to paint a sideset of mesh to neighboring faces give...
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.
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:69
void finalize()
This method finalizes the object, setting names back in the boundary_info object and releasing memory...
registerMooseObject("MooseApp", SideSetsFromNormalsGenerator)
static InputParameters validParams()
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
std::vector< BoundaryName > _boundary_names
holds the boundary names for the sidesets
A mesh generator to generate new sidesets from all faces matching the normal.
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
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...
std::map< BoundaryID, RealVectorValue > & _boundary_to_normal_map
a map from the boundaries to the normals
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
MeshGeneratorSystem & getMeshGeneratorSystem()
Gets the system that manages the MeshGenerators.
Definition: MooseApp.h:838
std::map< boundary_id_type, std::set< const Elem * > > _visited
void setup(MeshBase &mesh)
This method is used to construct the FE object so we can compute normals of faces.