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 
95  virtual void initStatefulProperties(unsigned int n_points);
96 
97  virtual bool isInterfaceMaterial() { return false; };
98 
102  virtual void computeProperties() = 0;
103 
110  virtual void resetProperties();
111 
119  virtual void computePropertiesAtQp(unsigned int qp);
120 
122 
125  template <typename T>
126  MaterialProperty<T> & declarePropertyByName(const std::string & prop_name)
127  {
128  return declareGenericPropertyByName<T, false>(prop_name);
129  }
130  template <typename T>
131  MaterialProperty<T> & declareProperty(const std::string & name);
132  template <typename T>
133  ADMaterialProperty<T> & declareADPropertyByName(const std::string & prop_name)
134  {
135  return declareGenericPropertyByName<T, true>(prop_name);
136  }
137  template <typename T>
138  ADMaterialProperty<T> & declareADProperty(const std::string & name);
139 
140  template <typename T, bool is_ad>
141  auto & declareGenericProperty(const std::string & prop_name)
142  {
143  if constexpr (is_ad)
144  return declareADProperty<T>(prop_name);
145  else
146  return declareProperty<T>(prop_name);
147  }
148  template <typename T, bool is_ad>
149  GenericMaterialProperty<T, is_ad> & declareGenericPropertyByName(const std::string & prop_name);
151 
156  template <typename T, bool is_ad>
158  getGenericZeroMaterialProperty(const std::string & name);
159  template <typename T, bool is_ad>
161  getGenericZeroMaterialPropertyByName(const std::string & prop_name);
162 
166  template <typename T, bool is_ad>
168 
170  template <typename T, typename... Ts>
172  {
173  return getGenericZeroMaterialProperty<T, false>(args...);
174  }
175 
177  template <typename T, typename... Ts>
179  {
180  return getGenericZeroMaterialPropertyByName<T, false>(args...);
181  }
182 
187  virtual const std::set<std::string> & getRequestedItems() override { return _requested_props; }
188 
193  virtual const std::set<std::string> & getSuppliedItems() override { return _supplied_props; }
194 
199  const std::set<unsigned int> & getSuppliedPropIDs() { return _supplied_prop_ids; }
200 
201  void checkStatefulSanity() const;
202 
207  std::set<OutputName> getOutputs();
208 
212  virtual bool isBoundaryMaterial() const = 0;
213 
217  virtual void subdomainSetup() override;
218 
225  virtual const std::unordered_set<unsigned int> & getMatPropDependencies() const = 0;
226 
231 
238  virtual bool ghostable() const { return false; }
239 
240  void setFaceInfo(const FaceInfo & fi) { _face_info = &fi; }
241 
245  template <typename Consumers>
246  static std::deque<MaterialBase *>
247  buildRequiredMaterials(const Consumers & mat_consumers,
248  const std::vector<std::shared_ptr<MaterialBase>> & mats,
249  const bool allow_stateful);
250 
256  void setActiveProperties(const std::unordered_set<unsigned int> & needed_props);
257 
265  bool forceStatefulInit() const { return _force_stateful_init; }
266 
267 protected:
271  virtual void computeQpProperties();
272 
282  virtual void resetQpProperties();
283 
296  virtual void initQpStatefulProperties();
297 
298  virtual const MaterialData & materialData() const = 0;
299  virtual MaterialData & materialData() = 0;
300 
301  virtual const FEProblemBase & miProblem() const { return _fe_problem; }
302  virtual FEProblemBase & miProblem() { return _fe_problem; }
303 
304  virtual const QBase & qRule() const = 0;
305 
309  bool isPropertyActive(const unsigned int prop_id) const
310  {
311  return _active_prop_ids.count(prop_id) > 0;
312  }
313 
315 
319 
320  unsigned int _qp;
321 
325 
327 
330 
332  std::set<std::string> _requested_props;
333 
335  std::set<std::string> _supplied_props;
336 
344  std::set<unsigned int> _supplied_prop_ids;
345 
347  std::unordered_set<unsigned int> _active_prop_ids;
348 
350  const bool _compute;
351 
353  {
356  };
357 
361  std::unordered_map<unsigned int, unsigned int> _props_to_min_states;
362 
364  void registerPropName(const std::string & prop_name, bool is_get, const unsigned int state);
365 
367  void checkExecutionStage();
368 
369  std::vector<unsigned int> _displacements;
370 
372 
374 
375  const FaceInfo * _face_info = nullptr;
376 
377 private:
381  void markMatPropRequested(const std::string & name);
382 
391  void storeSubdomainZeroMatProp(SubdomainID block_id, const MaterialPropertyName & name);
392 
401  void storeBoundaryZeroMatProp(BoundaryID boundary_id, const MaterialPropertyName & name);
402 
406  unsigned int getMaxQps() const;
407 
409  const MaterialPropertyName _declare_suffix;
410 
413 
415  friend class FunctorMaterial;
416 };
417 
418 template <typename T>
421 {
422  // Check if the supplied parameter is a valid input parameter key
423  std::string prop_name = name;
424  if (_pars.have_parameter<MaterialPropertyName>(name))
425  prop_name = _pars.get<MaterialPropertyName>(name);
426 
427  return declarePropertyByName<T>(prop_name);
428 }
429 
430 template <typename T, bool is_ad>
432 MaterialBase::declareGenericPropertyByName(const std::string & prop_name)
433 {
434  const auto prop_name_modified =
435  _declare_suffix.empty()
436  ? prop_name
437  : MooseUtils::join(std::vector<std::string>({prop_name, _declare_suffix}), "_");
438 
439  // Call this before so that the ID is valid
440  auto & prop = materialData().declareProperty<T, is_ad>(prop_name_modified, *this);
441 
442  registerPropName(prop_name_modified, false, 0);
443  return prop;
444 }
445 
446 template <typename T, bool is_ad>
449 {
450  // Check if the supplied parameter is a valid input parameter key
451  std::string prop_name = name;
452  if (_pars.have_parameter<MaterialPropertyName>(name))
453  prop_name = _pars.get<MaterialPropertyName>(name);
454 
455  return getGenericZeroMaterialPropertyByName<T, is_ad>(prop_name);
456 }
457 
458 template <typename T, bool is_ad>
461 {
463  auto & preload_with_zero = materialData().getProperty<T, is_ad>(prop_name, 0, *this);
464 
465  _requested_props.insert(prop_name);
466  registerPropName(prop_name, true, 0);
467  markMatPropRequested(prop_name);
468 
469  // Register this material on these blocks and boundaries as a zero property with relaxed
470  // consistency checking
471  for (std::set<SubdomainID>::const_iterator it = blockIDs().begin(); it != blockIDs().end(); ++it)
472  storeSubdomainZeroMatProp(*it, prop_name);
473  for (std::set<BoundaryID>::const_iterator it = boundaryIDs().begin(); it != boundaryIDs().end();
474  ++it)
475  storeBoundaryZeroMatProp(*it, prop_name);
476 
477  // set values for all qpoints to zero
478  // (in multiapp scenarios getMaxQps can return different values in each app; we need the max)
479  unsigned int nqp = getMaxQps();
480  if (nqp > preload_with_zero.size())
481  preload_with_zero.resize(nqp);
482  for (unsigned int qp = 0; qp < nqp; ++qp)
483  MathUtils::mooseSetToZero(preload_with_zero[qp]);
484 
485  return preload_with_zero;
486 }
487 
488 template <typename T, bool is_ad>
491 {
492  // static zero property storage
494 
495  // resize to accomodate maximum number of qpoints
496  // (in multiapp scenarios getMaxQps can return different values in each app; we need the max)
497  unsigned int nqp = getMaxQps();
498  if (nqp > zero.size())
499  zero.resize(nqp);
500 
501  // set values for all qpoints to zero
502  for (unsigned int qp = 0; qp < nqp; ++qp)
504 
505  return zero;
506 }
507 
508 template <typename T>
511 {
512  // Check if the supplied parameter is a valid input parameter key
513  std::string prop_name = name;
514  if (_pars.have_parameter<MaterialPropertyName>(name))
515  prop_name = _pars.get<MaterialPropertyName>(name);
516 
517  return declareADPropertyByName<T>(prop_name);
518 }
519 
520 template <typename Consumers>
521 std::deque<MaterialBase *>
522 MaterialBase::buildRequiredMaterials(const Consumers & mat_consumers,
523  const std::vector<std::shared_ptr<MaterialBase>> & mats,
524  const bool allow_stateful)
525 {
526  std::deque<MaterialBase *> required_mats;
527 
528  std::unordered_set<unsigned int> needed_mat_props;
529  for (const auto & consumer : mat_consumers)
530  {
531  const auto & mp_deps = consumer->getMatPropDependencies();
532  needed_mat_props.insert(mp_deps.begin(), mp_deps.end());
533  }
534 
535  // A predicate of calling this function is that these materials come in already sorted by
536  // dependency with the front of the container having no other material dependencies and following
537  // materials potentially depending on the ones in front of them. So we can start at the back and
538  // iterate forward checking whether the current material supplies anything that is needed, and if
539  // not we discard it
540  for (auto it = mats.rbegin(); it != mats.rend(); ++it)
541  {
542  auto * const mat = it->get();
543  bool supplies_needed = false;
544 
545  const auto & supplied_props = mat->getSuppliedPropIDs();
546 
547  // Do O(N) with the small container
548  for (const auto supplied_prop : supplied_props)
549  {
550  if (needed_mat_props.count(supplied_prop))
551  {
552  supplies_needed = true;
553  break;
554  }
555  }
556 
557  if (!supplies_needed)
558  continue;
559 
560  if (!allow_stateful && mat->hasStatefulProperties())
561  ::mooseError(
562  "Someone called buildRequiredMaterials with allow_stateful = false but a material "
563  "dependency ",
564  mat->name(),
565  " computes stateful properties.");
566 
567  const auto & mp_deps = mat->getMatPropDependencies();
568  needed_mat_props.insert(mp_deps.begin(), mp_deps.end());
569  required_mats.push_front(mat);
570  }
571 
572  return required_mats;
573 }
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:409
virtual bool ghostable() const
Whether this material supports ghosted computations.
Definition: MaterialBase.h:238
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:316
A class for creating restricted objects.
Definition: Restartable.h:28
virtual void computeQpProperties()
Users must override this method.
Definition: MaterialBase.C:213
MaterialProperty< T > & declareProperty(const std::string &name)
Definition: MaterialBase.h:420
MaterialBase(const InputParameters &parameters)
Definition: MaterialBase.C:76
virtual FEProblemBase & miProblem()
Definition: MaterialBase.h:302
Keeps track of stuff related to assembling.
Definition: Assembly.h:101
static InputParameters validParams()
Definition: MaterialBase.C:21
void setFaceInfo(const FaceInfo &fi)
Definition: MaterialBase.h:240
bool _has_stateful_property
Definition: MaterialBase.h:371
bool hasStatefulProperties() const
Definition: MaterialBase.h:230
MaterialProperty< T > & declarePropertyByName(const std::string &prop_name)
Declare the property named "name".
Definition: MaterialBase.h:126
virtual void resetProperties()
Resets the properties at each quadrature point (see resetQpProperties), only called if &#39;compute = fal...
Definition: MaterialBase.C:218
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:314
const Moose::CoordinateSystemType & _coord_sys
Coordinate system.
Definition: MaterialBase.h:329
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:141
/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:194
const MaterialProperty< T > & getZeroMaterialPropertyByName(Ts... args)
for backwards compatibility
Definition: MaterialBase.h:178
virtual const std::set< std::string > & getRequestedItems() override
Return a set of properties accessed with getMaterialProperty.
Definition: MaterialBase.h:187
virtual void initQpStatefulProperties()
Initialize stateful properties at quadrature points.
Definition: MaterialBase.C:139
virtual void subdomainSetup() override
Subdomain setup evaluating material properties when required.
Definition: MaterialBase.C:201
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 const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:57
virtual void computeProperties()=0
Performs the quadrature point loop, calling computeQpProperties.
Definition: MaterialBase.C:207
void checkStatefulSanity() const
Definition: MaterialBase.C:145
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:361
virtual void resetQpProperties()
Resets the properties prior to calculation of traditional materials (only if &#39;compute = false&#39;)...
Definition: MaterialBase.C:225
ADMaterialProperty< T > & declareADPropertyByName(const std::string &prop_name)
Definition: MaterialBase.h:133
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:263
unsigned int _qp
Definition: MaterialBase.h:320
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:375
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:193
virtual const FEProblemBase & miProblem() const
Definition: MaterialBase.h:301
FunctorMaterials compute functor material properties.
std::set< std::string > _requested_props
Set of properties accessed via get method.
Definition: MaterialBase.h:332
MooseMesh & _mesh
Definition: MaterialBase.h:326
unsigned int getMaxQps() const
Definition: MaterialBase.C:269
Interface for notifications that the mesh has changed.
const bool _compute
If False MOOSE does not compute this property.
Definition: MaterialBase.h:350
THREAD_ID _tid
Definition: MaterialBase.h:317
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:243
Assembly & _assembly
Definition: MaterialBase.h:318
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:347
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:88
std::set< unsigned int > _supplied_prop_ids
The ids of the supplied properties, i.e.
Definition: MaterialBase.h:344
bool forceStatefulInit() const
Definition: MaterialBase.h:265
Interface for objects that need to use UserObjects.
virtual void initStatefulProperties(unsigned int n_points)
Initialize stateful properties (if material has some)
Definition: MaterialBase.C:120
bool _overrides_init_stateful_props
Definition: MaterialBase.h:373
const bool _force_stateful_init
Whether or not to force stateful init; see forceStatefulInit()
Definition: MaterialBase.h:412
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:185
static constexpr PropertyValue::id_type zero_property_id
The material property ID for a zero property.
std::vector< unsigned int > _displacements
Definition: MaterialBase.h:369
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:251
CoordinateSystemType
Definition: MooseTypes.h:809
const MooseArray< Point > & _normals
normals at quadrature points (valid only in boundary materials)
Definition: MaterialBase.h:324
ADMaterialProperty< T > & declareADProperty(const std::string &name)
Definition: MaterialBase.h:510
const GenericMaterialProperty< T, is_ad > & getGenericZeroMaterialPropertyByName(const std::string &prop_name)
Definition: MaterialBase.h:460
GenericMaterialProperty< T, is_ad > & getProperty(const std::string &prop_name, const unsigned int state, const MooseObject &requestor)
Retrieves a material property.
Definition: MaterialData.h:111
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:522
const GenericMaterialProperty< T, is_ad > & getGenericZeroMaterialProperty()
Return a constant zero anonymous material property.
Definition: MaterialBase.h:490
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:309
Interface for sorting dependent vectors of objects.
Interface for objects that needs scalar coupling capabilities.
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
const InputParameters & _pars
Parameters of this object, references the InputParameters stored in the InputParametersWarehouse.
const InputParameters & parameters() const
Get the parameters of the object.
Proxy for accessing MaterialPropertyStorage.
Definition: MaterialData.h:33
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:155
virtual void computePropertiesAtQp(unsigned int qp)
A method for (re)computing the properties of a MaterialBase.
Definition: MaterialBase.C:236
std::set< std::string > _supplied_props
Set of properties declared.
Definition: MaterialBase.h:335
GenericMaterialProperty< T, is_ad > & declareGenericPropertyByName(const std::string &prop_name)
Definition: MaterialBase.h:432
const MaterialProperty< T > & getZeroMaterialProperty(Ts... args)
for backwards compatibility
Definition: MaterialBase.h:171
const MooseArray< Real > & _coord
Definition: MaterialBase.h:322
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:257
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:199
GenericMaterialProperty< T, is_ad > & declareProperty(const std::string &prop_name, const MooseObject &requestor)
Declares a material property.
Definition: MaterialData.h:126
Interface class for classes which interact with Postprocessors.
virtual bool isInterfaceMaterial()
Definition: MaterialBase.h:97
unsigned int THREAD_ID
Definition: MooseTypes.h:209