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 #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<bool>(
36  "append_date", false, "When true the date and time are appended to the output filename.");
37  params.addParam<std::string>("append_date_format",
38  "The format of the date/time to append, if not given UTC format "
39  "is used (see http://www.cplusplus.com/reference/ctime/strftime).");
40  // Add the padding option and list it as 'Advanced'
41  params.addParam<unsigned int>(
42  "padding", 4, "The number of digits for the extension suffix (e.g., out.e-s002)");
43  params.addParam<std::vector<std::string>>("output_if_base_contains",
44  std::vector<std::string>(),
45  "If this is supplied then output will only be done in "
46  "the case that the output base contains one of these "
47  "strings. This is helpful in outputting only a subset "
48  "of outputs when using MultiApps.");
49  params.addParamNamesToGroup(
50  "file_base append_date append_date_format padding output_if_base_contains",
51  "File name customization");
52 
53  return params;
54 }
55 
57  : PetscOutput(parameters),
58  _file_num(declareRecoverableData<unsigned int>("file_num", 0)),
59  _padding(getParam<unsigned int>("padding")),
60  _output_if_base_contains(getParam<std::vector<std::string>>("output_if_base_contains"))
61 {
62  // If restarting reset the file number
63  if (_app.isRestarting())
64  _file_num = 0;
65 
66  if (isParamValid("file_base"))
67  {
68  // Check that we are the only process or not a subapp
69  if (!_app.isUltimateMaster())
70  if (_app.multiAppNumber() > 0)
71  mooseError("The parameter 'file_base' may not be specified for a child app when the "
72  "MultiApp has multiple instances of the child app, since all instances would "
73  "use the same file base and thus write to the same file.");
74  setFileBaseInternal(getParam<std::string>("file_base"));
75  }
76 }
77 
78 bool
80 {
81  if (!checkFilename())
82  return false;
83  return Output::shouldOutput();
84 }
85 
86 bool
88 {
89  // Return true if 'output_if_base_contains' is not utilized
90  if (_output_if_base_contains.empty())
91  return true;
92 
93  // Assumed output is false
94  bool output = false;
95 
96  // Loop through each string in the list
97  for (const auto & search_string : _output_if_base_contains)
98  {
99  // Search for the string in the file base, if found set the output to true and break the loop
100  if (_file_base.find(search_string) != std::string::npos)
101  {
102  output = true;
103  break;
104  }
105  }
106 
107  // Return the value
108  return output;
109 }
110 
111 std::string
113 {
114  return _file_base;
115 }
116 
117 void
118 FileOutput::setFileBase(const std::string & file_base)
119 {
120  if (!isParamValid("file_base"))
121  setFileBaseInternal(file_base);
122 }
123 
124 void
125 FileOutput::setFileBaseInternal(const std::string & file_base)
126 {
127  _file_base = file_base;
128 
129  // Append the date/time
130  if (getParam<bool>("append_date"))
131  {
132  std::string format;
133  if (isParamValid("append_date_format"))
134  format = getParam<std::string>("append_date_format");
135  else
136  format = "%Y-%m-%dT%T%z";
137 
138  // Get the current time
139  std::time_t now;
140  ::time(&now); // need :: to avoid confusion with time() method of Output class
141 
142  // Format the time
143  char buffer[80];
144  strftime(buffer, 80, format.c_str(), localtime(&now));
145  _file_base += "_";
146  _file_base += buffer;
147  }
148 
149  // Check the file directory of file_base and create if needed
150  std::filesystem::path directory_base = _file_base;
151  directory_base.remove_filename();
152  if (directory_base.empty())
153  directory_base = ".";
154  // ensure relative path
155  directory_base = std::filesystem::relative(std::filesystem::absolute(directory_base));
156 
157  std::filesystem::path possible_dir_to_make;
158  for (auto it = directory_base.begin(); it != directory_base.end(); ++it)
159  {
160  possible_dir_to_make = possible_dir_to_make / *it;
161  const auto dir_string = possible_dir_to_make.generic_string();
162  if (_app.processor_id() == 0 && access(dir_string.c_str(), F_OK) == -1)
163  // Directory does not exist. Create
164  if (Utility::mkdir(dir_string.c_str()) == -1)
165  mooseError("Could not create directory: " + dir_string + " for file base: " + _file_base);
166  }
167 }
168 
169 void
170 FileOutput::setFileNumber(unsigned int num)
171 {
172  _file_num = num;
173 }
174 
175 unsigned int
177 {
178  return _file_num;
179 }
virtual void setFileBaseInternal(const std::string &file_base)
Internal function that sets the file_base.
Definition: FileOutput.C:125
virtual bool shouldOutput() override
Checks if the output method should be executed.
Definition: FileOutput.C:79
bool isUltimateMaster() const
Whether or not this app is the ultimate master app.
Definition: MooseApp.h:823
virtual std::string filename()
The filename for the output file.
Definition: FileOutput.C:112
virtual Real time() override
Get the output time.
Definition: PetscOutput.C:254
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:176
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:418
bool isRestarting() const
Whether or not this is a "restart" calculation.
Definition: MooseApp.C:1173
unsigned int multiAppNumber() const
The MultiApp number.
Definition: MooseApp.h:818
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:278
void setFileNumber(unsigned int num)
Sets the file number manually.
Definition: FileOutput.C:170
static InputParameters validParams()
Definition: PetscOutput.C:124
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:118
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:69
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 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:80
FileOutput(const InputParameters &parameters)
Class constructor.
Definition: FileOutput.C:56
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:87
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...