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-2021 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().timeName();
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  if (!Pstream::master())
110  {
112  << "Request for file() can only be done by the master process"
113  << abort(FatalError);
114  }
115 
116  if (filePtrs_.size() != 1)
117  {
119  << "Requested single file, but multiple files are present"
120  << endl;
121  }
122 
123  if (!filePtrs_.set(0))
124  {
126  << "File pointer at index " << 0 << " not allocated"
127  << abort(FatalError);
128  }
129 
130  return filePtrs_[0];
131 }
132 
133 
135 {
136  if (!Pstream::master())
137  {
139  << "Request for files() can only be done by the master process"
140  << abort(FatalError);
141  }
142 
143  return filePtrs_;
144 }
145 
146 
148 {
149  if (!Pstream::master())
150  {
152  << "Request for file(i) can only be done by the master process"
153  << abort(FatalError);
154  }
155 
156  if (!filePtrs_.set(i))
157  {
159  << "File pointer at index " << i << " not allocated"
160  << abort(FatalError);
161  }
162 
163  return filePtrs_[i];
164 }
165 
166 
168 {
169  createFiles();
170 
171  return true;
172 }
173 
174 
175 // ************************************************************************* //
logFiles(const objectRegistry &obr, const word &prefix)
Construct from objectRegistry and prefix.
Definition: logFiles.C:82
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
virtual bool write()
Write function.
Definition: logFiles.C:167
A class for handling file names.
Definition: fileName.H:79
virtual void createFiles()
Create the output file.
Definition: logFiles.C:32
error FatalError
wordList names_
File names.
Definition: logFiles.H:67
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
Output to file stream.
Definition: OFstream.H:82
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:423
virtual void writeFileHeader(const label i=0)=0
File header information.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
PtrList< OFstream > & files()
Return access to the files.
Definition: logFiles.C:134
void initStream(Ostream &os) const
Initialise the output stream for writing.
Definition: writeFile.C:42
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:125
static word timeName(const scalar, const int precision=curPrecision_)
Return time name of given scalar time.
Definition: Time.C:666
A class for handling words, derived from string.
Definition: word.H:59
void append(const T &)
Append an element at the end of the list.
Definition: ListI.H:178
writeFile(const objectRegistry &obr, const word &prefix)
Construct from objectRegistry and prefix.
Definition: writeFile.C:85
word timeName
Definition: getTimeIndex.H:3
virtual void resetName(const word &name)
Reset the list of names to a single name entry.
Definition: logFiles.C:66
const wordList & names() const
Return const access to the names.
Definition: logFiles.C:101
errorManip< error > abort(error &err)
Definition: errorManip.H:131
const word prefix_
Prefix.
Definition: writeFile.H:66
const Time & time() const
Return time.
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:290
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
fileName baseFileDir() const
Return the base directory for output.
Definition: writeFile.C:49
#define WarningInFunction
Report a warning using Foam::Warning.
const objectRegistry & fileObr_
Reference to the region objectRegistry.
Definition: writeFile.H:63
OFstream & file()
Return access to the file (if only 1)
Definition: logFiles.C:107
Registry of regIOobjects.
PtrList< OFstream > filePtrs_
File pointer.
Definition: logFiles.H:70
virtual void resetNames(const wordList &names)
Reset the list of names from a wordList.
Definition: logFiles.C:53
virtual ~logFiles()
Destructor.
Definition: logFiles.C:95