www.mooseframework.org
OutputWarehouse.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 // MOOSE includes
16 #include "OutputWarehouse.h"
17 #include "Output.h"
18 #include "Console.h"
19 #include "FileOutput.h"
20 #include "Checkpoint.h"
21 #include "FEProblem.h"
22 
23 #include <libgen.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 
29  : _app(app),
30  _buffer_action_console_outputs(false),
31  _output_exec_flag(EXEC_CUSTOM),
32  _force_output(false),
33  _logging_requested(false)
34 {
35  // Set the reserved names
36  _reserved.insert("none"); // allows 'none' to be used as a keyword in 'outputs' parameter
37  _reserved.insert("all"); // allows 'all' to be used as a keyword in 'outputs' parameter
38 }
39 
41 {
42  // If the output buffer is not empty, it needs to be written
43  if (_console_buffer.str().length())
44  mooseConsole();
45 }
46 
47 void
49 {
50  for (const auto & obj : _all_objects)
51  obj->initialSetup();
52 }
53 
54 void
56 {
57  for (const auto & obj : _all_objects)
58  obj->timestepSetup();
59 }
60 
61 void
63 {
64  for (const auto & obj : _all_objects)
65  obj->solveSetup();
66 }
67 
68 void
70 {
71  for (const auto & obj : _all_objects)
72  obj->jacobianSetup();
73 }
74 
75 void
77 {
78  for (const auto & obj : _all_objects)
79  obj->residualSetup();
80 }
81 
82 void
84 {
85  for (const auto & obj : _all_objects)
86  obj->subdomainSetup();
87 }
88 
89 void
90 OutputWarehouse::addOutput(std::shared_ptr<Output> & output)
91 {
92  _all_ptrs.push_back(output);
93 
94  // Add the object to the warehouse storage, Checkpoint placed at end so they are called last
95  Checkpoint * cp = dynamic_cast<Checkpoint *>(output.get());
96  if (cp != NULL)
97  _all_objects.push_back(output.get());
98  else
99  _all_objects.insert(_all_objects.begin(), output.get());
100 
101  // Store the name and pointer
102  _object_map[output->name()] = output.get();
103  _object_names.insert(output->name());
104 
105  // If the output object is a FileOutput then store the output filename
106  FileOutput * ptr = dynamic_cast<FileOutput *>(output.get());
107  if (ptr != NULL)
108  addOutputFilename(ptr->filename());
109 
110  // Insert object sync times to the global set
111  if (output->parameters().isParamValid("sync_times"))
112  {
113  std::vector<Real> sync_times = output->parameters().get<std::vector<Real>>("sync_times");
114  _sync_times.insert(sync_times.begin(), sync_times.end());
115  }
116 }
117 
118 bool
119 OutputWarehouse::hasOutput(const std::string & name) const
120 {
121  return _object_map.find(name) != _object_map.end();
122 }
123 
124 const std::set<OutputName> &
126 {
127  if (_object_names.empty())
128  {
129  const auto & actions = _app.actionWarehouse().getActionListByName("add_output");
130  for (const auto & act : actions)
131  _object_names.insert(act->name());
132  }
133  return _object_names;
134 }
135 
136 void
137 OutputWarehouse::addOutputFilename(const OutFileBase & filename)
138 {
139  if (_file_base_set.find(filename) != _file_base_set.end())
140  mooseError("An output file with the name, ", filename, ", already exists.");
141  _file_base_set.insert(filename);
142 }
143 
144 void
146 {
147  if (_force_output)
148  type = EXEC_FORCED;
149 
150  for (const auto & obj : _all_objects)
151  if (obj->enabled())
152  obj->outputStep(type);
153 
165 
166  // Reset force output flag
167  _force_output = false;
168 }
169 
170 void
172 {
173  for (const auto & obj : _all_objects)
174  obj->meshChanged();
175 }
176 
177 void
179 {
180  // Loop through all Console Output objects and pass the current output buffer
181  std::vector<Console *> objects = getOutputs<Console>();
182  if (!objects.empty())
183  {
184  for (const auto & obj : objects)
185  obj->mooseConsole(_console_buffer.str());
186 
187  // Reset
188  _console_buffer.clear();
189  _console_buffer.str("");
190  }
191  else
192  {
194  {
195  // this will cause messages to console before its construction immediately flushed and
196  // cleared.
197  std::string message = _console_buffer.str();
198  if (_app.multiAppLevel() > 0)
199  MooseUtils::indentMessage(_app.name(), message);
200  Moose::out << message << std::flush;
201  _console_buffer.clear();
202  _console_buffer.str("");
203  }
204  }
205 }
206 
207 void
209 {
210  if (!_console_buffer.str().empty())
211  mooseConsole();
212 }
213 
214 void
215 OutputWarehouse::setFileNumbers(std::map<std::string, unsigned int> input, unsigned int offset)
216 {
217  for (const auto & obj : _all_objects)
218  {
219  FileOutput * ptr = dynamic_cast<FileOutput *>(obj);
220  if (ptr != NULL)
221  {
222  std::map<std::string, unsigned int>::const_iterator it = input.find(ptr->name());
223  if (it != input.end())
224  {
225  int value = it->second + offset;
226  if (value < 0)
227  ptr->setFileNumber(0);
228  else
229  ptr->setFileNumber(it->second + offset);
230  }
231  }
232  }
233 }
234 
235 std::map<std::string, unsigned int>
237 {
238 
239  std::map<std::string, unsigned int> output;
240  for (const auto & obj : _all_objects)
241  {
242  FileOutput * ptr = dynamic_cast<FileOutput *>(obj);
243  if (ptr != NULL)
244  output[ptr->name()] = ptr->getFileNumber();
245  }
246  return output;
247 }
248 
249 void
251 {
252  _common_params_ptr = params_ptr;
253 }
254 
257 {
258  return _common_params_ptr;
259 }
260 
261 std::set<Real> &
263 {
264  return _sync_times;
265 }
266 
267 void
268 OutputWarehouse::addInterfaceHideVariables(const std::string & output_name,
269  const std::set<std::string> & variable_names)
270 {
271  _interface_map[output_name].insert(variable_names.begin(), variable_names.end());
272 }
273 
274 void
275 OutputWarehouse::buildInterfaceHideVariables(const std::string & output_name,
276  std::set<std::string> & hide)
277 {
278  std::map<std::string, std::set<std::string>>::const_iterator it =
279  _interface_map.find(output_name);
280  if (it != _interface_map.end())
281  hide = it->second;
282 }
283 
284 void
285 OutputWarehouse::checkOutputs(const std::set<OutputName> & names)
286 {
287  for (const auto & name : names)
288  if (!isReservedName(name) && !hasOutput(name))
289  mooseError("The output object '", name, "' is not a defined output object");
290 }
291 
292 const std::set<std::string> &
294 {
295  return _reserved;
296 }
297 
298 bool
299 OutputWarehouse::isReservedName(const std::string & name)
300 {
301  return _reserved.find(name) != _reserved.end();
302 }
303 
304 void
306 {
308 }
309 
310 void
312 {
313  for (const auto & obj : _all_objects)
314  obj->allowOutput(state);
315 }
316 
317 void
319 {
320  _force_output = true;
321 }
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:47
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:170
bool _buffer_action_console_outputs
True to buffer console outputs in actions.
void initialSetup()
Calls the initialSetup function for each of the output objects.
For use with custom executioners that want to fire objects at a specific time.
Definition: MooseTypes.h:110
std::map< OutputName, Output * > _object_map
A map of the output pointers.
unsigned int multiAppLevel() const
The MultiApp Level.
Definition: MooseApp.h:465
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 mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
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:438
std::set< OutputName > _object_names
A set of output names.
const std::list< Action * > & getActionListByName(const std::string &task) const
Retrieve a constant list of Action pointers associated with the passed in task.
bool isReservedName(const std::string &name)
Test if the given name is reserved.
Base class for MOOSE-based applications.
Definition: MooseApp.h:58
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:182
void setOutputExecutionType(ExecFlagType type)
Sets the execution flag type.
std::vector< Output * > _all_objects
All instances of objects (raw pointers)
const std::string & name() const
Get the name of the object.
Definition: MooseApp.h:70
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:176
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 (.
Forces execution to occur (output only)
Definition: MooseTypes.h:106
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()
ActionWarehouse & actionWarehouse()
Definition: MooseApp.h:130
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.
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...
bool hasOutput(const std::string &name) const
Returns true if the output object exists.
void mooseConsole()
Send current output buffer to Console output objects.
ExecFlagType
Execution flags - when is the object executed/evaluated.
Definition: MooseTypes.h:90
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:32
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.
const std::set< std::string > & getReservedNames() const
Return a set of reserved output names.
void flushConsoleBuffer()
If content exists in the buffer, write it.
std::set< Real > _sync_times
Sync times for all objects.