https://mooseframework.inl.gov
SubdomainBoundingBoxGenerator.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 
11 #include "Conversion.h"
12 #include "CastUniquePointer.h"
13 #include "MooseUtils.h"
14 #include "MooseMeshUtils.h"
15 
16 #include "libmesh/elem.h"
17 
19 
22 {
23  MooseEnum location("INSIDE OUTSIDE", "INSIDE");
24 
26 
27  params.addRequiredParam<MeshGeneratorName>("input", "The mesh we want to modify");
28  params.addClassDescription("Changes the subdomain ID of elements either (XOR) inside or outside "
29  "the specified box to the specified ID.");
31  "bottom_left", "The bottom left point (in x,y,z with spaces in-between).");
33  "top_right", "The bottom left point (in x,y,z with spaces in-between).");
35  "block_id", "Subdomain id to set for inside/outside the bounding box");
36  params.addParam<SubdomainName>(
37  "block_name", "Subdomain name to set for inside/outside the bounding box (optional)");
38  params.addParam<MooseEnum>(
39  "location", location, "Control of where the subdomain id is to be set");
40  params.addParam<std::vector<SubdomainName>>(
41  "restricted_subdomains",
42  "Only reset subdomain ID for given subdomains within the bounding box");
43 
44  params.addParam<std::string>("integer_name",
45  "Element integer to be assigned (default to subdomain ID)");
46  return params;
47 }
48 
50  : MeshGenerator(parameters),
51  _input(getMesh("input")),
52  _location(parameters.get<MooseEnum>("location")),
53  _block_id(parameters.get<subdomain_id_type>("block_id")),
54  _has_restriction(isParamValid("restricted_subdomains")),
55  _bounding_box(MooseUtils::buildBoundingBox(parameters.get<RealVectorValue>("bottom_left"),
56  parameters.get<RealVectorValue>("top_right")))
57 {
58 }
59 
60 std::unique_ptr<MeshBase>
62 {
63  std::unique_ptr<MeshBase> mesh = std::move(_input);
64 
65  // We'll need cached subdomain_ids later
66  if (!mesh->preparation().has_cached_elem_data)
67  mesh->cache_elem_data();
68 
69  std::set<SubdomainID> restricted_ids;
70  if (_has_restriction)
71  {
72  auto names = getParam<std::vector<SubdomainName>>("restricted_subdomains");
73  for (auto & name : names)
74  {
75  // check that the subdomain exists in the mesh
77  paramError("restricted_subdomains", "The block '", name, "' was not found in the mesh");
78 
79  restricted_ids.insert(MooseMeshUtils::getSubdomainID(name, *mesh));
80  }
81  }
82 
83  if (isParamValid("integer_name"))
84  {
85  std::string integer_name = getParam<std::string>("integer_name");
86 
87  if (!mesh->has_elem_integer(integer_name))
88  mooseError("Mesh does not have an element integer names as ", integer_name);
89 
90  unsigned int id = mesh->get_elem_integer_index(integer_name);
91 
92  // Loop over the elements
93  for (const auto & elem : mesh->active_element_ptr_range())
94  {
95  if (_has_restriction && restricted_ids.count(elem->subdomain_id()) == 0)
96  continue;
97 
98  bool contains = _bounding_box.contains_point(elem->vertex_average());
99  if ((contains && _location == "INSIDE") || (!contains && _location == "OUTSIDE"))
100  elem->set_extra_integer(id, _block_id);
101  }
102  }
103  else
104  {
105  // Loop over the elements
106  for (const auto & elem : mesh->element_ptr_range())
107  {
108  if (_has_restriction && restricted_ids.count(elem->subdomain_id()) == 0)
109  continue;
110 
111  bool contains = _bounding_box.contains_point(elem->vertex_average());
112  if ((contains && _location == "INSIDE") || (!contains && _location == "OUTSIDE"))
113  elem->subdomain_id() = _block_id;
114  }
115 
116  // Assign block name, if provided
117  if (isParamValid("block_name"))
118  mesh->subdomain_name(_block_id) = getParam<SubdomainName>("block_name");
119  }
120 
121  mesh->unset_is_prepared();
122  return dynamic_pointer_cast<MeshBase>(mesh);
123 }
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition: MooseBase.h:467
MeshGenerator for defining a Subdomain inside or outside of a bounding box.
MeshBase & mesh
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
SubdomainBoundingBoxGenerator(const InputParameters &parameters)
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
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::unique_ptr< MeshBase > & _input
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...
SubdomainID getSubdomainID(const SubdomainName &subdomain_name, const MeshBase &mesh)
Gets the subdomain ID associated with the given SubdomainName.
const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:103
subdomain_id_type _block_id
Block ID to assign to the region.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:54
bool contains(std::string_view superstring, std::string_view substring)
static InputParameters validParams()
Definition: MeshGenerator.C:23
bool hasSubdomainName(const MeshBase &input_mesh, const SubdomainName &name)
Whether a particular subdomain name exists in the mesh.
const bool _has_restriction
Whether or not we apply the bounding box only for certain subdomains.
registerMooseObject("MooseApp", SubdomainBoundingBoxGenerator)
MooseEnum _location
ID location (inside of outside of box)
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition: MooseBase.h:281
BoundingBox buildBoundingBox(const Point &p1, const Point &p2)
Definition: MooseUtils.C:1131
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...
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...
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseBase.h:209
BoundingBox _bounding_box
Bounding box for testing element centroids against.
MeshGenerators are objects that can modify or add to an existing mesh.
Definition: MeshGenerator.h:33
const Elem & get(const ElemType type_in)