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 57974 : ReporterData::ReporterData(MooseApp & moose_app) : _app(moose_app) {} 15 : 16 : void 17 223480 : ReporterData::copyValuesBack() 18 : { 19 453087 : for (const auto & name_context_pair : _context_ptrs) 20 229607 : name_context_pair.second->copyValuesBack(); 21 223480 : } 22 : 23 : void 24 4214 : ReporterData::restoreState(bool verbose) 25 : { 26 : // Table of reporter values that were and were not restored 27 16856 : VariadicTable<std::string, std::string, std::string> summary_table({"Name", "Type", "Restored?"}); 28 : 29 13832 : for (const auto & [rname, context] : _context_ptrs) 30 : { 31 9618 : const bool restored = context->restoreState(); 32 : 33 9618 : if (verbose) 34 7356 : summary_table.addRow( 35 4904 : rname.getCombinedName(), rname.specialTypeToName(), restored ? "YES" : "NO"); 36 : } 37 : 38 4214 : if (verbose && !_context_ptrs.empty()) 39 : { 40 356 : std::stringstream oss; 41 356 : oss << "Reporter Restoration Summary:\n"; 42 356 : summary_table.print(oss); 43 356 : mooseInfo(oss.str()); 44 356 : } 45 12642 : } 46 : 47 : void 48 467297 : ReporterData::finalize(const std::string & object_name) 49 : { 50 2163980 : for (auto & name_context_pair : _context_ptrs) 51 1696691 : if (name_context_pair.first.getObjectName() == object_name) 52 518744 : name_context_pair.second->finalize(); 53 467289 : } 54 : 55 : bool 56 3699096 : ReporterData::hasReporterValue(const ReporterName & reporter_name) const 57 : { 58 3699096 : return _context_ptrs.count(reporter_name); 59 : } 60 : 61 : std::set<ReporterName> 62 343822 : ReporterData::getReporterNames() const 63 : { 64 343822 : std::set<ReporterName> output; 65 812378 : for (const auto & name_context_pair : _context_ptrs) 66 468556 : output.insert(name_context_pair.second->name()); 67 343822 : return output; 68 0 : } 69 : 70 : std::set<std::string> 71 55939 : ReporterData::getPostprocessorNames() const 72 : { 73 55939 : std::set<std::string> output; 74 132212 : for (const auto & name_context_pair : _context_ptrs) 75 76273 : if (name_context_pair.first.isPostprocessor()) 76 50037 : output.insert(name_context_pair.first.getObjectName()); 77 55939 : return output; 78 0 : } 79 : 80 : DenseVector<Real> 81 1144 : ReporterData::getAllRealReporterValues() const 82 : { 83 1144 : DenseVector<Real> all_values; 84 : 85 1144 : std::vector<Real> & output = all_values.get_values(); 86 : 87 7400 : for (const auto & name_context_pair : _context_ptrs) 88 : { 89 6256 : const ReporterName & rname = name_context_pair.first; 90 : 91 6256 : if (hasReporterValue<Real>(rname)) 92 4792 : output.push_back(getReporterValue<Real>(rname.getCombinedName())); 93 : 94 6256 : 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 1144 : 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 1791222 : ReporterData::getReporterContextBase(const ReporterName & reporter_name) const 131 : { 132 1791222 : if (!hasReporterValue(reporter_name)) 133 0 : mooseError("Unable to locate Reporter context with name: ", reporter_name); 134 1791222 : return *_context_ptrs.at(reporter_name); 135 : } 136 : 137 : ReporterContextBase & 138 585 : ReporterData::getReporterContextBase(const ReporterName & reporter_name) 139 : { 140 585 : if (!hasReporterValue(reporter_name)) 141 0 : mooseError("Unable to locate Reporter context with name: ", reporter_name); 142 585 : return *_context_ptrs.at(reporter_name); 143 : } 144 : 145 : const ReporterStateBase & 146 1170044 : ReporterData::getReporterStateBase(const ReporterName & reporter_name) const 147 : { 148 1170044 : if (!hasReporterState(reporter_name)) 149 0 : mooseError("Unable to locate Reporter state with name: ", reporter_name); 150 1170044 : return *_states.at(reporter_name); 151 : } 152 : 153 : ReporterStateBase & 154 1567 : ReporterData::getReporterStateBase(const ReporterName & reporter_name) 155 : { 156 1567 : if (!hasReporterState(reporter_name)) 157 0 : mooseError("Unable to locate Reporter state with name: ", reporter_name); 158 1567 : return *_states.at(reporter_name); 159 : } 160 : 161 : void 162 54975 : ReporterData::check() const 163 : { 164 54975 : std::string missing; 165 130928 : for (const auto & name_state_pair : _states) 166 75953 : if (!hasReporterValue(name_state_pair.first)) 167 0 : missing += getReporterInfo(name_state_pair.first) + "\n"; 168 : 169 54975 : if (missing.size()) 170 0 : mooseError("The following Reporter(s) were not declared:\n\n", missing); 171 54975 : } 172 : 173 : RestartableDataValue & 174 1247515 : ReporterData::getRestartableDataHelper(std::unique_ptr<RestartableDataValue> data_ptr, 175 : bool declare) const 176 : { 177 1247515 : return _app.registerRestartableData(std::move(data_ptr), 0, !declare); 178 : } 179 : 180 : bool 181 13244 : ReporterData::hasReporterWithMode(const std::string & obj_name, const ReporterMode & mode) const 182 : { 183 91833 : for (const auto & name_context_pair : _context_ptrs) 184 130560 : if (name_context_pair.first.getObjectName() == obj_name && 185 51242 : name_context_pair.second->getProducerModeEnum() == mode) 186 729 : return true; 187 12515 : 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 3589028 : ReporterData::hasReporterState(const ReporterName & reporter_name) const 198 : { 199 3589028 : 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 : }