https://mooseframework.inl.gov
MFEMMesh.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 
12 #include "MFEMMesh.h"
13 #include "libmesh/mesh_generation.h"
14 
15 registerMooseObject("MooseApp", MFEMMesh);
16 
19 {
21  params.addParam<unsigned int>(
22  "serial_refine",
23  0,
24  "Number of serial refinements to perform on the mesh. Equivalent to uniform_refine.");
25  params.addParam<unsigned int>(
26  "uniform_refine",
27  0,
28  "Number of serial refinements to perform on the mesh. Equivalent to serial_refine");
29  params.addParam<unsigned int>(
30  "parallel_refine", 0, "Number of parallel refinements to perform on the mesh.");
31  params.addParam<std::string>("displacement", "Optional variable to use for mesh displacement.");
32  params.addParam<bool>("nonconforming",
33  false,
34  "Ensures the mesh is non-conforming: necessary for refining quad/hex "
35  "meshes and load (re)balancing.");
36  params.addParam<bool>("reorder_mesh",
37  false,
38  "Determines whether we reorder the mesh to improve dynamic partitioning. "
39  "Only Hilbert sorting is supported at present.");
40 
41  params.addClassDescription("Class to read in and store an mfem::ParMesh from file.");
42 
43  return params;
44 }
45 
46 MFEMMesh::MFEMMesh(const InputParameters & parameters) : FileMesh(parameters) {}
47 
49 
50 void
52 {
53  TIME_SECTION("buildMesh", 2, "Reading Mesh");
54 
55  // Build a dummy MOOSE mesh to enable this class to work with other MOOSE classes.
57 
58  // Build the MFEM ParMesh from a serial MFEM mesh
59  mfem::Mesh mfem_ser_mesh(getFileName());
60 
61  if (isParamSetByUser("serial_refine") && isParamSetByUser("uniform_refine"))
62  paramError(
63  "Cannot define serial_refine and uniform_refine to be nonzero at the same time (they "
64  "are the same variable). Please choose one.\n");
65 
66  uniformRefinement(mfem_ser_mesh,
67  isParamSetByUser("serial_refine") ? getParam<unsigned int>("serial_refine")
68  : getParam<unsigned int>("uniform_refine"));
69 
70  // MFEM supports load balancing of parallel non-conforming meshes
71  // with a space-filling curve partitioning, and we can improve it
72  // by re-ordering the mesh. For now, we only support the Hilbert
73  // ordering, although there is one other option.
74  if (getParam<bool>("reorder_mesh"))
75  {
76  mfem::Array<int> ordering;
77  mfem_ser_mesh.GetHilbertElementOrdering(ordering);
78  mfem_ser_mesh.ReorderElements(ordering);
79  }
80 
81  // Make sure mesh is in non-conforming mode to enable local refinement of
82  // quadrilaterals/hexahedra (c.f. MFEM example 6p). The argument (true/false)
83  // determines whether a simplex mesh is considered to be non-conforming.
84  if (getParam<bool>("nonconforming"))
85  mfem_ser_mesh.EnsureNCMesh(true);
86 
87  // multi app should take the mpi comm from moose so is split correctly??
88  auto comm = this->comm().get();
89  _mfem_par_mesh = std::make_shared<mfem::ParMesh>(comm, mfem_ser_mesh);
90 
91  // Perform parallel refinements
92  uniformRefinement(*_mfem_par_mesh, getParam<unsigned int>("parallel_refine"));
93 
94  if (isParamSetByUser("displacement"))
95  _mesh_displacement_variable.emplace(getParam<std::string>("displacement"));
96 }
97 
98 void
99 MFEMMesh::displace(mfem::GridFunction const & displacement)
100 {
101  _mfem_par_mesh->EnsureNodes();
102  mfem::GridFunction * nodes = _mfem_par_mesh->GetNodes();
103 
104  *nodes += displacement;
105 }
106 
107 void
109 {
110  auto & dummy = static_cast<UnstructuredMesh &>(getMesh());
111  MeshTools::Generation::build_square(dummy, 1, 1, 0., 1., 0., 1., ElemType::QUAD9);
112 }
113 
114 void
115 MFEMMesh::uniformRefinement(mfem::Mesh & mesh, const unsigned int nref) const
116 {
117  for (unsigned int i = 0; i < nref; ++i)
118  mesh.UniformRefinement();
119 }
120 
121 std::unique_ptr<MooseMesh>
123 {
124  return _app.getFactory().copyConstruct(*this);
125 }
126 
127 #endif
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:467
void uniformRefinement(mfem::Mesh &mesh, const unsigned int nref) const
Performs a uniform refinement on the chosen mesh nref times.
Definition: MFEMMesh.C:115
virtual ~MFEMMesh()
Definition: MFEMMesh.C:48
std::optional< std::string > _mesh_displacement_variable
Holds name of variable used for mesh displacement, if set.
Definition: MFEMMesh.h:92
MeshBase & mesh
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const Parallel::Communicator & comm() const
std::unique_ptr< T > copyConstruct(const T &object)
Copy constructs the object object.
Definition: Factory.h:358
Factory & getFactory()
Retrieve a writable reference to the Factory associated with this App.
Definition: MooseApp.h:406
static InputParameters validParams()
Definition: MFEMMesh.C:18
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:3575
void buildDummyMooseMesh()
Builds a placeholder mesh when no MOOSE mesh is required.
Definition: MFEMMesh.C:108
void buildMesh() override
Build MFEM ParMesh and a placeholder MOOSE mesh.
Definition: MFEMMesh.C:51
std::unique_ptr< MooseMesh > safeClone() const override
Clones the mesh.
Definition: MFEMMesh.C:122
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:385
registerMooseObject("MooseApp", MFEMMesh)
MFEMMesh(const InputParameters &parameters)
Definition: MFEMMesh.C:46
std::shared_ptr< mfem::ParMesh > _mfem_par_mesh
Smart pointers to mfem::ParMesh object.
Definition: MFEMMesh.h:98
void displace(mfem::GridFunction const &displacement)
Displace the nodes of the mesh by the given displacement.
Definition: MFEMMesh.C:99
MFEMMesh inherits a MOOSE mesh class which allows us to work with other MOOSE objects.
Definition: MFEMMesh.h:20
static InputParameters validParams()
Definition: FileMesh.C:28
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...
virtual std::string getFileName() const override
Returns the name of the mesh file read to produce this mesh if any or an empty string otherwise...
Definition: FileMesh.h:31
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:215