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() const { 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);
271  bool hasActiveProperties() { return _active_prop_ids.size() > 0; }
272 
280  bool forceStatefulInit() const { return _force_stateful_init; }
281 
282 protected:
286  virtual void computeQpProperties();
287 
297  virtual void resetQpProperties();
298 
311  virtual void initQpStatefulProperties();
312 
313  virtual const MaterialData & materialData() const = 0;
314  virtual MaterialData & materialData() = 0;
316 
317  virtual const FEProblemBase & miProblem() const { return _fe_problem; }
318  virtual FEProblemBase & miProblem() { return _fe_problem; }
319 
320  virtual const QBase & qRule() const = 0;
321 
325  bool isPropertyActive(const unsigned int prop_id) const
326  {
327  return _active_prop_ids.count(prop_id) > 0;
328  }
329 
331 
335 
336  unsigned int _qp;
337 
341 
343 
346 
348  std::set<std::string> _requested_props;
349 
351  std::set<std::string> _supplied_props;
352 
360  std::set<unsigned int> _supplied_prop_ids;
361 
363  std::unordered_set<unsigned int> _active_prop_ids;
364 
366  const bool _compute;
367 
369  {
372  };
373 
377  std::unordered_map<unsigned int, unsigned int> _props_to_min_states;
378 
380  void registerPropName(const std::string & prop_name, bool is_get, const unsigned int state);
381 
383  void checkExecutionStage();
384 
385  std::vector<unsigned int> _displacements;
386 
388 
390 
391  const FaceInfo * _face_info = nullptr;
392 
394  const MaterialPropertyName _declare_suffix;
395 
396 private:
400  void markMatPropRequested(const std::string & name);
401 
410  void storeSubdomainZeroMatProp(SubdomainID block_id, const MaterialPropertyName & name);
411 
420  void storeBoundaryZeroMatProp(BoundaryID boundary_id, const MaterialPropertyName & name);
421 
425  unsigned int getMaxQps() const;
426 
429 
431  friend class FunctorMaterial;
432 };
433 
434 template <typename T>
437 {
438  // Check if the supplied parameter is a valid input parameter key
439  std::string prop_name = name;
440  if (_pars.have_parameter<MaterialPropertyName>(name))
441  prop_name = _pars.get<MaterialPropertyName>(name);
442 
443  return declarePropertyByName<T>(prop_name);
444 }
445 
446 template <typename T, bool is_ad>
448 MaterialBase::declareGenericPropertyByName(const std::string & prop_name)
449 {
450  const auto prop_name_modified =
451  _declare_suffix.empty()
452  ? prop_name
453  : MooseUtils::join(std::vector<std::string>({prop_name, _declare_suffix}), "_");
454 
455  // Call this before so that the ID is valid
456  auto & prop = materialData().declareProperty<T, is_ad>(prop_name_modified, *this);
457 
458  registerPropName(prop_name_modified, false, 0);
459  return prop;
460 }
461 
462 template <typename T, bool is_ad>
465 {
466  // Check if the supplied parameter is a valid input parameter key
467  std::string prop_name = name;
468  if (_pars.have_parameter<MaterialPropertyName>(name))
469  prop_name = _pars.get<MaterialPropertyName>(name);
470 
471  return getGenericZeroMaterialPropertyByName<T, is_ad>(prop_name);
472 }
473 
474 template <typename T, bool is_ad>
477 {
479  auto & preload_with_zero = materialData().getProperty<T, is_ad>(prop_name, 0, *this);
480 
481  _requested_props.insert(prop_name);
482  registerPropName(prop_name, true, 0);
483  markMatPropRequested(prop_name);
484 
485  // Register this material on these blocks and boundaries as a zero property with relaxed
486  // consistency checking
487  for (std::set<SubdomainID>::const_iterator it = blockIDs().begin(); it != blockIDs().end(); ++it)
488  storeSubdomainZeroMatProp(*it, prop_name);
489  for (std::set<BoundaryID>::const_iterator it = boundaryIDs().begin(); it != boundaryIDs().end();
490  ++it)
491  storeBoundaryZeroMatProp(*it, prop_name);
492 
493  // set values for all qpoints to zero
494  // (in multiapp scenarios getMaxQps can return different values in each app; we need the max)
495  unsigned int nqp = getMaxQps();
496  if (nqp > preload_with_zero.size())
497  preload_with_zero.resize(nqp);
498  for (unsigned int qp = 0; qp < nqp; ++qp)
499  MathUtils::mooseSetToZero(preload_with_zero[qp]);
500 
501  return preload_with_zero;
502 }
503 
504 template <typename T, bool is_ad>
507 {
508  // static zero property storage
510 
511  // resize to accomodate maximum number of qpoints
512  // (in multiapp scenarios getMaxQps can return different values in each app; we need the max)
513  unsigned int nqp = getMaxQps();
514  if (nqp > zero.size())
515  zero.resize(nqp);
516 
517  // set values for all qpoints to zero
518  for (unsigned int qp = 0; qp < nqp; ++qp)
520 
521  return zero;
522 }
523 
524 template <typename T>
527 {
528  // Check if the supplied parameter is a valid input parameter key
529  std::string prop_name = name;
530  if (_pars.have_parameter<MaterialPropertyName>(name))
531  prop_name = _pars.get<MaterialPropertyName>(name);
532 
533  return declareADPropertyByName<T>(prop_name);
534 }
535 
536 template <typename Consumers>
537 std::deque<MaterialBase *>
538 MaterialBase::buildRequiredMaterials(const Consumers & mat_consumers,
539  const std::vector<std::shared_ptr<MaterialBase>> & mats,
540  const bool allow_stateful)
541 {
542  std::deque<MaterialBase *> required_mats;
543 
544  std::unordered_set<unsigned int> needed_mat_props;
545  for (const auto & consumer : mat_consumers)
546  {
547  const auto & mp_deps = consumer->getMatPropDependencies();
548  needed_mat_props.insert(mp_deps.begin(), mp_deps.end());
549  }
550 
551  // A predicate of calling this function is that these materials come in already sorted by
552  // dependency with the front of the container having no other material dependencies and following
553  // materials potentially depending on the ones in front of them. So we can start at the back and
554  // iterate forward checking whether the current material supplies anything that is needed, and if
555  // not we discard it
556  for (auto it = mats.rbegin(); it != mats.rend(); ++it)
557  {
558  auto * const mat = it->get();
559  bool supplies_needed = false;
560 
561  const auto & supplied_props = mat->getSuppliedPropIDs();
562 
563  // Do O(N) with the small container
564  for (const auto supplied_prop : supplied_props)
565  {
566  if (needed_mat_props.count(supplied_prop))
567  {
568  supplies_needed = true;
569  break;
570  }
571  }
572 
573  if (!supplies_needed)
574  continue;
575 
576  if (!allow_stateful && mat->hasStatefulProperties())
577  ::mooseError(
578  "Someone called buildRequiredMaterials with allow_stateful = false but a material "
579  "dependency ",
580  mat->name(),
581  " computes stateful properties.");
582 
583  const auto & mp_deps = mat->getMatPropDependencies();
584  needed_mat_props.insert(mp_deps.begin(), mp_deps.end());
585  required_mats.push_front(mat);
586  }
587 
588  return required_mats;
589 }
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:394
virtual bool ghostable() const
Whether this material supports ghosted computations.
Definition: MaterialBase.h:249
void mooseSetToZero(T &v)
Helper function templates to set a variable to zero.
Definition: MathUtils.h:377
FEProblemBase & _fe_problem
Definition: MaterialBase.h:332
A class for creating restricted objects.
Definition: Restartable.h:28
const InputParameters & _pars
The object&#39;s parameters.
Definition: MooseBase.h:394
virtual void computeQpProperties()
Users must override this method.
Definition: MaterialBase.C:261
MaterialProperty< T > & declareProperty(const std::string &name)
Definition: MaterialBase.h:436
MaterialBase(const InputParameters &parameters)
Definition: MaterialBase.C:77
virtual FEProblemBase & miProblem()
Definition: MaterialBase.h:318
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:387
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:330
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseBase.h:131
const Moose::CoordinateSystemType & _coord_sys
Coordinate system.
Definition: MaterialBase.h:345
MaterialDataType
MaterialData types.
Definition: MooseTypes.h:740
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:377
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:336
Interface for objects that needs transient capabilities.
const FaceInfo * _face_info
Definition: MaterialBase.h:391
This data structure is used to store geometric and variable related metadata about each cell face in ...
Definition: FaceInfo.h:37
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:317
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:348
MooseMesh & _mesh
Definition: MaterialBase.h:342
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:366
THREAD_ID _tid
Definition: MaterialBase.h:333
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:334
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:363
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:93
std::set< unsigned int > _supplied_prop_ids
The ids of the supplied properties, i.e.
Definition: MaterialBase.h:360
bool forceStatefulInit() const
Definition: MaterialBase.h:280
Interface for objects that need to use UserObjects.
bool hasActiveProperties()
Whether this material has active properties.
Definition: MaterialBase.h:271
bool _overrides_init_stateful_props
Definition: MaterialBase.h:389
const std::set< unsigned int > & getSuppliedPropIDs() const
Get the prop ids corresponding to declareProperty.
Definition: MaterialBase.h:205
const bool _force_stateful_init
Whether or not to force stateful init; see forceStatefulInit()
Definition: MaterialBase.h:428
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:385
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:858
const MooseArray< Point > & _normals
normals at quadrature points (valid only in boundary materials)
Definition: MaterialBase.h:340
ADMaterialProperty< T > & declareADProperty(const std::string &name)
Definition: MaterialBase.h:526
const GenericMaterialProperty< T, is_ad > & getGenericZeroMaterialPropertyByName(const std::string &prop_name)
Definition: MaterialBase.h:476
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:538
const GenericMaterialProperty< T, is_ad > & getGenericZeroMaterialProperty()
Return a constant zero anonymous material property.
Definition: MaterialBase.h:506
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:325
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:281
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:351
GenericMaterialProperty< T, is_ad > & declareGenericPropertyByName(const std::string &prop_name)
Definition: MaterialBase.h:448
const MaterialProperty< T > & getZeroMaterialProperty(Ts... args)
for backwards compatibility
Definition: MaterialBase.h:177
const MooseArray< Real > & _coord
Definition: MaterialBase.h:338
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.
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:237
bool hasRestoredProperties() const
Definition: MaterialBase.C:193