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  : PerfGraphInterface(app, "OutputWarehouse"),
27  _app(app),
28  _buffer_action_console_outputs(false),
29  _common_params_ptr(NULL),
30  _output_exec_flag(EXEC_CUSTOM),
31  _force_output(false),
32  _last_message_ended_in_newline(true),
33  _last_buffer(NULL),
34  _num_printed(0)
35 {
36  // Set the reserved names
37  _reserved.insert("none"); // allows 'none' to be used as a keyword in 'outputs' parameter
38  _reserved.insert("all"); // allows 'all' to be used as a keyword in 'outputs' parameter
39 }
40 
42 {
43  // If the output buffer is not empty, it needs to be written
44  if (_console_buffer.str().length())
45  mooseConsole();
46 }
47 
48 void
50 {
51  TIME_SECTION("initialSetup", 5, "Setting Up Outputs");
52 
53  resetFileBase();
54 
55  for (const auto & obj : _all_objects)
56  obj->initialSetup();
57 }
58 
59 void
61 {
62  for (const auto & obj : _all_objects)
63  obj->timestepSetup();
64 }
65 
66 void
68 {
69  for (const auto & obj : _all_objects)
70  obj->customSetup(exec_type);
71 }
72 
73 void
75 {
76  for (const auto & obj : _all_objects)
77  obj->solveSetup();
78 }
79 
80 void
82 {
83  for (const auto & obj : _all_objects)
84  obj->jacobianSetup();
85 }
86 
87 void
89 {
90  for (const auto & obj : _all_objects)
91  obj->residualSetup();
92 }
93 
94 void
96 {
97  for (const auto & obj : _all_objects)
98  obj->subdomainSetup();
99 }
100 
101 void
102 OutputWarehouse::addOutput(std::shared_ptr<Output> const output)
103 {
104  _all_ptrs.push_back(output);
105 
106  // Add the object to the warehouse storage, Checkpoint placed at end so they are called last
107  Checkpoint * cp = dynamic_cast<Checkpoint *>(output.get());
108  if (cp != NULL)
109  _all_objects.push_back(output.get());
110  else
111  _all_objects.insert(_all_objects.begin(), output.get());
112 
113  // Store the name and pointer
114  _object_map[output->name()] = output.get();
115  _object_names.insert(output->name());
116 
117  // Insert object sync times to the global set
118  const std::set<Real> & sync_times = output->getSyncTimes();
119  _sync_times.insert(sync_times.begin(), sync_times.end());
120 }
121 
122 bool
123 OutputWarehouse::hasOutput(const std::string & name) const
124 {
125  return _object_map.find(name) != _object_map.end();
126 }
127 
128 const std::set<OutputName> &
130 {
131  if (_object_names.empty() && _app.actionWarehouse().hasActions("add_output"))
132  {
133  const auto & actions = _app.actionWarehouse().getActionListByName("add_output");
134  for (const auto & act : actions)
135  _object_names.insert(act->name());
136  }
137  return _object_names;
138 }
139 
140 void
141 OutputWarehouse::addOutputFilename(const OutputName & obj_name, const OutFileBase & filename)
142 {
143  _file_base_map[obj_name].insert(filename);
144  for (const auto & it : _file_base_map)
145  if (it.first != obj_name && it.second.find(filename) != it.second.end())
146  mooseError("An output file with the name, ", filename, ", already exists.");
147 }
148 
149 void
151 {
152  if (_force_output)
153  type = EXEC_FORCED;
154 
155  for (const auto & obj : _all_objects)
156  if (obj->enabled())
157  obj->outputStep(type);
158 
170 
171  // Reset force output flag
172  _force_output = false;
173 }
174 
175 void
177 {
178  for (const auto & obj : _all_objects)
179  obj->meshChanged();
180 }
181 
182 static std::mutex moose_console_mutex;
183 
184 void
186 {
188 }
189 
190 void
191 OutputWarehouse::mooseConsole(std::ostringstream & buffer)
192 {
193  std::lock_guard<std::mutex> lock(moose_console_mutex);
194 
195  std::string message = buffer.str();
196 
197  // If someone else is writing - then we may need a newline
198  if (&buffer != _last_buffer && !_last_message_ended_in_newline)
199  message = '\n' + message;
200 
201  // Loop through all Console Output objects and pass the current output buffer
202  std::vector<Console *> objects = getOutputs<Console>();
203  if (!objects.empty())
204  {
205  for (const auto & obj : objects)
206  obj->mooseConsole(message);
207 
208  // Reset
209  buffer.clear();
210  buffer.str("");
211  }
212  else if (!_app.actionWarehouse().isTaskComplete("add_output"))
213  {
215  {
216  // this will cause messages to console before its construction immediately flushed and
217  // cleared.
218  bool this_message_ends_in_newline = message.empty() ? true : message.back() == '\n';
219 
220  // If that last message ended in newline then this one may need
221  // to start with indenting
222  // Note that we only indent the first line if the last message ended in new line
223  if (_app.multiAppLevel() > 0)
225 
226  Moose::out << message << std::flush;
227  buffer.clear();
228  buffer.str("");
229 
230  _last_message_ended_in_newline = this_message_ends_in_newline;
231  }
232  }
233 
234  _last_buffer = &buffer;
235 
236  _num_printed++;
237 }
238 
239 void
241 {
242  if (!_console_buffer.str().empty())
243  mooseConsole();
244 }
245 
246 void
247 OutputWarehouse::setFileNumbers(std::map<std::string, unsigned int> input, unsigned int offset)
248 {
249  for (const auto & obj : _all_objects)
250  {
251  FileOutput * ptr = dynamic_cast<FileOutput *>(obj);
252  if (ptr != NULL)
253  {
254  std::map<std::string, unsigned int>::const_iterator it = input.find(ptr->name());
255  if (it != input.end())
256  {
257  int value = it->second + offset;
258  if (value < 0)
259  ptr->setFileNumber(0);
260  else
261  ptr->setFileNumber(it->second + offset);
262  }
263  }
264  }
265 }
266 
267 std::map<std::string, unsigned int>
269 {
270 
271  std::map<std::string, unsigned int> output;
272  for (const auto & obj : _all_objects)
273  {
274  FileOutput * ptr = dynamic_cast<FileOutput *>(obj);
275  if (ptr != NULL)
276  output[ptr->name()] = ptr->getFileNumber();
277  }
278  return output;
279 }
280 
281 void
283 {
284  _common_params_ptr = params_ptr;
285 }
286 
287 const InputParameters *
289 {
290  return _common_params_ptr;
291 }
292 
293 std::set<Real> &
295 {
296  return _sync_times;
297 }
298 
299 void
300 OutputWarehouse::addInterfaceHideVariables(const std::string & output_name,
301  const std::set<std::string> & variable_names)
302 {
303  _interface_map[output_name].insert(variable_names.begin(), variable_names.end());
304 }
305 
306 void
307 OutputWarehouse::buildInterfaceHideVariables(const std::string & output_name,
308  std::set<std::string> & hide)
309 {
310  std::map<std::string, std::set<std::string>>::const_iterator it =
311  _interface_map.find(output_name);
312  if (it != _interface_map.end())
313  hide = it->second;
314 }
315 
316 void
317 OutputWarehouse::checkOutputs(const std::set<OutputName> & names)
318 {
319  for (const auto & name : names)
320  if (!isReservedName(name) && !hasOutput(name))
321  mooseError("The output object '", name, "' is not a defined output object");
322 }
323 
324 const std::set<std::string> &
326 {
327  return _reserved;
328 }
329 
330 bool
331 OutputWarehouse::isReservedName(const std::string & name)
332 {
333  return _reserved.find(name) != _reserved.end();
334 }
335 
336 void
338 {
339  _output_exec_flag = type;
340 }
341 
342 void
344 {
345  for (const auto & obj : _all_objects)
346  obj->allowOutput(state);
347 }
348 
349 void
351 {
352  _force_output = true;
353 }
354 
355 void
357 {
358  for (const auto & pair : _object_map)
359  {
360  auto * table = dynamic_cast<TableOutput *>(pair.second);
361  if (table != NULL)
362  table->clear();
363  auto * exodus = dynamic_cast<Exodus *>(pair.second);
364  if (exodus != NULL)
365  exodus->clear();
366  }
367 }
368 
369 void
371 {
372  // Set the file base from the application to FileOutputs and add associated filenames
373  for (const auto & obj : _all_objects)
374  {
375  FileOutput * file_output = dynamic_cast<FileOutput *>(obj);
376  if (file_output)
377  {
378  const std::string file_base = obj->parameters().get<bool>("_built_by_moose")
380  : (_app.getOutputFileBase(true) + "_" + obj->name());
381  file_output->setFileBase(file_base);
382 
383  addOutputFilename(obj->name(), file_output->filename());
384  }
385  }
386 }
std::string name(const ElemQuality q)
const std::string & name() const
Get the name of the object.
Definition: MooseApp.h:106
std::map< OutputName, std::set< OutFileBase > > _file_base_map
List of object names.
void outputStep(ExecFlagType type)
Calls the outputStep method for each output object.
const InputParameters * _common_params_ptr
Pointer to the common InputParameters (.
virtual std::string filename()
The filename for the output file.
Definition: FileOutput.C:112
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
Definition: Moose.C:41
const ExecFlagType EXEC_FORCED
Definition: Moose.C:39
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:284
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
bool isTaskComplete(const std::string &task) const
std::string getOutputFileBase(bool for_non_moose_build_output=false) const
Get the output file base name.
Definition: MooseApp.C:1072
std::set< OutputName > _object_names
A set of output names.
unsigned int multiAppLevel() const
The MultiApp Level.
Definition: MooseApp.h:812
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:69
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:176
void customSetup(const ExecFlagType &exec_type)
Calls the setup function for each of the output objects.
void clear()
Reset Exodus output.
Definition: Exodus.C:524
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)
virtual const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:56
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:170
std::map< std::string, std::set< std::string > > _interface_map
Storage for variables to hide as prescribed by the object via the OutputInterface.
const std::ostringstream * _last_buffer
What the last buffer was that was printed.
ExecFlagType _output_exec_flag
The current output execution flag.
MooseApp & _app
MooseApp.
void resetFileBase()
Resets the file base for all FileOutput objects.
OutputWarehouse(MooseApp &app)
Class constructor.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
void indentMessage(const std::string &prefix, std::string &message, const char *color=COLOR_CYAN, bool dont_indent_first_line=true, const std::string &post_prefix=": ")
Indents the supplied message given the prefix and color.
Definition: MooseUtils.C:721
static std::mutex moose_console_mutex
std::map< std::string, unsigned int > getFileNumbers()
Extracts the file numbers from the output objects.
Writes out three things:
Definition: Checkpoint.h:58
void meshChanged()
Calls the meshChanged method for every output object.
bool _force_output
Flag indicating that next call to outputStep is forced.
void setFileBase(const std::string &file_base)
Sets the file base string if the &#39;file_base&#39; parameter is not set.
Definition: FileOutput.C:118
virtual ~OutputWarehouse()
Class for output data to the ExodusII format.
Definition: Exodus.h:24
void clear()
Definition: TableOutput.C:247
ActionWarehouse & actionWarehouse()
Return a writable reference to the ActionWarehouse associated with this app.
Definition: MooseApp.h:206
void checkOutputs(const std::set< OutputName > &names)
Test that the output names exist.
std::atomic< unsigned long long int > _num_printed
Number of times the stream has been printed to.
Interface for objects interacting with the PerfGraph.
std::ostringstream _console_buffer
The stream for holding messages passed to _console prior to Output object construction.
void addOutputFilename(const OutputName &obj_name, const OutFileBase &filename)
Adds the file name to the map of filenames being output with an associated object The main function o...
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.
bool hasActions(const std::string &task) const
Check if Actions associated with passed in task exist.
const InputParameters * getCommonParameters() const
Get a reference to the common output parameters.
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:18
void subdomainSetup()
Calls the subdomainSetup function for each of the output objects.
void allowOutput(bool state)
Ability to enable/disable output calls This is private, users should utilize FEProblemBase::allowOutp...
void addOutput(std::shared_ptr< Output > output)
Adds an existing output object to the warehouse.
Base class for scalar variables and postprocessors output objects.
Definition: TableOutput.h:28
void mooseConsole()
Send current output buffer to Console output objects.
void reset()
Reset the output system.
void setCommonParameters(const InputParameters *params_ptr)
Stores the common InputParameters object.
const InputParameters & parameters() const
Get the parameters of the object.
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:20
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.
bool _last_message_ended_in_newline
Whether or not the last thing output by mooseConsole had a newline as the last character.
void flushConsoleBuffer()
If content exists in the buffer, write it.
std::set< Real > _sync_times
Sync times for all objects.