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 "MooseApp.h"
12 #include "MooseMesh.h"
13 #include "FEProblem.h"
14 #include "ActionWarehouse.h"
15 #include "MooseMeshUtils.h"
16 #include "CastUniquePointer.h"
17 
19 
20 template <>
23 {
25 
26  params.addRequiredParam<MeshGeneratorName>("input", "The mesh we want to modify");
27  params.addRequiredParam<std::vector<BoundaryName>>("new_boundary",
28  "The name of the boundary to create");
29 
30  params.addParam<std::vector<unsigned int>>("nodes",
31  "The nodes you want to be in the nodeset "
32  "(Either this parameter or \"coord\" must be "
33  "supplied).");
34  params.addParam<std::vector<Real>>("coord",
35  "The nodes with coordinates you want to be in the "
36  "nodeset (Either this parameter or \"nodes\" must be "
37  "supplied).");
38  params.addParam<Real>(
39  "tolerance", TOLERANCE, "The tolerance in which two nodes are considered identical");
40 
41  return params;
42 }
43 
45  : MeshGenerator(parameters), _input(getMesh("input"))
46 {
47 }
48 
49 std::unique_ptr<MeshBase>
51 {
52  std::unique_ptr<MeshBase> mesh = std::move(_input);
53 
54  // make sure the input is not empty
55  bool data_valid = false;
56  if (isParamValid("nodes"))
57  if (getParam<std::vector<unsigned int>>("nodes").size() != 0)
58  data_valid = true;
59  if (_pars.isParamValid("coord"))
60  {
61  unsigned int n_coord = getParam<std::vector<Real>>("coord").size();
62  if (n_coord % mesh->mesh_dimension() != 0)
63  mooseError("Size of node coordinates does not match the mesh dimension");
64  if (n_coord != 0)
65  data_valid = true;
66  }
67  if (!data_valid)
68  mooseError("Node set can not be empty!");
69 
70  // Get the BoundaryIDs from the mesh
71  std::vector<BoundaryName> boundary_names = getParam<std::vector<BoundaryName>>("new_boundary");
72  std::vector<boundary_id_type> boundary_ids =
73  MooseMeshUtils::getBoundaryIDs(*mesh, boundary_names, true);
74 
75  // Get a reference to our BoundaryInfo object
76  BoundaryInfo & boundary_info = mesh->get_boundary_info();
77 
78  // add nodes with their ids
79  const std::vector<unsigned int> & nodes = getParam<std::vector<unsigned int>>("nodes");
80  for (const auto & node_id : nodes)
81  for (const auto & boundary_id : boundary_ids)
82  boundary_info.add_node(node_id, boundary_id);
83 
84  // add nodes with their coordinates
85  const std::vector<Real> & coord = getParam<std::vector<Real>>("coord");
86  unsigned int dim = mesh->mesh_dimension();
87  unsigned int n_nodes = coord.size() / dim;
88 
89  std::unique_ptr<PointLocatorBase> locator = mesh->sub_point_locator();
90  locator->enable_out_of_mesh_mode();
91 
92  for (unsigned int i = 0; i < n_nodes; ++i)
93  {
94  Point p;
95  for (unsigned int j = 0; j < dim; ++j)
96  p(j) = coord[i * dim + j];
97 
98  bool on_node = false;
99 
100  const Elem * elem = (*locator)(p);
101 
102  if (elem)
103  {
104  for (unsigned int j = 0; j < elem->n_nodes(); ++j)
105  {
106  const Node * node = elem->node_ptr(j);
107 
108  Point q;
109  for (unsigned int k = 0; k < dim; ++k)
110  q(k) = (*node)(k);
111 
112  if (p.absolute_fuzzy_equals(q, getParam<Real>("tolerance")))
113  {
114  for (const auto & boundary_id : boundary_ids)
115  boundary_info.add_node(node, boundary_id);
116 
117  on_node = true;
118  break;
119  }
120  }
121  }
122 
123  bool found_elem = elem;
124 
125  // If we are on a distributed mesh, then any particular processor
126  // may be unable to find any particular node, but *some* processor
127  // should have found it.
128  if (!mesh->is_replicated())
129  {
130  this->comm().max(found_elem);
131  this->comm().max(on_node);
132  }
133 
134  if (!found_elem)
135  mooseError(
136  "Unable to locate the following point within the domain, please check its coordinates:\n",
137  p);
138 
139  if (!on_node)
140  mooseError("Point can not be located!");
141  }
142 
143  for (unsigned int i = 0; i < boundary_ids.size(); ++i)
144  boundary_info.nodeset_name(boundary_ids[i]) = boundary_names[i];
145 
146  return dynamic_pointer_cast<MeshBase>(mesh);
147 }
InputParameters validParams< ExtraNodesetGenerator >()
registerMooseObject("MooseApp", ExtraNodesetGenerator)
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::unique_ptr< MeshBase > & _input
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.
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseObject.h:191
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...
const InputParameters & _pars
Parameters of this object, references the InputParameters stored in the InputParametersWarehouse.
Definition: MooseObject.h:174
ExtraNodesetGenerator(const InputParameters &parameters)
std::vector< libMesh::boundary_id_type > getBoundaryIDs(const libMesh::MeshBase &mesh, const std::vector< BoundaryName > &boundary_name, bool generate_unknown)
MPI_Comm comm
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...
InputParameters validParams< MeshGenerator >()
Definition: MeshGenerator.C:16
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:89
MeshGenerators are objects that can modify or add to an existing mesh.
Definition: MeshGenerator.h:30
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.