LCOV - code coverage report
Current view: top level - include/auxkernels - MaterialAuxBase.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 42 44 95.5 %
Date: 2025-07-17 01:28:37 Functions: 104 116 89.7 %
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             : #pragma once
      11             : 
      12             : // MOOSE includes
      13             : #include "AuxKernel.h"
      14             : #include "Assembly.h"
      15             : 
      16             : #include <unordered_set>
      17             : 
      18             : /**
      19             :  * A base class for the various Material related AuxKernal objects.
      20             :  * \p RT is short for return type
      21             :  */
      22             : template <typename T, bool is_ad, bool is_functor = false, typename RT = Real>
      23             : class MaterialAuxBaseTempl : public AuxKernelTempl<RT>
      24             : {
      25             : public:
      26             :   static InputParameters validParams();
      27             : 
      28             :   /**
      29             :    * Class constructor
      30             :    * @param parameters The input parameters for this object
      31             :    */
      32             :   MaterialAuxBaseTempl(const InputParameters & parameters);
      33             : 
      34             :   /// Functors really only work for Real and RealVectorValue for now :(
      35             :   using PropertyType = typename std::conditional<is_functor,
      36             :                                                  Moose::Functor<Moose::GenericType<T, is_ad>>,
      37             :                                                  GenericMaterialProperty<T, is_ad>>::type;
      38             : 
      39             : protected:
      40             :   virtual RT computeValue() override;
      41             : 
      42             :   /// Perform a sanity check on the retrieved value (e.g. to check dynamic sizes)
      43    32841821 :   virtual void checkFullValue() {}
      44             : 
      45             :   /// Returns material property values at quadrature points
      46             :   virtual RT getRealValue() = 0;
      47             : 
      48             :   /// (Functor)Material property for this AuxKernel
      49             :   const PropertyType & _prop;
      50             : 
      51             :   /// Evaluate at this quadrature point only
      52             :   const unsigned int _selected_qp;
      53             : 
      54             :   /// T Value evaluated from either the property or the functor
      55             :   Moose::GenericType<T, is_ad> _full_value;
      56             : 
      57             : private:
      58             :   /// Helper function to retrieve the property or functor
      59             :   const PropertyType & getPropertyHelper();
      60             : 
      61             :   /// Multiplier for the material property
      62             :   const Real _factor;
      63             : 
      64             :   /// Value to be added to the material property
      65             :   const RT _offset;
      66             : 
      67             :   /// ID of the subdomain currently being iterated over
      68             :   const SubdomainID & _current_subdomain_id;
      69             : };
      70             : 
      71             : template <typename T, bool is_ad, bool is_functor, typename RT>
      72             : InputParameters
      73      431002 : MaterialAuxBaseTempl<T, is_ad, is_functor, RT>::validParams()
      74             : {
      75      431002 :   InputParameters params = AuxKernelTempl<RT>::validParams();
      76             :   if constexpr (is_functor)
      77       57310 :     params.addRequiredParam<MooseFunctorName>("functor", "The functor name.");
      78             :   else
      79      373692 :     params.addRequiredParam<MaterialPropertyName>("property", "The material property name.");
      80             : 
      81     1293006 :   params.addParam<Real>(
      82      862004 :       "factor", 1, "The factor by which to multiply your material property for visualization");
      83      431002 :   params.addParam<RT>("offset", 0, "The offset to add to your material property for visualization");
      84             : 
      85             :   if constexpr (!is_functor)
      86             :   {
      87      373692 :     params.addParam<unsigned int>(
      88             :         "selected_qp",
      89             :         "Evaluate the material property at a specified quadrature point. This only needs "
      90             :         "to be used if you are interested in a particular quadrature point in each element. "
      91             :         "Otherwise do not include this parameter in your input file.");
      92      373692 :     params.addParamNamesToGroup("selected_qp", "Advanced");
      93             :   }
      94             : 
      95      431002 :   return params;
      96           0 : }
      97             : 
      98             : template <typename T, bool is_ad, bool is_functor, typename RT>
      99       31456 : MaterialAuxBaseTempl<T, is_ad, is_functor, RT>::MaterialAuxBaseTempl(
     100             :     const InputParameters & parameters)
     101             :   : AuxKernelTempl<RT>(parameters),
     102       62900 :     _prop(getPropertyHelper()),
     103       62896 :     _selected_qp(this->isParamValid("selected_qp")
     104       31448 :                      ? this->template getParam<unsigned int>("selected_qp")
     105             :                      : libMesh::invalid_uint),
     106       31448 :     _factor(this->template getParam<Real>("factor")),
     107       31448 :     _offset(this->template getParam<RT>("offset")),
     108       90530 :     _current_subdomain_id(this->_assembly.currentSubdomainID())
     109             : {
     110       31448 : }
     111             : 
     112             : template <typename T, bool is_ad, bool is_functor, typename RT>
     113             : const typename MaterialAuxBaseTempl<T, is_ad, is_functor, RT>::PropertyType &
     114       31452 : MaterialAuxBaseTempl<T, is_ad, is_functor, RT>::getPropertyHelper()
     115             : {
     116             :   if constexpr (is_functor)
     117         130 :     return this->template getFunctor<Moose::GenericType<T, is_ad>>("functor");
     118             :   else
     119       31322 :     return this->template getGenericMaterialProperty<T, is_ad>("property");
     120             : }
     121             : 
     122             : template <typename T, bool is_ad, bool is_functor, typename RT>
     123             : RT
     124    32841826 : MaterialAuxBaseTempl<T, is_ad, is_functor, RT>::computeValue()
     125             : {
     126             :   // Functor Values
     127             :   if constexpr (is_functor)
     128             :   {
     129        1000 :     if (this->isNodal())
     130             :     {
     131         288 :       const std::set<SubdomainID> sub_id_set = {_current_subdomain_id};
     132         288 :       const Moose::NodeArg node_arg{this->_current_node, &sub_id_set};
     133         288 :       const auto state = this->determineState();
     134         288 :       _full_value = _prop(node_arg, state);
     135         288 :     }
     136             :     else
     137             :     {
     138         712 :       const auto elem_arg = this->makeElemArg(this->_current_elem);
     139         712 :       const auto state = this->determineState();
     140         712 :       _full_value = _prop(elem_arg, state);
     141             :     }
     142             :   }
     143             :   // Material Properties
     144             :   else
     145             :   {
     146    32840826 :     if (_selected_qp != libMesh::invalid_uint)
     147             :     {
     148         261 :       if (_selected_qp >= this->_q_point.size())
     149             :       {
     150           5 :         Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
     151           4 :         this->paramError("selected_qp",
     152             :                          "Trying to evaluate qp ",
     153           4 :                          _selected_qp,
     154             :                          " but there are only ",
     155           4 :                          this->_q_point.size(),
     156             :                          " quadrature points in the element");
     157           0 :       }
     158         256 :       _full_value = _prop[_selected_qp];
     159             :     }
     160             :     else
     161    32840565 :       _full_value = _prop[this->_qp];
     162             :   }
     163             : 
     164    32841821 :   checkFullValue();
     165    32841821 :   return _factor * getRealValue() + _offset;
     166             : }
     167             : 
     168             : template <typename T = Real>
     169             : using MaterialAuxBase = MaterialAuxBaseTempl<T, false, false, Real>;

Generated by: LCOV version 1.14