www.mooseframework.org
MeshExtruderGenerator.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 "MeshExtruderGenerator.h"
11 #include "MooseMeshUtils.h"
12 #include "CastUniquePointer.h"
13 
14 #include "libmesh/replicated_mesh.h"
15 #include "libmesh/boundary_info.h"
16 #include "libmesh/elem.h"
17 
19 
22 {
24 
25  params.addRequiredParam<MeshGeneratorName>("input", "the mesh we want to extrude");
26  params.addClassDescription("Takes a 1D or 2D mesh and extrudes the entire structure along the "
27  "specified axis increasing the dimensionality of the mesh.");
28  params.addRequiredParam<RealVectorValue>("extrusion_vector",
29  "The direction and length of the extrusion");
30  params.addParam<unsigned int>("num_layers", 1, "The number of layers in the extruded mesh");
31  params.addParam<std::vector<BoundaryName>>(
32  "bottom_sideset", "The boundary that will be applied to the bottom of the extruded mesh");
33  params.addParam<std::vector<BoundaryName>>(
34  "top_sideset", "The boundary that will be to the top of the extruded mesh");
35 
36  params.addParam<std::vector<SubdomainID>>(
37  "existing_subdomains",
38  std::vector<SubdomainID>(),
39  "The subdomains that will be remapped for specific layers");
40  params.addParam<std::vector<unsigned int>>(
41  "layers",
42  std::vector<unsigned int>(),
43  "The layers where the \"existing_subdomain\" will be remapped to new ids");
44  params.addParam<std::vector<unsigned int>>(
45  "new_ids",
46  std::vector<unsigned int>(),
47  "The list of new ids, This list should be either length \"existing_subdomains\" or "
48  "\"existing_subdomains\" * layers");
49 
50  return params;
51 }
52 
54  : MeshGenerator(parameters),
55  _input(getMesh("input")),
56  _extrusion_vector(getParam<RealVectorValue>("extrusion_vector")),
57  _num_layers(getParam<unsigned int>("num_layers")),
58  _existing_subdomains(getParam<std::vector<SubdomainID>>("existing_subdomains")),
59  _layers(getParam<std::vector<unsigned int>>("layers")),
60  _new_ids(getParam<std::vector<unsigned int>>("new_ids"))
61 {
62  // Check the length of the vectors
63  if (_existing_subdomains.size() != _new_ids.size() &&
64  _existing_subdomains.size() * _layers.size() != _new_ids.size())
65  mooseError(
66  "The length of the \"existing_subdomains\", \"layers\", and \"new_ids\" are not valid");
67 }
68 
69 std::unique_ptr<MeshBase>
71 {
72  std::unique_ptr<MeshBase> source_mesh = std::move(_input);
73 
74  auto dest_mesh = buildMeshBaseObject();
75 
76  // check that the existing_subdomains exist in the mesh
77  for (const auto & id : _existing_subdomains)
78  if (!MooseMeshUtils::hasSubdomainID(*source_mesh, id))
79  paramError("existing_subdomains", "The block ID '", id, "' was not found in the mesh");
80 
81  if (source_mesh->mesh_dimension() == 3)
82  mooseError("You cannot extrude a 3D mesh !");
83 
84  std::unique_ptr<QueryElemSubdomainID> elem_subdomain_id;
85  if (_existing_subdomains.size() > 0)
86  elem_subdomain_id = std::make_unique<QueryElemSubdomainID>(
88 
89  MeshTools::Generation::build_extrusion(dynamic_cast<libMesh::UnstructuredMesh &>(*dest_mesh),
90  *source_mesh,
93  elem_subdomain_id.get());
94 
95  // See if the user has requested specific sides for the top and bottom
96  std::set<boundary_id_type> side_ids = dest_mesh->get_boundary_info().get_side_boundary_ids();
97 
98  // Handle distributed meshes: processors may not know all side ids
99  _communicator.set_union(side_ids);
100 
101  std::set<boundary_id_type>::reverse_iterator last_side_it = side_ids.rbegin();
102 
103  const boundary_id_type old_top = *last_side_it;
104  mooseAssert(last_side_it != side_ids.rend(), "Error in generating sidesets for extruded mesh");
105  const boundary_id_type old_bottom = *++last_side_it;
106 
107  // Update the IDs
108  if (isParamValid("bottom_sideset"))
109  changeID(*dest_mesh, getParam<std::vector<BoundaryName>>("bottom_sideset"), old_bottom);
110  if (isParamValid("top_sideset"))
111  changeID(*dest_mesh, getParam<std::vector<BoundaryName>>("top_sideset"), old_top);
112 
113  dest_mesh->set_isnt_prepared();
114  return dynamic_pointer_cast<MeshBase>(dest_mesh);
115 }
116 
117 void
119  const std::vector<BoundaryName> & names,
120  BoundaryID old_id)
121 {
122  std::vector<boundary_id_type> boundary_ids = MooseMeshUtils::getBoundaryIDs(mesh, names, true);
123 
124  if (std::find(boundary_ids.begin(), boundary_ids.end(), old_id) == boundary_ids.end())
125  MooseMeshUtils::changeBoundaryId(mesh, old_id, boundary_ids[0], true);
126 
127  for (unsigned int i = 0; i < boundary_ids.size(); ++i)
128  {
129  mesh.get_boundary_info().sideset_name(boundary_ids[i]) = names[i];
130  mesh.get_boundary_info().nodeset_name(boundary_ids[i]) = names[i];
131  }
132 }
133 
135  const std::vector<SubdomainID> & existing_subdomains,
136  std::vector<unsigned int> layers,
137  const std::vector<unsigned int> & new_ids,
138  unsigned int num_layers)
139  : QueryElemSubdomainIDBase(), _num_layers(num_layers)
140 {
141  // Setup our stride depending on whether the user passed unique sets in new ids or just a single
142  // set of new ids
143  const unsigned int stride =
144  existing_subdomains.size() == new_ids.size() ? 0 : existing_subdomains.size();
145 
146  if (layers.size() == 0)
147  for (unsigned int i = 0; i < _num_layers; i++)
148  layers.push_back(i);
149 
150  // Populate the data structure
151  for (unsigned int i = 0; i < layers.size(); ++i)
152  for (unsigned int j = 0; j < existing_subdomains.size(); ++j)
153  _layer_data[layers[i]][existing_subdomains[j]] = new_ids[i * stride + j];
154 }
155 
158  unsigned int layer)
159 {
160  mooseAssert(layer < _num_layers, "Access out of bounds: " << layer);
161 
162  // First locate the layer if it exists
163  std::map<unsigned int, std::map<SubdomainID, unsigned int>>::const_iterator layer_it =
164  _layer_data.find(layer);
165 
166  if (layer_it == _layer_data.end())
167  // If the layer wasn't found, there is no mapping so just return the original subdomain id
168  return old_elem->subdomain_id();
169  else
170  {
171  std::map<SubdomainID, unsigned int>::const_iterator sub_id_it =
172  layer_it->second.find(old_elem->subdomain_id());
173 
174  if (sub_id_it == layer_it->second.end())
175  // If the subdomain wasn't found, it won't be remapped, so just return the original subdomain
176  // id
177  return old_elem->subdomain_id();
178 
179  // Return the remapped id
180  return sub_id_it->second;
181  }
182 }
Extrude a 1D or 2D mesh to a 2D or 3D mesh respectively.
QueryElemSubdomainID(const std::vector< SubdomainID > &existing_subdomains, std::vector< unsigned int > layers, const std::vector< unsigned int > &new_ids, unsigned int num_layers)
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
MeshBase & mesh
virtual subdomain_id_type get_subdomain_for_layer(const Elem *old_elem, unsigned int layer)
The override from the base class for obtaining a new id based on the old (original) element and the s...
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.
const Parallel::Communicator & _communicator
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...
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
registerMooseObject("MooseApp", MeshExtruderGenerator)
int8_t boundary_id_type
static InputParameters validParams()
boundary_id_type BoundaryID
void changeID(MeshBase &mesh, const std::vector< BoundaryName > &names, BoundaryID old_id)
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
std::vector< unsigned int > _layers
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.
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 ...
static InputParameters validParams()
Definition: MeshGenerator.C:23
std::unique_ptr< MeshBase > & _input
Mesh that comes from another generator.
std::map< unsigned int, std::map< SubdomainID, unsigned int > > _layer_data
Data structure for holding the old -> new id mapping based on the layer number.
const RealVectorValue _extrusion_vector
std::vector< SubdomainID > _existing_subdomains
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
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...
std::unique_ptr< MeshBase > buildMeshBaseObject(unsigned int dim=libMesh::invalid_uint)
Build a MeshBase object whose underlying type will be determined by the Mesh input file block...
void changeBoundaryId(MeshBase &mesh, const libMesh::boundary_id_type old_id, const libMesh::boundary_id_type new_id, bool delete_prev)
Changes the old boundary ID to a new ID in the mesh.
MeshExtruderGenerator(const InputParameters &parameters)
MeshGenerators are objects that can modify or add to an existing mesh.
Definition: MeshGenerator.h:32
void ErrorVector unsigned int
bool hasSubdomainID(MeshBase &input_mesh, const SubdomainID &id)
Whether a particular subdomain ID exists in the mesh.
std::vector< unsigned int > _new_ids
unsigned int _num_layers
The total number of layers in the extrusion.
void set_union(T &data, const unsigned int root_id) const