https://mooseframework.inl.gov
NEML2ToMOOSEMaterialProperty.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
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 
11 #include "NEML2ModelExecutor.h"
12 
13 #define registerNEML2ToMOOSEMaterialProperty(alias) \
14  registerMooseObject("MooseApp", NEML2ToMOOSE##alias##MaterialProperty)
15 
22 
23 template <typename T>
26 {
27  auto params = Material::validParams();
28  params.addClassDescription(
29  NEML2Utils::docstring("Provide an output (or its derivative) from a NEML2 model as a MOOSE "
30  "material property of type " +
31  demangle(typeid(T).name()) + "."));
32 
33  params.addRequiredParam<UserObjectName>(
34  "neml2_executor",
35  NEML2Utils::docstring("User object managing the execution of the NEML2 model."));
36  params.addRequiredParam<MaterialPropertyName>(
37  "to_moose",
39  "MOOSE material property used to store the NEML2 output variable (or its derivative)"));
40  params.addRequiredParam<std::string>("from_neml2",
41  NEML2Utils::docstring("NEML2 output variable to read from"));
42  params.addParam<std::string>(
43  "neml2_input_derivative",
45  "If supplied return the derivative of the NEML2 output variable with respect to this"));
46  params.addParam<std::string>(
47  "neml2_parameter_derivative",
49  "If supplied return the derivative of neml2_variable with respect to this"));
50 
51  // provide an optional initialization of the moose property (because we don't really know if it is
52  // going to become stateful or not)
53  params.addParam<MaterialPropertyName>(
54  "moose_material_property_init",
55  NEML2Utils::docstring("Optional material property as the initial condition"));
56 
57  return params;
58 }
59 
60 template <typename T>
62  : Material(params)
63 #ifdef NEML2_ENABLED
64  ,
65  _execute_neml2_model(getUserObject<NEML2ModelExecutor>("neml2_executor")),
66  _prop(declareProperty<T>(getParam<MaterialPropertyName>("to_moose"))),
67  _prop0(isParamValid("moose_material_property_init")
68  ? &getMaterialProperty<T>("moose_material_property_init")
69  : nullptr),
70  _value(
71  !isParamValid("neml2_input_derivative")
72  ? (!isParamValid("neml2_parameter_derivative")
73  ? _execute_neml2_model.getOutput(
74  NEML2Utils::parseVariableName(getParam<std::string>("from_neml2")))
75  : _execute_neml2_model.getOutputParameterDerivative(
76  NEML2Utils::parseVariableName(getParam<std::string>("from_neml2")),
77  getParam<std::string>("neml2_parameter_derivative")))
78  : _execute_neml2_model.getOutputDerivative(
79  NEML2Utils::parseVariableName(getParam<std::string>("from_neml2")),
80  NEML2Utils::parseVariableName(getParam<std::string>("neml2_input_derivative"))))
81 #endif
82 {
84 }
85 
86 #ifdef NEML2_ENABLED
87 template <typename T>
88 void
90 {
91  // See issue #28971: Using _prop0 to set initial condition for this possibly stateful property may
92  // not work. As a workaround, we set the initial condition here when _t_step == 0.
93  if (_t_step == 0 && _prop0)
94  {
95  _prop.set() = _prop0->get();
96  return;
97  }
98 
99  if (!_execute_neml2_model.outputReady())
100  return;
101 
102  // look up start index for current element
103  const auto i = _execute_neml2_model.getBatchIndex(_current_elem->id());
104  for (_qp = 0; _qp < _qrule->n_points(); ++_qp)
105  NEML2Utils::copyTensorToMOOSEData(_value.batch_index({neml2::Size(i + _qp)}), _prop[_qp]);
106 }
107 #endif
108 
109 #define instantiateNEML2ToMOOSEMaterialProperty(T) template class NEML2ToMOOSEMaterialProperty<T>
110 
std::string name(const ElemQuality q)
RankFourTensorTempl is designed to handle any N-dimensional fourth order tensor, C.
void computeProperties() override
Performs the quadrature point loop, calling computeQpProperties.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
NEML2ToMOOSEMaterialProperty(const InputParameters &params)
instantiateNEML2ToMOOSEMaterialProperty(Real)
static InputParameters validParams()
Definition: Material.C:14
SymmetricRankTwoTensorTempl is designed to handle the Stress or Strain Tensor for an anisotropic mate...
void copyTensorToMOOSEData(const at::Tensor &src, T &dest)
Directly copy a contiguous chunk of memory of a at::Tensor to a MOOSE data of type T...
Definition: NEML2Utils.h:135
std::string docstring(const std::string &desc)
Augment docstring if NEML2 is not enabled.
Definition: NEML2Utils.C:77
std::string demangle(const char *name)
Materials compute MaterialProperties.
Definition: Material.h:34
void assertNEML2Enabled()
Assert that NEML2 is enabled.
Definition: NEML2Utils.C:87
NEML2ModelExecutor executes a NEML2 model.
registerNEML2ToMOOSEMaterialProperty(Real)
RankTwoTensorTempl is designed to handle the Stress or Strain Tensor for a fully anisotropic material...
Definition: RankTwoTensor.h:87
SymmetricRankFourTensorTempl is designed to handle an N-dimensional fourth order tensor with minor sy...
neml2::VariableName parseVariableName(const std::string &)
Parse a raw string into NEML2 variable name.
Definition: NEML2Utils.C:49