https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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 // Captured by FEProblemBase::addOutput(), before the common [Outputs] block's file_base (if
36 // any) is copied down onto this object's own parameters, since that copy makes 'file_base'
37 // appear valid and user-set here even when this object's own block never set it (see #4215).
38 params.addPrivateParam<bool>("_file_base_set_by_own_block", false);
39 params.addParam<std::string>("file_base_suffix", "Suffix to add to the file base");
40 params.addParam<bool>(
41 "append_object_name",
42 true,
43 "Append the output object name to the default file base when using full output syntax.");
44 params.addParam<bool>(
45 "append_date", false, "When true the date and time are appended to the output filename.");
46 params.addParam<std::string>("append_date_format",
47 "The format of the date/time to append, if not given UTC format "
48 "is used (see http://www.cplusplus.com/reference/ctime/strftime).");
49 // Add the padding option and list it as 'Advanced'
50 params.addParam<unsigned int>(
51 "padding", 4, "The number of digits for the extension suffix (e.g., out.e-s002)");
52 params.addParam<std::vector<std::string>>("output_if_base_contains",
53 std::vector<std::string>(),
54 "If this is supplied then output will only be done in "
55 "the case that the output base contains one of these "
56 "strings. This is helpful in outputting only a subset "
57 "of outputs when using MultiApps.");
59 "file_base append_object_name append_date append_date_format padding output_if_base_contains",
60 "File name customization");
61
62 return params;
63}
64
66 : PetscOutput(parameters),
67 _file_num(declareRecoverableData<unsigned int>("file_num", 0)),
68 _padding(getParam<unsigned int>("padding")),
69 _output_if_base_contains(getParam<std::vector<std::string>>("output_if_base_contains"))
70{
71 // If restarting reset the file number
72 if (_app.isRestarting())
73 _file_num = 0;
74
75 if (isParamValid("file_base"))
76 {
77 // Check that we are the only process or not a subapp
79 if (_app.multiAppNumber() > 0)
80 mooseError("The parameter 'file_base' may not be specified for a child app when the "
81 "MultiApp has multiple instances of the child app, since all instances would "
82 "use the same file base and thus write to the same file.");
83 setFileBaseInternal(getParam<std::string>("file_base"));
84 }
85}
86
87bool
89{
90 if (!checkFilename())
91 return false;
92 return Output::shouldOutput();
93}
94
95bool
97{
98 // Return true if 'output_if_base_contains' is not utilized
99 if (_output_if_base_contains.empty())
100 return true;
101
102 // Assumed output is false
103 bool output = false;
104
105 // Loop through each string in the list
106 for (const auto & search_string : _output_if_base_contains)
107 {
108 // Search for the string in the file base, if found set the output to true and break the loop
109 if (_file_base.find(search_string) != std::string::npos)
110 {
111 output = true;
112 break;
113 }
114 }
115
116 // Return the value
117 return output;
118}
119
120std::string
122{
123 return _file_base;
124}
125
126void
127FileOutput::setFileBase(const std::string & file_base)
128{
129 // Only refuse to override this object's file base if its own block explicitly set 'file_base';
130 // 'isParamValid' alone is not enough, since a 'file_base' inherited from a common [Outputs]
131 // block also reads as valid here even though this object's own block never set it (see #4215).
132 if (!getParam<bool>("_file_base_set_by_own_block"))
133 setFileBaseInternal(file_base);
134}
135
136void
137FileOutput::setFileBaseInternal(const std::string & file_base)
138{
139 _file_base = file_base;
140
141 if (isParamValid("file_base_suffix"))
142 _file_base += "_" + getParam<std::string>("file_base_suffix");
143
144 // Append the date/time
145 if (getParam<bool>("append_date"))
146 {
147 std::string format;
148 if (isParamValid("append_date_format"))
149 format = getParam<std::string>("append_date_format");
150 else
151 format = "%Y-%m-%dT%T%z";
152
153 // Get the current time
154 std::time_t now;
155 ::time(&now); // need :: to avoid confusion with time() method of Output class
156
157 // Format the time
158 char buffer[80];
159 strftime(buffer, 80, format.c_str(), localtime(&now));
160 _file_base += "_";
161 _file_base += buffer;
162 }
163
164 // Check the file directory of file_base and create if needed
165 std::filesystem::path directory_base = _file_base;
166 directory_base.remove_filename();
167 if (directory_base.empty())
168 directory_base = ".";
169 // ensure relative path
170 directory_base = std::filesystem::relative(std::filesystem::absolute(directory_base));
171
172 std::filesystem::path possible_dir_to_make;
173 for (auto it = directory_base.begin(); it != directory_base.end(); ++it)
174 {
175 possible_dir_to_make = possible_dir_to_make / *it;
176 const auto dir_string = possible_dir_to_make.generic_string();
177 if (_app.processor_id() == 0 && access(dir_string.c_str(), F_OK) == -1)
178 // Directory does not exist. Create
179 if (Utility::mkdir(dir_string.c_str()) == -1)
180 mooseError("Could not create directory: " + dir_string + " for file base: " + _file_base);
181 }
182}
183
184void
186{
187 _file_num = num;
188}
189
190unsigned int
192{
193 return _file_num;
194}
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
void ErrorVector unsigned int
FileOutput(const InputParameters &parameters)
Class constructor.
Definition FileOutput.C:65
unsigned int getFileNumber()
Return the current file number for this outputter.
Definition FileOutput.C:191
std::vector< std::string > _output_if_base_contains
Storage for 'output_if_base_contains'.
Definition FileOutput.h:86
virtual std::string filename()
The filename for the output file.
Definition FileOutput.C:121
void setFileNumber(unsigned int num)
Sets the file number manually.
Definition FileOutput.C:185
static InputParameters validParams()
Definition FileOutput.C:24
virtual bool shouldOutput() override
Checks if the output method should be executed.
Definition FileOutput.C:88
std::string _file_base
The base filename from the input paramaters.
Definition FileOutput.h:89
void setFileBase(const std::string &file_base)
Sets the file base string if the 'file_base' parameter is not set.
Definition FileOutput.C:127
bool checkFilename()
Checks the filename for output Checks the output against the 'output_if_base_contians' list.
Definition FileOutput.C:96
virtual void setFileBaseInternal(const std::string &file_base)
Internal function that sets the file_base.
Definition FileOutput.C:137
unsigned int & _file_num
A file number counter, initialized to 0 (this must be controlled by the child class,...
Definition FileOutput.h:80
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
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...
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.
void addPrivateParam(const std::string &name, const T &value)
These method add a parameter to the InputParameters object which can be retrieved like any other para...
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.
processor_id_type processor_id() const
Returns the MPI processor ID of the current processor.
Definition MooseApp.h:417
bool isRestarting() const
Whether or not this is a "restart" calculation.
Definition MooseApp.C:1681
unsigned int multiAppNumber() const
The MultiApp number.
Definition MooseApp.h:861
bool isUltimateMaster() const
Whether or not this app is the ultimate master app.
Definition MooseApp.h:866
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition MooseBase.h:199
MooseApp & _app
The MOOSE application this is associated with.
Definition MooseBase.h:375
virtual bool shouldOutput()
Handles logic for determining if a step should be output.
Definition Output.C:272
virtual void output()=0
Overload this function with the desired output activities.
Adds the ability to output on every nonlinear and/or linear residual.
Definition PetscOutput.h:42
virtual Real time() override
Get the output time.
static InputParameters validParams()
int mkdir(const char *pathname)