LCOV - code coverage report
Current view: top level - src/acquisitions - ParallelAcquisitionFunctionBase.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: #32971 (54bef8) with base c6cf66 Lines: 41 51 80.4 %
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://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 "ParallelAcquisitionFunctionBase.h"
      11             : 
      12             : InputParameters
      13          96 : ParallelAcquisitionFunctionBase::validParams()
      14             : {
      15          96 :   InputParameters params = MooseObject::validParams();
      16          96 :   params.addClassDescription("Base class for parallel acquisition functions");
      17          96 :   params.registerBase("ParallelAcquisitionFunctionBase");
      18          96 :   params.registerSystemAttributeName("ParallelAcquisitionFunctionBase");
      19          96 :   return params;
      20           0 : }
      21             : 
      22          48 : ParallelAcquisitionFunctionBase::ParallelAcquisitionFunctionBase(const InputParameters & parameters)
      23          48 :   : MooseObject(parameters)
      24             : {
      25          48 : }
      26             : 
      27             : void
      28         108 : ParallelAcquisitionFunctionBase::computeAcquisition(
      29             :     std::vector<Real> & acq,
      30             :     const std::vector<Real> & gp_mean,
      31             :     const std::vector<Real> & gp_std,
      32             :     const std::vector<std::vector<Real>> & test_inputs,
      33             :     const std::vector<std::vector<Real>> & train_inputs,
      34             :     const std::vector<Real> & generic) const
      35             : {
      36             :   // Size checks
      37         108 :   if (test_inputs.size() == 0)
      38           0 :     mooseError("computeAcquisition: test_inputs must be non-empty.");
      39             : 
      40         108 :   if (gp_mean.size() != test_inputs.size() || gp_std.size() != test_inputs.size())
      41           0 :     mooseError("computeAcquisition: gp_mean, gp_std, and test_inputs must have the same length.");
      42             : 
      43         108 :   if (acq.size() != test_inputs.size())
      44           0 :     mooseError("computeAcquisition: output 'acq' must be pre-sized to test_inputs.size().");
      45             : 
      46             :   // Dimensionality checks (all rows same dim; train dim matches test dim)
      47         108 :   if (test_inputs.front().size() == 0)
      48           0 :     mooseError("computeAcquisition: test_inputs row dimension must be > 0.");
      49             : 
      50         108 :   if (train_inputs[0].size() != test_inputs.front().size())
      51           0 :     mooseError("computeAcquisition: train_inputs rows must match test_inputs dimension.");
      52             : 
      53             :   // Hand off to the derived implementation
      54         108 :   computeAcquisitionInternal(acq, gp_mean, gp_std, test_inputs, train_inputs, generic);
      55         108 : }
      56             : 
      57             : void
      58         108 : ParallelAcquisitionFunctionBase::penalizeAcquisition(std::vector<Real> & modified_acq,
      59             :                                                      std::vector<unsigned int> & sorted_indices,
      60             :                                                      const std::vector<Real> & acq,
      61             :                                                      const std::vector<Real> & length_scales,
      62             :                                                      const std::vector<std::vector<Real>> & inputs)
      63             : {
      64         108 :   if (inputs.size() == 0)
      65           0 :     mooseError("penalizeAcquisition: 'inputs' must be non-empty.");
      66         108 :   if (modified_acq.size() != inputs.size() || sorted_indices.size() != inputs.size() ||
      67             :       acq.size() != inputs.size())
      68           0 :     mooseError(
      69             :         "penalizeAcquisition: modified_acq, sorted_indices, and acq must match inputs.size().");
      70             : 
      71             :   if (inputs.front().size() != inputs.front().size())
      72             :     mooseError("penalizeAcquisition: all input rows must have the same dimension.");
      73         108 :   if (length_scales.size() != inputs.front().size())
      74           0 :     mooseError("penalizeAcquisition: length_scales must match input dimension.");
      75             : 
      76         108 :   std::vector<Real> negate_acq = acq;
      77             :   std::transform(negate_acq.cbegin(), negate_acq.cend(), negate_acq.begin(), std::negate<double>());
      78             :   std::vector<size_t> ind;
      79         108 :   Moose::indirectSort(negate_acq.begin(), negate_acq.end(), ind);
      80         108 :   modified_acq[0] = -negate_acq[ind[0]];
      81         108 :   sorted_indices[0] = ind[0];
      82             : 
      83         108 :   Real correlation = 0.0;
      84       93600 :   for (unsigned int i = 0; i < inputs.size() - 1; ++i)
      85             :   {
      86    92159892 :     for (unsigned int j = 0; j < inputs.size(); ++j)
      87             :     {
      88    92066400 :       computeCorrelation(correlation, inputs[j], inputs[ind[0]], length_scales);
      89    92066400 :       negate_acq[j] = negate_acq[j] * correlation;
      90             :     }
      91       93492 :     Moose::indirectSort(negate_acq.begin(), negate_acq.end(), ind);
      92       93492 :     modified_acq[i + 1] = -negate_acq[ind[0]];
      93       93492 :     sorted_indices[i + 1] = ind[0];
      94             :   }
      95         108 : }
      96             : 
      97             : void
      98    92066400 : ParallelAcquisitionFunctionBase::computeCorrelation(Real & corr,
      99             :                                                     const std::vector<Real> & input1,
     100             :                                                     const std::vector<Real> & input2,
     101             :                                                     const std::vector<Real> & length_scales)
     102             : {
     103    92066400 :   if (input1.size() != input2.size() || input1.size() != length_scales.size())
     104           0 :     mooseError("computeCorrelation: input1, input2, and length_scales must be the same size.");
     105    92066400 :   corr = 0.0;
     106   276199200 :   for (unsigned int i = 0; i < input1.size(); ++i)
     107   184132800 :     corr -= Utility::pow<2>(input1[i] - input2[i]) / (2.0 * Utility::pow<2>(length_scales[i]));
     108    92066400 :   corr = 1.0 - std::exp(corr);
     109    92066400 : }

Generated by: LCOV version 1.14