www.mooseframework.org
ExtraNodesetGenerator.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 "ExtraNodesetGenerator.h"
11 #include "MooseMesh.h"
12 #include "Conversion.h"
13 #include "MooseMeshUtils.h"
14 #include "CastUniquePointer.h"
15 
16 #include "libmesh/elem.h"
17 
19 
22 {
24 
25  params.addRequiredParam<MeshGeneratorName>("input", "The mesh we want to modify");
26  params.addRequiredParam<std::vector<BoundaryName>>("new_boundary",
27  "The names of the boundaries to create");
28 
29  params.addParam<std::vector<unsigned int>>("nodes",
30  "The nodes you want to be in the nodeset "
31  "(Either this parameter or \"coord\" must be "
32  "supplied).");
33  params.addParam<std::vector<std::vector<Real>>>(
34  "coord",
35  {},
36  "The nodes with coordinates you want to be in the "
37  "nodeset. Separate multple coords with ';' (Either this parameter or \"nodes\" must be "
38  "supplied).");
39  params.addParam<Real>(
40  "tolerance", TOLERANCE, "The tolerance in which two nodes are considered identical");
41  params.addClassDescription(
42  "Creates a new node set and a new boundary made with the nodes the user provides.");
43 
44  return params;
45 }
46 
48  : MeshGenerator(parameters), _input(getMesh("input"))
49 {
50 }
51 
52 std::unique_ptr<MeshBase>
54 {
55  std::unique_ptr<MeshBase> mesh = std::move(_input);
56 
57  // Get the BoundaryIDs from the mesh
58  std::vector<BoundaryName> boundary_names = getParam<std::vector<BoundaryName>>("new_boundary");
59  std::vector<boundary_id_type> boundary_ids =
60  MooseMeshUtils::getBoundaryIDs(*mesh, boundary_names, true);
61 
62  // Get a reference to our BoundaryInfo object
63  BoundaryInfo & boundary_info = mesh->get_boundary_info();
64 
65  // add nodes with their ids
66  if (isParamValid("nodes"))
67  for (const auto & node_id : getParam<std::vector<unsigned int>>("nodes"))
68  {
69  // Our mesh may be distributed and this node may not exist on this process
70  if (!mesh->query_node_ptr(node_id))
71  continue;
72 
73  for (const auto & boundary_id : boundary_ids)
74  boundary_info.add_node(node_id, boundary_id);
75  }
76 
77  // add nodes with their coordinates
78  const auto dim = mesh->mesh_dimension();
79 
80  std::unique_ptr<PointLocatorBase> locator = mesh->sub_point_locator();
81  locator->enable_out_of_mesh_mode();
82 
83  const auto tolerance = getParam<Real>("tolerance");
84  for (const auto & c : getParam<std::vector<std::vector<Real>>>("coord"))
85  {
86  Point p;
87  if (c.size() < dim)
88  paramError("coord",
89  "Coordinate ",
91  " does not have enough components for a ",
92  dim,
93  "D mesh.");
94 
95  if (c.size() > 3)
96  paramError("coord",
97  "Coordinate ",
99  " has too many components. Did you maybe forget to separate multiple coordinates "
100  "with a ';'?");
101 
102  for (unsigned int j = 0; j < c.size(); ++j)
103  p(j) = c[j];
104 
105  // locate candidate element
106  bool on_node = false;
107  bool found_elem = false;
108  const Elem * elem = (*locator)(p);
109  if (elem)
110  {
111  found_elem = true;
112  for (unsigned int j = 0; j < elem->n_nodes(); ++j)
113  {
114  const Node * node = elem->node_ptr(j);
115  if (p.absolute_fuzzy_equals(*node, tolerance))
116  {
117  for (const auto & boundary_id : boundary_ids)
118  boundary_info.add_node(node, boundary_id);
119 
120  on_node = true;
121  break;
122  }
123  }
124  }
125 
126  // If we are on a distributed mesh, then any particular processor
127  // may be unable to find any particular node, but *some* processor
128  // should have found it.
129  if (!mesh->is_replicated())
130  {
131  this->comm().max(found_elem);
132  this->comm().max(on_node);
133  }
134 
135  if (!found_elem)
136  mooseError("Unable to locate the following point within the domain, please check its "
137  "coordinates:\n",
138  p);
139 
140  if (!on_node)
141  mooseError("No node found at point:\n", p);
142  }
143 
144  for (unsigned int i = 0; i < boundary_ids.size(); ++i)
145  boundary_info.nodeset_name(boundary_ids[i]) = boundary_names[i];
146 
147  // This is a terrible hack that we'll want to remove once BMBBG isn't terrible
149  mesh->set_isnt_prepared();
150  return dynamic_pointer_cast<MeshBase>(mesh);
151 }
MeshBase & mesh
registerMooseObject("MooseApp", ExtraNodesetGenerator)
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
static constexpr std::size_t dim
This is the dimension of all vector and tensor datastructures used in MOOSE.
Definition: Moose.h:148
const Parallel::Communicator & comm() const
std::unique_ptr< MeshBase > & _input
mesh to modify
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 > generate() override
Generate / modify the mesh.
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...
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
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 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 ...
static InputParameters validParams()
Definition: MeshGenerator.C:23
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:62
ExtraNodesetGenerator(const InputParameters &parameters)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void max(const T &r, T &o, Request &req) const
static InputParameters validParams()
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...
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
MeshGenerators are objects that can modify or add to an existing mesh.
Definition: MeshGenerator.h:32