LCOV - code coverage report
Current view: top level - src/materials - MaterialBase.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 6f668f Lines: 121 135 89.6 %
Date: 2025-09-22 20:01:15 Functions: 17 21 81.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             : // MOOSE includes
      11             : #include "MaterialBase.h"
      12             : #include "MaterialPropertyStorage.h"
      13             : #include "FEProblemBase.h"
      14             : #include "Assembly.h"
      15             : #include "Executioner.h"
      16             : #include "Transient.h"
      17             : 
      18             : #include "libmesh/quadrature.h"
      19             : 
      20             : InputParameters
      21     2410022 : MaterialBase::validParams()
      22             : {
      23             : 
      24     2410022 :   InputParameters params = MooseObject::validParams();
      25     2410022 :   params += BlockRestrictable::validParams();
      26     2410022 :   params += BoundaryRestrictable::validParams();
      27     2410022 :   params += TransientInterface::validParams();
      28     2410022 :   params += RandomInterface::validParams();
      29     2410022 :   params += ADFunctorInterface::validParams();
      30             : 
      31     7230066 :   params.addParam<bool>("use_displaced_mesh",
      32     4820044 :                         false,
      33             :                         "Whether or not this object should use the "
      34             :                         "displaced mesh for computation.  Note that "
      35             :                         "in the case this is true but no "
      36             :                         "displacements are provided in the Mesh block "
      37             :                         "the undisplaced mesh will still be used.");
      38     4820044 :   params.addParam<bool>("compute",
      39     4820044 :                         true,
      40             :                         "When false, MOOSE will not call compute methods on this material. "
      41             :                         "The user must call computeProperties() after retrieving the MaterialBase "
      42             :                         "via MaterialBasePropertyInterface::getMaterialBase(). "
      43             :                         "Non-computed MaterialBases are not sorted for dependencies.");
      44             : 
      45     4820044 :   params.addPrivateParam<bool>("_neighbor", false);
      46     4820044 :   params.addPrivateParam<bool>("_interface", false);
      47             : 
      48             :   // Forces the calling of initStatefulProperties() even when this material
      49             :   // does not declare any properties that are stateful. Right now,
      50             :   // this is only used for porous_flow... pretty please keep it that way?
      51     4820044 :   params.addPrivateParam<bool>("_force_stateful_init", false);
      52             : 
      53             :   // Outputs
      54     2410022 :   params += OutputInterface::validParams();
      55     7230066 :   params.set<std::vector<OutputName>>("outputs") = {"none"};
      56     9640088 :   params.addParam<std::vector<std::string>>(
      57             :       "output_properties",
      58             :       {},
      59             :       "List of material properties, from this material, to output (outputs "
      60             :       "must also be defined to an output type)");
      61     7230066 :   params.addParam<MaterialPropertyName>(
      62             :       "declare_suffix",
      63             :       "",
      64             :       "An optional suffix parameter that can be appended to any declared properties. The suffix "
      65             :       "will be prepended with a '_' character.");
      66             : 
      67             :   // Allow Material objects to be enabled/disabled by Control objects
      68     7230066 :   params.declareControllable("enable");
      69             : 
      70     9640088 :   params.addParamNamesToGroup("outputs output_properties", "Outputs");
      71     9640088 :   params.addParamNamesToGroup("use_displaced_mesh", "Advanced");
      72     2410022 :   params.registerBase("MaterialBase");
      73     2410022 :   return params;
      74     2410022 : }
      75             : 
      76       39403 : MaterialBase::MaterialBase(const InputParameters & parameters)
      77             :   : MooseObject(parameters),
      78             :     BlockRestrictable(this),
      79             :     BoundaryRestrictable(this, blockIDs(), false), // false for being _not_ nodal
      80             :     SetupInterface(this),
      81             :     MooseVariableDependencyInterface(this),
      82             :     ScalarCoupleable(this),
      83             :     FunctionInterface(this),
      84             :     DistributionInterface(this),
      85             :     UserObjectInterface(this),
      86             :     TransientInterface(this),
      87             :     PostprocessorInterface(this),
      88             :     VectorPostprocessorInterface(this),
      89             :     DependencyResolverInterface(),
      90             :     Restartable(this, "MaterialBases"),
      91             :     MeshChangedInterface(parameters),
      92             :     // The false flag disables the automatic call buildOutputVariableHideList;
      93             :     // for MaterialBase objects the hide lists are handled by MaterialBaseOutputAction
      94             :     OutputInterface(parameters, false),
      95             :     RandomInterface(parameters,
      96      157612 :                     *parameters.getCheckedPointerParam<FEProblemBase *>("_fe_problem_base"),
      97       39403 :                     parameters.get<THREAD_ID>("_tid"),
      98             :                     false),
      99             :     ElementIDInterface(this),
     100             :     GeometricSearchInterface(this),
     101             :     ADFunctorInterface(this),
     102             :     SolutionInvalidInterface(this),
     103      118209 :     _subproblem(*getCheckedPointerParam<SubProblem *>("_subproblem")),
     104      157612 :     _fe_problem(*getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")),
     105       39403 :     _tid(parameters.get<THREAD_ID>("_tid")),
     106       39403 :     _assembly(_subproblem.assembly(_tid, 0)),
     107       39403 :     _qp(std::numeric_limits<unsigned int>::max()),
     108       39403 :     _coord(_assembly.coordTransformation()),
     109       39403 :     _normals(_assembly.normals()),
     110       39403 :     _mesh(_subproblem.mesh()),
     111       39403 :     _coord_sys(_assembly.coordSystem()),
     112       78806 :     _compute(getParam<bool>("compute")),
     113       39403 :     _has_stateful_property(false),
     114       39403 :     _declare_suffix(getParam<MaterialPropertyName>("declare_suffix")),
     115      394030 :     _force_stateful_init(getParam<bool>("_force_stateful_init"))
     116             : {
     117       39403 : }
     118             : 
     119             : #ifdef MOOSE_KOKKOS_ENABLED
     120       36803 : MaterialBase::MaterialBase(const MaterialBase & object, const Moose::Kokkos::FunctorCopy & key)
     121             :   : MooseObject(object, key),
     122             :     BlockRestrictable(object, key),
     123             :     BoundaryRestrictable(object, key),
     124             :     SetupInterface(object, key),
     125             :     MooseVariableDependencyInterface(object, key),
     126             :     ScalarCoupleable(object, key),
     127             :     FunctionInterface(object, key),
     128             :     DistributionInterface(object, key),
     129             :     UserObjectInterface(object, key),
     130             :     TransientInterface(object, key),
     131             :     PostprocessorInterface(object, key),
     132             :     VectorPostprocessorInterface(object, key),
     133             :     DependencyResolverInterface(object, key),
     134             :     Restartable(object, key),
     135             :     MeshChangedInterface(object, key),
     136             :     OutputInterface(object, key),
     137             :     RandomInterface(object, key),
     138             :     ElementIDInterface(object, key),
     139             :     GeometricSearchInterface(object, key),
     140             :     ADFunctorInterface(object, key),
     141             :     SolutionInvalidInterface(object, key),
     142       36803 :     _subproblem(object._subproblem),
     143       36803 :     _fe_problem(object._fe_problem),
     144       36803 :     _tid(object._tid),
     145       36803 :     _assembly(object._assembly),
     146       36803 :     _coord(object._coord),
     147       36803 :     _normals(object._normals),
     148       36803 :     _mesh(object._mesh),
     149       36803 :     _coord_sys(object._coord_sys),
     150       36803 :     _compute(object._compute),
     151       36803 :     _has_stateful_property(object._has_stateful_property),
     152       36803 :     _declare_suffix(object._declare_suffix),
     153       36803 :     _force_stateful_init(object._force_stateful_init)
     154             : {
     155       36803 : }
     156             : #endif
     157             : 
     158             : void
     159      664364 : MaterialBase::initStatefulProperties(const unsigned int n_points)
     160             : {
     161     3363952 :   for (_qp = 0; _qp < n_points; ++_qp)
     162     2699588 :     initQpStatefulProperties();
     163             : 
     164             :   // checking for statefulness of properties via this loop is necessary
     165             :   // because owned props might have been promoted to stateful by calls to
     166             :   // getMaterialProperty[Old/Older] from other objects.  In these cases, this
     167             :   // object won't otherwise know that it owns stateful properties.
     168     2201266 :   for (const auto id : _supplied_prop_ids)
     169     1984972 :     if (materialData().getMaterialPropertyStorage().getPropRecord(id).stateful() &&
     170      448070 :         !_overrides_init_stateful_props)
     171           0 :       mooseWarning(std::string("Material \"") + name() +
     172             :                    "\" provides one or more stateful "
     173             :                    "properties but initQpStatefulProperties() "
     174             :                    "was not overridden in the derived class.");
     175      664364 : }
     176             : 
     177             : void
     178           0 : MaterialBase::initQpStatefulProperties()
     179             : {
     180           0 :   _overrides_init_stateful_props = false;
     181           0 : }
     182             : 
     183             : void
     184       14536 : MaterialBase::checkStatefulSanity() const
     185             : {
     186       35794 :   for (const auto & [id, min_state] : _props_to_min_states)
     187       21258 :     if (min_state > 0)
     188           0 :       mooseError("The stateful property '",
     189           0 :                  _fe_problem.getMaterialPropertyRegistry().getName(id),
     190             :                  "' is undefined");
     191       14536 : }
     192             : 
     193             : bool
     194        1016 : MaterialBase::hasRestoredProperties() const
     195             : {
     196        1946 :   for (auto & prop : _supplied_props)
     197        1064 :     if (materialData().getMaterialPropertyStorage().isRestoredProperty(prop))
     198         134 :       return true;
     199             : 
     200         882 :   return false;
     201             : }
     202             : 
     203             : void
     204       85179 : MaterialBase::registerPropName(const std::string & prop_name, bool is_get, const unsigned int state)
     205             : {
     206       85179 :   if (!is_get)
     207             :   {
     208       65285 :     const auto property_id = materialData().getPropertyId(prop_name);
     209             : 
     210       65285 :     _supplied_props.insert(prop_name);
     211       65285 :     _supplied_prop_ids.insert(property_id);
     212             : 
     213             :     // Store the minimum state declared
     214       65285 :     auto find_min_state = _props_to_min_states.find(property_id);
     215       65285 :     if (find_min_state == _props_to_min_states.end())
     216       65285 :       _props_to_min_states.emplace(property_id, state);
     217             :     else
     218           0 :       find_min_state->second = std::min(find_min_state->second, state);
     219             : 
     220             :     // Store material properties for block ids
     221      135657 :     for (const auto & block_id : blockIDs())
     222       70372 :       _fe_problem.storeSubdomainMatPropName(block_id, prop_name);
     223             : 
     224             :     // Store material properties for the boundary ids
     225      131633 :     for (const auto & boundary_id : boundaryIDs())
     226       66348 :       _fe_problem.storeBoundaryMatPropName(boundary_id, prop_name);
     227             :   }
     228             : 
     229       85179 :   if (state > 0)
     230        5184 :     _has_stateful_property = true;
     231       85179 : }
     232             : 
     233             : void
     234     3088693 : MaterialBase::setActiveProperties(const std::unordered_set<unsigned int> & needed_props)
     235             : {
     236     3088693 :   _active_prop_ids.clear();
     237     7406370 :   for (const auto supplied_id : _supplied_prop_ids)
     238     4317677 :     if (needed_props.count(supplied_id))
     239     2093992 :       _active_prop_ids.insert(supplied_id);
     240     3088693 : }
     241             : 
     242             : std::set<OutputName>
     243       44366 : MaterialBase::getOutputs()
     244             : {
     245       88732 :   const std::vector<OutputName> & out = getParam<std::vector<OutputName>>("outputs");
     246       44366 :   return std::set<OutputName>(out.begin(), out.end());
     247             : }
     248             : 
     249             : void
     250           0 : MaterialBase::subdomainSetup()
     251             : {
     252           0 :   mooseError("MaterialBase::subdomainSetup in Material'", name(), "' needs to be implemented");
     253             : }
     254             : 
     255             : void
     256           0 : MaterialBase::computeProperties()
     257             : {
     258           0 :   mooseError("MaterialBase::computeProperties in Material '", name(), "' needs to be implemented");
     259             : }
     260             : 
     261             : void
     262           0 : MaterialBase::computeQpProperties()
     263             : {
     264           0 : }
     265             : 
     266             : void
     267        3089 : MaterialBase::resetProperties()
     268             : {
     269       15429 :   for (_qp = 0; _qp < qRule().n_points(); ++_qp)
     270       12344 :     resetQpProperties();
     271        3085 : }
     272             : 
     273             : void
     274         204 : MaterialBase::resetQpProperties()
     275             : {
     276         204 :   if (!_compute)
     277         204 :     mooseDoOnce(mooseWarning("You disabled the computation of this (",
     278             :                              name(),
     279             :                              ") material by MOOSE, but have not overridden the 'resetQpProperties' "
     280             :                              "method, this can lead to unintended values being set for material "
     281             :                              "property values."));
     282         200 : }
     283             : 
     284             : void
     285      135360 : MaterialBase::computePropertiesAtQp(unsigned int qp)
     286             : {
     287      135360 :   _qp = qp;
     288      135360 :   computeQpProperties();
     289      135360 : }
     290             : 
     291             : void
     292       19894 : MaterialBase::checkExecutionStage()
     293             : {
     294       19894 :   if (_fe_problem.startedInitialSetup())
     295           0 :     mooseError("Material properties must be retrieved during material object construction to "
     296             :                "ensure correct dependency resolution.");
     297       19894 : }
     298             : 
     299             : void
     300        9522 : MaterialBase::markMatPropRequested(const std::string & name)
     301             : {
     302        9522 :   _fe_problem.markMatPropRequested(name);
     303        9522 : }
     304             : 
     305             : void
     306        9522 : MaterialBase::storeSubdomainZeroMatProp(SubdomainID block_id, const MaterialPropertyName & name)
     307             : {
     308        9522 :   _fe_problem.storeSubdomainZeroMatProp(block_id, name);
     309        9522 : }
     310             : 
     311             : void
     312        9522 : MaterialBase::storeBoundaryZeroMatProp(BoundaryID boundary_id, const MaterialPropertyName & name)
     313             : {
     314        9522 :   _fe_problem.storeBoundaryZeroMatProp(boundary_id, name);
     315        9522 : }
     316             : 
     317             : unsigned int
     318       10134 : MaterialBase::getMaxQps() const
     319             : {
     320       10134 :   return _fe_problem.getMaxQps();
     321             : }

Generated by: LCOV version 1.14