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 "MaterialFunctorConverter.h" 11 : 12 : #include "metaphysicl/raw_type.h" 13 : 14 : registerMooseObject("MooseApp", MaterialFunctorConverter); 15 : registerMooseObject("MooseApp", VectorMaterialFunctorConverter); 16 : 17 : template <typename T> 18 : InputParameters 19 29078 : MaterialFunctorConverterTempl<T>::validParams() 20 : { 21 29078 : InputParameters params = Material::validParams(); 22 29078 : params.addClassDescription("Converts functor to non-AD and AD regular material properties"); 23 29078 : params.addParam<std::vector<MooseFunctorName>>( 24 : "functors_in", {}, "The names of the functors to convert to regular material properties"); 25 29078 : params.addParam<std::vector<MaterialPropertyName>>( 26 : "ad_props_out", {}, "The names of the output AD properties"); 27 29078 : params.addParam<std::vector<MaterialPropertyName>>( 28 : "reg_props_out", {}, "The names of the output regular properties"); 29 29078 : return params; 30 0 : } 31 : 32 : template <typename T> 33 402 : MaterialFunctorConverterTempl<T>::MaterialFunctorConverterTempl(const InputParameters & parameters) 34 : : Material(parameters), 35 402 : _num_functors_to_convert(getParam<std::vector<MooseFunctorName>>("functors_in").size()) 36 : { 37 402 : const auto & functors_in = getParam<std::vector<MooseFunctorName>>("functors_in"); 38 402 : const auto & reg_props_out = getParam<std::vector<MaterialPropertyName>>("reg_props_out"); 39 402 : const auto & ad_props_out = getParam<std::vector<MaterialPropertyName>>("ad_props_out"); 40 : 41 402 : if (reg_props_out.size() && ad_props_out.size()) 42 4 : 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 4 : type(), 47 : "'. Please create two instances, one for regular and one for AD."); 48 : 49 1088 : if ((reg_props_out.size() && (functors_in.size() != reg_props_out.size())) || 50 690 : (ad_props_out.size() && (functors_in.size() != ad_props_out.size()))) 51 4 : 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 : 56 394 : _functors_in.resize(_num_functors_to_convert); 57 394 : _ad_props_out.resize(ad_props_out.size()); 58 394 : _reg_props_out.resize(reg_props_out.size()); 59 : 60 1292 : for (const auto i : make_range(_num_functors_to_convert)) 61 898 : _functors_in[i] = &getFunctor<Moose::GenericType<T, true>>(functors_in[i]); 62 : 63 942 : for (const auto i : index_range(ad_props_out)) 64 548 : _ad_props_out[i] = &declareADProperty<T>(ad_props_out[i]); 65 : 66 744 : for (const auto i : index_range(reg_props_out)) 67 350 : _reg_props_out[i] = &declareProperty<T>(reg_props_out[i]); 68 394 : } 69 : 70 : template <typename T> 71 : void 72 0 : MaterialFunctorConverterTempl<T>::initQpStatefulProperties() 73 : { 74 0 : computeQpProperties(); 75 0 : } 76 : 77 : template <typename T> 78 : void 79 3376 : MaterialFunctorConverterTempl<T>::computeQpProperties() 80 : { 81 : // Using Qp 0 can leverage the functor caching 82 : // TODO: Find a way to effectively use subdomain-constant-ness 83 3376 : unsigned int qp_used = (_constant_option == ConstantTypeEnum::NONE) ? _qp : 0; 84 : 85 3376 : const auto state = Moose::currentState(); 86 3376 : if (_bnd) 87 : { 88 1984 : const Moose::ElemSideQpArg side_arg = { 89 992 : _current_elem, _current_side, qp_used, _qrule, _q_point[_qp]}; 90 2092 : for (const auto i : index_range(_ad_props_out)) 91 1100 : (*_ad_props_out[i])[_qp] = (*_functors_in[i])(side_arg, state); 92 : 93 1208 : for (const auto i : index_range(_reg_props_out)) 94 216 : (*_reg_props_out[i])[_qp] = MetaPhysicL::raw_value((*_functors_in[i])(side_arg, state)); 95 : } 96 : else 97 : { 98 2384 : const Elem * elem = _neighbor ? _current_elem->neighbor_ptr(_current_side) : _current_elem; 99 : mooseAssert(elem, "We should have an element"); 100 2384 : const Moose::ElemQpArg elem_arg = {elem, qp_used, _qrule, _q_point[_qp]}; 101 5848 : for (const auto i : index_range(_ad_props_out)) 102 3464 : (*_ad_props_out[i])[_qp] = (*_functors_in[i])(elem_arg, state); 103 : 104 4904 : for (const auto i : index_range(_reg_props_out)) 105 2520 : (*_reg_props_out[i])[_qp] = MetaPhysicL::raw_value((*_functors_in[i])(elem_arg, state)); 106 : } 107 3376 : } 108 : 109 : template class MaterialFunctorConverterTempl<Real>; 110 : template class MaterialFunctorConverterTempl<RealVectorValue>;