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 -------------------------------------------------------------------------------
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 "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  bool masterOnly =
99  isGlobal
100  && (
101  IOobject::fileModificationChecking == IOobject::timeStampMaster
102  || IOobject::fileModificationChecking == IOobject::inotifyMaster
103  );
104 
105  bool osGood = false;
106  if (Pstream::master() || !masterOnly)
107  {
108  osGood = fileHandler().writeObject(*this, streamOpt, valid);
109  }
110  else
111  {
112  // Or scatter the master osGood?
113  osGood = true;
114  }
115 
116  if (OFstream::debug)
117  {
118  Pout<< " .... written" << endl;
119  }
120 
121  // Only update the lastModified_ time if this object is re-readable,
122  // i.e. lastModified_ is already set
123  if (watchIndices_.size())
124  {
125  fileHandler().setUnmodified(watchIndices_.last());
126  }
127 
128  return osGood;
129 }
130 
131 
132 bool Foam::regIOobject::write(const bool valid) const
133 {
134  return writeObject
135  (
136  IOstreamOption(time().writeFormat(), time().writeCompression()),
137  valid
138  );
139 }
140 
141 
143 (
147  const bool valid
148 ) const
149 {
150  return writeObject(IOstreamOption(fmt, ver, cmp), valid);
151 }
152 
153 
154 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
regIOobject.H
Foam::IOobject::instance
const fileName & instance() const noexcept
Definition: IOobjectI.H:196
Foam::system
int system(const std::string &command, const bool bg=false)
Execute the specified command via the shell.
Definition: MSwindows.C:1150
Foam::fileHandler
const fileOperation & fileHandler()
Get current file handler.
Definition: fileOperation.C:1485
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::Pout
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Foam::IOobject::time
const Time & time() const
Return Time associated with the objectRegistry.
Definition: IOobject.C:493
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:132
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:306
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:751
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:36
Foam::fileOperation::setUnmodified
virtual void setUnmodified(const label) const
Set current state of file (using handle) to unmodified.
Definition: fileOperation.C:942
Time.H
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:73
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:77
constant
constant condensation/saturation model.