Skip to content

Commit

Permalink
Add unit tests for getting MFEM coefficients for functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cmacmackin committed Oct 23, 2024
1 parent 8fd7148 commit cd2f2e8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/problem/MFEMProblem.C
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,9 @@ MFEMProblem::addFunction(const std::string & type,
}
else
{
mooseWarning("Could not identify whether function time ",
mooseWarning("Could not identify whether function ",
type,
"is scalar or vector; no MFEM coefficient object created.");
" is scalar or vector; no MFEM coefficient object created.");
}
}

Expand Down
78 changes: 78 additions & 0 deletions unit/src/FunctionTest.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "MFEMObjectUnitTest.h"

class FunctionTest : public MFEMObjectUnitTest
{
public:
mfem::IsoparametricTransformation fe_transform;
mfem::IntegrationPoint point;
FunctionTest() : MFEMObjectUnitTest("PlatypusApp")
{
point.Init(3);
point.Set3(0., 0., 0.);
fe_transform.SetIdentityTransformation(mfem::Geometry::CUBE);
}
};

/**
* Test MFEMProblem::getScalarFunctionCoefficient works as it should.
*/
TEST_F(FunctionTest, GetScalarFunctionCoefficient)
{
// Build required kernel inputs
InputParameters func_params1 = _factory.getValidParams("ParsedFunction");
func_params1.set<std::string>("expression") = "1.";
_mfem_problem->addFunction("ParsedFunction", "coef1", func_params1);
_mfem_problem->getFunction("coef1").initialSetup();
std::shared_ptr<mfem::Coefficient> coef = _mfem_problem->getScalarFunctionCoefficient("coef1");
EXPECT_EQ(coef->Eval(fe_transform, point), 1);

EXPECT_THROW(_mfem_problem->getVectorFunctionCoefficient("coef1"), std::runtime_error);
EXPECT_THROW(_mfem_problem->getScalarFunctionCoefficient("coef2"), std::runtime_error);
}

/**
* Test MFEMProblem::getVectorFunctionCoefficient works as it should.
*/
TEST_F(FunctionTest, GetVectorFunctionCoefficient)
{
// Build required kernel inputs
InputParameters func_params1 = _factory.getValidParams("ParsedVectorFunction");
func_params1.set<std::string>("expression_x") = "1.";
func_params1.set<std::string>("expression_y") = "2.";
func_params1.set<std::string>("expression_z") = "3.";
_mfem_problem->addFunction("ParsedVectorFunction", "vec_coef1", func_params1);
_mfem_problem->getFunction("vec_coef1").initialSetup();
std::shared_ptr<mfem::VectorCoefficient> coef =
_mfem_problem->getVectorFunctionCoefficient("vec_coef1");
mfem::Vector vec;
coef->Eval(vec, fe_transform, point);
EXPECT_EQ(vec[0], 1.);
EXPECT_EQ(vec[1], 2.);
EXPECT_EQ(vec[2], 3.);

EXPECT_THROW(_mfem_problem->getVectorFunctionCoefficient("vec_coef2"), std::runtime_error);
EXPECT_THROW(_mfem_problem->getScalarFunctionCoefficient("vec_coef1"), std::runtime_error);
}

/**
* Test MFEMProblem::addFunction when unkown function type is used.
*/
TEST_F(FunctionTest, AddUnknownFunction)
{
InputParameters func_params1 = _factory.getValidParams("ParsedFunction");
func_params1.set<std::string>("expression") = "1.";
_mfem_problem->addFunction("ParsedFunction", "coef1", func_params1);
InputParameters func_params2 = _factory.getValidParams("ParsedFunction");
func_params2.set<std::string>("expression") = "x*y";
_mfem_problem->addFunction("ParsedFunction", "coef2", func_params2);
InputParameters func_params3 = _factory.getValidParams("LinearCombinationFunction");
func_params3.set<std::vector<FunctionName>>("functions") = {"coef1", "coef2"};
func_params3.set<std::vector<double>>("w") = {1., 2.};
_mfem_problem->addFunction("LinearCombinationFunction", "coef3", func_params3);
_mfem_problem->getFunction("coef1").initialSetup();
_mfem_problem->getFunction("coef2").initialSetup();
_mfem_problem->getFunction("coef3").initialSetup();

EXPECT_THROW(_mfem_problem->getScalarFunctionCoefficient("coef3"), std::runtime_error);
EXPECT_THROW(_mfem_problem->getVectorFunctionCoefficient("coef3"), std::runtime_error);
}

0 comments on commit cd2f2e8

Please sign in to comment.