www.mooseframework.org
FileOutput.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 // C POSIX includes
16 #include <sys/stat.h>
17 
18 // MOOSE includes
19 #include "FileOutput.h"
20 #include "MooseApp.h"
21 #include "FEProblem.h"
22 
23 #include <unistd.h>
24 #include <time.h>
25 
26 template <>
29 {
30  // Create InputParameters object for this stand-alone object
32  params.addParam<std::string>("file_base",
33  "The desired solution output name without an extension");
34  params.addParam<bool>(
35  "append_date", false, "When true the date and time are appended to the output filename.");
36  params.addParam<std::string>("append_date_format",
37  "The format of the date/time to append, if not given UTC format "
38  "used (see http://www.cplusplus.com/reference/ctime/strftime).");
39  // Add the padding option and list it as 'Advanced'
40  params.addParam<unsigned int>(
41  "padding", 4, "The number of for extension suffix (e.g., out.e-s002)");
42  params.addParam<std::vector<std::string>>("output_if_base_contains",
43  std::vector<std::string>(),
44  "If this is supplied then output will only be done in "
45  "the case that the output base contains one of these "
46  "strings. This is helpful in outputting only a subset "
47  "of outputs when using MultiApps.");
48  params.addParamNamesToGroup("padding output_if_base_contains", "Advanced");
49 
50  return params;
51 }
52 
54  : PetscOutput(parameters),
55  _file_num(declareRecoverableData<unsigned int>("file_num", 0)),
56  _padding(getParam<unsigned int>("padding")),
57  _output_if_base_contains(parameters.get<std::vector<std::string>>("output_if_base_contains"))
58 {
59  // If restarting reset the file number
60  if (_app.isRestarting())
61  _file_num = 0;
62 
63  // Set the file base
64  if (isParamValid("file_base"))
65  _file_base = getParam<std::string>("file_base");
66  else if (getParam<bool>("_built_by_moose"))
68  else
70 
71  // Append the date/time
72  if (getParam<bool>("append_date"))
73  {
74  std::string format;
75  if (isParamValid("append_date_format"))
76  format = getParam<std::string>("append_date_format");
77  else
78  format = "%Y-%m-%dT%T%z";
79 
80  // Get the current time
81  time_t now;
82  ::time(&now); // need :: to avoid confusion with time() method of Output class
83 
84  // Format the time
85  char buffer[80];
86  strftime(buffer, 80, format.c_str(), localtime(&now));
87  _file_base += "_";
88  _file_base += buffer;
89  }
90 
91  // Check the file directory of file_base and create if needed
92  std::string base = "./" + _file_base;
93  base = base.substr(0, base.find_last_of('/'));
94 
95  if (_app.processor_id() == 0 && access(base.c_str(), W_OK) == -1)
96  {
97  // Directory does not exist. Loop through incremental directories and create as needed.
98  std::vector<std::string> path_names;
99  MooseUtils::tokenize(base, path_names);
100  std::string inc_path = path_names[0];
101  for (unsigned int i = 1; i < path_names.size(); ++i)
102  {
103  inc_path += '/' + path_names[i];
104  if (access(inc_path.c_str(), W_OK) == -1)
105  if (mkdir(inc_path.c_str(), S_IRWXU | S_IRGRP) == -1)
106  mooseError("Could not create directory: " + inc_path + " for file base: " + _file_base);
107  }
108  }
109 }
110 
111 std::string
112 FileOutput::getOutputFileBase(MooseApp & app, std::string suffix)
113 {
114  // If the App has an outputfile, then use it (MultiApp scenario)
115  if (!app.getOutputFileBase().empty())
116  return app.getOutputFileBase();
117 
118  // If the output base is not set it must be determined from the input file
119  /* This will only return a non-empty string if the setInputFileName() was called, which is
120  * generally not the case. One exception is when CoupledExecutioner is used */
121  std::string input_filename = app.getInputFileName();
122  if (input_filename.empty())
123  input_filename = app.getFileName();
124 
125  // Assert that the filename is not empty
126  mooseAssert(!input_filename.empty(), "Input Filename is NULL");
127 
128  // Determine location of "." in extension, assert if it is not found
129  size_t pos = input_filename.find_last_of('.');
130  mooseAssert(pos != std::string::npos, "Unable to determine suffix of input file name");
131 
132  // Append the "_out" to the name and return it
133  return input_filename.substr(0, pos) + suffix;
134 }
135 
136 bool
138 {
139  if (!checkFilename())
140  return false;
141  return Output::shouldOutput(type);
142 }
143 
144 bool
146 {
147  // Return true if 'output_if_base_contains' is not utilized
148  if (_output_if_base_contains.empty())
149  return true;
150 
151  // Assumed output is false
152  bool output = false;
153 
154  // Loop through each string in the list
155  for (const auto & search_string : _output_if_base_contains)
156  {
157  // Search for the string in the file base, if found set the output to true and break the loop
158  if (_file_base.find(search_string) != std::string::npos)
159  {
160  output = true;
161  break;
162  }
163  }
164 
165  // Return the value
166  return output;
167 }
168 
169 std::string
171 {
172  return _file_base;
173 }
174 
175 void
176 FileOutput::setFileNumber(unsigned int num)
177 {
178  _file_num = num;
179 }
180 
181 unsigned int
183 {
184  return _file_num;
185 }
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:47
virtual std::string filename()
The filename for the output file.
Definition: FileOutput.C:170
void tokenize(const std::string &str, std::vector< T > &elements, unsigned int min_len=1, const std::string &delims="/")
This function will split the passed in string on a set of delimiters appending the substrings to the ...
Definition: MooseUtils.h:350
InputParameters validParams< FileOutput >()
Definition: FileOutput.C:28
virtual bool shouldOutput(const ExecFlagType &type) override
Checks if the output method should be executed.
Definition: FileOutput.C:137
static std::string getOutputFileBase(MooseApp &app, std::string suffix="_out")
Returns the default output file base.
Definition: FileOutput.C:112
virtual Real time() override
Get the output time.
Definition: PetscOutput.C:258
virtual void output(const ExecFlagType &type)=0
Overload this function with the desired output activities.
virtual bool shouldOutput(const ExecFlagType &type)
Handles logic for determining if a step should be output.
Definition: Output.C:164
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
std::string _file_base
The base filename from the input paramaters.
Definition: FileOutput.h:87
std::string getOutputFileBase()
Override the selection of the output file base name.
Definition: MooseApp.C:536
void setFileNumber(unsigned int num)
Sets the file number manually.
Definition: FileOutput.C:176
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:67
MatType type
InputParameters validParams< PetscOutput >()
Definition: PetscOutput.C:25
std::vector< std::string > _output_if_base_contains
Storage for &#39;output_if_base_contains&#39;.
Definition: FileOutput.h:96
std::string getInputFileName()
Returns the input file name that was set with setInputFileName.
Definition: MooseApp.h:144
MooseApp & _app
The MooseApp this object is associated with.
Definition: MooseObject.h:108
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
void mooseError(Args &&...args) const
Definition: MooseObject.h:80
unsigned int & _file_num
A file number counter, initialized to 0 (this must be controlled by the child class, see Exodus)
Definition: FileOutput.h:90
ExecFlagType
Execution flags - when is the object executed/evaluated.
Definition: MooseTypes.h:90
bool isRestarting() const
Whether or not this is a "restart" calculation.
Definition: MooseApp.C:613
FileOutput(const InputParameters &parameters)
Class constructor.
Definition: FileOutput.C:53
Adds the ability to output on every nonlinear and/or linear residual.
Definition: PetscOutput.h:30
bool checkFilename()
Checks the filename for output Checks the output against the &#39;output_if_base_contians&#39; list...
Definition: FileOutput.C:145
std::string getFileName(bool stripLeadingPath=true) const
Return the filename that was parsed.
Definition: MooseApp.C:834
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
This method takes a space delimited list of parameter names and adds them to the specified group name...