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 
33  params.addClassDescription("Class to read in and store an mfem::ParMesh from file.");
34 
35  return params;
36 }
37 
38 MFEMMesh::MFEMMesh(const InputParameters & parameters) : FileMesh(parameters) {}
39 
41 
42 void
44 {
45  TIME_SECTION("buildMesh", 2, "Reading Mesh");
46 
47  // Build a dummy MOOSE mesh to enable this class to work with other MOOSE classes.
49 
50  // Build the MFEM ParMesh from a serial MFEM mesh
51  mfem::Mesh mfem_ser_mesh(getFileName());
52 
53  if (isParamSetByUser("serial_refine") && isParamSetByUser("uniform_refine"))
54  paramError(
55  "Cannot define serial_refine and uniform_refine to be nonzero at the same time (they "
56  "are the same variable). Please choose one.\n");
57 
58  uniformRefinement(mfem_ser_mesh,
59  isParamSetByUser("serial_refine") ? getParam<unsigned int>("serial_refine")
60  : getParam<unsigned int>("uniform_refine"));
61 
62  // multi app should take the mpi comm from moose so is split correctly??
63  auto comm = this->comm().get();
64  _mfem_par_mesh = std::make_shared<mfem::ParMesh>(comm, mfem_ser_mesh);
65 
66  // Perform parallel refinements
67  uniformRefinement(*_mfem_par_mesh, getParam<unsigned int>("parallel_refine"));
68 
69  if (isParamSetByUser("displacement"))
70  {
71  _mesh_displacement_variable.emplace(getParam<std::string>("displacement"));
72  }
73 }
74 
75 void
76 MFEMMesh::displace(mfem::GridFunction const & displacement)
77 {
78  _mfem_par_mesh->EnsureNodes();
79  mfem::GridFunction * nodes = _mfem_par_mesh->GetNodes();
80 
81  *nodes += displacement;
82 }
83 
84 void
86 {
87  MeshTools::Generation::build_point(static_cast<UnstructuredMesh &>(getMesh()));
88 }
89 
90 void
91 MFEMMesh::uniformRefinement(mfem::Mesh & mesh, const unsigned int nref) const
92 {
93  for (unsigned int i = 0; i < nref; ++i)
94  mesh.UniformRefinement();
95 }
96 
97 std::unique_ptr<MooseMesh>
99 {
100  return _app.getFactory().copyConstruct(*this);
101 }
102 
103 #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:439
void uniformRefinement(mfem::Mesh &mesh, const unsigned int nref) const
Performs a uniform refinement on the chosen mesh nref times.
Definition: MFEMMesh.C:91
virtual ~MFEMMesh()
Definition: MFEMMesh.C:40
std::optional< std::string > _mesh_displacement_variable
Holds name of variable used for mesh displacement, if set.
Definition: MFEMMesh.h:94
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:401
static InputParameters validParams()
Definition: MFEMMesh.C:18
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:3488
void buildDummyMooseMesh()
Builds a placeholder mesh when no MOOSE mesh is required.
Definition: MFEMMesh.C:85
void buildMesh() override
Build MFEM ParMesh and a placeholder MOOSE mesh.
Definition: MFEMMesh.C:43
std::unique_ptr< MooseMesh > safeClone() const override
Clones the mesh.
Definition: MFEMMesh.C:98
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:357
registerMooseObject("MooseApp", MFEMMesh)
MFEMMesh(const InputParameters &parameters)
Definition: MFEMMesh.C:38
std::shared_ptr< mfem::ParMesh > _mfem_par_mesh
Smart pointers to mfem::ParMesh object.
Definition: MFEMMesh.h:100
void displace(mfem::GridFunction const &displacement)
Displace the nodes of the mesh by the given displacement.
Definition: MFEMMesh.C:76
MFEMMesh inherits a MOOSE mesh class which allows us to work with other MOOSE objects.
Definition: MFEMMesh.h:22
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:205