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 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 "OSspecific.H"
32 #include "OFstream.H"
33 
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 
37 (
38  IOstreamOption streamOpt,
39  const bool valid
40 ) const
41 {
42  if (!good())
43  {
45  << "bad object " << name()
46  << endl;
47 
48  return false;
49  }
50 
51  if (instance().empty())
52  {
54  << "instance undefined for object " << name()
55  << endl;
56 
57  return false;
58  }
59 
60 
61  //- uncomment this if you want to write global objects on master only
62  //bool isGlobal = global();
63  bool isGlobal = false;
64 
65  if (instance() == time().timeName())
66  {
67  // Mark as written to local directory
68  isGlobal = false;
69  }
70  else if
71  (
72  instance() != time().system()
73  && instance() != time().caseSystem()
74  && instance() != time().constant()
75  && instance() != time().caseConstant()
76  )
77  {
78  // Update instance
79  const_cast<regIOobject&>(*this).instance() = time().timeName();
80 
81  // Mark as written to local directory
82  isGlobal = false;
83  }
84 
85  if (OFstream::debug)
86  {
87  if (isGlobal)
88  {
89  Pout<< "regIOobject::write() : "
90  << "writing (global) file " << objectPath();
91  }
92  else
93  {
94  Pout<< "regIOobject::write() : "
95  << "writing (local) file " << objectPath();
96  }
97  }
98 
99 
100  bool osGood = false;
101 
102 
103  // Everyone check or just master
104  bool masterOnly =
105  isGlobal
106  && (
107  regIOobject::fileModificationChecking == timeStampMaster
108  || regIOobject::fileModificationChecking == inotifyMaster
109  );
110 
111 
112  if (Pstream::master() || !masterOnly)
113  {
114  //if (mkDir(path()))
115  //{
116  // // Try opening an OFstream for object
117  // OFstream os(objectPath(), streamOpt);
118  //
119  // // If any of these fail, return (leave error handling to Ostream
120  // // class)
121  // if (!os.good())
122  // {
123  // return false;
124  // }
125  //
126  // if (!writeHeader(os))
127  // {
128  // return false;
129  // }
130  //
131  // // Write the data to the Ostream
132  // if (!writeData(os))
133  // {
134  // return false;
135  // }
136  //
137  // writeEndDivider(os);
138  //
139  // osGood = os.good();
140  //}
141  osGood = fileHandler().writeObject(*this, streamOpt, valid);
142  }
143  else
144  {
145  // Or scatter the master osGood?
146  osGood = true;
147  }
148 
149  if (OFstream::debug)
150  {
151  Pout<< " .... written" << endl;
152  }
153 
154  // Only update the lastModified_ time if this object is re-readable,
155  // i.e. lastModified_ is already set
156  if (watchIndices_.size())
157  {
158  fileHandler().setUnmodified(watchIndices_.last());
159  }
160 
161  return osGood;
162 }
163 
164 
165 bool Foam::regIOobject::write(const bool valid) const
166 {
167  return writeObject
168  (
169  IOstreamOption(time().writeFormat(), time().writeCompression()),
170  valid
171  );
172 }
173 
174 
176 (
180  const bool valid
181 ) const
182 {
183  return writeObject(IOstreamOption(fmt, ver, cmp), valid);
184 }
185 
186 
187 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
regIOobject.H
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::IOobject::instance
const fileName & instance() const
Definition: IOobjectI.H:191
Foam::system
int system(const std::string &command, const bool bg=false)
Execute the specified command via the shell.
Definition: MSwindows.C:1140
Foam::fileHandler
const fileOperation & fileHandler()
Get current file handler.
Definition: fileOperation.C:1354
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::Pout
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Foam::IOobject::time
const Time & time() const
Return time.
Definition: IOobject.C:463
OFstream.H
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:85
Foam::regIOobject::write
virtual bool write(const bool valid=true) const
Write using setting from DB.
Definition: regIOobjectWrite.C:165
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
SeriousErrorInFunction
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Definition: messageStream.H:281
Foam::fileOperation::writeObject
virtual bool writeObject(const regIOobject &io, IOstreamOption streamOpt=IOstreamOption(), const bool valid=true) const
Writes a regIOobject (so header, contents and divider).
Definition: fileOperation.C:617
timeName
word timeName
Definition: getTimeIndex.H:3
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
Foam::regIOobject::writeObject
virtual bool writeObject(IOstreamOption streamOpt, const bool valid) const
Write using stream options.
Definition: regIOobjectWrite.C:37
Foam::fileOperation::setUnmodified
virtual void setUnmodified(const label) const
Set current state of file (using handle) to unmodified.
Definition: fileOperation.C:808
Time.H
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:71
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:77
constant
constant condensation/saturation model.