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 "PostprocessorInterface.h"
11 :
12 : #include "FEProblem.h"
13 : #include "MooseObject.h"
14 :
15 : InputParameters
16 497214 : PostprocessorInterface::validParams()
17 : {
18 497214 : return emptyInputParameters();
19 : }
20 :
21 1130159 : PostprocessorInterface::PostprocessorInterface(const MooseObject * moose_object)
22 1130159 : : _ppi_moose_object(*moose_object),
23 2260318 : _ppi_params(_ppi_moose_object.parameters()),
24 1130159 : _ppi_feproblem(*_ppi_params.getCheckedPointerParam<FEProblemBase *>("_fe_problem_base"))
25 : {
26 1130159 : }
27 :
28 0 : PostprocessorInterface::PostprocessorInterface(const FEProblemBase * problem)
29 0 : : _ppi_moose_object(*problem),
30 0 : _ppi_params(_ppi_moose_object.parameters()),
31 0 : _ppi_feproblem(*problem)
32 : {
33 0 : }
34 :
35 : const PostprocessorValue &
36 20711 : PostprocessorInterface::getPostprocessorValue(const std::string & param_name,
37 : const unsigned int index /* = 0 */) const
38 : {
39 20711 : return getPostprocessorValueInternal(param_name, index, /* t_index = */ 0);
40 : }
41 :
42 : const PostprocessorValue &
43 555 : PostprocessorInterface::getPostprocessorValueOld(const std::string & param_name,
44 : const unsigned int index /* = 0 */) const
45 : {
46 555 : return getPostprocessorValueInternal(param_name, index, /* t_index = */ 1);
47 : }
48 :
49 : const PostprocessorValue &
50 10 : PostprocessorInterface::getPostprocessorValueOlder(const std::string & param_name,
51 : const unsigned int index /* = 0 */) const
52 : {
53 10 : return getPostprocessorValueInternal(param_name, index, /* t_index = */ 2);
54 : }
55 :
56 : const PostprocessorValue &
57 45769 : PostprocessorInterface::getPostprocessorValueByName(const PostprocessorName & name) const
58 : {
59 45769 : return getPostprocessorValueByNameInternal(name, /* t_index = */ 0);
60 : }
61 :
62 : const PostprocessorValue &
63 498 : PostprocessorInterface::getPostprocessorValueOldByName(const PostprocessorName & name) const
64 : {
65 498 : return getPostprocessorValueByNameInternal(name, /* t_index = */ 1);
66 : }
67 :
68 : const PostprocessorValue &
69 296 : PostprocessorInterface::getPostprocessorValueOlderByName(const PostprocessorName & name) const
70 : {
71 296 : return getPostprocessorValueByNameInternal(name, /* t_index = */ 2);
72 : }
73 :
74 : bool
75 378 : PostprocessorInterface::isDefaultPostprocessorValue(const std::string & param_name,
76 : const unsigned int index /* = 0 */) const
77 : {
78 378 : return isDefaultPostprocessorValueByName(getPostprocessorNameInternal(param_name, index));
79 : }
80 :
81 : bool
82 21642 : PostprocessorInterface::isDefaultPostprocessorValueByName(const PostprocessorName & name) const
83 : {
84 : // We do allow actual Postprocessor objects to have names that will succeed in being
85 : // represented as a double... so if the name does actually exist as a PP, it's not a default
86 43284 : if (_ppi_feproblem.getReporterData().hasReporterValue<PostprocessorValue>(
87 43284 : PostprocessorReporterName(name)))
88 5825 : return false;
89 :
90 15817 : std::istringstream ss(name);
91 15817 : Real real_value = -std::numeric_limits<Real>::max();
92 15817 : return (ss >> real_value && ss.eof());
93 15817 : }
94 :
95 : PostprocessorValue
96 14606 : PostprocessorInterface::getDefaultPostprocessorValueByName(const PostprocessorName & name) const
97 : {
98 : mooseAssert(isDefaultPostprocessorValueByName(name), "Not a default value");
99 :
100 14606 : Real real_value = -std::numeric_limits<Real>::max();
101 14606 : std::istringstream ss(name);
102 14606 : ss >> real_value;
103 14606 : return real_value;
104 14606 : }
105 :
106 : bool
107 4 : PostprocessorInterface::hasPostprocessor(const std::string & param_name,
108 : const unsigned int index /* = 0 */) const
109 : {
110 4 : if (!postprocessorsAdded())
111 4 : _ppi_moose_object.mooseError(
112 : "Cannot call hasPostprocessor() until all Postprocessors have been constructed.");
113 :
114 0 : return hasPostprocessorByName(getPostprocessorNameInternal(param_name, index));
115 : }
116 :
117 : bool
118 70926 : PostprocessorInterface::hasPostprocessorByName(const PostprocessorName & name) const
119 : {
120 70926 : if (!postprocessorsAdded())
121 4 : _ppi_moose_object.mooseError(
122 : "Cannot call hasPostprocessorByName() until all Postprocessors have been constructed.");
123 :
124 141844 : return _ppi_feproblem.getReporterData().hasReporterValue<PostprocessorValue>(
125 141844 : PostprocessorReporterName(name));
126 : }
127 :
128 : std::size_t
129 1042 : PostprocessorInterface::coupledPostprocessors(const std::string & param_name) const
130 : {
131 1042 : checkParam(param_name);
132 :
133 1042 : if (_ppi_params.isType<PostprocessorName>(param_name))
134 0 : return 1;
135 1042 : return _ppi_params.get<std::vector<PostprocessorName>>(param_name).size();
136 : }
137 :
138 : void
139 22700 : PostprocessorInterface::checkParam(
140 : const std::string & param_name,
141 : const unsigned int index /* = std::numeric_limits<unsigned int>::max() */) const
142 : {
143 22700 : const bool check_index = index != std::numeric_limits<unsigned int>::max();
144 :
145 22700 : if (!_ppi_params.isParamValid(param_name))
146 4 : _ppi_moose_object.mooseError(
147 : "When getting a Postprocessor, failed to get a parameter with the name \"",
148 : param_name,
149 : "\".",
150 : "\n\nKnown parameters:\n",
151 4 : _ppi_moose_object.parameters());
152 :
153 22696 : if (_ppi_params.isType<PostprocessorName>(param_name))
154 : {
155 19427 : if (check_index && index > 0)
156 4 : _ppi_moose_object.paramError(param_name,
157 : "A Postprocessor was requested with index ",
158 : index,
159 : " but a single Postprocessor is coupled.");
160 : }
161 3269 : else if (_ppi_params.isType<std::vector<PostprocessorName>>(param_name))
162 : {
163 3265 : const auto & names = _ppi_params.get<std::vector<PostprocessorName>>(param_name);
164 3265 : if (check_index && names.size() <= index)
165 4 : _ppi_moose_object.paramError(param_name,
166 : "A Postprocessor was requested with index ",
167 : index,
168 : " but only ",
169 : names.size(),
170 : " Postprocessors are coupled.");
171 : }
172 : else
173 : {
174 4 : _ppi_moose_object.mooseError(
175 : "Supplied parameter with name \"",
176 : param_name,
177 : "\" of type \"",
178 4 : _ppi_params.type(param_name),
179 : "\" is not an expected type for getting a Postprocessor.\n\n",
180 : "Allowed types are \"PostprocessorName\" and \"std::vector<PostprocessorName>\".");
181 : }
182 22684 : }
183 :
184 : const PostprocessorName &
185 4 : PostprocessorInterface::getPostprocessorName(const std::string & param_name,
186 : const unsigned int index /* = 0 */) const
187 : {
188 4 : return getPostprocessorNameInternal(param_name, index, /* allow_default_value = */ false);
189 : }
190 :
191 : const PostprocessorName &
192 21658 : PostprocessorInterface::getPostprocessorNameInternal(
193 : const std::string & param_name,
194 : const unsigned int index,
195 : const bool allow_default_value /* = true */) const
196 : {
197 21658 : checkParam(param_name, index);
198 :
199 21642 : const auto & name = _ppi_params.isType<PostprocessorName>(param_name)
200 21642 : ? _ppi_params.get<PostprocessorName>(param_name)
201 2219 : : _ppi_params.get<std::vector<PostprocessorName>>(param_name)[index];
202 :
203 21642 : if (!allow_default_value && isDefaultPostprocessorValueByName(name))
204 : {
205 4 : std::stringstream oss;
206 : oss << "Cannot get the name associated with PostprocessorName parameter \"" << param_name
207 4 : << "\"";
208 4 : if (index)
209 4 : oss << " at index " << index;
210 4 : oss << ",\nbecause said parameter is a default Postprocessor value.";
211 4 : _ppi_moose_object.paramError(param_name, oss.str());
212 0 : }
213 :
214 21638 : return name;
215 : }
216 :
217 : const PostprocessorValue &
218 21276 : PostprocessorInterface::getPostprocessorValueInternal(const std::string & param_name,
219 : unsigned int index,
220 : std::size_t t_index) const
221 : {
222 21276 : const auto & name = getPostprocessorNameInternal(param_name, index);
223 :
224 : // If the Postprocessor is a default value (either set by addParam or set to a constant
225 : // value by the user in input/an action), we covert the name to a value, store said
226 : // value locally, and return it so that it fits in with the rest of the interface
227 : // (needing a reference to a value)
228 21260 : if (isDefaultPostprocessorValueByName(name))
229 : {
230 14606 : const auto value = getDefaultPostprocessorValueByName(name);
231 : const auto & value_ref =
232 29212 : *_default_values.emplace(name, std::make_unique<PostprocessorValue>(value))
233 14606 : .first->second; // first is inserted pair, second is value in pair
234 : mooseAssert(value == value_ref, "Inconsistent default value");
235 14606 : return value_ref;
236 : }
237 : // If not a default and all pps have been added, we check check for existance
238 6654 : else if (postprocessorsAdded() && !hasPostprocessorByName(name))
239 4 : _ppi_moose_object.paramError(
240 : param_name, "A Postprocessor with the name \"", name, "\" was not found.");
241 :
242 6650 : return getPostprocessorValueByNameInternal(name, t_index);
243 : }
244 :
245 : const PostprocessorValue &
246 53213 : PostprocessorInterface::getPostprocessorValueByNameInternal(const PostprocessorName & name,
247 : std::size_t t_index) const
248 : {
249 : mooseAssert(t_index < 3, "Invalid time index");
250 : mooseAssert(!isDefaultPostprocessorValueByName(name), "Should not be default");
251 :
252 : // If all pps have been added, we can check for existance
253 53213 : if (postprocessorsAdded() && !hasPostprocessorByName(name))
254 4 : _ppi_moose_object.mooseError("A Postprocessor with the name \"", name, "\" was not found.");
255 :
256 53209 : if (t_index == 0)
257 52120 : addPostprocessorDependencyHelper(name);
258 :
259 106418 : return _ppi_feproblem.getReporterData().getReporterValue<PostprocessorValue>(
260 106418 : PostprocessorReporterName(name), _ppi_moose_object, REPORTER_MODE_ROOT, t_index);
261 : }
262 :
263 : bool
264 130797 : PostprocessorInterface::postprocessorsAdded() const
265 : {
266 130797 : return _ppi_feproblem.getMooseApp().actionWarehouse().isTaskComplete("add_postprocessor");
267 : }
|