https://mooseframework.inl.gov
LibtorchNeuralNetControlTransfer.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 
14 
16 
19 {
22 
23  params.addClassDescription("Copies a neural network from a trainer object on the main app to a "
24  "LibtorchNeuralNetControl object on the subapp.");
25 
26  params.suppressParameter<MultiAppName>("from_multi_app");
27  params.suppressParameter<MultiAppName>("multi_app");
28  params.suppressParameter<MultiMooseEnum>("direction");
29 
30  params.addRequiredParam<UserObjectName>("trainer_name",
31  "Trainer object that contains the neural networks."
32  " for different samples.");
33  params.addRequiredParam<std::string>("control_name", "Controller object name.");
34  return params;
35 }
36 
38  const InputParameters & parameters)
39  : MultiAppTransfer(parameters),
41  _control_name(getParam<std::string>("control_name")),
42  _trainer(getSurrogateTrainerByName<LibtorchDRLControlTrainer>(
43  getParam<UserObjectName>("trainer_name")))
44 {
45 }
46 
47 void
49 {
50  // Get the control neural net from the trainer
52 
53  // Get the control object from the other app
54  FEProblemBase & app_problem = _multi_app->appProblemBase(0);
55  auto & control_warehouse = app_problem.getControlWarehouse();
56  std::shared_ptr<Control> control_ptr = control_warehouse.getActiveObject(_control_name);
57  LibtorchNeuralNetControl * control_object =
58  dynamic_cast<LibtorchNeuralNetControl *>(control_ptr.get());
59 
60  if (!control_object)
61  paramError("control_name", "The given gontrol is not a LibtorchNeuralNetrControl!");
62 
63  // Copy and the neural net and execute it to get the initial values
64  control_object->loadControlNeuralNet(trainer_nn);
65  control_object->execute();
66 }
67 #endif
const LibtorchDRLControlTrainer & _trainer
The trainer object which will contains the control neural net.
std::shared_ptr< MultiApp > _multi_app
void addRequiredParam(const std::string &name, const std::string &doc_string)
void suppressParameter(const std::string &name)
registerMooseObject("StochasticToolsApp", LibtorchNeuralNetControlTransfer)
std::shared_ptr< Control > getActiveObject(const std::string &name, THREAD_ID tid=0) const
const Moose::LibtorchArtificialNeuralNet & controlNeuralNet() const
const std::string _control_name
The name of the control object on the other app where we want to copy our neural net.
void paramError(const std::string &param, Args... args) const
static InputParameters validParams()
Interface for objects that need to use samplers.
void addClassDescription(const std::string &doc_string)
This trainer is responsible for training neural networks that efficiently control different processes...
LibtorchNeuralNetControlTransfer(const InputParameters &parameters)
virtual void execute() override
ExecuteMooseObjectWarehouse< Control > & getControlWarehouse()
static InputParameters validParams()
void loadControlNeuralNet(const Moose::LibtorchArtificialNeuralNet &input_nn)