Line data Source code
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 : 16 : registerMooseObject("MooseApp", MaterialADConverter); 17 : registerMooseObject("MooseApp", RankFourTensorMaterialADConverter); 18 : registerMooseObject("MooseApp", RankTwoTensorMaterialADConverter); 19 : registerMooseObjectRenamed("MooseApp", MaterialConverter, "06/30/2022 24:00", MaterialADConverter); 20 : registerMooseObjectRenamed("MooseApp", 21 : RankTwoTensorMaterialConverter, 22 : "06/30/2022 24:00", 23 : RankTwoTensorMaterialADConverter); 24 : registerMooseObjectRenamed("MooseApp", 25 : RankFourTensorMaterialConverter, 26 : "06/30/2022 24:00", 27 : RankFourTensorMaterialADConverter); 28 : 29 : template <typename T> 30 : InputParameters 31 85942 : MaterialADConverterTempl<T>::validParams() 32 : { 33 85942 : InputParameters params = Material::validParams(); 34 85942 : params.addClassDescription( 35 : "Converts regular material properties to AD properties and vice versa"); 36 85942 : params.addParam<std::vector<MaterialPropertyName>>( 37 : "reg_props_in", 38 : {}, 39 : "The names of the regular material properties to convert to AD properties"); 40 85942 : params.addParam<std::vector<MaterialPropertyName>>( 41 : "ad_props_out", {}, "The names of the output AD properties"); 42 85942 : params.addParam<std::vector<MaterialPropertyName>>( 43 : "ad_props_in", 44 : {}, 45 : "The names of the AD material properties to convert to regular properties"); 46 85942 : params.addParam<std::vector<MaterialPropertyName>>( 47 : "reg_props_out", {}, "The names of the output regular properties"); 48 257826 : params.addParam<bool>( 49 171884 : "intra_convert", false, "Whether to allow intra conversion, e.g. regular->regular, ad->ad"); 50 85942 : return params; 51 0 : } 52 : 53 : template <typename T> 54 270 : MaterialADConverterTempl<T>::MaterialADConverterTempl(const InputParameters & parameters) 55 270 : : Material(parameters), _intra_convert(getParam<bool>("intra_convert")) 56 : { 57 : 58 270 : auto reg_props_in = getParam<std::vector<MaterialPropertyName>>("reg_props_in"); 59 270 : auto ad_props_out = getParam<std::vector<MaterialPropertyName>>("ad_props_out"); 60 270 : auto ad_props_in = getParam<std::vector<MaterialPropertyName>>("ad_props_in"); 61 270 : auto reg_props_out = getParam<std::vector<MaterialPropertyName>>("reg_props_out"); 62 : 63 270 : if (_intra_convert) 64 : { 65 0 : if (reg_props_in.size() != reg_props_out.size()) 66 0 : 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 270 : if (reg_props_in.size() != ad_props_out.size()) 74 0 : 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 270 : _num_reg_props_to_convert = reg_props_in.size(); 81 : 82 270 : if (_intra_convert) 83 : { 84 0 : if (ad_props_in.size() != ad_props_out.size()) 85 0 : 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 270 : if (ad_props_in.size() != reg_props_out.size()) 93 0 : 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 270 : _num_ad_props_to_convert = ad_props_in.size(); 100 : 101 270 : _reg_props_in.resize(_num_reg_props_to_convert); 102 270 : _ad_props_out.resize(ad_props_out.size()); 103 270 : _ad_props_in.resize(_num_ad_props_to_convert); 104 270 : _reg_props_out.resize(reg_props_out.size()); 105 : 106 462 : for (MooseIndex(_num_reg_props_to_convert) i = 0; i < _num_reg_props_to_convert; ++i) 107 192 : _reg_props_in[i] = &getMaterialProperty<T>(reg_props_in[i]); 108 : 109 462 : for (MooseIndex(ad_props_out) i = 0; i < ad_props_out.size(); ++i) 110 192 : _ad_props_out[i] = &declareADProperty<T>(ad_props_out[i]); 111 : 112 348 : for (MooseIndex(_num_ad_props_to_convert) i = 0; i < _num_ad_props_to_convert; ++i) 113 78 : _ad_props_in[i] = &getADMaterialProperty<T>(ad_props_in[i]); 114 : 115 348 : for (MooseIndex(reg_props_out) i = 0; i < reg_props_out.size(); ++i) 116 78 : _reg_props_out[i] = &declareProperty<T>(reg_props_out[i]); 117 270 : } 118 : 119 : template <typename T> 120 : void 121 0 : MaterialADConverterTempl<T>::initQpStatefulProperties() 122 : { 123 0 : computeQpProperties(); 124 0 : } 125 : 126 : template <typename T> 127 : void 128 36124 : MaterialADConverterTempl<T>::computeQpProperties() 129 : { 130 36124 : if (_intra_convert) 131 0 : for (MooseIndex(_num_reg_props_to_convert) i = 0; i < _num_reg_props_to_convert; ++i) 132 0 : (*_reg_props_out[i])[_qp] = (*_reg_props_in[i])[_qp]; 133 : else 134 54328 : for (MooseIndex(_num_reg_props_to_convert) i = 0; i < _num_reg_props_to_convert; ++i) 135 18204 : (*_ad_props_out[i])[_qp] = (*_reg_props_in[i])[_qp]; 136 : 137 36124 : if (_intra_convert) 138 0 : for (MooseIndex(_num_ad_props_to_convert) i = 0; i < _num_ad_props_to_convert; ++i) 139 0 : (*_ad_props_out[i])[_qp] = (*_ad_props_in[i])[_qp]; 140 : else 141 54044 : for (MooseIndex(_num_ad_props_to_convert) i = 0; i < _num_ad_props_to_convert; ++i) 142 17920 : (*_reg_props_out[i])[_qp] = MetaPhysicL::raw_value((*_ad_props_in[i])[_qp]); 143 36124 : } 144 : 145 : template class MaterialADConverterTempl<Real>; 146 : template class MaterialADConverterTempl<RankFourTensor>; 147 : template class MaterialADConverterTempl<RankTwoTensor>;