forked from idaholab/moose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MooseObject.h
309 lines (263 loc) · 10.3 KB
/
MooseObject.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html
#pragma once
// MOOSE includes
#include "InputParameters.h"
#include "ConsoleStreamInterface.h"
#include "Registry.h"
#include "MooseUtils.h"
#include "DataFileInterface.h"
#include "MooseObjectParameterName.h"
#include "libmesh/parallel_object.h"
#define usingMooseObjectMembers \
using MooseObject::isParamValid; \
using MooseObject::paramError
class MooseApp;
// needed to avoid #include cycle with MooseApp and MooseObject
[[noreturn]] void callMooseErrorRaw(std::string & msg, MooseApp * app);
/**
* Get canonical paramError prefix for param-related error/warning/info messages.
*
* Use this for building custom messages when the default paramError isn't
* quite what you need.
*/
std::string paramErrorPrefix(const InputParameters & params, const std::string & param);
// helper macro to explicitly instantiate AD classes
#define adBaseClass(X) \
template class X<RESIDUAL>; \
template class X<JACOBIAN>
/**
* Every object that can be built by the factory should be derived from this class.
*/
class MooseObject : public ConsoleStreamInterface,
public libMesh::ParallelObject,
public DataFileInterface<MooseObject>
{
public:
static InputParameters validParams();
MooseObject(const InputParameters & parameters);
virtual ~MooseObject() = default;
/**
* Get the type of this object.
* @return the name of the type of this object
*/
const std::string & type() const { return _type; }
/**
* Get the name of the object
* @return The name of the object
*/
virtual const std::string & name() const { return _name; }
/**
* The unique parameter name of a valid parameter of this object for accessing parameter controls
*/
MooseObjectParameterName uniqueParameterName(const std::string & parameter_name) const
{
return MooseObjectParameterName(_pars.get<std::string>("_moose_base"), _name, parameter_name);
}
/**
* Get the object's combined type and name; useful in error handling.
* @return The type and name of this object in the form '<type()> "<name()>"'.
*/
std::string typeAndName() const;
/**
* Get the parameters of the object
* @return The parameters of the object
*/
const InputParameters & parameters() const { return _pars; }
/**
* Retrieve a parameter for the object
* @param name The name of the parameter
* @return The value of the parameter
*/
template <typename T>
const T & getParam(const std::string & name) const;
/**
* Retrieve two parameters and provide pair of parameters for the object
* @param param1 The name of first parameter
* @param param2 The name of second parameter
* @return Vector of pairs of first and second parameters
*/
template <typename T1, typename T2>
std::vector<std::pair<T1, T2>> getParam(const std::string & param1,
const std::string & param2) const;
/**
* Verifies that the requested parameter exists and is not NULL and returns it to the caller.
* The template parameter must be a pointer or an error will be thrown.
*/
template <typename T>
T getCheckedPointerParam(const std::string & name, const std::string & error_string = "") const;
/**
* Test if the supplied parameter is valid
* @param name The name of the parameter to test
*/
inline bool isParamValid(const std::string & name) const { return _pars.isParamValid(name); }
/**
* Get the MooseApp this object is associated with.
*/
MooseApp & getMooseApp() const { return _app; }
/**
* Return the enabled status of the object.
*/
virtual bool enabled() const { return _enabled; }
/**
* Emits an error prefixed with the file and line number of the given param (from the input
* file) along with the full parameter path+name followed by the given args as the message.
* If this object's parameters were not created directly by the Parser, then this function falls
* back to the normal behavior of mooseError - only printing a message using the given args.
*/
template <typename... Args>
[[noreturn]] void paramError(const std::string & param, Args... args) const;
/**
* Emits a warning prefixed with the file and line number of the given param (from the input
* file) along with the full parameter path+name followed by the given args as the message.
* If this object's parameters were not created directly by the Parser, then this function falls
* back to the normal behavior of mooseWarning - only printing a message using the given args.
*/
template <typename... Args>
void paramWarning(const std::string & param, Args... args) const;
/**
* Emits an informational message prefixed with the file and line number of the given param
* (from the input file) along with the full parameter path+name followed by the given args as
* the message. If this object's parameters were not created directly by the Parser, then this
* function falls back to the normal behavior of mooseInfo - only printing a message using
* the given args.
*/
template <typename... Args>
void paramInfo(const std::string & param, Args... args) const;
/**
* Emits an error prefixed with object name and type.
*/
template <typename... Args>
[[noreturn]] void mooseError(Args &&... args) const
{
std::ostringstream oss;
moose::internal::mooseStreamAll(oss, errorPrefix("error"), std::forward<Args>(args)...);
std::string msg = oss.str();
callMooseErrorRaw(msg, &_app);
}
/**
* Emits an error without the prefixing included in mooseError().
*/
template <typename... Args>
[[noreturn]] void mooseErrorNonPrefixed(Args &&... args) const
{
std::ostringstream oss;
moose::internal::mooseStreamAll(oss, std::forward<Args>(args)...);
std::string msg = oss.str();
callMooseErrorRaw(msg, &_app);
}
/**
* Emits a warning prefixed with object name and type.
*/
template <typename... Args>
void mooseWarning(Args &&... args) const
{
moose::internal::mooseWarningStream(
_console, errorPrefix("warning"), std::forward<Args>(args)...);
}
/**
* Emits a warning without the prefixing included in mooseWarning().
*/
template <typename... Args>
void mooseWarningNonPrefixed(Args &&... args) const
{
moose::internal::mooseWarningStream(_console, std::forward<Args>(args)...);
}
template <typename... Args>
void mooseDeprecated(Args &&... args) const
{
moose::internal::mooseDeprecatedStream(_console, false, std::forward<Args>(args)...);
}
template <typename... Args>
void mooseInfo(Args &&... args) const
{
moose::internal::mooseInfoStream(_console, std::forward<Args>(args)...);
}
/**
* A descriptive prefix for errors for this object:
*
* The following <error_type> occurred in the object "<name>", of type "<type>".
*/
std::string errorPrefix(const std::string & error_type) const;
protected:
/// Parameters of this object, references the InputParameters stored in the InputParametersWarehouse
const InputParameters & _pars;
/// The MooseApp this object is associated with
MooseApp & _app;
/// The type of this object (the Class name)
const std::string & _type;
/// The name of this object, reference to value stored in InputParameters
const std::string & _name;
/// Reference to the "enable" InputParaemters, used by Controls for toggling on/off MooseObjects
const bool & _enabled;
private:
template <typename... Args>
std::string paramErrorMsg(const std::string & param, Args... args) const
{
auto param_prefix = paramErrorPrefix(_pars, param);
// With no input location information, append object info (name + type)
const std::string object_prefix =
_pars.inputLocation(param).empty() ? errorPrefix("parameter error") : "";
std::ostringstream oss;
moose::internal::mooseStreamAll(oss, std::forward<Args>(args)...);
std::string msg = oss.str();
// Wrap error message to a separate line from param_prefix if it is about to
// blow past 100 chars. But only wrap if the param_prefix is long enough (12
// chars) for the wrap to buy us much extra length. We don't check object_prefix
// here because it will go before the parameter error
if ((param_prefix.size() > 12 && msg.size() + param_prefix.size() > 99) ||
msg.find("\n") != std::string::npos)
{
if (param_prefix.size() > 0 && param_prefix[param_prefix.size() - 1] != ':')
param_prefix += ":";
return object_prefix + param_prefix + "\n " + MooseUtils::replaceAll(msg, "\n", "\n ");
}
return object_prefix + param_prefix + " " + msg;
}
};
template <typename T>
const T &
MooseObject::getParam(const std::string & name) const
{
return InputParameters::getParamHelper(name, _pars, static_cast<T *>(0));
}
template <typename... Args>
[[noreturn]] void
MooseObject::paramError(const std::string & param, Args... args) const
{
Moose::show_trace = false;
std::string msg = paramErrorMsg(param, std::forward<Args>(args)...);
callMooseErrorRaw(msg, &_app);
Moose::show_trace = true;
}
template <typename... Args>
void
MooseObject::paramWarning(const std::string & param, Args... args) const
{
mooseWarning(paramErrorMsg(param, std::forward<Args>(args)...));
}
template <typename... Args>
void
MooseObject::paramInfo(const std::string & param, Args... args) const
{
mooseInfo(paramErrorMsg(param, std::forward<Args>(args)...));
}
template <typename T1, typename T2>
std::vector<std::pair<T1, T2>>
MooseObject::getParam(const std::string & param1, const std::string & param2) const
{
return _pars.get<T1, T2>(param1, param2);
}
template <typename T>
T
MooseObject::getCheckedPointerParam(const std::string & name,
const std::string & error_string) const
{
return parameters().getCheckedPointerParam<T>(name, error_string);
}