LCOV - code coverage report
Current view: top level - src/materials - MaterialFunctorConverter.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 45 49 91.8 %
Date: 2025-07-17 01:28:37 Functions: 6 8 75.0 %
Legend: Lines: hit not hit

          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       29042 : MaterialFunctorConverterTempl<T>::validParams()
      20             : {
      21       29042 :   InputParameters params = Material::validParams();
      22       29042 :   params.addClassDescription("Converts functor to non-AD and AD regular material properties");
      23       29042 :   params.addParam<std::vector<MooseFunctorName>>(
      24             :       "functors_in", {}, "The names of the functors to convert to regular material properties");
      25       29042 :   params.addParam<std::vector<MaterialPropertyName>>(
      26             :       "ad_props_out", {}, "The names of the output AD properties");
      27       29042 :   params.addParam<std::vector<MaterialPropertyName>>(
      28             :       "reg_props_out", {}, "The names of the output regular properties");
      29       29042 :   return params;
      30           0 : }
      31             : 
      32             : template <typename T>
      33         376 : MaterialFunctorConverterTempl<T>::MaterialFunctorConverterTempl(const InputParameters & parameters)
      34             :   : Material(parameters),
      35         376 :     _num_functors_to_convert(getParam<std::vector<MooseFunctorName>>("functors_in").size())
      36             : {
      37         376 :   const auto & functors_in = getParam<std::vector<MooseFunctorName>>("functors_in");
      38         376 :   const auto & reg_props_out = getParam<std::vector<MaterialPropertyName>>("reg_props_out");
      39         376 :   const auto & ad_props_out = getParam<std::vector<MaterialPropertyName>>("ad_props_out");
      40             : 
      41         376 :   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        1017 :   if ((reg_props_out.size() && (functors_in.size() != reg_props_out.size())) ||
      50         645 :       (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         368 :   _functors_in.resize(_num_functors_to_convert);
      57         368 :   _ad_props_out.resize(ad_props_out.size());
      58         368 :   _reg_props_out.resize(reg_props_out.size());
      59             : 
      60        1204 :   for (const auto i : make_range(_num_functors_to_convert))
      61         836 :     _functors_in[i] = &getFunctor<Moose::GenericType<T, true>>(functors_in[i]);
      62             : 
      63         879 :   for (const auto i : index_range(ad_props_out))
      64         511 :     _ad_props_out[i] = &declareADProperty<T>(ad_props_out[i]);
      65             : 
      66         693 :   for (const auto i : index_range(reg_props_out))
      67         325 :     _reg_props_out[i] = &declareProperty<T>(reg_props_out[i]);
      68         368 : }
      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        3002 : 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        3002 :   unsigned int qp_used = (_constant_option == ConstantTypeEnum::NONE) ? _qp : 0;
      84             : 
      85        3002 :   const auto state = Moose::currentState();
      86        3002 :   if (_bnd)
      87             :   {
      88        1764 :     const Moose::ElemSideQpArg side_arg = {
      89         882 :         _current_elem, _current_side, qp_used, _qrule, _q_point[_qp]};
      90        1860 :     for (const auto i : index_range(_ad_props_out))
      91         978 :       (*_ad_props_out[i])[_qp] = (*_functors_in[i])(side_arg, state);
      92             : 
      93        1074 :     for (const auto i : index_range(_reg_props_out))
      94         192 :       (*_reg_props_out[i])[_qp] = MetaPhysicL::raw_value((*_functors_in[i])(side_arg, state));
      95             :   }
      96             :   else
      97             :   {
      98        2120 :     const Elem * elem = _neighbor ? _current_elem->neighbor_ptr(_current_side) : _current_elem;
      99             :     mooseAssert(elem, "We should have an element");
     100        2120 :     const Moose::ElemQpArg elem_arg = {elem, qp_used, _qrule, _q_point[_qp]};
     101        5200 :     for (const auto i : index_range(_ad_props_out))
     102        3080 :       (*_ad_props_out[i])[_qp] = (*_functors_in[i])(elem_arg, state);
     103             : 
     104        4360 :     for (const auto i : index_range(_reg_props_out))
     105        2240 :       (*_reg_props_out[i])[_qp] = MetaPhysicL::raw_value((*_functors_in[i])(elem_arg, state));
     106             :   }
     107        3002 : }
     108             : 
     109             : template class MaterialFunctorConverterTempl<Real>;
     110             : template class MaterialFunctorConverterTempl<RealVectorValue>;

Generated by: LCOV version 1.14