https://mooseframework.inl.gov
MaterialBase.h
Go to the documentation of this file.
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 "MooseObject.h"
14 #include "BlockRestrictable.h"
15 #include "BoundaryRestrictable.h"
16 #include "SetupInterface.h"
18 #include "ScalarCoupleable.h"
19 #include "FunctionInterface.h"
20 #include "DistributionInterface.h"
21 #include "UserObjectInterface.h"
22 #include "TransientInterface.h"
23 #include "PostprocessorInterface.h"
26 #include "Restartable.h"
27 #include "MeshChangedInterface.h"
28 #include "OutputInterface.h"
29 #include "RandomInterface.h"
30 #include "ElementIDInterface.h"
31 #include "MaterialProperty.h"
32 #include "MaterialData.h"
33 #include "MathUtils.h"
34 #include "Assembly.h"
36 #include "ADFunctorInterface.h"
39 
40 #define usingMaterialBaseMembers \
41  usingMooseObjectMembers; \
42  usingTransientInterfaceMembers; \
43  using MaterialBase::_subproblem; \
44  using MaterialBase::_fe_problem; \
45  using MaterialBase::_tid; \
46  using MaterialBase::_assembly; \
47  using MaterialBase::_qp; \
48  using MaterialBase::_coord; \
49  using MaterialBase::_normals; \
50  using MaterialBase::_mesh
51 
52 // forward declarations
53 class MaterialBase;
54 class MooseMesh;
55 class SubProblem;
56 class FaceInfo;
57 class FEProblemBase;
58 
62 class MaterialBase : public MooseObject,
63  public BlockRestrictable,
64  public BoundaryRestrictable,
65  public SetupInterface,
67  public ScalarCoupleable,
68  public FunctionInterface,
69  public DistributionInterface,
70  public UserObjectInterface,
71  public TransientInterface,
75  public Restartable,
76  public MeshChangedInterface,
77  public OutputInterface,
78  public RandomInterface,
79  public ElementIDInterface,
80  protected GeometricSearchInterface,
81  protected ADFunctorInterface,
82  protected SolutionInvalidInterface
83 {
84 public:
86 
88 
89 #ifdef MOOSE_KOKKOS_ENABLED
90 
93  MaterialBase(const MaterialBase & object, const Moose::Kokkos::FunctorCopy & key);
94 #endif
95 
102  virtual void initStatefulProperties(const unsigned int n_points);
103 
104  virtual bool isInterfaceMaterial() { return false; };
105 
109  virtual void computeProperties() = 0;
110 
117  virtual void resetProperties();
118 
126  virtual void computePropertiesAtQp(unsigned int qp);
127 
129 
132  template <typename T>
133  MaterialProperty<T> & declarePropertyByName(const std::string & prop_name)
134  {
135  return declareGenericPropertyByName<T, false>(prop_name);
136  }
137  template <typename T>
138  MaterialProperty<T> & declareProperty(const std::string & name);
139  template <typename T>
140  ADMaterialProperty<T> & declareADPropertyByName(const std::string & prop_name)
141  {
142  return declareGenericPropertyByName<T, true>(prop_name);
143  }
144  template <typename T>
145  ADMaterialProperty<T> & declareADProperty(const std::string & name);
146 
147  template <typename T, bool is_ad>
148  auto & declareGenericProperty(const std::string & prop_name)
149  {
150  if constexpr (is_ad)
151  return declareADProperty<T>(prop_name);
152  else
153  return declareProperty<T>(prop_name);
154  }
155  template <typename T, bool is_ad>
156  GenericMaterialProperty<T, is_ad> & declareGenericPropertyByName(const std::string & prop_name);
158 
163  template <typename T, bool is_ad>
165  getGenericZeroMaterialProperty(const std::string & name);
166  template <typename T, bool is_ad>
168  getGenericZeroMaterialPropertyByName(const std::string & prop_name);
169 
173  template <typename T, bool is_ad>
175 
177  template <typename T, typename... Ts>
179  {
180  return getGenericZeroMaterialProperty<T, false>(args...);
181  }
182 
184  template <typename T, typename... Ts>
186  {
187  return getGenericZeroMaterialPropertyByName<T, false>(args...);
188  }
189 
194  virtual const std::set<std::string> & getRequestedItems() override { return _requested_props; }
195 
200  virtual const std::set<std::string> & getSuppliedItems() override { return _supplied_props; }
201 
206  const std::set<unsigned int> & getSuppliedPropIDs() { return _supplied_prop_ids; }
207 
208  void checkStatefulSanity() const;
209 
214  std::set<OutputName> getOutputs();
215 
219  virtual bool isBoundaryMaterial() const = 0;
220 
224  virtual void subdomainSetup() override;
225 
232  virtual const std::unordered_set<unsigned int> & getMatPropDependencies() const = 0;
233 
238 
242  bool hasRestoredProperties() const;
243 
250  virtual bool ghostable() const { return false; }
251 
252  void setFaceInfo(const FaceInfo & fi) { _face_info = &fi; }
253 
257  template <typename Consumers>
258  static std::deque<MaterialBase *>
259  buildRequiredMaterials(const Consumers & mat_consumers,
260  const std::vector<std::shared_ptr<MaterialBase>> & mats,
261  const bool allow_stateful);
262 
268  void setActiveProperties(const std::unordered_set<unsigned int> & needed_props);
269 
277  bool forceStatefulInit() const { return _force_stateful_init; }
278 
279 protected:
283  virtual void computeQpProperties();
284 
294  virtual void resetQpProperties();
295 
308  virtual void initQpStatefulProperties();
309 
310  virtual const MaterialData & materialData() const = 0;
311  virtual MaterialData & materialData() = 0;
313 
314  virtual const FEProblemBase & miProblem() const { return _fe_problem; }
315  virtual FEProblemBase & miProblem() { return _fe_problem; }
316 
317  virtual const QBase & qRule() const = 0;
318 
322  bool isPropertyActive(const unsigned int prop_id) const
323  {
324  return _active_prop_ids.count(prop_id) > 0;
325  }
326 
328 
332 
333  unsigned int _qp;
334 
338 
340 
343 
345  std::set<std::string> _requested_props;
346 
348  std::set<std::string> _supplied_props;
349 
357  std::set<unsigned int> _supplied_prop_ids;
358 
360  std::unordered_set<unsigned int> _active_prop_ids;
361 
363  const bool _compute;
364 
366  {
369  };
370 
374  std::unordered_map<unsigned int, unsigned int> _props_to_min_states;
375 
377  void registerPropName(const std::string & prop_name, bool is_get, const unsigned int state);
378 
380  void checkExecutionStage();
381 
382  std::vector<unsigned int> _displacements;
383 
385 
387 
388  const FaceInfo * _face_info = nullptr;
389 
391  const MaterialPropertyName _declare_suffix;
392 
393 private:
397  void markMatPropRequested(const std::string & name);
398 
407  void storeSubdomainZeroMatProp(SubdomainID block_id, const MaterialPropertyName & name);
408 
417  void storeBoundaryZeroMatProp(BoundaryID boundary_id, const MaterialPropertyName & name);
418 
422  unsigned int getMaxQps() const;
423 
426 
428  friend class FunctorMaterial;
429 };
430 
431 template <typename T>
434 {
435  // Check if the supplied parameter is a valid input parameter key
436  std::string prop_name = name;
437  if (_pars.have_parameter<MaterialPropertyName>(name))
438  prop_name = _pars.get<MaterialPropertyName>(name);
439 
440  return declarePropertyByName<T>(prop_name);
441 }
442 
443 template <typename T, bool is_ad>
445 MaterialBase::declareGenericPropertyByName(const std::string & prop_name)
446 {
447  const auto prop_name_modified =
448  _declare_suffix.empty()
449  ? prop_name
450  : MooseUtils::join(std::vector<std::string>({prop_name, _declare_suffix}), "_");
451 
452  // Call this before so that the ID is valid
453  auto & prop = materialData().declareProperty<T, is_ad>(prop_name_modified, *this);
454 
455  registerPropName(prop_name_modified, false, 0);
456  return prop;
457 }
458 
459 template <typename T, bool is_ad>
462 {
463  // Check if the supplied parameter is a valid input parameter key
464  std::string prop_name = name;
465  if (_pars.have_parameter<MaterialPropertyName>(name))
466  prop_name = _pars.get<MaterialPropertyName>(name);
467 
468  return getGenericZeroMaterialPropertyByName<T, is_ad>(prop_name);
469 }
470 
471 template <typename T, bool is_ad>
474 {
476  auto & preload_with_zero = materialData().getProperty<T, is_ad>(prop_name, 0, *this);
477 
478  _requested_props.insert(prop_name);
479  registerPropName(prop_name, true, 0);
480  markMatPropRequested(prop_name);
481 
482  // Register this material on these blocks and boundaries as a zero property with relaxed
483  // consistency checking
484  for (std::set<SubdomainID>::const_iterator it = blockIDs().begin(); it != blockIDs().end(); ++it)
485  storeSubdomainZeroMatProp(*it, prop_name);
486  for (std::set<BoundaryID>::const_iterator it = boundaryIDs().begin(); it != boundaryIDs().end();
487  ++it)
488  storeBoundaryZeroMatProp(*it, prop_name);
489 
490  // set values for all qpoints to zero
491  // (in multiapp scenarios getMaxQps can return different values in each app; we need the max)
492  unsigned int nqp = getMaxQps();
493  if (nqp > preload_with_zero.size())
494  preload_with_zero.resize(nqp);
495  for (unsigned int qp = 0; qp < nqp; ++qp)
496  MathUtils::mooseSetToZero(preload_with_zero[qp]);
497 
498  return preload_with_zero;
499 }
500 
501 template <typename T, bool is_ad>
504 {
505  // static zero property storage
507 
508  // resize to accomodate maximum number of qpoints
509  // (in multiapp scenarios getMaxQps can return different values in each app; we need the max)
510  unsigned int nqp = getMaxQps();
511  if (nqp > zero.size())
512  zero.resize(nqp);
513 
514  // set values for all qpoints to zero
515  for (unsigned int qp = 0; qp < nqp; ++qp)
517 
518  return zero;
519 }
520 
521 template <typename T>
524 {
525  // Check if the supplied parameter is a valid input parameter key
526  std::string prop_name = name;
527  if (_pars.have_parameter<MaterialPropertyName>(name))
528  prop_name = _pars.get<MaterialPropertyName>(name);
529 
530  return declareADPropertyByName<T>(prop_name);
531 }
532 
533 template <typename Consumers>
534 std::deque<MaterialBase *>
535 MaterialBase::buildRequiredMaterials(const Consumers & mat_consumers,
536  const std::vector<std::shared_ptr<MaterialBase>> & mats,
537  const bool allow_stateful)
538 {
539  std::deque<MaterialBase *> required_mats;
540 
541  std::unordered_set<unsigned int> needed_mat_props;
542  for (const auto & consumer : mat_consumers)
543  {
544  const auto & mp_deps = consumer->getMatPropDependencies();
545  needed_mat_props.insert(mp_deps.begin(), mp_deps.end());
546  }
547 
548  // A predicate of calling this function is that these materials come in already sorted by
549  // dependency with the front of the container having no other material dependencies and following
550  // materials potentially depending on the ones in front of them. So we can start at the back and
551  // iterate forward checking whether the current material supplies anything that is needed, and if
552  // not we discard it
553  for (auto it = mats.rbegin(); it != mats.rend(); ++it)
554  {
555  auto * const mat = it->get();
556  bool supplies_needed = false;
557 
558  const auto & supplied_props = mat->getSuppliedPropIDs();
559 
560  // Do O(N) with the small container
561  for (const auto supplied_prop : supplied_props)
562  {
563  if (needed_mat_props.count(supplied_prop))
564  {
565  supplies_needed = true;
566  break;
567  }
568  }
569 
570  if (!supplies_needed)
571  continue;
572 
573  if (!allow_stateful && mat->hasStatefulProperties())
574  ::mooseError(
575  "Someone called buildRequiredMaterials with allow_stateful = false but a material "
576  "dependency ",
577  mat->name(),
578  " computes stateful properties.");
579 
580  const auto & mp_deps = mat->getMatPropDependencies();
581  needed_mat_props.insert(mp_deps.begin(), mp_deps.end());
582  required_mats.push_front(mat);
583  }
584 
585  return required_mats;
586 }
virtual void initStatefulProperties(const unsigned int n_points)
Initialize stateful properties (if material has some)
Definition: MaterialBase.C:160
Interface for objects that need parallel consistent random numbers without patterns over the course o...
const MaterialPropertyName _declare_suffix
Suffix to append to the name of the material property/ies when declaring it/them. ...
Definition: MaterialBase.h:391
virtual bool ghostable() const
Whether this material supports ghosted computations.
Definition: MaterialBase.h:250
std::string join(Iterator begin, Iterator end, const std::string &delimiter)
Python-like join function for strings over an iterator range.
Definition: MooseUtils.h:142
void mooseSetToZero(T &v)
Helper function templates to set a variable to zero.
Definition: MathUtils.h:372
FEProblemBase & _fe_problem
Definition: MaterialBase.h:329
A class for creating restricted objects.
Definition: Restartable.h:28
const InputParameters & _pars
The object&#39;s parameters.
Definition: MooseBase.h:366
virtual void computeQpProperties()
Users must override this method.
Definition: MaterialBase.C:263
MaterialProperty< T > & declareProperty(const std::string &name)
Definition: MaterialBase.h:433
MaterialBase(const InputParameters &parameters)
Definition: MaterialBase.C:77
virtual FEProblemBase & miProblem()
Definition: MaterialBase.h:315
Keeps track of stuff related to assembling.
Definition: Assembly.h:109
static InputParameters validParams()
Definition: MaterialBase.C:21
void setFaceInfo(const FaceInfo &fi)
Definition: MaterialBase.h:252
bool _has_stateful_property
Definition: MaterialBase.h:384
bool hasStatefulProperties() const
Definition: MaterialBase.h:237
MaterialProperty< T > & declarePropertyByName(const std::string &prop_name)
Declare the property named "name".
Definition: MaterialBase.h:133
virtual void resetProperties()
Resets the properties at each quadrature point (see resetQpProperties), only called if &#39;compute = fal...
Definition: MaterialBase.C:268
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
SubProblem & _subproblem
Definition: MaterialBase.h:327
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseBase.h:131
const Moose::CoordinateSystemType & _coord_sys
Coordinate system.
Definition: MaterialBase.h:342
MaterialDataType
MaterialData types.
Definition: MooseTypes.h:692
A class to provide an common interface to objects requiring "outputs" option.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
auto & declareGenericProperty(const std::string &prop_name)
Definition: MaterialBase.h:148
/class BoundaryRestrictable /brief Provides functionality for limiting the object to certain boundary...
std::set< OutputName > getOutputs()
Get the list of output objects that this class is restricted.
Definition: MaterialBase.C:244
const MaterialProperty< T > & getZeroMaterialPropertyByName(Ts... args)
for backwards compatibility
Definition: MaterialBase.h:185
virtual const std::set< std::string > & getRequestedItems() override
Return a set of properties accessed with getMaterialProperty.
Definition: MaterialBase.h:194
virtual void initQpStatefulProperties()
Initialize stateful properties at quadrature points.
Definition: MaterialBase.C:179
virtual void subdomainSetup() override
Subdomain setup evaluating material properties when required.
Definition: MaterialBase.C:251
virtual const std::unordered_set< unsigned int > & getMatPropDependencies() const =0
Retrieve the set of material properties that this object depends on.
virtual const std::set< SubdomainID > & blockIDs() const
Return the block subdomain ids for this object Note, if this is not block restricted, this function returns all mesh subdomain ids.
const Number zero
virtual const QBase & qRule() const =0
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
virtual void computeProperties()=0
Performs the quadrature point loop, calling computeQpProperties.
Definition: MaterialBase.C:257
void checkStatefulSanity() const
Definition: MaterialBase.C:185
An interface for accessing Moose::Functors for systems that care about automatic differentiation, e.g.
std::unordered_map< unsigned int, unsigned int > _props_to_min_states
The minimum states requested (0 = current, 1 = old, 2 = older) This is sparse and is used to keep tra...
Definition: MaterialBase.h:374
virtual void resetQpProperties()
Resets the properties prior to calculation of traditional materials (only if &#39;compute = false&#39;)...
Definition: MaterialBase.C:275
ADMaterialProperty< T > & declareADPropertyByName(const std::string &prop_name)
Definition: MaterialBase.h:140
void storeBoundaryZeroMatProp(BoundaryID boundary_id, const MaterialPropertyName &name)
Adds to a map based on boundary ids of material properties for which a zero value can be returned...
Definition: MaterialBase.C:313
unsigned int _qp
Definition: MaterialBase.h:333
An interface that allows the marking of invalid solutions during a solve.
Interface for objects that needs transient capabilities.
const FaceInfo * _face_info
Definition: MaterialBase.h:388
This data structure is used to store geometric and variable related metadata about each cell face in ...
Definition: FaceInfo.h:36
virtual const std::set< std::string > & getSuppliedItems() override
Return a set of properties accessed with declareProperty.
Definition: MaterialBase.h:200
virtual const FEProblemBase & miProblem() const
Definition: MaterialBase.h:314
const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:103
FunctorMaterials compute functor material properties.
std::set< std::string > _requested_props
Set of properties accessed via get method.
Definition: MaterialBase.h:345
MooseMesh & _mesh
Definition: MaterialBase.h:339
unsigned int getMaxQps() const
Definition: MaterialBase.C:319
Interface for notifications that the mesh has changed.
const bool _compute
If False MOOSE does not compute this property.
Definition: MaterialBase.h:363
THREAD_ID _tid
Definition: MaterialBase.h:330
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:27
void checkExecutionStage()
Check and throw an error if the execution has progressed past the construction stage.
Definition: MaterialBase.C:293
Assembly & _assembly
Definition: MaterialBase.h:331
boundary_id_type BoundaryID
Interface for objects that need to use distributions.
typename GenericMaterialPropertyStruct< T, is_ad >::type GenericMaterialProperty
std::unordered_set< unsigned int > _active_prop_ids
The ids of the current active supplied properties.
Definition: MaterialBase.h:360
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:92
std::set< unsigned int > _supplied_prop_ids
The ids of the supplied properties, i.e.
Definition: MaterialBase.h:357
bool forceStatefulInit() const
Definition: MaterialBase.h:277
Interface for objects that need to use UserObjects.
bool _overrides_init_stateful_props
Definition: MaterialBase.h:386
const bool _force_stateful_init
Whether or not to force stateful init; see forceStatefulInit()
Definition: MaterialBase.h:425
bool have_parameter(std::string_view name) const
A wrapper around the Parameters base class method.
void setActiveProperties(const std::unordered_set< unsigned int > &needed_props)
Set active properties of this material Note: This function is called by FEProblemBase::setActiveMater...
Definition: MaterialBase.C:235
virtual Moose::MaterialDataType materialDataType()=0
static constexpr PropertyValue::id_type zero_property_id
The material property ID for a zero property.
std::vector< unsigned int > _displacements
Definition: MaterialBase.h:382
Generic class for solving transient nonlinear problems.
Definition: SubProblem.h:78
void markMatPropRequested(const std::string &name)
Helper method for adding a material property name to the material property requested set...
Definition: MaterialBase.C:301
CoordinateSystemType
Definition: MooseTypes.h:810
const MooseArray< Point > & _normals
normals at quadrature points (valid only in boundary materials)
Definition: MaterialBase.h:337
ADMaterialProperty< T > & declareADProperty(const std::string &name)
Definition: MaterialBase.h:523
const GenericMaterialProperty< T, is_ad > & getGenericZeroMaterialPropertyByName(const std::string &prop_name)
Definition: MaterialBase.h:473
GenericMaterialProperty< T, is_ad > & getProperty(const std::string &prop_name, const unsigned int state, const MooseObject &requestor)
Retrieves a material property.
Definition: MaterialData.h:127
static std::deque< MaterialBase * > buildRequiredMaterials(const Consumers &mat_consumers, const std::vector< std::shared_ptr< MaterialBase >> &mats, const bool allow_stateful)
Build the materials required by a set of consumer objects.
Definition: MaterialBase.h:535
const GenericMaterialProperty< T, is_ad > & getGenericZeroMaterialProperty()
Return a constant zero anonymous material property.
Definition: MaterialBase.h:503
An interface that restricts an object to subdomains via the &#39;blocks&#39; input parameter.
bool isPropertyActive(const unsigned int prop_id) const
Check whether a material property is active.
Definition: MaterialBase.h:322
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition: MooseBase.h:271
Interface for sorting dependent vectors of objects.
Interface for objects that needs scalar coupling capabilities.
Proxy for accessing MaterialPropertyStorage.
Definition: MaterialData.h:37
void registerPropName(const std::string &prop_name, bool is_get, const unsigned int state)
Small helper function to call store{Subdomain,Boundary}MatPropName.
Definition: MaterialBase.C:205
virtual void computePropertiesAtQp(unsigned int qp)
A method for (re)computing the properties of a MaterialBase.
Definition: MaterialBase.C:286
std::set< std::string > _supplied_props
Set of properties declared.
Definition: MaterialBase.h:348
GenericMaterialProperty< T, is_ad > & declareGenericPropertyByName(const std::string &prop_name)
Definition: MaterialBase.h:445
const MaterialProperty< T > & getZeroMaterialProperty(Ts... args)
for backwards compatibility
Definition: MaterialBase.h:178
const MooseArray< Real > & _coord
Definition: MaterialBase.h:335
virtual const std::set< BoundaryID > & boundaryIDs() const
Return the boundary IDs for this object.
virtual const MaterialData & materialData() const =0
void storeSubdomainZeroMatProp(SubdomainID block_id, const MaterialPropertyName &name)
Adds to a map based on block ids of material properties for which a zero value can be returned...
Definition: MaterialBase.C:307
MaterialBases compute MaterialProperties.
Definition: MaterialBase.h:62
Interface for objects that need to use functions.
virtual bool isBoundaryMaterial() const =0
Returns true of the MaterialData type is not associated with volume data.
const std::set< unsigned int > & getSuppliedPropIDs()
Get the prop ids corresponding to declareProperty.
Definition: MaterialBase.h:206
GenericMaterialProperty< T, is_ad > & declareProperty(const std::string &prop_name, const MooseObject &requestor)
Declares a material property.
Definition: MaterialData.h:142
Interface class for classes which interact with Postprocessors.
virtual bool isInterfaceMaterial()
Definition: MaterialBase.h:104
unsigned int THREAD_ID
Definition: MooseTypes.h:209
bool hasRestoredProperties() const
Definition: MaterialBase.C:195