https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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"
16#include "LibtorchUtils.h"
17
19
20namespace
21{
22
23std::vector<Real>
24exportHyperParameter(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
33Real
34exportScalarHyperParameter(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{
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
63void
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
registerMooseObject("StochasticToolsApp", GaussianProcessData)
static bool isVectorHyperParameter(const torch::Tensor &tensor)
Return true if a hyperparameter tensor stores a vector of values.
static bool isScalarHyperParameter(const torch::Tensor &tensor)
Return true if a hyperparameter tensor stores one scalar value.
std::vector< VectorPostprocessorValue * > _hp_vector
Vector of hyperparamater values.
const GaussianProcessSurrogate & _gp_surrogate
Reference to GaussianProcess.
virtual void initialize() override
GaussianProcessData(const InputParameters &parameters)
static InputParameters validParams()
const StochasticTools::GaussianProcess & getGP() const
static InputParameters validParams()
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
void mooseError(Args &&... args) const
const HyperParameterMap & getHyperParamMap() const
Interface for objects that need to use samplers.
static InputParameters validParams()
VectorPostprocessorValue & declareVector(const std::string &vector_name)
torch::Tensor toCPUContiguous(const torch::Tensor &tensor)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real