Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Allow specifying initial conditions #62

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/ics/MFEMScalarIC.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "MFEMGeneralUserObject.h"

class MFEMScalarIC : public MFEMGeneralUserObject
{
public:
static InputParameters validParams();
MFEMScalarIC(const InputParameters & params);
virtual void execute() override;
};
14 changes: 14 additions & 0 deletions include/problem/MFEMProblem.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,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 @@ -219,6 +223,16 @@ class MFEMProblem : public ExternalProblem
std::optional<std::reference_wrapper<mfem::ParGridFunction const>>
getMeshDisplacementGridFunction();

/**
* @returns a shared pointer to an MFEM coefficient created using the [Coefficients] syntax
*/
std::shared_ptr<mfem::Coefficient> getCoefficient(const std::string & name);

/**
* @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
3 changes: 2 additions & 1 deletion src/base/PlatypusApp.C
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ associateSyntaxInner(Syntax & syntax, ActionFactory & /*action_factory*/)
addTaskDependency("add_material", "add_mfem_coefficients");
addTaskDependency("add_mfem_coefficients", "add_variable");
addTaskDependency("add_mfem_coefficients", "add_aux_variable");
addTaskDependency("add_mfem_coefficients", "add_ic");
addTaskDependency("add_ic", "add_mfem_coefficients");
addTaskDependency("add_mfem_coefficients", "add_function");

// add vector coefficients
registerMooseObjectTask("add_mfem_vector_coefficients", MFEMVectorCoefficient, false);
Expand Down
32 changes: 32 additions & 0 deletions src/ics/MFEMScalarIC.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "MFEMScalarIC.h"
#include "MFEMProblem.h"
#include <libmesh/libmesh_common.h>
#include <mfem.hpp>

registerMooseObject("PlatypusApp", MFEMScalarIC);

InputParameters
MFEMScalarIC::validParams()
{
auto params = MFEMGeneralUserObject::validParams();
params.addRequiredParam<std::string>("variable",
"The variable to apply the initial condition for");
params.addRequiredParam<std::string>("coefficient", "The scalar coefficient");
params.registerBase("InitialCondition");
// We cannot generally execute this at construction time since the coefficient may depend on, for
// instance, 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().getCoefficient(getParam<std::string>("coefficient"));
auto grid_function = getMFEMProblem().getGridFunction(getParam<std::string>("variable"));
grid_function->ProjectCoefficient(*coeff);
}
22 changes: 22 additions & 0 deletions src/problem/MFEMProblem.C
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "MFEMProblem.h"
#include "MFEMScalarIC.h"

#include <vector>
#include <algorithm>
Expand Down Expand Up @@ -421,3 +422,24 @@ MFEMProblem::mesh()
"Please choose the MFEMMesh mesh type for an MFEMProblem\n");
return (MFEMMesh &)_mesh;
}

std::shared_ptr<mfem::Coefficient>
MFEMProblem::getCoefficient(const std::string & name)
{
return getUserObject<MFEMCoefficient>(name).getCoefficient();
}

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
}
26 changes: 17 additions & 9 deletions test/tests/kernels/diffusion.i
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@
[]
[]

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

[Functions]
[value_bottom]
[one]
type = ParsedFunction
expression = 1.0
[]
[value_top]
[zero]
type = ParsedFunction
expression = 0.0
[]
Expand All @@ -39,13 +47,13 @@
type = MFEMScalarDirichletBC
variable = diffused
boundary = '1'
coefficient = BottomValue
coefficient = one
[]
[low_terminal]
type = MFEMScalarDirichletBC
variable = diffused
boundary = '2'
coefficient = TopValue
coefficient = zero
[]
[]

Expand All @@ -58,13 +66,13 @@
[]

[Coefficients]
[TopValue]
[zero]
type = MFEMFunctionCoefficient
function = value_top
function = zero
[]
[BottomValue]
[one]
type = MFEMFunctionCoefficient
function = value_bottom
function = one
[]
[]

Expand All @@ -86,7 +94,7 @@
type = MFEMHypreGMRES
preconditioner = boomeramg
l_tol = 1e-16
l_max_its = 1000
l_max_its = 1000
[]

[Executioner]
Expand Down