Skip to content

Commit

Permalink
Unit test all supported functor name type retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
lindsayad committed Oct 4, 2021
1 parent b83a1ed commit f3287fa
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 13 deletions.
8 changes: 8 additions & 0 deletions framework/include/interfaces/FunctorInterface.h
Expand Up @@ -36,6 +36,14 @@ class FunctorInterface

FunctorInterface(const MooseObject * moose_object);

/**
* helper to look up a functor name through the input parameter keys
* @param name The input parameter name that we are trying to deduce the functor name for
* @param params The input parameters object that we will be checking for parameters named \p name
* @return The functor name
*/
static std::string deduceFunctorName(const std::string & name, const InputParameters & params);

protected:
/**
* retrieves a functor from the subproblem. This method also leverages the ability to create
Expand Down
32 changes: 19 additions & 13 deletions framework/src/interfaces/FunctorInterface.C
Expand Up @@ -24,30 +24,36 @@ FunctorInterface::FunctorInterface(const MooseObject * const moose_object)
}

std::string
FunctorInterface::deduceFunctorName(const std::string & name) const
FunctorInterface::deduceFunctorName(const std::string & name, const InputParameters & params)
{
if (_fi_params.have_parameter<MooseFunctorName>(name))
return _fi_params.get<MooseFunctorName>(name);
if (params.have_parameter<MooseFunctorName>(name))
return params.get<MooseFunctorName>(name);
// variables, functor material properties, and functions are also functors
else if (_fi_params.have_parameter<MaterialPropertyName>(name))
return _fi_params.get<MaterialPropertyName>(name);
else if (_fi_params.have_parameter<VariableName>(name))
return _fi_params.get<VariableName>(name);
else if (_fi_params.have_parameter<std::vector<VariableName>>(name))
else if (params.have_parameter<MaterialPropertyName>(name))
return params.get<MaterialPropertyName>(name);
else if (params.have_parameter<VariableName>(name))
return params.get<VariableName>(name);
else if (params.have_parameter<std::vector<VariableName>>(name))
{
const auto & var_names = _fi_params.get<std::vector<VariableName>>(name);
const auto & var_names = params.get<std::vector<VariableName>>(name);
if (var_names.size() != 1)
mooseError("We only support a single variable name for retrieving a functor");
return var_names[0];
}
else if (_fi_params.have_parameter<NonlinearVariableName>(name))
return _fi_params.get<NonlinearVariableName>(name);
else if (_fi_params.have_parameter<FunctionName>(name))
return _fi_params.get<FunctionName>(name);
else if (params.have_parameter<NonlinearVariableName>(name))
return params.get<NonlinearVariableName>(name);
else if (params.have_parameter<FunctionName>(name))
return params.get<FunctionName>(name);
else
return name;
}

std::string
FunctorInterface::deduceFunctorName(const std::string & name) const
{
return deduceFunctorName(name, _fi_params);
}

template <>
const Moose::Functor<Real> *
FunctorInterface::defaultFunctor(const std::string & name)
Expand Down
52 changes: 52 additions & 0 deletions unit/src/FunctorInterfaceTest.C
@@ -0,0 +1,52 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#include "gtest/gtest.h"
#include "FunctorInterface.h"
#include "InputParameters.h"
#include "MooseTypes.h"

TEST(FunctorInterfaceTest, deduceFunctorName)
{
const auto base_params = emptyInputParameters();
{
auto params = base_params;
params.set<MooseFunctorName>("test") = "functor";
EXPECT_EQ(FunctorInterface::deduceFunctorName("test", params), "functor");
}
{
auto params = base_params;
params.set<MaterialPropertyName>("test") = "prop";
EXPECT_EQ(FunctorInterface::deduceFunctorName("test", params), "prop");
}
{
auto params = base_params;
params.set<VariableName>("test") = "var";
EXPECT_EQ(FunctorInterface::deduceFunctorName("test", params), "var");
}
{
auto params = base_params;
params.set<std::vector<VariableName>>("test") = {"vector_var"};
EXPECT_EQ(FunctorInterface::deduceFunctorName("test", params), "vector_var");
}
{
auto params = base_params;
params.set<NonlinearVariableName>("test") = "nl_var";
EXPECT_EQ(FunctorInterface::deduceFunctorName("test", params), "nl_var");
}
{
auto params = base_params;
params.set<FunctionName>("test") = "function";
EXPECT_EQ(FunctorInterface::deduceFunctorName("test", params), "function");
}
{
auto params = base_params;
EXPECT_EQ(FunctorInterface::deduceFunctorName("test", params), "test");
}
}

0 comments on commit f3287fa

Please sign in to comment.