10#ifdef MOOSE_LIBTORCH_ENABLED
21 "Material object which relies on the evaluation of a TorchScript module.");
23 "prop_names",
"The names of the properties this material will generate.");
25 "input_names",
"The input parameters for the neural network.");
27 "torch_script_userobject",
28 "The name of the user object which contains the torch script module.");
35 _prop_names(getParam<
std::vector<
std::string>>(
"prop_names")),
36 _num_props(_prop_names.size()),
37 _input_names(getParam<
std::vector<PostprocessorName>>(
"input_names")),
38 _num_inputs(_input_names.size()),
40 _input_tensor(torch::zeros(
42 torch::TensorOptions().dtype(torch::kFloat64).device(_app.getLibtorchDevice())))
45 paramError(
"prop_names",
"Must declare at least one property!");
48 paramError(
"input_names",
"Must declare at least one input to the neural net!");
73 for (
unsigned int input_i = 0; input_i <
_num_inputs; ++input_i)
78 mooseError(
"The tensor needs to be the same length (right now ",
80 ") as the number of properties (right now ",
84 const auto output_accessor = output.accessor<Real, 2>();
85 for (
unsigned int prop_i = 0; prop_i <
_num_props; ++prop_i)
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
registerMooseObject("MooseApp", TorchScriptMaterial)
auto & declareGenericProperty(const std::string &prop_name)
Materials compute MaterialProperties.
static InputParameters validParams()
void paramError(const std::string ¶m, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
virtual const PostprocessorValue & getPostprocessorValueByName(const PostprocessorName &name) const
Retrieve the value of the Postprocessor.
This material declares properties which are evaluated as based on a torch script neural network.
TorchScriptMaterial(const InputParameters ¶meters)
void computeQpValues()
A helper method for evaluating the torch script module and populating the material properties.
static InputParameters validParams()
torch::Tensor _input_tensor
Place holder for the inputs to the neural network.
const unsigned int _num_inputs
Number of inputs to the neural net.
const TorchScriptUserObject & _torch_script_userobject
The user object that holds the torch module.
const std::vector< PostprocessorName > _input_names
The functions to use for each property.
virtual void initQpStatefulProperties() override
Initialize stateful properties at quadrature points.
const unsigned int _num_props
Number of properties that will be defined.
const std::vector< std::string > _prop_names
Names of the material properties to define.
virtual void computeQpProperties() override
Users must override this method.
std::vector< const PostprocessorValue * > _module_inputs
The module input parameters stored as postprocessor values.
std::vector< GenericMaterialProperty< Real, false > * > _properties
Vector of all the properties, for now we don't support AD.
A user object the loads a torch module using the torch script format and just-in-time compilation.
torch::Tensor evaluate(const torch::Tensor &input) const
Function to evaluate the torch script module at certain input.