https://mooseframework.inl.gov
PlaneIDMeshGenerator.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 "PlaneIDMeshGenerator.h"
11 #include "CastUniquePointer.h"
12 
13 #include "libmesh/elem.h"
14 
16 
19 {
21 
22  params.addRequiredParam<MeshGeneratorName>("input", "The mesh we want to modify");
23  params.addRequiredParam<std::vector<Real>>(
24  "plane_coordinates",
25  "Coordinates of planes along the axis. The origin are at x/y/z=0 depending on the axis");
26  params.addParam<std::vector<unsigned int>>("num_ids_per_plane", "Number of unique ids per plane");
27  MooseEnum plane_axis("x y z", "z");
28  params.addParam<MooseEnum>("plane_axis", plane_axis, "Axis of plane");
29  params.addRequiredParam<std::string>("id_name", "Name of extra integer ID set");
30  params.addParam<Real>("tolerance", 1.0E-4, "Tolerance for plane coordinate check");
31  params.addClassDescription("Adds an extra element integer that identifies planes in a mesh.");
32  return params;
33 }
34 
36  : MeshGenerator(parameters),
37  _input(getMesh("input")),
38  _axis_index(getParam<MooseEnum>("plane_axis")),
39  _element_id_name(getParam<std::string>("id_name"))
40 {
41  if (!isParamValid("num_ids_per_plane"))
42  _planes = getParam<std::vector<Real>>("plane_coordinates");
43  else
44  {
45  _planes.clear();
46  std::vector<Real> base_planes = getParam<std::vector<Real>>("plane_coordinates");
47  std::vector<unsigned int> sublayers = getParam<std::vector<unsigned int>>("num_ids_per_plane");
48  if (base_planes.size() != sublayers.size() + 1)
49  paramError("plane_coordinates",
50  "Sizes of 'plane_coordinates' and 'num_ids_per_plane' disagree");
51  _planes.push_back(base_planes[0]);
52  for (unsigned int i = 0; i < sublayers.size(); ++i)
53  {
54  Real layer_size = (base_planes[i + 1] - base_planes[i]) / (Real)sublayers[i];
55  if (MooseUtils::absoluteFuzzyGreaterEqual(base_planes[i], base_planes[i + 1]))
56  paramError("plane_coordinates", "Plane coordinates must be in increasing order");
57 
58  for (unsigned int j = 0; j < sublayers[i]; ++j)
59  _planes.push_back(_planes.back() + layer_size);
60  }
61  }
62  if (_planes.size() < 2)
63  paramError("plane_coordinates", "Size of 'plane_coordinates' should be at least two");
64 }
65 
66 std::unique_ptr<MeshBase>
68 {
69  std::unique_ptr<MeshBase> mesh = std::move(_input);
70  if (mesh->mesh_dimension() < _axis_index + 1)
71  paramError("plane_axis", "Plane axis must be contained in the mesh. Check mesh dimension");
72  unsigned int extra_id_index = 0;
73  if (!mesh->has_elem_integer(_element_id_name))
74  extra_id_index = mesh->add_elem_integer(_element_id_name);
75  else
76  {
77  extra_id_index = mesh->get_elem_integer_index(_element_id_name);
79  "id_name", "An element integer with the name '", _element_id_name, "' already exists");
80  }
81 
82  // Check that the planes do not cut the elements beyond the acceptable tolerance
83  const Real tol = getParam<Real>("tolerance");
84  for (auto & elem : mesh->active_element_ptr_range())
85  {
86  const int layer_id = getPlaneID(elem->vertex_average());
87  for (unsigned int i = 0; i < elem->n_nodes(); ++i)
88  {
89  const Point & p = elem->point(i) - (elem->point(i) - elem->vertex_average()) * tol;
90  if (getPlaneID(p) != layer_id)
91  mooseError("Element at ", elem->vertex_average(), " is cut by the planes");
92  }
93  elem->set_extra_integer(extra_id_index, layer_id);
94  }
95  return dynamic_pointer_cast<MeshBase>(mesh);
96 }
97 
98 int
99 PlaneIDMeshGenerator::getPlaneID(const Point & p) const
100 {
101  // check the input point is located within the plane range
102  if (p(_axis_index) < _planes[0] || p(_axis_index) > _planes.back())
103  mooseError("The planes do not cover element at ", p);
104  int id = 0;
105  // looping over each plane to find a plane ID corresponding to the input point
106  for (unsigned int layer_id = 0; layer_id < _planes.size() - 1; ++layer_id)
107  {
108  // check the input point is located between _planes[layer_id] and _planes[layer_id + 1]
109  if (_planes[layer_id] < p(_axis_index) && _planes[layer_id + 1] >= p(_axis_index))
110  {
111  id = layer_id;
112  break;
113  }
114  }
115  return id;
116 }
Assigns plane extra ID for existing 3D meshes.
const unsigned int _axis_index
index of plane axis
PlaneIDMeshGenerator(const InputParameters &parameters)
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.
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...
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
std::unique_ptr< MeshBase > & _input
input mesh for adding reporting ID
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:33
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
static InputParameters validParams()
Definition: MeshGenerator.C:23
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
registerMooseObject("MooseApp", PlaneIDMeshGenerator)
bool absoluteFuzzyGreaterEqual(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether a variable is greater than or equal to another variable within an absolute ...
Definition: MooseUtils.h:404
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
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...
static InputParameters validParams()
void paramWarning(const std::string &param, Args... args) const
Emits a warning prefixed with the file and line number of the given param (from the input file) along...
MeshGenerators are objects that can modify or add to an existing mesh.
Definition: MeshGenerator.h:32
std::vector< Real > _planes
coordinates of planes
const std::string _element_id_name
name of integer ID
int getPlaneID(const Point &p) const
get plane ID for given plane coordiantes
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.