www.mooseframework.org
ElementGenerator.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 "ElementGenerator.h"
11 #include "CastUniquePointer.h"
12 
13 #include "libmesh/replicated_mesh.h"
14 
15 #include "MooseEnum.h"
16 
17 #include "libmesh/edge_edge2.h"
18 #include "libmesh/edge_edge3.h"
19 #include "libmesh/edge_edge4.h"
20 #include "libmesh/face_quad4.h"
21 #include "libmesh/face_quad8.h"
22 #include "libmesh/face_quad9.h"
23 #include "libmesh/face_tri3.h"
24 #include "libmesh/face_tri6.h"
25 #include "libmesh/cell_hex8.h"
26 #include "libmesh/cell_hex20.h"
27 #include "libmesh/cell_hex27.h"
28 #include "libmesh/cell_tet4.h"
29 #include "libmesh/cell_tet10.h"
30 #include "libmesh/cell_prism6.h"
31 #include "libmesh/cell_prism15.h"
32 #include "libmesh/cell_prism18.h"
33 #include "libmesh/cell_pyramid5.h"
34 #include "libmesh/cell_pyramid13.h"
35 #include "libmesh/cell_pyramid14.h"
36 
38 
39 template <>
42 {
44 
45  MooseEnum elem_types("EDGE2 EDGE3 EDGE4 QUAD4 QUAD8 QUAD9 TRI3 TRI6 HEX8 HEX20 HEX27 TET4 TET10 "
46  "PRISM6 PRISM15 PRISM18 PYRAMID5 PYRAMID13 PYRAMID14");
47 
48  params.addParam<MeshGeneratorName>("input", "Optional input mesh to add the elements to");
49 
50  params.addRequiredParam<std::vector<Point>>("nodal_positions",
51  "The x,y,z positions of the nodes");
52 
53  params.addRequiredParam<std::vector<dof_id_type>>("element_connectivity",
54  "List of nodes to use for each element");
55 
56  params.addParam<MooseEnum>("elem_type",
57  elem_types,
58  "The type of element from libMesh to "
59  "generate");
60 
61  return params;
62 }
63 
65  : MeshGenerator(parameters),
66  _input(getMesh("input")),
67  _nodal_positions(getParam<std::vector<Point>>("nodal_positions")),
68  _element_connectivity(getParam<std::vector<dof_id_type>>("element_connectivity")),
69  _elem_type(getParam<MooseEnum>("elem_type"))
70 {
71 }
72 
73 Elem *
74 ElementGenerator::getElemType(const std::string & type)
75 {
76  if (type == "EDGE2")
77  {
78  Elem * elem = new Edge2;
79  return elem;
80  }
81  if (type == "EDGE3")
82  {
83  Elem * elem = new Edge3;
84  return elem;
85  }
86  if (type == "EDGE4")
87  {
88  Elem * elem = new Edge4;
89  return elem;
90  }
91  if (type == "QUAD4")
92  {
93  Elem * elem = new Quad4;
94  return elem;
95  }
96  if (type == "QUAD8")
97  {
98  Elem * elem = new Quad8;
99  return elem;
100  }
101  if (type == "QUAD9")
102  {
103  Elem * elem = new Quad9;
104  return elem;
105  }
106  if (type == "TRI3")
107  {
108  Elem * elem = new Tri3;
109  return elem;
110  }
111  if (type == "TRI6")
112  {
113  Elem * elem = new Tri6;
114  return elem;
115  }
116  if (type == "HEX8")
117  {
118  Elem * elem = new Hex8;
119  return elem;
120  }
121  if (type == "HEX20")
122  {
123  Elem * elem = new Hex20;
124  return elem;
125  }
126  if (type == "HEX27")
127  {
128  Elem * elem = new Hex27;
129  return elem;
130  }
131  if (type == "TET4")
132  {
133  Elem * elem = new Tet4;
134  return elem;
135  }
136  if (type == "TET10")
137  {
138  Elem * elem = new Tet10;
139  return elem;
140  }
141  if (type == "PRISM6")
142  {
143  Elem * elem = new Prism6;
144  return elem;
145  }
146  if (type == "PRISM15")
147  {
148  Elem * elem = new Prism15;
149  return elem;
150  }
151  if (type == "PRISM18")
152  {
153  Elem * elem = new Prism18;
154  return elem;
155  }
156  if (type == "PYRAMID5")
157  {
158  Elem * elem = new Pyramid5;
159  return elem;
160  }
161  if (type == "PYRAMID13")
162  {
163  Elem * elem = new Pyramid13;
164  return elem;
165  }
166  if (type == "PYRAMID14")
167  {
168  Elem * elem = new Pyramid14;
169  return elem;
170  }
171 
172  mooseError("This element type is not available.");
173 }
174 
175 std::unique_ptr<MeshBase>
177 {
178  std::unique_ptr<MeshBase> mesh = std::move(_input);
179 
180  // If there was no input mesh then let's just make a new one
181  if (!mesh)
182  mesh = _mesh->buildMeshBaseObject();
183 
184  MooseEnum elem_type_enum = getParam<MooseEnum>("elem_type");
185  auto elem = getElemType(elem_type_enum);
186 
187  mesh->set_mesh_dimension(std::max((unsigned int)elem->dim(), mesh->mesh_dimension()));
188 
189  std::vector<Node *> nodes;
190 
191  nodes.reserve(_nodal_positions.size());
192 
193  // Add all the nodes
194  for (auto & point : _nodal_positions)
195  nodes.push_back(mesh->add_point(point));
196 
197  mesh->add_elem(elem);
198 
199  auto n = elem->n_nodes();
200 
201  for (dof_id_type i = 0; i < _element_connectivity.size(); i += n)
202  {
203  for (unsigned int j = 0; j < n; j++)
204  {
205  elem->set_node(j) = nodes[_element_connectivity[j + i]];
206  }
207  elem->subdomain_id() = 0;
208  }
209 
210  return dynamic_pointer_cast<MeshBase>(mesh);
211 }
InputParameters validParams< ElementGenerator >()
Elem * getElemType(const std::string &type)
std::unique_ptr< MeshBase > & _input
Mesh that possibly comes from another generator.
const std::vector< Point > & _nodal_positions
The nodal positions.
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.
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
ElementGenerator(const InputParameters &parameters)
const std::vector< dof_id_type > & _element_connectivity
The connectivity of the elements to the nodes.
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 std::string & type() const
Get the type of this object.
Definition: MooseObject.h:53
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
Generates individual elements given a list of nodal positions.
registerMooseObject("MooseApp", ElementGenerator)
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
MatType type
PetscInt n
std::shared_ptr< MooseMesh > & _mesh
References to the mesh and displaced mesh (currently in the ActionWarehouse)
Definition: MeshGenerator.h:72
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
MeshGenerators are objects that can modify or add to an existing mesh.
Definition: MeshGenerator.h:30