writeFile.C 7.28 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           | www.openfoam.com
OpenFOAM bot's avatar
OpenFOAM bot committed
6
7
     \\/     M anipulation  |
-------------------------------------------------------------------------------
OpenFOAM bot's avatar
OpenFOAM bot committed
8
    Copyright (C) 2012-2018 OpenFOAM Foundation
9
    Copyright (C) 2015-2020 OpenCFD Ltd.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

29
#include "writeFile.H"
30
#include "Time.H"
31
#include "polyMesh.H"
32
#include "IFstream.H"
33
#include "functionObject.H"
34
35
36

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

37
Foam::label Foam::functionObjects::writeFile::addChars = 8;
38

39

40
41
// * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //

42
void Foam::functionObjects::writeFile::initStream(Ostream& os) const
43
44
{
    os.setf(ios_base::scientific, ios_base::floatfield);
45
    os.precision(writePrecision_);
46
47
48
49
    os.width(charWidth());
}


50
Foam::fileName Foam::functionObjects::writeFile::baseFileDir() const
51
{
52
53
    // Put in undecomposed case
    // (Note: gives problems for distributed data running)
54

55
56
57
58
59
    fileName baseDir =
    (
        fileObr_.time().globalPath()
      / functionObject::outputPrefix
    );
60

61
    // Append mesh name if not default region
62
    if (isA<polyMesh>(fileObr_))
63
    {
64
        const polyMesh& mesh = refCast<const polyMesh>(fileObr_);
65
66
        if (mesh.name() != polyMesh::defaultRegion)
        {
67
            baseDir /= mesh.name();
68
69
70
        }
    }

71
    baseDir.clean();  // Remove unneeded ".."
72

73
74
75
76
    return baseDir;
}


77
Foam::fileName Foam::functionObjects::writeFile::baseTimeDir() const
78
{
79
    return baseFileDir()/prefix_/fileObr_.time().timeName();
80
81
82
}


83
Foam::autoPtr<Foam::OFstream> Foam::functionObjects::writeFile::createFile
84
(
85
    const word& name,
86
    scalar timeValue
87
) const
88
{
89
90
91
    autoPtr<OFstream> osPtr;

    if (Pstream::master() && writeToFile_)
92
    {
93
94
95
96
        if (useUserTime_)
        {
            timeValue = fileObr_.time().timeToUserTime(timeValue);
        }
97

98
        const word timeName = Time::timeName(timeValue);
99

100
        fileName outputDir(baseFileDir()/prefix_/timeName);
101

102
        mkDir(outputDir);
103

104
        word fName(name);
105

106
107
108
109
        // Check if file already exists
        IFstream is(outputDir/(fName + ".dat"));
        if (is.good())
        {
110
            fName = fName + "_" + timeName;
111
112
        }

113
        osPtr.reset(new OFstream(outputDir/(fName + ".dat")));
114

115
116
117
118
119
120
        if (!osPtr->good())
        {
            FatalIOErrorInFunction(osPtr()) << "Cannot open file"
                << exit(FatalIOError);
        }

121
        initStream(osPtr());
122
    }
123
124

    return osPtr;
125
126
127
}


128
129
130
131
132
133
134
135
136
137
Foam::autoPtr<Foam::OFstream> Foam::functionObjects::writeFile::createFile
(
    const word& name
) const
{
    return createFile(name, startTime_);

}


138
void Foam::functionObjects::writeFile::resetFile(const word& fileName)
139
{
140
141
    fileName_ = fileName;
    filePtr_ = createFile(fileName_);
142
143
144
}


145
146
147
148
Foam::Omanip<int> Foam::functionObjects::writeFile::valueWidth
(
    const label offset
) const
149
{
150
    return setw(writePrecision_ + addChars + offset);
151
152
153
}


154
155
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

156
157
158
159
160
161
162
163
Foam::functionObjects::writeFile::writeFile(const writeFile& wf)
:
    fileObr_(wf.fileObr_),
    prefix_(wf.prefix_),
    fileName_(wf.fileName_),
    filePtr_(),
    writePrecision_(wf.writePrecision_),
    writeToFile_(wf.writeToFile_),
164
    updateHeader_(wf.updateHeader_),
