www.mooseframework.org
MeshExtruder.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "MeshExtruder.h"
16 #include "MooseMesh.h"
17 
18 #include "libmesh/mesh_generation.h"
19 #include "libmesh/mesh.h"
20 #include "libmesh/elem.h"
21 #include "libmesh/boundary_info.h"
22 
23 template <>
26 {
28  params.addClassDescription("Takes a 1D or 2D mesh and extrudes the entire structure along the "
29  "specified axis increasing the dimensionality of the mesh.");
30  params.addRequiredParam<RealVectorValue>("extrusion_vector",
31  "The direction and length of the extrusion");
32  params.addParam<unsigned int>("num_layers", 1, "The number of layers in the extruded mesh");
33  params.addParam<std::vector<BoundaryName>>(
34  "bottom_sideset", "The boundary that will be applied to the bottom of the extruded mesh");
35  params.addParam<std::vector<BoundaryName>>(
36  "top_sideset", "The boundary that will be to the top of the extruded mesh");
37 
38  params.addParam<std::vector<SubdomainID>>(
39  "existing_subdomains",
40  std::vector<SubdomainID>(),
41  "The subdomains that will be remapped for specific layers");
42  params.addParam<std::vector<unsigned int>>(
43  "layers",
44  std::vector<unsigned int>(),
45  "The layers where the \"existing_subdomain\" will be remapped to new ids");
46  params.addParam<std::vector<unsigned int>>(
47  "new_ids",
48  std::vector<unsigned int>(),
49  "The list of new ids, This list should be either length \"existing_subdomains\" or "
50  "\"existing_subdomains\" * layers");
51  return params;
52 }
53 
55  : MeshModifier(parameters),
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 void
71 {
72  // When we clone, we're responsible to clean up after ourselves!
73  // TODO: traditionally clone() methods return pointers...
74  MooseMesh * source_mesh = &(_mesh_ptr->clone());
75 
76  if (source_mesh->getMesh().mesh_dimension() == 3)
77  mooseError("You cannot extrude a 3D mesh!");
78 
79  _mesh_ptr->getMesh().clear();
80 
81  std::unique_ptr<QueryElemSubdomainID> elem_subdomain_id;
82  if (_existing_subdomains.size() > 0)
83  elem_subdomain_id = libmesh_make_unique<QueryElemSubdomainID>(
85 
86  // The first argument to build_extrusion() is required to be UnstructuredMesh&, a common
87  // base class of both ReplicatedMesh and DistributedMesh, hence the dynamic_cast...
88  MeshTools::Generation::build_extrusion(
89  dynamic_cast<libMesh::UnstructuredMesh &>(_mesh_ptr->getMesh()),
90  source_mesh->getMesh(),
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 =
97  _mesh_ptr->getMesh().get_boundary_info().get_side_boundary_ids();
98 
99  // Handle distributed meshes: processors may not know all side ids
100  _communicator.set_union(side_ids);
101 
102  std::set<boundary_id_type>::reverse_iterator last_side_it = side_ids.rbegin();
103 
104  const boundary_id_type old_top = *last_side_it;
105  mooseAssert(last_side_it != side_ids.rend(), "Error in generating sidesets for extruded mesh");
106  const boundary_id_type old_bottom = *++last_side_it;
107 
108  // Update the IDs
109  if (isParamValid("bottom_sideset"))
110  changeID(getParam<std::vector<BoundaryName>>("bottom_sideset"), old_bottom);
111  if (isParamValid("top_sideset"))
112  changeID(getParam<std::vector<BoundaryName>>("top_sideset"), old_top);
113 
114  // Update the dimension
115  _mesh_ptr->getMesh().set_mesh_dimension(source_mesh->getMesh().mesh_dimension() + 1);
116 
117  // Clean up the source mesh we allocated
118  delete source_mesh;
119 }
120 
121 void
122 MeshExtruder::changeID(const std::vector<BoundaryName> & names, BoundaryID old_id)
123 {
124  std::vector<BoundaryID> boundary_ids = _mesh_ptr->getBoundaryIDs(names, true);
125 
126  if (std::find(boundary_ids.begin(), boundary_ids.end(), old_id) == boundary_ids.end())
127  _mesh_ptr->changeBoundaryId(old_id, boundary_ids[0], true);
128 
129  for (unsigned int i = 0; i < boundary_ids.size(); ++i)
130  {
131  _mesh_ptr->getMesh().get_boundary_info().sideset_name(boundary_ids[i]) = names[i];
132  _mesh_ptr->getMesh().get_boundary_info().nodeset_name(boundary_ids[i]) = names[i];
133  }
134 }
135 
137  std::vector<SubdomainID> existing_subdomains,
138  std::vector<unsigned int> layers,
139  std::vector<unsigned int> new_ids,
140  unsigned int libmesh_dbg_var(num_layers))
141  : QueryElemSubdomainIDBase()
142 #ifndef NDEBUG
143  ,
144  _num_layers(num_layers)
145 #endif
146 {
147  // Setup our stride depending on whether the user passed unique sets in new ids or just a single
148  // set of new ids
149  const unsigned int zero = 0;
150  const unsigned int stride =
151  existing_subdomains.size() == new_ids.size() ? zero : existing_subdomains.size();
152 
153  // Populate the data structure
154  for (unsigned int i = 0; i < layers.size(); ++i)
155  for (unsigned int j = 0; j < existing_subdomains.size(); ++j)
156  _layer_data[layers[i]][existing_subdomains[j]] = new_ids[i * stride + j];
157 }
158 
159 subdomain_id_type
161  unsigned int layer)
162 {
163  mooseAssert(layer < _num_layers, "Access out of bounds: " << layer);
164 
165  // First locate the layer if it exists
166  std::map<unsigned int, std::map<SubdomainID, unsigned int>>::const_iterator layer_it =
167  _layer_data.find(layer);
168 
169  if (layer_it == _layer_data.end())
170  // If the layer wasn't found, there is no mapping so just return the original subdomain id
171  return old_elem->subdomain_id();
172  else
173  {
174  std::map<SubdomainID, unsigned int>::const_iterator sub_id_it =
175  layer_it->second.find(old_elem->subdomain_id());
176 
177  if (sub_id_it == layer_it->second.end())
178  // If the subdomain wasn't found, it won't be remapped, so just return the original subdomain
179  // id
180  return old_elem->subdomain_id();
181 
182  // Return the remapped id
183  return sub_id_it->second;
184  }
185 }
virtual MooseMesh & clone() const =0
Clone method.
VectorValue< Real > RealVectorValue
Definition: Assembly.h:40
subdomain_id_type SubdomainID
Definition: MooseTypes.h:77
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:1990
MeshModifiers are objects that can modify or add to an existing mesh.
Definition: MeshModifier.h:31
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 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:1847
void changeID(const std::vector< BoundaryName > &names, BoundaryID old_id)
Definition: MeshExtruder.C:122
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:74
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:57
std::vector< SubdomainID > _existing_subdomains
Definition: MeshExtruder.h:36
const RealVectorValue _extrusion_vector
Definition: MeshExtruder.h:34
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:2408
std::vector< unsigned int > _layers
Definition: MeshExtruder.h:37
unsigned int _num_layers
The total number of layers in the extrusion.
Definition: MeshExtruder.h:62
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:74
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:67
InputParameters validParams< MeshExtruder >()
Definition: MeshExtruder.C:25
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseObject.h:122
virtual void modify() override
Pure virtual modify function MUST be overridden by children classes.
Definition: MeshExtruder.C:70
std::vector< unsigned int > _new_ids
Definition: MeshExtruder.h:38
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...
void mooseError(Args &&...args) const
Definition: MooseObject.h:80
QueryElemSubdomainID(std::vector< SubdomainID > existing_subdomains, std::vector< unsigned int > layers, std::vector< unsigned int > new_ids, unsigned int num_layers)
Definition: MeshExtruder.C:136
unsigned int _num_layers
Definition: MeshExtruder.h:35
InputParameters validParams< MeshModifier >()
Definition: MeshModifier.C:20
MeshExtruder(const InputParameters &parameters)
Definition: MeshExtruder.C:54
boundary_id_type BoundaryID
Definition: MooseTypes.h:75