Skip to content

Commit

Permalink
Allow default Real values in inputs for functor properties
Browse files Browse the repository at this point in the history
  • Loading branch information
lindsayad committed Jul 21, 2021
1 parent 9b120c2 commit ccf08d1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
35 changes: 32 additions & 3 deletions framework/include/materials/MaterialPropertyInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class MaterialPropertyInterface
template <typename T>
const MaterialProperty<T> & getMaterialProperty(const std::string & name);
template <typename T>
const FunctorMaterialProperty<T> & getFunctorMaterialProperty(const std::string & name) const;
const FunctorMaterialProperty<T> & getFunctorMaterialProperty(const std::string & name);
template <typename T>
const ADMaterialProperty<T> & getADMaterialProperty(const std::string & name);
template <typename T, bool is_ad, typename std::enable_if<is_ad, int>::type = 0>
Expand Down Expand Up @@ -272,7 +272,7 @@ class MaterialPropertyInterface
FEProblemBase & _mi_feproblem;

/// Reference to the subproblem
const SubProblem & _mi_subproblem;
SubProblem & _mi_subproblem;

/// Current threaded it
const THREAD_ID _mi_tid;
Expand Down Expand Up @@ -301,6 +301,13 @@ class MaterialPropertyInterface
template <typename T>
const MaterialProperty<T> * defaultMaterialProperty(const std::string & name);

/**
* Helper function to parse default material property values. This is implemented
* as a specialization for supported types and returns NULL in all other cases.
*/
template <typename T>
const FunctorMaterialProperty<T> * defaultFunctorMaterialProperty(const std::string & name);

/**
* Helper function to parse default material property values. This is implemented
* as a specialization for supported types and returns NULL in all other cases.
Expand Down Expand Up @@ -392,11 +399,17 @@ MaterialPropertyInterface::getMaterialProperty(const std::string & name)

template <typename T>
const FunctorMaterialProperty<T> &
MaterialPropertyInterface::getFunctorMaterialProperty(const std::string & name) const
MaterialPropertyInterface::getFunctorMaterialProperty(const std::string & name)
{
// Check if the supplied parameter is a valid input parameter key
std::string prop_name = deducePropertyName(name);

// Check if it's just a constant
const FunctorMaterialProperty<T> * const default_property =
defaultFunctorMaterialProperty<T>(prop_name);
if (default_property)
return *default_property;

return _mi_subproblem.getFunctorProperty<T>(prop_name);
}

Expand Down Expand Up @@ -465,6 +478,14 @@ MaterialPropertyInterface::defaultMaterialProperty(const std::string & /*name*/)
return NULL;
}

// General version for types that do not accept default values
template <typename T>
const FunctorMaterialProperty<T> *
MaterialPropertyInterface::defaultFunctorMaterialProperty(const std::string & /*name*/)
{
return nullptr;
}

// General version for types that do not accept default values
template <typename T>
const ADMaterialProperty<T> *
Expand All @@ -478,6 +499,14 @@ template <>
const MaterialProperty<Real> *
MaterialPropertyInterface::defaultMaterialProperty<Real>(const std::string & name);

template <>
const FunctorMaterialProperty<Real> *
MaterialPropertyInterface::defaultFunctorMaterialProperty<Real>(const std::string & name);

template <>
const FunctorMaterialProperty<ADReal> *
MaterialPropertyInterface::defaultFunctorMaterialProperty<ADReal>(const std::string & name);

template <>
const ADMaterialProperty<Real> *
MaterialPropertyInterface::defaultADMaterialProperty<Real>(const std::string & name);
Expand Down
40 changes: 40 additions & 0 deletions framework/src/materials/MaterialPropertyInterface.C
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,46 @@ MaterialPropertyInterface::defaultMaterialProperty(const std::string & name)
return nullptr;
}

template <>
const FunctorMaterialProperty<Real> *
MaterialPropertyInterface::defaultFunctorMaterialProperty(const std::string & name)
{
std::istringstream ss(name);
Real real_value;

// check if the string parsed cleanly into a Real number
if (ss >> real_value && ss.eof())
{
auto & prop = _mi_subproblem.declareFunctorProperty<Real>(name);
prop.setFunction(_mi_subproblem.mesh(),
_mi_block_ids,
[real_value](auto /*geom_quantity*/) -> Real { return real_value; });
return &prop;
}

return nullptr;
}

template <>
const FunctorMaterialProperty<ADReal> *
MaterialPropertyInterface::defaultFunctorMaterialProperty(const std::string & name)
{
std::istringstream ss(name);
Real real_value;

// check if the string parsed cleanly into a Real number
if (ss >> real_value && ss.eof())
{
auto & prop = _mi_subproblem.declareFunctorProperty<ADReal>(name);
prop.setFunction(_mi_subproblem.mesh(),
_mi_block_ids,
[real_value](auto /*geom_quantity*/) -> ADReal { return real_value; });
return &prop;
}

return nullptr;
}

template <>
const ADMaterialProperty<Real> *
MaterialPropertyInterface::defaultADMaterialProperty(const std::string & name)
Expand Down

0 comments on commit ccf08d1

Please sign in to comment.