Skip to content

Commit

Permalink
Backwards compatible approach (#27513)
Browse files Browse the repository at this point in the history
  • Loading branch information
dschwen committed May 8, 2024
1 parent d7c677e commit c24beae
Show file tree
Hide file tree
Showing 36 changed files with 409 additions and 419 deletions.
79 changes: 22 additions & 57 deletions framework/include/actions/MaterialOutputAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,72 +9,48 @@

#pragma once

// MOOSE includes
#include "Action.h"
#include "MaterialData.h"
#include "FEProblemBase.h"
#include "SerialAccess.h"

class MooseObjectAction;
class MaterialBase;

struct MaterialOutputActionDescriptor
{
};

/**
* Creates AuxVariables and AuxKernels for automatic output of material properties
*/
class MaterialOutputAction : public Action
{
public:
/**
* Class constructor
* @param params Input parameters for this action object
*/
static InputParameters validParams();

MaterialOutputAction(const InputParameters & params);
virtual void act() override;

/// output function called from SetupOutput::apply (through Moose::typeLoop)
template <typename T, int I>
void setupOutput(const MaterialPropertyName & prop_name, const MaterialBase & material);
virtual void act() override;

protected:
template <typename T, int I, bool is_ad, bool is_functor>
void setupOutputHelper(const MaterialPropertyName & prop_name, const MaterialBase & material);

/// List of supported raw types (equivalent AD types are also supported)
typedef Moose::TypeList<Real,
RealVectorValue,
RealTensorValue,
RankTwoTensor,
RankFourTensor,
SymmetricRankTwoTensor,
SymmetricRankFourTensor>
SupportedTypes;

/// List of AuxKernels used for the respective property type output ('AD' prefix is added automatically for is_ad)
static const std::vector<std::string> _aux_kernel_names;

/// List of index symbols
static const std::vector<std::string> _index_symbols;

/// List of coefficient parameter names
static const std::vector<std::vector<std::string>> _param_names;

template <typename T, int I>
struct SetupOutput
/// Meta data describing the setup of an output object
struct OutputMetaData
{
static void apply(MaterialOutputAction * self,
const MaterialPropertyName & prop_name,
const MaterialBase & material)
{
self->setupOutput<T, I>(prop_name, material);
}
std::string _aux_kernel_name;
std::string _index_symbols;
std::vector<std::string> _param_names;
};

protected:
/**
* A function to be overriden by derived actions to handle a set of material property types
*/
virtual std::vector<std::string> materialOutput(const std::string & property_name,
const MaterialBase & material,
bool get_names_only);

/// Universal output object setup function
std::vector<std::string> outputHelper(const OutputMetaData & metadata,
const MaterialPropertyName & property_name,
const std::string & var_name_base,
const MaterialBase & material,
bool get_names_only);

/**
* Helper method for testing if the material exists as a block or boundary material
* @tparam T The property type (e.g., REAL)
Expand Down Expand Up @@ -108,7 +84,6 @@ class MaterialOutputAction : public Action
*
* @return An InputParameter object with common properties added.
*/
template <bool is_functor>
InputParameters getParams(const std::string & type,
const std::string & property_name,
const std::string & variable_name,
Expand All @@ -127,12 +102,6 @@ class MaterialOutputAction : public Action
/// variables for the current MaterialBase object
std::set<std::string> _material_variable_names;

/// all variables added by this action
std::set<std::string> _all_variable_names;

/// property names we succeeded to set output up for
std::set<std::string> _supported_properties;

/// Map of output names and list of variables associated with the output
std::map<OutputName, std::set<std::string>> _material_variable_names_map;

Expand Down Expand Up @@ -169,9 +138,5 @@ template <typename T>
bool
MaterialOutputAction::hasFunctorProperty(const std::string & property_name)
{
// functors support a limited set of types
if constexpr (std::is_same_v<T, Real> || std::is_same_v<T, RealVectorValue>)
return _problem->hasFunctorWithType<T>(property_name, 0);
else
return false;
return _problem->hasFunctorWithType<T>(property_name, 0);
}
114 changes: 46 additions & 68 deletions framework/include/auxkernels/MaterialAuxBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* A base class for the various Material related AuxKernal objects.
* \p RT is short for return type
*/
template <typename T, bool is_ad, typename RT = Real>
template <typename T, bool is_ad, bool is_functor = false, typename RT = Real>
class MaterialAuxBaseTempl : public AuxKernelTempl<RT>
{
public:
Expand All @@ -29,10 +29,9 @@ class MaterialAuxBaseTempl : public AuxKernelTempl<RT>
MaterialAuxBaseTempl(const InputParameters & parameters);

/// Functors really only work for Real and RealVectorValue for now :(
using MaterialAuxFunctorType =
typename std::conditional<std::is_same_v<T, Real> || std::is_same_v<T, RealVectorValue>,
Moose::Functor<GenericType<T, is_ad>>,
void>::type;
using PropertyType = typename std::conditional<is_functor,
Moose::Functor<Moose::GenericType<T, is_ad>>,
GenericMaterialProperty<T, is_ad>>::type;

protected:
virtual RT computeValue() override;
Expand All @@ -43,17 +42,14 @@ class MaterialAuxBaseTempl : public AuxKernelTempl<RT>
/// Returns material property values at quadrature points
virtual RT getRealValue() = 0;

/// Material property for this AuxKernel
const GenericMaterialProperty<T, is_ad> * _property;

/// Functor for this AuxKernel
const MaterialAuxFunctorType * _functor;
/// (Functor)Material property for this AuxKernel
const PropertyType & _prop;

/// Evaluate at this quadrature point only
const unsigned int _selected_qp;

/// T Value evaluated from either the property or the functor
GenericType<T, is_ad> _full_value;
Moose::GenericType<T, is_ad> _full_value;

private:
/// Multiplier for the material property
Expand All @@ -63,65 +59,61 @@ class MaterialAuxBaseTempl : public AuxKernelTempl<RT>
const RT _offset;
};

template <typename T, bool is_ad, typename RT>
template <typename T, bool is_ad, bool is_functor, typename RT>
InputParameters
MaterialAuxBaseTempl<T, is_ad, RT>::validParams()
MaterialAuxBaseTempl<T, is_ad, is_functor, RT>::validParams()
{
InputParameters params = AuxKernelTempl<RT>::validParams();
params.addParam<MaterialPropertyName>(
"property", "The scalar material property name (set either this or `functor`).");
// functors do not support all types
if constexpr (!std::is_same_v<MaterialAuxFunctorType, void>)
params.addParam<MooseFunctorName>(
"functor", "The scalar material property name (set either this or `property`).");
params.addRequiredParam<MaterialPropertyName>("property",
"The scalar (functor)material property name.");
params.addParam<Real>(
"factor", 1, "The factor by which to multiply your material property for visualization");
params.addParam<RT>("offset", 0, "The offset to add to your material property for visualization");

params.addParam<unsigned int>(
"selected_qp",
"Evaluate the material property at a specified quadrature point. This only needs "
"to be used if you are interested in a particular quadrature point in each element. "
"Otherwise do not include this parameter in your input file.");
params.addParamNamesToGroup("selected_qp", "Advanced");
if constexpr (!is_functor)
{
params.addParam<unsigned int>(
"selected_qp",
"Evaluate the material property at a specified quadrature point. This only needs "
"to be used if you are interested in a particular quadrature point in each element. "
"Otherwise do not include this parameter in your input file.");
params.addParamNamesToGroup("selected_qp", "Advanced");
}

return params;
}

template <typename T, bool is_ad, typename RT>
MaterialAuxBaseTempl<T, is_ad, RT>::MaterialAuxBaseTempl(const InputParameters & parameters)
template <typename T, bool is_ad, bool is_functor, typename RT>
MaterialAuxBaseTempl<T, is_ad, is_functor, RT>::MaterialAuxBaseTempl(
const InputParameters & parameters)
: AuxKernelTempl<RT>(parameters),
_property(this->isParamValid("property")
? &this->template getGenericMaterialProperty<T, is_ad>("property")
: nullptr),
_functor(this->isParamValid("functor")
? [this]() {
if constexpr (!std::is_same_v<MaterialAuxFunctorType, void>)
return &this->template getFunctor<GenericType<T, is_ad>>("functor");
else
{
libmesh_ignore(this);
return nullptr;
}
}()
: nullptr),
_selected_qp(this->isParamValid("selected_qp") ? this->template getParam<unsigned int>("selected_qp") : libMesh::invalid_uint),
_prop([this]() -> const auto & {
if constexpr (is_functor)
return this->template getFunctor<Moose::GenericType<T, is_ad>>("property");
else
return this->template getGenericMaterialProperty<T, is_ad>("property");
}()),
_selected_qp(this->isParamValid("selected_qp")
? this->template getParam<unsigned int>("selected_qp")
: libMesh::invalid_uint),
_factor(this->template getParam<Real>("factor")),
_offset(this->template getParam<RT>("offset"))
{
if (!_property == !_functor)
mooseError("Specify either a `property` or a `functor` parameter.");
if (_functor && _selected_qp != libMesh::invalid_uint)
this->paramError("selected_qp",
"Selective quadrature point evaluation is not implemented for functors.");
}

template <typename T, bool is_ad, typename RT>
template <typename T, bool is_ad, bool is_functor, typename RT>
RT
MaterialAuxBaseTempl<T, is_ad, RT>::computeValue()
MaterialAuxBaseTempl<T, is_ad, is_functor, RT>::computeValue()
{
// Material Property Values
if (_property)
// Functor Values
if constexpr (is_functor)
{
const auto elem_arg = this->makeElemArg(this->_current_elem);
const auto state = this->determineState();
_full_value = _prop(elem_arg, state);
}
// Material Properties
else
{
if (_selected_qp != libMesh::invalid_uint)
{
Expand All @@ -135,29 +127,15 @@ MaterialAuxBaseTempl<T, is_ad, RT>::computeValue()
this->_q_point.size(),
" quadrature points in the element");
}
_full_value = (*_property)[_selected_qp];
}
else
_full_value = (*_property)[this->_qp];
}

// Functor Values
if (_functor)
{
if constexpr (!std::is_same_v<MaterialAuxFunctorType, void>)
{
const auto elem_arg = this->makeElemArg(this->_current_elem);
const auto state = this->determineState();
_full_value = (*_functor)(elem_arg, state);
_full_value = _prop[_selected_qp];
}
else
mooseError("Unsupported functor type");
_full_value = _prop[this->_qp];
}

checkFullValue();

return _factor * getRealValue() + _offset;
}

template <typename T = Real>
using MaterialAuxBase = MaterialAuxBaseTempl<T, false, Real>;
using MaterialAuxBase = MaterialAuxBaseTempl<T, false, false, Real>;
2 changes: 0 additions & 2 deletions framework/include/auxkernels/MaterialRateAuxBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ MaterialRateAuxBaseTempl<T, is_ad, RT>::MaterialRateAuxBaseTempl(const InputPara
: MaterialAuxBaseTempl<RT, is_ad>(parameters),
_prop_old(this->template getMaterialPropertyOld<T>("property"))
{
if (!this->_property)
this->paramError("functor", "This object does not support functors yet.");
}

template <typename T = Real>
Expand Down
14 changes: 6 additions & 8 deletions framework/include/auxkernels/MaterialRealAux.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@
// MOOSE includes
#include "MaterialAuxBase.h"

// Forward Declarations
template <bool>
class MaterialRealAuxTempl;
typedef MaterialRealAuxTempl<false> MaterialRealAux;

/**
* Object for passing a scalar, REAL material property to an AuxVariable
*/
template <bool is_ad>
class MaterialRealAuxTempl : public MaterialAuxBaseTempl<Real, is_ad>
template <bool is_ad, bool is_functor>
class MaterialRealAuxTempl : public MaterialAuxBaseTempl<Real, is_ad, is_functor>
{
public:
static InputParameters validParams();
Expand All @@ -37,4 +32,7 @@ class MaterialRealAuxTempl : public MaterialAuxBaseTempl<Real, is_ad>
Real getRealValue() override;
};

typedef MaterialRealAuxTempl<true> ADMaterialRealAux;
typedef MaterialRealAuxTempl<false, false> MaterialRealAux;
typedef MaterialRealAuxTempl<true, false> ADMaterialRealAux;
typedef MaterialRealAuxTempl<false, true> FunctorMaterialRealAux;
typedef MaterialRealAuxTempl<true, true> ADFunctorMaterialRealAux;
16 changes: 10 additions & 6 deletions framework/include/auxkernels/MaterialRealVectorValueAux.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
/**
* AuxKernel for outputting a RealVectorValue material property component to an AuxVariable
*/
template <typename T, bool is_ad>
class MaterialRealVectorValueAuxTempl : public MaterialAuxBaseTempl<T, is_ad>
template <typename T, bool is_ad, bool is_functor>
class MaterialRealVectorValueAuxTempl : public MaterialAuxBaseTempl<T, is_ad, is_functor>
{
public:
static InputParameters validParams();
Expand All @@ -35,9 +35,13 @@ class MaterialRealVectorValueAuxTempl : public MaterialAuxBaseTempl<T, is_ad>
unsigned int _component;
};

typedef MaterialRealVectorValueAuxTempl<RealVectorValue, false> MaterialRealVectorValueAux;
typedef MaterialRealVectorValueAuxTempl<RealVectorValue, true> ADMaterialRealVectorValueAux;
typedef MaterialRealVectorValueAuxTempl<SymmetricRankTwoTensor, false>
typedef MaterialRealVectorValueAuxTempl<RealVectorValue, false, false> MaterialRealVectorValueAux;
typedef MaterialRealVectorValueAuxTempl<RealVectorValue, true, false> ADMaterialRealVectorValueAux;
typedef MaterialRealVectorValueAuxTempl<RealVectorValue, false, true>
FunctorMaterialRealVectorValueAux;
typedef MaterialRealVectorValueAuxTempl<RealVectorValue, true, true>
ADFunctorMaterialRealVectorValueAux;
typedef MaterialRealVectorValueAuxTempl<SymmetricRankTwoTensor, false, false>
MaterialSymmetricRankTwoTensorAux;
typedef MaterialRealVectorValueAuxTempl<SymmetricRankTwoTensor, true>
typedef MaterialRealVectorValueAuxTempl<SymmetricRankTwoTensor, true, false>
ADMaterialSymmetricRankTwoTensorAux;
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
template <bool is_ad>
class VectorMaterialRealVectorValueAuxTempl
: public MaterialAuxBaseTempl<RealVectorValue, is_ad, RealVectorValue>
: public MaterialAuxBaseTempl<RealVectorValue, is_ad, false, RealVectorValue>
{
public:
static InputParameters validParams();
Expand Down
2 changes: 1 addition & 1 deletion framework/include/base/Assembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ class Assembly
}

template <bool is_ad>
const MooseArray<GenericType<Point, is_ad>> & genericQPoints() const;
const MooseArray<Moose::GenericType<Point, is_ad>> & genericQPoints() const;

/**
* Return the current element
Expand Down
Loading

0 comments on commit c24beae

Please sign in to comment.