https://mooseframework.inl.gov
MaterialADConverter.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 "MaterialADConverter.h"
11 #include "RankTwoTensor.h"
12 #include "RankFourTensor.h"
13 
14 #include "metaphysicl/raw_type.h"
15 
19 registerMooseObjectRenamed("MooseApp", MaterialConverter, "06/30/2022 24:00", MaterialADConverter);
21  RankTwoTensorMaterialConverter,
22  "06/30/2022 24:00",
25  RankFourTensorMaterialConverter,
26  "06/30/2022 24:00",
28 
29 template <typename T>
32 {
34  params.addClassDescription(
35  "Converts regular material properties to AD properties and vice versa");
36  params.addParam<std::vector<MaterialPropertyName>>(
37  "reg_props_in",
38  {},
39  "The names of the regular material properties to convert to AD properties");
40  params.addParam<std::vector<MaterialPropertyName>>(
41  "ad_props_out", {}, "The names of the output AD properties");
42  params.addParam<std::vector<MaterialPropertyName>>(
43  "ad_props_in",
44  {},
45  "The names of the AD material properties to convert to regular properties");
46  params.addParam<std::vector<MaterialPropertyName>>(
47  "reg_props_out", {}, "The names of the output regular properties");
48  params.addParam<bool>(
49  "intra_convert", false, "Whether to allow intra conversion, e.g. regular->regular, ad->ad");
50  return params;
51 }
52 
53 template <typename T>
55  : Material(parameters), _intra_convert(getParam<bool>("intra_convert"))
56 {
57 
58  auto reg_props_in = getParam<std::vector<MaterialPropertyName>>("reg_props_in");
59  auto ad_props_out = getParam<std::vector<MaterialPropertyName>>("ad_props_out");
60  auto ad_props_in = getParam<std::vector<MaterialPropertyName>>("ad_props_in");
61  auto reg_props_out = getParam<std::vector<MaterialPropertyName>>("reg_props_out");
62 
63  if (_intra_convert)
64  {
65  if (reg_props_in.size() != reg_props_out.size())
66  paramError("reg_props_out",
67  "The number of output regular properties must match the number of input regular "
68  "properties, which is " +
69  std::to_string(reg_props_in.size()));
70  }
71  else
72  {
73  if (reg_props_in.size() != ad_props_out.size())
74  paramError("ad_props_out",
75  "The number of output AD properties must match the number of input regular "
76  "properties, which is " +
77  std::to_string(reg_props_in.size()));
78  }
79 
80  _num_reg_props_to_convert = reg_props_in.size();
81 
82  if (_intra_convert)
83  {
84  if (ad_props_in.size() != ad_props_out.size())
85  paramError("ad_props_out",
86  "The number of output AD properties must match the number of input AD "
87  "properties, which is " +
88  std::to_string(ad_props_in.size()));
89  }
90  else
91  {
92  if (ad_props_in.size() != reg_props_out.size())
93  paramError("reg_props_out",
94  "The number of output regular properties must match the number of input AD "
95  "properties, which is " +
96  std::to_string(ad_props_in.size()));
97  }
98 
99  _num_ad_props_to_convert = ad_props_in.size();
100 
102  _ad_props_out.resize(ad_props_out.size());
104  _reg_props_out.resize(reg_props_out.size());
105 
106  for (MooseIndex(_num_reg_props_to_convert) i = 0; i < _num_reg_props_to_convert; ++i)
107  _reg_props_in[i] = &getMaterialProperty<T>(reg_props_in[i]);
108 
109  for (MooseIndex(ad_props_out) i = 0; i < ad_props_out.size(); ++i)
110  _ad_props_out[i] = &declareADProperty<T>(ad_props_out[i]);
111 
112  for (MooseIndex(_num_ad_props_to_convert) i = 0; i < _num_ad_props_to_convert; ++i)
113  _ad_props_in[i] = &getADMaterialProperty<T>(ad_props_in[i]);
114 
115  for (MooseIndex(reg_props_out) i = 0; i < reg_props_out.size(); ++i)
116  _reg_props_out[i] = &declareProperty<T>(reg_props_out[i]);
117 }
118 
119 template <typename T>
120 void
122 {
123  computeQpProperties();
124 }
125 
126 template <typename T>
127 void
129 {
130  if (_intra_convert)
131  for (MooseIndex(_num_reg_props_to_convert) i = 0; i < _num_reg_props_to_convert; ++i)
132  (*_reg_props_out[i])[_qp] = (*_reg_props_in[i])[_qp];
133  else
134  for (MooseIndex(_num_reg_props_to_convert) i = 0; i < _num_reg_props_to_convert; ++i)
135  (*_ad_props_out[i])[_qp] = (*_reg_props_in[i])[_qp];
136 
137  if (_intra_convert)
138  for (MooseIndex(_num_ad_props_to_convert) i = 0; i < _num_ad_props_to_convert; ++i)
139  (*_ad_props_out[i])[_qp] = (*_ad_props_in[i])[_qp];
140  else
141  for (MooseIndex(_num_ad_props_to_convert) i = 0; i < _num_ad_props_to_convert; ++i)
142  (*_reg_props_out[i])[_qp] = MetaPhysicL::raw_value((*_ad_props_in[i])[_qp]);
143 }
144 
145 template class MaterialADConverterTempl<Real>;
This material converts regular material properties to AD properties and AD properties to regular prop...
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
registerMooseObject("MooseApp", MaterialADConverter)
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...
virtual void computeQpProperties() override
Users must override this method.
static InputParameters validParams()
Definition: Material.C:14
std::vector< const MaterialProperty< T > * > _reg_props_in
static InputParameters validParams()
registerMooseObjectRenamed("MooseApp", MaterialConverter, "06/30/2022 24:00", MaterialADConverter)
Materials compute MaterialProperties.
Definition: Material.h:34
std::vector< MaterialProperty< T > * > _reg_props_out
std::vector< ADMaterialProperty< T > * > _ad_props_out
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...
MaterialADConverterTempl(const InputParameters &parameters)
std::vector< const ADMaterialProperty< T > * > _ad_props_in
void initQpStatefulProperties() override
Initialize stateful properties at quadrature points.