forked from idaholab/moose
/
OutputWarehouse.C
344 lines (298 loc) · 8.1 KB
/
OutputWarehouse.C
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//* 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
// MOOSE includes
#include "OutputWarehouse.h"
#include "Output.h"
#include "Console.h"
#include "FileOutput.h"
#include "Checkpoint.h"
#include "FEProblem.h"
#include "TableOutput.h"
#include "Exodus.h"
#include <libgen.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
OutputWarehouse::OutputWarehouse(MooseApp & app)
: _app(app),
_buffer_action_console_outputs(false),
_output_exec_flag(EXEC_CUSTOM),
_force_output(false),
_logging_requested(false),
_last_message_ended_in_newline(true),
_num_printed(0)
{
// Set the reserved names
_reserved.insert("none"); // allows 'none' to be used as a keyword in 'outputs' parameter
_reserved.insert("all"); // allows 'all' to be used as a keyword in 'outputs' parameter
}
OutputWarehouse::~OutputWarehouse()
{
// If the output buffer is not empty, it needs to be written
if (_console_buffer.str().length())
mooseConsole();
}
void
OutputWarehouse::initialSetup()
{
for (const auto & obj : _all_objects)
obj->initialSetup();
}
void
OutputWarehouse::timestepSetup()
{
for (const auto & obj : _all_objects)
obj->timestepSetup();
}
void
OutputWarehouse::solveSetup()
{
for (const auto & obj : _all_objects)
obj->solveSetup();
}
void
OutputWarehouse::jacobianSetup()
{
for (const auto & obj : _all_objects)
obj->jacobianSetup();
}
void
OutputWarehouse::residualSetup()
{
for (const auto & obj : _all_objects)
obj->residualSetup();
}
void
OutputWarehouse::subdomainSetup()
{
for (const auto & obj : _all_objects)
obj->subdomainSetup();
}
void
OutputWarehouse::addOutput(std::shared_ptr<Output> & output)
{
_all_ptrs.push_back(output);
// Add the object to the warehouse storage, Checkpoint placed at end so they are called last
Checkpoint * cp = dynamic_cast<Checkpoint *>(output.get());
if (cp != NULL)
_all_objects.push_back(output.get());
else
_all_objects.insert(_all_objects.begin(), output.get());
// Store the name and pointer
_object_map[output->name()] = output.get();
_object_names.insert(output->name());
// If the output object is a FileOutput then store the output filename
FileOutput * ptr = dynamic_cast<FileOutput *>(output.get());
if (ptr != NULL)
addOutputFilename(ptr->filename());
// Insert object sync times to the global set
if (output->parameters().isParamValid("sync_times"))
{
std::vector<Real> sync_times = output->parameters().get<std::vector<Real>>("sync_times");
_sync_times.insert(sync_times.begin(), sync_times.end());
}
}
bool
OutputWarehouse::hasOutput(const std::string & name) const
{
return _object_map.find(name) != _object_map.end();
}
const std::set<OutputName> &
OutputWarehouse::getOutputNames()
{
if (_object_names.empty() && _app.actionWarehouse().hasActions("add_output"))
{
const auto & actions = _app.actionWarehouse().getActionListByName("add_output");
for (const auto & act : actions)
_object_names.insert(act->name());
}
return _object_names;
}
void
OutputWarehouse::addOutputFilename(const OutFileBase & filename)
{
if (_file_base_set.find(filename) != _file_base_set.end())
mooseError("An output file with the name, ", filename, ", already exists.");
_file_base_set.insert(filename);
}
void
OutputWarehouse::outputStep(ExecFlagType type)
{
if (_force_output)
type = EXEC_FORCED;
for (const auto & obj : _all_objects)
if (obj->enabled())
obj->outputStep(type);
/**
* This is one of three locations where we explicitly flush the output buffers during a
* simulation:
* PetscOutput::petscNonlinearOutput()
* PetscOutput::petscLinearOutput()
* OutputWarehouse::outputStep()
*
* All other Console output _should_ be using newlines to avoid covering buffer errors
* and to avoid excessive I/O
*/
flushConsoleBuffer();
// Reset force output flag
_force_output = false;
}
void
OutputWarehouse::meshChanged()
{
for (const auto & obj : _all_objects)
obj->meshChanged();
}
void
OutputWarehouse::mooseConsole()
{
// Loop through all Console Output objects and pass the current output buffer
std::vector<Console *> objects = getOutputs<Console>();
if (!objects.empty())
{
for (const auto & obj : objects)
obj->mooseConsole(_console_buffer.str());
// Reset
_console_buffer.clear();
_console_buffer.str("");
}
else
{
if (!_buffer_action_console_outputs)
{
// this will cause messages to console before its construction immediately flushed and
// cleared.
std::string message = _console_buffer.str();
bool this_message_ends_in_newline = message.empty() ? true : message.back() == '\n';
// If that last message ended in newline then this one may need
// to start with indenting
if (_last_message_ended_in_newline && _app.multiAppLevel() > 0)
MooseUtils::indentMessage(_app.name(), message);
Moose::out << message << std::flush;
_console_buffer.clear();
_console_buffer.str("");
_last_message_ended_in_newline = this_message_ends_in_newline;
}
}
_num_printed++;
}
void
OutputWarehouse::flushConsoleBuffer()
{
if (!_console_buffer.str().empty())
mooseConsole();
}
void
OutputWarehouse::setFileNumbers(std::map<std::string, unsigned int> input, unsigned int offset)
{
for (const auto & obj : _all_objects)
{
FileOutput * ptr = dynamic_cast<FileOutput *>(obj);
if (ptr != NULL)
{
std::map<std::string, unsigned int>::const_iterator it = input.find(ptr->name());
if (it != input.end())
{
int value = it->second + offset;
if (value < 0)
ptr->setFileNumber(0);
else
ptr->setFileNumber(it->second + offset);
}
}
}
}
std::map<std::string, unsigned int>
OutputWarehouse::getFileNumbers()
{
std::map<std::string, unsigned int> output;
for (const auto & obj : _all_objects)
{
FileOutput * ptr = dynamic_cast<FileOutput *>(obj);
if (ptr != NULL)
output[ptr->name()] = ptr->getFileNumber();
}
return output;
}
void
OutputWarehouse::setCommonParameters(InputParameters * params_ptr)
{
_common_params_ptr = params_ptr;
}
InputParameters *
OutputWarehouse::getCommonParameters()
{
return _common_params_ptr;
}
std::set<Real> &
OutputWarehouse::getSyncTimes()
{
return _sync_times;
}
void
OutputWarehouse::addInterfaceHideVariables(const std::string & output_name,
const std::set<std::string> & variable_names)
{
_interface_map[output_name].insert(variable_names.begin(), variable_names.end());
}
void
OutputWarehouse::buildInterfaceHideVariables(const std::string & output_name,
std::set<std::string> & hide)
{
std::map<std::string, std::set<std::string>>::const_iterator it =
_interface_map.find(output_name);
if (it != _interface_map.end())
hide = it->second;
}
void
OutputWarehouse::checkOutputs(const std::set<OutputName> & names)
{
for (const auto & name : names)
if (!isReservedName(name) && !hasOutput(name))
mooseError("The output object '", name, "' is not a defined output object");
}
const std::set<std::string> &
OutputWarehouse::getReservedNames() const
{
return _reserved;
}
bool
OutputWarehouse::isReservedName(const std::string & name)
{
return _reserved.find(name) != _reserved.end();
}
void
OutputWarehouse::setOutputExecutionType(ExecFlagType type)
{
_output_exec_flag = type;
}
void
OutputWarehouse::allowOutput(bool state)
{
for (const auto & obj : _all_objects)
obj->allowOutput(state);
}
void
OutputWarehouse::forceOutput()
{
_force_output = true;
}
void
OutputWarehouse::reset()
{
for (const auto & pair : _object_map)
{
auto * table = dynamic_cast<TableOutput *>(pair.second);
if (table != NULL)
table->clear();
auto * exodus = dynamic_cast<Exodus *>(pair.second);
if (exodus != NULL)
exodus->clear();
}
}