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 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Description
27  write function for regIOobjects
28 
29 \*---------------------------------------------------------------------------*/
30 
31 #include "regIOobject.H"
32 #include "Time.H"
33 #include "OSspecific.H"
34 #include "OFstream.H"
35 
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 
39 (
43  const bool valid
44 ) const
45 {
46  if (!good())
47  {
49  << "bad object " << name()
50  << endl;
51 
52  return false;
53  }
54 
55  if (instance().empty())
56  {
58  << "instance undefined for object " << name()
59  << endl;
60 
61  return false;
62  }
63 
64 
65 
66  //- uncomment this if you want to write global objects on master only
67  //bool isGlobal = global();
68  bool isGlobal = false;
69 
70  if (instance() == time().timeName())
71  {
72  // Mark as written to local directory
73  isGlobal = false;
74  }
75  else if
76  (
77  instance() != time().system()
78  && instance() != time().caseSystem()
79  && instance() != time().constant()
80  && instance() != time().caseConstant()
81  )
82  {
83  // Update instance
84  const_cast<regIOobject&>(*this).instance() = time().timeName();
85 
86  // Mark as written to local directory
87  isGlobal = false;
88  }
89 
90  if (OFstream::debug)
91  {
92  if (isGlobal)
93  {
94  Pout<< "regIOobject::write() : "
95  << "writing (global) file " << objectPath();
96  }
97  else
98  {
99  Pout<< "regIOobject::write() : "
100  << "writing (local) file " << objectPath();
101  }
102  }
103 
104 
105  bool osGood = false;
106 
107 
108  // Everyone check or just master
109  bool masterOnly =
110  isGlobal
111  && (
112  regIOobject::fileModificationChecking == timeStampMaster
113  || regIOobject::fileModificationChecking == inotifyMaster
114  );
115 
116 
117  if (Pstream::master() || !masterOnly)
118  {
119  //if (mkDir(path()))
120  //{
121  // // Try opening an OFstream for object
122  // OFstream os(objectPath(), fmt, ver, cmp);
123  //
124  // // If any of these fail, return (leave error handling to Ostream
125  // // class)
126  // if (!os.good())
127  // {
128  // return false;
129  // }
130  //
131  // if (!writeHeader(os))
132  // {
133  // return false;
134  // }
135  //
136  // // Write the data to the Ostream
137  // if (!writeData(os))
138  // {
139  // return false;
140  // }
141  //
142  // writeEndDivider(os);
143  //
144  // osGood = os.good();
145  //}
146  osGood = fileHandler().writeObject(*this, fmt, ver, cmp, valid);
147  }
148  else
149  {
150  // Or scatter the master osGood?
151  osGood = true;
152  }
153 
154  if (OFstream::debug)
155  {
156  Pout<< " .... written" << endl;
157  }
158 
159  // Only update the lastModified_ time if this object is re-readable,
160  // i.e. lastModified_ is already set
161  if (watchIndices_.size())
162  {
163  fileHandler().setUnmodified(watchIndices_.last());
164  }
165 
166  return osGood;
167 }
168 
169 
170 bool Foam::regIOobject::write(const bool valid) const
171 {
172  return writeObject
173  (
174  time().writeFormat(),
176  time().writeCompression(),
177  valid
178  );
179 }
180 
181 
182 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
regIOobject.H
Foam::regIOobject::writeObject
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool valid) const
Write using given format, version and compression.
Definition: regIOobjectWrite.C:39
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:167
Foam::system
int system(const std::string &command, const bool bg=false)
Execute the specified command via the shell.
Definition: MSwindows.C:1140
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number.
Definition: IOstreamOption.H:193
Foam::fileHandler
const fileOperation & fileHandler()
Get current file handler.
Definition: fileOperation.C:1181
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::Pout
prefixOSstream Pout
An Ostream wrapper for parallel output to std::cout.
Foam::IOobject::time
const Time & time() const
Return time.
Definition: IOobject.C:438
OFstream.H
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:79
Foam::regIOobject::write
virtual bool write(const bool valid=true) const
Write using setting from DB.
Definition: regIOobjectWrite.C:170
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
SeriousErrorInFunction
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Definition: messageStream.H:272
timeName
word timeName
Definition: getTimeIndex.H:3
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:64
Foam::fileOperation::setUnmodified
virtual void setUnmodified(const label) const
Set current state of file (using handle) to unmodified.
Definition: fileOperation.C:670
Time.H
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:67
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:71
constant
constant condensation/saturation model.
Foam::fileOperation::writeObject
virtual bool writeObject(const regIOobject &, IOstream::streamFormat format=IOstream::ASCII, IOstream::versionNumber version=IOstream::currentVersion, IOstream::compressionType compression=IOstream::UNCOMPRESSED, const bool valid=true) const
Writes a regIOobject (so header, contents and divider).
Definition: fileOperation.C:465