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 writeOnProc
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 or just master
98  const bool masterOnly
99  (
100  isGlobal
101  && (
104  )
105  );
106 
107  bool osGood = false;
108  if (!masterOnly || UPstream::master())
109  {
110  osGood = fileHandler().writeObject(*this, streamOpt, writeOnProc);
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_.empty())
126  {
127  fileHandler().setUnmodified(watchIndices_.back());
128  }
129 
130  return osGood;
131 }
132 
133 
134 bool Foam::regIOobject::write(const bool writeOnProc) const
135 {
136  return writeObject
137  (
138  IOstreamOption(time().writeFormat(), time().writeCompression()),
139  writeOnProc
140  );
141 }
142 
143 
144 // ************************************************************************* //
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:195
bool empty() const noexcept
True if List is empty (ie, size() is zero)
Definition: UList.H:666
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler()
A simple container for options an IOstream can normally have.
bool good() const noexcept
Did last readHeader() succeed?
Definition: IOobjectI.H:298
virtual bool writeObject(IOstreamOption streamOpt, const bool writeOnProc) const
Write using stream options.
fileName objectPath() const
The complete path + object name.
Definition: IOobjectI.H:284
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
word timeName
Definition: getTimeIndex.H:3
virtual bool write(const bool writeOnProc=true) const
Write using setting from DB.
const Time & time() const noexcept
Return Time associated with the objectRegistry.
Definition: IOobject.C:456
static word timeName(const scalar t, const int precision=precision_)
Return a time name for the given scalar time value formatted with the given precision.
Definition: Time.C:714
int debug
Static debugging option.
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:343
const fileName & instance() const noexcept
Read access to instance path component.
Definition: IOobjectI.H:266
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1082
T & back()
Access last element of the list, position [size()-1].
Definition: UListI.H:251
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:66
int system(const std::string &command, const bool bg=false)
Execute the specified command via the shell.
Definition: POSIX.C:1702
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.