LCOV - code coverage report
Current view: top level - include/covariances - CovarianceFunctionBase.h (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: #33416 (b10b36) with base 9fbd27 Lines: 2 2 100.0 %
Date: 2026-07-23 16:21:17 Functions: 0 0 -
Legend: Lines: hit not hit

          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             : #ifdef MOOSE_LIBTORCH_ENABLED
      10             : 
      11             : #pragma once
      12             : 
      13             : #include "StochasticToolsApp.h"
      14             : #include "MooseObject.h"
      15             : #include "CovarianceInterface.h"
      16             : 
      17             : /**
      18             :  * Base class for covariance functions that are used in Gaussian Processes
      19             :  */
      20             : class CovarianceFunctionBase : public MooseObject, public CovarianceInterface
      21             : {
      22             : public:
      23             :   using HyperParameterMap = std::unordered_map<std::string, torch::Tensor>;
      24             : 
      25             :   /// Return true if a hyperparameter tensor stores one scalar value
      26             :   static bool isScalarHyperParameter(const torch::Tensor & tensor);
      27             : 
      28             :   /// Return true if a hyperparameter tensor stores a vector of values
      29             :   static bool isVectorHyperParameter(const torch::Tensor & tensor);
      30             : 
      31             :   static InputParameters validParams();
      32             :   CovarianceFunctionBase(const InputParameters & parameters);
      33             : 
      34             :   /// Generates the Covariance Matrix given two sets of points in the parameter space
      35             :   /// @param K Reference to a matrix which should be populated by the covariance entries
      36             :   /// @param x Reference to the first set of points
      37             :   /// @param xp Reference to the second set of points
      38             :   /// @param is_self_covariance Switch to enable adding the noise variance to the diagonal of the covariance matrix
      39             :   virtual void computeCovarianceMatrix(torch::Tensor & K,
      40             :                                        const torch::Tensor & x,
      41             :                                        const torch::Tensor & xp,
      42             :                                        const bool is_self_covariance) const = 0;
      43             : 
      44             :   /// Load some hyperparameters into the local map contained in this object.
      45             :   /// @param map Input map of hyperparameters
      46             :   void loadHyperParamMap(const HyperParameterMap & map);
      47             : 
      48             :   /// Populates the input maps with the owned hyperparameters.
      49             :   /// @param map Map of hyperparameters that should be populated
      50             :   void buildHyperParamMap(HyperParameterMap & map) const;
      51             : 
      52             :   /// Get the default minimum and maximum and size of a hyperparameter.
      53             :   /// Returns false is the parameter has not been found in this covariance object.
      54             :   /// @param name The name of the hyperparameter
      55             :   /// @param size Reference to an unsigned int that will contain the size of the
      56             :   ///             hyperparameter (will be populated with 1 if it is scalar)
      57             :   /// @param min Reference to a number which will be populated by the maximum allowed value of the hyperparameter
      58             :   /// @param max Reference to a number which will be populated by the minimum allowed value of the hyperparameter
      59             :   virtual bool
      60             :   getTuningData(const std::string & name, unsigned int & size, Real & min, Real & max) const;
      61             : 
      62             :   /// Populate a map with the names and types of the dependent covariance functions
      63             :   /// @param name_type_map Reference to the map which should be populated
      64             :   void dependentCovarianceTypes(std::map<UserObjectName, std::string> & name_type_map) const;
      65             : 
      66             :   /// Get the names of the dependent covariances
      67             :   const std::vector<UserObjectName> & dependentCovarianceNames() const
      68             :   {
      69         240 :     return _dependent_covariance_names;
      70             :   }
      71             : 
      72             :   /// Redirect dK/dhp for hyperparameter "hp".
      73             :   /// Returns false is the parameter has not been found in this covariance object.
      74             :   /// @param dKdhp The matrix which should be populated with the derivatives
      75             :   /// @param x The input vector for which the derivatives of the covariance matrix
      76             :   ///          is computed
      77             :   /// @param hyper_param_name The name of the hyperparameter
      78             :   /// @param ind The index within the hyperparameter. 0 if it is a scalar parameter.
      79             :   ///            If it is a vector parameter, it should be the index within the vector.
      80             :   virtual bool computedKdhyper(torch::Tensor & dKdhp,
      81             :                                const torch::Tensor & x,
      82             :                                const std::string & hyper_param_name,
      83             :                                unsigned int ind) const;
      84             : 
      85             :   /// Check if a given parameter is tunable
      86             :   /// @param The name of the hyperparameter
      87             :   virtual bool isTunable(const std::string & name) const;
      88             : 
      89             :   /// Return the number of outputs assumed for this covariance function
      90      134369 :   unsigned int numOutputs() const { return _num_outputs; }
      91             : 
      92             : protected:
      93             :   /// Register a scalar hyperparameter to this covariance function
      94             :   /// @param name The name of the parameter
      95             :   /// @param value The initial value of the parameter
      96             :   /// @param is_tunable If the parameter is tunable during optimization
      97             :   torch::Tensor &
      98             :   addRealHyperParameter(const std::string & name, const Real value, const bool is_tunable);
      99             : 
     100             :   /// Register a vector hyperparameter to this covariance function
     101             :   /// @param name The name of the parameter
     102             :   /// @param value The initial value of the parameter
     103             :   /// @param is_tunable If the parameter is tunable during optimization
     104             :   torch::Tensor & addVectorRealHyperParameter(const std::string & name,
     105             :                                               const std::vector<Real> & value,
     106             :                                               const bool is_tunable);
     107             : 
     108             :   /// Map of hyperparameters stored as rank-0 or rank-1 tensors
     109             :   HyperParameterMap _hyperparameters;
     110             : 
     111             :   /// list of tunable hyper-parameters
     112             :   std::unordered_set<std::string> _tunable_hp;
     113             : 
     114             :   /// The number of outputs this covariance function is used to describe
     115             :   const unsigned int _num_outputs;
     116             : 
     117             :   /// The names of the dependent covariance functions
     118             :   const std::vector<UserObjectName> _dependent_covariance_names;
     119             : 
     120             :   /// The types of the dependent covariance functions
     121             :   std::vector<std::string> _dependent_covariance_types;
     122             : 
     123             :   /// Vector of pointers to the dependent covariance functions
     124             :   std::vector<CovarianceFunctionBase *> _covariance_functions;
     125             : };
     126             : 
     127             : #endif

Generated by: LCOV version 1.14