https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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 "MooseApp.h"
14#include "libmesh/mesh_generation.h"
15
16#include <fstream>
17
20{
22 params.addParam<unsigned int>(
23 "serial_refine",
24 0,
25 "Number of serial refinements to perform on the mesh. Equivalent to uniform_refine.");
26 params.addParam<unsigned int>(
27 "uniform_refine",
28 0,
29 "Number of serial refinements to perform on the mesh. Equivalent to serial_refine");
30 params.addParam<unsigned int>(
31 "parallel_refine", 0, "Number of parallel refinements to perform on the mesh.");
32 params.addParam<std::string>("displacement", "Optional variable to use for mesh displacement.");
33 params.addParam<bool>("nonconforming",
34 false,
35 "Ensures the mesh is non-conforming: necessary for refining quad/hex "
36 "meshes and load (re)balancing.");
37 params.addParam<bool>("reorder_mesh",
38 false,
39 "Determines whether we reorder the mesh to improve dynamic partitioning. "
40 "Only Hilbert sorting is supported at present.");
41
42 return params;
43}
44
45MFEMMesh::MFEMMesh(const InputParameters & parameters) : MooseMesh(parameters) {}
46
47void
49{
50 // MooseMesh::init() handles the libMesh dummy mesh:
51 // - recovery: reads the libMesh mesh back from its checkpoint file
52 // - normal run: calls buildMesh(), which for MFEMMesh builds both the
53 // dummy libMesh mesh and the MFEM ParMesh
55
57 {
58 // MooseMesh::init() already restored the libMesh dummy mesh from its checkpoint.
59 // Now restore the MFEM parallel mesh from its own checkpoint file.
60 const auto checkpoint_file = _app.getRestartRecoverFileBase() + _app.checkpointSuffix() +
61 ".mfem.mesh." + std::to_string(this->processor_id());
62 std::ifstream input(checkpoint_file);
63 if (!input)
64 mooseError("Unable to open MFEM recovery mesh file '", checkpoint_file, "'.");
65
66 _mfem_par_mesh = std::make_shared<mfem::ParMesh>(this->comm().get(), input);
67
68 if (isParamSetByUser("displacement"))
69 _mesh_displacement_variable.emplace(getParam<std::string>("displacement"));
70 }
71}
72
73void
75{
76 TIME_SECTION("buildMesh", 2, "Building MFEM Mesh");
77
78 // Build the MFEM ParMesh from a serial MFEM mesh
79 mfem::Mesh mfem_ser_mesh = buildSerialMFEMMesh();
80
81 if (isParamSetByUser("serial_refine") && isParamSetByUser("uniform_refine"))
82 paramError("serial_refine",
83 "Cannot set both serial_refine and uniform_refine at the same time.");
84
85 uniformRefinement(mfem_ser_mesh,
86 isParamSetByUser("serial_refine") ? getParam<unsigned int>("serial_refine")
87 : getParam<unsigned int>("uniform_refine"));
88
89 // MFEM supports load balancing of parallel non-conforming meshes
90 // with a space-filling curve partitioning, and we can improve it
91 // by re-ordering the mesh. For now, we only support the Hilbert
92 // ordering, although there is one other option.
93 if (getParam<bool>("reorder_mesh"))
94 {
95 mfem::Array<int> ordering;
96 mfem_ser_mesh.GetHilbertElementOrdering(ordering);
97 mfem_ser_mesh.ReorderElements(ordering);
98 }
99
100 // Make sure mesh is in non-conforming mode to enable local refinement of
101 // quadrilaterals/hexahedra (c.f. MFEM example 6p). The argument (true/false)
102 // determines whether a simplex mesh is considered to be non-conforming.
103 if (getParam<bool>("nonconforming"))
104 mfem_ser_mesh.EnsureNCMesh(true);
105
106 _mfem_par_mesh = std::make_shared<mfem::ParMesh>(this->comm().get(), mfem_ser_mesh);
107
108 // Perform parallel refinements
109 uniformRefinement(*_mfem_par_mesh, getParam<unsigned int>("parallel_refine"));
110
111 if (isParamSetByUser("displacement"))
112 _mesh_displacement_variable.emplace(getParam<std::string>("displacement"));
113
114 // Build a dummy MOOSE mesh to enable this class to work with other MOOSE classes.
116}
117
118std::vector<std::filesystem::path>
119MFEMMesh::writeRecoveryFiles(const std::filesystem::path & file_base)
120{
122
123 mooseAssert(_mfem_par_mesh, "MFEM parallel mesh is not initialized");
124
125 const auto checkpoint_file =
126 file_base.string() + ".mfem.mesh." + std::to_string(this->processor_id());
127 std::ofstream output(checkpoint_file);
128 if (!output)
129 mooseError("Unable to open MFEM recovery mesh file '", checkpoint_file, "' for writing.");
130
131 _mfem_par_mesh->ParPrint(output);
132 return {checkpoint_file};
133}
134
135void
136MFEMMesh::displace(mfem::GridFunction const & displacement)
137{
138 _mfem_par_mesh->EnsureNodes();
139 *_mfem_par_mesh->GetNodes() += displacement;
140}
141
142void
144{
145 // The libMesh placeholder is always replicated, independently of the distributed MFEM mesh.
147 auto & dummy = cast_ref<UnstructuredMesh &>(getMesh());
148 MeshTools::Generation::build_point(dummy);
149 if (dimension() >= 2)
150 MeshTools::Generation::build_square(dummy, 1, 1, 0., 1., 0., 1., ElemType::QUAD9);
151}
152
153void
154MFEMMesh::uniformRefinement(mfem::Mesh & mesh, const unsigned int nref) const
155{
156 for (unsigned int i = 0; i < nref; ++i)
157 mesh.UniformRefinement();
158}
159
160#endif
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.
std::optional< std::string > _mesh_displacement_variable
Holds name of variable used for mesh displacement, if set.
Definition MFEMMesh.h:95
unsigned int dimension() const override
Returns MeshBase::mesh_dimension(), (not MeshBase::spatial_dimension()!) of the underlying libMesh me...
Definition MFEMMesh.h:68
std::vector< std::filesystem::path > writeRecoveryFiles(const std::filesystem::path &file_base) override
Write the mesh files needed for recovery/checkpointing.
Definition MFEMMesh.C:119
static InputParameters validParams()
Definition MFEMMesh.C:19
void uniformRefinement(mfem::Mesh &mesh, const unsigned int nref) const
Performs a uniform refinement on the chosen mesh nref times.
Definition MFEMMesh.C:154
void displace(mfem::GridFunction const &displacement)
Displace the nodes of the mesh by the given displacement.
Definition MFEMMesh.C:136
virtual mfem::Mesh buildSerialMFEMMesh()=0
Build and return the serial mfem::Mesh for this object.
void buildDummyMooseMesh()
Builds a dimension-compatible libMesh placeholder after initializing the MFEM mesh.
Definition MFEMMesh.C:143
void init() override
Initialize the MFEM ParMesh and placeholder MOOSE mesh.
Definition MFEMMesh.C:48
std::shared_ptr< mfem::ParMesh > _mfem_par_mesh
Smart pointer to mfem::ParMesh object.
Definition MFEMMesh.h:101
void buildMesh() override final
Must be overridden by child classes.
Definition MFEMMesh.C:74
MFEMMesh(const InputParameters &parameters)
Definition MFEMMesh.C:45
static const std::string & checkpointSuffix()
The file suffix for the checkpoint mesh.
Definition MooseApp.C:3044
bool isUltimateMaster() const
Whether or not this app is the ultimate master app.
Definition MooseApp.h:866
std::string getRestartRecoverFileBase() const
The file_base for the recovery file.
Definition MooseApp.h:500
bool isRecovering() const
Whether or not this is a "recover" calculation.
Definition MooseApp.C:1674
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
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
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition MooseMesh.h:95
virtual std::vector< std::filesystem::path > writeRecoveryFiles(const std::filesystem::path &file_base)
Write the mesh files needed for recovery/checkpointing.
Definition MooseMesh.C:2986
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition MooseMesh.C:3557
virtual void init()
Initialize the Mesh object.
Definition MooseMesh.C:2937
static InputParameters validParams()
Typical "Moose-style" constructor and copy constructor.
Definition MooseMesh.C:81
void setParallelType(ParallelType parallel_type)
Allow to change parallel type.
Definition MooseMesh.h:2318
bool allowRecovery() const
Returns whether this mesh is allowed to read a recovery file.
Definition MooseMesh.h:1573
MooseApp & _app
The MOOSE application this is associated with.
Definition MooseBase.h:375
processor_id_type processor_id() const
const Parallel::Communicator & comm() const
MeshBase & mesh