165
166
167
168
169
170
    writtenHeader_(wf.writtenHeader_),
    useUserTime_(wf.useUserTime_),
    startTime_(wf.startTime_)
{}


171
Foam::functionObjects::writeFile::writeFile
172
173
(
    const objectRegistry& obr,
174
175
176
    const fileName& prefix,
    const word& name,
    const bool writeToFile
177
178
)
:
179
    fileObr_(obr),
180
    prefix_(prefix),
181
    fileName_(name),
182
183
    filePtr_(),
    writePrecision_(IOstream::defaultPrecision()),
184
    updateHeader_(true),
185
    writeToFile_(writeToFile),
186
    writtenHeader_(false),
187
    useUserTime_(true),
188
    startTime_(obr.time().startTime().value())
189
190
191
{}


192
Foam::functionObjects::writeFile::writeFile
193
194
(
    const objectRegistry& obr,
195
196
197
198
    const fileName& prefix,
    const word& name,
    const dictionary& dict,
    const bool writeToFile
199
200
)
:
201
    writeFile(obr, prefix, name, writeToFile)
202
{
203
    read(dict);
204

205
    if (writeToFile_)
206
    {
207
        filePtr_ = createFile(fileName_);
208
    }
209
210
211
212
213
}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

214
bool Foam::functionObjects::writeFile::read(const dictionary& dict)
andy's avatar
andy committed
215
{
216
    writePrecision_ =
217
        dict.getOrDefault("writePrecision", IOstream::defaultPrecision());
218

219
220
221
    updateHeader_ =
        dict.lookupOrDefault("updateHeader", updateHeader_);

222
223
    // Only write on master
    writeToFile_ =
224
        Pstream::master() && dict.getOrDefault("writeToFile", writeToFile_);
225

226
    // Use user time, e.g. CA deg in preference to seconds
227
    useUserTime_ = dict.getOrDefault("useUserTime", true);
228

229
    return true;
andy's avatar
andy committed
230
231
232
}


233
Foam::OFstream& Foam::functionObjects::writeFile::file()
234
{
235
    if (!writeToFile_)
236
    {
237
        return Snull;
238
239
    }

240
    if (!filePtr_.valid())
241
    {
242
        FatalErrorInFunction
243
            << "File pointer not allocated\n";
244
245
    }

246
    return *filePtr_;
247
248
249
}


250
bool Foam::functionObjects::writeFile::writeToFile() const
251
{
252
    return writeToFile_;
253
254
255
}


256
257
258
259
260
261
bool Foam::functionObjects::writeFile::canWriteHeader() const
{
    return writeToFile_ && (updateHeader_ || !writtenHeader_);
}


262
Foam::label Foam::functionObjects::writeFile::charWidth() const
263
{
264
    return writePrecision_ + addChars;
265
266
267
}


268
void Foam::functionObjects::writeFile::writeCommented
269
270
271
272
273
(
    Ostream& os,
    const string& str
) const
{
274
275
276
277
278
279
280
    os  << setw(1) << "#";

    if (str.size())
    {
        os  << setw(1) << ' '
            << setf(ios_base::left) << setw(charWidth() - 2) << str.c_str();
    }
281
282
283
}


284
void Foam::functionObjects::writeFile::writeTabbed
285
286
287
288
289
290
291
292
293
(
    Ostream& os,
    const string& str
) const
{
    os  << tab << setw(charWidth()) << str.c_str();
}


294
void Foam::functionObjects::writeFile::writeHeader
295
296
297
298
299
(
    Ostream& os,
    const string& str
) const
{
300
301
    writeCommented(os, str);
    os  << nl;
302
303
304
}


305
void Foam::functionObjects::writeFile::writeCurrentTime(Ostream& os) const
306
{
307
308
309
310
311
312
    const scalar timeValue =
    (
        useUserTime_
      ? fileObr_.time().timeOutputValue()
      : fileObr_.time().value()
    );
313

314
    os  << setw(charWidth()) << Time::timeName(timeValue);
315
316
317
}


318
319
320
321
322
323
void Foam::functionObjects::writeFile::writeBreak(Ostream& os) const
{
    writeHeader(os, "===");
}


324
// ************************************************************************* //