Loading...
Searching...
No Matches
ensightFile.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) 2011-2015 OpenFOAM Foundation
9 Copyright (C) 2016-2024 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
27Class
28 Foam::ensightFile
29
30Description
31 A variant of OFstream with specialised handling for Ensight writing
32 of strings, integers and floats (ASCII and BINARY).
33
34\*---------------------------------------------------------------------------*/
35
36#ifndef Foam_ensightFile_H
37#define Foam_ensightFile_H
38
39#include "OFstream.H"
40#include "ensightFileName.H"
41#include "ensightVarName.H"
42#include "DynamicList.H"
43#include "IndirectListBase.H"
44
45// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46
47namespace Foam
48{
49
50/*---------------------------------------------------------------------------*\
51 Class ensightFile Declaration
52\*---------------------------------------------------------------------------*/
53
54class ensightFile
55:
56 public OFstream
57{
58 // Private Data
59
60 //- Allow undef in results
61 static bool allowUndef_;
62
63 //- Value to represent undef in results (default: 1e+37, floatVGREAT)
64 static float undefValue_;
65
66 //- Transient single-file:
67 //- the original file size when opened in append mode, zero otherwise
68 int64_t origFileSize_;
69
70 //- Transient single-file:
71 //- the time-step file-offsets (position after "BEGIN TIME STEP").
72 // Set on initial reading and added to by beginTimeStep().
73 DynamicList<int64_t> timeStepOffsets_;
74
75
76 // Private Member Functions
77
78 //- Set the ASCII output formatting etc,
79 //- and handle transient single-file timestep information
80 void init();
81
82
83 // Constructors
84
85 //- Construct with file name, no ensight file naming adjustment.
86 // Created as an atomic or in append-mode (single-file format).
87 // In append-mode, attempts to parse existing time-step information.
88 // Changes APPEND_APP to APPEND_ATE (file rewriting).
89 ensightFile
90 (
91 std::nullptr_t, // dispatch tag
92 IOstreamOption::appendType append, // (NO_APPEND or APPEND_ATE)
93 const fileName& pathname,
95 );
96
97
98public:
99
100 // Public Data Types
101
102 //- Ensight uses \c float not \d double for floating-point
103 typedef float floatType;
104
105
106 // Static Data Members
107
108 //- The keyword "coordinates"
109 static const char* const coordinates;
110
111
112 // Static Functions
113
114 //- Return a null ensightFile
115 static const ensightFile& null() noexcept
116 {
118 }
119
121 // Generated Methods
122
123 //- No copy construct
124 ensightFile(const ensightFile&) = delete;
125
126 //- No copy assignment
127 void operator=(const ensightFile&) = delete;
129
130 // Constructors
131
132 //- Construct from path-name.
133 // The path-name is adjusted for valid ensight file naming.
134 // Created as an atomic or in append mode (single-file format).
135 // In append mode, attempts to parse existing time-step information.
136 ensightFile
137 (
140 const fileName& pathname,
142 );
143
144 //- Construct from path and name.
145 // Only the name portion is adjusted for valid ensight file naming.
146 // Created as an atomic or in append mode (single-file format).
147 // In append mode, attempts to parse existing time-step information.
148 ensightFile
149 (
152 const fileName& path,
153 const fileName& name,
155 );
156
157 //- Construct from path-name.
158 // The path-name is adjusted for valid ensight file naming.
159 // Created as an atomic, non-append mode.
160 explicit ensightFile
161 (
162 const fileName& pathname,
164 )
165 :
166 ensightFile(IOstreamOption::NO_APPEND, pathname, fmt)
167 {}
168
169 //- Construct from path and name.
170 // Only the name portion is adjusted for valid ensight file naming.
171 // Created as an atomic, non-append mode.
173 (
174 const fileName& path,
175 const fileName& name,
177 )
178 :
180 {}
181
182
183 //- Destructor. Commits the time-step footer information (if any)
184 ~ensightFile();
185
187 // Member Functions
188
189 // Access
190
191 //- Return setting for whether 'undef' values are allowed in results
192 static bool allowUndef() noexcept;
193
194
195 // Edit
196
197 //- Enable/disable use of \c undef keyword and value
198 static bool allowUndef(bool on) noexcept;
199
200 //- Assign the value to represent undef in the results
201 // Returns the previous value
202 // NB: do not use values larger than floatScalarVGREAT
203 static float undefValue(float value) noexcept;
204
205
206 // Output
207
208 //- Write "C Binary" string for binary files (eg, geometry/measured)
209 void writeBinaryHeader();
210
211 //- Write character/string content as "%79s" or as binary (max 80 chars)
212 void writeString(const char* str, size_t len);
213
214 //- Write C-string as "%79s" or as binary (max 80 chars)
215 void writeString(const char* str);
216
217 //- Write string as "%79s" or as binary (max 80 chars)
218 void writeString(const std::string& str);
219
220 //- Write integer value with specified width or as binary
221 void writeInt(const int32_t val, const int fieldWidth);
222
223 //- Write (narrowed) integer value with specified width or as binary
224 void writeInt(const int64_t val, const int fieldWidth);
225
226 //- Write floating-point with specified width or as binary
227 void writeFloat(const float val, const int fieldWidth);
228
229 //- Write (narrowed) floating-point with specified width or as binary
230 void writeFloat(const double val, const int fieldWidth);
231
232 //- Write undef value
233 void writeUndef();
234
235
236 //- Write element keyword with trailing newline,
237 //- optionally with undef and the value for undefined
238 virtual Ostream& writeKeyword(const keyType& key) override;
239
240 //- Writing token does not make sense
241 virtual bool write(const token&) override
242 {
244 return false;
245 }
246
247 //- Writing single character does not make sense
248 virtual Ostream& write(const char) override
249 {
251 return *this;
252 }
253
254 //- Binary write
255 virtual Ostream& write(const char* buf, std::streamsize count) override;
256
257 //- Write C-string, uses writeString()
258 virtual Ostream& write(const char* str) override;
259
260 //- Write word, uses writeString()
261 virtual Ostream& write(const word& str) override;
262
263 //- Write string, uses writeString()
264 virtual Ostream& write(const std::string& str) override;
265
266 //- Write integer value as "%10d" or as binary
267 virtual Ostream& write(const int32_t val) override;
268
269 //- Write integer value as "%10d" or as binary (narrowed to int32_t)
270 virtual Ostream& write(const int64_t val) override;
271
272 //- Write floating-point as "%12.5e" or as binary
273 virtual Ostream& write(const float val) override;
274
275 //- Write floating-point as "%12.5e" or as binary (narrowed to float)
276 virtual Ostream& write(const double val) override;
277
278 //- Add carriage return to ascii stream
279 void newline();
280
281
282 // Transient single-file format
283
284 //- Write "BEGIN TIME STEP" string and newline
285 //- (for transient single-file format).
286 // \returns file position after the write
287 int64_t beginTimeStep();
288
289 //- Write "END TIME STEP" string and newline
290 //- (for transient single-file format)
291 // \returns file position after the write
292 int64_t endTimeStep();
293
294 //- Transient single-file:
295 //- write the time-step file-offsets as footer information.
296 // Maintains the current file position to allow manual use
297 // and seamless overwriting.
298 // \return the output file position at the start of the footer
299 int64_t writeTimeStepFooter();
300
301 //- Transient single-file:
302 //- forget time-step file positions (advanced use)
304 {
305 timeStepOffsets_.clear();
306 }
307
308 //- Transient single-file:
309 //- the current number of time steps
310 label nTimes() const noexcept
311 {
312 return timeStepOffsets_.size();
313 }
314
315 //- Transient single-file:
316 //- the current file-offsets for time steps within the file
318 {
319 return timeStepOffsets_;
320 }
321
322
323 // Convenience Output Methods
324
325 //- Begin a part (0-based index internally).
326 void beginPart(const label index);
327
328 //- Begin a part (0-based index internally), with a description.
329 //- Only used for geometry files
330 void beginPart(const label index, const std::string& description);
331
332 //- Begin a "coordinates" block. Only used for geometry files.
333 void beginCoordinates(const label nparticles);
334
335 //- Begin a "particle coordinates" block (measured data)
336 void beginParticleCoordinates(const label nparticles);
337
338 //- Write a list of integers
339 // Adds newline after each value (ascii stream)
340 void writeLabels(const UList<label>& list);
341
342 //- Write a list of integers
343 // Adds newline after each value (ascii stream)
344 template<class Addr>
345 void writeLabels(const IndirectListBase<label, Addr>& list);
346
347 //- Write a list of integers as float values
348 // Adds newline after each value (ascii stream)
349 void writeList(const UList<label>& field);
350
351 //- Write a list of floats as "%12.5e" or as binary.
352 // Adds newline after each value (ascii stream)
353 void writeList(const UList<float>& field);
354
355 //- Write a list of double as "%12.5e" or as binary.
356 //- (double is narrowed to float)
357 // Adds newline after each value (ascii stream)
358 void writeList(const UList<double>& field);
359
360 //- Write an indirect list of float as "%12.5e" or as binary
361 // Adds newline after each value (ascii stream)
362 template<class Addr>
363 void writeList(const IndirectListBase<float, Addr>& field);
364
365 //- Write an indirect list of double as "%12.5e" or as binary.
366 //- (double is narrowed to float)
367 // Adds newline after each value (ascii stream)
368 template<class Addr>
369 void writeList(const IndirectListBase<double, Addr>& field);
370
371
372 // Other Methods
373
374 //- Check for any NaN in the field
375 static bool hasUndef(const UList<float>& field);
376
377 //- Check for any NaN in the field
378 static bool hasUndef(const UList<double>& field);
379
380 //- Check for any NaN in the field
381 template<class Addr>
382 static bool hasUndef(const IndirectListBase<float, Addr>& field);
383
384 //- Check for any NaN in the field
385 template<class Addr>
386 static bool hasUndef(const IndirectListBase<double, Addr>& field);
387};
388
389
390// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
391
392} // End namespace Foam
393
394// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
395
396#ifdef NoRepository
397 #include "ensightFile.txx"
398#endif
399
400// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
401
402#endif
404// ************************************************************************* //
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
A simple container for options an IOstream can normally have.
streamFormat
Data format (ascii | binary | coherent).
appendType
File appending (NO_APPEND | APPEND_APP | APPEND_ATE).
@ NO_APPEND
no append (truncates existing)
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
virtual const fileName & name() const override
Read/write access to the name of the stream.
Definition OSstream.H:134
OFstream(std::nullptr_t)
Construct a null output file stream that behaves like /dev/null.
Definition OFstream.C:36
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
Ostream(const Ostream &)=default
Copy construct.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
A variant of OFstream with specialised handling for Ensight writing of strings, integers and floats (...
Definition ensightFile.H:50
static bool hasUndef(const IndirectListBase< float, Addr > &field)
Check for any NaN in the field.
static const ensightFile & null() noexcept
Return a null ensightFile.
static bool hasUndef(const IndirectListBase< double, Addr > &field)
Check for any NaN in the field.
void writeUndef()
Write undef value.
void clearTimeSteps() noexcept
Transient single-file: forget time-step file positions (advanced use).
void writeString(const char *str, size_t len)
Write character/string content as "%79s" or as binary (max 80 chars).
virtual Ostream & write(const char) override
Writing single character does not make sense.
int64_t writeTimeStepFooter()
Transient single-file: write the time-step file-offsets as footer information.
int64_t beginTimeStep()
Write "BEGIN TIME STEP" string and newline (for transient single-file format).
void writeList(const IndirectListBase< float, Addr > &field)
Write an indirect list of float as "%12.5e" or as binary.
static bool allowUndef() noexcept
Return setting for whether 'undef' values are allowed in results.
void writeLabels(const IndirectListBase< label, Addr > &list)
Write a list of integers.
static const char *const coordinates
The keyword "coordinates".
void operator=(const ensightFile &)=delete
No copy assignment.
virtual bool write(const token &) override
Writing token does not make sense.
void writeBinaryHeader()
Write "C Binary" string for binary files (eg, geometry/measured).
void beginCoordinates(const label nparticles)
Begin a "coordinates" block. Only used for geometry files.
void beginPart(const label index)
Begin a part (0-based index internally).
int64_t endTimeStep()
Write "END TIME STEP" string and newline (for transient single-file format).
void writeList(const UList< label > &field)
Write a list of integers as float values.
void writeLabels(const UList< label > &list)
Write a list of integers.
static float undefValue(float value) noexcept
Assign the value to represent undef in the results.
label nTimes() const noexcept
Transient single-file: the current number of time steps.
ensightFile(const fileName &path, const fileName &name, IOstreamOption::streamFormat fmt=IOstreamOption::BINARY)
Construct from path and name.
const UList< int64_t > & timeStepOffets() const noexcept
Transient single-file: the current file-offsets for time steps within the file.
void writeList(const IndirectListBase< double, Addr > &field)
Write an indirect list of double as "%12.5e" or as binary. (double is narrowed to float).
void beginParticleCoordinates(const label nparticles)
Begin a "particle coordinates" block (measured data).
virtual Ostream & writeKeyword(const keyType &key) override
Write element keyword with trailing newline, optionally with undef and the value for undefined.
static bool hasUndef(const UList< float > &field)
Check for any NaN in the field.
Definition ensightFile.C:71
void writeFloat(const float val, const int fieldWidth)
Write floating-point with specified width or as binary.
void newline()
Add carriage return to ascii stream.
ensightFile(const ensightFile &)=delete
No copy construct.
void writeInt(const int32_t val, const int fieldWidth)
Write integer value with specified width or as binary.
float floatType
Ensight uses float not \d double for floating-point.
ensightFile(const fileName &pathname, IOstreamOption::streamFormat fmt=IOstreamOption::BINARY)
Construct from path-name.
~ensightFile()
Destructor. Commits the time-step footer information (if any).
A class for handling file names.
Definition fileName.H:75
A class for handling keywords in dictionaries.
Definition keyType.H:69
A token holds an item read from Istream.
Definition token.H:70
A class for handling words, derived from Foam::string.
Definition word.H:66
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
rDeltaTY field()
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition error.H:688
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
Namespace for OpenFOAM.
const T & NullObjectRef() noexcept
Const reference (of type T) to the nullObject.
Definition nullObject.H:228
const direction noexcept
Definition scalarImpl.H:265
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
runTime write()