LCOV - code coverage report
Current view: top level - src/auxscalarkernels - SurrogateModelScalarAux.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: f45d79 Lines: 40 42 95.2 %
Date: 2025-07-25 05:00:46 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 "SurrogateModelScalarAux.h"
      11             : 
      12             : registerMooseObject("StochasticToolsApp", SurrogateModelScalarAux);
      13             : 
      14             : InputParameters
      15          76 : SurrogateModelScalarAux::validParams()
      16             : {
      17          76 :   InputParameters params = AuxScalarKernel::validParams();
      18          76 :   params += SurrogateModelInterface::validParams();
      19          76 :   params.addClassDescription("Sets a value of a scalar variable based on a surrogate model.");
      20         152 :   params.addRequiredParam<UserObjectName>("model", "Name of surrogate models.");
      21         152 :   params.addRequiredParam<std::vector<std::string>>(
      22             :       "parameters",
      23             :       "Parameter values at which the surrogate is evaluated. Accepts scalar variables, "
      24             :       "postprocessors, and real numbers.");
      25          76 :   return params;
      26           0 : }
      27             : 
      28          44 : SurrogateModelScalarAux::SurrogateModelScalarAux(const InputParameters & parameters)
      29          44 :   : AuxScalarKernel(parameters), SurrogateModelInterface(this), _model(getSurrogateModel("model"))
      30             : {
      31          44 : }
      32             : 
      33             : void
      34          44 : SurrogateModelScalarAux::initialSetup()
      35             : {
      36         132 :   const auto parameter_names = getParam<std::vector<std::string>>("parameters");
      37          44 :   _n_params = parameter_names.size();
      38         132 :   for (unsigned int j = 0; j < _n_params; ++j)
      39             :   {
      40          88 :     const auto name = parameter_names[j];
      41             : 
      42             :     // name can be a postprocessor, scalar var, or real number
      43          88 :     if (_sc_fe_problem.hasScalarVariable(name))
      44             :     {
      45          22 :       auto & scalar_val = _sc_fe_problem.getScalarVariable(_tid, name).sln()[0];
      46          22 :       _scalar_var_params.push_back(&scalar_val);
      47          22 :       _scalar_var_indices.push_back(j);
      48             :     }
      49         132 :     else if (_sc_fe_problem.hasPostprocessorValueByName(name))
      50             :     {
      51          22 :       _pp_indices.push_back(j);
      52          44 :       _pp_params.push_back(&getPostprocessorValueByName(name));
      53             :     }
      54             :     else
      55             :     {
      56             :       // first check if the entry is a real
      57          44 :       std::stringstream ss(name);
      58             :       Real v;
      59             :       ss >> v;
      60             : 
      61          44 :       if (ss.fail())
      62           0 :         paramError("parameters",
      63             :                    "Parameter ",
      64             :                    name,
      65             :                    " is not a scalar variable, postprocessor, or real number");
      66             : 
      67          44 :       _real_params.push_back(v);
      68          44 :       _real_indices.push_back(j);
      69          44 :     }
      70             :   }
      71          44 :   _n_scalar = _scalar_var_indices.size();
      72          44 :   _n_pp = _pp_indices.size();
      73          44 :   _n_real = _real_indices.size();
      74          44 : }
      75             : 
      76             : Real
      77          32 : SurrogateModelScalarAux::computeValue()
      78             : {
      79          32 :   std::vector<Real> x(_n_params);
      80             :   // insert scalar variables
      81          48 :   for (unsigned int j = 0; j < _n_scalar; ++j)
      82          16 :     x[_scalar_var_indices[j]] = *_scalar_var_params[j];
      83             :   // insert postprocessors
      84          48 :   for (unsigned int j = 0; j < _n_pp; ++j)
      85          16 :     x[_pp_indices[j]] = *_pp_params[j];
      86             :   // insert real values
      87          64 :   for (unsigned int j = 0; j < _n_real; ++j)
      88          32 :     x[_real_indices[j]] = _real_params[j];
      89          64 :   return _model.evaluate(x);
      90             : }

Generated by: LCOV version 1.14