https://mooseframework.inl.gov
MaterialFunctorConverter.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 
11 
12 #include "metaphysicl/raw_type.h"
13 
16 
17 template <typename T>
20 {
22  params.addClassDescription("Converts functor to non-AD and AD regular material properties");
23  params.addParam<std::vector<MooseFunctorName>>(
24  "functors_in", {}, "The names of the functors to convert to regular material properties");
25  params.addParam<std::vector<MaterialPropertyName>>(
26  "ad_props_out", {}, "The names of the output AD properties");
27  params.addParam<std::vector<MaterialPropertyName>>(
28  "reg_props_out", {}, "The names of the output regular properties");
29  return params;
30 }
31 
32 template <typename T>
34  : Material(parameters),
35  _num_functors_to_convert(getParam<std::vector<MooseFunctorName>>("functors_in").size())
36 {
37  const auto & functors_in = getParam<std::vector<MooseFunctorName>>("functors_in");
38  const auto & reg_props_out = getParam<std::vector<MaterialPropertyName>>("reg_props_out");
39  const auto & ad_props_out = getParam<std::vector<MaterialPropertyName>>("ad_props_out");
40 
41  if (reg_props_out.size() && ad_props_out.size())
42  paramError("reg_props_out",
43  "We dont support converting functors to both regular and AD "
44  "material properties in "
45  "a single instance of '",
46  type(),
47  "'. Please create two instances, one for regular and one for AD.");
48 
49  if ((reg_props_out.size() && (functors_in.size() != reg_props_out.size())) ||
50  (ad_props_out.size() && (functors_in.size() != ad_props_out.size())))
51  paramError(
52  "functors_in",
53  "The number of output properties must match the number of input functors, which is " +
54  std::to_string(functors_in.size()));
55 
57  _ad_props_out.resize(ad_props_out.size());
58  _reg_props_out.resize(reg_props_out.size());
59 
60  for (const auto i : make_range(_num_functors_to_convert))
62 
63  for (const auto i : index_range(ad_props_out))
64  _ad_props_out[i] = &declareADProperty<T>(ad_props_out[i]);
65 
66  for (const auto i : index_range(reg_props_out))
67  _reg_props_out[i] = &declareProperty<T>(reg_props_out[i]);
68 }
69 
70 template <typename T>
71 void
73 {
74  computeQpProperties();
75 }
76 
77 template <typename T>
78 void
80 {
81  // Using Qp 0 can leverage the functor caching
82  // TODO: Find a way to effectively use subdomain-constant-ness
83  unsigned int qp_used = (_constant_option == ConstantTypeEnum::NONE) ? _qp : 0;
84 
85  const auto state = Moose::currentState();
86  if (_bnd)
87  {
88  const Moose::ElemSideQpArg side_arg = {
89  _current_elem, _current_side, qp_used, _qrule, _q_point[_qp]};
90  for (const auto i : index_range(_ad_props_out))
91  (*_ad_props_out[i])[_qp] = (*_functors_in[i])(side_arg, state);
92 
93  for (const auto i : index_range(_reg_props_out))
94  (*_reg_props_out[i])[_qp] = MetaPhysicL::raw_value((*_functors_in[i])(side_arg, state));
95  }
96  else
97  {
98  const Elem * elem = _neighbor ? _current_elem->neighbor_ptr(_current_side) : _current_elem;
99  mooseAssert(elem, "We should have an element");
100  const Moose::ElemQpArg elem_arg = {elem, qp_used, _qrule, _q_point[_qp]};
101  for (const auto i : index_range(_ad_props_out))
102  (*_ad_props_out[i])[_qp] = (*_functors_in[i])(elem_arg, state);
103 
104  for (const auto i : index_range(_reg_props_out))
105  (*_reg_props_out[i])[_qp] = MetaPhysicL::raw_value((*_functors_in[i])(elem_arg, state));
106  }
107 }
108 
void initQpStatefulProperties() override
Initialize stateful properties at quadrature points.
MaterialFunctorConverterTempl(const InputParameters &parameters)
std::vector< const Moose::Functor< Moose::GenericType< T, true > > * > _functors_in
Incoming functors to convert. We up-convert non-AD functors to AD functors to ease implementation...
std::vector< ADMaterialProperty< T > * > _ad_props_out
AD material properties to create.
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition: MooseBase.h:435
virtual void computeQpProperties() override
Users must override this method.
auto raw_value(const Eigen::Map< T > &in)
Definition: EigenADReal.h:73
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
static InputParameters validParams()
typename std::conditional< is_ad, typename ADType< T >::type, T >::type GenericType
Definition: MooseTypes.h:645
std::vector< MaterialProperty< T > * > _reg_props_out
Regular material properties to create.
registerMooseObject("MooseApp", MaterialFunctorConverter)
static InputParameters validParams()
Definition: Material.C:14
const std::string & type() const
Get the type of this class.
Definition: MooseBase.h:89
Argument for requesting functor evaluation at a quadrature point location in an element.
Materials compute MaterialProperties.
Definition: Material.h:34
const Moose::Functor< T > & getFunctor(const std::string &name)
Retrieves a functor from the subproblem.
IntRange< T > make_range(T beg, T end)
This material converts functor to regular (AD or not) material properties.
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object...
StateArg currentState()
auto index_range(const T &sizable)
Argument for requesting functor evaluation at quadrature point locations on an element side...
const std::size_t _num_functors_to_convert
Number of material properties to create.