LCOV - code coverage report
Current view: top level - src/reporters - EvaluateSurrogate.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: #33416 (b10b36) with base 9fbd27 Lines: 57 60 95.0 %
Date: 2026-07-23 16:21:17 Functions: 3 3 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             : // Stocastic Tools Includes
      11             : #include "EvaluateSurrogate.h"
      12             : 
      13             : #include "Sampler.h"
      14             : 
      15             : registerMooseObject("StochasticToolsApp", EvaluateSurrogate);
      16             : 
      17             : InputParameters
      18         754 : EvaluateSurrogate::validParams()
      19             : {
      20         754 :   InputParameters params = StochasticReporter::validParams();
      21         754 :   params += SurrogateModelInterface::validParams();
      22         754 :   params += SamplerInterface::validParams();
      23         754 :   params.addClassDescription("Tool for sampling surrogate models.");
      24        1508 :   params.addRequiredParam<std::vector<UserObjectName>>("model", "Name of surrogate models.");
      25        1508 :   params.addRequiredParam<SamplerName>("sampler",
      26             :                                        "Sampler to use for evaluating surrogate models.");
      27        1508 :   MultiMooseEnum rtypes(SurrogateModel::defaultResponseTypes().getRawNames(), "real");
      28        1508 :   params.addParam<MultiMooseEnum>(
      29             :       "response_type",
      30             :       rtypes,
      31             :       "The type of return value expected from the surrogate models, a single entry will use it for "
      32             :       "every model. Warning: not every model is able evaluate every response type.");
      33         754 :   MultiMooseEnum estd("false=0 true=1", "false");
      34        1508 :   params.addParam<MultiMooseEnum>(
      35             :       "evaluate_std",
      36             :       estd,
      37             :       "Whether or not to evaluate standard deviation associated with each sample, a single entry "
      38             :       "will use it for every model. Warning: not every model can compute standard deviation.");
      39         754 :   return params;
      40         754 : }
      41             : 
      42         375 : EvaluateSurrogate::EvaluateSurrogate(const InputParameters & parameters)
      43             :   : StochasticReporter(parameters),
      44             :     SurrogateModelInterface(this),
      45         375 :     _sampler(getSampler("sampler")),
      46        1500 :     _response_types(getParam<MultiMooseEnum>("response_type"))
      47             : {
      48         375 :   const auto & model_names = getParam<std::vector<UserObjectName>>("model");
      49         375 :   _model.reserve(model_names.size());
      50         799 :   for (const auto & nm : model_names)
      51         424 :     _model.push_back(&getSurrogateModelByName(nm));
      52             : 
      53         375 :   if (_response_types.size() != 1 && _response_types.size() != _model.size())
      54           0 :     paramError("response_type",
      55             :                "Number of entries must be 1 or equal to the number of entries in 'model'.");
      56             : 
      57         375 :   const auto & estd = getParam<MultiMooseEnum>("evaluate_std");
      58         375 :   if (estd.size() != 1 && estd.size() != _model.size())
      59           0 :     paramError("evaluate_std",
      60             :                "Nmber of entries must be 1 or equal to the number of entries in 'model'.");
      61         375 :   _doing_std.resize(_model.size());
      62         799 :   for (const auto i : index_range(_model))
      63         424 :     _doing_std[i] = estd.size() == 1 ? estd[0] == "true" : estd[i] == "true";
      64             : 
      65         375 :   _real_values.resize(_model.size(), nullptr);
      66         375 :   _real_std.resize(_model.size(), nullptr);
      67         375 :   _vector_real_values.resize(_model.size(), nullptr);
      68         375 :   _vector_real_std.resize(_model.size(), nullptr);
      69         799 :   for (const auto i : index_range(_model))
      70             :   {
      71         424 :     const std::string rtype = _response_types.size() == 1 ? _response_types[0] : _response_types[i];
      72         424 :     if (rtype == "real")
      73             :     {
      74         386 :       _real_values[i] = &declareStochasticReporter<Real>(model_names[i], _sampler);
      75         386 :       if (_doing_std[i])
      76         200 :         _real_std[i] = &declareStochasticReporter<Real>(model_names[i] + "_std", _sampler);
      77             :     }
      78          38 :     else if (rtype == "vector_real")
      79             :     {
      80          38 :       _vector_real_values[i] =
      81          76 :           &declareStochasticReporter<std::vector<Real>>(model_names[i], _sampler);
      82          38 :       if (_doing_std[i])
      83          24 :         _vector_real_std[i] =
      84          48 :             &declareStochasticReporter<std::vector<Real>>(model_names[i] + "_std", _sampler);
      85             :     }
      86             :     else
      87           0 :       paramError("response_type", "Unknown response type ", _response_types[i]);
      88             :   }
      89         375 : }
      90             : 
      91             : void
      92         375 : EvaluateSurrogate::execute()
      93             : {
      94             :   // Loop over samples
      95       74870 :   for (const auto ind : make_range(_sampler.getNumberOfLocalRows()))
      96             :   {
      97       74495 :     const std::vector<Real> data = _sampler.getNextLocalRow();
      98      149340 :     for (const auto m : index_range(_model))
      99             :     {
     100       74845 :       if (_real_values[m] && _real_std[m])
     101       57210 :         (*_real_values[m])[ind] = _model[m]->evaluate(data, (*_real_std[m])[ind]);
     102       17635 :       else if (_real_values[m])
     103       17460 :         (*_real_values[m])[ind] = _model[m]->evaluate(data);
     104         175 :       else if (_vector_real_values[m] && _vector_real_std[m])
     105          75 :         _model[m]->evaluate(data, (*_vector_real_values[m])[ind], (*_vector_real_std[m])[ind]);
     106         100 :       else if (_vector_real_values[m])
     107         100 :         _model[m]->evaluate(data, (*_vector_real_values[m])[ind]);
     108             :     }
     109       74495 :   }
     110         375 : }

Generated by: LCOV version 1.14