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 "NEML2ToMOOSEMaterialProperty.h" 11 : #include "NEML2ModelExecutor.h" 12 : 13 : #define registerNEML2ToMOOSEMaterialProperty(alias) \ 14 : registerMooseObject("MooseApp", NEML2ToMOOSE##alias##MaterialProperty) 15 : 16 : registerNEML2ToMOOSEMaterialProperty(Real); 17 : registerNEML2ToMOOSEMaterialProperty(SymmetricRankTwoTensor); 18 : registerNEML2ToMOOSEMaterialProperty(SymmetricRankFourTensor); 19 : registerNEML2ToMOOSEMaterialProperty(RealVectorValue); 20 : registerNEML2ToMOOSEMaterialProperty(RankTwoTensor); 21 : registerNEML2ToMOOSEMaterialProperty(RankFourTensor); 22 : 23 : template <typename T> 24 : InputParameters 25 18436 : NEML2ToMOOSEMaterialProperty<T>::validParams() 26 : { 27 18436 : auto params = Material::validParams(); 28 18436 : params.addClassDescription("Provide an output (or its derivative) from a NEML2 model as a MOOSE " 29 : "material property of type " + 30 : demangle(typeid(T).name()) + "."); 31 : 32 73744 : params.addRequiredParam<UserObjectName>("neml2_executor", 33 : "User object managing the execution of the NEML2 model."); 34 73744 : params.addRequiredParam<MaterialPropertyName>( 35 : "to_moose", 36 : "MOOSE material property used to store the NEML2 output variable (or its derivative)"); 37 73744 : params.addRequiredParam<std::string>("from_neml2", "NEML2 output variable to read from"); 38 73744 : params.addParam<std::string>( 39 : "neml2_input_derivative", 40 : 41 : "If supplied return the derivative of the NEML2 output variable with respect to this"); 42 73744 : params.addParam<std::string>( 43 : "neml2_parameter_derivative", 44 : "If supplied return the derivative of neml2_variable with respect to this"); 45 : 46 : // provide an optional initialization of the moose property (because we don't really know if it is 47 : // going to become stateful or not) 48 55308 : params.addParam<MaterialPropertyName>("moose_material_property_init", 49 : "Optional material property as the initial condition"); 50 : 51 18436 : return params; 52 0 : } 53 : 54 : template <typename T> 55 54 : NEML2ToMOOSEMaterialProperty<T>::NEML2ToMOOSEMaterialProperty(const InputParameters & params) 56 0 : : Material(params) 57 : #ifdef NEML2_ENABLED 58 : , 59 54 : _execute_neml2_model(getUserObject<NEML2ModelExecutor>("neml2_executor")), 60 108 : _prop(declareProperty<T>(getParam<MaterialPropertyName>("to_moose"))), 61 108 : _prop0(isParamValid("moose_material_property_init") 62 54 : ? &getMaterialProperty<T>("moose_material_property_init") 63 : : nullptr), 64 54 : _value( 65 108 : !isParamValid("neml2_input_derivative") 66 189 : ? (!isParamValid("neml2_parameter_derivative") 67 54 : ? _execute_neml2_model.getOutput( 68 135 : NEML2Utils::parseVariableName(getParam<std::string>("from_neml2"))) 69 0 : : _execute_neml2_model.getOutputParameterDerivative( 70 54 : NEML2Utils::parseVariableName(getParam<std::string>("from_neml2")), 71 54 : getParam<std::string>("neml2_parameter_derivative"))) 72 81 : : _execute_neml2_model.getOutputDerivative( 73 135 : NEML2Utils::parseVariableName(getParam<std::string>("from_neml2")), 74 189 : NEML2Utils::parseVariableName(getParam<std::string>("neml2_input_derivative")))) 75 : #endif 76 : { 77 54 : NEML2Utils::assertNEML2Enabled(); 78 54 : } 79 : 80 : #ifdef NEML2_ENABLED 81 : template <typename T> 82 : void 83 40220 : NEML2ToMOOSEMaterialProperty<T>::computeProperties() 84 : { 85 : // See issue #28971: Using _prop0 to set initial condition for this possibly stateful property may 86 : // not work. As a workaround, we set the initial condition here when _t_step == 0. 87 40220 : if (_t_step == 0 && _prop0) 88 : { 89 0 : _prop.set() = _prop0->get(); 90 0 : return; 91 : } 92 : 93 40220 : if (!_execute_neml2_model.outputReady()) 94 100 : return; 95 : 96 : // look up start index for current element 97 40120 : const auto i = _execute_neml2_model.getBatchIndex(_current_elem->id()); 98 120360 : for (_qp = 0; _qp < _qrule->n_points(); ++_qp) 99 240720 : NEML2Utils::copyTensorToMOOSEData(_value.batch_index({neml2::Size(i + _qp)}), _prop[_qp]); 100 80240 : } 101 : #endif 102 : 103 : #define instantiateNEML2ToMOOSEMaterialProperty(T) template class NEML2ToMOOSEMaterialProperty<T> 104 : 105 : instantiateNEML2ToMOOSEMaterialProperty(Real); 106 : instantiateNEML2ToMOOSEMaterialProperty(SymmetricRankTwoTensor); 107 : instantiateNEML2ToMOOSEMaterialProperty(SymmetricRankFourTensor); 108 : instantiateNEML2ToMOOSEMaterialProperty(RealVectorValue); 109 : instantiateNEML2ToMOOSEMaterialProperty(RankTwoTensor); 110 : instantiateNEML2ToMOOSEMaterialProperty(RankFourTensor);