www.mooseframework.org
AddSideSetsFromBoundingBox.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 "Conversion.h"
12 #include "MooseMesh.h"
13 #include "MooseTypes.h"
14 
15 #include "libmesh/elem.h"
16 
18 
19 template <>
22 {
24  params.addClassDescription("Find sidesets with given boundary ids in bounding box and add new "
25  "boundary id. This can be done by finding all required boundary "
26  "and adding the new boundary id to those sidesets. Alternatively, "
27  "a number of boundary ids can be provided and all nodes within the "
28  "bounding box that have all the required boundary ids will have a new"
29  "boundary id added.");
30 
31  MooseEnum location("INSIDE OUTSIDE", "INSIDE");
32 
34  "bottom_left", "The bottom left point (in x,y,z with spaces in-between).");
36  "top_right", "The bottom left point (in x,y,z with spaces in-between).");
37  params.addRequiredParam<std::vector<BoundaryName>>(
38  "boundary_id_old", "Boundary id on specified block within the bounding box to select");
39  params.addRequiredParam<boundary_id_type>(
40  "boundary_id_new", "Boundary id on specified block within the bounding box to assign");
41  params.addParam<bool>("boundary_id_overlap",
42  false,
43  "Set to true if boundaries need to overlap on sideset to be detected.");
44  params.addParam<MooseEnum>(
45  "location", location, "Control of where the subdomain id is to be set");
46 
47  return params;
48 }
49 
51  : MeshModifier(parameters),
52  _location(parameters.get<MooseEnum>("location")),
53  _boundary_id_old(parameters.get<std::vector<BoundaryName>>("boundary_id_old")),
54  _boundary_id_new(parameters.get<boundary_id_type>("boundary_id_new")),
55  _bounding_box(parameters.get<RealVectorValue>("bottom_left"),
56  parameters.get<RealVectorValue>("top_right")),
57  _boundary_id_overlap(parameters.get<bool>("boundary_id_overlap"))
58 {
59 }
60 
61 void
63 {
64  // this modifier is not designed for working with distributed mesh
65  _mesh_ptr->errorIfDistributedMesh("AddSideSetsFromBoundingBox");
66 
67  // Check that we have access to the mesh
68  if (!_mesh_ptr)
69  mooseError("_mesh_ptr must be initialized before calling SubdomainBoundingBox::modify()");
70 
71  // Reference the the libMesh::MeshBase
72  MeshBase & mesh = _mesh_ptr->getMesh();
73 
74  // Get a reference to our BoundaryInfo object for later use
75  BoundaryInfo & boundary_info = mesh.get_boundary_info();
76 
77  bool found_element = false;
78  bool found_side_sets = false;
79 
81  {
82  // Loop over the elements
83  for (const auto & elem : mesh.active_element_ptr_range())
84  {
85  // boolean if element centroid is in bounding box
86  bool contains = _bounding_box.contains_point(elem->centroid());
87 
88  // check if active elements are found in the bounding box
89  if (contains)
90  {
91  found_element = true;
92  // loop over sides of elements within bounding box
93  for (unsigned int side = 0; side < elem->n_sides(); side++)
94  // loop over provided boundary vector to check all side sets for all boundary ids
95  for (unsigned int boundary_id_number = 0; boundary_id_number < _boundary_id_old.size();
96  boundary_id_number++)
97  // check if side has same boundary id that you are looking for
98  if (boundary_info.has_boundary_id(
99  elem, side, boundary_info.get_id_by_name(_boundary_id_old[boundary_id_number])))
100  {
101  // assign new boundary value to boundary which meets meshmodifier criteria
102  boundary_info.add_side(elem, side, _boundary_id_new);
103  found_side_sets = true;
104  }
105  }
106  }
107  if (!found_element)
108  mooseError("No elements found within the bounding box");
109 
110  if (!found_side_sets)
111  mooseError("No side sets found on active elements within the bounding box");
112  }
113 
114  else if (_boundary_id_overlap)
115  {
116  if (_boundary_id_old.size() < 2)
117  mooseError("boundary_id_old out of bounds: ",
118  _boundary_id_old.size(),
119  " Must be 2 boundary inputs or more.");
120 
121  bool found_node = false;
122  const bool inside = (_location == "INSIDE");
123 
124  // Loop over the elements and assign node set id to nodes within the bounding box
125  for (auto node = mesh.active_nodes_begin(); node != mesh.active_nodes_end(); ++node)
126  {
127  // check if nodes are inside of bounding box
128  if (_bounding_box.contains_point(**node) == inside)
129  {
130  // read out boundary ids for nodes
131  std::vector<boundary_id_type> boundary_id_list;
132  boundary_info.boundary_ids(*node, boundary_id_list);
133  std::vector<boundary_id_type> boundary_id_old_list =
135 
136  // sort boundary ids on node and sort boundary ids provided in input file
137  std::sort(boundary_id_list.begin(), boundary_id_list.end());
138  std::sort(boundary_id_old_list.begin(), boundary_id_old_list.end());
139 
140  // check if input boundary ids are all contained in the node
141  // if true, write new boundary id on respective node
142  if (std::includes(boundary_id_list.begin(),
143  boundary_id_list.end(),
144  boundary_id_old_list.begin(),
145  boundary_id_old_list.end()))
146  {
147  boundary_info.add_node(*node, _boundary_id_new);
148  found_node = true;
149  }
150  }
151  }
152 
153  if (!found_node)
154  mooseError("No nodes found within the bounding box");
155  }
156 }
VectorValue< Real > RealVectorValue
Definition: Assembly.h:31
MeshModifiers are objects that can modify or add to an existing mesh.
Definition: MeshModifier.h:25
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
BoundingBox _bounding_box
Bounding box for testing element centroids against.
AddSideSetsFromBoundingBox(const InputParameters &parameters)
Class constructor.
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
void errorIfDistributedMesh(std::string name) const
Generate a unified error message if the underlying libMesh mesh is a DistributedMesh.
Definition: MooseMesh.C:2685
virtual void modify() override
Pure virtual modify function MUST be overridden by children classes.
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:2567
boundary_id_type _boundary_id_new
boundary ID to assign
registerMooseObject("MooseApp", AddSideSetsFromBoundingBox)
const bool _boundary_id_overlap
Flag to determine if the provided boundaries need to overlap.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
MeshModifier for defining a Subdomain inside or outside of a bounding box.
std::vector< BoundaryName > _boundary_id_old
boundary ID to select
InputParameters validParams< AddSideSetsFromBoundingBox >()
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
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...
MooseEnum _location
ID location (inside of outside of box)
InputParameters validParams< MeshModifier >()
Definition: MeshModifier.C:15