www.mooseframework.org
MaterialPropertyInterface.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 // MOOSE includes
13 #include "MaterialProperty.h"
14 #include "FEProblemBase.h"
15 #include "MooseTypes.h"
16 #include "MaterialData.h"
17 
18 // Forward declarations
19 class InputParameters;
21 class MooseObject;
22 
23 template <typename T>
25 
26 template <>
28 
29 #define adDeclareADProperty this->template declareADProperty
30 #define adDeclareProperty this->template declareProperty
31 #define adGetADMaterialProperty this->template getADMaterialProperty
32 #define adGetMaterialProperty this->template getMaterialProperty
33 #define adGetMaterialPropertyOld this->template getMaterialPropertyOld
34 #define adGetADMaterialPropertyByName this->template getADMaterialPropertyByName
35 #define adGetMaterialPropertyByName this->template getMaterialPropertyByName
36 #define adGetMaterialPropertyOldByName this->template getMaterialPropertyOldByName
37 #define adHasMaterialProperty this->template hasMaterialProperty
38 #define adHasMaterialPropertyByName this->template hasMaterialPropertyByName
39 
51 {
52 public:
53  MaterialPropertyInterface(const MooseObject * moose_object,
54  const std::set<SubdomainID> & block_ids,
55  const std::set<BoundaryID> & boundary_ids);
56 
58 
66  template <typename T>
67  const MaterialProperty<T> & getMaterialProperty(const std::string & name);
68  template <typename T>
69  const ADMaterialPropertyObject<T> & getADMaterialProperty(const std::string & name);
70  template <typename T>
71  const MaterialProperty<T> & getMaterialPropertyOld(const std::string & name);
72  template <typename T>
73  const MaterialProperty<T> & getMaterialPropertyOlder(const std::string & name);
75 
77 
83  template <typename T>
84  const MaterialProperty<T> & getMaterialPropertyByName(const MaterialPropertyName & name);
85  template <typename T>
87  getADMaterialPropertyByName(const MaterialPropertyName & name);
88  template <typename T>
89  const MaterialProperty<T> & getMaterialPropertyOldByName(const MaterialPropertyName & name);
90  template <typename T>
91  const MaterialProperty<T> & getMaterialPropertyOlderByName(const MaterialPropertyName & name);
93 
104  template <typename T>
105  std::pair<const MaterialProperty<T> *, std::set<SubdomainID>>
106  getBlockMaterialProperty(const MaterialPropertyName & name);
107 
112  template <typename T>
113  const MaterialProperty<T> & getZeroMaterialProperty(const std::string & prop_name);
114 
120  std::set<SubdomainID> getMaterialPropertyBlocks(const std::string & name);
121 
127  std::vector<SubdomainName> getMaterialPropertyBlockNames(const std::string & name);
128 
134  std::set<BoundaryID> getMaterialPropertyBoundaryIDs(const std::string & name);
135 
141  std::vector<BoundaryName> getMaterialPropertyBoundaryNames(const std::string & name);
142 
147  void checkBlockAndBoundaryCompatibility(std::shared_ptr<Material> discrete);
148 
150 
158  Material & getMaterial(const std::string & name);
159  Material & getMaterialByName(const std::string & name, bool no_warn = false);
160  template <ComputeStage>
161  Material & getMaterial(const std::string & name);
162  template <ComputeStage>
163  Material & getMaterialByName(const std::string & name, bool no_warn = false);
165 
167 
172  template <typename T>
173  bool hasMaterialProperty(const std::string & name);
174  template <typename T>
175  bool hasMaterialPropertyByName(const std::string & name);
177 
183  void statefulPropertiesAllowed(bool);
184 
189 
196  const std::set<unsigned int> & getMatPropDependencies() const
197  {
199  }
200 
201 protected:
204 
206  const std::string _mi_name;
207 
210 
212  std::shared_ptr<MaterialData> _material_data;
213 
216 
219 
225  void checkMaterialProperty(const std::string & name);
226 
230  void markMatPropRequested(const std::string &);
231 
235  std::string deducePropertyName(const std::string & name);
236 
241  template <typename T>
242  const MaterialProperty<T> * defaultMaterialProperty(const std::string & name);
243 
248  template <typename T>
249  const ADMaterialPropertyObject<T> * defaultADMaterialProperty(const std::string & name);
250 
256 
263 
265  std::vector<std::unique_ptr<MaterialProperty<Real>>> _default_real_properties;
267  std::vector<std::unique_ptr<ADMaterialPropertyObject<Real>>> _default_ad_real_properties;
269  std::vector<std::unique_ptr<ADMaterialPropertyObject<RealVectorValue>>>
271 
273  std::set<unsigned int> _material_property_dependencies;
274 
275 private:
277  void checkExecutionStage();
278 
281 
283  const std::set<SubdomainID> & _mi_block_ids;
284 
286  const std::set<BoundaryID> & _mi_boundary_ids;
287 };
288 
294 template <typename T>
295 inline void
297 {
303  v = 0;
304 }
305 template <typename T>
306 inline void
308 {
309  mooseError("Cannot use pointer types for MaterialProperty derivatives.");
310 }
311 
312 template <typename T>
313 const MaterialProperty<T> &
315 {
316  // Check if the supplied parameter is a valid input parameter key
317  std::string prop_name = deducePropertyName(name);
318 
319  // Check if it's just a constant
320  const MaterialProperty<T> * default_property = defaultMaterialProperty<T>(prop_name);
321  if (default_property)
322  return *default_property;
323 
324  return getMaterialPropertyByName<T>(prop_name);
325 }
326 
327 template <typename T>
330 {
331  // Check if the supplied parameter is a valid input parameter key
332  std::string prop_name = deducePropertyName(name);
333 
334  // Check if it's just a constant
335  const ADMaterialPropertyObject<T> * default_property = defaultADMaterialProperty<T>(prop_name);
336  if (default_property)
337  return *default_property;
338 
339  return getADMaterialPropertyByName<T>(prop_name);
340 }
341 
342 template <typename T>
343 const MaterialProperty<T> &
345 {
346  if (!_stateful_allowed)
347  mooseError("Stateful material properties not allowed for this object."
348  " Old property for \"",
349  name,
350  "\" was requested.");
351 
352  // Check if the supplied parameter is a valid input parameter key
353  std::string prop_name = deducePropertyName(name);
354 
355  // Check if it's just a constant
356  const MaterialProperty<T> * default_property = defaultMaterialProperty<T>(prop_name);
357  if (default_property)
358  return *default_property;
359 
360  return getMaterialPropertyOldByName<T>(prop_name);
361 }
362 
363 template <typename T>
364 const MaterialProperty<T> &
366 {
367  if (!_stateful_allowed)
368  mooseError("Stateful material properties not allowed for this object."
369  " Older property for \"",
370  name,
371  "\" was requested.");
372 
373  // Check if the supplied parameter is a valid input parameter key
374  std::string prop_name = deducePropertyName(name);
375 
376  // Check if it's just a constant
377  const MaterialProperty<T> * default_property = defaultMaterialProperty<T>(prop_name);
378  if (default_property)
379  return *default_property;
380 
381  return getMaterialPropertyOlderByName<T>(prop_name);
382 }
383 
384 // General version for types that do not accept default values
385 template <typename T>
386 const MaterialProperty<T> *
388 {
389  return NULL;
390 }
391 
392 // General version for types that do not accept default values
393 template <typename T>
396 {
397  return NULL;
398 }
399 
400 // Forward declare explicit specializations
401 template <>
404 
405 template <>
407 MaterialPropertyInterface::defaultADMaterialProperty<Real>(const std::string & name);
408 
409 template <>
411 MaterialPropertyInterface::defaultADMaterialProperty<RealVectorValue>(const std::string & name);
412 
413 template <typename T>
414 const MaterialProperty<T> &
416 {
418  checkMaterialProperty(name);
419 
420  // mark property as requested
421  markMatPropRequested(name);
422 
423  // Update the boolean flag.
425 
426  _material_property_dependencies.insert(_material_data->getPropertyId(name));
427 
428  return _material_data->getProperty<T>(name);
429 }
430 
431 template <typename T>
434 {
436 
438  checkMaterialProperty(name);
439 
440  // mark property as requested
441  markMatPropRequested(name);
442 
443  // Update the boolean flag.
445 
446  _material_property_dependencies.insert(_material_data->getPropertyId(name));
447 
448  return _material_data->getADProperty<T>(name);
449 }
450 
451 template <typename T>
452 const MaterialProperty<T> &
454 {
455  if (!_stateful_allowed)
456  mooseError("Stateful material properties not allowed for this object."
457  " Old property for \"",
458  name,
459  "\" was requested.");
460 
461  // mark property as requested
462  markMatPropRequested(name);
463 
464  _material_property_dependencies.insert(_material_data->getPropertyId(name));
465 
466  return _material_data->getPropertyOld<T>(name);
467 }
468 
469 template <typename T>
470 const MaterialProperty<T> &
472 {
473  if (!_stateful_allowed)
474  mooseError("Stateful material properties not allowed for this object."
475  " Older property for \"",
476  name,
477  "\" was requested.");
478 
479  // mark property as requested
480  markMatPropRequested(name);
481 
482  _material_property_dependencies.insert(_material_data->getPropertyId(name));
483 
484  return _material_data->getPropertyOlder<T>(name);
485 }
486 
487 template <typename T>
488 std::pair<const MaterialProperty<T> *, std::set<SubdomainID>>
489 MaterialPropertyInterface::getBlockMaterialProperty(const MaterialPropertyName & name)
490 {
491  if (_mi_block_ids.empty())
492  mooseError("getBlockMaterialProperty must be called by a block restrictable object");
493 
494  if (!hasMaterialPropertyByName<T>(name))
495  return std::pair<const MaterialProperty<T> *, std::set<SubdomainID>>(NULL,
496  std::set<SubdomainID>());
497 
498  _material_property_dependencies.insert(_material_data->getPropertyId(name));
499 
500  return std::pair<const MaterialProperty<T> *, std::set<SubdomainID>>(
501  &_material_data->getProperty<T>(name), _mi_feproblem.getMaterialPropertyBlocks(name));
502 }
503 
504 template <typename T>
505 bool
507 {
508  // Check if the supplied parameter is a valid input parameter key
509  std::string prop_name = deducePropertyName(name);
510  return hasMaterialPropertyByName<T>(prop_name);
511 }
512 
513 template <typename T>
514 bool
516 {
517  return _material_data->haveProperty<T>(name);
518 }
519 
520 template <typename T>
521 const MaterialProperty<T> &
522 MaterialPropertyInterface::getZeroMaterialProperty(const std::string & /*prop_name*/)
523 {
524  // static zero property storage
525  static MaterialProperty<T> zero;
526 
527  // resize to accomodate maximum number of qpoints
528  // (in multiapp scenarios getMaxQps can return different values in each app; we need the max)
529  unsigned int nqp = _mi_feproblem.getMaxQps();
530  if (nqp > zero.size())
531  zero.resize(nqp);
532 
533  // set values for all qpoints to zero
534  for (unsigned int qp = 0; qp < nqp; ++qp)
535  mooseSetToZero<T>(zero[qp]);
536 
537  return zero;
538 }
539 
InputParameters validParams< MaterialPropertyInterface >()
FEProblemBase & _mi_feproblem
Reference to the FEProblemBase class.
const THREAD_ID _mi_tid
Current threaded it.
const MaterialProperty< T > & getMaterialProperty(const std::string &name)
Retrieve reference to material property or one of it&#39;s old or older values.
const ADMaterialPropertyObject< T > & getADMaterialProperty(const std::string &name)
void checkBlockAndBoundaryCompatibility(std::shared_ptr< Material > discrete)
Check if block and boundary restrictions of a given material are compatible with the current material...
const std::set< SubdomainID > & _mi_block_ids
Storage for the block ids created by BlockRestrictable.
std::vector< BoundaryName > getMaterialPropertyBoundaryNames(const std::string &name)
Retrieve the boundary namess that the material property is defined.
InputParameters validParams()
This is the templated validParams() function that every MooseObject-derived class is required to spec...
bool hasMaterialPropertyByName(const std::string &name)
MaterialPropertyInterface(const MooseObject *moose_object, const std::set< SubdomainID > &block_ids, const std::set< BoundaryID > &boundary_ids)
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
std::pair< const MaterialProperty< T > *, std::set< SubdomainID > > getBlockMaterialProperty(const MaterialPropertyName &name)
Retrieve pointer to a material property with the mesh blocks where it is defined The name required by...
MaterialDataType
MaterialData types.
Definition: MooseTypes.h:459
std::vector< SubdomainName > getMaterialPropertyBlockNames(const std::string &name)
Retrieve the block names that the material property is defined.
const MaterialProperty< T > & getZeroMaterialProperty(const std::string &prop_name)
Return a material property that is initialized to zero by default and does not need to (but can) be d...
Moose::MaterialDataType _material_data_type
The type of data.
const bool _mi_boundary_restricted
BoundaryRestricted flag.
Material & getMaterial(const std::string &name)
Return a Material reference - usable for computing directly.
std::set< BoundaryID > getMaterialPropertyBoundaryIDs(const std::string &name)
Retrieve the boundary ids that the material property is defined.
const MaterialProperty< T > * defaultMaterialProperty(const std::string &name)
Helper function to parse default material property values.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const std::set< BoundaryID > & _mi_boundary_ids
Storage for the boundary ids created by BoundaryRestrictable.
const std::string _mi_name
The name of the object that this interface belongs to.
const ADMaterialPropertyObject< T > & getADMaterialPropertyByName(const MaterialPropertyName &name)
std::set< unsigned int > _material_property_dependencies
The set of material properties (as given by their IDs) that this object depends on.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
const MaterialProperty< T > & getMaterialPropertyOlderByName(const MaterialPropertyName &name)
void markMatPropRequested(const std::string &)
A proxy method for _mi_feproblem.markMatPropRequested(name)
std::vector< std::unique_ptr< ADMaterialPropertyObject< Real > > > _default_ad_real_properties
Storage vector for ADMaterialPropertyObject<Real> default objects.
std::vector< std::unique_ptr< MaterialProperty< Real > > > _default_real_properties
Storage vector for MaterialProperty<Real> default objects.
std::string deducePropertyName(const std::string &name)
Small helper to look up a material property name through the input parameter keys.
std::set< SubdomainID > getMaterialPropertyBlocks(const std::string &name)
Retrieve the block ids that the material property is defined.
const std::set< unsigned int > & getMatPropDependencies() const
Retrieve the set of material properties that this object depends on.
void usingADMatProps(bool using_ad_mat_props)
Set the global automatic differentiaion (AD) flag which indicates whether any consumer has requested ...
void mooseSetToZero(T &v)
Helper function templates to set a variable to zero.
const MaterialProperty< T > & getMaterialPropertyOlder(const std::string &name)
Material & getMaterialByName(const std::string &name, bool no_warn=false)
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:42
const ADMaterialPropertyObject< T > * defaultADMaterialProperty(const std::string &name)
Helper function to parse default material property values.
const InputParameters & _mi_params
Parameters of the object with this interface.
std::shared_ptr< MaterialData > _material_data
Pointer to the material data class that stores properties.
const MaterialProperty< T > & getMaterialPropertyOld(const std::string &name)
Materials compute MaterialProperties.
Definition: Material.h:46
virtual void resize(int n) override
Resizes the property to the size n.
bool hasMaterialProperty(const std::string &name)
Check if the material property exists.
void checkMaterialProperty(const std::string &name)
A helper method for checking material properties This method was required to avoid a compiler problem...
virtual std::set< SubdomainID > getMaterialPropertyBlocks(const std::string &prop_name)
Get a vector containing the block ids the material property is defined on.
Definition: SubProblem.C:291
void statefulPropertiesAllowed(bool)
Derived classes can declare whether or not they work with stateful material properties.
An interface for accessing Materials.
bool getMaterialPropertyCalled() const
Returns true if getMaterialProperty() has been called, false otherwise.
Concrete definition of a parameter value for a specified type.
const MaterialProperty< T > & getMaterialPropertyOldByName(const MaterialPropertyName &name)
std::vector< std::unique_ptr< ADMaterialPropertyObject< RealVectorValue > > > _default_ad_real_vector_properties
Storage vector for ADMaterialPropertyObject<RealVectorValue> default objects.
const MaterialProperty< T > & getMaterialPropertyByName(const MaterialPropertyName &name)
Retrieve reference to material property or its old or older value The name required by this method is...
bool _get_material_property_called
Initialized to false.
void checkExecutionStage()
Check and throw an error if the execution has progressed past the construction stage.
bool _stateful_allowed
True by default.
virtual unsigned int size() const override
unsigned int THREAD_ID
Definition: MooseTypes.h:161
unsigned int getMaxQps() const