https://mooseframework.inl.gov
Loading...
Searching...
No Matches
BreakBoundaryOnSubdomainGenerator.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 "CastUniquePointer.h"
12
13#include "MooseUtils.h"
14#include "MooseMeshUtils.h"
15
16#include "libmesh/elem.h"
17
19
22{
24
25 params.addRequiredParam<MeshGeneratorName>("input", "The mesh we want to modify");
26 params.addClassDescription("Break boundaries based on the subdomains to which their sides are "
27 "attached. Naming convention for the new boundaries will be the old "
28 "boundary name plus \"_to_\" plus the subdomain name");
29 params.addParam<std::vector<BoundaryName>>(
30 "boundaries", "Boundaries to be broken. Default means to break all boundaries");
31
32 return params;
33}
34
36 const InputParameters & parameters)
37 : MeshGenerator(parameters), _input(getMesh("input"))
38{
39}
40
41std::unique_ptr<MeshBase>
43{
44 // get the mesh and boundary info
45 std::unique_ptr<MeshBase> mesh = std::move(_input);
46 auto & boundary_info = mesh->get_boundary_info();
47
48 // get IDs of all boundaries to be broken
49 std::set<boundary_id_type> breaking_boundary_ids;
50 if (isParamValid("boundaries"))
51 {
52 auto & boundary_names = getParam<std::vector<BoundaryName>>("boundaries");
53 for (auto & boundary_name : boundary_names)
54 {
55 // check that the boundary exists in the mesh
56 if (!MooseMeshUtils::hasBoundaryNameOrID(*mesh, boundary_name))
57 paramError("boundaries", "The boundary '", boundary_name, "' was not found in the mesh");
58
59 breaking_boundary_ids.insert(boundary_info.get_id_by_name(boundary_name));
60 }
61 }
62 else
63 {
64 breaking_boundary_ids = boundary_info.get_boundary_ids();
65
66 // We might be on a distributed mesh with remote boundary ids
67 if (!mesh->is_replicated())
68 this->comm().set_union(breaking_boundary_ids);
69 }
70
71 // create a list of new boundary names
72 std::set<std::string> new_boundary_name_set;
73 std::vector<boundary_id_type> side_boundary_ids;
74 for (const auto & elem : mesh->active_element_ptr_range())
75 {
76 auto subdomain_id = elem->subdomain_id();
77 // subdomain_name now invalidates the mesh state, so we want to
78 // be sure we're not calling the non-const version
79 auto subdomain_name = std::as_const(mesh)->subdomain_name(subdomain_id);
80 if (subdomain_name == "")
81 subdomain_name = std::to_string(subdomain_id);
82 for (unsigned int side = 0; side < elem->n_sides(); ++side)
83 {
84 boundary_info.boundary_ids(elem, side, side_boundary_ids);
85 for (auto boundary_id : side_boundary_ids)
86 if (breaking_boundary_ids.count(boundary_id) > 0)
87 new_boundary_name_set.emplace(boundary_info.get_sideset_name(boundary_id) + "_to_" +
88 subdomain_name);
89 }
90 }
91
92 // We might be on a distributed mesh with remote elements that had
93 // new boundary ids added
94 if (!mesh->is_replicated())
95 this->comm().set_union(new_boundary_name_set);
96
97 // assign boundary IDs to the boundaries to be added
98 std::vector<BoundaryName> new_boundary_names(new_boundary_name_set.begin(),
99 new_boundary_name_set.end());
100 auto new_boundary_ids = MooseMeshUtils::getBoundaryIDs(*mesh, new_boundary_names, true);
101
102 // assign boundary names to the new boundaries
103 mooseAssert(new_boundary_ids.size() == new_boundary_names.size(),
104 "sizes of boundary names and boundary IDs mismatch");
105 for (MooseIndex(new_boundary_ids) i = 0; i < new_boundary_ids.size(); ++i)
106 {
107 boundary_info.sideset_name(new_boundary_ids[i]) = new_boundary_names[i];
108 boundary_info.nodeset_name(new_boundary_ids[i]) = new_boundary_names[i];
109 }
110
111 // add sides into the side sets
112 for (const auto & elem : mesh->active_element_ptr_range())
113 {
114 auto subdomain_id = elem->subdomain_id();
115 auto subdomain_name = std::as_const(mesh)->subdomain_name(subdomain_id);
116 if (subdomain_name == "")
117 subdomain_name = std::to_string(subdomain_id);
118 for (MooseIndex(elem->n_sides()) side = 0; side < elem->n_sides(); ++side)
119 {
120 std::vector<boundary_id_type> side_boundary_ids;
121 boundary_info.boundary_ids(elem, side, side_boundary_ids);
122 for (auto boundary_id : side_boundary_ids)
123 {
124 if (breaking_boundary_ids.count(boundary_id) > 0)
125 {
126 BoundaryName bname =
127 boundary_info.get_sideset_name(boundary_id) + "_to_" + subdomain_name;
128 auto bid = boundary_info.get_id_by_name(bname);
129 boundary_info.add_side(elem, side, bid);
130 }
131 }
132 }
133 }
134
135 mesh->unset_is_prepared();
136
137 return dynamic_pointer_cast<MeshBase>(mesh);
138}
registerMooseObject("MooseApp", BreakBoundaryOnSubdomainGenerator)
MeshGenerator for breaking all boundaries based on which block they are attached to.
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
std::unique_ptr< MeshBase > & _input
the input mesh to be modified
BreakBoundaryOnSubdomainGenerator(const InputParameters &parameters)
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
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...
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.
MeshGenerators are objects that can modify or add to an existing mesh.
static InputParameters validParams()
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
void set_union(T &data, const unsigned int root_id) const
const Parallel::Communicator & comm() const
MeshBase & mesh
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.
bool hasBoundaryNameOrID(const MeshBase &mesh, const BoundaryName &name_or_id)
Whether a particular boundary name or ID exists in the mesh.