https://mooseframework.inl.gov
Loading...
Searching...
No Matches
TorchScriptMaterial.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
10#ifdef MOOSE_LIBTORCH_ENABLED
11
12#include "TorchScriptMaterial.h"
13
15
18{
21 "Material object which relies on the evaluation of a TorchScript module.");
22 params.addRequiredParam<std::vector<std::string>>(
23 "prop_names", "The names of the properties this material will generate.");
24 params.addRequiredParam<std::vector<PostprocessorName>>(
25 "input_names", "The input parameters for the neural network.");
26 params.addRequiredParam<UserObjectName>(
27 "torch_script_userobject",
28 "The name of the user object which contains the torch script module.");
29
30 return params;
31}
32
34 : Material(parameters),
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()),
39 _torch_script_userobject(getUserObject<TorchScriptUserObject>("torch_script_userobject")),
40 _input_tensor(torch::zeros(
41 {1, _num_inputs},
42 torch::TensorOptions().dtype(torch::kFloat64).device(_app.getLibtorchDevice())))
43{
44 if (!_num_props)
45 paramError("prop_names", "Must declare at least one property!");
46
47 if (!_num_inputs)
48 paramError("input_names", "Must declare at least one input to the neural net!");
49
50 for (const auto & input_name : _input_names)
51 _module_inputs.push_back(&getPostprocessorValueByName(input_name));
52
53 for (const auto & prop_name : _prop_names)
54 _properties.push_back(&declareGenericProperty<Real, false>(prop_name));
55}
56
57void
62
63void
68
69void
71{
72 auto input_accessor = _input_tensor.accessor<Real, 2>();
73 for (unsigned int input_i = 0; input_i < _num_inputs; ++input_i)
74 input_accessor[0][input_i] = (*_module_inputs[input_i]);
75
77 if (_num_props != output.numel())
78 mooseError("The tensor needs to be the same length (right now ",
79 output.numel(),
80 ") as the number of properties (right now ",
82 ")!");
83
84 const auto output_accessor = output.accessor<Real, 2>();
85 for (unsigned int prop_i = 0; prop_i < _num_props; ++prop_i)
86 (*_properties[prop_i])[_qp] = output_accessor[0][prop_i];
87}
88
89#endif
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
registerMooseObject("MooseApp", TorchScriptMaterial)
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump.
unsigned int _qp
auto & declareGenericProperty(const std::string &prop_name)
Materials compute MaterialProperties.
Definition Material.h:36
static InputParameters validParams()
Definition Material.C:14
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition MooseBase.h:457
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 &parameters)
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.