www.mooseframework.org
OutputWarehouse.h
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 #pragma once
11 
12 // MOOSE includes
13 #include "Output.h"
14 #include "PerfGraphInterface.h"
15 
16 // System includes
17 #include <atomic>
18 
19 // Forward declarations
20 class FEProblemBase;
21 class InputParameters;
22 
27 {
28 public:
33 
34  /*
35  * Class destructor
36  * The OutputWarehouse deletes all output objects passed in via addOutput
37  */
38  virtual ~OutputWarehouse();
39 
46  void addOutput(std::shared_ptr<Output> output);
47 
59  const std::set<OutputName> & getOutputNames();
60 
65  bool hasOutput(const std::string & name) const;
66 
70  void meshChanged();
71 
85  void buildInterfaceHideVariables(const std::string & output_name, std::set<std::string> & hide);
86 
90  void setFileNumbers(std::map<std::string, unsigned int> input, unsigned int offset = 0);
91 
96  std::map<std::string, unsigned int> getFileNumbers();
97 
104  void setCommonParameters(const InputParameters * params_ptr);
105 
110  const InputParameters * getCommonParameters() const;
111 
115  std::set<Real> & getSyncTimes();
116 
123  void checkOutputs(const std::set<OutputName> & names);
124 
131  template <typename T>
132  T * getOutput(const OutputName & name);
133 
140  template <typename T>
141  std::vector<T *> getOutputs(const std::vector<OutputName> & names);
142 
148  template <typename T>
149  std::vector<T *> getOutputs() const;
150 
156  template <typename T>
157  std::vector<OutputName> getOutputNames();
158 
163  const std::set<std::string> & getReservedNames() const;
164 
170  bool isReservedName(const std::string & name);
171 
175  void mooseConsole();
176 
180  void mooseConsole(std::ostringstream & buffer);
181 
186  std::ostringstream & consoleBuffer() { return _console_buffer; }
187 
193  {
195  }
196 
198  void reset();
199 
209  void solveSetup();
210 
212  unsigned long long int numPrinted() const { return _num_printed; }
213 
214 private:
221  void outputStep(ExecFlagType type);
222 
224 
229  void allowOutput(bool state);
230  template <typename T>
231  void allowOutput(bool state);
233 
239  void forceOutput();
240 
245  std::vector<std::shared_ptr<Output>> _all_ptrs;
246 
255  void addOutputFilename(const OutputName & obj_name, const OutFileBase & filename);
256 
261  void initialSetup();
262 
267  void timestepSetup();
268 
273  void customSetup(const ExecFlagType & exec_type);
274 
279  void jacobianSetup();
280 
285  void residualSetup();
286 
291  void subdomainSetup();
292 
301  void addInterfaceHideVariables(const std::string & output_name,
302  const std::set<std::string> & variable_names);
303 
310 
316  void flushConsoleBuffer();
317 
321  void resetFileBase();
322 
325 
327  std::vector<Output *> _all_objects;
328 
331 
333  std::map<OutputName, Output *> _object_map;
334 
336  std::set<OutputName> _object_names;
337 
339  std::map<OutputName, std::set<OutFileBase>> _file_base_map;
340 
343 
345  std::set<Real> _sync_times;
346 
348  std::string _input_file_name;
349 
351  std::map<OutputName, std::set<AuxVariableName>> _material_output_map;
352 
354  std::set<AuxVariableName> _all_material_output_variables;
355 
357  std::set<std::string> _reserved;
358 
360  std::ostringstream _console_buffer;
361 
363  std::map<std::string, std::set<std::string>> _interface_map;
364 
367 
370 
373 
375  const std::ostringstream * _last_buffer;
376 
378  std::atomic<unsigned long long int> _num_printed;
379 
380  // Allow complete access:
381  // FEProblemBase for calling initial, timestepSetup, outputStep, etc. methods
382  friend class FEProblemBase;
383 
384  // MaterialOutputAction for calling addInterfaceHideVariables
385  friend class MaterialOutputAction;
386 
387  // OutputInterface for calling addInterfaceHideVariables
388  friend class OutputInterface;
389 
390  // Console for calling flushConsoleBuffer()
391  friend class PetscOutputInterface;
392 
393  // MooseApp for resetFileBase()
394  friend class MooseApp;
395 };
396 
397 template <typename T>
398 T *
399 OutputWarehouse::getOutput(const OutputName & name)
400 {
401  // Check that the object exists
402  if (!hasOutput(name))
403  mooseError("An output object with the name '", name, "' does not exist.");
404 
405  // Attempt to cast the object to the correct type
406  T * output = dynamic_cast<T *>(_object_map[name]);
407 
408  // Error if the cast fails
409  if (output == NULL)
410  mooseError("An output object with the name '", name, "' for the specified type does not exist");
411 
412  // Return the object
413  return output;
414 }
415 
416 template <typename T>
417 std::vector<T *>
418 OutputWarehouse::getOutputs(const std::vector<OutputName> & names)
419 {
420  // The vector to output
421  std::vector<T *> outputs;
422 
423  // Populate the vector
424  for (std::vector<OutputName>::const_iterator it = names.begin(); it != names.end(); ++it)
425  outputs.push_back(getOutput<T>(*it));
426 
427  // Return the objects
428  return outputs;
429 }
430 
431 template <typename T>
432 std::vector<T *>
434 {
435  // The vector to output
436  std::vector<T *> outputs;
437 
438  // Populate the vector
439  for (std::map<OutputName, Output *>::const_iterator it = _object_map.begin();
440  it != _object_map.end();
441  ++it)
442  {
443  T * output = dynamic_cast<T *>(it->second);
444  if (output != NULL)
445  outputs.push_back(output);
446  }
447 
448  // Return the objects
449  return outputs;
450 }
451 
452 template <typename T>
453 std::vector<OutputName>
455 {
456  // The output vector
457  std::vector<OutputName> names;
458 
459  // Loop through the objects and store the name if the type cast succeeds
460  for (std::map<OutputName, Output *>::const_iterator it = _object_map.begin();
461  it != _object_map.end();
462  ++it)
463  {
464  T * output = dynamic_cast<T *>(it->second);
465  if (output != NULL)
466  names.push_back(it->first);
467  }
468 
469  // Return the names
470  return names;
471 }
472 
473 template <typename T>
474 void
476 {
477  std::vector<T *> outputs = getOutputs<T>();
478  for (typename std::vector<T *>::iterator it = outputs.begin(); it != outputs.end(); ++it)
479  (*it)->allowOutput(state);
480 }
std::string name(const ElemQuality q)
T * getOutput(const OutputName &name)
Return an Output object by name.
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 (.
bool _buffer_action_console_outputs
True to buffer console outputs in actions.
void initialSetup()
Calls the initialSetup function for each of the output objects.
unsigned long long int numPrinted() const
The number of times something has been printed.
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::set< OutputName > _object_names
A set of output names.
bool isReservedName(const std::string &name)
Test if the given name is reserved.
A class to provide an common interface to objects requiring "outputs" option.
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...
void customSetup(const ExecFlagType &exec_type)
Calls the setup function for each of the output objects.
void setOutputExecutionType(ExecFlagType type)
Sets the execution flag type.
std::vector< Output * > _all_objects
All instances of objects (raw pointers)
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
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.
std::vector< T * > getOutputs() const
Return a vector of objects of a given type.
Creates AuxVariables and AuxKernels for automatic output of material properties.
void jacobianSetup()
Calls the jacobianSetup function for each of the output objects.
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.
std::set< AuxVariableName > _all_material_output_variables
List of all variable created by auto material output.
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()
std::ostringstream & consoleBuffer()
The buffered messages stream for Console objects.
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.
Class for storing and utilizing output objects.
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.
std::string _input_file_name
Input file name for this output object.
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.
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.
void addInterfaceHideVariables(const std::string &output_name, const std::set< std::string > &variable_names)
Insert variable names for hiding via the OutoutInterface.
void bufferConsoleOutputsBeforeConstruction(bool buffer)
Set if the outputs to Console before its construction are to be buffered or to screen directly...
void buildInterfaceHideVariables(const std::string &output_name, std::set< std::string > &hide)
Return the list of hidden variables for the given output name.
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::map< OutputName, std::set< AuxVariableName > > _material_output_map
Map of output name and AuxVariable names to be output (used by auto Material output) ...
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.