https://mooseframework.inl.gov
MFEMProblem.h
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 #pragma once
13 
14 #include "Attributes.h"
15 #include "ExternalProblem.h"
16 #include "MFEMProblemData.h"
17 #include "MFEMMesh.h"
18 #include "MFEMRefinementMarker.h"
19 #include "MFEMComplexVariable.h"
20 
22 {
23 public:
28 
32  MFEMProblem(const InputParameters & params);
33 
37  virtual ~MFEMProblem() {}
38 
39  virtual void initialSetup() override;
40  virtual void execute(const ExecFlagType & exec_type) override;
41  virtual void externalSolve() override {}
42  virtual void syncSolutions(Direction) override {}
43 
48  virtual MFEMMesh & mesh() override;
49  virtual const MFEMMesh & mesh() const override;
51 
56  virtual std::vector<VariableName> getAuxVariableNames();
57 
58  void addBoundaryCondition(const std::string & bc_name,
59  const std::string & name,
60  InputParameters & parameters) override;
61 
62  void addMaterial(const std::string & material_name,
63  const std::string & name,
64  InputParameters & parameters) override;
65 
66  void addFunctorMaterial(const std::string & material_name,
67  const std::string & name,
68  InputParameters & parameters) override;
69 
73  void addFESpace(const std::string & type, const std::string & name, InputParameters & parameters);
77  void setDevice();
78 
82  void setMesh();
83 
87  void addSubMesh(const std::string & type, const std::string & name, InputParameters & parameters);
88 
92  void addTransfer(const std::string & transfer_name,
93  const std::string & name,
94  InputParameters & parameters) override;
99  virtual void addVariable(const std::string & var_type,
100  const std::string & var_name,
101  InputParameters & parameters) override;
102 
106  void addGridFunction(const std::string & var_type,
107  const std::string & var_name,
109 
115  void addAuxVariable(const std::string & var_type,
116  const std::string & var_name,
117  InputParameters & parameters) override;
118 
123  void
124  addElementalFieldVariable(const std::string &, const std::string &, InputParameters &) override
125  {
126  }
127 
132  void addKernel(const std::string & kernel_name,
133  const std::string & name,
134  InputParameters & parameters) override;
135 
139  void addRealComponentToKernel(const std::string & kernel_name,
140  const std::string & name,
142 
146  void addImagComponentToKernel(const std::string & kernel_name,
147  const std::string & name,
149 
153  void addRealComponentToBC(const std::string & kernel_name,
154  const std::string & name,
156 
160  void addImagComponentToBC(const std::string & kernel_name,
161  const std::string & name,
163 
167  void addAuxKernel(const std::string & kernel_name,
168  const std::string & name,
169  InputParameters & parameters) override;
170 
175  void addFunction(const std::string & type,
176  const std::string & name,
177  InputParameters & parameters) override;
178 
182  void addInitialCondition(const std::string & ic_name,
183  const std::string & name,
184  InputParameters & parameters) override;
185 
191  void addPostprocessor(const std::string & type,
192  const std::string & name,
193  InputParameters & parameters) override;
194 
198  void addVectorPostprocessor(const std::string & type,
199  const std::string & name,
200  InputParameters & parameters) override;
201 
205  void addMFEMPreconditioner(const std::string & user_object_name,
206  const std::string & name,
212  void addIndicator(const std::string & type,
213  const std::string & name,
214  InputParameters & parameters) override;
215 
220  void addMarker(const std::string & type,
221  const std::string & name,
222  InputParameters & parameters) override;
223 
227  virtual void addMFEMSolver(const std::string & user_object_name,
228  const std::string & name,
230 
235  void addMFEMNonlinearSolver(unsigned int nl_max_its,
236  mfem::real_t nl_abs_tol,
237  mfem::real_t nl_rel_tol,
238  unsigned int print_level);
239 
243  void executeMFEMObjects(const ExecFlagType & exec_type);
244 
251 
258 
264 
268  const MFEMProblemData & getProblemData() const { return _problem_data; }
269 
273  MPI_Comm getComm() { return getProblemData().comm; }
274 
278  const mfem::ParMesh & getMFEMVariableMesh(std::string var_name)
279  {
280  if (_problem_data.gridfunctions.Has(var_name))
281  return *_problem_data.gridfunctions.Get(var_name)->ParFESpace()->GetParMesh();
282  else if (_problem_data.cmplx_gridfunctions.Has(var_name))
283  return *_problem_data.cmplx_gridfunctions.Get(var_name)->ParFESpace()->GetParMesh();
284  else
285  mooseError("Variable " + var_name +
286  " not found in MFEMProblem real or complex gridfunctions.");
287  }
288 
292  void displaceMesh();
293 
297  void rebalanceMesh(mfem::ParMesh & pmesh);
298 
302  std::optional<std::reference_wrapper<mfem::ParGridFunction const>>
304 
305  Moose::FEBackend feBackend() const override { return Moose::FEBackend::MFEM; }
306 
307  std::string solverTypeString(unsigned int solver_sys_num) override;
308 
312  void updateFESpaces();
313 
317  void updateGridFunctions();
318 
322  bool hRefine() { return _problem_data.refiner && _problem_data.refiner->hRefine(); }
323 
327  bool pRefine() { return _problem_data.refiner && _problem_data.refiner->pRefine(); }
328 
332  std::shared_ptr<mfem::ParGridFunction> getGridFunction(const std::string & name)
333  {
335  }
336 
340  std::shared_ptr<mfem::ParComplexGridFunction> getComplexGridFunction(const std::string & name)
341  {
343  }
344 
348  enum class NumericType
349  {
350  REAL,
351  COMPLEX
352  };
353 
358 
362  template <typename T>
363  T & getMFEMObject(const std::string & system,
364  const std::string & name,
365  const THREAD_ID tid = 0) const;
366 
370  bool hasMFEMObject(const std::string & system, const std::string & name) const;
371 
372 protected:
377 
382 };
383 
384 template <typename T>
385 T &
386 MFEMProblem::getMFEMObject(const std::string & system,
387  const std::string & name,
388  const THREAD_ID tid) const
389 {
390  std::vector<T *> objs;
391  theWarehouse()
392  .query()
393  .condition<AttribSystem>(system)
394  .condition<AttribThread>(tid)
395  .condition<AttribName>(name)
396  .queryInto(objs);
397  if (objs.empty())
398  mooseError("Unable to find MFEM object with system '" + system + "' and name '" + name + "'");
399  mooseAssert(objs.size() == 1, "Shouldn't find more than one object with given system and name");
400  return *(objs[0]);
401 }
402 
403 #endif
std::shared_ptr< mfem::ParGridFunction > getGridFunction(const std::string &name)
Definition: MFEMProblem.h:332
void addGridFunction(const std::string &var_type, const std::string &var_name, InputParameters &parameters)
Adds one MFEM GridFunction to be used in the MFEM solve.
Definition: MFEMProblem.C:246
void addMarker(const std::string &type, const std::string &name, InputParameters &parameters) override
Override of FEProblemBase::addMarker.
Definition: MFEMProblem.C:107
void addMFEMPreconditioner(const std::string &user_object_name, const std::string &name, InputParameters &parameters)
Method called in AddMFEMPreconditionerAction which will create the solver.
Definition: MFEMProblem.C:88
virtual void syncSolutions(Direction) override
Method to transfer data to/from the external application to the associated transfer mesh...
Definition: MFEMProblem.h:42
Base problem data struct.
void addRealComponentToBC(const std::string &kernel_name, const std::string &name, InputParameters &parameters)
Adds a real component BC to the parent MFEMComplexIntegratedBC.
Definition: MFEMProblem.C:359
Moose::MFEM::ComplexGridFunctions cmplx_gridfunctions
const MFEMProblemData & getProblemData() const
Return the current MFEM problem data in a const context.
Definition: MFEMProblem.h:268
MFEMProblemData & getProblemData()
Method to get the current MFEMProblemData object storing the current data specifying the FE problem...
Definition: MFEMProblem.h:263
bool Has(const std::string &field_name) const
Predicate to check if a field is registered with name field_name.
void addImagComponentToBC(const std::string &kernel_name, const std::string &name, InputParameters &parameters)
Adds an imaginary component BC to the parent MFEMComplexIntegratedBC.
Definition: MFEMProblem.C:374
void addPostprocessor(const std::string &type, const std::string &name, InputParameters &parameters) override
Override of ExternalProblem::addPostprocessor.
Definition: MFEMProblem.C:481
NumericType _num_type
The numeric representation currently active for this problem.
Definition: MFEMProblem.h:381
void addFunction(const std::string &type, const std::string &name, InputParameters &parameters) override
Override of ExternalProblem::addFunction.
Definition: MFEMProblem.C:441
T & getMFEMObject(const std::string &system, const std::string &name, const THREAD_ID tid=0) const
Retrieve an MFEM object from the warehouse by system and name.
Definition: MFEMProblem.h:386
InputParameters addMFEMFESpaceFromMOOSEVariable(InputParameters &moosevar_params)
Method used to get an mfem FEC depending on the variable family specified in the input file...
Definition: MFEMProblem.C:512
static InputParameters validParams()
Return the input parameters used to construct an MFEM problem.
Definition: MFEMProblem.C:31
virtual MFEMMesh & mesh() override
Overwritten mesh() method from base MooseMesh to retrieve the correct mesh type, in this case MFEMMes...
Definition: MFEMProblem.C:631
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseBase.h:131
virtual void addVariable(const std::string &var_type, const std::string &var_name, InputParameters &parameters) override
Override of ExternalProblem::addVariable.
Definition: MFEMProblem.C:227
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void executeMFEMObjects(const ExecFlagType &exec_type)
Execute MFEM executed objects scheduled on the supplied execute flag.
Definition: MFEMProblem.C:674
std::string solverTypeString(unsigned int solver_sys_num) override
Return solver type as a human readable string.
Definition: MFEMProblem.C:719
Front-end class for creating and storing MFEM coefficients.
void displaceMesh()
Displace the mesh, if mesh displacement is enabled.
Definition: MFEMProblem.C:574
virtual void addAuxVariable(const std::string &var_type, const std::string &var_name, InputParameters &params)
Canonical method for adding an auxiliary variable.
MFEMProblemData _problem_data
Aggregated MFEM-side state for meshes, spaces, variables, coefficients, and solvers.
Definition: MFEMProblem.h:376
bool hRefine()
If AMR is enabled, request (and perform if needed) h-refinement.
Definition: MFEMProblem.h:322
bool hasMFEMObject(const std::string &system, const std::string &name) const
Determine whether an MFEM object with the supplied system and name exists.
Definition: MFEMProblem.C:739
Moose::MFEM::CoefficientManager coefficients
void rebalanceMesh(mfem::ParMesh &pmesh)
Rebalance the (necessarily nonconforming) mesh.
Definition: MFEMProblem.C:600
const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:103
TheWarehouse & theWarehouse() const
std::complex< Real > COMPLEX
T * Get(const std::string &field_name) const
Returns a non-owning pointer to the field. This is guaranteed to return a non-null pointer...
Moose::MFEM::CoefficientManager & getCoefficients()
Method to get the PropertyManager object for storing material properties and converting them to MFEM ...
Definition: MFEMProblem.h:257
void addMaterial(const std::string &material_name, const std::string &name, InputParameters &parameters) override
Definition: MFEMProblem.C:200
std::shared_ptr< T > GetShared(const std::string &field_name) const
Returns a shared pointer to the field. This is guaranteed to return a non-null shared pointer...
virtual void initialSetup() override
Definition: MFEMProblem.C:56
const std::string & type() const
Get the type of this class.
Definition: MooseBase.h:93
void setMesh()
Set the mesh used by MFEM.
Definition: MFEMProblem.C:78
virtual void addMFEMSolver(const std::string &user_object_name, const std::string &name, InputParameters &parameters)
Method called in AddMFEMSolverAction which will create the solver.
Definition: MFEMProblem.C:116
Moose::FEBackend feBackend() const override
Definition: MFEMProblem.h:305
void addAuxKernel(const std::string &kernel_name, const std::string &name, InputParameters &parameters) override
Override of ExternalProblem::addAuxKernel.
Definition: MFEMProblem.C:296
NumericType
Enumerates the supported numeric representations for MFEM variables and operators.
Definition: MFEMProblem.h:348
void addElementalFieldVariable(const std::string &, const std::string &, InputParameters &) override
Override of FEProblemBase::addElementalFieldVariable to be a no-op because we do not use the Marker/I...
Definition: MFEMProblem.h:124
std::shared_ptr< MFEMRefinementMarker > refiner
std::shared_ptr< mfem::ParComplexGridFunction > getComplexGridFunction(const std::string &name)
Definition: MFEMProblem.h:340
MPI_Comm getComm()
Return the MPI communicator associated with this FE problem&#39;s mesh.
Definition: MFEMProblem.h:273
virtual ~MFEMProblem()
Destroy the MFEM problem.
Definition: MFEMProblem.h:37
MFEMProblem(const InputParameters &params)
Construct an MFEM problem from the supplied parameters.
Definition: MFEMProblem.C:42
void addFunctorMaterial(const std::string &material_name, const std::string &name, InputParameters &parameters) override
Definition: MFEMProblem.C:207
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:18
void addFESpace(const std::string &type, const std::string &name, InputParameters &parameters)
Add an MFEM FESpace to the problem.
Definition: MFEMProblem.C:215
void updateGridFunctions()
Calls Update() on all gridfunctions.
Definition: MFEMProblem.C:618
void addIndicator(const std::string &type, const std::string &name, InputParameters &parameters) override
Override of FEProblemBase::addIndicator.
Definition: MFEMProblem.C:96
MFEMMesh inherits a MOOSE mesh class which allows us to work with other MOOSE objects.
Definition: MFEMMesh.h:20
virtual std::vector< VariableName > getAuxVariableNames()
Returns all the variable names from the auxiliary system base.
Definition: MFEMProblem.C:625
void setDevice()
Set the device to use to solve the FE problem.
Query query()
query creates and returns an initialized a query object for querying objects from the warehouse...
Definition: TheWarehouse.h:467
void addBoundaryCondition(const std::string &bc_name, const std::string &name, InputParameters &parameters) override
Definition: MFEMProblem.C:142
virtual MooseMesh & mesh() override
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:281
void updateFESpaces()
Calls Update() on all FE spaces.
Definition: MFEMProblem.C:611
void addImagComponentToKernel(const std::string &kernel_name, const std::string &name, InputParameters &parameters)
Adds an imaginary component kernel to the parent MFEMComplexKernel.
Definition: MFEMProblem.C:347
virtual void externalSolve() override
New interface for solving an External problem.
Definition: MFEMProblem.h:41
QueryCache & condition(Args &&... args)
Adds a new condition to the query.
Definition: TheWarehouse.h:285
void addVectorPostprocessor(const std::string &type, const std::string &name, InputParameters &parameters) override
Add a vector postprocessor and register its vectors with the MFEM execution system.
Definition: MFEMProblem.C:498
void addKernel(const std::string &kernel_name, const std::string &name, InputParameters &parameters) override
Override of ExternalProblem::addKernel.
Definition: MFEMProblem.C:304
NumericType getNumericType() const
Retrieve the numeric type of the problem.
Definition: MFEMProblem.h:357
std::optional< std::reference_wrapper< mfem::ParGridFunction const > > getMeshDisplacementGridFunction()
Returns optional reference to the displacement GridFunction to apply to nodes.
Definition: MFEMProblem.C:585
const mfem::ParMesh & getMFEMVariableMesh(std::string var_name)
Return the ParMesh associated with a particular variable.
Definition: MFEMProblem.h:278
Moose::MFEM::GridFunctions gridfunctions
void addAuxVariable(const std::string &var_type, const std::string &var_name, InputParameters &parameters) override
Override of ExternalProblem::addAuxVariable.
Definition: MFEMProblem.C:286
bool pRefine()
If AMR is enabled, request (and perform if needed) p-refinement.
Definition: MFEMProblem.h:327
void addRealComponentToKernel(const std::string &kernel_name, const std::string &name, InputParameters &parameters)
Adds a real component kernel to the parent MFEMComplexKernel.
Definition: MFEMProblem.C:335
void addSubMesh(const std::string &type, const std::string &name, InputParameters &parameters)
Add an MFEM SubMesh to the problem.
Definition: MFEMProblem.C:645
virtual void execute(const ExecFlagType &exec_type) override
Convenience function for performing execution of MOOSE systems.
Definition: MFEMProblem.C:69
void addMFEMNonlinearSolver(unsigned int nl_max_its, mfem::real_t nl_abs_tol, mfem::real_t nl_rel_tol, unsigned int print_level)
Add the nonlinear solver to the system.
Definition: MFEMProblem.C:125
unsigned int THREAD_ID
Definition: MooseTypes.h:237
void addTransfer(const std::string &transfer_name, const std::string &name, InputParameters &parameters) override
Add transfers between MultiApps and/or MFEM SubMeshes.
Definition: MFEMProblem.C:655
void addInitialCondition(const std::string &ic_name, const std::string &name, InputParameters &parameters) override
Add an MFEM initial condition to the problem.
Definition: MFEMProblem.C:666