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 -------------------------------------------------------------------------------
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::ensightFile
29 
30 Description
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 
47 namespace Foam
48 {
49 
50 /*---------------------------------------------------------------------------*\
51  Class ensightFile Declaration
52 \*---------------------------------------------------------------------------*/
53 
54 class 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).
90  (
91  std::nullptr_t, // dispatch tag
92  IOstreamOption::appendType append, // (NO_APPEND or APPEND_ATE)
93  const fileName& pathname,
95  );
96 
97 
98 public:
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  {
117  return NullObjectRef<ensightFile>();
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.
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.
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 
186 
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)
303  void clearTimeSteps() noexcept
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
317  const UList<int64_t>& timeStepOffets() const noexcept
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 "ensightFileTemplates.C"
398 #endif
399 
400 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
401 
402 #endif
404 // ************************************************************************* //
A variant of OFstream with specialised handling for Ensight writing of strings, integers and floats (...
Definition: ensightFile.H:47
void writeBinaryHeader()
Write "C Binary" string for binary files (eg, geometry/measured)
Definition: ensightFile.C:437
A class for handling keywords in dictionaries.
Definition: keyType.H:66
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
int64_t endTimeStep()
Write "END TIME STEP" string and newline (for transient single-file format)
Definition: ensightFile.C:552
rDeltaTY field()
A class for handling file names.
Definition: fileName.H:72
Output to file stream as an OSstream, normally using std::ofstream for the actual output...
Definition: OFstream.H:71
A token holds an item read from Istream.
Definition: token.H:65
A simple container for options an IOstream can normally have.
virtual const fileName & name() const override
Get the name of the output serial stream. (eg, the name of the Fstream file name) ...
Definition: OSstream.H:134
int64_t writeTimeStepFooter()
Transient single-file: write the time-step file-offsets as footer information.
Definition: ensightFile.C:463
static const char *const coordinates
The keyword "coordinates".
Definition: ensightFile.H:120
int64_t beginTimeStep()
Write "BEGIN TIME STEP" string and newline (for transient single-file format).
Definition: ensightFile.C:512
void beginPart(const label index)
Begin a part (0-based index internally).
Definition: ensightFile.C:561
void clearTimeSteps() noexcept
Transient single-file: forget time-step file positions (advanced use)
Definition: ensightFile.H:394
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
void writeUndef()
Write undef value.
Definition: ensightFile.C:412
const UList< int64_t > & timeStepOffets() const noexcept
Transient single-file: the current file-offsets for time steps within the file.
Definition: ensightFile.H:412
~ensightFile()
Destructor. Commits the time-step footer information (if any)
Definition: ensightFile.C:228
no append (truncates existing)
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
void writeLabels(const UList< label > &list)
Write a list of integers.
Definition: ensightFile.C:601
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
label nTimes() const noexcept
Transient single-file: the current number of time steps.
Definition: ensightFile.H:403
void beginParticleCoordinates(const label nparticles)
Begin a "particle coordinates" block (measured data)
Definition: ensightFile.C:592
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:405
void beginCoordinates(const label nparticles)
Begin a "coordinates" block. Only used for geometry files.
Definition: ensightFile.C:582
float floatType
Ensight uses float not double for floating-point.
Definition: ensightFile.H:112
appendType
File appending (NO_APPEND | APPEND_APP | APPEND_ATE)
void writeString(const char *str, size_t len)
Write character/string content as "%79s" or as binary (max 80 chars)
Definition: ensightFile.C:269
virtual bool write(const token &) override
Writing token does not make sense.
Definition: ensightFile.H:301
void newline()
Add carriage return to ascii stream.
Definition: ensightFile.C:403
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
void writeList(const UList< label > &field)
Write a list of integers as float values.
Definition: ensightFile.C:611
void writeInt(const int32_t val, const int fieldWidth)
Write integer value with specified width or as binary.
Definition: ensightFile.C:347
streamFormat
Data format (ascii | binary)
static const ensightFile & null() noexcept
Return a null ensightFile.
Definition: ensightFile.H:128
static float undefValue(float value) noexcept
Assign the value to represent undef in the results.
Definition: ensightFile.C:256
void operator=(const ensightFile &)=delete
No copy assignment.
static bool hasUndef(const UList< float > &field)
Check for any NaN in the field.
Definition: ensightFile.C:71
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:696
static bool allowUndef() noexcept
Return setting for whether &#39;undef&#39; values are allowed in results.
Definition: ensightFile.C:236
Ostream(const Ostream &)=default
Copy construct.
void writeFloat(const float val, const int fieldWidth)
Write floating-point with specified width or as binary.
Definition: ensightFile.C:359
virtual Ostream & writeKeyword(const keyType &key) override
Write element keyword with trailing newline, optionally with undef and the value for undefined...
Definition: ensightFile.C:418
Namespace for OpenFOAM.