logFiles.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2012-2016 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 "IFstream.H"
29 
30 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
31 
33 {
34  if (Pstream::master())
35  {
36  const word startTimeName =
38 
39  forAll(names_, i)
40  {
41  if (!filePtrs_.set(i))
42  {
43  fileName outputDir(baseFileDir()/prefix_/startTimeName);
44  mkDir(outputDir);
45 
46  word fName(names_[i]);
47 
48  // Check if file already exists
49  IFstream is(outputDir/(fName + ".dat"));
50  if (is.good())
51  {
52  fName = fName + "_" + fileObr_.time().timeName();
53  }
54 
55  filePtrs_.set(i, new OFstream(outputDir/(fName + ".dat")));
56 
58 
59  writeFileHeader(i);
60  }
61  }
62  }
63 }
64 
65 
67 {
68  names_.clear();
69  names_.append(names);
70 
71  if (Pstream::master())
72  {
73  filePtrs_.clear();
74  filePtrs_.setSize(names_.size());
75  }
76 }
77 
78 
80 {
81  names_.clear();
82  names_.append(name);
83 
84  if (Pstream::master())
85  {
86  filePtrs_.clear();
87  filePtrs_.setSize(1);
88  }
89 }
90 
91 
92 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
93 
94 Foam::functionObjects::logFiles::logFiles
95 (
96  const objectRegistry& obr,
97  const word& prefix
98 )
99 :
100  writeFile(obr, prefix),
101  names_(),
102  filePtrs_()
103 {}
104 
105 
106 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
107 
109 {}
110 
111 
112 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
113 
115 {
116  return names_;
117 }
118 
119 
121 {
122  if (!Pstream::master())
123  {
125  << "Request for file() can only be done by the master process"
126  << abort(FatalError);
127  }
128 
129  if (filePtrs_.size() != 1)
130  {
132  << "Requested single file, but multiple files are present"
133  << endl;
134  }
135 
136  if (!filePtrs_.set(0))
137  {
139  << "File pointer at index " << 0 << " not allocated"
140  << abort(FatalError);
141  }
142 
143  return filePtrs_[0];
144 }
145 
146 
148 {
149  if (!Pstream::master())
150  {
152  << "Request for files() can only be done by the master process"
153  << abort(FatalError);
154  }
155 
156  return filePtrs_;
157 }
158 
159 
161 {
162  if (!Pstream::master())
163  {
165  << "Request for file(i) can only be done by the master process"
166  << abort(FatalError);
167  }
168 
169  if (!filePtrs_.set(i))
170  {
172  << "File pointer at index " << i << " not allocated"
173  << abort(FatalError);
174  }
175 
176  return filePtrs_[i];
177 }
178 
179 
181 {
182  createFiles();
183 
184  return true;
185 }
186 
187 
188 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
virtual bool write()
Write function.
Definition: logFiles.C:180
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
A class for handling file names.
Definition: fileName.H:69
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:319
Output to file stream.
Definition: OFstream.H:82
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
virtual dimensionedScalar startTime() const
Return start time.
Definition: Time.C:769
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:412
static word timeName(const scalar, const int precision=precision_)
Return time name of given scalar time.
Definition: Time.C:644
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
virtual void writeFileHeader(const label i=0)=0
File header information.
PtrList< OFstream > & files()
Return access to the files.
Definition: logFiles.C:147
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:124
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:184
const Type & value() const
Return const reference to value.
virtual void resetName(const word &name)
Reset the list of names to a single name entry.
Definition: logFiles.C:79
const wordList & names() const
Return const access to the names.
Definition: logFiles.C:114
errorManip< error > abort(error &err)
Definition: errorManip.H:131
const word prefix_
Prefix.
Definition: writeFile.H:66
const Time & time() const
Return time.
Input from file stream.
Definition: IFstream.H:81
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:297
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:120
Registry of regIOobjects.
functionObject base class for writing single files
Definition: writeFile.H:55
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:66
virtual ~logFiles()
Destructor.
Definition: logFiles.C:108