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 400617 : PostprocessorInterface::validParams()
17 : {
18 400617 : return emptyInputParameters();
19 : }
20 :
21 1019048 : PostprocessorInterface::PostprocessorInterface(const MooseObject * moose_object)
22 1019048 : : _ppi_moose_object(*moose_object),
23 2038096 : _ppi_params(_ppi_moose_object.parameters()),
24 1019048 : _ppi_feproblem(*_ppi_params.getCheckedPointerParam<FEProblemBase *>("_fe_problem_base"))
25 : {
26 1019048 : }
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 19182 : PostprocessorInterface::getPostprocessorValue(const std::string & param_name,
37 : const unsigned int index /* = 0 */) const
38 : {
39 19182 : return getPostprocessorValueInternal(param_name, index, /* t_index = */ 0);
40 : }
41 :
42 : const PostprocessorValue &
43 388 : PostprocessorInterface::getPostprocessorValueOld(const std::string & param_name,
44 : const unsigned int index /* = 0 */) const
45 : {
46 388 : 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 41107 : PostprocessorInterface::getPostprocessorValueByName(const PostprocessorName & name) const
58 : {
59 41107 : return getPostprocessorValueByNameInternal(name, /* t_index = */ 0);
60 : }
61 :
62 : const PostprocessorValue &
63 393 : PostprocessorInterface::getPostprocessorValueOldByName(const PostprocessorName & name) const
64 : {
65 393 : return getPostprocessorValueByNameInternal(name, /* t_index = */ 1);
66 : }
67 :
68 : const PostprocessorValue &
69 274 : PostprocessorInterface::getPostprocessorValueOlderByName(const PostprocessorName & name) const
70 : {
71 274 : return getPostprocessorValueByNameInternal(name, /* t_index = */ 2);
72 : }
73 :
74 : bool
75 349 : PostprocessorInterface::isDefaultPostprocessorValue(const std::string & param_name,
76 : const unsigned int index /* = 0 */) const
77 : {
78 349 : return isDefaultPostprocessorValueByName(getPostprocessorNameInternal(param_name, index));
79 : }
80 :
81 : bool
82 19917 : 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 39834 : if (_ppi_feproblem.getReporterData().hasReporterValue<PostprocessorValue>(
87 39834 : PostprocessorReporterName(name)))
88 5129 : return false;
89 :
90 14788 : std::istringstream ss(name);
91 14788 : Real real_value = -std::numeric_limits<Real>::max();
92 14788 : return (ss >> real_value && ss.eof());
93 14788 : }
94 :
95 : PostprocessorValue
96 13748 : PostprocessorInterface::getDefaultPostprocessorValueByName(const PostprocessorName & name) const
97 : {
98 : mooseAssert(isDefaultPostprocessorValueByName(name), "Not a default value");
99 :
100 13748 : Real real_value = -std::numeric_limits<Real>::max();
101 13748 : std::istringstream ss(name);
102 13748 : ss >> real_value;
103 13748 : return real_value;
104 13748 : }
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 64307 : PostprocessorInterface::hasPostprocessorByName(const PostprocessorName & name) const
119 : {
120 64307 : if (!postprocessorsAdded())
121 4 : _ppi_moose_object.mooseError(
122 : "Cannot call hasPostprocessorByName() until all Postprocessors have been constructed.");
123 :
124 128606 : return _ppi_feproblem.getReporterData().hasReporterValue<PostprocessorValue>(
125 128606 : PostprocessorReporterName(name));
126 : }
127 :
128 : std::size_t
129 908 : PostprocessorInterface::coupledPostprocessors(const std::string & param_name) const
130 : {
131 908 : checkParam(param_name);
132 :
133 908 : if (_ppi_params.isType<PostprocessorName>(param_name))
134 0 : return 1;
135 908 : return _ppi_params.get<std::vector<PostprocessorName>>(param_name).size();
136 : }
137 :
138 : void
139 20841 : PostprocessorInterface::checkParam(
140 : const std::string & param_name,
141 : const unsigned int index /* = std::numeric_limits<unsigned int>::max() */) const
142 : {
143 20841 : const bool check_index = index != std::numeric_limits<unsigned int>::max();
144 :
145 20841 : 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 20837 : if (_ppi_params.isType<PostprocessorName>(param_name))
154 : {
155 17973 : 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 2864 : else if (_ppi_params.isType<std::vector<PostprocessorName>>(param_name))
162 : {
163 2860 : const auto & names = _ppi_params.get<std::vector<PostprocessorName>>(param_name);
164 2860 : 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 20825 : }
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 19933 : PostprocessorInterface::getPostprocessorNameInternal(
193 : const std::string & param_name,
194 : const unsigned int index,
195 : const bool allow_default_value /* = true */) const
196 : {
197 19933 : checkParam(param_name, index);
198 :
199 19917 : const auto & name = _ppi_params.isType<PostprocessorName>(param_name)
200 19917 : ? _ppi_params.get<PostprocessorName>(param_name)
201 1948 : : _ppi_params.get<std::vector<PostprocessorName>>(param_name)[index];
202 :
203 19917 : 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 19913 : return name;
215 : }
216 :
217 : const PostprocessorValue &
218 19580 : PostprocessorInterface::getPostprocessorValueInternal(const std::string & param_name,
219 : unsigned int index,
220 : std::size_t t_index) const
221 : {
222 19580 : 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 19564 : if (isDefaultPostprocessorValueByName(name))
229 : {
230 13748 : const auto value = getDefaultPostprocessorValueByName(name);
231 : const auto & value_ref =
232 27496 : *_default_values.emplace(name, std::make_unique<PostprocessorValue>(value))
233 13748 : .first->second; // first is inserted pair, second is value in pair
234 : mooseAssert(value == value_ref, "Inconsistent default value");
235 13748 : return value_ref;
236 : }
237 : // If not a default and all pps have been added, we check check for existance
238 5816 : 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 5812 : return getPostprocessorValueByNameInternal(name, t_index);
243 : }
244 :
245 : const PostprocessorValue &
246 47586 : 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 47586 : if (postprocessorsAdded() && !hasPostprocessorByName(name))
254 4 : _ppi_moose_object.mooseError("A Postprocessor with the name \"", name, "\" was not found.");
255 :
256 47582 : if (t_index == 0)
257 46767 : addPostprocessorDependencyHelper(name);
258 :
259 95164 : return _ppi_feproblem.getReporterData().getReporterValue<PostprocessorValue>(
260 95164 : PostprocessorReporterName(name), _ppi_moose_object, REPORTER_MODE_ROOT, t_index);
261 : }
262 :
263 : bool
264 117713 : PostprocessorInterface::postprocessorsAdded() const
265 : {
266 117713 : return _ppi_feproblem.getMooseApp().actionWarehouse().isTaskComplete("add_postprocessor");
267 : }
|