https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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"
16#include "SetupInterface.h"
18#include "ScalarCoupleable.h"
19#include "FunctionInterface.h"
21#include "UserObjectInterface.h"
22#include "TransientInterface.h"
26#include "Restartable.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
53class MaterialBase;
54class MooseMesh;
55class SubProblem;
56class FaceInfo;
57class FEProblemBase;
58
63 public BlockRestrictable,
65 public SetupInterface,
67 public ScalarCoupleable,
68 public FunctionInterface,
71 public TransientInterface,
75 public Restartable,
77 public OutputInterface,
78 public RandomInterface,
79 public ElementIDInterface,
81 protected ADFunctorInterface
82{
83public:
85
87
88#ifdef MOOSE_KOKKOS_ENABLED
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>
157
162 template <typename T, bool is_ad>
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
281
282protected:
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 {
371 PREV
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
396private:
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
434template <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
446template <typename T, bool is_ad>
448MaterialBase::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
462template <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
474template <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
504template <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
524template <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
536template <typename Consumers>
537std::deque<MaterialBase *>
538MaterialBase::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())
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}
boundary_id_type BoundaryID
typename GenericMaterialPropertyStruct< T, is_ad >::type GenericMaterialProperty
unsigned int THREAD_ID
Definition MooseTypes.h:237
An interface for accessing Moose::Functors for systems that care about automatic differentiation,...
Keeps track of stuff related to assembling.
Definition Assembly.h:110
An interface that restricts an object to subdomains via the 'blocks' input parameter.
virtual const std::set< SubdomainID > & blockIDs() const
Return the block subdomain ids for this object Note, if this is not block restricted,...
/class BoundaryRestrictable /brief Provides functionality for limiting the object to certain boundary...
virtual const std::set< BoundaryID > & boundaryIDs() const
Return the boundary IDs for this object.
Interface for sorting dependent vectors of objects.
Interface for objects that need to use distributions.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
This data structure is used to store geometric and variable related metadata about each cell face in ...
Definition FaceInfo.h:38
Interface for objects that need to use functions.
FunctorMaterials compute functor material properties.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
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.
bool have_parameter(std::string_view name) const
A wrapper around the Parameters base class method.
MaterialBases compute MaterialProperties.
const Moose::CoordinateSystemType & _coord_sys
Coordinate system.
const MooseArray< Point > & _normals
normals at quadrature points (valid only in boundary materials)
const MooseArray< Real > & _coord
bool forceStatefulInit() const
Assembly & _assembly
virtual MaterialData & materialData()=0
virtual void computeProperties()=0
Performs the quadrature point loop, calling computeQpProperties.
unsigned int getMaxQps() const
THREAD_ID _tid
virtual void computeQpProperties()
Users must override this method.
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.
const MaterialProperty< T > & getZeroMaterialProperty(Ts... args)
for backwards compatibility
virtual void initStatefulProperties(const unsigned int n_points)
Initialize stateful properties (if material has some)
bool isPropertyActive(const unsigned int prop_id) const
Check whether a material property is active.
GenericMaterialProperty< T, is_ad > & declareGenericPropertyByName(const std::string &prop_name)
virtual bool isInterfaceMaterial()
bool hasActiveProperties()
Whether this material has active properties.
ADMaterialProperty< T > & declareADPropertyByName(const std::string &prop_name)
unsigned int _qp
const MaterialProperty< T > & getZeroMaterialPropertyByName(Ts... args)
for backwards compatibility
SubProblem & _subproblem
virtual const std::set< std::string > & getSuppliedItems() override
Return a set of properties accessed with declareProperty.
void markMatPropRequested(const std::string &name)
Helper method for adding a material property name to the material property requested set.
void checkStatefulSanity() const
std::set< unsigned int > _supplied_prop_ids
The ids of the supplied properties, i.e.
virtual const FEProblemBase & miProblem() const
virtual Moose::MaterialDataType materialDataType()=0
void checkExecutionStage()
Check and throw an error if the execution has progressed past the construction stage.
virtual FEProblemBase & miProblem()
virtual void initQpStatefulProperties()
Initialize stateful properties at quadrature points.
auto & declareGenericProperty(const std::string &prop_name)
MooseMesh & _mesh
const MaterialPropertyName _declare_suffix
Suffix to append to the name of the material property/ies when declaring it/them.
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.
FEProblemBase & _fe_problem
virtual const MaterialData & materialData() const =0
virtual void resetProperties()
Resets the properties at each quadrature point (see resetQpProperties), only called if 'compute = fal...
void setActiveProperties(const std::unordered_set< unsigned int > &needed_props)
Set active properties of this material Note: This function is called by FEProblemBase::setActiveMater...
const std::set< unsigned int > & getSuppliedPropIDs() const
Get the prop ids corresponding to declareProperty.
bool hasRestoredProperties() const
virtual const std::set< std::string > & getRequestedItems() override
Return a set of properties accessed with getMaterialProperty.
const GenericMaterialProperty< T, is_ad > & getGenericZeroMaterialProperty()
Return a constant zero anonymous material property.
std::set< OutputName > getOutputs()
Get the list of output objects that this class is restricted.
virtual const std::unordered_set< unsigned int > & getMatPropDependencies() const =0
Retrieve the set of material properties that this object depends on.
std::set< std::string > _requested_props
Set of properties accessed via get method.
MaterialProperty< T > & declarePropertyByName(const std::string &prop_name)
Declare the property named "name".
void setFaceInfo(const FaceInfo &fi)
virtual const QBase & qRule() const =0
const FaceInfo * _face_info
virtual void computePropertiesAtQp(unsigned int qp)
A method for (re)computing the properties of a MaterialBase.
const bool _compute
If False MOOSE does not compute this property.
virtual bool ghostable() const
Whether this material supports ghosted computations.
const bool _force_stateful_init
Whether or not to force stateful init; see forceStatefulInit()
MaterialProperty< T > & declareProperty(const std::string &name)
void registerPropName(const std::string &prop_name, bool is_get, const unsigned int state)
Small helper function to call store{Subdomain,Boundary}MatPropName.
static InputParameters validParams()
virtual void resetQpProperties()
Resets the properties prior to calculation of traditional materials (only if 'compute = false').
bool _overrides_init_stateful_props
ADMaterialProperty< T > & declareADProperty(const std::string &name)
bool _has_stateful_property
std::unordered_set< unsigned int > _active_prop_ids
The ids of the current active supplied properties.
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...
virtual bool isBoundaryMaterial() const =0
Returns true of the MaterialData type is not associated with volume data.
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.
std::vector< unsigned int > _displacements
std::set< std::string > _supplied_props
Set of properties declared.
const GenericMaterialProperty< T, is_ad > & getGenericZeroMaterialPropertyByName(const std::string &prop_name)
virtual void subdomainSetup() override
Subdomain setup evaluating material properties when required.
bool hasStatefulProperties() const
Proxy for accessing MaterialPropertyStorage.
GenericMaterialProperty< T, is_ad > & declareProperty(const std::string &prop_name, const MooseObject &requestor)
Declares a material property.
GenericMaterialProperty< T, is_ad > & getProperty(const std::string &prop_name, const unsigned int state, const MooseObject &requestor)
Retrieves a material property.
static constexpr PropertyValue::id_type zero_property_id
The material property ID for a zero property.
Interface for notifications that the mesh has changed.
forward declarations
Definition MooseArray.h:18
const InputParameters & parameters() const
Get the parameters of the object.
Definition MooseBase.h:131
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103
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
const InputParameters & _pars
The object's parameters.
Definition MooseBase.h:384
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition MooseMesh.h:95
Every object that can be built by the factory should be derived from this class.
Definition MooseObject.h:31
A class to provide an common interface to objects requiring "outputs" option.
Interface class for classes which interact with Postprocessors.
Interface for objects that need parallel consistent random numbers without patterns over the course o...
A class for creating restricted objects.
Definition Restartable.h:29
Interface for objects that needs scalar coupling capabilities.
Generic class for solving transient nonlinear problems.
Definition SubProblem.h:79
Interface for objects that needs transient capabilities.
Interface for objects that need to use UserObjects.
void mooseSetToZero(T &v)
Helper function templates to set a variable to zero.
Definition MathUtils.h:373
CoordinateSystemType
Definition MooseTypes.h:864
MaterialDataType
MaterialData types.
Definition MooseTypes.h:746