Skip to content

Commit

Permalink
Some MFEM development in support of traditional MOOSE use
Browse files Browse the repository at this point in the history
- Allow specifying initial conditions for MFEM based simulations
- Add support for properties restricted to block names
  • Loading branch information
lindsayad committed Jan 9, 2025
1 parent a8b8940 commit fe8f147
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 6 deletions.
15 changes: 15 additions & 0 deletions framework/include/mfem/ics/MFEMScalarIC.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifdef MFEM_ENABLED

#pragma once

#include "MFEMGeneralUserObject.h"

class MFEMScalarIC : public MFEMGeneralUserObject
{
public:
static InputParameters validParams();
MFEMScalarIC(const InputParameters & params);
virtual void execute() override;
};

#endif
3 changes: 2 additions & 1 deletion framework/include/mfem/materials/MFEMMaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class MFEMMaterial : public MFEMGeneralUserObject
{
public:
static InputParameters validParams();
static std::vector<std::string> subdomainsToStrings(std::vector<SubdomainName> blocks);
static libMesh::Point pointFromMFEMVector(const mfem::Vector & vec);

MFEMMaterial(const InputParameters & parameters);
Expand All @@ -21,6 +20,8 @@ class MFEMMaterial : public MFEMGeneralUserObject
const std::vector<SubdomainName> & getBlocks() const { return _block_ids; }

protected:
std::vector<std::string> subdomainsToStrings(std::vector<SubdomainName> blocks);

std::vector<SubdomainName> _block_ids;
platypus::PropertyManager & _properties;
};
Expand Down
9 changes: 9 additions & 0 deletions framework/include/mfem/problem/MFEMProblem.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ class MFEMProblem : public ExternalProblem
const std::string & name,
InputParameters & parameters) override;

void addInitialCondition(const std::string & ic_name,
const std::string & name,
InputParameters & parameters) override;

/**
* Method called in AddMFEMPreconditionerAction which will create the solver.
*/
Expand Down Expand Up @@ -196,6 +200,11 @@ class MFEMProblem : public ExternalProblem
std::optional<std::reference_wrapper<mfem::ParGridFunction const>>
getMeshDisplacementGridFunction();

/**
* @returns a shared pointer to an MFEM parallel grid function
*/
std::shared_ptr<mfem::ParGridFunction> getGridFunction(const std::string & name);

protected:
/**
* Template method for adding kernels. We can only add kernels using equation system problem
Expand Down
36 changes: 36 additions & 0 deletions framework/src/mfem/ics/MFEMScalarIC.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifdef MFEM_ENABLED

#include "MFEMScalarIC.h"
#include "MFEMProblem.h"
#include <libmesh/libmesh_common.h>
#include <mfem.hpp>

registerMooseObject("MooseApp", MFEMScalarIC);

InputParameters
MFEMScalarIC::validParams()
{
auto params = MFEMGeneralUserObject::validParams();
params.addRequiredParam<std::string>("variable",
"The variable to apply the initial condition for");
params.addRequiredParam<FunctionName>("coefficient", "The scalar coefficient");
params.registerBase("InitialCondition");
// We cannot generally execute this at construction time since the coefficient may be based on a
// MOOSE function which is not itself setup until its initialSetup is called. UserObject initial
// execution occurs after function initialSetup
params.set<ExecFlagEnum>("execute_on") = {EXEC_INITIAL};
params.suppressParameter<ExecFlagEnum>("execute_on");
return params;
}

MFEMScalarIC::MFEMScalarIC(const InputParameters & params) : MFEMGeneralUserObject(params) {}

void
MFEMScalarIC::execute()
{
auto coeff = getMFEMProblem().getScalarFunctionCoefficient(getParam<FunctionName>("coefficient"));
auto grid_function = getMFEMProblem().getGridFunction(getParam<std::string>("variable"));
grid_function->ProjectCoefficient(*coeff);
}

#endif
28 changes: 23 additions & 5 deletions framework/src/mfem/materials/MFEMMaterial.C
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,31 @@ std::vector<std::string>
MFEMMaterial::subdomainsToStrings(std::vector<SubdomainName> blocks)
{
std::vector<std::string> result(blocks.size());
auto & mesh = getMFEMProblem().mesh().getMFEMParMesh();
// FIXME: Is there really no better way to do this conversion? It doesn't seem like it should be
// necessary to do the various copies etc. for this.
std::transform(blocks.begin(),
blocks.end(),
result.begin(),
// FIXME: How do I pass the string constructor directly?
[](const SubdomainName & x) -> std::string { return std::string(x); });
std::transform(
blocks.begin(),
blocks.end(),
result.begin(),
// FIXME: How do I pass the string constructor directly?
[this, &mesh](const SubdomainName & x) -> std::string
{
try
{
// Is this a block ID?
std::stoi(x);
return std::string(x);
}
catch (...)
{
// It was not
auto & block_ids = mesh.attribute_sets.GetAttributeSet(x);
if (block_ids.Size() != 1)
this->mooseError("There should be a 1-to-1 correspondence between block name and block ID");
return std::to_string(block_ids[0]);
}
});
return result;
}

Expand Down
16 changes: 16 additions & 0 deletions framework/src/mfem/problem/MFEMProblem.C
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifdef MFEM_ENABLED

#include "MFEMProblem.h"
#include "MFEMScalarIC.h"

#include <vector>
#include <algorithm>
Expand Down Expand Up @@ -383,4 +384,19 @@ MFEMProblem::mesh()
return (MFEMMesh &)_mesh;
}

std::shared_ptr<mfem::ParGridFunction>
MFEMProblem::getGridFunction(const std::string & name)
{
return getUserObject<MFEMVariable>(name).getGridFunction();
}

void
MFEMProblem::addInitialCondition(const std::string & ic_name,
const std::string & name,
InputParameters & parameters)
{
FEProblemBase::addUserObject(ic_name, name, parameters);
getUserObject<MFEMScalarIC>(name); // error check
}

#endif
16 changes: 16 additions & 0 deletions test/tests/mfem/kernels/diffusion.i
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@
[]
[]

[ICs]
[diffused_ic]
type = MFEMScalarIC
coefficient = one
variable = diffused
[]
[]

[Functions]
[one]
type = ParsedFunction
expression = 1.0
[]
[]

[BCs]
[bottom]
type = MFEMScalarDirichletBC
Expand All @@ -43,6 +58,7 @@
type = MFEMGenericConstantMaterial
prop_names = diffusivity
prop_values = 1.0
block = 'the_domain'
[]
[]

Expand Down
Binary file modified test/tests/mfem/kernels/gold/mug.e
Binary file not shown.
Binary file modified unit/data/mug.e
Binary file not shown.

0 comments on commit fe8f147

Please sign in to comment.