https://mooseframework.inl.gov
Loading...
Searching...
No Matches
MFEMGeneratedMeshGenerator.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#ifdef MOOSE_MFEM_ENABLED
11
13
15
18{
20
21 MooseEnum dims("1=1 2=2 3=3");
22 params.addRequiredParam<MooseEnum>("dim", dims, "Spatial dimension of the mesh (1, 2, or 3).");
23
24 params.addParam<unsigned int>("nx", 1, "Number of elements in the x direction.");
25 params.addParam<unsigned int>("ny", 1, "Number of elements in the y direction.");
26 params.addParam<unsigned int>("nz", 1, "Number of elements in the z direction.");
27
28 params.addParam<Real>("xmax", 1.0, "Upper bound of the domain in the x direction.");
29 params.addParam<Real>("ymax", 1.0, "Upper bound of the domain in the y direction.");
30 params.addParam<Real>("zmax", 1.0, "Upper bound of the domain in the z direction.");
31
32 // The MooseEnum values are set to match mfem::Element::Type so that
33 // getEnum<mfem::Element::Type>() can be used directly below, while the names correspond to the
34 // form MOOSE users of libMesh-based meshes are already familiar with. POINT, WEDGE, and PYRAMID
35 // are omitted: mfem::Mesh::MakeCartesian1D/2D/3D, which this generator uses, only ever produce an
36 // edge/segment (1D), a triangle/quadrilateral (2D), or a tetrahedron/hexahedron (3D).
37 MooseEnum elem_types("EDGE=1 TRI=2 QUAD=3 TET=4 HEX=5");
38 params.addParam<MooseEnum>("elem_type",
39 elem_types,
40 "Element type. Use EDGE for 1D meshes, TRI or QUAD for 2D meshes, "
41 "TET or HEX for 3D meshes. If not specified, defaults to EDGE for "
42 "1D, QUAD for 2D, and HEX for 3D.");
43
44 params.addClassDescription("Generates a structured Cartesian MFEM mesh (line, rectangle, or box) "
45 "with uniformly spaced elements.");
46
47 return params;
48}
49
51 : MFEMMeshGenerator(parameters),
52 _dim(getParam<MooseEnum>("dim")),
53 _nx(getParam<unsigned int>("nx")),
54 _ny(getParam<unsigned int>("ny")),
55 _nz(getParam<unsigned int>("nz")),
56 _xmax(getParam<Real>("xmax")),
57 _ymax(getParam<Real>("ymax")),
58 _zmax(getParam<Real>("zmax")),
59 _elem_type(
60 [this]()
61 {
62 // Apply dimension-dependent default for elem_type if not set by user
63 if (!isParamSetByUser("elem_type"))
64 {
65 if (_dim == 1)
66 return mfem::Element::SEGMENT;
67 else if (_dim == 2)
68 return mfem::Element::QUADRILATERAL;
69 return mfem::Element::HEXAHEDRON;
70 }
71
72 const auto elem_type = getParam<MooseEnum>("elem_type").getEnum<mfem::Element::Type>();
73 if ((_dim == 1 && elem_type != mfem::Element::SEGMENT) ||
74 (_dim == 2 && elem_type != mfem::Element::TRIANGLE &&
75 elem_type != mfem::Element::QUADRILATERAL) ||
76 (_dim == 3 && elem_type != mfem::Element::TETRAHEDRON &&
77 elem_type != mfem::Element::HEXAHEDRON))
78 paramError("elem_type",
79 "Use EDGE for 1D meshes, TRI or QUAD for 2D meshes, "
80 "and TET or HEX for 3D meshes.");
81 return elem_type;
82 }())
83{
84}
85
86namespace
87{
88void
89addBdrSet(mfem::Mesh & mesh, int attr, const std::string & name)
90{
91 mesh.bdr_attribute_sets.SetAttributeSet(name, mfem::Array<int>{attr});
92}
93} // namespace
94
95std::unique_ptr<mfem::Mesh>
97{
98 if (_dim == 1)
99 {
100 auto mesh = std::make_unique<mfem::Mesh>(mfem::Mesh::MakeCartesian1D(_nx, _xmax));
101 addBdrSet(*mesh, 1, "left");
102 addBdrSet(*mesh, 2, "right");
103 return mesh;
104 }
105
106 if (_dim == 2)
107 {
108 auto mesh = std::make_unique<mfem::Mesh>(
109 mfem::Mesh::MakeCartesian2D(_nx, _ny, _elem_type, true, _xmax, _ymax));
110 addBdrSet(*mesh, 1, "bottom");
111 addBdrSet(*mesh, 2, "right");
112 addBdrSet(*mesh, 3, "top");
113 addBdrSet(*mesh, 4, "left");
114 return mesh;
115 }
116
117 // dim == 3
118 auto mesh = std::make_unique<mfem::Mesh>(
119 mfem::Mesh::MakeCartesian3D(_nx, _ny, _nz, _elem_type, _xmax, _ymax, _zmax));
120 addBdrSet(*mesh, 1, "bottom");
121 addBdrSet(*mesh, 2, "front");
122 addBdrSet(*mesh, 3, "right");
123 addBdrSet(*mesh, 4, "back");
124 addBdrSet(*mesh, 5, "left");
125 addBdrSet(*mesh, 6, "top");
126 return mesh;
127}
128
129#endif
registerMooseObject("MooseApp", MFEMGeneratedMeshGenerator)
void ErrorVector unsigned int
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.
Generates a structured Cartesian MFEM mesh: a line (1D), rectangle (2D), or box (3D) with uniformly s...
const Real _ymax
Upper bound of the domain in the y direction (lower bound is 0)
const unsigned int _dim
Mesh dimension (1, 2, or 3)
const unsigned int _ny
Number of elements in the y direction.
const Real _xmax
Upper bound of the domain in the x direction (lower bound is 0)
const unsigned int _nz
Number of elements in the z direction.
const unsigned int _nx
Number of elements in the x direction.
const Real _zmax
Upper bound of the domain in the z direction (lower bound is 0)
static InputParameters validParams()
std::unique_ptr< mfem::Mesh > generateMFEMMesh() override
Implement this to produce the mfem::Mesh for this generator.
MFEMGeneratedMeshGenerator(const InputParameters &parameters)
const mfem::Element::Type _elem_type
Element type (resolved from user input or dimension-based default); unused for dim == 1.
Abstract base class for MFEM-native mesh generators.
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
bool isParamSetByUser(const std::string &name) const
Test if the supplied parameter is set by a user, as opposed to not set or set to default.
Definition MooseBase.h:205
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition MooseEnum.h:55
MeshBase & mesh