LCOV - code coverage report
Current view: top level - include/materials - Material.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 57 66 86.4 %
Date: 2025-07-17 01:28:37 Functions: 34 54 63.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             : #pragma once
      11             : 
      12             : // MOOOSE includes
      13             : #include "MaterialBase.h"
      14             : #include "Coupleable.h"
      15             : #include "MaterialPropertyInterface.h"
      16             : #include "FEProblemBase.h"
      17             : 
      18             : #include <string>
      19             : 
      20             : #define usingMaterialMembers                                                                       \
      21             :   usingMaterialBaseMembers;                                                                        \
      22             :   usingCoupleableMembers;                                                                          \
      23             :   usingMaterialPropertyInterfaceMembers;                                                           \
      24             :   using Material::_q_point;                                                                        \
      25             :   using Material::_qrule;                                                                          \
      26             :   using Material::_JxW;                                                                            \
      27             :   using Material::_current_elem;                                                                   \
      28             :   using Material::_current_subdomain_id;                                                           \
      29             :   using Material::_current_side
      30             : 
      31             : /**
      32             :  * Materials compute MaterialProperties.
      33             :  */
      34             : class Material : public MaterialBase, public Coupleable, public MaterialPropertyInterface
      35             : {
      36             : public:
      37             :   static InputParameters validParams();
      38             : 
      39             :   Material(const InputParameters & parameters);
      40             : 
      41             :   /**
      42             :    * Gets an element integer for the proper current element with a parameter of
      43             :    * the object derived from this interface
      44             :    * Note: This overrides the function in ElementIDInterface to assure derived materials
      45             :    *       call the functions in ElementIDInterface properly.
      46             :    */
      47          78 :   virtual const dof_id_type & getElementID(const std::string & id_parameter_name,
      48             :                                            unsigned int comp = 0) const override
      49             :   {
      50          78 :     return _neighbor ? ElementIDInterface::getElementIDNeighbor(id_parameter_name, comp)
      51          78 :                      : ElementIDInterface::getElementID(id_parameter_name, comp);
      52             :   }
      53             :   /**
      54             :    * Directly calling this function is not needed for materials because the same material has
      55             :    * three copies for element interior, element side and neighbor side.
      56             :    */
      57           0 :   virtual const dof_id_type & getElementIDNeighbor(const std::string & id_parameter_name,
      58             :                                                    unsigned int comp = 0) const override
      59             :   {
      60           0 :     mooseError("Directly calling 'getElementIDNeighbor' is not allowed for materials. Please call "
      61             :                "'getElementID' instead");
      62             :     return ElementIDInterface::getElementIDNeighbor(id_parameter_name, comp);
      63             :   }
      64             : 
      65             :   /**
      66             :    * Gets an element integer for the proper current element with the element integer name
      67             :    * Note: This overrides the function in ElementIDInterface to assure derived materials
      68             :    *       call the functions in ElementIDInterface properly.
      69             :    */
      70             :   virtual const dof_id_type &
      71           0 :   getElementIDByName(const std::string & id_parameter_name) const override
      72             :   {
      73           0 :     return _neighbor ? ElementIDInterface::getElementIDNeighborByName(id_parameter_name)
      74           0 :                      : ElementIDInterface::getElementIDByName(id_parameter_name);
      75             :   }
      76             :   /**
      77             :    * Directly calling this function is not needed for materials because the same material has
      78             :    * three copies for element interior, element side and neighbor side.
      79             :    */
      80             :   virtual const dof_id_type &
      81           0 :   getElementIDNeighborByName(const std::string & id_parameter_name) const override
      82             :   {
      83           0 :     mooseError("Directly calling 'getElementIDNeighborByName' is not allowed for materials. Please "
      84             :                "call 'getElementIDByName' instead");
      85             :     return ElementIDInterface::getElementIDNeighborByName(id_parameter_name);
      86             :   }
      87             : 
      88             :   virtual void computeProperties() override;
      89             : 
      90             :   ///@{
      91             :   /**
      92             :    * Retrieve the property through a given input parameter key with a fallback
      93             :    * to getting it by name
      94             :    */
      95             :   template <typename T, bool is_ad>
      96             :   const GenericMaterialProperty<T, is_ad> &
      97             :   getGenericMaterialProperty(const std::string & name, const unsigned int state = 0);
      98             :   template <typename T>
      99        1454 :   const MaterialProperty<T> & getMaterialProperty(const std::string & name,
     100             :                                                   const unsigned int state = 0)
     101             :   {
     102        1454 :     return getGenericMaterialProperty<T, false>(name, state);
     103             :   }
     104             :   template <typename T>
     105          78 :   const ADMaterialProperty<T> & getADMaterialProperty(const std::string & name)
     106             :   {
     107          78 :     return getGenericMaterialProperty<T, true>(name, 0);
     108             :   }
     109             :   template <typename T>
     110        2613 :   const MaterialProperty<T> & getMaterialPropertyOld(const std::string & name)
     111             :   {
     112        2613 :     return getGenericMaterialProperty<T, false>(name, 1);
     113             :   }
     114             :   template <typename T>
     115         645 :   const MaterialProperty<T> & getMaterialPropertyOlder(const std::string & name)
     116             :   {
     117         645 :     return getGenericMaterialProperty<T, false>(name, 2);
     118             :   }
     119             :   ///@}
     120             : 
     121             :   ///@{
     122             :   /**
     123             :    * Retrieve the property named "name"
     124             :    */
     125             :   template <typename T, bool is_ad>
     126             :   const GenericMaterialProperty<T, is_ad> &
     127             :   getGenericMaterialPropertyByName(const std::string & name, const unsigned int state = 0);
     128             :   template <typename T>
     129             :   const MaterialProperty<T> & getMaterialPropertyByName(const std::string & prop_name,
     130             :                                                         const unsigned int state = 0)
     131             :   {
     132             :     return getGenericMaterialPropertyByName<T, false>(prop_name, state);
     133             :   }
     134             :   template <typename T>
     135             :   const ADMaterialProperty<T> & getADMaterialPropertyByName(const std::string & prop_name)
     136             :   {
     137             :     return getGenericMaterialPropertyByName<T, true>(prop_name, 0);
     138             :   }
     139             :   template <typename T>
     140         468 :   const MaterialProperty<T> & getMaterialPropertyOldByName(const std::string & prop_name)
     141             :   {
     142         468 :     return getGenericMaterialPropertyByName<T, false>(prop_name, 1);
     143             :   }
     144             :   template <typename T>
     145             :   const MaterialProperty<T> & getMaterialPropertyOlderByName(const std::string & prop_name)
     146             :   {
     147             :     return getGenericMaterialPropertyByName<T, false>(prop_name, 2);
     148             :   }
     149             :   ///@}
     150             : 
     151             :   /**
     152             :    * Retrieve the discrete material with a given parameter key named "name"
     153             :    */
     154         207 :   MaterialBase & getMaterial(const std::string & name)
     155             :   {
     156         207 :     return getMaterialByName(parameters().get<MaterialName>(name));
     157             :   }
     158             : 
     159             :   /**
     160             :    * Retrieve the discrete material named "name".
     161             :    *
     162             :    * @param no_warn If true, suppress warning about retrieving the material potentially during its
     163             :    * calculation. If you don't know what this is/means, then you don't need it.
     164             :    * @param no_dep Use no_dep = true if no dependency resolution for the material is required. Using
     165             :    * no_dep = false is useful for discrete materials.
     166             :    */
     167             :   MaterialBase &
     168             :   getMaterialByName(const std::string & name, bool no_warn = false, bool no_dep = false);
     169             : 
     170             :   ///@{ Optional material property getters
     171             :   template <typename T, bool is_ad>
     172             :   const GenericOptionalMaterialProperty<T, is_ad> &
     173             :   getGenericOptionalMaterialProperty(const std::string & name, const unsigned int state = 0);
     174             :   template <typename T>
     175         153 :   const OptionalMaterialProperty<T> & getOptionalMaterialProperty(const std::string & name,
     176             :                                                                   const unsigned int state = 0)
     177             :   {
     178         153 :     return getGenericOptionalMaterialProperty<T, false>(name, state);
     179             :   }
     180             :   template <typename T>
     181         153 :   const OptionalADMaterialProperty<T> & getOptionalADMaterialProperty(const std::string & name)
     182             :   {
     183         153 :     return getGenericOptionalMaterialProperty<T, true>(name, 0);
     184             :   }
     185             :   template <typename T>
     186         153 :   const OptionalMaterialProperty<T> & getOptionalMaterialPropertyOld(const std::string & name)
     187             :   {
     188         153 :     return getGenericOptionalMaterialProperty<T, false>(name, 1);
     189             :   }
     190             :   template <typename T>
     191         153 :   const OptionalMaterialProperty<T> & getOptionalMaterialPropertyOlder(const std::string & name)
     192             :   {
     193         153 :     return getGenericOptionalMaterialProperty<T, false>(name, 2);
     194             :   }
     195             :   ///@}
     196             : 
     197             :   using MaterialBase::getGenericZeroMaterialProperty;
     198             :   using MaterialBase::getGenericZeroMaterialPropertyByName;
     199             :   using MaterialBase::getZeroMaterialProperty;
     200             : 
     201          47 :   virtual bool isBoundaryMaterial() const override { return _bnd; }
     202             : 
     203      728669 :   virtual const std::unordered_set<unsigned int> & getMatPropDependencies() const override
     204             :   {
     205      728669 :     return MaterialPropertyInterface::getMatPropDependencies();
     206             :   }
     207             :   virtual void subdomainSetup() override;
     208             : 
     209             :   enum class ConstantTypeEnum
     210             :   {
     211             :     NONE,
     212             :     ELEMENT,
     213             :     SUBDOMAIN
     214             :   };
     215             : 
     216        7492 :   bool ghostable() const override final { return _ghostable; }
     217             : 
     218             :   /// resolve all optional properties
     219             :   virtual void resolveOptionalProperties() override;
     220             : 
     221             : protected:
     222             :   virtual void checkMaterialProperty(const std::string & name, const unsigned int state) override;
     223             : 
     224           0 :   virtual const MaterialData & materialData() const override { return _material_data; }
     225     1894233 :   virtual MaterialData & materialData() override { return _material_data; }
     226             : 
     227       13729 :   virtual const QBase & qRule() const override { return *_qrule; }
     228             : 
     229             :   bool _bnd;
     230             :   bool _neighbor;
     231             : 
     232             :   const MooseArray<Point> & _q_point;
     233             :   const QBase * const & _qrule;
     234             : 
     235             :   const MooseArray<Real> & _JxW;
     236             : 
     237             :   const Elem * const & _current_elem;
     238             : 
     239             :   const SubdomainID & _current_subdomain_id;
     240             : 
     241             :   /// current side of the current element
     242             :   const unsigned int & _current_side;
     243             : 
     244             :   /// Options of the constantness level of the material
     245             :   const ConstantTypeEnum _constant_option;
     246             : 
     247             : private:
     248             :   ConstantTypeEnum computeConstantOption();
     249             : 
     250             :   /// Whether this material can be computed in a ghosted context. If properties are constant or
     251             :   /// depend only on finite volume variables, then this material can be computed in a ghosted
     252             :   /// context. If properties depend on finite element variables, then this material cannot be
     253             :   /// computed in a ghosted context
     254             :   bool _ghostable;
     255             : 
     256             :   /// optional material properties
     257             :   std::vector<std::unique_ptr<OptionalMaterialPropertyProxyBase<Material>>>
     258             :       _optional_property_proxies;
     259             : };
     260             : 
     261             : template <typename T, bool is_ad>
     262             : const GenericMaterialProperty<T, is_ad> &
     263        7289 : Material::getGenericMaterialProperty(const std::string & name, const unsigned int state)
     264             : {
     265             :   // Check if the supplied parameter is a valid input parameter key
     266        7289 :   const auto prop_name = getMaterialPropertyName(name);
     267             : 
     268             :   // Check if it's just a constant.
     269        7289 :   if (const auto * default_property = defaultGenericMaterialProperty<T, is_ad>(prop_name))
     270         156 :     return *default_property;
     271             : 
     272        7133 :   return getGenericMaterialPropertyByName<T, is_ad>(prop_name, state);
     273        7289 : }
     274             : 
     275             : template <typename T, bool is_ad>
     276             : const GenericMaterialProperty<T, is_ad> &
     277        8069 : Material::getGenericMaterialPropertyByName(const std::string & prop_name_in,
     278             :                                            const unsigned int state)
     279             : {
     280        8069 :   if (_use_interpolated_state)
     281             :   {
     282         936 :     if (state == 1)
     283         468 :       return getGenericMaterialPropertyByName<T, is_ad>(prop_name_in + _interpolated_old, 0);
     284         468 :     if (state == 2)
     285           0 :       return getGenericMaterialPropertyByName<T, is_ad>(prop_name_in + _interpolated_older, 0);
     286             :   }
     287             : 
     288        7601 :   MaterialBase::checkExecutionStage();
     289             : 
     290             :   // The property may not exist yet, so declare it (declare/getMaterialProperty are referencing the
     291             :   // same memory)
     292       15202 :   const auto prop_name =
     293        7601 :       _get_suffix.empty()
     294        7601 :           ? prop_name_in
     295        7601 :           : MooseUtils::join(std::vector<std::string>({prop_name_in, _get_suffix}), "_");
     296             : 
     297        7601 :   if (state == 0)
     298        4133 :     _requested_props.insert(prop_name);
     299             : 
     300             :   // Do this before so that we register the ID first
     301             :   auto & prop =
     302        7601 :       MaterialPropertyInterface::getGenericMaterialPropertyByName<T, is_ad>(prop_name_in, state);
     303             : 
     304        7601 :   registerPropName(prop_name, true, state);
     305             : 
     306        7601 :   return prop;
     307        7601 : }
     308             : 
     309             : template <typename T, bool is_ad>
     310             : const GenericOptionalMaterialProperty<T, is_ad> &
     311         612 : Material::getGenericOptionalMaterialProperty(const std::string & name, const unsigned int state)
     312             : {
     313         612 :   auto proxy = std::make_unique<OptionalMaterialPropertyProxy<Material, T, is_ad>>(name, state);
     314         612 :   auto & optional_property = proxy->value();
     315         612 :   _optional_property_proxies.push_back(std::move(proxy));
     316         612 :   return optional_property;
     317         612 : }

Generated by: LCOV version 1.14