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 #include "libmesh/string_to_enum.h"
15 
16 #include "MooseEnum.h"
17 
19 
22 {
24 
25  MooseEnum elem_types(LIST_GEOM_ELEM); // no default
26 
27  params.addParam<MeshGeneratorName>("input", "Optional input mesh to add the elements to");
28 
29  params.addRequiredParam<std::vector<Point>>("nodal_positions",
30  "The x,y,z positions of the nodes");
31 
32  params.addRequiredParam<std::vector<dof_id_type>>("element_connectivity",
33  "List of nodes to use for each element");
34 
35  params.addParam<MooseEnum>(
36  "elem_type", elem_types, "The type of element from libMesh to generate");
37 
38  params.addClassDescription("Generates individual elements given a list of nodal positions.");
39 
40  return params;
41 }
42 
44  : MeshGenerator(parameters),
45  _input(getMesh("input", /* allow_invalid = */ true)),
46  _nodal_positions(getParam<std::vector<Point>>("nodal_positions")),
47  _element_connectivity(getParam<std::vector<dof_id_type>>("element_connectivity")),
48  _elem_type(getParam<MooseEnum>("elem_type"))
49 {
50 }
51 
52 Elem *
53 ElementGenerator::getElemType(const std::string & type)
54 {
55  return Elem::build(Utility::string_to_enum<ElemType>(type)).release();
56 }
57 
58 std::unique_ptr<MeshBase>
60 {
61  std::unique_ptr<MeshBase> mesh = std::move(_input);
62 
63  // If there was no input mesh then let's just make a new one
64  if (!mesh)
66 
67  MooseEnum elem_type_enum = getParam<MooseEnum>("elem_type");
68  auto elem = getElemType(elem_type_enum);
69 
70  mesh->set_mesh_dimension(std::max((unsigned int)elem->dim(), mesh->mesh_dimension()));
71 
72  std::vector<Node *> nodes;
73 
74  nodes.reserve(_nodal_positions.size());
75 
76  // Add all the nodes
77  for (auto & point : _nodal_positions)
78  nodes.push_back(mesh->add_point(point));
79 
80  mesh->add_elem(elem);
81 
82  auto n = elem->n_nodes();
83 
84  for (dof_id_type i = 0; i < _element_connectivity.size(); i += n)
85  {
86  for (unsigned int j = 0; j < n; j++)
87  {
88  elem->set_node(j) = nodes[_element_connectivity[j + i]];
89  }
90  elem->subdomain_id() = 0;
91  }
92 
93  return dynamic_pointer_cast<MeshBase>(mesh);
94 }
void elem_types(const MeshBase &mesh, std::vector< ElemType > &et)
const std::string LIST_GEOM_ELEM
Definition: MooseMesh.h:58
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.
MeshBase & mesh
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.
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...
auto max(const L &left, const R &right)
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
Generates individual elements given a list of nodal positions.
registerMooseObject("MooseApp", ElementGenerator)
const std::string & type() const
Get the type of this class.
Definition: MooseBase.h:50
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
static InputParameters validParams()
Definition: MeshGenerator.C:23
static InputParameters validParams()
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...
MeshGenerators are objects that can modify or add to an existing mesh.
Definition: MeshGenerator.h:32
uint8_t dof_id_type