LCOV - code coverage report
Current view: top level - include/actions - ProjectedStatefulMaterialStorageAction.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 419b9d Lines: 63 66 95.5 %
Date: 2025-08-08 20:01:16 Functions: 12 13 92.3 %
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             : #include "Action.h"
      13             : #include "Registry.h"
      14             : #include "Conversion.h"
      15             : #include "SerialAccess.h"
      16             : #include "RankTwoTensorForward.h"
      17             : #include "RankFourTensorForward.h"
      18             : #include "libmesh/fe_type.h"
      19             : 
      20             : // created types
      21             : #include "InterpolatedStatefulMaterial.h"
      22             : #include "ProjectedStatefulMaterialAux.h"
      23             : #include "ProjectedStatefulMaterialNodalPatchRecovery.h"
      24             : 
      25             : /**
      26             :  * Set up AuxKernels and AuxVariables for projected material property storage (PoMPS).
      27             :  */
      28             : class ProjectedStatefulMaterialStorageAction : public Action
      29             : {
      30             : public:
      31             :   static InputParameters validParams();
      32             : 
      33             :   ProjectedStatefulMaterialStorageAction(const InputParameters & parameters);
      34             : 
      35             :   virtual void act() override;
      36             : 
      37             :   using Action::addRelationshipManagers;
      38             :   virtual void addRelationshipManagers(Moose::RelationshipManagerType input_rm_type) override;
      39             : 
      40             :   /// List of supported raw types (equivalent AD types are also supported)
      41             :   typedef Moose::TypeList<Real, RealVectorValue, RankTwoTensor, RankFourTensor> SupportedTypes;
      42             : 
      43             :   /// get an enum with all supported types
      44             :   static MooseEnum getTypeEnum();
      45             : 
      46             :   template <typename T, bool is_ad>
      47             :   void processProperty(const MaterialPropertyName & prop_name);
      48             : 
      49             : protected:
      50             :   template <typename T, int I>
      51             :   struct ProcessProperty
      52             :   {
      53        1248 :     static void apply(ProjectedStatefulMaterialStorageAction * self,
      54             :                       const MaterialPropertyName & prop_name)
      55             :     {
      56        1248 :       self->processProperty<T, false>(prop_name);
      57        1248 :       self->processProperty<T, true>(prop_name);
      58        1248 :     }
      59             :   };
      60             : 
      61             :   template <typename... Ts>
      62             :   static MooseEnum getTypeEnum(typename Moose::TypeList<Ts...>);
      63             : 
      64             :   /// Property names for which we will do stateful material property projection
      65             :   const std::vector<MaterialPropertyName> & _prop_names;
      66             : 
      67             :   /// Variable order to project the properties into
      68             :   const MooseEnum _order;
      69             : 
      70             :   /// FEType for the variables to project the properties into
      71             :   FEType _fe_type;
      72             : 
      73             :   /// MooseObject name for the underlying variable type
      74             :   const std::string _var_type;
      75             : };
      76             : 
      77             : template <typename... Ts>
      78             : MooseEnum
      79           0 : ProjectedStatefulMaterialStorageAction::getTypeEnum(Moose::TypeList<Ts...>)
      80             : {
      81           0 :   return MooseEnum(Moose::stringify(std::vector<std::string>{typeid(Ts).name()...}, " "));
      82           0 : }
      83             : 
      84             : template <typename T, bool is_ad>
      85             : void
      86        2496 : ProjectedStatefulMaterialStorageAction::processProperty(const MaterialPropertyName & prop_name)
      87             : {
      88             :   // check if a property of type T exists
      89        2496 :   const auto & block_data = _problem->getMaterialData(Moose::BLOCK_MATERIAL_DATA);
      90        2496 :   const auto & boundary_data = _problem->getMaterialData(Moose::BOUNDARY_MATERIAL_DATA);
      91        4680 :   if (!block_data.haveGenericProperty<T, is_ad>(prop_name) &&
      92        2184 :       !boundary_data.haveGenericProperty<T, is_ad>(prop_name))
      93        2184 :     return;
      94             : 
      95             :   // number of scalar components
      96         312 :   const auto size = Moose::SerialAccess<T>::size();
      97             : 
      98             :   // generate variable names
      99         312 :   std::vector<VariableName> vars;
     100        7644 :   for (const auto i : make_range(size))
     101        7332 :     vars.push_back("_var_" + prop_name + '_' + Moose::stringify(i));
     102             : 
     103         312 :   if (_current_task == "setup_projected_properties")
     104             :   {
     105             :     // add the AuxVars for storage
     106        3822 :     for (const auto & var : vars)
     107             :     {
     108        3666 :       auto params = _factory.getValidParams(_var_type);
     109        3666 :       params.applyParameters(parameters());
     110        7332 :       params.set<std::vector<OutputName>>("outputs") = {"none"};
     111        3666 :       _problem->addAuxVariable(_var_type, var, params);
     112             :     }
     113             : 
     114             :     // add material
     115             :     {
     116         156 :       const auto type = Registry::getClassName<InterpolatedStatefulMaterialTempl<T>>();
     117         156 :       auto params = _factory.getValidParams(type);
     118         312 :       params.applySpecificParameters(parameters(), {"block"});
     119         156 :       params.template set<std::vector<VariableName>>("old_state") = vars;
     120         156 :       params.template set<MaterialPropertyName>("prop_name") = prop_name;
     121         156 :       _problem->addMaterial(type, "_mat_" + prop_name, params);
     122         156 :     }
     123             : 
     124             :     // use nodal patch recovery for lagrange
     125         156 :     if (_fe_type.family == LAGRANGE)
     126             :     {
     127             :       // nodal variables require patch recovery (add user object)
     128          52 :       const auto & type =
     129             :           Registry::getClassName<ProjectedStatefulMaterialNodalPatchRecoveryTempl<T, is_ad>>();
     130          52 :       auto params = _factory.getValidParams(type);
     131         104 :       params.applySpecificParameters(parameters(), {"block"});
     132          52 :       params.template set<MaterialPropertyName>("property") = prop_name;
     133          52 :       params.template set<MooseEnum>("patch_polynomial_order") = _order;
     134         156 :       params.template set<ExecFlagEnum>("execute_on") = {EXEC_INITIAL, EXEC_TIMESTEP_END};
     135          52 :       params.template set<bool>("force_preaux") = true;
     136          52 :       _problem->addUserObject(type, "_npruo_" + prop_name, params);
     137          52 :     }
     138             :   }
     139             : 
     140         312 :   if (_current_task == "add_aux_kernel")
     141             :   {
     142             :     // create variables
     143         156 :     std::vector<std::string> auxnames;
     144        3822 :     for (const auto i : make_range(size))
     145        3666 :       auxnames.push_back("_aux_" + prop_name + '_' + Moose::stringify(i));
     146             : 
     147             :     // use nodal patch recovery for lagrange
     148         156 :     if (_fe_type.family == LAGRANGE)
     149             :     {
     150             :       // nodal variables require patch recovery (add aux kernel)
     151          52 :       const auto & type = "ProjectedMaterialPropertyNodalPatchRecoveryAux";
     152        1274 :       for (const auto i : make_range(size))
     153             :       {
     154        1222 :         auto params = _factory.getValidParams(type);
     155        2444 :         params.applySpecificParameters(parameters(), {"block"});
     156        1222 :         params.template set<AuxVariableName>("variable") = vars[i];
     157        1222 :         params.template set<unsigned int>("component") = i;
     158        1222 :         params.template set<UserObjectName>("nodal_patch_recovery_uo") = "_npruo_" + prop_name;
     159        3666 :         params.template set<ExecFlagEnum>("execute_on") = {EXEC_INITIAL, EXEC_TIMESTEP_END};
     160        1222 :         _problem->addAuxKernel(type, auxnames[i], params);
     161             :       }
     162             :     }
     163             :     else
     164             :     {
     165             :       // elemental variables
     166         104 :       const auto & type = Registry::getClassName<ProjectedStatefulMaterialAuxTempl<T, is_ad>>();
     167        2548 :       for (const auto i : make_range(size))
     168             :       {
     169        2444 :         auto params = _factory.getValidParams(type);
     170        4888 :         params.applySpecificParameters(parameters(), {"block"});
     171        2444 :         params.template set<AuxVariableName>("variable") = vars[i];
     172        2444 :         params.template set<unsigned int>("component") = i;
     173        2444 :         params.template set<MaterialPropertyName>("prop") = prop_name;
     174        7332 :         params.template set<ExecFlagEnum>("execute_on") = {EXEC_INITIAL, EXEC_TIMESTEP_END};
     175        2444 :         _problem->addAuxKernel(type, auxnames[i], params);
     176             :       }
     177         104 :     }
     178         156 :   }
     179       15444 : }

Generated by: LCOV version 1.14