https://mooseframework.inl.gov
Loading...
Searching...
No Matches
StackGenerator.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 "StackGenerator.h"
11#include "CastUniquePointer.h"
12
13#include "libmesh/replicated_mesh.h"
14#include "libmesh/distributed_mesh.h"
15#include "libmesh/boundary_info.h"
16#include "libmesh/mesh_modification.h"
17#include "libmesh/bounding_box.h"
18#include "libmesh/mesh_tools.h"
19#include "libmesh/point.h"
20#include "MooseMeshUtils.h"
21
22#include <typeinfo>
23
25
28{
30
31 MooseEnum dims("2=2 3=3");
32 params.addRequiredParam<MooseEnum>("dim", dims, "The dimension of the mesh to be generated");
33
34 params.addRequiredParam<std::vector<MeshGeneratorName>>("inputs",
35 "The meshes we want to stitch together");
36
37 params.addParam<Real>("bottom_height", 0, "The height of the bottom of the final mesh");
38
39 // y boundary names (2D case)
40 params.addParam<BoundaryName>("top_boundary", "top", "name of the top (y) boundary");
41 params.addParam<BoundaryName>("bottom_boundary", "bottom", "name of the bottom (y) boundary");
42
43 // z boundary names (3D case)
44 params.addParam<BoundaryName>("front_boundary", "front", "name of the front (z) boundary");
45 params.addParam<BoundaryName>("back_boundary", "back", "name of the back (z) boundary");
46
47 params.addClassDescription("Use the supplied meshes and stitch them on top of each other");
48
49 return params;
50}
51
53 : MeshGenerator(parameters),
54 _dim(getParam<MooseEnum>("dim")),
55 _mesh_ptrs(getMeshes("inputs")),
56 _input_names(getParam<std::vector<MeshGeneratorName>>("inputs")),
57 _bottom_height(getParam<Real>("bottom_height"))
58{
59}
60
61std::unique_ptr<MeshBase>
63{
64 std::unique_ptr<ReplicatedMesh> mesh = dynamic_pointer_cast<ReplicatedMesh>(*_mesh_ptrs[0]);
65 if (mesh == nullptr)
66 mooseError("StackGenerator only works with ReplicatedMesh : mesh from Meshgenerator ",
67 _input_names[0],
68 "is not a ReplicatedMesh.");
69
70 int dim = static_cast<int>(_dim);
71
72 if (dim != int(mesh->mesh_dimension()))
73 paramError("dim",
74 "incompatible mesh dimensions: dim=",
75 dim,
76 " and first mesh dimension is ",
77 mesh->mesh_dimension());
78
79 // Reserve spaces for the other meshes (no need to store the first one another time)
80 std::vector<std::unique_ptr<ReplicatedMesh>> meshes;
81 meshes.reserve(_input_names.size() - 1);
82
83 // Read in all of the other meshes
84 for (MooseIndex(_input_names) i = 1; i < _input_names.size(); ++i)
85 meshes.push_back(dynamic_pointer_cast<ReplicatedMesh>(*_mesh_ptrs[i]));
86
87 // Check that the casts didn't fail, and that the dimensions match
88 for (MooseIndex(meshes) i = 0; i < meshes.size(); ++i)
89 {
90 if (meshes[i] == nullptr)
91 mooseError("StackGenerator only works with ReplicatedMesh : mesh from Meshgenerator ",
92 _input_names[i + 1],
93 "is not a ReplicatedMesh.");
94 if (static_cast<int>(meshes[i]->mesh_dimension()) != dim)
95 mooseError("Mesh from MeshGenerator : ", _input_names[i + 1], " is not in ", _dim, "D.");
96 }
97
98 // Getting the boundaries provided by the user
99 std::vector<BoundaryName> boundary_names = {getParam<BoundaryName>("top_boundary"),
100 getParam<BoundaryName>("bottom_boundary")};
101 if (dim == 3)
102 boundary_names = {getParam<BoundaryName>("front_boundary"),
103 getParam<BoundaryName>("back_boundary")};
104
105 std::vector<boundary_id_type> ids =
106 MooseMeshUtils::getBoundaryIDs(*meshes[0], boundary_names, true);
107
108 mooseAssert(ids.size() == boundary_names.size(),
109 "Unexpected number of ids returned for MooseMeshUtils::getBoundaryIDs");
110
111 boundary_id_type first = ids[0], second = ids[1];
112
113 // Getting the width of each mesh
114 std::vector<Real> heights;
115 heights.push_back(computeWidth(*mesh, _dim) + _bottom_height);
116 for (MooseIndex(meshes) i = 0; i < meshes.size(); ++i)
117 heights.push_back(computeWidth(*meshes[i], _dim) + *heights.rbegin());
118
119 // Move the first mesh at the provided height
120 switch (_dim)
121 {
122 case 2:
123 MeshTools::Modification::translate(*mesh, 0, _bottom_height, 0);
124 break;
125 case 3:
126 MeshTools::Modification::translate(*mesh, 0, 0, _bottom_height);
127 break;
128 }
129
130 // Move all of the other meshes in the right spots then stitch them one by one to the first one
131 for (MooseIndex(meshes) i = 0; i < meshes.size(); ++i)
132 {
133 switch (_dim)
134 {
135 case 2:
136 MeshTools::Modification::translate(*meshes[i], 0, heights[i], 0);
137 break;
138 case 3:
139 MeshTools::Modification::translate(*meshes[i], 0, 0, heights[i]);
140 break;
141 }
142 mesh->stitch_meshes(*meshes[i], first, second, TOLERANCE, /*clear_stitched_boundary_ids=*/true);
143 }
144
145 return dynamic_pointer_cast<MeshBase>(mesh);
146}
147
148Real
149StackGenerator::computeWidth(const MeshBase & mesh, const int & dim)
150{
151 BoundingBox bbox = MeshTools::create_bounding_box(mesh);
152 return bbox.max()(dim - 1) - bbox.min()(dim - 1);
153}
registerMooseObject("MooseApp", StackGenerator)
unsigned int dim
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
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.
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...
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.
MeshGenerators are objects that can modify or add to an existing mesh.
static InputParameters validParams()
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 ...
Definition MooseBase.h:457
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition MooseBase.h:271
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition MooseEnum.h:55
Take several 3D meshes and stitch them on top of each other like a stack.
const Real _bottom_height
Height (z) of the bottom of the final mesh.
const std::vector< MeshGeneratorName > & _input_names
The meshgenerators to read.
const std::vector< std::unique_ptr< MeshBase > * > _mesh_ptrs
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.
StackGenerator(const InputParameters &parameters)
Real computeWidth(const MeshBase &mesh, const int &dim)
static InputParameters validParams()
const MooseEnum _dim
The dimension of the mesh.
MeshBase & mesh
std::vector< BoundaryID > getBoundaryIDs(const libMesh::MeshBase &mesh, const std::vector< BoundaryName > &boundary_name, bool generate_unknown, const std::set< BoundaryID > &mesh_boundary_ids)
Gets the boundary IDs with their names.
libMesh::BoundingBox create_bounding_box(const MeshBase &mesh)