writeFiles.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 "writeFiles.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 + "_" + obr_.time().timeName();
53  }
54 
55  filePtrs_.set(i, new OFstream(outputDir/(fName + ".dat")));
56 
57  initStream(filePtrs_[i]);
58 
59  writeFileHeader(i);
60 
61  }
62  }
63  }
64 }
65 
66 
68 {
69  names_.clear();
70  names_.append(names);
71 
72  if (Pstream::master())
73  {
74  filePtrs_.clear();
75  filePtrs_.setSize(names_.size());
76  }
77 }
78 
79 
81 {
82  names_.clear();
83  names_.append(name);
84 
85  if (Pstream::master())
86  {
87  filePtrs_.clear();
88  filePtrs_.setSize(1);
89  }
90 }
91 
92 
93 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
94 
96 (
97  const word& name,
98  const Time& time,
99  const dictionary& dict,
100  const word& prefix
101 )
102 :
103  writeFile(name, time, dict, prefix),
104  names_(),
105  filePtrs_()
106 {}
107 
108 
110 (
111  const word& name,
112  const objectRegistry& obr,
113  const dictionary& dict,
114  const word& prefix
115 )
116 :
117  writeFile(name, obr, dict, prefix),
118  names_(),
119  filePtrs_()
120 {}
121 
122 
123 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
124 
126 {}
127 
128 
129 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
130 
132 {
133  return names_;
134 }
135 
136 
138 {
139  if (!Pstream::master())
140  {
142  << "Request for file() can only be done by the master process"
143  << abort(FatalError);
144  }
145 
146  if (filePtrs_.size() != 1)
147  {
149  << "Requested single file, but multiple files are present"
150  << endl;
151  }
152 
153  if (!filePtrs_.set(0))
154  {
156  << "File pointer at index " << 0 << " not allocated"
157  << abort(FatalError);
158  }
159 
160  return filePtrs_[0];
161 }
162 
163 
165 {
166  if (!Pstream::master())
167  {
169  << "Request for files() can only be done by the master process"
170  << abort(FatalError);
171  }
172 
173  return filePtrs_;
174 }
175 
176 
178 {
179  if (!Pstream::master())
180  {
182  << "Request for file(i) can only be done by the master process"
183  << abort(FatalError);
184  }
185 
186  if (!filePtrs_.set(i))
187  {
189  << "File pointer at index " << i << " not allocated"
190  << abort(FatalError);
191  }
192 
193  return filePtrs_[i];
194 }
195 
196 
198 {
199  createFiles();
200 
201  return true;
202 }
203 
204 
205 // ************************************************************************* //
const Time & time() const
Return time.
virtual void initStream(Ostream &os) const
Initialise the output stream for writing.
Definition: writeFile.C:43
virtual void resetNames(const wordList &names)
Reset the list of names from a wordList.
Definition: writeFiles.C:67
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
Output to file stream.
Definition: OFstream.H:81
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
virtual dimensionedScalar startTime() const
Return start time.
Definition: Time.C:835
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:411
const Type & value() const
Return const reference to value.
static word timeName(const scalar, const int precision=precision_)
Return time name of given scalar time.
Definition: Time.C:715
virtual ~writeFiles()
Destructor.
Definition: writeFiles.C:125
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
virtual fileName baseFileDir() const
Return the base directory for output.
Definition: writeFile.C:50
virtual void writeFileHeader(const label i=0)
File header information.
Definition: writeFile.C:85
virtual void createFiles()
Create the output file.
Definition: writeFiles.C:32
void clear()
Clear the list, i.e. set size to zero.
Definition: List.C:356
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:97
const wordList & names() const
Return const access to the names.
Definition: writeFiles.C:131
writeFile(const writeFile &)
Disallow default bitwise copy construct.
OFstream & file()
Return access to the file (if only 1)
Definition: writeFiles.C:137
PtrList< OFstream > & files()
Return access to the files.
Definition: writeFiles.C:164
errorManip< error > abort(error &err)
Definition: errorManip.H:131
const word prefix_
Prefix.
Definition: writeFile.H:66
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:295
#define WarningInFunction
Report a warning using Foam::Warning.
const objectRegistry & obr_
Reference to the region objectRegistry.
virtual bool write()
Write function.
Definition: writeFiles.C:197
writeFiles(const writeFiles &)
Disallow default bitwise copy construct.
Registry of regIOobjects.
virtual void resetName(const word &name)
Reset the list of names to a single name entry.
Definition: writeFiles.C:80