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 61654 : ReporterData::ReporterData(MooseApp & moose_app) : _app(moose_app) {} 15 : 16 : void 17 229029 : ReporterData::copyValuesBack() 18 : { 19 466596 : for (const auto & name_context_pair : _context_ptrs) 20 237567 : name_context_pair.second->copyValuesBack(); 21 229029 : } 22 : 23 : void 24 3299 : ReporterData::restoreState(bool verbose) 25 : { 26 : // Table of reporter values that were and were not restored 27 6598 : VariadicTable<std::string, std::string, std::string> summary_table({"Name", "Type", "Restored?"}); 28 : 29 8328 : for (const auto & [rname, context] : _context_ptrs) 30 : { 31 5029 : const bool restored = context->restoreState(); 32 : 33 5029 : if (verbose) 34 6063 : summary_table.addRow( 35 4042 : rname.getCombinedName(), rname.specialTypeToName(), restored ? "YES" : "NO"); 36 : } 37 : 38 3299 : if (verbose && !_context_ptrs.empty()) 39 : { 40 305 : std::stringstream oss; 41 305 : oss << "Reporter Restoration Summary:\n"; 42 305 : summary_table.print(oss); 43 305 : mooseInfo(oss.str()); 44 305 : } 45 3299 : } 46 : 47 : void 48 512584 : ReporterData::finalize(const std::string & object_name) 49 : { 50 2374946 : for (auto & name_context_pair : _context_ptrs) 51 1862368 : if (name_context_pair.first.getObjectName() == object_name) 52 567877 : name_context_pair.second->finalize(); 53 512578 : } 54 : 55 : bool 56 4575153 : ReporterData::hasReporterValue(const ReporterName & reporter_name) const 57 : { 58 4575153 : return _context_ptrs.count(reporter_name); 59 : } 60 : 61 : std::set<ReporterName> 62 361771 : ReporterData::getReporterNames() const 63 : { 64 361771 : std::set<ReporterName> output; 65 861249 : for (const auto & name_context_pair : _context_ptrs) 66 499478 : output.insert(name_context_pair.second->name()); 67 361771 : return output; 68 0 : } 69 : 70 : std::set<std::string> 71 59759 : ReporterData::getPostprocessorNames() const 72 : { 73 59759 : std::set<std::string> output; 74 140249 : for (const auto & name_context_pair : _context_ptrs) 75 80490 : if (name_context_pair.first.isPostprocessor()) 76 49846 : output.insert(name_context_pair.first.getObjectName()); 77 59759 : 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 2228356 : ReporterData::getReporterContextBase(const ReporterName & reporter_name) const 131 : { 132 2228356 : if (!hasReporterValue(reporter_name)) 133 0 : mooseError("Unable to locate Reporter context with name: ", reporter_name); 134 2228356 : 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 1573630 : ReporterData::getReporterStateBase(const ReporterName & reporter_name) const 147 : { 148 1573630 : if (!hasReporterState(reporter_name)) 149 0 : mooseError("Unable to locate Reporter state with name: ", reporter_name); 150 1573630 : 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 58937 : ReporterData::check() const 163 : { 164 58937 : std::string missing; 165 139288 : for (const auto & name_state_pair : _states) 166 80351 : if (!hasReporterValue(name_state_pair.first)) 167 0 : missing += getReporterInfo(name_state_pair.first) + "\n"; 168 : 169 58937 : if (missing.size()) 170 0 : mooseError("The following Reporter(s) were not declared:\n\n", missing); 171 58937 : } 172 : 173 : RestartableDataValue & 174 1655258 : ReporterData::getRestartableDataHelper(std::unique_ptr<RestartableDataValue> data_ptr, 175 : bool declare) const 176 : { 177 4965774 : 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 4803900 : ReporterData::hasReporterState(const ReporterName & reporter_name) const 198 : { 199 4803900 : 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 : }