writeFile.H 6.94 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-2016 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
-------------------------------------------------------------------------------
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/>.

Class
28
    Foam::functionObjects::writeFile
29 30

Description
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    Base class for writing single files from the function objects.

Usage

    \verbatim
    <userDefinedSubDictName1>
    {
        // Mandatory and other optional entries
        ...

        // Optional (inherited) entries (runtime modifiable)
        writePrecision    8;
        writeToFile       true;
        useUserTime       true;
    }
    \endverbatim

    where the entries mean:
    \table
      Property        | Description                      | Type | Req'd | Dflt
      writePrecision  | Number of decimal points | label | no  | \<system dflt\>
      writeToFile     | Flag to produce text file output | bool | no    | true
      useUserTime | Flag to use user time, e.g. degrees  | bool | no    | true
    \endtable
55

56
See also
57 58
    - Foam::functionObject
    - Foam::functionObjects::logFiles
59 60

SourceFiles
61
    writeFile.C
62 63 64

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

65 66
#ifndef functionObjects_writeFile_H
#define functionObjects_writeFile_H
67 68

#include "objectRegistry.H"
69
#include "OFstream.H"
70
#include "IOmanip.H"
71 72 73 74 75

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{
76 77
namespace functionObjects
{
78 79

/*---------------------------------------------------------------------------*\
80
                 Class functionObjects::writeFile Declaration
81 82
\*---------------------------------------------------------------------------*/

83
class writeFile
84
{
85
protected:
86

87 88
    // Protected data

89 90
        //- Reference to the region objectRegistry
        const objectRegistry& fileObr_;
91 92

        //- Prefix
93
        const fileName prefix_;
94

95 96
        //- Name of file
        word fileName_;
97 98

        //- File pointer
99 100 101 102
        autoPtr<OFstream> filePtr_;

        //- Write precision
        label writePrecision_;
103

104 105 106
        //- Flag to enable/disable writing to file
        bool writeToFile_;

107 108 109 110
        //- Flag to update the header, e.g. on mesh changes.
        //- Default is true.
        bool updateHeader_;

111 112 113
        //- Flag to identify whether the header has been written
        bool writtenHeader_;

114 115 116 117
        //- Flag to use the specified user time, e.g. CA deg instead
        //- of seconds.  Default = true
        bool useUserTime_;

118 119 120
        //- Start time value
        scalar startTime_;

121

122 123
    // Protected Member Functions

124
        //- Initialise the output stream for writing
125
        void initStream(Ostream& os) const;
126

127
        //- Return the base directory for output
128
        fileName baseFileDir() const;
129

130
        //- Return the base directory for the current time value
131
        fileName baseTimeDir() const;
132

133
        //- Return autoPtr to a new file for a given time
134 135 136
        virtual autoPtr<OFstream> createFile
        (
            const word& name,
137
            scalar timeValue
138 139
        ) const;

140
        //- Return autoPtr to a new file using the simulation start time
141 142 143 144
        virtual autoPtr<OFstream> createFile
        (
            const word& name
        ) const;
145

146 147
        //- Reset internal file pointer to new file with new name
        virtual void resetFile(const word& name);
148

149
        //- Return the value width when writing to stream with optional offset
150 151
        Omanip<int> valueWidth(const label offset = 0) const;

andy's avatar
andy committed
152

153 154
        //- No copy assignment
        void operator=(const writeFile&) = delete;
155 156 157 158


public:

159 160 161
    //- Additional characters for writing
    static label addChars;

162

163 164
    // Constructors

165
        //- Construct from objectRegistry, prefix, fileName
166 167 168
        writeFile
        (
            const objectRegistry& obr,
169 170 171
            const fileName& prefix,
            const word& name = "undefined",
            const bool writeToFile = true
172
        );
173

174 175
        //- Construct from objectRegistry, prefix, fileName
        //- and read options from dictionary
176
        writeFile
177 178
        (
            const objectRegistry& obr,
179 180 181 182
            const fileName& prefix,
            const word& name,
            const dictionary& dict,
            const bool writeToFile = true
183 184
        );

185 186 187
        //- Construct copy
        writeFile(const writeFile& wf);

188 189

    //- Destructor
190
    virtual ~writeFile() = default;
191 192 193 194


    // Member Functions

195
        //- Read
196
        virtual bool read(const dictionary& dict);
andy's avatar
andy committed
197

198
        //- Return access to the file (if only 1)
199
        virtual OFstream& file();
200

201
        //- Flag to allow writing to file
202
        virtual bool writeToFile() const;
203

204 205 206
        //- Flag to allow writing the header
        virtual bool canWriteHeader() const;

207
        //- Return width of character stream output
208
        virtual label charWidth() const;
209

210
        //- Write a commented string to stream
211
        virtual void writeCommented(Ostream& os, const string& str) const;
212 213

        //- Write a tabbed string to stream
214
        virtual void writeTabbed(Ostream& os, const string& str) const;
215 216

        //- Write a commented header to stream
217
        virtual void writeHeader(Ostream& os, const string& str) const;
218

219
        //- Write the current time to stream
220
        virtual void writeCurrentTime(Ostream& os) const;
221

222 223 224
        //- Write a break marker to the stream
        virtual void writeBreak(Ostream& os) const;

225
        //- Write a (commented) header property and value pair
226
        template<class Type>
227
        void writeHeaderValue
228
        (
229 230 231
            Ostream& os,
            const string& property,
            const Type& value
232
        ) const;
233 234 235 236 237
};


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

238
} // End namespace functionObjects
239 240 241 242
} // End namespace Foam

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

243
#ifdef NoRepository
244
    #include "writeFileTemplates.C"
245 246 247 248
#endif

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

249 250 251
#endif

// ************************************************************************* //