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-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::ensightFile
29 
30 Description
31  Ensight output with specialized write() for strings, integers and floats.
32  Correctly handles binary write as well.
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 "IndirectListBase.H"
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 
49 /*---------------------------------------------------------------------------*\
50  Class ensightFile Declaration
51 \*---------------------------------------------------------------------------*/
52 
53 class ensightFile
54 :
55  public OFstream
56 {
57  // Private Data
58 
59  //- Allow undef in results
60  static bool allowUndef_;
61 
62  //- Value to represent undef in results (default: 1e+37, floatVGREAT)
63  static float undefValue_;
64 
65 
66  // Private Member Functions
67 
68  //- Initialize sets the ASCII output formatting
69  void init();
70 
71  //- No copy construct
72  ensightFile(const ensightFile&) = delete;
73 
74  //- No copy assignment
75  void operator=(const ensightFile&) = delete;
76 
77 
78 public:
79 
80  // Public Data Types
81 
82  //- Ensight uses \c float not \d double for floating-point
83  typedef float floatType;
84 
85 
86  // Static Data Members
87 
88  //- The keyword "coordinates"
89  static const char* const coordinates;
90 
91 
92  // Static Functions
93 
94  //- Return a null ensightFile
95  static const ensightFile& null()
96  {
97  return NullObjectRef<ensightFile>();
98  }
99 
100 
101  // Constructors
102 
103  //- Construct from path-name.
104  // The path-name is adjusted for valid ensight file naming.
105  // Always created as an atomic
106  explicit ensightFile
107  (
108  const fileName& pathname,
110  );
111 
112  //- Construct from path and name.
113  // Only the name portion is adjusted for valid ensight file naming.
114  // Always created as an atomic
116  (
117  const fileName& path,
118  const fileName& name,
120  );
121 
122 
123  //- Destructor
124  ~ensightFile() = default;
125 
126 
127  // Member Functions
128 
129  // Access
130 
131  //- Return setting for whether 'undef' values are allowed in results
132  static bool allowUndef() noexcept;
133 
134 
135  // Edit
136 
137  //- Enable/disable use of \c undef keyword and value
138  static bool allowUndef(bool on) noexcept;
139 
140  //- Assign the value to represent undef in the results
141  // Returns the previous value
142  // NB: do not use values larger than floatScalarVGREAT
143  static float undefValue(float value) noexcept;
144 
145 
146  // Output
147 
148  //- Write "C Binary" string for binary files (eg, geometry/measured)
149  void writeBinaryHeader();
150 
151  //- Write "BEGIN TIME STEP" string and newline
152  void beginTimeStep();
153 
154  //- Write "END TIME STEP" string and newline
155  void endTimeStep();
156 
157  //- Write character/string content as "%79s" or as binary (max 80 chars)
158  void writeString(const char* str, size_t len);
159 
160  //- Write C-string as "%79s" or as binary (max 80 chars)
161  void writeString(const char* str);
162 
163  //- Write string as "%79s" or as binary (max 80 chars)
164  void writeString(const std::string& str);
165 
166  //- Write undef value
167  void writeUndef();
168 
169 
170  //- Write element keyword with trailing newline,
171  //- optionally with undef and the value for undefined
172  virtual Ostream& writeKeyword(const keyType& key) override;
173 
174  //- Writing token does not make sense
175  virtual bool write(const token&) override
176  {
178  return false;
179  }
180 
181  //- Writing single character does not make sense
182  virtual Ostream& write(const char) override
183  {
185  return *this;
186  }
187 
188  //- Binary write
189  virtual Ostream& write(const char* buf, std::streamsize count) override;
190 
191  //- Write C-string, uses writeString()
192  virtual Ostream& write(const char* str) override;
193 
194  //- Write word, uses writeString()
195  virtual Ostream& write(const word& str) override;
196 
197  //- Write string, uses writeString()
198  virtual Ostream& write(const std::string& str) override;
199 
200  //- Write integer as "%10d" or as binary
201  virtual Ostream& write(const int32_t val) override;
202 
203  //- Write integer as "%10d" or as binary
204  virtual Ostream& write(const int64_t val) override;
205 
206  //- Write integer with specified width or as binary
207  Ostream& write(const label value, const label fieldWidth);
208 
209  //- Write floating-point as "%12.5e" or as binary
210  virtual Ostream& write(const float val) override;
211 
212  //- Write floating-point as "%12.5e" or as binary (narrowed to float)
213  virtual Ostream& write(const double val) override;
214 
215  //- Add carriage return to ascii stream
216  void newline();
218 
219  // Convenience Output Methods
220 
221  //- Begin a part (0-based index internally).
222  void beginPart(const label index);
223 
224  //- Begin a "particle coordinates" block (measured data)
225  void beginParticleCoordinates(const label nparticles);
227  //- Write a list of integers
228  // Adds newline after each value (ascii stream)
229  void writeLabels(const UList<label>& list);
230 
231  //- Write a list of integers
232  // Adds newline after each value (ascii stream)
233  template<class Addr>
234  void writeLabels(const IndirectListBase<label, Addr>& list);
235 
236  //- Write a list of integers as float values
237  // Adds newline after each value (ascii stream)
238  void writeList(const UList<label>& field);
239 
240  //- Write a list of floats as "%12.5e" or as binary.
241  // Adds newline after each value (ascii stream)
242  void writeList(const UList<float>& field);
243 
244  //- Write a list of double as "%12.5e" or as binary.
245  //- (double is narrowed to float)
246  // Adds newline after each value (ascii stream)
247  void writeList(const UList<double>& field);
248 
249  //- Write an indirect list of float as "%12.5e" or as binary
250  // Adds newline after each value (ascii stream)
251  template<class Addr>
253 
254  //- Write an indirect list of double as "%12.5e" or as binary.
255  //- (double is narrowed to float)
256  // Adds newline after each value (ascii stream)
257  template<class Addr>
259 
260 
261  // Other Methods
262 
263  //- Check for any NaN in the field
264  static bool hasUndef(const UList<float>& field);
265 
266  //- Check for any NaN in the field
267  static bool hasUndef(const UList<double>& field);
268 
269  //- Check for any NaN in the field
270  template<class Addr>
271  static bool hasUndef(const IndirectListBase<float, Addr>& field);
272 
273  //- Check for any NaN in the field
274  template<class Addr>
275  static bool hasUndef(const IndirectListBase<double, Addr>& field);
276 };
277 
278 
279 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
280 
281 } // End namespace Foam
282 
283 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
284 
285 #ifdef NoRepository
286  #include "ensightFileTemplates.C"
287 #endif
288 
289 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
290 
291 #endif
292 
293 // ************************************************************************* //
static const ensightFile & null()
Return a null ensightFile.
Definition: ensightFile.H:104
Ensight output with specialized write() for strings, integers and floats. Correctly handles binary wr...
Definition: ensightFile.H:46
void writeBinaryHeader()
Write "C Binary" string for binary files (eg, geometry/measured)
Definition: ensightFile.C:331
A class for handling keywords in dictionaries.
Definition: keyType.H:66
rDeltaTY field()
A class for handling file names.
Definition: fileName.H:72
Output to file stream, using an OSstream.
Definition: OFstream.H:49
A token holds an item read from Istream.
Definition: token.H:65
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:128
Base for lists with indirect addressing, templated on the list contents type and the addressing type...
static const char *const coordinates
The keyword "coordinates".
Definition: ensightFile.H:96
void beginPart(const label index)
Begin a part (0-based index internally).
Definition: ensightFile.C:359
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:306
void beginTimeStep()
Write "BEGIN TIME STEP" string and newline.
Definition: ensightFile.C:341
void writeLabels(const UList< label > &list)
Write a list of integers.
Definition: ensightFile.C:377
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())
void beginParticleCoordinates(const label nparticles)
Begin a "particle coordinates" block (measured data)
Definition: ensightFile.C:368
float floatType
Ensight uses float not double for floating-point.
Definition: ensightFile.H:88
void writeString(const char *str, size_t len)
Write character/string content as "%79s" or as binary (max 80 chars)
Definition: ensightFile.C:143
virtual bool write(const token &) override
Writing token does not make sense.
Definition: ensightFile.H:217
void newline()
Add carriage return to ascii stream.
Definition: ensightFile.C:296
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:387
streamFormat
Data format (ascii | binary)
static float undefValue(float value) noexcept
Assign the value to represent undef in the results.
Definition: ensightFile.C:130
void endTimeStep()
Write "END TIME STEP" string and newline.
Definition: ensightFile.C:348
static bool hasUndef(const UList< float > &field)
Check for any NaN in the field.
Definition: ensightFile.C:39
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:686
~ensightFile()=default
Destructor.
static bool allowUndef() noexcept
Return setting for whether &#39;undef&#39; values are allowed in results.
Definition: ensightFile.C:110
Ostream(const Ostream &)=default
Copy construct.
virtual Ostream & writeKeyword(const keyType &key) override
Write element keyword with trailing newline, optionally with undef and the value for undefined...
Definition: ensightFile.C:312
Namespace for OpenFOAM.