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 6177723 : virtual ~MooseBase() = default;
83 :
84 : /**
85 : * Get the MooseApp this class is associated with.
86 : */
87 9155569 : 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 8303111 : const std::string & type() const
94 : {
95 : mooseAssert(_type.size(), "Empty type");
96 8303111 : return _type;
97 : }
98 :
99 : /**
100 : * Get the name of the class
101 : * @return The name of the class
102 : */
103 38934304 : const std::string & name() const
104 : {
105 : mooseAssert(_name.size(), "Empty name");
106 38934304 : 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 34828518 : 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 159068 : bool hasBase() const { return _pars.hasBase(); }
143 :
144 : /**
145 : * @returns The registered base for this object (set via InputParameters::registerBase())
146 : */
147 864029 : 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 a parameter of the given name and type does not exist or if the
161 : * parameter is not valid, nullptr will be returned
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 20070821 : 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 3559156 : inline bool isParamSetByUser(const std::string & name) const
206 : {
207 3559156 : 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 16429 : std::string messagePrefix(const bool hit_prefix = true) const
257 : {
258 16429 : 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 1358 : [[noreturn]] void mooseError(Args &&... args) const
272 : {
273 1358 : callMooseError(argumentsToString(std::forward<Args>(args)...), /* with_prefix = */ true);
274 : }
275 :
276 : template <typename... Args>
277 5 : [[noreturn]] void mooseDocumentedError(const std::string & repo_name,
278 : const unsigned int issue_num,
279 : Args &&... args) const
280 : {
281 9 : 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 2810 : void mooseWarning(Args &&... args) const
300 : {
301 2810 : moose::internal::mooseWarningStream(_console, messagePrefix(true), std::forward<Args>(args)...);
302 2666 : }
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 : /**
314 : * Emits a deprecation warning prefixed with the object name and type, and a stack trace.
315 : */
316 : template <typename... Args>
317 1360 : void mooseDeprecated(Args &&... args) const
318 : {
319 2720 : moose::internal::mooseDeprecatedStream(
320 1360 : _console, false, true, true, messagePrefix(true), std::forward<Args>(args)...);
321 1360 : }
322 :
323 : /**
324 : * Emits a deprecation warning prefixed with the object name and type, and no stack trace.
325 : */
326 : template <typename... Args>
327 2 : void mooseDeprecatedNoTrace(Args &&... args) const
328 : {
329 4 : moose::internal::mooseDeprecatedStream(
330 2 : _console, false, true, false, messagePrefix(true), std::forward<Args>(args)...);
331 0 : }
332 :
333 : template <typename... Args>
334 12257 : void mooseInfo(Args &&... args) const
335 : {
336 12257 : moose::internal::mooseInfoStream(_console, messagePrefix(true), std::forward<Args>(args)...);
337 12257 : }
338 :
339 : /**
340 : * External method for calling moose error with added object context.
341 : * @param msg The message
342 : * @param with_prefix If true, add the prefix from messagePrefix(), which is the object
343 : * information (type, name, etc)
344 : * @param node Optional hit node to add file path context as a prefix
345 : * @param show_trace Whether or not to show a stack trace, defaults to true
346 : */
347 : [[noreturn]] void callMooseError(std::string msg,
348 : const bool with_prefix,
349 : const hit::Node * node = nullptr,
350 : const bool show_trace = true) const;
351 :
352 : /**
353 : * External method for calling moose error with added object context.
354 : *
355 : * Needed so that objects without the MooseBase context (InputParameters)
356 : * can call errors with context
357 : *
358 : * @param app The app pointer (if available); adds multiapp context and clears the console
359 : * @param params The parameters, needed to obtain object information
360 : * @param msg The message
361 : * @param with_prefix If true, add the prefix from messagePrefix(), which is the object
362 : * information (type, name, etc)
363 : * @param node Optional hit node to add file path context as a prefix
364 : * @param show_trace Whether or not to show a stack trace, defaults to true
365 : */
366 : [[noreturn]] static void callMooseError(MooseApp * const app,
367 : const InputParameters & params,
368 : std::string msg,
369 : const bool with_prefix,
370 : const hit::Node * node,
371 : const bool show_trace = true);
372 :
373 : protected:
374 : /// The MOOSE application this is associated with
375 : MooseApp & _app;
376 :
377 : /// The type of this class
378 : const std::string & _type;
379 :
380 : /// The name of this class
381 : const std::string & _name;
382 :
383 : /// The object's parameters
384 : const InputParameters & _pars;
385 :
386 : private:
387 : /**
388 : * Internal method for getting the message prefix for an object (object type, name, etc).
389 : *
390 : * Needs to be static so that we can call it externally from InputParameters for
391 : * errors that do not have context of the MooseBase
392 : */
393 : static std::string messagePrefix(const InputParameters & params, const bool hit_prefix);
394 :
395 : /**
396 : * Internal method for getting a hit node (if available) given a set of parameters
397 : *
398 : * Needs to be static so that we can call it externally from InputParameters for
399 : * errors that do not have context of the MooseBase
400 : */
401 : static const hit::Node * getHitNode(const InputParameters & params);
402 : };
403 :
404 : template <typename T>
405 : const T &
406 46061212 : MooseBase::getParam(const std::string & name) const
407 : {
408 46061212 : return InputParameters::getParamHelper<T>(name, _pars);
409 : }
410 :
411 : template <typename T>
412 : const T *
413 11375 : MooseBase::queryParam(const std::string & name) const
414 : {
415 11375 : return _pars.queryParam<T>(name);
416 : }
417 :
418 : template <typename T>
419 : const T &
420 47219 : MooseBase::getRenamedParam(const std::string & old_name, const std::string & new_name) const
421 : {
422 : // Most important: accept new parameter
423 47219 : if (isParamSetByUser(new_name) && !isParamValid(old_name))
424 46640 : return getParam<T>(new_name);
425 : // Second most: accept old parameter
426 579 : if (isParamValid(old_name) && !isParamSetByUser(new_name))
427 285 : return getParam<T>(old_name);
428 : // Third most: accept default for new parameter
429 294 : if (isParamValid(new_name) && !isParamValid(old_name))
430 294 : return getParam<T>(new_name);
431 : // Refuse: no default, no value passed
432 0 : if (!isParamValid(old_name) && !isParamValid(new_name))
433 0 : mooseError("parameter '" + new_name +
434 : "' is being retrieved without being set.\nDid you misspell it?");
435 : // Refuse: both old and new parameters set by user
436 : else
437 0 : mooseError("Parameter '" + new_name + "' may not be provided alongside former parameter '" +
438 : old_name + "'");
439 : }
440 :
441 : template <typename T1, typename T2>
442 : std::vector<std::pair<T1, T2>>
443 68026 : MooseBase::getParam(const std::string & param1, const std::string & param2) const
444 : {
445 68026 : return _pars.get<T1, T2>(param1, param2);
446 : }
447 :
448 : template <typename T>
449 : T
450 6011003 : MooseBase::getCheckedPointerParam(const std::string & name, const std::string & error_string) const
451 : {
452 6011003 : return _pars.getCheckedPointerParam<T>(name, error_string);
453 : }
454 :
455 : template <typename... Args>
456 : [[noreturn]] void
457 1211 : MooseBase::paramError(const std::string & param, Args... args) const
458 : {
459 1215 : _pars.paramError(param, std::forward<Args>(args)...);
460 : }
461 :
462 : template <typename... Args>
463 : void
464 68 : MooseBase::paramWarning(const std::string & param, Args... args) const
465 : {
466 68 : mooseWarning(_pars.paramMessage(param, std::forward<Args>(args)...));
467 56 : }
468 :
469 : template <typename... Args>
470 : void
471 138 : MooseBase::paramInfo(const std::string & param, Args... args) const
472 : {
473 138 : mooseInfo(_pars.paramMessage(param, std::forward<Args>(args)...));
474 138 : }
|