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 6006033 : virtual ~MooseBase() = default;
83 :
84 : /**
85 : * Get the MooseApp this class is associated with.
86 : */
87 9036580 : 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 8103972 : const std::string & type() const
94 : {
95 : mooseAssert(_type.size(), "Empty type");
96 8103972 : return _type;
97 : }
98 :
99 : /**
100 : * Get the name of the class
101 : * @return The name of the class
102 : */
103 38104861 : const std::string & name() const
104 : {
105 : mooseAssert(_name.size(), "Empty name");
106 38104861 : 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 33824953 : 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 156766 : bool hasBase() const { return _pars.hasBase(); }
143 :
144 : /**
145 : * @returns The registered base for this object (set via InputParameters::registerBase())
146 : */
147 848979 : 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 a parameter of the given name and type exists
197 : * @param name The name of the parameter to test
198 : */
199 : template <typename T>
200 9632 : inline bool haveParameter(const std::string & name) const
201 : {
202 9632 : return _pars.have_parameter<T>(name);
203 : }
204 :
205 : /**
206 : * Test if the supplied parameter is valid
207 : * @param name The name of the parameter to test
208 : */
209 19668055 : inline bool isParamValid(const std::string & name) const { return _pars.isParamValid(name); }
210 :
211 : /**
212 : * Test if the supplied parameter is set by a user, as opposed to not set or set to default
213 : * @param name The name of the parameter to test
214 : */
215 3429344 : inline bool isParamSetByUser(const std::string & name) const
216 : {
217 3429344 : return _pars.isParamSetByUser(name);
218 : }
219 :
220 : /**
221 : * Connect controllable parameter of this action with the controllable parameters of the
222 : * objects added by this action.
223 : * @param parameter Name of the controllable parameter of this action
224 : * @param object_type Type of the object added by this action.
225 : * @param object_name Name of the object added by this action.
226 : * @param object_parameter Name of the parameter of the object.
227 : */
228 : void connectControllableParams(const std::string & parameter,
229 : const std::string & object_type,
230 : const std::string & object_name,
231 : const std::string & object_parameter) const;
232 :
233 : /**
234 : * Emits an error prefixed with the file and line number of the given param (from the input
235 : * file) along with the full parameter path+name followed by the given args as the message.
236 : * If this object's parameters were not created directly by the Parser, then this function falls
237 : * back to the normal behavior of mooseError - only printing a message using the given args.
238 : */
239 : template <typename... Args>
240 : [[noreturn]] void paramError(const std::string & param, Args... args) const;
241 :
242 : /**
243 : * Emits a warning prefixed with the file and line number of the given param (from the input
244 : * file) along with the full parameter path+name followed by the given args as the message.
245 : * If this object's parameters were not created directly by the Parser, then this function falls
246 : * back to the normal behavior of mooseWarning - only printing a message using the given args.
247 : */
248 : template <typename... Args>
249 : void paramWarning(const std::string & param, Args... args) const;
250 :
251 : /**
252 : * Emits an informational message prefixed with the file and line number of the given param
253 : * (from the input file) along with the full parameter path+name followed by the given args as
254 : * the message. If this object's parameters were not created directly by the Parser, then this
255 : * function falls back to the normal behavior of mooseInfo - only printing a message using
256 : * the given args.
257 : */
258 : template <typename... Args>
259 : void paramInfo(const std::string & param, Args... args) const;
260 :
261 : /**
262 : * @returns A prefix to be used in messages that contain the input
263 : * file location associated with this object (if any) and the
264 : * name and type of the object.
265 : */
266 16073 : std::string messagePrefix(const bool hit_prefix = true) const
267 : {
268 16073 : return messagePrefix(_pars, hit_prefix);
269 : }
270 :
271 : /**
272 : * Deprecated message prefix; the error type is no longer used
273 : */
274 : std::string errorPrefix(const std::string &) const { return messagePrefix(); }
275 :
276 : /**
277 : * Emits an error prefixed with object name and type and optionally a file path
278 : * to the top-level block parameter if available.
279 : */
280 : template <typename... Args>
281 1357 : [[noreturn]] void mooseError(Args &&... args) const
282 : {
283 1357 : callMooseError(argumentsToString(std::forward<Args>(args)...), /* with_prefix = */ true);
284 : }
285 :
286 : template <typename... Args>
287 5 : [[noreturn]] void mooseDocumentedError(const std::string & repo_name,
288 : const unsigned int issue_num,
289 : Args &&... args) const
290 : {
291 9 : callMooseError(moose::internal::formatMooseDocumentedError(
292 : repo_name, issue_num, argumentsToString(std::forward<Args>(args)...)),
293 : /* with_prefix = */ true);
294 : }
295 :
296 : /**
297 : * Emits an error without the prefixing included in mooseError().
298 : */
299 : template <typename... Args>
300 : [[noreturn]] void mooseErrorNonPrefixed(Args &&... args) const
301 : {
302 : callMooseError(argumentsToString(std::forward<Args>(args)...), /* with_prefix = */ false);
303 : }
304 :
305 : /**
306 : * Emits a warning prefixed with object name and type.
307 : */
308 : template <typename... Args>
309 2575 : void mooseWarning(Args &&... args) const
310 : {
311 2575 : moose::internal::mooseWarningStream(_console, messagePrefix(true), std::forward<Args>(args)...);
312 2436 : }
313 :
314 : /**
315 : * Emits a warning without the prefixing included in mooseWarning().
316 : */
317 : template <typename... Args>
318 : void mooseWarningNonPrefixed(Args &&... args) const
319 : {
320 : moose::internal::mooseWarningStream(_console, std::forward<Args>(args)...);
321 : }
322 :
323 : /**
324 : * Emits a deprecation warning prefixed with the object name and type, and a stack trace.
325 : */
326 : template <typename... Args>
327 1333 : void mooseDeprecated(Args &&... args) const
328 : {
329 2666 : moose::internal::mooseDeprecatedStream(
330 1333 : _console, false, true, true, messagePrefix(true), std::forward<Args>(args)...);
331 1333 : }
332 :
333 : /**
334 : * Emits a deprecation warning prefixed with the object name and type, and no stack trace.
335 : */
336 : template <typename... Args>
337 2 : void mooseDeprecatedNoTrace(Args &&... args) const
338 : {
339 4 : moose::internal::mooseDeprecatedStream(
340 2 : _console, false, true, false, messagePrefix(true), std::forward<Args>(args)...);
341 0 : }
342 :
343 : template <typename... Args>
344 12163 : void mooseInfo(Args &&... args) const
345 : {
346 12163 : moose::internal::mooseInfoStream(_console, messagePrefix(true), std::forward<Args>(args)...);
347 12163 : }
348 :
349 : /**
350 : * External method for calling moose error with added object context.
351 : * @param msg The message
352 : * @param with_prefix If true, add the prefix from messagePrefix(), which is the object
353 : * information (type, name, etc)
354 : * @param node Optional hit node to add file path context as a prefix
355 : * @param show_trace Whether or not to show a stack trace, defaults to true
356 : */
357 : [[noreturn]] void callMooseError(std::string msg,
358 : const bool with_prefix,
359 : const hit::Node * node = nullptr,
360 : const bool show_trace = true) const;
361 :
362 : /**
363 : * External method for calling moose error with added object context.
364 : *
365 : * Needed so that objects without the MooseBase context (InputParameters)
366 : * can call errors with context
367 : *
368 : * @param app The app pointer (if available); adds multiapp context and clears the console
369 : * @param params The parameters, needed to obtain object information
370 : * @param msg The message
371 : * @param with_prefix If true, add the prefix from messagePrefix(), which is the object
372 : * information (type, name, etc)
373 : * @param node Optional hit node to add file path context as a prefix
374 : * @param show_trace Whether or not to show a stack trace, defaults to true
375 : */
376 : [[noreturn]] static void callMooseError(MooseApp * const app,
377 : const InputParameters & params,
378 : std::string msg,
379 : const bool with_prefix,
380 : const hit::Node * node,
381 : const bool show_trace = true);
382 :
383 : protected:
384 : /// The MOOSE application this is associated with
385 : MooseApp & _app;
386 :
387 : /// The type of this class
388 : const std::string & _type;
389 :
390 : /// The name of this class
391 : const std::string & _name;
392 :
393 : /// The object's parameters
394 : const InputParameters & _pars;
395 :
396 : private:
397 : /**
398 : * Internal method for getting the message prefix for an object (object type, name, etc).
399 : *
400 : * Needs to be static so that we can call it externally from InputParameters for
401 : * errors that do not have context of the MooseBase
402 : */
403 : static std::string messagePrefix(const InputParameters & params, const bool hit_prefix);
404 :
405 : /**
406 : * Internal method for getting a hit node (if available) given a set of parameters
407 : *
408 : * Needs to be static so that we can call it externally from InputParameters for
409 : * errors that do not have context of the MooseBase
410 : */
411 : static const hit::Node * getHitNode(const InputParameters & params);
412 : };
413 :
414 : template <typename T>
415 : const T &
416 45098691 : MooseBase::getParam(const std::string & name) const
417 : {
418 45098691 : return InputParameters::getParamHelper<T>(name, _pars);
419 : }
420 :
421 : template <typename T>
422 : const T *
423 9632 : MooseBase::queryParam(const std::string & name) const
424 : {
425 9632 : return haveParameter<T>(name) && isParamValid(name) ? &getParam<T>(name) : nullptr;
426 : }
427 :
428 : template <typename T>
429 : const T &
430 46016 : MooseBase::getRenamedParam(const std::string & old_name, const std::string & new_name) const
431 : {
432 : // Most important: accept new parameter
433 46016 : if (isParamSetByUser(new_name) && !isParamValid(old_name))
434 45437 : return getParam<T>(new_name);
435 : // Second most: accept old parameter
436 579 : if (isParamValid(old_name) && !isParamSetByUser(new_name))
437 285 : return getParam<T>(old_name);
438 : // Third most: accept default for new parameter
439 294 : if (isParamValid(new_name) && !isParamValid(old_name))
440 294 : return getParam<T>(new_name);
441 : // Refuse: no default, no value passed
442 0 : if (!isParamValid(old_name) && !isParamValid(new_name))
443 0 : mooseError("parameter '" + new_name +
444 : "' is being retrieved without being set.\nDid you misspell it?");
445 : // Refuse: both old and new parameters set by user
446 : else
447 0 : mooseError("Parameter '" + new_name + "' may not be provided alongside former parameter '" +
448 : old_name + "'");
449 : }
450 :
451 : template <typename T1, typename T2>
452 : std::vector<std::pair<T1, T2>>
453 66693 : MooseBase::getParam(const std::string & param1, const std::string & param2) const
454 : {
455 66693 : return _pars.get<T1, T2>(param1, param2);
456 : }
457 :
458 : template <typename T>
459 : T
460 5843520 : MooseBase::getCheckedPointerParam(const std::string & name, const std::string & error_string) const
461 : {
462 5843520 : return _pars.getCheckedPointerParam<T>(name, error_string);
463 : }
464 :
465 : template <typename... Args>
466 : [[noreturn]] void
467 1204 : MooseBase::paramError(const std::string & param, Args... args) const
468 : {
469 1208 : _pars.paramError(param, std::forward<Args>(args)...);
470 : }
471 :
472 : template <typename... Args>
473 : void
474 68 : MooseBase::paramWarning(const std::string & param, Args... args) const
475 : {
476 68 : mooseWarning(_pars.paramMessage(param, std::forward<Args>(args)...));
477 56 : }
478 :
479 : template <typename... Args>
480 : void
481 138 : MooseBase::paramInfo(const std::string & param, Args... args) const
482 : {
483 138 : mooseInfo(_pars.paramMessage(param, std::forward<Args>(args)...));
484 138 : }
|