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

Generated by: LCOV version 1.14