LCOV - code coverage report
Current view: top level - src/samplers - BayesianActiveLearningSampler.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: #33416 (b10b36) with base 9fbd27 Lines: 31 35 88.6 %
Date: 2026-07-23 16:21:17 Functions: 6 6 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             : 
      10             : #include "BayesianActiveLearningSampler.h"
      11             : #include "Normal.h"
      12             : #include "Uniform.h"
      13             : 
      14             : registerMooseObject("StochasticToolsApp", BayesianActiveLearningSampler);
      15             : 
      16             : InputParameters
      17           7 : BayesianActiveLearningSampler::validParams()
      18             : {
      19           7 :   InputParameters params = PMCMCBase::validParams();
      20           7 :   params.addClassDescription("Fast Bayesian inference with the parallel active learning (partly "
      21             :                              "inspired from El Gammal et al. 2023).");
      22          14 :   params.addRequiredParam<ReporterName>(
      23             :       "sorted_indices", "The sorted sample indices in order of importance to evaluate the subApp.");
      24          14 :   params.addRequiredRangeCheckedParam<unsigned int>(
      25             :       "num_tries",
      26             :       "num_tries>0",
      27             :       "Number of samples to propose in each iteration (not all are sent for subApp evals).");
      28           7 :   return params;
      29           0 : }
      30             : 
      31           4 : BayesianActiveLearningSampler::BayesianActiveLearningSampler(const InputParameters & parameters)
      32             :   : PMCMCBase(parameters),
      33           4 :     _sorted_indices(getReporterValue<std::vector<unsigned int>>("sorted_indices")),
      34           8 :     _num_tries(getParam<unsigned int>("num_tries")),
      35           4 :     _inputs_test(_num_tries, std::vector<Real>(_priors.size())),
      36           8 :     _var_test(_num_tries)
      37             : {
      38           4 : }
      39             : 
      40             : const std::vector<std::vector<Real>> &
      41           3 : BayesianActiveLearningSampler::getSampleTries() const
      42             : {
      43           3 :   return _inputs_test;
      44             : }
      45             : 
      46             : const std::vector<Real> &
      47           3 : BayesianActiveLearningSampler::getVarSampleTries() const
      48             : {
      49           3 :   return _var_test;
      50             : }
      51             : 
      52             : void
      53          24 : BayesianActiveLearningSampler::proposeSamples()
      54             : {
      55        2420 :   auto fill_vector = [&](std::vector<Real> & vector)
      56             :   {
      57        7260 :     for (unsigned int i = 0; i < _priors.size(); ++i)
      58        4840 :       vector[i] = _priors[i]->quantile(random());
      59        2444 :   };
      60             : 
      61             :   /* If step is 1, randomly generate the samples.
      62             :   Else, generate the samples informed by the GP from the reporter "sorted_indices" */
      63         144 :   for (dof_id_type i = 0; i < _num_parallel_proposals; ++i)
      64             :   {
      65         120 :     if (_t_step < 1)
      66             :     {
      67          20 :       fill_vector(_new_samples[i]);
      68          20 :       if (_var_prior)
      69           0 :         _new_var_samples[i] = _var_prior->quantile(random());
      70             :     }
      71             :     else
      72             :     {
      73         100 :       _new_samples[i] = _inputs_test[_sorted_indices[i]];
      74         100 :       if (_var_prior)
      75           0 :         _new_var_samples[i] = _var_test[_sorted_indices[i]];
      76             :     }
      77             :   }
      78             : 
      79             :   /* Finally, generate several new samples randomly for the GP to try and pass it to the
      80             :   reporter */
      81        2424 :   for (dof_id_type i = 0; i < _num_tries; ++i)
      82             :   {
      83        2400 :     fill_vector(_inputs_test[i]);
      84        2400 :     if (_var_prior)
      85           0 :       _var_test[i] = _var_prior->quantile(random());
      86             :   }
      87          24 : }

Generated by: LCOV version 1.14