https://mooseframework.inl.gov
CompileTimeDerivativesMaterial.h
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 #pragma once
11 
12 #include "Material.h"
13 #include "CompileTimeDerivatives.h"
16 
25 template <int N, bool is_ad = false, int MaxD = 3>
27 {
28 public:
30  const std::array<std::string, N> variables);
32 
33 protected:
39  template <typename T>
40  void evaluate(const T & F);
41 
42  const MaterialPropertyName _F_name;
44 
48  std::array<VariableName, N> _var_name;
49 
53  std::array<GenericMaterialProperty<Real, is_ad> *,
54  CompileTimeDerivativesMaterialInternal::total_derivatives<MaxD, N>()>
56 
64  GenericVariableValue<is_ad>>(std::make_index_sequence<N>{}))::type _refs;
65 
66 private:
67  template <std::size_t... Ns>
68  auto makeRefsTuple(const std::array<std::string, N> & variables,
69  const unsigned int & qp,
70  std::index_sequence<Ns...>)
71  {
72  return std::make_tuple(
73  CompileTimeDerivatives::makeRef<Ns>(coupledGenericValue<is_ad>(variables[Ns]), qp)...);
74  }
75 
76  template <std::size_t I>
77  constexpr void loopDeclareProperties(std::index_sequence<>)
78  {
79  }
80  template <std::size_t I, std::size_t first, std::size_t... tail>
81  constexpr void loopDeclareProperties(std::index_sequence<first, tail...> int_seq)
82  {
84  if constexpr (is_sorted(int_seq))
85  {
86  _prop_dF[I] = &declarePropertyDerivative<Real, is_ad>(
87  _F_name, std::vector<VariableName>{_var_name[first], _var_name[tail]...});
88  loopDeclareProperties<I + 1>(details::increment<N>(int_seq));
89  }
90  else
91  loopDeclareProperties<I>(details::increment<N>(int_seq));
92  }
93 
98  {
100  loopDeclareProperties<0>(zeroes<MaxD>());
101  }
102 
103  template <std::size_t I, typename T>
104  constexpr void loopEvaluate(const T &, std::index_sequence<>)
105  {
106  }
107  template <std::size_t I, typename T, std::size_t first, std::size_t... tail>
108  constexpr void loopEvaluate(const T & expression, std::index_sequence<first, tail...> int_seq)
109  {
111  if constexpr (is_sorted(int_seq))
112  {
113  (*_prop_dF[I])[_qp] = take_derivatives(expression, int_seq)();
114  loopEvaluate<I + 1>(expression, details::increment<N>(int_seq));
115  }
116  else
117  loopEvaluate<I>(expression, details::increment<N>(int_seq));
118  }
119 };
120 
121 template <int N, bool is_ad, int MaxD>
124 {
126  params.addRequiredParam<MaterialPropertyName>("property_name",
127  "Name of the parsed material property");
128  return params;
129 }
130 
131 template <int N, bool is_ad, int MaxD>
133  const InputParameters & params, const std::array<std::string, N> variables)
135  _F_name(getParam<MaterialPropertyName>("property_name")),
137  _refs(makeRefsTuple(variables, _qp, std::make_index_sequence<N>{}))
138 {
139  // get the names of all coupled variables
140  for (const auto i : make_range(N))
141  _var_name[i] = coupledName(variables[i]);
142 
143  // declare all material properties
144  declareProperties();
145 }
146 
147 template <int N, bool is_ad, int MaxD>
148 template <typename T>
149 void
151 {
152  _prop_F[_qp] = F();
154  loopEvaluate<0>(F, zeroes<MaxD>());
155 }
GenericMaterialProperty< Real, is_ad > & _prop_F
constexpr void loopDeclareProperties(std::index_sequence<>)
constexpr auto make_tuple_array(std::index_sequence< Ns... >)
Create a tuple with sizeof...(Ns) entries, containing CTArrayRefs with tags given by the Ns...
auto makeRefsTuple(const std::array< std::string, N > &variables, const unsigned int &qp, std::index_sequence< Ns... >)
CompileTimeDerivativesMaterial(const InputParameters &params, const std::array< std::string, N > variables)
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
auto & declareGenericProperty(const std::string &prop_name)
Definition: MaterialBase.h:141
auto take_derivatives(const T &expression, std::index_sequence< first, tags... >)
Take all derivatives of expression listed in the index sequence.
InputParameters validParams()
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
constexpr void loopEvaluate(const T &, std::index_sequence<>)
std::array< GenericMaterialProperty< Real, is_ad > *, CompileTimeDerivativesMaterialInternal::total_derivatives< MaxD, N >)> _prop_dF
A serialized list of material properties for all derivatives up to and including order MaxD...
void declareProperties()
Entry point for the compile time loop that declares all derivative material properties.
unsigned int _qp
Definition: MaterialBase.h:320
constexpr void loopEvaluate(const T &expression, std::index_sequence< first, tail... > int_seq)
Moose::GenericType< VariableValue, is_ad > GenericVariableValue
Definition: MooseTypes.h:662
typename GenericMaterialPropertyStruct< T, is_ad >::type GenericMaterialProperty
const std::string & type() const
Get the type of this class.
Definition: MooseBase.h:51
decltype(CompileTimeDerivativesMaterialInternal::make_tuple_array< GenericVariableValue< is_ad >>(std::make_index_sequence< N >{})) ::type _refs
Set up a tuple with one entry per coupled variable (each entry with an increment).
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
constexpr bool is_sorted()
Check if the given index sequence is sorted ()internal function)
Materials compute MaterialProperties.
Definition: Material.h:34
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Interface class ("Veneer") to provide generator methods for derivative material property names...
IntRange< T > make_range(T beg, T end)
void evaluate(const T &F)
Entry point for the compile time loop that evaluates all derivatives and assigns them to their respec...
constexpr void loopDeclareProperties(std::index_sequence< first, tail... > int_seq)
std::array< VariableName, N > _var_name
The names of the coupled variables.
Material class to set up an expression and its derivatives built at compile time using the CompileTim...