Line data Source code
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 160 : exportHyperParameter(const torch::Tensor & tensor) 25 : { 26 160 : auto cpu_tensor = LibtorchUtils::toCPUContiguous(tensor); 27 160 : if (cpu_tensor.scalar_type() != at::kDouble) 28 0 : cpu_tensor = cpu_tensor.to(at::kDouble).contiguous(); 29 160 : const auto flattened = cpu_tensor.reshape({-1}); 30 480 : return {flattened.data_ptr<Real>(), flattened.data_ptr<Real>() + flattened.numel()}; 31 : } 32 : 33 : Real 34 264 : exportScalarHyperParameter(const torch::Tensor & tensor) 35 : { 36 264 : auto cpu_tensor = LibtorchUtils::toCPUContiguous(tensor); 37 264 : if (cpu_tensor.scalar_type() != at::kDouble) 38 0 : cpu_tensor = cpu_tensor.to(at::kDouble); 39 528 : return cpu_tensor.item<Real>(); 40 : } 41 : 42 : } // namespace 43 : 44 : InputParameters 45 226 : GaussianProcessData::validParams() 46 : { 47 226 : InputParameters params = GeneralVectorPostprocessor::validParams(); 48 226 : params += SurrogateModelInterface::validParams(); 49 226 : params.addClassDescription( 50 : "Tool for extracting hyperparameter data from gaussian process user object and " 51 : "storing in VectorPostprocessor vectors."); 52 452 : params.addRequiredParam<UserObjectName>("gp_name", "Name of GaussianProcess."); 53 226 : return params; 54 0 : } 55 : 56 112 : GaussianProcessData::GaussianProcessData(const InputParameters & parameters) 57 : : GeneralVectorPostprocessor(parameters), 58 : SurrogateModelInterface(this), 59 112 : _gp_surrogate(getSurrogateModel<GaussianProcessSurrogate>("gp_name")) 60 : { 61 112 : } 62 : 63 : void 64 112 : GaussianProcessData::initialize() 65 : { 66 112 : const auto & hyperparam_map = _gp_surrogate.getGP().getHyperParamMap(); 67 : 68 536 : for (const auto & iter : hyperparam_map) 69 : { 70 424 : if (CovarianceFunctionBase::isScalarHyperParameter(iter.second)) 71 : { 72 264 : _hp_vector.push_back(&declareVector(iter.first)); 73 264 : _hp_vector.back()->push_back(exportScalarHyperParameter(iter.second)); 74 264 : continue; 75 : } 76 : 77 160 : if (!CovarianceFunctionBase::isVectorHyperParameter(iter.second)) 78 0 : mooseError("Unsupported hyperparameter rank ", iter.second.dim(), " for ", iter.first, "."); 79 : 80 160 : const auto vec = exportHyperParameter(iter.second); 81 472 : for (unsigned int ii = 0; ii < vec.size(); ++ii) 82 : { 83 624 : _hp_vector.push_back(&declareVector(iter.first + std::to_string(ii))); 84 312 : _hp_vector.back()->push_back(vec[ii]); 85 : } 86 160 : } 87 112 : } 88 : 89 : #endif