logFiles.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2012-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "logFiles.H"
27 #include "Time.H"
28 #include "OSspecific.H"
29 
30 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
31 
33 {
34  if (Pstream::master())
35  {
36  const word timeName = fileObr_.time().name();
37 
38  forAll(names_, i)
39  {
40  if (!filePtrs_.set(i))
41  {
42  const fileName outputDir(baseFileDir()/prefix_/timeName);
43  mkDir(outputDir);
44  filePtrs_.set(i, new OFstream(outputDir/(names_[i] + ".dat")));
46  writeFileHeader(i);
47  }
48  }
49  }
50 }
51 
52 
54 {
55  names_.clear();
56  names_.append(names);
57 
58  if (Pstream::master())
59  {
60  filePtrs_.clear();
61  filePtrs_.setSize(names_.size());
62  }
63 }
64 
65 
67 {
68  names_.clear();
69  names_.append(name);
70 
71  if (Pstream::master())
72  {
73  filePtrs_.clear();
74  filePtrs_.setSize(1);
75  }
76 }
77 
78 
79 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
80 
82 (
83  const objectRegistry& obr,
84  const word& prefix
85 )
86 :
87  writeFile(obr, prefix),
88  names_(),
89  filePtrs_()
90 {}
91 
92 
93 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
94 
96 {}
97 
98 
99 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
100 
102 {
103  return names_;
104 }
105 
106 
108 {
109  return names_;
110 }
111 
112 
114 {
115  if (!Pstream::master())
116  {
118  << "Request for file() can only be done by the master process"
120  }
121 
122  if (filePtrs_.size() != 1)
123  {
125  << "Requested single file, but multiple files are present"
126  << endl;
127  }
128 
129  if (!filePtrs_.set(0))
130  {
132  << "File pointer at index " << 0 << " not allocated"
133  << abort(FatalError);
134  }
135 
136  return filePtrs_[0];
137 }
138 
139 
141 {
142  if (!Pstream::master())
143  {
145  << "Request for files() can only be done by the master process"
146  << abort(FatalError);
147  }
148 
149  return filePtrs_;
150 }
151 
152 
154 {
155  if (!Pstream::master())
156  {
158  << "Request for file(i) can only be done by the master process"
159  << abort(FatalError);
160  }
161 
162  if (!filePtrs_.set(i))
163  {
165  << "File pointer at index " << i << " not allocated"
166  << abort(FatalError);
167  }
168 
169  return filePtrs_[i];
170 }
171 
172 
174 {
175  createFiles();
176 
177  return true;
178 }
179 
180 
181 // ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Output to file stream.
Definition: OFstream.H:86
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:423
const word & name() const
Return const reference to name.
A class for handling file names.
Definition: fileName.H:82
const wordList & toc() const
Return the list of log file names.
Definition: logFiles.C:101
virtual void resetNames(const wordList &names)
Reset the list of names from a wordList.
Definition: logFiles.C:53
virtual void createFiles()
Create the output file.
Definition: logFiles.C:32
PtrList< OFstream > filePtrs_
File pointer.
Definition: logFiles.H:70
PtrList< OFstream > & files()
Return access to the files.
Definition: logFiles.C:140
virtual void resetName(const word &name)
Reset the list of names to a single name entry.
Definition: logFiles.C:66
wordList names_
File names.
Definition: logFiles.H:67
OFstream & file()
Return access to the file (if only 1)
Definition: logFiles.C:113
const wordList & names() const
Return the list of log file names.
Definition: logFiles.C:107
virtual void writeFileHeader(const label i=0)=0
File header information.
virtual ~logFiles()
Destructor.
Definition: logFiles.C:95
logFiles(const objectRegistry &obr, const word &prefix)
Construct from objectRegistry and prefix.
Definition: logFiles.C:82
virtual bool write()
Write function.
Definition: logFiles.C:173
functionObject base class for writing single files
Definition: writeFile.H:56
fileName baseFileDir() const
Return the base directory for output.
Definition: writeFile.C:49
const word prefix_
Prefix.
Definition: writeFile.H:66
void initStream(Ostream &os) const
Initialise the output stream for writing.
Definition: writeFile.C:42
const objectRegistry & fileObr_
Reference to the region objectRegistry.
Definition: writeFile.H:63
Registry of regIOobjects.
const Time & time() const
Return time.
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
word timeName
Definition: getTimeIndex.H:3
#define WarningInFunction
Report a warning using Foam::Warning.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:290
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
errorManip< error > abort(error &err)
Definition: errorManip.H:131
error FatalError