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 
21 #include <map>
22 
23 namespace Moose::MFEM
24 {
25 struct SolutionState;
26 }
27 
29 {
30 public:
35 
39  MFEMProblem(const InputParameters & params);
40 
44  virtual ~MFEMProblem() {}
45 
46  virtual void initialSetup() override;
47  virtual void execute(const ExecFlagType & exec_type) override;
48  virtual void externalSolve() override {}
49  virtual void syncSolutions(Direction) override {}
50 
55  virtual MFEMMesh & mesh() override;
56  virtual const MFEMMesh & mesh() const override;
58 
63  virtual std::vector<VariableName> getAuxVariableNames();
64 
65  void addBoundaryCondition(const std::string & bc_name,
66  const std::string & name,
67  InputParameters & parameters) override;
68 
69  void addMaterial(const std::string & material_name,
70  const std::string & name,
71  InputParameters & parameters) override;
72 
73  void addFunctorMaterial(const std::string & material_name,
74  const std::string & name,
75  InputParameters & parameters) override;
76 
80  void addFESpace(const std::string & type, const std::string & name, InputParameters & parameters);
81 
85  void addFESpaceHierarchy(const std::string & type,
86  const std::string & name,
88 
92  void setMesh();
93 
97  void addSubMesh(const std::string & type, const std::string & name, InputParameters & parameters);
98 
102  void addQuadratureFunction(const std::string & type,
103  const std::string & name,
105 
109  void addTransfer(const std::string & transfer_name,
110  const std::string & name,
111  InputParameters & parameters) override;
116  virtual void addVariable(const std::string & var_type,
117  const std::string & var_name,
118  InputParameters & parameters) override;
119 
123  void addGridFunction(const std::string & var_type,
124  const std::string & var_name,
126 
132  void addAuxVariable(const std::string & var_type,
133  const std::string & var_name,
134  InputParameters & parameters) override;
135 
140  void
141  addElementalFieldVariable(const std::string &, const std::string &, InputParameters &) override
142  {
143  }
144 
149  void addKernel(const std::string & kernel_name,
150  const std::string & name,
151  InputParameters & parameters) override;
152 
156  void addRealComponentToKernel(const std::string & kernel_name,
157  const std::string & name,
159 
163  void addImagComponentToKernel(const std::string & kernel_name,
164  const std::string & name,
166 
170  void addRealComponentToBC(const std::string & kernel_name,
171  const std::string & name,
173 
177  void addImagComponentToBC(const std::string & kernel_name,
178  const std::string & name,
180 
184  void addAuxKernel(const std::string & kernel_name,
185  const std::string & name,
186  InputParameters & parameters) override;
187 
192  void addFunction(const std::string & type,
193  const std::string & name,
194  InputParameters & parameters) override;
195 
199  void addInitialCondition(const std::string & ic_name,
200  const std::string & name,
201  InputParameters & parameters) override;
202 
208  void addPostprocessor(const std::string & type,
209  const std::string & name,
210  InputParameters & parameters) override;
211 
215  void addVectorPostprocessor(const std::string & type,
216  const std::string & name,
217  InputParameters & parameters) override;
218 
223  void addIndicator(const std::string & type,
224  const std::string & name,
225  InputParameters & parameters) override;
226 
231  void addMarker(const std::string & type,
232  const std::string & name,
233  InputParameters & parameters) override;
234 
239  virtual void addMFEMSolver(const std::string & user_object_name,
240  const std::string & name,
242 
246  virtual void resolveMFEMSolvers();
247 
251  void executeMFEMObjects(const ExecFlagType & exec_type);
252 
259 
266 
272 
276  const MFEMProblemData & getProblemData() const { return _problem_data; }
277 
281  MPI_Comm getComm() { return getProblemData().comm; }
282 
286  const mfem::ParMesh & getMFEMVariableMesh(std::string var_name)
287  {
288  if (_problem_data.gridfunctions.Has(var_name))
289  return *_problem_data.gridfunctions.Get(var_name)->ParFESpace()->GetParMesh();
290  else if (_problem_data.cmplx_gridfunctions.Has(var_name))
291  return *_problem_data.cmplx_gridfunctions.Get(var_name)->ParFESpace()->GetParMesh();
292  else
293  mooseError("Variable " + var_name +
294  " not found in MFEMProblem real or complex gridfunctions.");
295  }
296 
300  void displaceMesh();
301 
305  void rebalanceMesh(mfem::ParMesh & pmesh);
306 
310  std::optional<std::reference_wrapper<mfem::ParGridFunction const>>
312 
313  Moose::FEBackend feBackend() const override { return Moose::FEBackend::MFEM; }
314 
315  std::string solverTypeString(unsigned int solver_sys_num) override;
316 
320  void updateFESpaces();
321 
325  void updateGridFunctions();
326 
330  bool hRefine() { return _problem_data.refiner && _problem_data.refiner->hRefine(); }
331 
335  bool pRefine() { return _problem_data.refiner && _problem_data.refiner->pRefine(); }
336 
340  std::shared_ptr<mfem::ParGridFunction> getGridFunction(const std::string & name)
341  {
343  }
344 
348  std::shared_ptr<mfem::ParComplexGridFunction> getComplexGridFunction(const std::string & name)
349  {
351  }
352 
356  enum class NumericType
357  {
358  REAL,
359  COMPLEX
360  };
361 
366 
370  template <typename T>
371  T & getMFEMObject(const std::string & system,
372  const std::string & name,
373  const THREAD_ID tid = 0) const;
374 
378  bool hasMFEMObject(const std::string & system, const std::string & name) const;
379 
380 protected:
384  void validateVariableNumericType(const std::string & var_type,
385  const std::string & var_name) const;
386 
388  {
389  std::string type;
391  bool referenced = false;
392  };
393 
398 
403 
410  std::map<std::string, MFEMSolverDefinition> _mfem_solver_definitions;
411 
414 };
415 
416 template <typename T>
417 T &
418 MFEMProblem::getMFEMObject(const std::string & system,
419  const std::string & name,
420  const THREAD_ID tid) const
421 {
422  std::vector<T *> objs;
423  theWarehouse()
424  .query()
425  .condition<AttribSystem>(system)
426  .condition<AttribThread>(tid)
427  .condition<AttribName>(name)
428  .queryInto(objs);
429  if (objs.empty())
430  mooseError("Unable to find MFEM object with system '" + system + "' and name '" + name + "'");
431  mooseAssert(objs.size() == 1, "Shouldn't find more than one object with given system and name");
432  return *(objs[0]);
433 }
434 
435 #endif
std::map< std::string, MFEMSolverDefinition > _mfem_solver_definitions
Solver definitions recorded by AddMFEMSolverAction before the dependency resolver constructs them...
Definition: MFEMProblem.h:410
virtual void resolveMFEMSolvers()
Construct recorded MFEM solvers in dependency order and select the problem driver solver(s)...
Definition: MFEMProblem.C:157
std::shared_ptr< mfem::ParGridFunction > getGridFunction(const std::string &name)
Definition: MFEMProblem.h:340
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:392
void addMarker(const std::string &type, const std::string &name, InputParameters &parameters) override
Override of FEProblemBase::addMarker.
Definition: MFEMProblem.C:140
virtual void syncSolutions(Direction) override
Method to transfer data to/from the external application to the associated transfer mesh...
Definition: MFEMProblem.h:49
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:505
Moose::MFEM::ComplexGridFunctions cmplx_gridfunctions
const MFEMProblemData & getProblemData() const
Return the current MFEM problem data in a const context.
Definition: MFEMProblem.h:276
MFEMProblemData & getProblemData()
Method to get the current MFEMProblemData object storing the current data specifying the FE problem...
Definition: MFEMProblem.h:271
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:520
void addPostprocessor(const std::string &type, const std::string &name, InputParameters &parameters) override
Override of ExternalProblem::addPostprocessor.
Definition: MFEMProblem.C:627
Moose::MFEM::SolutionState & _solution_state_data
Restartable MFEM solution state associated with this problem.
Definition: MFEMProblem.h:413
NumericType _num_type
The numeric representation currently active for this problem.
Definition: MFEMProblem.h:402
void addFunction(const std::string &type, const std::string &name, InputParameters &parameters) override
Override of ExternalProblem::addFunction.
Definition: MFEMProblem.C:587
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:418
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:658
static InputParameters validParams()
Return the input parameters used to construct an MFEM problem.
Definition: MFEMProblem.C:60
virtual MFEMMesh & mesh() override
Overwritten mesh() method from base MooseMesh to retrieve the correct mesh type, in this case MFEMMes...
Definition: MFEMProblem.C:777
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:372
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:829
std::string solverTypeString(unsigned int solver_sys_num) override
Return solver type as a human readable string.
Definition: MFEMProblem.C:874
Front-end class for creating and storing MFEM coefficients.
void displaceMesh()
Displace the mesh, if mesh displacement is enabled.
Definition: MFEMProblem.C:720
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:397
bool hRefine()
If AMR is enabled, request (and perform if needed) h-refinement.
Definition: MFEMProblem.h:330
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:894
Moose::MFEM::CoefficientManager coefficients
void rebalanceMesh(mfem::ParMesh &pmesh)
Rebalance the (necessarily nonconforming) mesh.
Definition: MFEMProblem.C:746
const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:103
TheWarehouse & theWarehouse() const
void addQuadratureFunction(const std::string &type, const std::string &name, InputParameters &parameters)
Add an MFEM QuadratureFunction-backed coefficient to the problem.
Definition: MFEMProblem.C:801
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:265
void addMaterial(const std::string &material_name, const std::string &name, InputParameters &parameters) override
Definition: MFEMProblem.C:302
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:88
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:119
virtual void addMFEMSolver(const std::string &user_object_name, const std::string &name, InputParameters &parameters)
Method called in AddMFEMSolverAction which records a solver for later dependency-ordered construction...
Definition: MFEMProblem.C:148
Moose::FEBackend feBackend() const override
Definition: MFEMProblem.h:313
void addAuxKernel(const std::string &kernel_name, const std::string &name, InputParameters &parameters) override
Override of ExternalProblem::addAuxKernel.
Definition: MFEMProblem.C:442
NumericType
Enumerates the supported numeric representations for MFEM variables and operators.
Definition: MFEMProblem.h:356
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:141
void addFESpaceHierarchy(const std::string &type, const std::string &name, InputParameters &parameters)
Add an MFEMFESpaceHierarchy to the problem.
Definition: MFEMProblem.C:335
std::shared_ptr< MFEMRefinementMarker > refiner
std::shared_ptr< mfem::ParComplexGridFunction > getComplexGridFunction(const std::string &name)
Definition: MFEMProblem.h:348
MPI_Comm getComm()
Return the MPI communicator associated with this FE problem&#39;s mesh.
Definition: MFEMProblem.h:281
virtual ~MFEMProblem()
Destroy the MFEM problem.
Definition: MFEMProblem.h:44
MFEMProblem(const InputParameters &params)
Construct an MFEM problem from the supplied parameters.
Definition: MFEMProblem.C:71
void addFunctorMaterial(const std::string &material_name, const std::string &name, InputParameters &parameters) override
Definition: MFEMProblem.C:309
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:317
void updateGridFunctions()
Calls Update() on all gridfunctions.
Definition: MFEMProblem.C:764
void addIndicator(const std::string &type, const std::string &name, InputParameters &parameters) override
Override of FEProblemBase::addIndicator.
Definition: MFEMProblem.C:129
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:771
SolutionState
Definition: MooseTypes.h:261
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:244
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:271
void updateFESpaces()
Calls Update() on all FE spaces.
Definition: MFEMProblem.C:757
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:493
Utilities for converting between vector(s) of libMesh Points and MFEM Vector(s).
virtual void externalSolve() override
New interface for solving an External problem.
Definition: MFEMProblem.h:48
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:644
void addKernel(const std::string &kernel_name, const std::string &name, InputParameters &parameters) override
Override of ExternalProblem::addKernel.
Definition: MFEMProblem.C:450
void validateVariableNumericType(const std::string &var_type, const std::string &var_name) const
Verify that a primary variable&#39;s numeric type matches the problem&#39;s equation system.
Definition: MFEMProblem.C:358
NumericType getNumericType() const
Retrieve the numeric type of the problem.
Definition: MFEMProblem.h:365
std::optional< std::reference_wrapper< mfem::ParGridFunction const > > getMeshDisplacementGridFunction()
Returns optional reference to the displacement GridFunction to apply to nodes.
Definition: MFEMProblem.C:731
const mfem::ParMesh & getMFEMVariableMesh(std::string var_name)
Return the ParMesh associated with a particular variable.
Definition: MFEMProblem.h:286
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:432
bool pRefine()
If AMR is enabled, request (and perform if needed) p-refinement.
Definition: MFEMProblem.h:335
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:481
void addSubMesh(const std::string &type, const std::string &name, InputParameters &parameters)
Add an MFEM SubMesh to the problem.
Definition: MFEMProblem.C:791
virtual void execute(const ExecFlagType &exec_type) override
Convenience function for performing execution of MOOSE systems.
Definition: MFEMProblem.C:110
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:810
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:821