masterOFstream.H
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) 2017 OpenFOAM Foundation
9  Copyright (C) 2020-2023 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 Class
28  Foam::masterOFstream
29 
30 Description
31  Master-only drop-in replacement for OFstream.
32 
33  Called on all processors (of the provided communicator).
34  Sends files to the master and writes them there.
35 
36 SourceFiles
37  masterOFstream.C
38 
39 \*---------------------------------------------------------------------------*/
40 
41 #ifndef Foam_masterOFstream_H
42 #define Foam_masterOFstream_H
43 
44 #include "StringStream.H"
45 #include "UPstream.H"
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 
52 /*---------------------------------------------------------------------------*\
53  Class masterOFstream Declaration
54 \*---------------------------------------------------------------------------*/
55 
56 class masterOFstream
57 :
58  public OStringStream
59 {
60  // Private Data
61 
62  //- The backend file name
63  const fileName pathName_;
64 
65  //- Atomic file creation (ignored with append)
66  const IOstreamOption::atomicType atomic_;
67 
68  //- Output file compression
69  const IOstreamOption::compressionType compression_;
70 
71  //- Open file in append mode
72  const IOstreamOption::appendType append_;
73 
74  //- Should file be written (on this processor)
75  const bool writeOnProc_;
76 
77  //- Communicator
78  const label comm_;
79 
80 
81  // Private Member Functions
82 
83  //- Open file with checking and write append contents
84  void checkWrite
85  (
86  const fileName& fName,
87  const char* str,
88  std::streamsize len
89  );
90 
91  //- Open file with checking and write append contents
92  void checkWrite(const fileName& fName, const std::string& s);
93 
94  //- Commit buffered information, including parallel gather as required
95  void commit();
96 
97 
98 public:
99 
100  // Constructors
101 
102  //- Construct with specified atomic behaviour and communicator
103  //- from pathname, stream option, optional append
105  (
107  const label comm,
108  const fileName& pathname,
109  IOstreamOption streamOpt = IOstreamOption(),
111  const bool writeOnProc = true
112  );
113 
114  //- Construct with specified communicator
115  //- from pathname, stream option, optional append
117  (
118  const label comm,
119  const fileName& pathname,
120  IOstreamOption streamOpt = IOstreamOption(),
122  const bool writeOnProc = true
123  )
124  :
126  (
128  comm,
129  pathname,
130  streamOpt,
131  append,
132  writeOnProc
133  )
134  {}
135 
136  //- Construct with specified atomic behaviour (with worldComm)
137  //- from pathname, stream option, optional append
139  (
141  const fileName& pathname,
142  IOstreamOption streamOpt = IOstreamOption(),
144  const bool writeOnProc = true
145  )
146  :
148  (
149  atomic,
150  UPstream::worldComm,
151  pathname,
152  streamOpt,
153  append,
154  writeOnProc
155  )
156  {}
157 
158  //- Construct (with worldComm)
159  //- from pathname, stream option, optional append
160  explicit masterOFstream
161  (
162  const fileName& pathname,
163  IOstreamOption streamOpt = IOstreamOption(),
165  const bool writeOnProc = true
166  )
167  :
169  (
171  UPstream::worldComm,
172  pathname,
173  streamOpt,
174  append,
175  writeOnProc
176  )
177  {}
178 
179 
180  //- Destructor - commits buffered information to file
181  ~masterOFstream();
182 };
183 
184 
185 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
186 
187 } // End namespace Foam
188 
189 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
190 
191 #endif
192 
193 // ************************************************************************* //
A class for handling file names.
Definition: fileName.H:72
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Foam::string str() const
Get the string - as Foam::string rather than std::string.
Definition: StringStream.H:96
Input/output from string buffers.
A simple container for options an IOstream can normally have.
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
atomicType
Atomic operations (output)
Master-only drop-in replacement for OFstream.
appendType
File appending (NON_APPEND | APPEND)
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
~masterOFstream()
Destructor - commits buffered information to file.
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:256
Inter-processor communications stream.
Definition: UPstream.H:60
Namespace for OpenFOAM.
masterOFstream(IOstreamOption::atomicType atomic, const label comm, const fileName &pathname, IOstreamOption streamOpt=IOstreamOption(), IOstreamOption::appendType append=IOstreamOption::NON_APPEND, const bool writeOnProc=true)
Construct with specified atomic behaviour and communicator from pathname, stream option, optional append.