LCOV - code coverage report
Current view: top level - src/reporters - BayesianActiveLearner.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: #33416 (b10b36) with base 9fbd27 Lines: 62 72 86.1 %
Date: 2026-07-23 16:21:17 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://www.mooseframework.org
       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             : #include "BayesianActiveLearner.h"
      12             : 
      13             : registerMooseObject("StochasticToolsApp", BayesianActiveLearner);
      14             : 
      15             : InputParameters
      16           6 : BayesianActiveLearner::validParams()
      17             : {
      18           6 :   InputParameters params = GenericActiveLearner::validParams();
      19           6 :   params += LikelihoodInterface::validParams();
      20           6 :   params.addClassDescription(
      21             :       "A reporter to support parallel active learning for Bayesian UQ tasks.");
      22          12 :   params.addRequiredParam<std::vector<UserObjectName>>("likelihoods", "Names of likelihoods.");
      23          12 :   params.addParam<ReporterValueName>(
      24             :       "noise", "noise", "Name of the model noise term to pass to Likelihoods object.");
      25           6 :   return params;
      26           0 : }
      27             : 
      28           3 : BayesianActiveLearner::BayesianActiveLearner(const InputParameters & parameters)
      29             :   : GenericActiveLearnerTempl<BayesianActiveLearningSampler>(parameters),
      30             :     LikelihoodInterface(parameters),
      31           6 :     _new_var_samples(_al_sampler.getVarSamples()),
      32           3 :     _var_prior(_al_sampler.getVarPrior()),
      33           3 :     _var_test(_al_sampler.getVarSampleTries()),
      34           6 :     _noise(declareValue<Real>("noise"))
      35             : {
      36             :   // Filling the `likelihoods` vector with the user-provided distributions.
      37           9 :   for (const UserObjectName & name : getParam<std::vector<UserObjectName>>("likelihoods"))
      38           3 :     _likelihoods.push_back(getLikelihoodFunctionByName(name));
      39             : 
      40           3 :   _num_confg_values = _al_sampler.getNumberOfConfigValues();
      41           3 :   _num_confg_params = _al_sampler.getNumberOfConfigParams();
      42             : 
      43             :   // Resize the length scales depending upon whether variance is included
      44           3 :   _n_dim = _al_sampler.getNumberOfCols() - _al_sampler.getNumberOfConfigParams();
      45           3 :   _n_dim_plus_var = _n_dim + 1;
      46           3 :   if (_var_prior)
      47           0 :     _length_scales.resize(_n_dim_plus_var);
      48             :   else
      49           3 :     _length_scales.resize(_n_dim);
      50             : 
      51             :   // Resize the log-likelihood vector to the number of parallel proposals
      52           3 :   _log_likelihood.resize(_props);
      53           3 : }
      54             : 
      55             : void
      56          12 : BayesianActiveLearner::setupGPData(const std::vector<Real> & data_out,
      57             :                                    const DenseMatrix<Real> & data_in)
      58             : {
      59             :   std::vector<Real> tmp;
      60          12 :   computeLogLikelihood(data_out);
      61          12 :   if (_var_prior)
      62           0 :     tmp.resize(_n_dim_plus_var);
      63             :   else
      64          12 :     tmp.resize(_n_dim);
      65          72 :   for (unsigned int i = 0; i < _props; ++i)
      66             :   {
      67         180 :     for (unsigned int j = 0; j < _n_dim; ++j)
      68         120 :       tmp[j] = data_in(i, j);
      69          60 :     if (_var_prior)
      70           0 :       tmp[_n_dim] = _new_var_samples[i];
      71          60 :     if (!std::isnan(_log_likelihood[i]))
      72             :     {
      73          60 :       _gp_inputs.push_back(tmp);
      74          60 :       _gp_outputs.push_back(_log_likelihood[i]);
      75             :     }
      76             :   }
      77          12 : }
      78             : 
      79             : void
      80          12 : BayesianActiveLearner::computeLogLikelihood(const std::vector<Real> & data_out)
      81             : {
      82          12 :   _log_likelihood.assign(_props, 0.0);
      83          12 :   std::vector<Real> out1(_num_confg_values);
      84          72 :   for (unsigned int i = 0; i < _props; ++i)
      85             :   {
      86         180 :     for (unsigned int j = 0; j < _num_confg_values; ++j)
      87         120 :       out1[j] = data_out[j * _props + i];
      88          60 :     if (_var_prior)
      89             :     {
      90           0 :       _noise = std::sqrt(_new_var_samples[i]);
      91           0 :       _log_likelihood[i] += _likelihoods[0]->function(out1);
      92             :     }
      93             :     else
      94          60 :       _log_likelihood[i] += _likelihoods[0]->function(out1);
      95             :   }
      96          12 : }
      97             : 
      98             : Real
      99           9 : BayesianActiveLearner::computeConvergenceValue()
     100             : {
     101             :   Real convergence_value = 0.0;
     102             :   unsigned int num_valid = 0;
     103          54 :   for (unsigned int ii = 0; ii < _props; ++ii)
     104             :   {
     105          45 :     if (!std::isnan(_log_likelihood[ii]))
     106             :     {
     107          45 :       convergence_value += Utility::pow<2>(_log_likelihood[ii] - _eval_outputs_current[ii]);
     108          45 :       ++num_valid;
     109             :     }
     110             :   }
     111           9 :   convergence_value = std::sqrt(convergence_value) / num_valid;
     112           9 :   return convergence_value;
     113             : }
     114             : 
     115             : void
     116          12 : BayesianActiveLearner::evaluateGPTest()
     117             : {
     118             :   std::vector<Real> tmp;
     119          12 :   if (_var_prior)
     120           0 :     tmp.resize(_n_dim_plus_var);
     121             :   else
     122          12 :     tmp.resize(_n_dim);
     123        1212 :   for (unsigned int i = 0; i < _gp_outputs_test.size(); ++i)
     124             :   {
     125        1200 :     std::copy(_inputs_test[i].begin(), _inputs_test[i].end(), tmp.begin());
     126        1200 :     if (_var_prior)
     127           0 :       tmp[_n_dim] = _var_test[i];
     128        1200 :     _gp_outputs_test[i] = _gp_eval.evaluate(tmp, _gp_std_test[i]);
     129             :   }
     130          12 : }
     131             : 
     132             : void
     133          12 : BayesianActiveLearner::includeAdditionalInputs()
     134             : {
     135          12 :   _inputs_test_modified = _inputs_test;
     136          12 :   if (_var_prior)
     137           0 :     for (unsigned int i = 0; i < _inputs_test.size(); ++i)
     138           0 :       _inputs_test_modified[i].push_back(_var_test[i]);
     139          12 : }
     140             : 
     141             : #endif

Generated by: LCOV version 1.14