www.mooseframework.org
MeshSideSetGenerator.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 "MeshSideSetGenerator.h"
11 #include "BndElement.h"
12 #include "CastUniquePointer.h"
13 
14 #include "libmesh/mesh_modification.h"
15 #include "libmesh/distributed_mesh.h"
16 #include "libmesh/elem.h"
17 
18 #include <typeinfo>
19 
21 
22 template <>
25 {
27 
28  params.addRequiredParam<MeshGeneratorName>("input", "The mesh we want to modify");
29  params.addClassDescription("Add lower dimensional elements along the faces contained in a side "
30  "set to set up mixed dimensional problems");
31  params.addRequiredParam<std::vector<BoundaryName>>("boundaries",
32  "The name of the boundary to mesh");
33  params.addRequiredParam<subdomain_id_type>(
34  "block_id", "Subdomain id to set for the new elements along the boundary");
35  params.addParam<SubdomainName>(
36  "block_name", "Subdomain name to set for the new elements along the boundary (optional)");
37 
38  return params;
39 }
40 
42  : MeshGenerator(parameters),
43  _input(getMesh("input")),
44  _block_id(parameters.get<subdomain_id_type>("block_id"))
45 {
46  if (typeid(_input).name() == typeid(std::unique_ptr<DistributedMesh>).name())
47  mooseError("MeshSideSetGenerator only works with ReplicatedMesh");
48 }
49 
50 std::unique_ptr<MeshBase>
52 {
53  std::unique_ptr<MeshBase> mesh = std::move(_input);
54 
55  auto & boundary_info = mesh->get_boundary_info();
56 
57  // get IDs of all boundaries with which a new block is created
58  std::set<boundary_id_type> mesh_boundary_ids;
59  if (isParamValid("boundaries"))
60  {
61  auto & boundary_names = getParam<std::vector<BoundaryName>>("boundaries");
62  for (auto & boundary_name : boundary_names)
63  mesh_boundary_ids.insert(boundary_info.get_id_by_name(boundary_name));
64  }
65  else
66  mesh_boundary_ids = boundary_info.get_boundary_ids();
67 
68  // Equivalent to MooseMesh::buildBndElemList()
69  auto bc_tuples = boundary_info.build_active_side_list();
70  int n = bc_tuples.size();
71  std::vector<std::unique_ptr<BndElement>> bnd_elems;
72  std::map<boundary_id_type, std::set<dof_id_type>> bnd_elem_ids;
73  bnd_elems.reserve(n);
74  for (const auto & t : bc_tuples)
75  {
76  auto elem_id = std::get<0>(t);
77  auto side_id = std::get<1>(t);
78  auto bc_id = std::get<2>(t);
79 
80  std::unique_ptr<BndElement> bndElem =
81  libmesh_make_unique<BndElement>(mesh->elem_ptr(elem_id), side_id, bc_id);
82  bnd_elems.push_back(std::move(bndElem));
83  bnd_elem_ids[bc_id].insert(elem_id);
84  }
85 
86  for (auto it = bnd_elems.begin(); it != bnd_elems.end(); ++it)
87  if (mesh_boundary_ids.count((*it)->_bnd_id) > 0)
88  {
89  Elem * elem = (*it)->_elem;
90  auto s = (*it)->_side;
91 
92  // build element from the side
93  std::unique_ptr<Elem> side(elem->build_side_ptr(s, false));
94  side->processor_id() = elem->processor_id();
95 
96  // Add the side set subdomain
97  Elem * new_elem = mesh->add_elem(side.release());
98  new_elem->subdomain_id() = _block_id;
99  }
100 
101  // Assign block name, if provided
102  if (isParamValid("block_name"))
103  mesh->subdomain_name(_block_id) = getParam<SubdomainName>("block_name");
104 
105  return dynamic_pointer_cast<MeshBase>(mesh);
106 }
MeshSideSetGenerator(const InputParameters &parameters)
Add lower dimensional elements along the faces contained in a side set.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
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.
registerMooseObject("MooseApp", MeshSideSetGenerator)
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
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 subdomain_id_type _block_id
Block ID to assign to the region.
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
PetscInt n
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:59
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...
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
std::unique_ptr< MeshBase > & _input
InputParameters validParams< MeshSideSetGenerator >()