www.mooseframework.org
MeshExtruder.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 "MeshExtruder.h"
11 #include "MooseMesh.h"
12 
13 #include "libmesh/mesh_generation.h"
14 #include "libmesh/mesh.h"
15 #include "libmesh/elem.h"
16 #include "libmesh/boundary_info.h"
17 
19 
20 template <>
23 {
25  params.addClassDescription("Takes a 1D or 2D mesh and extrudes the entire structure along the "
26  "specified axis increasing the dimensionality of the mesh.");
27  params.addRequiredParam<RealVectorValue>("extrusion_vector",
28  "The direction and length of the extrusion");
29  params.addParam<unsigned int>("num_layers", 1, "The number of layers in the extruded mesh");
30  params.addParam<std::vector<BoundaryName>>(
31  "bottom_sideset", "The boundary that will be applied to the bottom of the extruded mesh");
32  params.addParam<std::vector<BoundaryName>>(
33  "top_sideset", "The boundary that will be to the top of the extruded mesh");
34 
35  params.addParam<std::vector<SubdomainID>>(
36  "existing_subdomains",
37  std::vector<SubdomainID>(),
38  "The subdomains that will be remapped for specific layers");
39  params.addParam<std::vector<unsigned int>>(
40  "layers",
41  std::vector<unsigned int>(),
42  "The layers where the \"existing_subdomain\" will be remapped to new ids");
43  params.addParam<std::vector<unsigned int>>(
44  "new_ids",
45  std::vector<unsigned int>(),
46  "The list of new ids, This list should be either length \"existing_subdomains\" or "
47  "\"existing_subdomains\" * layers");
48  return params;
49 }
50 
52  : MeshModifier(parameters),
53  _extrusion_vector(getParam<RealVectorValue>("extrusion_vector")),
54  _num_layers(getParam<unsigned int>("num_layers")),
55  _existing_subdomains(getParam<std::vector<SubdomainID>>("existing_subdomains")),
56  _layers(getParam<std::vector<unsigned int>>("layers")),
57  _new_ids(getParam<std::vector<unsigned int>>("new_ids"))
58 {
59  // Check the length of the vectors
60  if (_existing_subdomains.size() != _new_ids.size() &&
61  _existing_subdomains.size() * _layers.size() != _new_ids.size())
62  mooseError(
63  "The length of the \"existing_subdomains\", \"layers\", and \"new_ids\" are not valid");
64 }
65 
66 void
68 {
69  std::unique_ptr<MooseMesh> source_mesh = _mesh_ptr->safeClone();
70 
71  if (source_mesh->getMesh().mesh_dimension() == 3)
72  mooseError("You cannot extrude a 3D mesh!");
73 
74  _mesh_ptr->getMesh().clear();
75 
76  std::unique_ptr<QueryElemSubdomainID> elem_subdomain_id;
77  if (_existing_subdomains.size() > 0)
78  elem_subdomain_id = libmesh_make_unique<QueryElemSubdomainID>(
80 
81  // The first argument to build_extrusion() is required to be UnstructuredMesh&, a common
82  // base class of both ReplicatedMesh and DistributedMesh, hence the dynamic_cast...
83  MeshTools::Generation::build_extrusion(
84  dynamic_cast<libMesh::UnstructuredMesh &>(_mesh_ptr->getMesh()),
85  source_mesh->getMesh(),
88  elem_subdomain_id.get());
89 
90  // See if the user has requested specific sides for the top and bottom
91  std::set<boundary_id_type> side_ids =
92  _mesh_ptr->getMesh().get_boundary_info().get_side_boundary_ids();
93 
94  // Handle distributed meshes: processors may not know all side ids
95  _communicator.set_union(side_ids);
96 
97  std::set<boundary_id_type>::reverse_iterator last_side_it = side_ids.rbegin();
98 
99  const boundary_id_type old_top = *last_side_it;
100  mooseAssert(last_side_it != side_ids.rend(), "Error in generating sidesets for extruded mesh");
101  const boundary_id_type old_bottom = *++last_side_it;
102 
103  // Update the IDs
104  if (isParamValid("bottom_sideset"))
105  changeID(getParam<std::vector<BoundaryName>>("bottom_sideset"), old_bottom);
106  if (isParamValid("top_sideset"))
107  changeID(getParam<std::vector<BoundaryName>>("top_sideset"), old_top);
108 
109  // Update the dimension
110  _mesh_ptr->getMesh().set_mesh_dimension(source_mesh->getMesh().mesh_dimension() + 1);
111 
112  // Redetect the mesh extents
113  _mesh_ptr->prepared(false);
114 }
115 
116 void
117 MeshExtruder::changeID(const std::vector<BoundaryName> & names, BoundaryID old_id)
118 {
119  std::vector<BoundaryID> boundary_ids = _mesh_ptr->getBoundaryIDs(names, true);
120 
121  if (std::find(boundary_ids.begin(), boundary_ids.end(), old_id) == boundary_ids.end())
122  _mesh_ptr->changeBoundaryId(old_id, boundary_ids[0], true);
123 
124  for (unsigned int i = 0; i < boundary_ids.size(); ++i)
125  {
126  _mesh_ptr->getMesh().get_boundary_info().sideset_name(boundary_ids[i]) = names[i];
127  _mesh_ptr->getMesh().get_boundary_info().nodeset_name(boundary_ids[i]) = names[i];
128  }
129 }
130 
132  std::vector<SubdomainID> existing_subdomains,
133  std::vector<unsigned int> layers,
134  std::vector<unsigned int> new_ids,
135  unsigned int libmesh_dbg_var(num_layers))
136  : QueryElemSubdomainIDBase()
137 #ifndef NDEBUG
138  ,
139  _num_layers(num_layers)
140 #endif
141 {
142  // Setup our stride depending on whether the user passed unique sets in new ids or just a single
143  // set of new ids
144  const unsigned int zero = 0;
145  const unsigned int stride =
146  existing_subdomains.size() == new_ids.size() ? zero : existing_subdomains.size();
147 
148  // Populate the data structure
149  for (unsigned int i = 0; i < layers.size(); ++i)
150  for (unsigned int j = 0; j < existing_subdomains.size(); ++j)
151  _layer_data[layers[i]][existing_subdomains[j]] = new_ids[i * stride + j];
152 }
153 
154 subdomain_id_type
156  unsigned int layer)
157 {
158  mooseAssert(layer < _num_layers, "Access out of bounds: " << layer);
159 
160  // First locate the layer if it exists
161  std::map<unsigned int, std::map<SubdomainID, unsigned int>>::const_iterator layer_it =
162  _layer_data.find(layer);
163 
164  if (layer_it == _layer_data.end())
165  // If the layer wasn't found, there is no mapping so just return the original subdomain id
166  return old_elem->subdomain_id();
167  else
168  {
169  std::map<SubdomainID, unsigned int>::const_iterator sub_id_it =
170  layer_it->second.find(old_elem->subdomain_id());
171 
172  if (sub_id_it == layer_it->second.end())
173  // If the subdomain wasn't found, it won't be remapped, so just return the original subdomain
174  // id
175  return old_elem->subdomain_id();
176 
177  // Return the remapped id
178  return sub_id_it->second;
179  }
180 }
bool prepared() const
Setter/getter for the _is_prepared flag.
Definition: MooseMesh.C:2291
VectorValue< Real > RealVectorValue
Definition: Assembly.h:31
MeshModifiers are objects that can modify or add to an existing mesh.
Definition: MeshModifier.h:25
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...
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
void changeBoundaryId(const boundary_id_type old_id, const boundary_id_type new_id, bool delete_prev)
Change all the boundary IDs for a given side from old_id to new_id.
Definition: MooseMesh.C:1911
void changeID(const std::vector< BoundaryName > &names, BoundaryID old_id)
Definition: MeshExtruder.C:117
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseObject.h:191
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...
MooseMesh * _mesh_ptr
Pointer to the mesh.
Definition: MeshModifier.h:68
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.
Definition: MeshExtruder.h:51
std::vector< SubdomainID > _existing_subdomains
Definition: MeshExtruder.h:30
const RealVectorValue _extrusion_vector
Definition: MeshExtruder.h:28
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:2567
std::vector< unsigned int > _layers
Definition: MeshExtruder.h:31
boundary_id_type BoundaryID
subdomain_id_type SubdomainID
InputParameters validParams< MeshExtruder >()
Definition: MeshExtruder.C:22
registerMooseObject("MooseApp", MeshExtruder)
virtual void modify() override
Pure virtual modify function MUST be overridden by children classes.
Definition: MeshExtruder.C:67
virtual std::unique_ptr< MooseMesh > safeClone() const =0
A safer version of the clone() method that hands back an allocated object wrapped in a smart pointer...
std::vector< unsigned int > _new_ids
Definition: MeshExtruder.h:32
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...
QueryElemSubdomainID(std::vector< SubdomainID > existing_subdomains, std::vector< unsigned int > layers, std::vector< unsigned int > new_ids, unsigned int num_layers)
Definition: MeshExtruder.C:131
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:89
unsigned int _num_layers
Definition: MeshExtruder.h:29
InputParameters validParams< MeshModifier >()
Definition: MeshModifier.C:15
MeshExtruder(const InputParameters &parameters)
Definition: MeshExtruder.C:51