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-2022 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 element keyword with trailing newline,
149  //- optionally with undef and the value for undefined
150  virtual Ostream& writeKeyword(const keyType& key);
151 
152  //- Write "C Binary" string for binary files (eg, geometry/measured)
154 
155  //- Write C-string as "%79s" or as binary (max 80 chars)
156  Ostream& writeString(const char* str);
157 
158  //- Write string as "%79s" or as binary (max 80 chars)
159  Ostream& writeString(const std::string& str);
160 
161  //- Write undef value
162  Ostream& writeUndef();
163 
164 
165  //- Writing token does not make sense
166  virtual bool write(const token&)
167  {
169  return false;
170  }
171 
172  //- Writing single character does not make sense
173  virtual Ostream& write(const char)
174  {
176  return *this;
177  }
178 
179  //- Binary write
180  virtual Ostream& write(const char* buf, std::streamsize count);
181 
182  //- Write C-string, uses writeString()
183  virtual Ostream& write(const char* str);
184 
185  //- Write word, uses writeString()
186  virtual Ostream& write(const word& str);
187 
188  //- Write string, uses writeString()
189  virtual Ostream& write(const string& str);
190 
191  //- Write integer as "%10d" or as binary
192  virtual Ostream& write(const int32_t val);
193 
194  //- Write integer as "%10d" or as binary
195  virtual Ostream& write(const int64_t val);
196 
197  //- Write integer with specified width or as binary
198  Ostream& write(const label value, const label fieldWidth);
199 
200  //- Write floating-point as "%12.5e" or as binary
201  virtual Ostream& write(const float val);
203  //- Write floating-point as "%12.5e" or as binary (narrowed to float)
204  virtual Ostream& write(const double val);
205 
206  //- Add carriage return to ascii stream
207  void newline();
208 
209 
210  // Convenience Output Methods
212  //- Begin a part (0-based index internally).
213  void beginPart(const label index);
214 
215  //- Begin a "particle coordinates" block (measured data)
216  void beginParticleCoordinates(const label nparticles);
217 
218  //- Write a list of integers
219  // Adds newline after each value (ascii stream)
220  void writeLabels(const UList<label>& list);
221 
222  //- Write a list of integers
223  // Adds newline after each value (ascii stream)
224  template<class Addr>
225  void writeLabels(const IndirectListBase<label, Addr>& list);
226 
227  //- Write a list of integers as float values
228  // Adds newline after each value (ascii stream)
229  void writeList(const UList<label>& field);
230 
231  //- Write a list of floats as "%12.5e" or as binary.
232  // Adds newline after each value (ascii stream)
233  void writeList(const UList<float>& field);
234 
235  //- Write a list of double as "%12.5e" or as binary.
236  //- (double is narrowed to float)
237  // Adds newline after each value (ascii stream)
238  void writeList(const UList<double>& field);
239 
240  //- Write an indirect list of float as "%12.5e" or as binary
241  // Adds newline after each value (ascii stream)
242  template<class Addr>
244 
245  //- Write an indirect list of double as "%12.5e" or as binary.
246  //- (double is narrowed to float)
247  // Adds newline after each value (ascii stream)
248  template<class Addr>
250 
251 
252  // Other Methods
253 
254  //- Check for any NaN in the field
255  static bool hasUndef(const UList<float>& field);
256 
257  //- Check for any NaN in the field
258  static bool hasUndef(const UList<double>& field);
259 
260  //- Check for any NaN in the field
261  template<class Addr>
262  static bool hasUndef(const IndirectListBase<float, Addr>& field);
263 
264  //- Check for any NaN in the field
265  template<class Addr>
266  static bool hasUndef(const IndirectListBase<double, Addr>& field);
267 };
268 
269 
270 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
271 
272 } // End namespace Foam
273 
274 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
275 
276 #ifdef NoRepository
277  #include "ensightFileTemplates.C"
278 #endif
279 
280 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
281 
282 #endif
283 
284 // ************************************************************************* //
Ostream & writeUndef()
Write undef value.
Definition: ensightFile.C:291
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
virtual const fileName & name() const
Get the name of the output serial stream. (eg, the name of the Fstream file name) ...
Definition: OSstream.H:128
A class for handling keywords in dictionaries.
Definition: keyType.H:66
rDeltaTY field()
A class for handling file names.
Definition: fileName.H:71
Output to file stream, using an OSstream.
Definition: OFstream.H:49
A token holds an item read from Istream.
Definition: token.H:64
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:332
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
void writeLabels(const UList< label > &list)
Write a list of integers.
Definition: ensightFile.C:350
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
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:341
float floatType
Ensight uses float not double for floating-point.
Definition: ensightFile.H:88
Ostream & writeBinaryHeader()
Write "C Binary" string for binary files (eg, geometry/measured)
Definition: ensightFile.C:317
void newline()
Add carriage return to ascii stream.
Definition: ensightFile.C:281
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:360
streamFormat
Data format (ascii | binary)
virtual Ostream & writeKeyword(const keyType &key)
Write element keyword with trailing newline, optionally with undef and the value for undefined...
Definition: ensightFile.C:298
virtual bool write(const token &)
Writing token does not make sense.
Definition: ensightFile.H:202
static float undefValue(float value) noexcept
Assign the value to represent undef in the results.
Definition: ensightFile.C:130
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:666
~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.
Namespace for OpenFOAM.
Ostream & writeString(const char *str)
Write C-string as "%79s" or as binary (max 80 chars)
Definition: ensightFile.C:143