LCOV - code coverage report
Current view: top level - src/samplers - ActiveLearningMonteCarloSampler.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: #32971 (54bef8) with base c6cf66 Lines: 42 44 95.5 %
Date: 2026-05-29 20:40:35 Functions: 4 4 100.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 "ActiveLearningMonteCarloSampler.h"
      11             : #include "Distribution.h"
      12             : 
      13             : registerMooseObject("StochasticToolsApp", ActiveLearningMonteCarloSampler);
      14             : 
      15             : InputParameters
      16          98 : ActiveLearningMonteCarloSampler::validParams()
      17             : {
      18          98 :   InputParameters params = Sampler::validParams();
      19          98 :   params.addClassDescription("Monte Carlo Sampler for active learning with surrogate model.");
      20         196 :   params.addRequiredParam<dof_id_type>("num_batch",
      21             :                                        "The number of full model evaluations in the batch.");
      22         196 :   params.addRequiredParam<std::vector<DistributionName>>(
      23             :       "distributions",
      24             :       "The distribution names to be sampled, the number of distributions provided defines the "
      25             :       "number of columns per matrix.");
      26         196 :   params.addRequiredParam<ReporterName>("flag_sample",
      27             :                                         "Flag samples if the surrogate prediction was inadequate.");
      28         196 :   params.addParam<unsigned int>(
      29             :       "num_random_seeds",
      30         196 :       100000,
      31             :       "Initialize a certain number of random seeds. Change from the default only if you have to.");
      32         196 :   params.addRequiredRangeCheckedParam<int>(
      33             :       "num_samples",
      34             :       "num_samples>0",
      35             :       "Number of samples to use (the total number of steps taken will be equal to this number + "
      36             :       "the number of re-training steps).");
      37          98 :   return params;
      38           0 : }
      39             : 
      40          53 : ActiveLearningMonteCarloSampler::ActiveLearningMonteCarloSampler(const InputParameters & parameters)
      41             :   : Sampler(parameters),
      42          53 :     _flag_sample(getReporterValue<std::vector<bool>>("flag_sample")),
      43         106 :     _step(getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")->timeStep()),
      44         106 :     _num_batch(getParam<dof_id_type>("num_batch")),
      45         212 :     _num_samples(getParam<int>("num_samples"))
      46             : {
      47         265 :   for (const DistributionName & name : getParam<std::vector<DistributionName>>("distributions"))
      48         159 :     _distributions.push_back(&getDistributionByName(name));
      49          53 :   setNumberOfRows(_num_batch);
      50          53 :   setNumberOfCols(_distributions.size());
      51          53 :   _inputs_sto.resize(_num_batch, std::vector<Real>(_distributions.size()));
      52         106 :   setNumberOfRandomSeeds(getParam<unsigned int>("num_random_seeds"));
      53          53 :   setAutoAdvanceGenerators(false);
      54          53 : }
      55             : 
      56             : void
      57        1135 : ActiveLearningMonteCarloSampler::executeSetUp()
      58             : {
      59        1135 :   if (_is_sampling_completed)
      60           0 :     mooseError("Internal bug: the adaptive sampling is supposed to be completed but another sample "
      61             :                "has been requested.");
      62             : 
      63             :   // Keep data where the GP failed
      64        1135 :   if (_step > 0)
      65        2668 :     for (dof_id_type i = 0; i < _num_batch; ++i)
      66        1586 :       if (_flag_sample[i])
      67             :       {
      68         192 :         _inputs_gp_fails.push_back(_inputs_sto[i]);
      69             : 
      70             :         // When the GP fails, the current time step is 'wasted' and the retraining step doesn't
      71             :         // happen until the next time step. Therefore, keep track of the number of retraining steps
      72             :         // to increase the total number of steps taken.
      73         192 :         ++_retraining_steps;
      74             :       }
      75             : 
      76             :   // If we don't have enough failed inputs, generate new ones
      77        1135 :   if (_inputs_gp_fails.size() < _num_batch)
      78             :   {
      79        2522 :     for (dof_id_type i = 0; i < _num_batch; ++i)
      80        5980 :       for (dof_id_type j = 0; j < _distributions.size(); ++j)
      81        4485 :         _inputs_sto[i][j] =
      82        4485 :             _distributions[j]->quantile(getRand(i * _distributions.size() + j, _step));
      83             :   }
      84             :   // If we do have enough failed inputs, assign them and clear the tracked ones
      85             :   else
      86             :   {
      87         108 :     _inputs_sto.assign(_inputs_gp_fails.begin(), _inputs_gp_fails.begin() + _num_batch);
      88         108 :     _inputs_gp_fails.erase(_inputs_gp_fails.begin(), _inputs_gp_fails.begin() + _num_batch);
      89             :   }
      90             : 
      91             :   // check if we have finished the sampling
      92        1135 :   if (_step >= _num_samples + _retraining_steps)
      93          53 :     _is_sampling_completed = true;
      94        1135 : }
      95             : 
      96             : Real
      97        7860 : ActiveLearningMonteCarloSampler::computeSample(dof_id_type row_index, dof_id_type col_index)
      98             : {
      99        7860 :   return _inputs_sto[row_index][col_index];
     100             : }

Generated by: LCOV version 1.14