Loading...
Searching...
No Matches
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\*---------------------------------------------------------------------------*/
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
102 );
103
104 bool osGood = true;
105 if (!masterOnly || UPstream::master())
106 {
107 osGood = fileHandler().writeObject(*this, streamOpt, writeOnProc);
108 }
109
110 // Or broadcast the master osGood?
111 // if (masterOnly)
112 // {
113 // Pstream::broadcast(osGood);
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_.empty())
124 {
125 fileHandler().setUnmodified(watchIndices_.back());
126 }
127
128 return osGood;
129}
130
131
132bool Foam::regIOobject::write(const bool writeOnProc) const
133{
134 return writeObject
135 (
136 IOstreamOption(time().writeFormat(), time().writeCompression()),
137 writeOnProc
138 );
139}
140
141
142// ************************************************************************* //
const Time & time() const noexcept
Return Time associated with the objectRegistry.
Definition IOobject.C:456
const word & name() const noexcept
Return the object name.
Definition IOobjectI.H:205
bool good() const noexcept
Did last readHeader() succeed?
Definition IOobjectI.H:354
static bool fileModificationChecking_masterOnly() noexcept
Test fileModificationChecking for master-only.
Definition IOobjectI.H:23
const fileName & instance() const noexcept
Read access to instance path component.
Definition IOobjectI.H:289
fileName objectPath() const
The complete path + object name.
Definition IOobjectI.H:313
A simple container for options an IOstream can normally have.
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
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition UPstream.H:1714
virtual bool writeObject(IOstreamOption streamOpt, const bool writeOnProc) const
Write using stream options.
regIOobject(const IOobject &io, const bool isTimeObject=false)
Construct from IOobject. The optional flag adds special handling if the object is the top-level regIO...
Definition regIOobject.C:43
virtual bool write(const bool writeOnProc=true) const
Write using setting from DB.
word timeName
Definition getTimeIndex.H:3
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Different types of constants.
int system(const std::string &command, const bool bg=false)
Execute the specified command via the shell.
Definition POSIX.C:1704
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler().
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.