From 87daeedeb3befe3adb8ea6459dd8d26ad017f733 Mon Sep 17 00:00:00 2001 From: Guillaume Giudicelli Date: Thu, 25 May 2023 21:02:27 -0600 Subject: [PATCH] Add converter to go from functors to regular material properties refs #19420 --- .../materials/MaterialFunctorConverter.md | 10 ++ .../materials/MaterialFunctorConverter.h | 41 +++++++++ .../src/materials/MaterialFunctorConverter.C | 91 +++++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 framework/doc/content/source/materials/MaterialFunctorConverter.md create mode 100644 framework/include/materials/MaterialFunctorConverter.h create mode 100644 framework/src/materials/MaterialFunctorConverter.C diff --git a/framework/doc/content/source/materials/MaterialFunctorConverter.md b/framework/doc/content/source/materials/MaterialFunctorConverter.md new file mode 100644 index 000000000000..d2989eaf0968 --- /dev/null +++ b/framework/doc/content/source/materials/MaterialFunctorConverter.md @@ -0,0 +1,10 @@ +# MaterialFunctorConverter + +The `MaterialFunctorConverter` is used to explicitly convert functors, such as [functor material properties](syntax/FunctorMaterials/index.md) +into regular nonAD or AD material properties. + +!syntax parameters /Materials/MaterialFunctorConverter + +!syntax inputs /Materials/MaterialFunctorConverter + +!syntax children /Materials/MaterialFunctorConverter diff --git a/framework/include/materials/MaterialFunctorConverter.h b/framework/include/materials/MaterialFunctorConverter.h new file mode 100644 index 000000000000..390387c1edbc --- /dev/null +++ b/framework/include/materials/MaterialFunctorConverter.h @@ -0,0 +1,41 @@ +//* This file is part of the MOOSE framework +//* https://www.mooseframework.org +//* +//* All rights reserved, see COPYRIGHT for full restrictions +//* https://github.com/idaholab/moose/blob/master/COPYRIGHT +//* +//* Licensed under LGPL 2.1, please see LICENSE for details +//* https://www.gnu.org/licenses/lgpl-2.1.html + +#pragma once + +#include "Material.h" + +/** + * This material converts functor to regular (AD or not) material properties + */ +template +class MaterialFunctorConverterTempl : public Material +{ +public: + static InputParameters validParams(); + + MaterialFunctorConverterTempl(const InputParameters & parameters); + +protected: + virtual void computeQpProperties() override; + void initQpStatefulProperties() override; + + /// Number of material properties to create + const std::size_t _num_functors_to_convert; + + /// Incoming functors to convert + std::vector> *> _functors_in; + /// Regular material properties to create + std::vector *> _reg_props_out; + /// AD material properties to create + std::vector *> _ad_props_out; +}; + +typedef MaterialFunctorConverterTempl MaterialFunctorConverter; +typedef MaterialFunctorConverterTempl VectorMaterialFunctorConverter; diff --git a/framework/src/materials/MaterialFunctorConverter.C b/framework/src/materials/MaterialFunctorConverter.C new file mode 100644 index 000000000000..bf4e5a377bdc --- /dev/null +++ b/framework/src/materials/MaterialFunctorConverter.C @@ -0,0 +1,91 @@ +//* This file is part of the MOOSE framework +//* https://www.mooseframework.org +//* +//* All rights reserved, see COPYRIGHT for full restrictions +//* https://github.com/idaholab/moose/blob/master/COPYRIGHT +//* +//* Licensed under LGPL 2.1, please see LICENSE for details +//* https://www.gnu.org/licenses/lgpl-2.1.html + +#include "MaterialFunctorConverter.h" + +#include "metaphysicl/raw_type.h" + +registerMooseObject("MooseApp", MaterialFunctorConverter); +registerMooseObject("MooseApp", VectorMaterialFunctorConverter); + +template +InputParameters +MaterialFunctorConverterTempl::validParams() +{ + InputParameters params = Material::validParams(); + params.addClassDescription("Converts functor to nonAD and AD regular material properties"); + params.addParam>( + "functors_in", "The names of the functors to convert to AD properties"); + params.addParam>("ad_props_out", + "The names of the output AD properties"); + params.addParam>("reg_props_out", + "The names of the output regular properties"); + return params; +} + +template +MaterialFunctorConverterTempl::MaterialFunctorConverterTempl(const InputParameters & parameters) + : Material(parameters), + _num_functors_to_convert(getParam>("functors_in").size()) +{ + auto functors_in = getParam>("functors_in"); + auto reg_props_out = getParam>("reg_props_out"); + auto ad_props_out = getParam>("ad_props_out"); + + if (isParamValid("reg_props_out") && isParamValid("ad_props_out")) + paramError("reg_props_out", + "We dont support converting functors to both AD and regular material properties. " + "Please use another " + + type() + " to convert to both types."); + + if (functors_in.size() != reg_props_out.size() && functors_in.size() != ad_props_out.size()) + paramError( + "functors_in", + "The number of output properties must match the number of input functors, which is " + + std::to_string(functors_in.size())); + + _functors_in.resize(_num_functors_to_convert); + _ad_props_out.resize(ad_props_out.size()); + _reg_props_out.resize(reg_props_out.size()); + + for (const auto i : make_range(_num_functors_to_convert)) + _functors_in[i] = &getFunctor>(functors_in[i]); + + for (const auto i : index_range(ad_props_out)) + _ad_props_out[i] = &declareADProperty(ad_props_out[i]); + + for (const auto i : index_range(reg_props_out)) + _reg_props_out[i] = &declareProperty(reg_props_out[i]); +} + +template +void +MaterialFunctorConverterTempl::initQpStatefulProperties() +{ + computeQpProperties(); +} + +template +void +MaterialFunctorConverterTempl::computeQpProperties() +{ + // TODO: do we know when we are on a face or an element? + const Moose::ElemQpArg arg = {_current_elem, _qp, _qrule}; + mooseAssert(_current_elem, "We must know where we are"); + mooseAssert(_qrule, "We must know the quadrature"); + const auto state = Moose::currentState(); + for (const auto i : index_range(_ad_props_out)) + (*_ad_props_out[i])[_qp] = (*_functors_in[i])(arg, state); + + for (const auto i : index_range(_reg_props_out)) + (*_reg_props_out[i])[_qp] = MetaPhysicL::raw_value((*_functors_in[i])(arg, state)); +} + +template class MaterialFunctorConverterTempl; +template class MaterialFunctorConverterTempl;