Loading [MathJax]/extensions/tex2jax.js
https://mooseframework.inl.gov
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
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 LIBTORCH_ENABLED
11 
12 #include "TorchScriptMaterial.h"
13 
15 
18 {
20  params.addClassDescription(
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 
57 void
59 {
61 }
62 
63 void
65 {
67 }
68 
69 void
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 
76  const auto output = _torch_script_userobject.evaluate(_input_tensor);
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 ",
81  _num_props,
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
std::vector< GenericMaterialProperty< Real, false > * > _properties
Vector of all the properties, for now we don&#39;t support AD.
static InputParameters validParams()
registerMooseObject("MooseApp", TorchScriptMaterial)
std::vector< const PostprocessorValue * > _module_inputs
The module input parameters stored as postprocessor values.
const unsigned int _num_inputs
Number of inputs to the neural net.
torch::Tensor _input_tensor
Place holder for the inputs to the neural network.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
A user object the loads a torch module using the torch script format and just-in-time compilation...
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...
This material declares properties which are evaluated as based on a torch script neural network...
const TorchScriptUserObject & _torch_script_userobject
The user object that holds the torch module.
unsigned int _qp
Definition: MaterialBase.h:320
static InputParameters validParams()
Definition: Material.C:14
TorchScriptMaterial(const InputParameters &parameters)
Materials compute MaterialProperties.
Definition: Material.h:34
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const unsigned int _num_props
Number of properties that will be defined.
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
virtual void initQpStatefulProperties() override
Initialize stateful properties at quadrature points.
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...
void computeQpValues()
A helper method for evaluating the torch script module and populating the material properties...
torch::Tensor evaluate(const torch::Tensor &input) const
Function to evaluate the torch script module at certain input.
virtual void computeQpProperties() override
Users must override this method.