https://mooseframework.inl.gov
GaussianProcessData.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 // Stocastic Tools Includes
11 
12 #ifdef MOOSE_LIBTORCH_ENABLED
13 
14 #include "GaussianProcessData.h"
15 #include "CovarianceFunctionBase.h"
16 #include "LibtorchUtils.h"
17 
18 registerMooseObject("StochasticToolsApp", GaussianProcessData);
19 
20 namespace
21 {
22 
23 std::vector<Real>
24 exportHyperParameter(const torch::Tensor & tensor)
25 {
26  auto cpu_tensor = LibtorchUtils::toCPUContiguous(tensor);
27  if (cpu_tensor.scalar_type() != at::kDouble)
28  cpu_tensor = cpu_tensor.to(at::kDouble).contiguous();
29  const auto flattened = cpu_tensor.reshape({-1});
30  return {flattened.data_ptr<Real>(), flattened.data_ptr<Real>() + flattened.numel()};
31 }
32 
33 Real
34 exportScalarHyperParameter(const torch::Tensor & tensor)
35 {
36  auto cpu_tensor = LibtorchUtils::toCPUContiguous(tensor);
37  if (cpu_tensor.scalar_type() != at::kDouble)
38  cpu_tensor = cpu_tensor.to(at::kDouble);
39  return cpu_tensor.item<Real>();
40 }
41 
42 } // namespace
43 
46 {
49  params.addClassDescription(
50  "Tool for extracting hyperparameter data from gaussian process user object and "
51  "storing in VectorPostprocessor vectors.");
52  params.addRequiredParam<UserObjectName>("gp_name", "Name of GaussianProcess.");
53  return params;
54 }
55 
57  : GeneralVectorPostprocessor(parameters),
59  _gp_surrogate(getSurrogateModel<GaussianProcessSurrogate>("gp_name"))
60 {
61 }
62 
63 void
65 {
66  const auto & hyperparam_map = _gp_surrogate.getGP().getHyperParamMap();
67 
68  for (const auto & iter : hyperparam_map)
69  {
71  {
72  _hp_vector.push_back(&declareVector(iter.first));
73  _hp_vector.back()->push_back(exportScalarHyperParameter(iter.second));
74  continue;
75  }
76 
78  mooseError("Unsupported hyperparameter rank ", iter.second.dim(), " for ", iter.first, ".");
79 
80  const auto vec = exportHyperParameter(iter.second);
81  for (unsigned int ii = 0; ii < vec.size(); ++ii)
82  {
83  _hp_vector.push_back(&declareVector(iter.first + std::to_string(ii)));
84  _hp_vector.back()->push_back(vec[ii]);
85  }
86  }
87 }
88 
89 #endif
const HyperParameterMap & getHyperParamMap() const
static bool isVectorHyperParameter(const torch::Tensor &tensor)
Return true if a hyperparameter tensor stores a vector of values.
const StochasticTools::GaussianProcess & getGP() const
torch::Tensor toCPUContiguous(const torch::Tensor &tensor)
static bool isScalarHyperParameter(const torch::Tensor &tensor)
Return true if a hyperparameter tensor stores one scalar value.
virtual void initialize() override
void addRequiredParam(const std::string &name, const std::string &doc_string)
static InputParameters validParams()
static InputParameters validParams()
const GaussianProcessSurrogate & _gp_surrogate
Reference to GaussianProcess.
VectorPostprocessorValue & declareVector(const std::string &vector_name)
std::vector< VectorPostprocessorValue * > _hp_vector
Vector of hyperparamater values.
GaussianProcessData(const InputParameters &parameters)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Interface for objects that need to use samplers.
void mooseError(Args &&... args) const
void addClassDescription(const std::string &doc_string)
registerMooseObject("StochasticToolsApp", GaussianProcessData)
static InputParameters validParams()