Skip to content

Commit

Permalink
Add more generic access functions (#15207)
Browse files Browse the repository at this point in the history
  • Loading branch information
dschwen committed May 21, 2020
1 parent e03cb93 commit 2e8cb8c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 14 deletions.
15 changes: 8 additions & 7 deletions framework/include/materials/DerivativeMaterialInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ class DerivativeMaterialInterface : public T, public DerivativeMaterialPropertyN
* @param name The input parameter key of type MaterialPropertyName
*/
template <typename U, bool is_ad = false>
const MaterialProperty<U> & getDefaultMaterialProperty(const std::string & name);
const GenericMaterialProperty<U, is_ad> & getDefaultMaterialProperty(const std::string & name);

/// Fetch a material property by name if it exists, otherwise return getZeroMaterialProperty
template <typename U, bool is_ad = false>
const MaterialProperty<U> & getDefaultMaterialPropertyByName(const std::string & name);
const GenericMaterialProperty<U, is_ad> &
getDefaultMaterialPropertyByName(const std::string & name);

///@{
/**
Expand Down Expand Up @@ -224,23 +225,23 @@ DerivativeMaterialInterface<T>::getDefaultMaterialProperty(const std::string & n
std::string prop_name = this->deducePropertyName(name);

// Check if it's just a constant
const MaterialProperty<U> * default_property =
this->template defaultMaterialProperty<U>(prop_name);
const auto * default_property =
this->template defaultGenericMaterialProperty<U, is_ad>(prop_name);
if (default_property)
return *default_property;

// if found return the requested property
return getDefaultMaterialPropertyByName<U>(prop_name);
return getDefaultMaterialPropertyByName<U, is_ad>(prop_name);
}

template <class T>
template <typename U, bool is_ad>
const MaterialProperty<U> &
const GenericMaterialProperty<U, is_ad> &
DerivativeMaterialInterface<T>::getDefaultMaterialPropertyByName(const std::string & prop_name)
{
// if found return the requested property
if (haveMaterialProperty<U>(prop_name))
return this->template getMaterialPropertyByName<U>(prop_name);
return this->template getGenericMaterialPropertyByName<U, is_ad>(prop_name);

return this->template getZeroMaterialProperty<U>(prop_name);
}
Expand Down
10 changes: 10 additions & 0 deletions framework/include/materials/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ class Material : public MaterialBase, public Coupleable, public MaterialProperty
/**
* Retrieve the property named "name"
*/
template <typename T, bool is_ad, typename std::enable_if<is_ad, int>::type = 0>
const ADMaterialProperty<T> & getGenericMaterialPropertyByName(const std::string & name)
{
return getADMaterialPropertyByName<T>(name);
}
template <typename T, bool is_ad, typename std::enable_if<!is_ad, int>::type = 0>
const MaterialProperty<T> & getGenericMaterialPropertyByName(const std::string & name)
{
return getMaterialPropertyByName<T>(name);
}
template <typename T>
const MaterialProperty<T> & getMaterialPropertyByName(const std::string & prop_name);
template <typename T>
Expand Down
20 changes: 14 additions & 6 deletions framework/include/materials/MaterialBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,16 @@ class MaterialBase : public MooseObject,
* Return a material property that is initialized to zero by default and does
* not need to (but can) be declared by another material.
*/
template <typename T, bool is_ad>
const GenericMaterialProperty<T, is_ad> &
getGenericZeroMaterialProperty(const std::string & prop_name);

/// for backwrads compatibility
template <typename T>
const MaterialProperty<T> & getZeroMaterialProperty(const std::string & prop_name);
const MaterialProperty<T> & getZeroMaterialProperty(const std::string & prop_name)
{
return getGenericZeroMaterialProperty<T, false>(prop_name);
}

/**
* Return a set of properties accessed with getMaterialProperty
Expand Down Expand Up @@ -299,12 +307,12 @@ MaterialBase::declarePropertyOlder(const std::string & prop_name)
return materialData().declarePropertyOlder<T>(prop_name);
}

template <typename T>
const MaterialProperty<T> &
MaterialBase::getZeroMaterialProperty(const std::string & prop_name)
template <typename T, bool is_ad>
const GenericMaterialProperty<T, is_ad> &
MaterialBase::getGenericZeroMaterialProperty(const std::string & prop_name)
{
checkExecutionStage();
MaterialProperty<T> & preload_with_zero = materialData().getProperty<T>(prop_name);
auto & preload_with_zero = materialData().getGenericProperty<T, is_ad>(prop_name);

_requested_props.insert(prop_name);
registerPropName(prop_name, true, MaterialBase::CURRENT);
Expand All @@ -324,7 +332,7 @@ MaterialBase::getZeroMaterialProperty(const std::string & prop_name)
if (nqp > preload_with_zero.size())
preload_with_zero.resize(nqp);
for (unsigned int qp = 0; qp < nqp; ++qp)
MathUtils::mooseSetToZero<T>(preload_with_zero[qp]);
MathUtils::mooseSetToZero(preload_with_zero[qp]);

return preload_with_zero;
}
Expand Down
21 changes: 21 additions & 0 deletions framework/include/materials/MaterialData.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,34 @@ class MaterialData
template <typename T>
bool haveADProperty(const std::string & prop_name) const;

template <typename T, bool is_ad, typename std::enable_if<is_ad, int>::type = 0>
bool haveGenericProperty(const std::string & prop_name) const
{
return haveADProperty<T>(prop_name);
}
template <typename T, bool is_ad, typename std::enable_if<!is_ad, int>::type = 0>
bool haveGenericProperty(const std::string & prop_name) const
{
return haveProperty<T>(prop_name);
}

///@{
/**
* Methods for retieving a MaterialProperty object
* @tparam T The type of the property
* @param prop_name The name of the property
* @return The property for the supplied type and name
*/
template <typename T, bool is_ad, typename std::enable_if<is_ad, int>::type = 0>
ADMaterialProperty<T> & getGenericProperty(const std::string & prop_name)
{
return getADProperty<T>(prop_name);
}
template <typename T, bool is_ad, typename std::enable_if<!is_ad, int>::type = 0>
MaterialProperty<T> & getGenericProperty(const std::string & prop_name)
{
return getProperty<T>(prop_name);
}
template <typename T>
MaterialProperty<T> & getProperty(const std::string & prop_name);
template <typename T>
Expand Down
25 changes: 24 additions & 1 deletion framework/include/materials/MaterialPropertyInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ class MaterialPropertyInterface
const MaterialProperty<T> & getMaterialPropertyByName(const MaterialPropertyName & name);
template <typename T>
const ADMaterialProperty<T> & getADMaterialPropertyByName(const MaterialPropertyName & name);
template <typename T, bool is_ad, typename std::enable_if<is_ad, int>::type = 0>
const ADMaterialProperty<T> & getGenericMaterialPropertyByName(const std::string & name)
{
return getADMaterialPropertyByName<T>(name);
}
template <typename T, bool is_ad, typename std::enable_if<!is_ad, int>::type = 0>
const MaterialProperty<T> & getGenericMaterialPropertyByName(const std::string & name)
{
return getMaterialPropertyByName<T>(name);
}
template <typename T>
const MaterialProperty<T> & getMaterialPropertyOldByName(const MaterialPropertyName & name);
template <typename T>
Expand Down Expand Up @@ -242,6 +252,19 @@ class MaterialPropertyInterface
template <typename T>
const ADMaterialProperty<T> * defaultADMaterialProperty(const std::string & name);

///@{ templated default material property helper
template <typename T, bool is_ad, typename std::enable_if<is_ad, int>::type = 0>
const ADMaterialProperty<T> * defaultGenericMaterialProperty(const std::string & name)
{
return defaultADMaterialProperty<T>(name);
}
template <typename T, bool is_ad, typename std::enable_if<!is_ad, int>::type = 0>
const MaterialProperty<T> * defaultGenericMaterialProperty(const std::string & name)
{
return defaultMaterialProperty<T>(name);
}
///@}

/**
* Check and throw an error if the execution has progressed past the construction stage
*/
Expand Down Expand Up @@ -373,7 +396,7 @@ MaterialPropertyInterface::defaultADMaterialProperty(const std::string & /*name*
// Forward declare explicit specializations
template <>
const MaterialProperty<Real> *
MaterialPropertyInterface::defaultMaterialProperty(const std::string & name);
MaterialPropertyInterface::defaultMaterialProperty<Real>(const std::string & name);

template <>
const ADMaterialProperty<Real> *
Expand Down

0 comments on commit 2e8cb8c

Please sign in to comment.