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 62498 : ReporterData::ReporterData(MooseApp & moose_app) : _app(moose_app) {} 15 : 16 : void 17 243187 : ReporterData::copyValuesBack() 18 : { 19 494693 : for (const auto & name_context_pair : _context_ptrs) 20 251506 : name_context_pair.second->copyValuesBack(); 21 243187 : } 22 : 23 : void 24 5304 : ReporterData::restoreState(bool verbose) 25 : { 26 : // Table of reporter values that were and were not restored 27 21216 : VariadicTable<std::string, std::string, std::string> summary_table({"Name", "Type", "Restored?"}); 28 : 29 16254 : for (const auto & [rname, context] : _context_ptrs) 30 : { 31 10950 : const bool restored = context->restoreState(); 32 : 33 10950 : if (verbose) 34 7857 : summary_table.addRow( 35 5238 : rname.getCombinedName(), rname.specialTypeToName(), restored ? "YES" : "NO"); 36 : } 37 : 38 5304 : if (verbose && !_context_ptrs.empty()) 39 : { 40 391 : std::stringstream oss; 41 391 : oss << "Reporter Restoration Summary:\n"; 42 391 : summary_table.print(oss); 43 391 : mooseInfo(oss.str()); 44 391 : } 45 15912 : } 46 : 47 : void 48 539108 : ReporterData::finalize(const std::string & object_name) 49 : { 50 2503892 : for (auto & name_context_pair : _context_ptrs) 51 1964792 : if (name_context_pair.first.getObjectName() == object_name) 52 595421 : name_context_pair.second->finalize(); 53 539100 : } 54 : 55 : bool 56 4857500 : ReporterData::hasReporterValue(const ReporterName & reporter_name) const 57 : { 58 4857500 : return _context_ptrs.count(reporter_name); 59 : } 60 : 61 : std::set<ReporterName> 62 373613 : ReporterData::getReporterNames() const 63 : { 64 373613 : std::set<ReporterName> output; 65 883220 : for (const auto & name_context_pair : _context_ptrs) 66 509607 : output.insert(name_context_pair.second->name()); 67 373613 : return output; 68 0 : } 69 : 70 : std::set<std::string> 71 60455 : ReporterData::getPostprocessorNames() const 72 : { 73 60455 : std::set<std::string> output; 74 143095 : for (const auto & name_context_pair : _context_ptrs) 75 82640 : if (name_context_pair.first.isPostprocessor()) 76 54256 : output.insert(name_context_pair.first.getObjectName()); 77 60455 : return output; 78 0 : } 79 : 80 : DenseVector<Real> 81 1244 : ReporterData::getAllRealReporterValues() const 82 : { 83 1244 : DenseVector<Real> all_values; 84 : 85 1244 : std::vector<Real> & output = all_values.get_values(); 86 : 87 8022 : for (const auto & name_context_pair : _context_ptrs) 88 : { 89 6778 : const ReporterName & rname = name_context_pair.first; 90 : 91 6778 : if (hasReporterValue<Real>(rname)) 92 5182 : output.push_back(getReporterValue<Real>(rname.getCombinedName())); 93 : 94 6778 : if (hasReporterValue<std::vector<Real>>(rname)) 95 : { 96 1596 : const auto & vec = getReporterValue<std::vector<Real>>(rname.getCombinedName()); 97 3984 : for (const auto & v : vec) 98 2388 : output.push_back(v); 99 : } 100 : } 101 : 102 1244 : return all_values; 103 0 : } 104 : 105 : std::vector<std::string> 106 4 : ReporterData::getAllRealReporterFullNames() const 107 : { 108 4 : std::vector<std::string> output; 109 : 110 16 : for (const auto & name_context_pair : _context_ptrs) 111 : { 112 12 : const ReporterName & rname = name_context_pair.first; 113 : 114 12 : if (hasReporterValue<Real>(rname)) 115 12 : output.push_back(rname.getCombinedName()); 116 : 117 12 : 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 4 : return output; 127 0 : } 128 : 129 : const ReporterContextBase & 130 2365456 : ReporterData::getReporterContextBase(const ReporterName & reporter_name) const 131 : { 132 2365456 : if (!hasReporterValue(reporter_name)) 133 0 : mooseError("Unable to locate Reporter context with name: ", reporter_name); 134 2365456 : return *_context_ptrs.at(reporter_name); 135 : } 136 : 137 : ReporterContextBase & 138 646 : ReporterData::getReporterContextBase(const ReporterName & reporter_name) 139 : { 140 646 : if (!hasReporterValue(reporter_name)) 141 0 : mooseError("Unable to locate Reporter context with name: ", reporter_name); 142 646 : return *_context_ptrs.at(reporter_name); 143 : } 144 : 145 : const ReporterStateBase & 146 1689972 : ReporterData::getReporterStateBase(const ReporterName & reporter_name) const 147 : { 148 1689972 : if (!hasReporterState(reporter_name)) 149 0 : mooseError("Unable to locate Reporter state with name: ", reporter_name); 150 1689972 : return *_states.at(reporter_name); 151 : } 152 : 153 : ReporterStateBase & 154 1732 : ReporterData::getReporterStateBase(const ReporterName & reporter_name) 155 : { 156 1732 : if (!hasReporterState(reporter_name)) 157 0 : mooseError("Unable to locate Reporter state with name: ", reporter_name); 158 1732 : return *_states.at(reporter_name); 159 : } 160 : 161 : void 162 59487 : ReporterData::check() const 163 : { 164 59487 : std::string missing; 165 141883 : for (const auto & name_state_pair : _states) 166 82396 : if (!hasReporterValue(name_state_pair.first)) 167 0 : missing += getReporterInfo(name_state_pair.first) + "\n"; 168 : 169 59487 : if (missing.size()) 170 0 : mooseError("The following Reporter(s) were not declared:\n\n", missing); 171 59487 : } 172 : 173 : RestartableDataValue & 174 1773885 : ReporterData::getRestartableDataHelper(std::unique_ptr<RestartableDataValue> data_ptr, 175 : bool declare) const 176 : { 177 1773885 : return _app.registerRestartableData(std::move(data_ptr), 0, !declare); 178 : } 179 : 180 : bool 181 14442 : ReporterData::hasReporterWithMode(const std::string & obj_name, const ReporterMode & mode) const 182 : { 183 100742 : for (const auto & name_context_pair : _context_ptrs) 184 143182 : if (name_context_pair.first.getObjectName() == obj_name && 185 56100 : name_context_pair.second->getProducerModeEnum() == mode) 186 782 : return true; 187 13660 : return false; 188 : } 189 : 190 : const ReporterProducerEnum & 191 112 : ReporterData::getReporterMode(const ReporterName & reporter_name) const 192 : { 193 112 : return getReporterContextBase(reporter_name).getProducerModeEnum(); 194 : } 195 : 196 : bool 197 5155413 : ReporterData::hasReporterState(const ReporterName & reporter_name) const 198 : { 199 5155413 : return _states.count(reporter_name); 200 : } 201 : 202 : std::string 203 82 : ReporterData::getReporterInfo(const ReporterStateBase & state, const ReporterContextBase * context) 204 : { 205 82 : std::stringstream oss; 206 : 207 82 : const auto & name = state.getReporterName(); 208 : 209 82 : if (name.isPostprocessor()) 210 20 : oss << "Postprocessor \"" << name.getObjectName() << "\":\n"; 211 : else 212 : { 213 124 : oss << name.specialTypeToName() << " \"" << name.getCombinedName() << "\":\n Type:\n " 214 62 : << state.valueType() << "\n"; 215 : } 216 82 : oss << " Producer:\n "; 217 82 : if (context) 218 : { 219 74 : oss << context->getProducer().type() << " \"" << context->getProducer().name() << "\""; 220 74 : oss << "\n Context type:\n " << context->contextType(); 221 : } 222 : else 223 8 : oss << "None"; 224 82 : oss << "\n Consumer(s):\n"; 225 82 : if (state.getConsumers().empty()) 226 64 : oss << " None\n"; 227 : else 228 36 : for (const auto & mode_object_pair : state.getConsumers()) 229 : { 230 18 : const ReporterMode mode = mode_object_pair.first; 231 18 : const MooseObject * object = mode_object_pair.second; 232 18 : oss << " " << object->typeAndName() << " (mode: " << mode << ")\n"; 233 18 : } 234 : 235 164 : return oss.str(); 236 82 : } 237 : 238 : std::string 239 82 : ReporterData::getReporterInfo(const ReporterName & reporter_name) const 240 : { 241 : return getReporterInfo(getReporterStateBase(reporter_name), 242 156 : hasReporterValue(reporter_name) ? &getReporterContextBase(reporter_name) 243 156 : : nullptr); 244 : } 245 : 246 : std::string 247 10 : ReporterData::getReporterInfo() const 248 : { 249 10 : std::string out = _states.empty() ? "No reporters were requested or declared." : ""; 250 80 : for (const auto & name : getReporterNames()) 251 80 : out += getReporterInfo(name) + "\n"; 252 10 : return out; 253 0 : }