LCOV - code coverage report
Current view: top level - src/reporters - ReporterData.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #32971 (54bef8) with base c6cf66 Lines: 117 133 88.0 %
Date: 2026-05-29 20:35:17 Functions: 21 21 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 "ReporterData.h"
      11             : #include "MooseApp.h"
      12             : #include "VariadicTable.h"
      13             : 
      14       61643 : ReporterData::ReporterData(MooseApp & moose_app) : _app(moose_app) {}
      15             : 
      16             : void
      17      229015 : ReporterData::copyValuesBack()
      18             : {
      19      466571 :   for (const auto & name_context_pair : _context_ptrs)
      20      237556 :     name_context_pair.second->copyValuesBack();
      21      229015 : }
      22             : 
      23             : void
      24        3295 : ReporterData::restoreState(bool verbose)
      25             : {
      26             :   // Table of reporter values that were and were not restored
      27        6590 :   VariadicTable<std::string, std::string, std::string> summary_table({"Name", "Type", "Restored?"});
      28             : 
      29        8296 :   for (const auto & [rname, context] : _context_ptrs)
      30             :   {
      31        5001 :     const bool restored = context->restoreState();
      32             : 
      33        5001 :     if (verbose)
      34        5979 :       summary_table.addRow(
      35        3986 :           rname.getCombinedName(), rname.specialTypeToName(), restored ? "YES" : "NO");
      36             :   }
      37             : 
      38        3295 :   if (verbose && !_context_ptrs.empty())
      39             :   {
      40         301 :     std::stringstream oss;
      41         301 :     oss << "Reporter Restoration Summary:\n";
      42         301 :     summary_table.print(oss);
      43         301 :     mooseInfo(oss.str());
      44         301 :   }
      45        3295 : }
      46             : 
      47             : void
      48      512559 : ReporterData::finalize(const std::string & object_name)
      49             : {
      50     2374842 :   for (auto & name_context_pair : _context_ptrs)
      51     1862289 :     if (name_context_pair.first.getObjectName() == object_name)
      52      567852 :       name_context_pair.second->finalize();
      53      512553 : }
      54             : 
      55             : bool
      56     4574872 : ReporterData::hasReporterValue(const ReporterName & reporter_name) const
      57             : {
      58     4574872 :   return _context_ptrs.count(reporter_name);
      59             : }
      60             : 
      61             : std::set<ReporterName>
      62      361724 : ReporterData::getReporterNames() const
      63             : {
      64      361724 :   std::set<ReporterName> output;
      65      861158 :   for (const auto & name_context_pair : _context_ptrs)
      66      499434 :     output.insert(name_context_pair.second->name());
      67      361724 :   return output;
      68           0 : }
      69             : 
      70             : std::set<std::string>
      71       59748 : ReporterData::getPostprocessorNames() const
      72             : {
      73       59748 :   std::set<std::string> output;
      74      140227 :   for (const auto & name_context_pair : _context_ptrs)
      75       80479 :     if (name_context_pair.first.isPostprocessor())
      76       49835 :       output.insert(name_context_pair.first.getObjectName());
      77       59748 :   return output;
      78           0 : }
      79             : 
      80             : DenseVector<Real>
      81        1115 : ReporterData::getAllRealReporterValues() const
      82             : {
      83        1115 :   DenseVector<Real> all_values;
      84             : 
      85        1115 :   std::vector<Real> & output = all_values.get_values();
      86             : 
      87        7153 :   for (const auto & name_context_pair : _context_ptrs)
      88             :   {
      89        6038 :     const ReporterName & rname = name_context_pair.first;
      90             : 
      91        6038 :     if (hasReporterValue<Real>(rname))
      92        4574 :       output.push_back(getReporterValue<Real>(rname.getCombinedName()));
      93             : 
      94        6038 :     if (hasReporterValue<std::vector<Real>>(rname))
      95             :     {
      96        1464 :       const auto & vec = getReporterValue<std::vector<Real>>(rname.getCombinedName());
      97        3720 :       for (const auto & v : vec)
      98        2256 :         output.push_back(v);
      99             :     }
     100             :   }
     101             : 
     102        1115 :   return all_values;
     103           0 : }
     104             : 
     105             : std::vector<std::string>
     106           3 : ReporterData::getAllRealReporterFullNames() const
     107             : {
     108           3 :   std::vector<std::string> output;
     109             : 
     110          12 :   for (const auto & name_context_pair : _context_ptrs)
     111             :   {
     112           9 :     const ReporterName & rname = name_context_pair.first;
     113             : 
     114           9 :     if (hasReporterValue<Real>(rname))
     115           9 :       output.push_back(rname.getCombinedName());
     116             : 
     117           9 :     if (hasReporterValue<std::vector<Real>>(rname))
     118             :     {
     119           0 :       auto pname = rname.getCombinedName();
     120           0 :       const auto & vec = getReporterValue<std::vector<Real>>(pname);
     121           0 :       for (unsigned int i = 0; i < vec.size(); ++i)
     122           0 :         output.push_back(pname + "/" + std::to_string(i));
     123           0 :     }
     124             :   }
     125             : 
     126           3 :   return output;
     127           0 : }
     128             : 
     129             : const ReporterContextBase &
     130     2228221 : ReporterData::getReporterContextBase(const ReporterName & reporter_name) const
     131             : {
     132     2228221 :   if (!hasReporterValue(reporter_name))
     133           0 :     mooseError("Unable to locate Reporter context with name: ", reporter_name);
     134     2228221 :   return *_context_ptrs.at(reporter_name);
     135             : }
     136             : 
     137             : ReporterContextBase &
     138         575 : ReporterData::getReporterContextBase(const ReporterName & reporter_name)
     139             : {
     140         575 :   if (!hasReporterValue(reporter_name))
     141           0 :     mooseError("Unable to locate Reporter context with name: ", reporter_name);
     142         575 :   return *_context_ptrs.at(reporter_name);
     143             : }
     144             : 
     145             : const ReporterStateBase &
     146     1573495 : ReporterData::getReporterStateBase(const ReporterName & reporter_name) const
     147             : {
     148     1573495 :   if (!hasReporterState(reporter_name))
     149           0 :     mooseError("Unable to locate Reporter state with name: ", reporter_name);
     150     1573495 :   return *_states.at(reporter_name);
     151             : }
     152             : 
     153             : ReporterStateBase &
     154        1517 : ReporterData::getReporterStateBase(const ReporterName & reporter_name)
     155             : {
     156        1517 :   if (!hasReporterState(reporter_name))
     157           0 :     mooseError("Unable to locate Reporter state with name: ", reporter_name);
     158        1517 :   return *_states.at(reporter_name);
     159             : }
     160             : 
     161             : void
     162       58926 : ReporterData::check() const
     163             : {
     164       58926 :   std::string missing;
     165      139266 :   for (const auto & name_state_pair : _states)
     166       80340 :     if (!hasReporterValue(name_state_pair.first))
     167           0 :       missing += getReporterInfo(name_state_pair.first) + "\n";
     168             : 
     169       58926 :   if (missing.size())
     170           0 :     mooseError("The following Reporter(s) were not declared:\n\n", missing);
     171       58926 : }
     172             : 
     173             : RestartableDataValue &
     174     1655112 : ReporterData::getRestartableDataHelper(std::unique_ptr<RestartableDataValue> data_ptr,
     175             :                                        bool declare) const
     176             : {
     177     4965336 :   return _app.registerRestartableData(std::move(data_ptr), 0, !declare);
     178             : }
     179             : 
     180             : bool
     181       15861 : ReporterData::hasReporterWithMode(const std::string & obj_name, const ReporterMode & mode) const
     182             : {
     183      112023 :   for (const auto & name_context_pair : _context_ptrs)
     184      159143 :     if (name_context_pair.first.getObjectName() == obj_name &&
     185       62219 :         name_context_pair.second->getProducerModeEnum() == mode)
     186         762 :       return true;
     187       15099 :   return false;
     188             : }
     189             : 
     190             : const ReporterProducerEnum &
     191         104 : ReporterData::getReporterMode(const ReporterName & reporter_name) const
     192             : {
     193         104 :   return getReporterContextBase(reporter_name).getProducerModeEnum();
     194             : }
     195             : 
     196             : bool
     197     4803484 : ReporterData::hasReporterState(const ReporterName & reporter_name) const
     198             : {
     199     4803484 :   return _states.count(reporter_name);
     200             : }
     201             : 
     202             : std::string
     203          72 : ReporterData::getReporterInfo(const ReporterStateBase & state, const ReporterContextBase * context)
     204             : {
     205          72 :   std::stringstream oss;
     206             : 
     207          72 :   const auto & name = state.getReporterName();
     208             : 
     209          72 :   if (name.isPostprocessor())
     210          18 :     oss << "Postprocessor \"" << name.getObjectName() << "\":\n";
     211             :   else
     212             :   {
     213         108 :     oss << name.specialTypeToName() << " \"" << name.getCombinedName() << "\":\n  Type:\n    "
     214          54 :         << state.valueType() << "\n";
     215             :   }
     216          72 :   oss << "  Producer:\n    ";
     217          72 :   if (context)
     218             :   {
     219          66 :     oss << context->getProducer().type() << " \"" << context->getProducer().name() << "\"";
     220          66 :     oss << "\n  Context type:\n    " << context->contextType();
     221             :   }
     222             :   else
     223           6 :     oss << "None";
     224          72 :   oss << "\n  Consumer(s):\n";
     225          72 :   if (state.getConsumers().empty())
     226          57 :     oss << "    None\n";
     227             :   else
     228          30 :     for (const auto & mode_object_pair : state.getConsumers())
     229             :     {
     230          15 :       const ReporterMode mode = mode_object_pair.first;
     231          15 :       const MooseObject * object = mode_object_pair.second;
     232          15 :       oss << "    " << object->typeAndName() << " (mode: " << mode << ")\n";
     233          15 :     }
     234             : 
     235         144 :   return oss.str();
     236          72 : }
     237             : 
     238             : std::string
     239          72 : ReporterData::getReporterInfo(const ReporterName & reporter_name) const
     240             : {
     241             :   return getReporterInfo(getReporterStateBase(reporter_name),
     242         138 :                          hasReporterValue(reporter_name) ? &getReporterContextBase(reporter_name)
     243         138 :                                                          : nullptr);
     244             : }
     245             : 
     246             : std::string
     247           9 : ReporterData::getReporterInfo() const
     248             : {
     249           9 :   std::string out = _states.empty() ? "No reporters were requested or declared." : "";
     250          72 :   for (const auto & name : getReporterNames())
     251          72 :     out += getReporterInfo(name) + "\n";
     252           9 :   return out;
     253           0 : }

Generated by: LCOV version 1.14