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 : #pragma once
11 :
12 : #include "ConsoleStreamInterface.h"
13 : #include "StreamArguments.h"
14 : #include "InputParameters.h"
15 : #include "MooseError.h"
16 : #include "MooseObjectName.h"
17 : #include "MooseObjectParameterName.h"
18 :
19 : class MooseApp;
20 :
21 : namespace hit
22 : {
23 : class Node;
24 : }
25 :
26 : #define usingMooseBaseMembers \
27 : using MooseBase::getMooseApp; \
28 : using MooseBase::type; \
29 : using MooseBase::name; \
30 : using MooseBase::typeAndName; \
31 : using MooseBase::uniqueName; \
32 : using MooseBase::parameters; \
33 : using MooseBase::isParamValid; \
34 : using MooseBase::isParamSetByUser; \
35 : using MooseBase::paramError; \
36 : using MooseBase::paramWarning; \
37 : using MooseBase::paramInfo; \
38 : using MooseBase::_app; \
39 : using MooseBase::_type; \
40 : using MooseBase::_name; \
41 : using MooseBase::_pars
42 :
43 : /**
44 : * Base class for everything in MOOSE with a name and a type.
45 : * You will most likely want to inherit instead
46 : * - MooseObject for an object created within a system
47 : * - Action for a class performing a setup task, like creating objects
48 : */
49 : class MooseBase : public ConsoleStreamInterface
50 : {
51 : public:
52 : /// The name of the parameter that contains the object type
53 : static const std::string type_param;
54 : /// The name of the parameter that contains the object name
55 : static const std::string name_param;
56 : /// The name of the parameter that contains the unique object name
57 : static const std::string unique_name_param;
58 : /// The name of the parameter that contains the MooseApp
59 : static const std::string app_param;
60 : /// The name of the parameter that contains the moose system base
61 : static const std::string moose_base_param;
62 : #ifdef MOOSE_KOKKOS_ENABLED
63 : /// The name of the parameter that indicates an object is a Kokkos functor
64 : static const std::string kokkos_object_param;
65 : #endif
66 :
67 : static InputParameters validParams();
68 :
69 : /**
70 : * Primary constructor for general objects
71 : * @param params The parameters
72 : */
73 : MooseBase(const InputParameters & params);
74 :
75 : /**
76 : * Constructor to only be used by MooseApp.
77 : * @param app The app
78 : * @param params The app params
79 : */
80 : MooseBase(MooseApp & app, const InputParameters & params);
81 :
82 5752802 : virtual ~MooseBase() = default;
83 :
84 : /**
85 : * Get the MooseApp this class is associated with.
86 : */
87 9679867 : MooseApp & getMooseApp() const { return _app; }
88 :
89 : /**
90 : * Get the type of this class.
91 : * @return the name of the type of this class
92 : */
93 8241366 : const std::string & type() const
94 : {
95 : mooseAssert(_type.size(), "Empty type");
96 8241366 : return _type;
97 : }
98 :
99 : /**
100 : * Get the name of the class
101 : * @return The name of the class
102 : */
103 40713559 : const std::string & name() const
104 : {
105 : mooseAssert(_name.size(), "Empty name");
106 40713559 : return _name;
107 : }
108 :
109 : /**
110 : * Get the class's combined type and name; useful in error handling.
111 : * @return The type and name of this class in the form '<type()> "<name()>"'.
112 : */
113 : std::string typeAndName() const;
114 :
115 : /**
116 : * @returns The unique parameter name of a valid parameter of this
117 : * object for accessing parameter controls
118 : */
119 : MooseObjectParameterName uniqueParameterName(const std::string & parameter_name) const;
120 :
121 : /**
122 : * @returns The unique name for accessing input parameters of this object in
123 : * the InputParameterWarehouse
124 : */
125 : MooseObjectName uniqueName() const;
126 :
127 : /**
128 : * Get the parameters of the object
129 : * @return The parameters of the object
130 : */
131 35628442 : const InputParameters & parameters() const { return _pars; }
132 :
133 : /**
134 : * @returns The block-level hit node for this object, if any
135 : */
136 : const hit::Node * getHitNode() const { return getHitNode(_pars); }
137 :
138 : /**
139 : * @returns Whether or not this object has a registered base (set via
140 : * InputParameters::registerBase())
141 : */
142 163926 : bool hasBase() const { return _pars.hasBase(); }
143 :
144 : /**
145 : * @returns The registered base for this object (set via InputParameters::registerBase())
146 : */
147 911201 : const std::string & getBase() const { return _pars.getBase(); }
148 :
149 : /**
150 : * Retrieve a parameter for the object
151 : * @param name The name of the parameter
152 : * @return The value of the parameter
153 : */
154 : template <typename T>
155 : const T & getParam(const std::string & name) const;
156 :
157 : /**
158 : * Query a parameter for the object
159 : *
160 : * If the parameter is not valid, nullptr will be returned
161 : *
162 : * @param name The name of the parameter
163 : * @return A pointer to the parameter value, if it exists
164 : */
165 : template <typename T>
166 : const T * queryParam(const std::string & name) const;
167 :
168 : /**
169 : * Retrieve a renamed parameter for the object. This helper makes sure we
170 : * check both names before erroring, and that only one parameter is passed to avoid
171 : * silent errors
172 : * @param old_name the old name for the parameter
173 : * @param new_name the new name for the parameter
174 : */
175 : template <typename T>
176 : const T & getRenamedParam(const std::string & old_name, const std::string & new_name) const;
177 :
178 : /**
179 : * Retrieve two parameters and provide pair of parameters for the object
180 : * @param param1 The name of first parameter
181 : * @param param2 The name of second parameter
182 : * @return Vector of pairs of first and second parameters
183 : */
184 : template <typename T1, typename T2>
185 : std::vector<std::pair<T1, T2>> getParam(const std::string & param1,
186 : const std::string & param2) const;
187 :
188 : /**
189 : * Verifies that the requested parameter exists and is not NULL and returns it to the caller.
190 : * The template parameter must be a pointer or an error will be thrown.
191 : */
192 : template <typename T>
193 : T getCheckedPointerParam(const std::string & name, const std::string & error_string = "") const;
194 :
195 : /**
196 : * Test if the supplied parameter is valid
197 : * @param name The name of the parameter to test
198 : */
199 21348754 : inline bool isParamValid(const std::string & name) const { return _pars.isParamValid(name); }
200 :
201 : /**
202 : * Test if the supplied parameter is set by a user, as opposed to not set or set to default
203 : * @param name The name of the parameter to test
204 : */
205 3563479 : inline bool isParamSetByUser(const std::string & name) const
206 : {
207 3563479 : return _pars.isParamSetByUser(name);
208 : }
209 :
210 : /**
211 : * Connect controllable parameter of this action with the controllable parameters of the
212 : * objects added by this action.
213 : * @param parameter Name of the controllable parameter of this action
214 : * @param object_type Type of the object added by this action.
215 : * @param object_name Name of the object added by this action.
216 : * @param object_parameter Name of the parameter of the object.
217 : */
218 : void connectControllableParams(const std::string & parameter,
219 : const std::string & object_type,
220 : const std::string & object_name,
221 : const std::string & object_parameter) const;
222 :
223 : /**
224 : * Emits an error prefixed with the file and line number of the given param (from the input
225 : * file) along with the full parameter path+name followed by the given args as the message.
226 : * If this object's parameters were not created directly by the Parser, then this function falls
227 : * back to the normal behavior of mooseError - only printing a message using the given args.
228 : */
229 : template <typename... Args>
230 : [[noreturn]] void paramError(const std::string & param, Args... args) const;
231 :
232 : /**
233 : * Emits a warning prefixed with the file and line number of the given param (from the input
234 : * file) along with the full parameter path+name followed by the given args as the message.
235 : * If this object's parameters were not created directly by the Parser, then this function falls
236 : * back to the normal behavior of mooseWarning - only printing a message using the given args.
237 : */
238 : template <typename... Args>
239 : void paramWarning(const std::string & param, Args... args) const;
240 :
241 : /**
242 : * Emits an informational message prefixed with the file and line number of the given param
243 : * (from the input file) along with the full parameter path+name followed by the given args as
244 : * the message. If this object's parameters were not created directly by the Parser, then this
245 : * function falls back to the normal behavior of mooseInfo - only printing a message using
246 : * the given args.
247 : */
248 : template <typename... Args>
249 : void paramInfo(const std::string & param, Args... args) const;
250 :
251 : /**
252 : * @returns A prefix to be used in messages that contain the input
253 : * file location associated with this object (if any) and the
254 : * name and type of the object.
255 : */
256 15899 : std::string messagePrefix(const bool hit_prefix = true) const
257 : {
258 15899 : return messagePrefix(_pars, hit_prefix);
259 : }
260 :
261 : /**
262 : * Deprecated message prefix; the error type is no longer used
263 : */
264 : std::string errorPrefix(const std::string &) const { return messagePrefix(); }
265 :
266 : /**
267 : * Emits an error prefixed with object name and type and optionally a file path
268 : * to the top-level block parameter if available.
269 : */
270 : template <typename... Args>
271 1725 : [[noreturn]] void mooseError(Args &&... args) const
272 : {
273 1725 : callMooseError(argumentsToString(std::forward<Args>(args)...), /* with_prefix = */ true);
274 : }
275 :
276 : template <typename... Args>
277 6 : [[noreturn]] void mooseDocumentedError(const std::string & repo_name,
278 : const unsigned int issue_num,
279 : Args &&... args) const
280 : {
281 10 : callMooseError(moose::internal::formatMooseDocumentedError(
282 : repo_name, issue_num, argumentsToString(std::forward<Args>(args)...)),
283 : /* with_prefix = */ true);
284 : }
285 :
286 : /**
287 : * Emits an error without the prefixing included in mooseError().
288 : */
289 : template <typename... Args>
290 : [[noreturn]] void mooseErrorNonPrefixed(Args &&... args) const
291 : {
292 : callMooseError(argumentsToString(std::forward<Args>(args)...), /* with_prefix = */ false);
293 : }
294 :
295 : /**
296 : * Emits a warning prefixed with object name and type.
297 : */
298 : template <typename... Args>
299 2726 : void mooseWarning(Args &&... args) const
300 : {
301 2726 : moose::internal::mooseWarningStream(_console, messagePrefix(true), std::forward<Args>(args)...);
302 2552 : }
303 :
304 : /**
305 : * Emits a warning without the prefixing included in mooseWarning().
306 : */
307 : template <typename... Args>
308 : void mooseWarningNonPrefixed(Args &&... args) const
309 : {
310 : moose::internal::mooseWarningStream(_console, std::forward<Args>(args)...);
311 : }
312 :
313 : template <typename... Args>
314 1480 : void mooseDeprecated(Args &&... args) const
315 : {
316 2960 : moose::internal::mooseDeprecatedStream(
317 1480 : _console, false, true, messagePrefix(true), std::forward<Args>(args)...);
318 1478 : }
319 :
320 : template <typename... Args>
321 11693 : void mooseInfo(Args &&... args) const
322 : {
323 11693 : moose::internal::mooseInfoStream(_console, messagePrefix(true), std::forward<Args>(args)...);
324 11693 : }
325 :
326 : /**
327 : * External method for calling moose error with added object context.
328 : * @param msg The message
329 : * @param with_prefix If true, add the prefix from messagePrefix(), which is the object
330 : * information (type, name, etc)
331 : * @param node Optional hit node to add file path context as a prefix
332 : */
333 : [[noreturn]] void
334 : callMooseError(std::string msg, const bool with_prefix, const hit::Node * node = nullptr) const;
335 :
336 : /**
337 : * External method for calling moose error with added object context.
338 : *
339 : * Needed so that objects without the MooseBase context (InputParameters)
340 : * can call errors with context
341 : *
342 : * @param app The app pointer (if available); adds multiapp context and clears the console
343 : * @param params The parameters, needed to obtain object information
344 : * @param msg The message
345 : * @param with_prefix If true, add the prefix from messagePrefix(), which is the object
346 : * information (type, name, etc)
347 : * @param node Optional hit node to add file path context as a prefix
348 : */
349 : [[noreturn]] static void callMooseError(MooseApp * const app,
350 : const InputParameters & params,
351 : std::string msg,
352 : const bool with_prefix,
353 : const hit::Node * node);
354 :
355 : protected:
356 : /// The MOOSE application this is associated with
357 : MooseApp & _app;
358 :
359 : /// The type of this class
360 : const std::string & _type;
361 :
362 : /// The name of this class
363 : const std::string & _name;
364 :
365 : /// The object's parameters
366 : const InputParameters & _pars;
367 :
368 : private:
369 : /**
370 : * Internal method for getting the message prefix for an object (object type, name, etc).
371 : *
372 : * Needs to be static so that we can call it externally from InputParameters for
373 : * errors that do not have context of the MooseBase
374 : */
375 : static std::string messagePrefix(const InputParameters & params, const bool hit_prefix);
376 :
377 : /**
378 : * Internal method for getting a hit node (if available) given a set of parameters
379 : *
380 : * Needs to be static so that we can call it externally from InputParameters for
381 : * errors that do not have context of the MooseBase
382 : */
383 : static const hit::Node * getHitNode(const InputParameters & params);
384 : };
385 :
386 : template <typename T>
387 : const T &
388 47553458 : MooseBase::getParam(const std::string & name) const
389 : {
390 47553458 : return InputParameters::getParamHelper<T>(name, _pars);
391 : }
392 :
393 : template <typename T>
394 : const T *
395 2370 : MooseBase::queryParam(const std::string & name) const
396 : {
397 2370 : return isParamValid(name) ? &getParam<T>(name) : nullptr;
398 : }
399 :
400 : template <typename T>
401 : const T &
402 51586 : MooseBase::getRenamedParam(const std::string & old_name, const std::string & new_name) const
403 : {
404 : // Most important: accept new parameter
405 51586 : if (isParamSetByUser(new_name) && !isParamValid(old_name))
406 49937 : return getParam<T>(new_name);
407 : // Second most: accept old parameter
408 1649 : if (isParamValid(old_name) && !isParamSetByUser(new_name))
409 1078 : return getParam<T>(old_name);
410 : // Third most: accept default for new parameter
411 571 : if (isParamValid(new_name) && !isParamValid(old_name))
412 571 : return getParam<T>(new_name);
413 : // Refuse: no default, no value passed
414 0 : if (!isParamValid(old_name) && !isParamValid(new_name))
415 0 : mooseError("parameter '" + new_name +
416 : "' is being retrieved without being set.\nDid you misspell it?");
417 : // Refuse: both old and new parameters set by user
418 : else
419 0 : mooseError("Parameter '" + new_name + "' may not be provided alongside former parameter '" +
420 : old_name + "'");
421 : }
422 :
423 : template <typename T1, typename T2>
424 : std::vector<std::pair<T1, T2>>
425 69669 : MooseBase::getParam(const std::string & param1, const std::string & param2) const
426 : {
427 69669 : return _pars.get<T1, T2>(param1, param2);
428 : }
429 :
430 : template <typename T>
431 : T
432 6068050 : MooseBase::getCheckedPointerParam(const std::string & name, const std::string & error_string) const
433 : {
434 6068050 : return _pars.getCheckedPointerParam<T>(name, error_string);
435 : }
436 :
437 : template <typename... Args>
438 : [[noreturn]] void
439 1564 : MooseBase::paramError(const std::string & param, Args... args) const
440 : {
441 1566 : _pars.paramError(param, std::forward<Args>(args)...);
442 : }
443 :
444 : template <typename... Args>
445 : void
446 80 : MooseBase::paramWarning(const std::string & param, Args... args) const
447 : {
448 80 : mooseWarning(_pars.paramMessage(param, std::forward<Args>(args)...));
449 64 : }
450 :
451 : template <typename... Args>
452 : void
453 133 : MooseBase::paramInfo(const std::string & param, Args... args) const
454 : {
455 133 : mooseInfo(_pars.paramMessage(param, std::forward<Args>(args)...));
456 133 : }
|