Loading [MathJax]/extensions/tex2jax.js
https://mooseframework.inl.gov
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
ElementGenerator.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
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  // Element shape and location
30  params.addRequiredParam<std::vector<Point>>("nodal_positions",
31  "The x,y,z positions of the nodes");
32  params.addRequiredParam<std::vector<dof_id_type>>("element_connectivity",
33  "List of nodes to use for each element");
35  "elem_type", elem_types, "The type of element from libMesh to generate");
36 
37  // Subdomain
38  params.addParam<SubdomainName>("subdomain_name", "Subdomain name");
39  params.addParam<SubdomainID>("subdomain_id", 0, "Subdomain id");
40  // Sidesets
41  params.addParam<bool>("create_sidesets",
42  false,
43  "Create separate sidesets for each side. "
44  "The side index is used as the boundary ID for each sideset.");
45 
46  params.addClassDescription("Generates individual elements given a list of nodal positions.");
47 
48  return params;
49 }
50 
52  : MeshGenerator(parameters),
53  _input(getMesh("input", /* allow_invalid = */ true)),
54  _nodal_positions(getParam<std::vector<Point>>("nodal_positions")),
55  _element_connectivity(getParam<std::vector<dof_id_type>>("element_connectivity")),
56  _elem_type(getParam<MooseEnum>("elem_type"))
57 {
58 }
59 
60 Elem *
61 ElementGenerator::getElemType(const std::string & type)
62 {
63  return Elem::build(Utility::string_to_enum<ElemType>(type)).release();
64 }
65 
66 std::unique_ptr<MeshBase>
68 {
69  std::unique_ptr<MeshBase> mesh = std::move(_input);
70 
71  // If there was no input mesh then let's just make a new one
72  if (!mesh)
74 
75  MooseEnum elem_type_enum = getParam<MooseEnum>("elem_type");
76  auto elem = getElemType(elem_type_enum);
77  elem->subdomain_id() = getParam<SubdomainID>("subdomain_id");
78  if (isParamValid("subdomain_name"))
79  mesh->subdomain_name(getParam<SubdomainID>("subdomain_id")) =
80  getParam<SubdomainName>("subdomain_name");
81 
82  mesh->set_mesh_dimension(std::max((unsigned int)elem->dim(), mesh->mesh_dimension()));
83 
84  std::vector<Node *> nodes;
85 
86  nodes.reserve(_nodal_positions.size());
87 
88  // Add all the nodes
89  for (auto & point : _nodal_positions)
90  nodes.push_back(mesh->add_point(point));
91 
92  mesh->add_elem(elem);
93 
94  auto n = elem->n_nodes();
95 
96  for (dof_id_type i = 0; i < _element_connectivity.size(); i += n)
97  {
98  for (unsigned int j = 0; j < n; j++)
99  {
100  elem->set_node(j, nodes[_element_connectivity[j + i]]);
101  }
102  }
103 
104  // We just added an element
105  mesh->set_isnt_prepared();
106 
107  if (getParam<bool>("create_sidesets"))
108  for (const auto i_side : make_range(elem->n_sides()))
109  mesh->get_boundary_info().add_side(elem, i_side, i_side);
110 
111  return dynamic_pointer_cast<MeshBase>(mesh);
112 }
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)
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
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:51
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:33
static InputParameters validParams()
Definition: MeshGenerator.C:23
static InputParameters validParams()
IntRange< T > make_range(T beg, T end)
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 optional 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