regIOobjectWrite.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 | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2017 OpenFOAM Foundation
9 Copyright (C) 2020-2021 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "regIOobject.H"
30#include "Time.H"
31#include "OFstream.H"
32
33// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34
36(
37 IOstreamOption streamOpt,
38 const bool valid
39) const
40{
41 if (!good())
42 {
44 << "bad object " << name() << endl;
45
46 return false;
47 }
48
49 if (instance().empty())
50 {
52 << "instance undefined for object " << name() << endl;
53
54 return false;
55 }
56
57
58 //- uncomment this if you want to write global objects on master only
59 //bool isGlobal = global();
60 bool isGlobal = false;
61
62 if (instance() == time().timeName())
63 {
64 // Mark as written to local directory
65 isGlobal = false;
66 }
67 else if
68 (
69 instance() != time().system()
70 && instance() != time().caseSystem()
71 && instance() != time().constant()
72 && instance() != time().caseConstant()
73 )
74 {
75 // Update instance
76 const_cast<regIOobject&>(*this).instance() = time().timeName();
77
78 // Mark as written to local directory
79 isGlobal = false;
80 }
81
82 if (OFstream::debug)
83 {
84 if (isGlobal)
85 {
86 Pout<< "regIOobject::write() : "
87 << "writing (global) file " << objectPath();
88 }
89 else
90 {
91 Pout<< "regIOobject::write() : "
92 << "writing (local) file " << objectPath();
93 }
94 }
95
96
97 // Everyone check or just master
98 const bool masterOnly
99 (
100 isGlobal
101 && (
104 )
105 );
106
107 bool osGood = false;
108 if (!masterOnly || Pstream::master())
109 {
110 osGood = fileHandler().writeObject(*this, streamOpt, valid);
111 }
112 else
113 {
114 // Or scatter the master osGood?
115 osGood = true;
116 }
117
118 if (OFstream::debug)
119 {
120 Pout<< " .... written" << endl;
121 }
122
123 // Only update the lastModified_ time if this object is re-readable,
124 // i.e. lastModified_ is already set
125 if (watchIndices_.size())
126 {
127 fileHandler().setUnmodified(watchIndices_.last());
128 }
129
130 return osGood;
131}
132
133
134bool Foam::regIOobject::write(const bool valid) const
135{
136 return writeObject
137 (
138 IOstreamOption(time().writeFormat(), time().writeCompression()),
139 valid
140 );
141}
142
143
145(
149 const bool valid
150) const
151{
152 return writeObject(IOstreamOption(fmt, ver, cmp), valid);
153}
154
155
156// ************************************************************************* //
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:65
const Time & time() const
Return Time associated with the objectRegistry.
Definition: IOobject.C:506
bool good() const noexcept
Did last readHeader() succeed?
Definition: IOobjectI.H:222
const fileName & instance() const noexcept
Read access to instance path component.
Definition: IOobjectI.H:196
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:303
fileName objectPath() const
The complete path + object name.
Definition: IOobjectI.H:214
Representation of a major/minor version number.
The IOstreamOption is a simple container for options an IOstream can normally have.
streamFormat
Data format (ascii | binary)
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:780
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
T & last()
Return the last element of the list.
Definition: UListI.H:216
virtual void setUnmodified(const label) const
Set current state of file (using handle) to unmodified.
virtual bool writeObject(const regIOobject &io, IOstreamOption streamOpt=IOstreamOption(), const bool valid=true) const
Writes a regIOobject (so header, contents and divider).
virtual bool write()
Write the output fields.
constant condensation/saturation model.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:76
virtual bool writeObject(IOstreamOption streamOpt, const bool valid) const
Write using stream options.
splitCell * master() const
Definition: splitCell.H:113
word timeName
Definition: getTimeIndex.H:3
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
const fileOperation & fileHandler()
Get current file handler.
int system(const std::string &command, const bool bg=false)
Execute the specified command via the shell.
Definition: MSwindows.C:1158
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.