https://mooseframework.inl.gov
FileOutput.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
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 #include "libmesh/utility.h"
22 
25 {
26  // Create InputParameters object for this stand-alone object
28  params.addClassDescription("Base class for all file-based output");
29  params.addParam<std::string>(
30  "file_base",
31  "The desired solution output name without an extension. If not provided, MOOSE sets it "
32  "with Outputs/file_base when available. Otherwise, MOOSE uses input file name and this "
33  "object name for a master input or uses master file_base, the subapp name and this object "
34  "name for a subapp input to set it.");
35  params.addParam<std::string>("file_base_suffix", "Suffix to add to the file base");
36  params.addParam<bool>(
37  "append_date", false, "When true the date and time are appended to the output filename.");
38  params.addParam<std::string>("append_date_format",
39  "The format of the date/time to append, if not given UTC format "
40  "is used (see http://www.cplusplus.com/reference/ctime/strftime).");
41  // Add the padding option and list it as 'Advanced'
42  params.addParam<unsigned int>(
43  "padding", 4, "The number of digits for the extension suffix (e.g., out.e-s002)");
44  params.addParam<std::vector<std::string>>("output_if_base_contains",
45  std::vector<std::string>(),
46  "If this is supplied then output will only be done in "
47  "the case that the output base contains one of these "
48  "strings. This is helpful in outputting only a subset "
49  "of outputs when using MultiApps.");
50  params.addParamNamesToGroup(
51  "file_base append_date append_date_format padding output_if_base_contains",
52  "File name customization");
53 
54  return params;
55 }
56 
58  : PetscOutput(parameters),
59  _file_num(declareRecoverableData<unsigned int>("file_num", 0)),
60  _padding(getParam<unsigned int>("padding")),
61  _output_if_base_contains(getParam<std::vector<std::string>>("output_if_base_contains"))
62 {
63  // If restarting reset the file number
64  if (_app.isRestarting())
65  _file_num = 0;
66 
67  if (isParamValid("file_base"))
68  {
69  // Check that we are the only process or not a subapp
70  if (!_app.isUltimateMaster())
71  if (_app.multiAppNumber() > 0)
72  mooseError("The parameter 'file_base' may not be specified for a child app when the "
73  "MultiApp has multiple instances of the child app, since all instances would "
74  "use the same file base and thus write to the same file.");
75  setFileBaseInternal(getParam<std::string>("file_base"));
76  }
77 }
78 
79 bool
81 {
82  if (!checkFilename())
83  return false;
84  return Output::shouldOutput();
85 }
86 
87 bool
89 {
90  // Return true if 'output_if_base_contains' is not utilized
91  if (_output_if_base_contains.empty())
92  return true;
93 
94  // Assumed output is false
95  bool output = false;
96 
97  // Loop through each string in the list
98  for (const auto & search_string : _output_if_base_contains)
99  {
100  // Search for the string in the file base, if found set the output to true and break the loop
101  if (_file_base.find(search_string) != std::string::npos)
102  {
103  output = true;
104  break;
105  }
106  }
107 
108  // Return the value
109  return output;
110 }
111 
112 std::string
114 {
115  return _file_base;
116 }
117 
118 void
119 FileOutput::setFileBase(const std::string & file_base)
120 {
121  if (!isParamValid("file_base"))
122  setFileBaseInternal(file_base);
123 }
124 
125 void
126 FileOutput::setFileBaseInternal(const std::string & file_base)
127 {
128  _file_base = file_base;
129 
130  if (isParamValid("file_base_suffix"))
131  _file_base += "_" + getParam<std::string>("file_base_suffix");
132 
133  // Append the date/time
134  if (getParam<bool>("append_date"))
135  {
136  std::string format;
137  if (isParamValid("append_date_format"))
138  format = getParam<std::string>("append_date_format");
139  else
140  format = "%Y-%m-%dT%T%z";
141 
142  // Get the current time
143  std::time_t now;
144  ::time(&now); // need :: to avoid confusion with time() method of Output class
145 
146  // Format the time
147  char buffer[80];
148  strftime(buffer, 80, format.c_str(), localtime(&now));
149  _file_base += "_";
150  _file_base += buffer;
151  }
152 
153  // Check the file directory of file_base and create if needed
154  std::filesystem::path directory_base = _file_base;
155  directory_base.remove_filename();
156  if (directory_base.empty())
157  directory_base = ".";
158  // ensure relative path
159  directory_base = std::filesystem::relative(std::filesystem::absolute(directory_base));
160 
161  std::filesystem::path possible_dir_to_make;
162  for (auto it = directory_base.begin(); it != directory_base.end(); ++it)
163  {
164  possible_dir_to_make = possible_dir_to_make / *it;
165  const auto dir_string = possible_dir_to_make.generic_string();
166  if (_app.processor_id() == 0 && access(dir_string.c_str(), F_OK) == -1)
167  // Directory does not exist. Create
168  if (Utility::mkdir(dir_string.c_str()) == -1)
169  mooseError("Could not create directory: " + dir_string + " for file base: " + _file_base);
170  }
171 }
172 
173 void
174 FileOutput::setFileNumber(unsigned int num)
175 {
176  _file_num = num;
177 }
178 
179 unsigned int
181 {
182  return _file_num;
183 }
virtual void setFileBaseInternal(const std::string &file_base)
Internal function that sets the file_base.
Definition: FileOutput.C:126
virtual bool shouldOutput() override
Checks if the output method should be executed.
Definition: FileOutput.C:80
bool isUltimateMaster() const
Whether or not this app is the ultimate master app.
Definition: MooseApp.h:837
virtual std::string filename()
The filename for the output file.
Definition: FileOutput.C:113
virtual Real time() override
Get the output time.
Definition: PetscOutput.C:262
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:180
std::string _file_base
The base filename from the input paramaters.
Definition: FileOutput.h:89
processor_id_type processor_id() const
Returns the MPI processor ID of the current processor.
Definition: MooseApp.h:434
bool isRestarting() const
Whether or not this is a "restart" calculation.
Definition: MooseApp.C:1801
unsigned int multiAppNumber() const
The MultiApp number.
Definition: MooseApp.h:832
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
virtual bool shouldOutput()
Handles logic for determining if a step should be output.
Definition: Output.C:277
void setFileNumber(unsigned int num)
Sets the file number manually.
Definition: FileOutput.C:174
static InputParameters validParams()
Definition: PetscOutput.C:125
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:119
static InputParameters validParams()
Definition: FileOutput.C:24
virtual void output()=0
Overload this function with the desired output activities.
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:84
std::vector< std::string > _output_if_base_contains
Storage for &#39;output_if_base_contains&#39;.
Definition: FileOutput.h:86
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional 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:80
FileOutput(const InputParameters &parameters)
Class constructor.
Definition: FileOutput.C:57
Adds the ability to output on every nonlinear and/or linear residual.
Definition: PetscOutput.h:41
bool checkFilename()
Checks the filename for output Checks the output against the &#39;output_if_base_contians&#39; list...
Definition: FileOutput.C:88
void ErrorVector unsigned int
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...