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 {
83 public:
85 
87 
88 #ifdef MOOSE_KOKKOS_ENABLED
89 
92  MaterialBase(const MaterialBase & object, const Moose::Kokkos::FunctorCopy & key);
93 #endif
94 
101  virtual void initStatefulProperties(const unsigned int n_points);
102 
103  virtual bool isInterfaceMaterial() { return false; };
104 
108  virtual void computeProperties() = 0;
109 
116  virtual void resetProperties();
117 
125  virtual void computePropertiesAtQp(unsigned int qp);
126 
128 
131  template <typename T>
132  MaterialProperty<T> & declarePropertyByName(const std::string & prop_name)
133  {
134  return declareGenericPropertyByName<T, false>(prop_name);
135  }
136  template <typename T>
137  MaterialProperty<T> & declareProperty(const std::string & name);
138  template <typename T>
139  ADMaterialProperty<T> & declareADPropertyByName(const std::string & prop_name)
140  {
141  return declareGenericPropertyByName<T, true>(prop_name);
142  }
143  template <typename T>
144  ADMaterialProperty<T> & declareADProperty(const std::string & name);
145 
146  template <typename T, bool is_ad>
147  auto & declareGenericProperty(const std::string & prop_name)
148  {
149  if constexpr (is_ad)
150  return declareADProperty<T>(prop_name);
151  else
152  return declareProperty<T>(prop_name);
153  }
154  template <typename T, bool is_ad>
155  GenericMaterialProperty<T, is_ad> & declareGenericPropertyByName(const std::string & prop_name);
157 
162  template <typename T, bool is_ad>
164  getGenericZeroMaterialProperty(const std::string & name);
165  template <typename T, bool is_ad>
167  getGenericZeroMaterialPropertyByName(const std::string & prop_name);
168 
172  template <typename T, bool is_ad>
174 
176  template <typename T, typename... Ts>
178  {
179  return getGenericZeroMaterialProperty<T, false>(args...);
180  }
181 
183  template <typename T, typename... Ts>
185  {
186  return getGenericZeroMaterialPropertyByName<T, false>(args...);
187  }
188 
193  virtual const std::set<std::string> & getRequestedItems() override { return _requested_props; }
194 
199  virtual const std::set<std::string> & getSuppliedItems() override { return _supplied_props; }
200 
205  const std::set<unsigned int> & getSuppliedPropIDs() { return _supplied_prop_ids; }
206 
207  void checkStatefulSanity() const;
208 
213  std::set<OutputName> getOutputs();
214 
218  virtual bool isBoundaryMaterial() const = 0;
219 
223  virtual void subdomainSetup() override;
224 
231  virtual const std::unordered_set<unsigned int> & getMatPropDependencies() const = 0;
232 
237 
241  bool hasRestoredProperties() const;
242 
249  virtual bool ghostable() const { return false; }
250 
251  void setFaceInfo(const FaceInfo & fi) { _face_info = &fi; }
252 
256  template <typename Consumers>
257  static std::deque<MaterialBase *>
258  buildRequiredMaterials(const Consumers & mat_consumers,
259  const std::vector<std::shared_ptr<MaterialBase>> & mats,
260  const bool allow_stateful);
261 
267  void setActiveProperties(const std::unordered_set<unsigned int> & needed_props);
268 
276  bool forceStatefulInit() const { return _force_stateful_init; }
277 
278 protected:
282  virtual void computeQpProperties();
283 
293  virtual void resetQpProperties();
294 
307  virtual void initQpStatefulProperties();
308 
309  virtual const MaterialData & materialData() const = 0;
310  virtual MaterialData & materialData() = 0;
312 
313  virtual const FEProblemBase & miProblem() const { return _fe_problem; }
314  virtual FEProblemBase & miProblem() { return _fe_problem; }
315 
316  virtual const QBase & qRule() const = 0;
317 
321  bool isPropertyActive(const unsigned int prop_id) const
322  {
323  return _active_prop_ids.count(prop_id) > 0;
324  }
325 
327 
331 
332  unsigned int _qp;
333 
337 
339 
342 
344  std::set<std::string> _requested_props;
345 
347  std::set<std::string> _supplied_props;
348 
356  std::set<unsigned int> _supplied_prop_ids;
357 
359  std::unordered_set<unsigned int> _active_prop_ids;
360 
362  const bool _compute;
363 
365  {
368  };
369 
373  std::unordered_map<unsigned int, unsigned int> _props_to_min_states;
374 
376  void registerPropName(const std::string & prop_name, bool is_get, const unsigned int state);
377 
379  void checkExecutionStage();
380 
381  std::vector<unsigned int> _displacements;
382 
384 
386 
387  const FaceInfo * _face_info = nullptr;
388 
390  const MaterialPropertyName _declare_suffix;
391 
392 private:
396  void markMatPropRequested(const std::string & name);
397 
406  void storeSubdomainZeroMatProp(SubdomainID block_id, const MaterialPropertyName & name);
407 
416  void storeBoundaryZeroMatProp(BoundaryID boundary_id, const MaterialPropertyName & name);
417 
421  unsigned int getMaxQps() const;
422 
425 
427  friend class FunctorMaterial;
428 };
429 
430 template <typename T>
433 {
434  // Check if the supplied parameter is a valid input parameter key
435  std::string prop_name = name;
436  if (_pars.have_parameter<MaterialPropertyName>(name))
437  prop_name = _pars.get<MaterialPropertyName>(name);
438 
439  return declarePropertyByName<T>(prop_name);
440 }
441 
442 template <typename T, bool is_ad>
444 MaterialBase::declareGenericPropertyByName(const std::string & prop_name)
445 {
446  const auto prop_name_modified =
447  _declare_suffix.empty()
448  ? prop_name
449  : MooseUtils::join(std::vector<std::string>({prop_name, _declare_suffix}), "_");
450 
451  // Call this before so that the ID is valid
452  auto & prop = materialData().declareProperty<T, is_ad>(prop_name_modified, *this);
453 
454  registerPropName(prop_name_modified, false, 0);
455  return prop;
456 }
457 
458 template <typename T, bool is_ad>
461 {
462  // Check if the supplied parameter is a valid input parameter key
463  std::string prop_name = name;
464  if (_pars.have_parameter<MaterialPropertyName>(name))
465  prop_name = _pars.get<MaterialPropertyName>(name);
466 
467  return getGenericZeroMaterialPropertyByName<T, is_ad>(prop_name);
468 }
469 
470 template <typename T, bool is_ad>
473 {
475  auto & preload_with_zero = materialData().getProperty<T, is_ad>(prop_name, 0, *this);
476 
477  _requested_props.insert(prop_name);
478  registerPropName(prop_name, true, 0);
479  markMatPropRequested(prop_name);
480 
481  // Register this material on these blocks and boundaries as a zero property with relaxed
482  // consistency checking
483  for (std::set<SubdomainID>::const_iterator it = blockIDs().begin(); it != blockIDs().end(); ++it)
484  storeSubdomainZeroMatProp(*it, prop_name);
485  for (std::set<BoundaryID>::const_iterator it = boundaryIDs().begin(); it != boundaryIDs().end();
486  ++it)
487  storeBoundaryZeroMatProp(*it, prop_name);
488 
489  // set values for all qpoints to zero
490  // (in multiapp scenarios getMaxQps can return different values in each app; we need the max)
491  unsigned int nqp = getMaxQps();
492  if (nqp > preload_with_zero.size())
493  preload_with_zero.resize(nqp);
494  for (unsigned int qp = 0; qp < nqp; ++qp)
495  MathUtils::mooseSetToZero(preload_with_zero[qp]);
496 
497  return preload_with_zero;
498 }
499 
500 template <typename T, bool is_ad>
503 {
504  // static zero property storage
506 
507  // resize to accomodate maximum number of qpoints
508  // (in multiapp scenarios getMaxQps can return different values in each app; we need the max)
509  unsigned int nqp = getMaxQps();
510  if (nqp > zero.size())
511  zero.resize(nqp);
512 
513  // set values for all qpoints to zero
514  for (unsigned int qp = 0; qp < nqp; ++qp)
516 
517  return zero;
518 }
519 
520 template <typename T>
523 {
524  // Check if the supplied parameter is a valid input parameter key
525  std::string prop_name = name;
526  if (_pars.have_parameter<MaterialPropertyName>(name))
527  prop_name = _pars.get<MaterialPropertyName>(name);
528 
529  return declareADPropertyByName<T>(prop_name);
530 }
531 
532 template <typename Consumers>
533 std::deque<MaterialBase *>
534 MaterialBase::buildRequiredMaterials(const Consumers & mat_consumers,
535  const std::vector<std::shared_ptr<MaterialBase>> & mats,
536  const bool allow_stateful)
537 {
538  std::deque<MaterialBase *> required_mats;
539 
540  std::unordered_set<unsigned int> needed_mat_props;
541  for (const auto & consumer : mat_consumers)
542  {
543  const auto & mp_deps = consumer->getMatPropDependencies();
544  needed_mat_props.insert(mp_deps.begin(), mp_deps.end());
545  }
546 
547  // A predicate of calling this function is that these materials come in already sorted by
548  // dependency with the front of the container having no other material dependencies and following
549  // materials potentially depending on the ones in front of them. So we can start at the back and
550  // iterate forward checking whether the current material supplies anything that is needed, and if
551  // not we discard it
552  for (auto it = mats.rbegin(); it != mats.rend(); ++it)
553  {
554  auto * const mat = it->get();
555  bool supplies_needed = false;
556 
557  const auto & supplied_props = mat->getSuppliedPropIDs();
558 
559  // Do O(N) with the small container
560  for (const auto supplied_prop : supplied_props)
561  {
562  if (needed_mat_props.count(supplied_prop))
563  {
564  supplies_needed = true;
565  break;
566  }
567  }
568 
569  if (!supplies_needed)
570  continue;
571 
572  if (!allow_stateful && mat->hasStatefulProperties())
573  ::mooseError(
574  "Someone called buildRequiredMaterials with allow_stateful = false but a material "
575  "dependency ",
576  mat->name(),
577  " computes stateful properties.");
578 
579  const auto & mp_deps = mat->getMatPropDependencies();
580  needed_mat_props.insert(mp_deps.begin(), mp_deps.end());
581  required_mats.push_front(mat);
582  }
583 
584  return required_mats;
585 }
virtual void initStatefulProperties(const unsigned int n_points)
Initialize stateful properties (if material has some)
Definition: MaterialBase.C:158
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:390
virtual bool ghostable() const
Whether this material supports ghosted computations.
Definition: MaterialBase.h:249
std::string join(Iterator begin, Iterator end, const std::string &delimiter)
Python-like join function for strings over an iterator range.
Definition: MooseUtils.h:144
void mooseSetToZero(T &v)
Helper function templates to set a variable to zero.
Definition: MathUtils.h:372
FEProblemBase & _fe_problem
Definition: MaterialBase.h:328
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:261
MaterialProperty< T > & declareProperty(const std::string &name)
Definition: MaterialBase.h:432
MaterialBase(const InputParameters &parameters)
Definition: MaterialBase.C:77
virtual FEProblemBase & miProblem()
Definition: MaterialBase.h:314
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:251
bool _has_stateful_property
Definition: MaterialBase.h:383
bool hasStatefulProperties() const
Definition: MaterialBase.h:236
MaterialProperty< T > & declarePropertyByName(const std::string &prop_name)
Declare the property named "name".
Definition: MaterialBase.h:132
virtual void resetProperties()
Resets the properties at each quadrature point (see resetQpProperties), only called if &#39;compute = fal...
Definition: MaterialBase.C:266
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:326
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseBase.h:131
const Moose::CoordinateSystemType & _coord_sys
Coordinate system.
Definition: MaterialBase.h:341
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:147
/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:242
const MaterialProperty< T > & getZeroMaterialPropertyByName(Ts... args)
for backwards compatibility
Definition: MaterialBase.h:184
virtual const std::set< std::string > & getRequestedItems() override
Return a set of properties accessed with getMaterialProperty.
Definition: MaterialBase.h:193
virtual void initQpStatefulProperties()
Initialize stateful properties at quadrature points.
Definition: MaterialBase.C:177
virtual void subdomainSetup() override
Subdomain setup evaluating material properties when required.
Definition: MaterialBase.C:249
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:255
void checkStatefulSanity() const
Definition: MaterialBase.C:183
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:373
virtual void resetQpProperties()
Resets the properties prior to calculation of traditional materials (only if &#39;compute = false&#39;)...
Definition: MaterialBase.C:273
ADMaterialProperty< T > & declareADPropertyByName(const std::string &prop_name)
Definition: MaterialBase.h:139
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:311
unsigned int _qp
Definition: MaterialBase.h:332
Interface for objects that needs transient capabilities.
const FaceInfo * _face_info
Definition: MaterialBase.h:387
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:199
virtual const FEProblemBase & miProblem() const
Definition: MaterialBase.h:313
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:344
MooseMesh & _mesh
Definition: MaterialBase.h:338
unsigned int getMaxQps() const
Definition: MaterialBase.C:317
Interface for notifications that the mesh has changed.
const bool _compute
If False MOOSE does not compute this property.
Definition: MaterialBase.h:362
THREAD_ID _tid
Definition: MaterialBase.h:329
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:28
void checkExecutionStage()
Check and throw an error if the execution has progressed past the construction stage.
Definition: MaterialBase.C:291
Assembly & _assembly
Definition: MaterialBase.h:330
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:359
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:356
bool forceStatefulInit() const
Definition: MaterialBase.h:276
Interface for objects that need to use UserObjects.
bool _overrides_init_stateful_props
Definition: MaterialBase.h:385
const bool _force_stateful_init
Whether or not to force stateful init; see forceStatefulInit()
Definition: MaterialBase.h:424
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:233
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:381
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:299
CoordinateSystemType
Definition: MooseTypes.h:810
const MooseArray< Point > & _normals
normals at quadrature points (valid only in boundary materials)
Definition: MaterialBase.h:336
ADMaterialProperty< T > & declareADProperty(const std::string &name)
Definition: MaterialBase.h:522
const GenericMaterialProperty< T, is_ad > & getGenericZeroMaterialPropertyByName(const std::string &prop_name)
Definition: MaterialBase.h:472
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:534
const GenericMaterialProperty< T, is_ad > & getGenericZeroMaterialProperty()
Return a constant zero anonymous material property.
Definition: MaterialBase.h:502
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:321
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:203
virtual void computePropertiesAtQp(unsigned int qp)
A method for (re)computing the properties of a MaterialBase.
Definition: MaterialBase.C:284
std::set< std::string > _supplied_props
Set of properties declared.
Definition: MaterialBase.h:347
GenericMaterialProperty< T, is_ad > & declareGenericPropertyByName(const std::string &prop_name)
Definition: MaterialBase.h:444
const MaterialProperty< T > & getZeroMaterialProperty(Ts... args)
for backwards compatibility
Definition: MaterialBase.h:177
const MooseArray< Real > & _coord
Definition: MaterialBase.h:334
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:305
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:205
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:103
unsigned int THREAD_ID
Definition: MooseTypes.h:209
bool hasRestoredProperties() const
Definition: MaterialBase.C:193