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

Generated by: LCOV version 1.14