LCOV - code coverage report
Current view: top level - src/reporters - ParsedVectorVectorRealReductionReporter.C (source / functions) Hit Total Coverage
Test: idaholab/moose optimization: #31405 (292dce) with base fef103 Lines: 43 45 95.6 %
Date: 2025-09-04 07:54:57 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             : #include "ParsedVectorVectorRealReductionReporter.h"
      11             : #include <cstddef>
      12             : 
      13             : registerMooseObject("OptimizationApp", ParsedVectorVectorRealReductionReporter);
      14             : 
      15             : InputParameters
      16         274 : ParsedVectorVectorRealReductionReporter::validParams()
      17             : {
      18         274 :   InputParameters params = ParsedReporterBase::validParams();
      19         274 :   params.addClassDescription("Use a parsed function to iterate through a rows of a vector of "
      20             :                              "vector and reduce it to a vector.");
      21         548 :   params.addRequiredParam<ReporterName>("reporter_name",
      22             :                                         "Reporter name with vector of vectors to reduce.");
      23         548 :   params.addRequiredParam<Real>("initial_value", "Value to intialize the reduction with.");
      24             : 
      25             :   // reporter_symbols are the two symbols for reduction value and current value for the reduction
      26             :   // operation, these symbols are enforced in the constructor with a mooseError
      27        1096 :   params.set<std::vector<std::string>>("reporter_symbols") = {"reduction_value", "indexed_value"};
      28         274 :   params.suppressParameter<std::vector<std::string>>("reporter_symbols");
      29             : 
      30             :   // This reporter is for postprocessing optimization results and should be exectuted at the end of
      31             :   // execution
      32         274 :   params.set<ExecFlagEnum>("execute_on") = EXEC_TIMESTEP_END;
      33         274 :   return params;
      34         548 : }
      35             : 
      36         137 : ParsedVectorVectorRealReductionReporter::ParsedVectorVectorRealReductionReporter(
      37         137 :     const InputParameters & parameters)
      38             :   : ParsedReporterBase(parameters),
      39         137 :     _initial_value(getParam<Real>("initial_value")),
      40         274 :     _vec_of_vec_name(getParam<ReporterName>("reporter_name")),
      41         137 :     _output_reporter(
      42         411 :         declareValueByName<std::vector<Real>>(getParam<std::string>("name"), REPORTER_MODE_ROOT)),
      43         274 :     _reporter_data(getReporterValueByName<std::vector<std::vector<Real>>>(
      44         137 :         getParam<ReporterName>("reporter_name")))
      45             : {
      46             :   // parse function
      47         274 :   std::string function = getParam<std::string>("expression");
      48             :   // make sure the expression has the two required variables, vi and vplus
      49         274 :   if (function.find("reduction_value") == std::string::npos ||
      50         137 :       function.find("indexed_value") == std::string::npos)
      51           0 :     paramError(
      52             :         "expression",
      53             :         "Parsed function must contain the two symbols 'reduction_value' and 'indexed_value'.");
      54         137 : }
      55             : 
      56             : void
      57         543 : ParsedVectorVectorRealReductionReporter::finalize()
      58             : {
      59         543 :   std::size_t ncols = _reporter_data.size();
      60         543 :   std::size_t nrows = 0;
      61         543 :   if (!_reporter_data.empty())
      62         543 :     nrows = _reporter_data[0].size();
      63             : 
      64        2287 :   for (auto & reporter_vector : _reporter_data)
      65             :   {
      66        1746 :     if (reporter_vector.size() != nrows)
      67           2 :       mooseError("Every vector in 'reporter_name=",
      68           2 :                  _vec_of_vec_name,
      69             :                  "' must be the same size.",
      70             :                  "\nFirst Vector size = ",
      71             :                  nrows,
      72             :                  "\nCurrent Vector size = ",
      73           2 :                  reporter_vector.size());
      74             :   }
      75             : 
      76         541 :   _output_reporter.clear();
      77         541 :   _output_reporter.resize(nrows, _initial_value);
      78        1506 :   for (const auto i_row : make_range(nrows))
      79             :   {
      80         965 :     Real reduction = _initial_value;
      81        3977 :     for (const auto j_col : make_range(ncols))
      82             :     {
      83        3012 :       _func_params[0] = reduction;
      84        3012 :       _func_params[1] = _reporter_data[j_col][i_row];
      85             : 
      86        3012 :       if (_use_t)
      87           0 :         _func_params[2] = _t;
      88             : 
      89        6024 :       reduction = evaluate(_func_F);
      90             :     }
      91         965 :     _output_reporter[i_row] = reduction;
      92             :   }
      93         541 : }

Generated by: LCOV version 1.14