www.mooseframework.org
OutputWarehouse.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 // MOOSE includes
11 #include "OutputWarehouse.h"
12 #include "Output.h"
13 #include "Console.h"
14 #include "FileOutput.h"
15 #include "Checkpoint.h"
16 #include "FEProblem.h"
17 #include "TableOutput.h"
18 #include "Exodus.h"
19 
20 #include <libgen.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 
26  : _app(app),
27  _buffer_action_console_outputs(false),
28  _output_exec_flag(EXEC_CUSTOM),
29  _force_output(false),
30  _logging_requested(false)
31 {
32  // Set the reserved names
33  _reserved.insert("none"); // allows 'none' to be used as a keyword in 'outputs' parameter
34  _reserved.insert("all"); // allows 'all' to be used as a keyword in 'outputs' parameter
35 }
36 
38 {
39  // If the output buffer is not empty, it needs to be written
40  if (_console_buffer.str().length())
41  mooseConsole();
42 }
43 
44 void
46 {
47  for (const auto & obj : _all_objects)
48  obj->initialSetup();
49 }
50 
51 void
53 {
54  for (const auto & obj : _all_objects)
55  obj->timestepSetup();
56 }
57 
58 void
60 {
61  for (const auto & obj : _all_objects)
62  obj->solveSetup();
63 }
64 
65 void
67 {
68  for (const auto & obj : _all_objects)
69  obj->jacobianSetup();
70 }
71 
72 void
74 {
75  for (const auto & obj : _all_objects)
76  obj->residualSetup();
77 }
78 
79 void
81 {
82  for (const auto & obj : _all_objects)
83  obj->subdomainSetup();
84 }
85 
86 void
87 OutputWarehouse::addOutput(std::shared_ptr<Output> & output)
88 {
89  _all_ptrs.push_back(output);
90 
91  // Add the object to the warehouse storage, Checkpoint placed at end so they are called last
92  Checkpoint * cp = dynamic_cast<Checkpoint *>(output.get());
93  if (cp != NULL)
94  _all_objects.push_back(output.get());
95  else
96  _all_objects.insert(_all_objects.begin(), output.get());
97 
98  // Store the name and pointer
99  _object_map[output->name()] = output.get();
100  _object_names.insert(output->name());
101 
102  // If the output object is a FileOutput then store the output filename
103  FileOutput * ptr = dynamic_cast<FileOutput *>(output.get());
104  if (ptr != NULL)
105  addOutputFilename(ptr->filename());
106 
107  // Insert object sync times to the global set
108  if (output->parameters().isParamValid("sync_times"))
109  {
110  std::vector<Real> sync_times = output->parameters().get<std::vector<Real>>("sync_times");
111  _sync_times.insert(sync_times.begin(), sync_times.end());
112  }
113 }
114 
115 bool
116 OutputWarehouse::hasOutput(const std::string & name) const
117 {
118  return _object_map.find(name) != _object_map.end();
119 }
120 
121 const std::set<OutputName> &
123 {
124  if (_object_names.empty() && _app.actionWarehouse().hasActions("add_output"))
125  {
126  const auto & actions = _app.actionWarehouse().getActionListByName("add_output");
127  for (const auto & act : actions)
128  _object_names.insert(act->name());
129  }
130  return _object_names;
131 }
132 
133 void
134 OutputWarehouse::addOutputFilename(const OutFileBase & filename)
135 {
136  if (_file_base_set.find(filename) != _file_base_set.end())
137  mooseError("An output file with the name, ", filename, ", already exists.");
138  _file_base_set.insert(filename);
139 }
140 
141 void
143 {
144  if (_force_output)
145  type = EXEC_FORCED;
146 
147  for (const auto & obj : _all_objects)
148  if (obj->enabled())
149  obj->outputStep(type);
150 
162 
163  // Reset force output flag
164  _force_output = false;
165 }
166 
167 void
169 {
170  for (const auto & obj : _all_objects)
171  obj->meshChanged();
172 }
173 
174 void
176 {
177  // Loop through all Console Output objects and pass the current output buffer
178  std::vector<Console *> objects = getOutputs<Console>();
179  if (!objects.empty())
180  {
181  for (const auto & obj : objects)
182  obj->mooseConsole(_console_buffer.str());
183 
184  // Reset
185  _console_buffer.clear();
186  _console_buffer.str("");
187  }
188  else
189  {
191  {
192  // this will cause messages to console before its construction immediately flushed and
193  // cleared.
194  std::string message = _console_buffer.str();
195  if (_app.multiAppLevel() > 0)
196  MooseUtils::indentMessage(_app.name(), message);
197  Moose::out << message << std::flush;
198  _console_buffer.clear();
199  _console_buffer.str("");
200  }
201  }
202 }
203 
204 void
206 {
207  if (!_console_buffer.str().empty())
208  mooseConsole();
209 }
210 
211 void
212 OutputWarehouse::setFileNumbers(std::map<std::string, unsigned int> input, unsigned int offset)
213 {
214  for (const auto & obj : _all_objects)
215  {
216  FileOutput * ptr = dynamic_cast<FileOutput *>(obj);
217  if (ptr != NULL)
218  {
219  std::map<std::string, unsigned int>::const_iterator it = input.find(ptr->name());
220  if (it != input.end())
221  {
222  int value = it->second + offset;
223  if (value < 0)
224  ptr->setFileNumber(0);
225  else
226  ptr->setFileNumber(it->second + offset);
227  }
228  }
229  }
230 }
231 
232 std::map<std::string, unsigned int>
234 {
235 
236  std::map<std::string, unsigned int> output;
237  for (const auto & obj : _all_objects)
238  {
239  FileOutput * ptr = dynamic_cast<FileOutput *>(obj);
240  if (ptr != NULL)
241  output[ptr->name()] = ptr->getFileNumber();
242  }
243  return output;
244 }
245 
246 void
248 {
249  _common_params_ptr = params_ptr;
250 }
251 
254 {
255  return _common_params_ptr;
256 }
257 
258 std::set<Real> &
260 {
261  return _sync_times;
262 }
263 
264 void
265 OutputWarehouse::addInterfaceHideVariables(const std::string & output_name,
266  const std::set<std::string> & variable_names)
267 {
268  _interface_map[output_name].insert(variable_names.begin(), variable_names.end());
269 }
270 
271 void
272 OutputWarehouse::buildInterfaceHideVariables(const std::string & output_name,
273  std::set<std::string> & hide)
274 {
275  std::map<std::string, std::set<std::string>>::const_iterator it =
276  _interface_map.find(output_name);
277  if (it != _interface_map.end())
278  hide = it->second;
279 }
280 
281 void
282 OutputWarehouse::checkOutputs(const std::set<OutputName> & names)
283 {
284  for (const auto & name : names)
285  if (!isReservedName(name) && !hasOutput(name))
286  mooseError("The output object '", name, "' is not a defined output object");
287 }
288 
289 const std::set<std::string> &
291 {
292  return _reserved;
293 }
294 
295 bool
296 OutputWarehouse::isReservedName(const std::string & name)
297 {
298  return _reserved.find(name) != _reserved.end();
299 }
300 
301 void
303 {
305 }
306 
307 void
309 {
310  for (const auto & obj : _all_objects)
311  obj->allowOutput(state);
312 }
313 
314 void
316 {
317  _force_output = true;
318 }
319 
320 void
322 {
323  for (const auto & pair : _object_map)
324  {
325  auto * table = dynamic_cast<TableOutput *>(pair.second);
326  if (table != NULL)
327  table->clear();
328  auto * exodus = dynamic_cast<Exodus *>(pair.second);
329  if (exodus != NULL)
330  exodus->clear();
331  }
332 }
const std::string & name() const
Get the name of the object.
Definition: MooseApp.h:73
void outputStep(ExecFlagType type)
Calls the outputStep method for each output object.
virtual std::string filename()
The filename for the output file.
Definition: FileOutput.C:172
bool _buffer_action_console_outputs
True to buffer console outputs in actions.
void initialSetup()
Calls the initialSetup function for each of the output objects.
const ExecFlagType EXEC_CUSTOM
const ExecFlagType EXEC_FORCED
std::map< OutputName, Output * > _object_map
A map of the output pointers.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
void addOutput(std::shared_ptr< Output > &output)
Adds an existing output object to the warehouse.
void setCommonParameters(InputParameters *params_ptr)
Stores the common InputParameters object.
void indentMessage(const std::string &prefix, std::string &message, const char *color=COLOR_CYAN)
Indents the supplied message given the prefix and color.
Definition: MooseUtils.C:431
std::set< OutputName > _object_names
A set of output names.
unsigned int multiAppLevel() const
The MultiApp Level.
Definition: MooseApp.h:517
bool isReservedName(const std::string &name)
Test if the given name is reserved.
bool hasOutput(const std::string &name) const
Returns true if the output object exists.
Base class for MOOSE-based applications.
Definition: MooseApp.h:59
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
unsigned int getFileNumber()
Return the current file number for this outputter.
Definition: FileOutput.C:184
void clear()
Reset Exodus output.
Definition: Exodus.h:103
const std::list< Action * > & getActionListByName(const std::string &task) const
Retrieve a constant list of Action pointers associated with the passed in task.
void setOutputExecutionType(ExecFlagType type)
Sets the execution flag type.
std::vector< Output * > _all_objects
All instances of objects (raw pointers)
void forceOutput()
Indicates that the next call to outputStep should be forced This is private, users should utilize FEP...
std::set< std::string > _reserved
List of reserved names.
void jacobianSetup()
Calls the jacobianSetup function for each of the output objects.
void setFileNumber(unsigned int num)
Sets the file number manually.
Definition: FileOutput.C:178
std::map< std::string, std::set< std::string > > _interface_map
Storage for variables to hide as prescribed by the object via the OutputInterface.
InputParameters * _common_params_ptr
Pointer to the common InputParameters (.
ExecFlagType _output_exec_flag
The current output execution flag.
MooseApp & _app
MooseApp.
OutputWarehouse(MooseApp &app)
Class constructor.
void addOutputFilename(const OutFileBase &filename)
Adds the file name to the list of filenames being output The main function of this object is to test ...
std::map< std::string, unsigned int > getFileNumbers()
Extracts the file numbers from the output objects.
void meshChanged()
Calls the meshChanged method for every output object.
bool _force_output
Flag indicating that next call to outputStep is forced.
virtual ~OutputWarehouse()
Class for output data to the ExodusII format.
Definition: Exodus.h:30
void clear()
Definition: TableOutput.C:221
ActionWarehouse & actionWarehouse()
Definition: MooseApp.h:146
void checkOutputs(const std::set< OutputName > &names)
Test that the output names exist.
std::ostringstream _console_buffer
The stream for holding messages passed to _console prior to Output object construction.
std::vector< std::shared_ptr< Output > > _all_ptrs
We are using std::shared_ptr to handle the cleanup of the pointers at the end of execution.
void residualSetup()
Calls the residualSetup function for each of the output objects.
void solveSetup()
Calls the timestepSetup function for each of the output objects.
const std::set< OutputName > & getOutputNames()
Get a complete set of all output object names.
MatType type
InputParameters * getCommonParameters()
Get a reference to the common output parameters.
bool hasActions(const std::string &task) const
Check if Actions associated with passed in task exist.
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:21
void subdomainSetup()
Calls the subdomainSetup function for each of the output objects.
std::set< OutFileBase > _file_base_set
List of object names.
void allowOutput(bool state)
Ability to enable/disable output calls This is private, users should utilize FEProblemBase::allowOutp...
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:59
Base class for scalar variables and postprocessors output objects.
Definition: TableOutput.h:33
void mooseConsole()
Send current output buffer to Console output objects.
void reset()
Reset the output system.
void addInterfaceHideVariables(const std::string &output_name, const std::set< std::string > &variable_names)
Insert variable names for hiding via the OutoutInterface.
void buildInterfaceHideVariables(const std::string &output_name, std::set< std::string > &hide)
Return the list of hidden variables for the given output name.
An outputter with filename support.
Definition: FileOutput.h:26
const std::set< std::string > & getReservedNames() const
Return a set of reserved output names.
void setFileNumbers(std::map< std::string, unsigned int > input, unsigned int offset=0)
Calls the setFileNumber method for every FileOutput output object.
std::set< Real > & getSyncTimes()
Return the sync times for all objects.
void timestepSetup()
Calls the timestepSetup function for each of the output objects.
void flushConsoleBuffer()
If content exists in the buffer, write it.
std::set< Real > _sync_times
Sync times for all objects.