www.mooseframework.org
SideSetsFromNormals.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 "SideSetsFromNormals.h"
11 #include "Parser.h"
12 #include "InputParameters.h"
13 #include "MooseMesh.h"
14 
15 #include "libmesh/mesh_generation.h"
16 #include "libmesh/mesh.h"
17 #include "libmesh/string_to_enum.h"
18 #include "libmesh/quadrature_gauss.h"
19 #include "libmesh/point_locator_base.h"
20 #include "libmesh/elem.h"
21 #include "libmesh/fe_base.h"
22 
24 
25 template <>
28 {
30  params.addClassDescription(
31  "Adds a new named sideset to the mesh for all faces matching the specified normal.");
32  params.addRequiredParam<std::vector<BoundaryName>>("new_boundary",
33  "The name of the boundary to create");
34  params.addRequiredParam<std::vector<Point>>(
35  "normals", "A list of normals for which to start painting sidesets");
36  return params;
37 }
38 
40  : AddSideSetsBase(parameters), _normals(getParam<std::vector<Point>>("normals"))
41 {
42 
43  // Get the BoundaryIDs from the mesh
44  _boundary_names = getParam<std::vector<BoundaryName>>("new_boundary");
45 
46  if (_normals.size() != _boundary_names.size())
47  mooseError("normal list and boundary list are not the same length");
48 
49  // Make sure that the normals are normalized
50  for (auto & normal : _normals)
51  {
52  mooseAssert(normal.norm() >= 1e-5, "Normal is zero");
53  normal /= normal.norm();
54  }
55 }
56 
57 void
59 {
60  if (!_mesh_ptr)
61  mooseError("_mesh_ptr must be initialized before calling SideSetsFromNormals::modify()!");
62 
63  // We can't call this in the constructor, it appears that _mesh_ptr is always NULL there.
64  _mesh_ptr->errorIfDistributedMesh("SideSetsFromNormals");
65 
66  std::vector<BoundaryID> boundary_ids = _mesh_ptr->getBoundaryIDs(_boundary_names, true);
67 
68  setup();
69 
70  _visited.clear();
71 
72  // We'll need to loop over all of the elements to find ones that match this normal.
73  // We can't rely on flood catching them all here...
74  for (const auto & elem : _mesh_ptr->getMesh().element_ptr_range())
75  for (unsigned int side = 0; side < elem->n_sides(); ++side)
76  {
77  if (elem->neighbor_ptr(side))
78  continue;
79 
80  _fe_face->reinit(elem, side);
81  const std::vector<Point> & normals = _fe_face->get_normals();
82 
83  for (unsigned int i = 0; i < boundary_ids.size(); ++i)
84  {
85  if (std::abs(1.0 - _normals[i] * normals[0]) < 1e-5)
86  flood(elem, _normals[i], boundary_ids[i]);
87  }
88  }
89 
90  finalize();
91 
92  BoundaryInfo & boundary_info = _mesh_ptr->getMesh().get_boundary_info();
93  for (unsigned int i = 0; i < boundary_ids.size(); ++i)
94  boundary_info.sideset_name(boundary_ids[i]) = _boundary_names[i];
95 }
MetaPhysicL::DualNumber< T, D > abs(const MetaPhysicL::DualNumber< T, D > &in)
std::unique_ptr< FEBase > _fe_face
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
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...
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...
virtual void modify() override
Pure virtual modify function MUST be overridden by children classes.
MooseMesh * _mesh_ptr
Pointer to the mesh.
Definition: MeshModifier.h:68
std::vector< BoundaryName > _boundary_names
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", SideSetsFromNormals)
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:2567
std::vector< Point > _normals
InputParameters validParams< AddSideSetsBase >()
std::map< BoundaryID, std::set< const Elem * > > _visited
void flood(const Elem *elem, Point normal, BoundaryID side_id)
This method implements a recursive flood routine to paint a sideset of mesh to neighboring faces give...
InputParameters validParams< SideSetsFromNormals >()
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::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
SideSetsFromNormals(const InputParameters &parameters)
void setup()
This method is used to construct the FE object so we can compute normals of faces.