LCOV - code coverage report
Current view: top level - src/reporters - BiFidelityActiveLearningGPDecision.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: f45d79 Lines: 40 41 97.6 %
Date: 2025-07-25 05:00:46 Functions: 5 5 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 "BiFidelityActiveLearningGPDecision.h"
      11             : #include "Sampler.h"
      12             : 
      13             : registerMooseObject("StochasticToolsApp", BiFidelityActiveLearningGPDecision);
      14             : 
      15             : InputParameters
      16          60 : BiFidelityActiveLearningGPDecision::validParams()
      17             : {
      18          60 :   InputParameters params = ActiveLearningGPDecision::validParams();
      19          60 :   params.addClassDescription("Perform active learning decision making in bi-fidelity modeling.");
      20         120 :   params.addRequiredParam<SamplerName>("sampler", "The sampler object.");
      21         120 :   params.addRequiredParam<ReporterName>("outputs_lf",
      22             :                                         "Value of the LF model output from the SubApp.");
      23         120 :   params.addParam<ReporterValueName>("lf_corrected", "lf_corrected", "GP-corrected LF prediciton.");
      24          60 :   return params;
      25           0 : }
      26             : 
      27          30 : BiFidelityActiveLearningGPDecision::BiFidelityActiveLearningGPDecision(
      28          30 :     const InputParameters & parameters)
      29             :   : ActiveLearningGPDecision(parameters),
      30          30 :     _sampler(getSampler("sampler")),
      31          60 :     _outputs_lf(getReporterValue<std::vector<Real>>("outputs_lf", REPORTER_MODE_DISTRIBUTED)),
      32          60 :     _lf_corrected(declareValue<std::vector<Real>>("lf_corrected",
      33          30 :                                                   std::vector<Real>(sampler().getNumberOfRows()))),
      34          60 :     _local_comm(_sampler.getLocalComm())
      35             : {
      36          30 : }
      37             : 
      38             : bool
      39         350 : BiFidelityActiveLearningGPDecision::facilitateDecision()
      40             : {
      41        1020 :   for (dof_id_type i = 0; i < _inputs.size(); ++i)
      42             :   {
      43         670 :     _gp_mean[i] = _gp_eval.evaluate(_inputs[i], _gp_std[i]);
      44         670 :     _flag_sample[i] = !learningFunction(_outputs_lf_batch[i] + _gp_mean[i], _gp_std[i]);
      45         670 :     _lf_corrected[i] = _outputs_lf_batch[i] + _gp_mean[i];
      46             :   }
      47             : 
      48         350 :   for (const auto & fs : _flag_sample)
      49         490 :     if (!fs)
      50             :       return false;
      51             :   return true;
      52             : }
      53             : 
      54             : void
      55         560 : BiFidelityActiveLearningGPDecision::preNeedSample()
      56             : {
      57         560 :   _outputs_lf_batch = _outputs_lf;
      58         560 :   _local_comm.allgather(_outputs_lf_batch);
      59             :   // Accumulate inputs and outputs if we previously decided we needed a sample
      60         560 :   if (_t_step > 1 && _decision)
      61             :   {
      62         270 :     std::vector<Real> differences(_outputs_global.size());
      63         740 :     for (dof_id_type i = 0; i < _outputs_global.size(); ++i)
      64         470 :       differences[i] = _outputs_global[i] - _outputs_lf_batch[i];
      65             : 
      66             :     // Accumulate data into _batch members
      67         270 :     setupData(_inputs, differences);
      68             : 
      69             :     // Retrain if we are outside the training phase
      70         270 :     if (_t_step >= _n_train)
      71          90 :       _al_gp.reTrain(_inputs_batch, _outputs_batch);
      72             :   }
      73             : 
      74             :   // Gather inputs for the current step
      75         560 :   _inputs = _inputs_global;
      76             : 
      77             :   // Evaluate GP and decide if we need more data if outside training phase
      78         560 :   if (_t_step >= _n_train)
      79         350 :     _decision = facilitateDecision();
      80         560 : }
      81             : 
      82             : bool
      83         560 : BiFidelityActiveLearningGPDecision::needSample(const std::vector<Real> &,
      84             :                                                dof_id_type,
      85             :                                                dof_id_type global_ind,
      86             :                                                Real & val)
      87             : {
      88         560 :   if (!_decision)
      89         280 :     val = _outputs_lf_batch[global_ind] + _gp_mean[global_ind];
      90         560 :   return _decision;
      91             : }

Generated by: LCOV version 1.14