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 62094 : ReporterData::ReporterData(MooseApp & moose_app) : _app(moose_app) {} 15 : 16 : void 17 232981 : ReporterData::copyValuesBack() 18 : { 19 480074 : for (const auto & name_context_pair : _context_ptrs) 20 247093 : name_context_pair.second->copyValuesBack(); 21 232981 : } 22 : 23 : void 24 3340 : ReporterData::restoreState(bool verbose) 25 : { 26 : // Table of reporter values that were and were not restored 27 6680 : VariadicTable<std::string, std::string, std::string> summary_table({"Name", "Type", "Restored?"}); 28 : 29 8369 : for (const auto & [rname, context] : _context_ptrs) 30 : { 31 5029 : const bool restored = context->restoreState(); 32 : 33 5029 : if (verbose) 34 270 : summary_table.addRow( 35 180 : rname.getCombinedName(), rname.specialTypeToName(), restored ? "YES" : "NO"); 36 : } 37 : 38 3340 : if (verbose && !_context_ptrs.empty()) 39 : { 40 18 : std::stringstream oss; 41 18 : oss << "Reporter Restoration Summary:\n"; 42 18 : summary_table.print(oss); 43 18 : mooseInfo(oss.str()); 44 18 : } 45 3340 : } 46 : 47 : void 48 521589 : ReporterData::finalize(const std::string & object_name) 49 : { 50 2406853 : for (auto & name_context_pair : _context_ptrs) 51 1885270 : if (name_context_pair.first.getObjectName() == object_name) 52 576926 : name_context_pair.second->finalize(); 53 521583 : } 54 : 55 : bool 56 4671469 : ReporterData::hasReporterValue(const ReporterName & reporter_name) const 57 : { 58 4671469 : return _context_ptrs.count(reporter_name); 59 : } 60 : 61 : std::set<ReporterName> 62 363651 : ReporterData::getReporterNames() const 63 : { 64 363651 : std::set<ReporterName> output; 65 867108 : for (const auto & name_context_pair : _context_ptrs) 66 503457 : output.insert(name_context_pair.second->name()); 67 363651 : return output; 68 0 : } 69 : 70 : std::set<std::string> 71 60183 : ReporterData::getPostprocessorNames() const 72 : { 73 60183 : std::set<std::string> output; 74 141665 : for (const auto & name_context_pair : _context_ptrs) 75 81482 : if (name_context_pair.first.isPostprocessor()) 76 50734 : output.insert(name_context_pair.first.getObjectName()); 77 60183 : 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 2275784 : ReporterData::getReporterContextBase(const ReporterName & reporter_name) const 131 : { 132 2275784 : if (!hasReporterValue(reporter_name)) 133 0 : mooseError("Unable to locate Reporter context with name: ", reporter_name); 134 2275784 : 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 1612943 : ReporterData::getReporterStateBase(const ReporterName & reporter_name) const 147 : { 148 1612943 : if (!hasReporterState(reporter_name)) 149 0 : mooseError("Unable to locate Reporter state with name: ", reporter_name); 150 1612943 : 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 59361 : ReporterData::check() const 163 : { 164 59361 : std::string missing; 165 140704 : for (const auto & name_state_pair : _states) 166 81343 : if (!hasReporterValue(name_state_pair.first)) 167 0 : missing += getReporterInfo(name_state_pair.first) + "\n"; 168 : 169 59361 : if (missing.size()) 170 0 : mooseError("The following Reporter(s) were not declared:\n\n", missing); 171 59361 : } 172 : 173 : RestartableDataValue & 174 1695563 : ReporterData::getRestartableDataHelper(std::unique_ptr<RestartableDataValue> data_ptr, 175 : bool declare) const 176 : { 177 5086689 : return _app.registerRestartableData(std::move(data_ptr), 0, !declare); 178 : } 179 : 180 : void 181 82764 : ReporterData::restoreReporterStateIfAvailable(RestartableDataValue & state) const 182 : { 183 82764 : _app.restoreDataIfAvailable(state, 0, {}); 184 82764 : } 185 : 186 : bool 187 15956 : ReporterData::hasReporterWithMode(const std::string & obj_name, const ReporterMode & mode) const 188 : { 189 112529 : for (const auto & name_context_pair : _context_ptrs) 190 159917 : if (name_context_pair.first.getObjectName() == obj_name && 191 62582 : name_context_pair.second->getProducerModeEnum() == mode) 192 762 : return true; 193 15194 : return false; 194 : } 195 : 196 : const ReporterProducerEnum & 197 104 : ReporterData::getReporterMode(const ReporterName & reporter_name) const 198 : { 199 104 : return getReporterContextBase(reporter_name).getProducerModeEnum(); 200 : } 201 : 202 : bool 203 4922831 : ReporterData::hasReporterState(const ReporterName & reporter_name) const 204 : { 205 4922831 : return _states.count(reporter_name); 206 : } 207 : 208 : std::string 209 72 : ReporterData::getReporterInfo(const ReporterStateBase & state, const ReporterContextBase * context) 210 : { 211 72 : std::stringstream oss; 212 : 213 72 : const auto & name = state.getReporterName(); 214 : 215 72 : if (name.isPostprocessor()) 216 18 : oss << "Postprocessor \"" << name.getObjectName() << "\":\n"; 217 : else 218 : { 219 108 : oss << name.specialTypeToName() << " \"" << name.getCombinedName() << "\":\n Type:\n " 220 54 : << state.valueType() << "\n"; 221 : } 222 72 : oss << " Producer:\n "; 223 72 : if (context) 224 : { 225 66 : oss << context->getProducer().type() << " \"" << context->getProducer().name() << "\""; 226 66 : oss << "\n Context type:\n " << context->contextType(); 227 : } 228 : else 229 6 : oss << "None"; 230 72 : oss << "\n Consumer(s):\n"; 231 72 : if (state.getConsumers().empty()) 232 57 : oss << " None\n"; 233 : else 234 30 : for (const auto & mode_object_pair : state.getConsumers()) 235 : { 236 15 : const ReporterMode mode = mode_object_pair.first; 237 15 : const MooseObject * object = mode_object_pair.second; 238 15 : oss << " " << object->typeAndName() << " (mode: " << mode << ")\n"; 239 15 : } 240 : 241 144 : return oss.str(); 242 72 : } 243 : 244 : std::string 245 72 : ReporterData::getReporterInfo(const ReporterName & reporter_name) const 246 : { 247 : return getReporterInfo(getReporterStateBase(reporter_name), 248 138 : hasReporterValue(reporter_name) ? &getReporterContextBase(reporter_name) 249 138 : : nullptr); 250 : } 251 : 252 : std::string 253 9 : ReporterData::getReporterInfo() const 254 : { 255 9 : std::string out = _states.empty() ? "No reporters were requested or declared." : ""; 256 72 : for (const auto & name : getReporterNames()) 257 72 : out += getReporterInfo(name) + "\n"; 258 9 : return out; 259 0 : }