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 //* This file is part of the MOOSE framework
13 //* https://mooseframework.inl.gov
14 //*
15 //* All rights reserved, see COPYRIGHT for full restrictions
16 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
17 //*
18 //* Licensed under LGPL 2.1, please see LICENSE for details
19 //* https://www.gnu.org/licenses/lgpl-2.1.html
20 
21 #include "MFEMMesh.h"
22 #include "libmesh/mesh_generation.h"
23 
24 registerMooseObject("MooseApp", MFEMMesh);
25 
28 {
30  params.addParam<unsigned int>(
31  "serial_refine",
32  0,
33  "Number of serial refinements to perform on the mesh. Equivalent to uniform_refine.");
34  params.addParam<unsigned int>(
35  "uniform_refine",
36  0,
37  "Number of serial refinements to perform on the mesh. Equivalent to serial_refine");
38  params.addParam<unsigned int>(
39  "parallel_refine", 0, "Number of parallel refinements to perform on the mesh.");
40  params.addParam<std::string>("displacement", "Optional variable to use for mesh displacement.");
41 
42  params.addClassDescription("Class to read in and store an mfem::ParMesh from file.");
43 
44  return params;
45 }
46 
47 MFEMMesh::MFEMMesh(const InputParameters & parameters) : FileMesh(parameters) {}
48 
50 
51 void
53 {
54  TIME_SECTION("buildMesh", 2, "Reading Mesh");
55 
56  // Build a dummy MOOSE mesh to enable this class to work with other MOOSE classes.
58 
59  // Build the MFEM ParMesh from a serial MFEM mesh
60  mfem::Mesh mfem_ser_mesh(getFileName());
61 
62  if (isParamSetByUser("serial_refine") && isParamSetByUser("uniform_refine"))
63  paramError(
64  "Cannot define serial_refine and uniform_refine to be nonzero at the same time (they "
65  "are the same variable). Please choose one.\n");
66 
67  uniformRefinement(mfem_ser_mesh,
68  isParamSetByUser("serial_refine") ? getParam<unsigned int>("serial_refine")
69  : getParam<unsigned int>("uniform_refine"));
70 
71  // multi app should take the mpi comm from moose so is split correctly??
72  auto comm = this->comm().get();
73  _mfem_par_mesh = std::make_shared<mfem::ParMesh>(comm, mfem_ser_mesh);
74 
75  // Perform parallel refinements
76  uniformRefinement(*_mfem_par_mesh, getParam<unsigned int>("parallel_refine"));
77 
78  if (isParamSetByUser("displacement"))
79  {
80  _mesh_displacement_variable.emplace(getParam<std::string>("displacement"));
81  }
82 }
83 
84 void
85 MFEMMesh::displace(mfem::GridFunction const & displacement)
86 {
87  _mfem_par_mesh->EnsureNodes();
88  mfem::GridFunction * nodes = _mfem_par_mesh->GetNodes();
89 
90  *nodes += displacement;
91 }
92 
93 void
95 {
96  MeshTools::Generation::build_point(static_cast<UnstructuredMesh &>(getMesh()));
97 }
98 
99 void
100 MFEMMesh::uniformRefinement(mfem::Mesh & mesh, const unsigned int nref) const
101 {
102  for (unsigned int i = 0; i < nref; ++i)
103  mesh.UniformRefinement();
104 }
105 
106 std::unique_ptr<MooseMesh>
108 {
109  return _app.getFactory().copyConstruct(*this);
110 }
111 
112 #endif
void uniformRefinement(mfem::Mesh &mesh, const unsigned int nref) const
Performs a uniform refinement on the chosen mesh nref times.
Definition: MFEMMesh.C:100
virtual ~MFEMMesh()
Definition: MFEMMesh.C:49
std::optional< std::string > _mesh_displacement_variable
Holds name of variable used for mesh displacement, if set.
Definition: MFEMMesh.h:103
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:310
Factory & getFactory()
Retrieve a writable reference to the Factory associated with this App.
Definition: MooseApp.h:424
static InputParameters validParams()
Definition: MFEMMesh.C:27
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:3443
void buildDummyMooseMesh()
Builds a placeholder mesh when no MOOSE mesh is required.
Definition: MFEMMesh.C:94
void buildMesh() override
Build MFEM ParMesh and a placeholder MOOSE mesh.
Definition: MFEMMesh.C:52
std::unique_ptr< MooseMesh > safeClone() const override
Clones the mesh.
Definition: MFEMMesh.C:107
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:84
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 ...
registerMooseObject("MooseApp", MFEMMesh)
MFEMMesh(const InputParameters &parameters)
Definition: MFEMMesh.C:47
std::shared_ptr< mfem::ParMesh > _mfem_par_mesh
Smart pointers to mfem::ParMesh object.
Definition: MFEMMesh.h:109
bool isParamSetByUser(const std::string &nm) const
Test if the supplied parameter is set by a user, as opposed to not set or set to default.
void displace(mfem::GridFunction const &displacement)
Displace the nodes of the mesh by the given displacement.
Definition: MFEMMesh.C:85
MFEMMesh inherits a MOOSE mesh class which allows us to work with other MOOSE objects.
Definition: MFEMMesh.h:31
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