writeFile.C 7.36 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
Foam::functionObjects::writeFile::writeFile(const writeFile& wf)
:
    fileObr_(wf.fileObr_),
    prefix_(wf.prefix_),
    fileName_(wf.fileName_),
Mark Olesen's avatar
Mark Olesen committed
161
    filePtr_(nullptr),
162 163
    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),
Mark Olesen's avatar
Mark Olesen committed
182
    filePtr_(nullptr),
183
    writePrecision_(IOstream::defaultPrecision()),
184
    writeToFile_(writeToFile),
Mark Olesen's avatar
Mark Olesen committed
185
    updateHeader_(true),
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 218 219 220 221 222
        dict.getCheckOrDefault
        (
            "writePrecision",
            IOstream::defaultPrecision(),
            labelMinMax::ge(0)
        );
223

Mark Olesen's avatar
Mark Olesen committed
224
    updateHeader_ = dict.getOrDefault("updateHeader", updateHeader_);
225

226 227
    // Only write on master
    writeToFile_ =
228
        Pstream::master() && dict.getOrDefault("writeToFile", writeToFile_);
229

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

233
    return true;
andy's avatar
andy committed
234 235 236
}


237
Foam::OFstream& Foam::functionObjects::writeFile::file()
238
{
239
    if (!writeToFile_)
240
    {
241
        return Snull;
242 243
    }

244
    if (!filePtr_.valid())
245
    {
246
        FatalErrorInFunction
247
            << "File pointer not allocated\n";
248 249
    }

250
    return *filePtr_;
251 252 253
}


254
bool Foam::functionObjects::writeFile::writeToFile() const
255
{
256
    return writeToFile_;
257 258 259
}


260 261 262 263 264 265
bool Foam::functionObjects::writeFile::canWriteHeader() const
{
    return writeToFile_ && (updateHeader_ || !writtenHeader_);
}


266
Foam::label Foam::functionObjects::writeFile::charWidth() const
267
{
268
    return writePrecision_ + addChars;
269 270 271
}


272
void Foam::functionObjects::writeFile::writeCommented
273 274 275 276 277
(
    Ostream& os,
    const string& str
) const
{
278 279 280 281 282 283 284
    os  << setw(1) << "#";

    if (str.size())
    {
        os  << setw(1) << ' '
            << setf(ios_base::left) << setw(charWidth() - 2) << str.c_str();
    }
285 286 287
}


288
void Foam::functionObjects::writeFile::writeTabbed
289 290 291 292 293 294 295 296 297
(
    Ostream& os,
    const string& str
) const
{
    os  << tab << setw(charWidth()) << str.c_str();
}


298
void Foam::functionObjects::writeFile::writeHeader
299 300 301 302 303
(
    Ostream& os,
    const string& str
) const
{
304 305
    writeCommented(os, str);
    os  << nl;
306 307 308
}


309
void Foam::functionObjects::writeFile::writeCurrentTime(Ostream& os) const
310
{
311 312 313 314 315 316
    const scalar timeValue =
    (
        useUserTime_
      ? fileObr_.time().timeOutputValue()
      : fileObr_.time().value()
    );
317

318
    os  << setw(charWidth()) << Time::timeName(timeValue);
319 320 321
}


322 323 324 325 326 327
void Foam::functionObjects::writeFile::writeBreak(Ostream& os) const
{
    writeHeader(os, "===");
}


328
// ************************************************************************* //