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