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