LCOV - code coverage report
Current view: top level - src/covariances - CovarianceFunctionBase.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: f45d79 Lines: 66 70 94.3 %
Date: 2025-07-25 05:00:46 Functions: 9 10 90.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             : 
      10             : #include "CovarianceFunctionBase.h"
      11             : 
      12             : InputParameters
      13         876 : CovarianceFunctionBase::validParams()
      14             : {
      15         876 :   InputParameters params = MooseObject::validParams();
      16        1752 :   params.addParam<std::vector<UserObjectName>>(
      17             :       "covariance_functions", {}, "Covariance functions that this covariance function depends on.");
      18        1752 :   params.addParam<unsigned int>(
      19        1752 :       "num_outputs", 1, "The number of outputs expected for this covariance function.");
      20         876 :   params.addClassDescription("Base class for covariance functions");
      21         876 :   params.registerBase("CovarianceFunctionBase");
      22         876 :   params.registerSystemAttributeName("CovarianceFunction");
      23         876 :   return params;
      24           0 : }
      25             : 
      26         438 : CovarianceFunctionBase::CovarianceFunctionBase(const InputParameters & parameters)
      27             :   : MooseObject(parameters),
      28             :     CovarianceInterface(parameters),
      29         876 :     _num_outputs(getParam<unsigned int>("num_outputs")),
      30        1752 :     _dependent_covariance_names(getParam<std::vector<UserObjectName>>("covariance_functions"))
      31             : 
      32             : {
      33             :   // Fetch the dependent covariance functions
      34         486 :   for (const auto & name : _dependent_covariance_names)
      35             :   {
      36          48 :     _covariance_functions.push_back(getCovarianceFunctionByName(name));
      37          48 :     _dependent_covariance_types.push_back(_covariance_functions.back()->type());
      38             :   }
      39         438 : }
      40             : 
      41             : bool
      42           0 : CovarianceFunctionBase::computedKdhyper(RealEigenMatrix & /*dKdhp*/,
      43             :                                         const RealEigenMatrix & /*x*/,
      44             :                                         const std::string & /*hyper_param_name*/,
      45             :                                         unsigned int /*ind*/) const
      46             : {
      47           0 :   mooseError("Hyperparameter tuning not set up for this covariance function. Please define "
      48             :              "computedKdhyper() to compute gradient.");
      49             : }
      50             : 
      51             : const Real &
      52         860 : CovarianceFunctionBase::addRealHyperParameter(const std::string & name,
      53             :                                               const Real value,
      54             :                                               const bool is_tunable)
      55             : {
      56         860 :   const auto prefixed_name = _name + ":" + name;
      57         860 :   if (is_tunable)
      58             :     _tunable_hp.insert(prefixed_name);
      59         860 :   return _hp_map_real.emplace(prefixed_name, value).first->second;
      60             : }
      61             : 
      62             : const std::vector<Real> &
      63         486 : CovarianceFunctionBase::addVectorRealHyperParameter(const std::string & name,
      64             :                                                     const std::vector<Real> value,
      65             :                                                     const bool is_tunable)
      66             : {
      67         486 :   const auto prefixed_name = _name + ":" + name;
      68         486 :   if (is_tunable)
      69             :     _tunable_hp.insert(prefixed_name);
      70         486 :   return _hp_map_vector_real.emplace(prefixed_name, value).first->second;
      71             : }
      72             : 
      73             : bool
      74         548 : CovarianceFunctionBase::isTunable(const std::string & name) const
      75             : {
      76             :   // First, we check if the dependent covariances have the parameter
      77         580 :   for (const auto dependent_covar : _covariance_functions)
      78          64 :     if (dependent_covar->isTunable(name))
      79             :       return true;
      80             : 
      81         516 :   if (_tunable_hp.find(name) != _tunable_hp.end())
      82             :     return true;
      83          64 :   else if (_hp_map_real.find(name) != _hp_map_real.end() ||
      84             :            _hp_map_vector_real.find(name) != _hp_map_vector_real.end())
      85           0 :     mooseError("We found hyperparameter ", name, " but it was not declared tunable!");
      86             : 
      87             :   return false;
      88             : }
      89             : 
      90             : void
      91     1014048 : CovarianceFunctionBase::loadHyperParamMap(
      92             :     const std::unordered_map<std::string, Real> & map,
      93             :     const std::unordered_map<std::string, std::vector<Real>> & vec_map)
      94             : {
      95             :   // First, load the hyperparameters of the dependent covariance functions
      96     1030064 :   for (const auto dependent_covar : _covariance_functions)
      97       16016 :     dependent_covar->loadHyperParamMap(map, vec_map);
      98             : 
      99             :   // Then we load the hyperparameters of this object
     100     3058112 :   for (auto & iter : _hp_map_real)
     101             :   {
     102     2044064 :     const auto & map_iter = map.find(iter.first);
     103     2044064 :     if (map_iter != map.end())
     104     2044064 :       iter.second = map_iter->second;
     105             :   }
     106     2044112 :   for (auto & iter : _hp_map_vector_real)
     107             :   {
     108     1030064 :     const auto & map_iter = vec_map.find(iter.first);
     109     1030064 :     if (map_iter != vec_map.end())
     110     1030064 :       iter.second = map_iter->second;
     111             :   }
     112     1014048 : }
     113             : 
     114             : void
     115        1068 : CovarianceFunctionBase::buildHyperParamMap(
     116             :     std::unordered_map<std::string, Real> & map,
     117             :     std::unordered_map<std::string, std::vector<Real>> & vec_map) const
     118             : {
     119             :   // First, add the hyperparameters of the dependent covariance functions
     120        1116 :   for (const auto dependent_covar : _covariance_functions)
     121          48 :     dependent_covar->buildHyperParamMap(map, vec_map);
     122             : 
     123             :   // At the end we just append the hyperparameters this object owns
     124        3236 :   for (const auto & iter : _hp_map_real)
     125        2168 :     map[iter.first] = iter.second;
     126        2184 :   for (const auto & iter : _hp_map_vector_real)
     127        1116 :     vec_map[iter.first] = iter.second;
     128        1068 : }
     129             : 
     130             : bool
     131         548 : CovarianceFunctionBase::getTuningData(const std::string & name,
     132             :                                       unsigned int & size,
     133             :                                       Real & min,
     134             :                                       Real & max) const
     135             : {
     136             :   // First, check the dependent covariances
     137         580 :   for (const auto dependent_covar : _covariance_functions)
     138          64 :     if (dependent_covar->getTuningData(name, size, min, max))
     139             :       return true;
     140             : 
     141         516 :   min = 1e-9;
     142         516 :   max = 1e9;
     143             : 
     144         516 :   if (_hp_map_real.find(name) != _hp_map_real.end())
     145             :   {
     146         226 :     size = 1;
     147         226 :     return true;
     148             :   }
     149         290 :   else if (_hp_map_vector_real.find(name) != _hp_map_vector_real.end())
     150             :   {
     151             :     const auto & vector_value = _hp_map_vector_real.find(name);
     152         258 :     size = vector_value->second.size();
     153             :     return true;
     154             :   }
     155             :   else
     156             :   {
     157          32 :     size = 0;
     158          32 :     return false;
     159             :   }
     160             : }
     161             : 
     162             : void
     163         434 : CovarianceFunctionBase::dependentCovarianceTypes(
     164             :     std::map<UserObjectName, std::string> & name_type_map) const
     165             : {
     166         482 :   for (const auto dependent_covar : _covariance_functions)
     167             :   {
     168          48 :     dependent_covar->dependentCovarianceTypes(name_type_map);
     169          96 :     name_type_map.insert(std::make_pair(dependent_covar->name(), dependent_covar->type()));
     170             :   }
     171         434 : }

Generated by: LCOV version 1.14