www.mooseframework.org
BreakBoundaryOnSubdomain.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 
11 
12 #include "MooseMesh.h"
13 #include "MooseUtils.h"
14 
15 #include "libmesh/elem.h"
16 
18 
19 template <>
22 {
24  params.addClassDescription("Break boundaries based on the subdomains to which their sides are "
25  "attached. Naming convention for the new boundaries will be the old "
26  "boundary name plus \"_to_\" plus the subdomain name");
27  params.addParam<std::vector<BoundaryName>>(
28  "boundaries", "Boundaries to be broken. Default means to break all boundaries");
29  return params;
30 }
31 
33  : MeshModifier(parameters)
34 {
35 }
36 
37 void
39 {
40  // get the mesh and boundary info
41  auto & mesh = _mesh_ptr->getMesh();
42  auto & boundary_info = mesh.get_boundary_info();
43 
44  // get IDs of all boundaries to be broken
45  std::set<BoundaryID> breaking_boundary_ids;
46  if (isParamValid("boundaries"))
47  {
48  auto & boundary_names = getParam<std::vector<BoundaryName>>("boundaries");
49  for (auto & boundary_name : boundary_names)
50  breaking_boundary_ids.insert(_mesh_ptr->getBoundaryID(boundary_name));
51  }
52  else
53  {
54  breaking_boundary_ids = boundary_info.get_boundary_ids();
55 
56  // We might be on a distributed mesh with remote boundary ids
57  if (!mesh.is_replicated())
58  this->comm().set_union(breaking_boundary_ids);
59  }
60 
61  // create a list of new boundary names
62  std::set<std::string> new_boundary_name_set;
63  std::vector<boundary_id_type> side_boundary_ids;
64  for (const auto & elem : mesh.active_element_ptr_range())
65  {
66  auto subdomain_id = elem->subdomain_id();
67  auto subdomain_name = mesh.subdomain_name(subdomain_id);
68  if (subdomain_name == "")
69  subdomain_name = std::to_string(subdomain_id);
70  for (unsigned int side = 0; side < elem->n_sides(); ++side)
71  {
72  boundary_info.boundary_ids(elem, side, side_boundary_ids);
73  for (auto boundary_id : side_boundary_ids)
74  if (breaking_boundary_ids.count(boundary_id) > 0)
75  new_boundary_name_set.emplace(boundary_info.sideset_name(boundary_id) + "_to_" +
76  subdomain_name);
77  }
78  }
79 
80  // We might be on a distributed mesh with remote elements that had
81  // new boundary ids added
82  if (!mesh.is_replicated())
83  this->comm().set_union(new_boundary_name_set);
84 
85  // assign boundary IDs to the boundaries to be added
86  std::vector<BoundaryName> new_boundary_names(new_boundary_name_set.begin(),
87  new_boundary_name_set.end());
88  auto new_boundary_ids = _mesh_ptr->getBoundaryIDs(new_boundary_names, true);
89 
90  // assign boundary names to the new boundaries
91  mooseAssert(new_boundary_ids.size() == new_boundary_names.size(),
92  "sizes of boundary names and boundary IDs mismatch");
93  for (MooseIndex(new_boundary_ids) i = 0; i < new_boundary_ids.size(); ++i)
94  {
95  boundary_info.sideset_name(new_boundary_ids[i]) = new_boundary_names[i];
96  boundary_info.nodeset_name(new_boundary_ids[i]) = new_boundary_names[i];
97  }
98 
99  // add sides into the side sets
100  for (const auto & elem : mesh.active_element_ptr_range())
101  {
102  auto subdomain_id = elem->subdomain_id();
103  auto subdomain_name = mesh.subdomain_name(subdomain_id);
104  if (subdomain_name == "")
105  subdomain_name = std::to_string(subdomain_id);
106  for (unsigned int side = 0; side < elem->n_sides(); ++side)
107  {
108  std::vector<boundary_id_type> side_boundary_ids;
109  boundary_info.boundary_ids(elem, side, side_boundary_ids);
110  for (auto boundary_id : side_boundary_ids)
111  {
112  if (breaking_boundary_ids.count(boundary_id) > 0)
113  {
114  BoundaryName bname = boundary_info.sideset_name(boundary_id) + "_to_" + subdomain_name;
115  auto bid = boundary_info.get_id_by_name(bname);
116  boundary_info.add_side(elem, side, bid);
117  }
118  }
119  }
120  }
121 }
InputParameters validParams< BreakBoundaryOnSubdomain >()
MeshModifiers are objects that can modify or add to an existing mesh.
Definition: MeshModifier.h:25
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
BreakBoundaryOnSubdomain(const InputParameters &parameters)
MooseMesh * _mesh_ptr
Pointer to the mesh.
Definition: MeshModifier.h:68
registerMooseObject("MooseApp", BreakBoundaryOnSubdomain)
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:2567
MPI_Comm comm
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...
std::vector< BoundaryID > getBoundaryIDs(const Elem *const elem, const unsigned short int side) const
Returns a vector of boundary IDs for the requested element on the requested side. ...
Definition: MooseMesh.C:2153
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...
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:89
InputParameters validParams< MeshModifier >()
Definition: MeshModifier.C:15
BoundaryID getBoundaryID(const BoundaryName &boundary_name) const
Get the associated BoundaryID for the boundary name.
Definition: MooseMesh.C:1007
virtual void modify()
Pure virtual modify function MUST be overridden by children classes.