https://mooseframework.inl.gov
Loading...
Searching...
No Matches
FunctorInterface.C
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#include "FunctorInterface.h"
11#include "MooseFunctor.h"
12
18
20 : _fi_params(moose_object->parameters()),
21 _fi_name(moose_object->name()),
22 _fi_subproblem(_fi_params.get<SubProblem *>("_subproblem")),
23 _fi_tid(_fi_params.get<THREAD_ID>("_tid"))
24{
25}
26
27#ifdef MOOSE_KOKKOS_ENABLED
30 : _fi_params(object._fi_params),
31 _fi_name(object._fi_name),
32 _fi_subproblem(object._fi_subproblem),
33 _fi_tid(object._fi_tid)
34{
35}
36#endif
37
38std::string
39FunctorInterface::deduceFunctorName(const std::string & name, const InputParameters & params)
40{
41 if (params.isParamValid(name))
42 {
43 if (params.have_parameter<MooseFunctorName>(name))
44 return params.get<MooseFunctorName>(name);
45 // variables, functor material properties, functions, and post-processors are also functors
46 else if (params.have_parameter<MaterialPropertyName>(name))
47 return params.get<MaterialPropertyName>(name);
48 else if (params.have_parameter<VariableName>(name))
49 return params.get<VariableName>(name);
50 else if (params.have_parameter<std::vector<VariableName>>(name))
51 {
52 const auto & var_names = params.get<std::vector<VariableName>>(name);
53 if (var_names.size() != 1)
54 mooseError("We only support a single variable name for retrieving a functor");
55 return var_names[0];
56 }
57 else if (params.have_parameter<NonlinearVariableName>(name))
58 return params.get<NonlinearVariableName>(name);
59 else if (params.have_parameter<FunctionName>(name))
60 return params.get<FunctionName>(name);
61 else if (params.have_parameter<PostprocessorName>(name))
62 return params.get<PostprocessorName>(name);
63 else
64 mooseError("Invalid parameter type for retrieving a functor");
65 }
66 else
67 return name;
68}
69
70std::string
71FunctorInterface::deduceFunctorName(const std::string & name) const
72{
73 return deduceFunctorName(name, _fi_params);
74}
75
76template <>
78FunctorInterface::defaultFunctor(const std::string & name)
79{
80 std::istringstream ss(name);
81 Real real_value;
82
83 // check if the string parsed cleanly into a Real number
84 if (ss >> real_value && ss.eof())
85 {
86 _default_real_functors.emplace_back(std::make_unique<Moose::Functor<Real>>(
87 std::make_unique<Moose::ConstantFunctor<Real>>(real_value)));
88 auto & default_property = _default_real_functors.back();
89 return default_property.get();
90 }
91
92 return nullptr;
93}
94
95template <>
97FunctorInterface::defaultFunctor(const std::string & name)
98{
99 std::istringstream ss(name);
100 Real real_value;
101
102 // check if the string parsed cleanly into a Real number
103 if (ss >> real_value && ss.eof())
104 {
105 _default_ad_real_functors.emplace_back(std::make_unique<Moose::Functor<ADReal>>(
106 std::make_unique<Moose::ConstantFunctor<ADReal>>(real_value)));
107 auto & default_property = _default_ad_real_functors.back();
108 return default_property.get();
109 }
110
111 return nullptr;
112}
113
114bool
115FunctorInterface::isFunctor(const std::string & name, const SubProblem & subproblem) const
116{
117 // Check if the supplied parameter is a valid input parameter key
118 std::string functor_name = deduceFunctorName(name);
119
120 return subproblem.hasFunctor(functor_name, _fi_tid);
121}
122
123bool
124FunctorInterface::isFunctor(const std::string & name) const
125{
126 mooseAssert(_fi_subproblem, "This must be non-null");
127 return isFunctor(name, *_fi_subproblem);
128}
129
131FunctorInterface::makeElemArg(const Elem * const elem, const bool correct_skewness) const
132{
133 return {elem, correct_skewness};
134}
InputParameters emptyInputParameters()
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
unsigned int THREAD_ID
Definition MooseTypes.h:237
An interface for accessing Moose::Functors.
const Moose::Functor< T > * defaultFunctor(const std::string &name)
Helper function to parse default functor values.
const THREAD_ID _fi_tid
Current threaded it.
static std::string deduceFunctorName(const std::string &name, const InputParameters &params)
Helper to look up a functor name through the input parameter keys.
std::vector< std::unique_ptr< Moose::Functor< ADReal > > > _default_ad_real_functors
Storage vector for Moose::Functor<ADReal> default objects.
Moose::ElemArg makeElemArg(const Elem *elem, bool correct_skewnewss=false) const
Helper method to create an elemental argument for a functor that includes whether to perform skewness...
static InputParameters validParams()
const InputParameters & _fi_params
Parameters of the object with this interface.
SubProblem *const _fi_subproblem
Pointer to subproblem if the subproblem pointer parameter was set.
bool isFunctor(const std::string &name) const
Checks the subproblem for the given functor.
std::vector< std::unique_ptr< Moose::Functor< Real > > > _default_real_functors
Storage vector for Moose::Functor<Real> default objects.
FunctorInterface(const MooseObject *moose_object)
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
bool have_parameter(std::string_view name) const
A wrapper around the Parameters base class method.
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another,...
Every object that can be built by the factory should be derived from this class.
Definition MooseObject.h:31
Class template for creating constant functors.
This is a wrapper that forwards calls to the implementation, which can be switched out at any time wi...
Generic class for solving transient nonlinear problems.
Definition SubProblem.h:79
bool hasFunctor(const std::string &name, const THREAD_ID tid) const
checks whether we have a functor corresponding to name on the thread id tid
A structure that is used to evaluate Moose functors logically at an element/cell center.