https://mooseframework.inl.gov
Loading...
Searching...
No Matches
NodeSetsGeneratorBase.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 "Parser.h"
12#include "InputParameters.h"
13#include "MooseMeshUtils.h"
14
15#include "libmesh/mesh_generation.h"
16#include "libmesh/mesh.h"
17#include "libmesh/elem.h"
18
21{
23 params.addRequiredParam<MeshGeneratorName>("input", "The mesh we want to modify");
24 params.addRequiredParam<std::vector<BoundaryName>>("new_nodeset",
25 "The list of nodeset names to create.");
26
27 params.addParam<bool>("replace",
28 false,
29 "If true, replace the old nodesets. If false, the current nodesets (if "
30 "any) will be preserved.");
31
32 params.addParam<std::vector<BoundaryName>>(
33 "included_nodesets",
34 "A set of nodeset names or ids whose nodes will be included in the new nodesets. A node "
35 "is only added if it also belongs to one of these nodesets.");
36 params.addParam<std::vector<BoundaryName>>(
37 "excluded_nodesets",
38 "A set of nodeset names or ids whose nodes will be excluded from the new nodesets. A node "
39 "is only added if does not belong to any of these nodesets.");
40 params.addParam<std::vector<SubdomainName>>(
41 "included_subdomains",
42 "A set of subdomain names or ids whose nodes will be included in the new nodesets. A node "
43 "is only added if the subdomain id of the corresponding element is in this set.");
44 params.addParam<std::vector<SubdomainName>>(
45 "excluded_subdomains",
46 "A set of subdomain names or ids whose nodes will be excluded in the new nodesets. A node "
47 "is only added if the subdomain id of the corresponding element is not in this set.");
48
49 params.addParam<bool>(
50 "include_only_external_nodes",
51 false,
52 "Whether to only include external nodes when considering nodes to add to the nodeset");
53
54 // Nodeset restriction param group
55 params.addParamNamesToGroup("included_nodesets excluded_nodesets included_subdomains "
56 "include_only_external_nodes",
57 "Nodeset restrictions");
58
59 return params;
60}
61
63 : MeshGenerator(parameters),
64 _input(getMesh("input")),
65 _nodeset_names(std::vector<BoundaryName>()),
66 _replace(getParam<bool>("replace")),
67 _check_included_nodesets(isParamValid("included_nodesets")),
68 _check_excluded_nodesets(isParamValid("excluded_nodesets")),
69 _check_included_subdomains(isParamValid("included_subdomains")),
70 _check_excluded_subdomains(isParamValid("excluded_subdomains")),
71 _included_nodeset_ids(std::vector<boundary_id_type>()),
72 _excluded_nodeset_ids(std::vector<boundary_id_type>()),
73 _included_subdomain_ids(std::vector<subdomain_id_type>()),
74 _excluded_subdomain_ids(std::vector<subdomain_id_type>()),
75 _include_only_external_nodes(getParam<bool>("include_only_external_nodes"))
76{
77 if (isParamValid("new_nodeset"))
78 _nodeset_names = getParam<std::vector<BoundaryName>>("new_nodeset");
79}
80
81void
83{
84 // We'll need cached subdomain_ids later
85 if (!mesh.preparation().has_cached_elem_data)
86 mesh.cache_elem_data();
87
88 // Parameter checks and filling vector of ids (used instead of names for efficiency)
90 {
91 const auto & included_nodesets = getParam<std::vector<BoundaryName>>("included_nodesets");
92 for (const auto & nodeset_name : _nodeset_names)
93 if (std::find(included_nodesets.begin(), included_nodesets.end(), nodeset_name) !=
94 included_nodesets.end())
96 "new_nodeset",
97 "A nodeset cannot be both the new nodeset and be included in the list of included "
98 "nodesets. If you are trying to restrict an existing nodeset, you must use a "
99 "different name for 'new_nodeset', delete the old nodeset, and then rename the "
100 "new nodeset to the old nodeset.");
101
102 _included_nodeset_ids = MooseMeshUtils::getBoundaryIDs(mesh, included_nodesets, false);
103
104 // Check that the included nodeset ids/names exist in the mesh
105 for (const auto i : index_range(_included_nodeset_ids))
107 paramError("included_nodesets",
108 "The nodeset '",
109 included_nodesets[i],
110 "' was not found within the mesh");
111 }
112
114 {
115 const auto & excluded_nodesets = getParam<std::vector<BoundaryName>>("excluded_nodesets");
116 for (const auto & nodeset_name : _nodeset_names)
117 if (std::find(excluded_nodesets.begin(), excluded_nodesets.end(), nodeset_name) !=
118 excluded_nodesets.end())
120 "new_nodeset",
121 "A nodeset cannot be both the new nodeset and be excluded in the list of excluded "
122 "nodesets.");
123 _excluded_nodeset_ids = MooseMeshUtils::getBoundaryIDs(mesh, excluded_nodesets, false);
124
125 // Check that the excluded nodeset ids/names exist in the mesh
126 for (const auto i : index_range(_excluded_nodeset_ids))
128 paramError("excluded_nodesets",
129 "The nodeset '",
130 excluded_nodesets[i],
131 "' was not found within the mesh");
132
134 {
135 // Check that included and excluded nodeset lists do not overlap
136 for (const auto & nodeset_id : _included_nodeset_ids)
137 if (std::find(_excluded_nodeset_ids.begin(), _excluded_nodeset_ids.end(), nodeset_id) !=
139 paramError("excluded_nodesets",
140 "'included_nodesets' and 'excluded_nodesets' lists should not overlap");
141 }
142 }
143
144 // Get the subdomain ids from the names
146 {
147 // check that the subdomains exist in the mesh
148 const auto subdomains = getParam<std::vector<SubdomainName>>("included_subdomains");
149 for (const auto & name : subdomains)
151 paramError("included_subdomains", "The block '", name, "' was not found in the mesh");
152
154 }
155
157 {
158 // check that the subdomains exist in the mesh
159 const auto subdomains = getParam<std::vector<SubdomainName>>("excluded_subdomains");
160 for (const auto & name : subdomains)
162 paramError("excluded_subdomains", "The block '", name, "' was not found in the mesh");
163
165
167 {
168 // Check that included and excluded nodeset lists do not overlap
169 for (const auto & subdomain_id : _included_subdomain_ids)
170 if (std::find(_excluded_subdomain_ids.begin(),
172 subdomain_id) != _excluded_subdomain_ids.end())
173 paramError("excluded_subdomains",
174 "'included_subdomains' and 'excluded_subdomains' lists should not overlap");
175 }
176 }
177
178 // Build the node to element map, which is usually provided by a MooseMesh but in the mesh
179 // generation process we are working with a MeshBase
180 for (const auto & elem : mesh.active_element_ptr_range())
181 for (unsigned int n = 0; n < elem->n_nodes(); n++)
182 _node_to_elem_map[elem->node_id(n)].push_back(elem->id());
183}
184
185bool
187 const std::vector<dof_id_type> & node_elems,
188 const MeshBase & mesh) const
189{
190 // Loop on the elements and check whether the node is part of a side with no neighbor (exterior)
191 for (const auto elem_id : node_elems)
192 {
193 const auto elem = mesh.elem_ptr(elem_id);
194 for (const auto side_i : make_range(elem->n_sides()))
195 {
196 // Node is part of the side
197 if (elem->side_ptr(side_i)->get_node_index(node) != libMesh::invalid_uint)
198 {
199 // No neighbor on that side
200 if (!elem->neighbor_ptr(side_i))
201 return true;
202 }
203 }
204 }
205 return false;
206}
207
208bool
209NodeSetsGeneratorBase::nodeElementsInIncludedSubdomains(const std::vector<dof_id_type> node_elems,
210 const MeshBase & mesh) const
211{
212 for (const auto elem_id : node_elems)
213 {
214 subdomain_id_type curr_subdomain = mesh.elem_ptr(elem_id)->subdomain_id();
215 if (std ::find(_included_subdomain_ids.begin(),
217 curr_subdomain) != _included_subdomain_ids.end())
218 return true;
219 }
220 return false;
221}
222
223bool
224NodeSetsGeneratorBase::nodeElementsInExcludedSubdomains(const std::vector<dof_id_type> node_elems,
225 const MeshBase & mesh) const
226{
227 for (const auto elem_id : node_elems)
228 {
229 subdomain_id_type curr_subdomain = mesh.elem_ptr(elem_id)->subdomain_id();
230 if (std ::find(_excluded_subdomain_ids.begin(),
232 curr_subdomain) != _excluded_subdomain_ids.end())
233 return true;
234 }
235 return false;
236}
237
238bool
239NodeSetsGeneratorBase::nodeInIncludedNodesets(const std::vector<BoundaryID> & node_nodesets) const
240{
241 for (const auto bid : node_nodesets)
242 if (std::find(_included_nodeset_ids.begin(), _included_nodeset_ids.end(), bid) !=
244 return true;
245 return false;
246}
247
248bool
249NodeSetsGeneratorBase::nodeInExcludedNodesets(const std::vector<BoundaryID> & node_nodesets) const
250{
251 for (const auto bid : node_nodesets)
252 if (std::find(_excluded_nodeset_ids.begin(), _excluded_nodeset_ids.end(), bid) !=
254 return true;
255 return false;
256}
257
258bool
260 const std::vector<BoundaryID> & node_nodesets,
261 const std::vector<dof_id_type> & node_elems,
262 const MeshBase & mesh) const
263{
264 // Skip if side has neighbor and we only want external nodes
266 return false;
267
268 // Skip if none of the elements owning the node are in the list of accepted subdomains
270 return false;
271
272 // Skip if a element owning the node is in the list of excluded subdomains
273 // Note: if a node is on the interface of included subdomains and excluded subdomains,
274 // it will pass the above check but return false here, i.e. subdomain excluding will
275 // win if both subdomain including and excluding are true.
277 return false;
278
279 // Skip if side is not part of included nodesets
281 return false;
282
283 // Skip if side is part of excluded nodesets
285 return false;
286
287 return true;
288}
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
This method takes a space delimited list of parameter names and adds them to the specified group name...
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.
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...
MeshGenerators are objects that can modify or add to an existing mesh.
static InputParameters validParams()
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103
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:457
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition MooseBase.h:199
bool nodeOnMeshExteriorBoundary(const Node *node, const std::vector< dof_id_type > &node_elems, const MeshBase &mesh) const
Determines whether the node is on the exterior of the mesh.
bool nodeSatisfiesRequirements(const Node *node, const std::vector< BoundaryID > &node_nodesets, const std::vector< dof_id_type > &node_elems, const MeshBase &mesh) const
Determines whether the given node satisfies the user-specified constraints.
std::vector< subdomain_id_type > _excluded_subdomain_ids
A list of included subdomain ids that the node must not be part of, extracted from the excluded_subdo...
std::vector< subdomain_id_type > _included_subdomain_ids
A list of included subdomain ids that the node has to be part of, extracted from the included_subdoma...
void setup(MeshBase &mesh)
This method prepares a few attributes which are commonly needed for nodeset generation such as a map ...
const bool _check_included_subdomains
whether to check subdomain ids of the element that included this node
static InputParameters validParams()
bool nodeElementsInIncludedSubdomains(const std::vector< dof_id_type > node_elems, const MeshBase &mesh) const
Determines whether any neighbor element of the node has a subdomain id in the given included_subdomai...
bool nodeElementsInExcludedSubdomains(const std::vector< dof_id_type > node_elems, const MeshBase &mesh) const
Determines whether any neighbor element of the node has a subdomain id in the given excluded_subdomai...
bool nodeInExcludedNodesets(const std::vector< BoundaryID > &node_nodesets) const
Determines whether the given node of an element belongs to any nodesets in the excluded_nodesets para...
const bool _include_only_external_nodes
Whether to only include external node when considering nodes to add to the nodeset.
bool nodeInIncludedNodesets(const std::vector< BoundaryID > &node_nodesets) const
Determines whether the given node belongs to any nodesets in the included_nodesets parameter.
std::unordered_map< dof_id_type, std::vector< dof_id_type > > _node_to_elem_map
A map from nodes (ids) to local elements (ids) which comprise the node.
const bool _check_excluded_nodesets
whether to check nodeset ids against the excluded nodeset list when adding nodes or not
std::vector< BoundaryName > _nodeset_names
The list of new nodeset names.
NodeSetsGeneratorBase(const InputParameters &parameters)
std::vector< boundary_id_type > _included_nodeset_ids
A list of nodeset ids that the node has to be part of, extracted from the included_nodesets parameter...
const bool _check_included_nodesets
whether to check nodeset ids against the included nodeset list when adding nodes or not
const bool _check_excluded_subdomains
whether to check subdomain ids of the element that excluded this node
std::vector< boundary_id_type > _excluded_nodeset_ids
A list of nodeset ids that the node must not be a part of, extracted from the excluded_nodesets param...
MeshBase & mesh
bool hasSubdomainName(const MeshBase &input_mesh, const SubdomainName &name)
Whether a particular subdomain name exists in the mesh.
std::vector< subdomain_id_type > getSubdomainIDs(const libMesh::MeshBase &mesh, const std::vector< SubdomainName > &subdomain_name)
Get the associated subdomainIDs for the subdomain names that are passed in.
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.
const BoundaryID INVALID_BOUNDARY_ID
Definition MooseTypes.C:22
const unsigned int invalid_uint