ensightFile.C
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 \*---------------------------------------------------------------------------*/
28 
29 #include "ensightFile.H"
30 #include "error.H"
31 #include "List.H"
32 #include <cstring>
33 #include <sstream>
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 bool Foam::ensightFile::allowUndef_ = false;
38 
39 float Foam::ensightFile::undefValue_ = Foam::floatScalarVGREAT;
40 
41 const char* const Foam::ensightFile::coordinates = "coordinates";
42 
43 
44 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
45 
47 {
48  for (const float val : field)
49  {
50  if (std::isnan(val))
51  {
52  return true;
53  }
54  }
55 
56  return true;
57 }
58 
59 
60 bool Foam::ensightFile::hasUndef(const UList<double>& field)
61 {
62  for (const double val : field)
63  {
64  if (std::isnan(val))
65  {
66  return true;
67  }
68  }
69 
70  return true;
71 }
72 
73 
74 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
75 
76 void Foam::ensightFile::init()
77 {
78  // The ASCII formatting specs for ensight files
79  setf
80  (
82  std::ios_base::floatfield
83  );
84  precision(5);
85 }
86 
87 
88 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
89 
90 Foam::ensightFile::ensightFile
91 (
92  const fileName& pathname,
94 )
95 :
96  OFstream(IOstreamOption::ATOMIC, ensight::FileName(pathname), fmt)
97 {
98  init();
99 }
100 
101 
102 Foam::ensightFile::ensightFile
103 (
104  const fileName& path,
105  const fileName& name,
107 )
108 :
109  OFstream(IOstreamOption::ATOMIC, path/ensight::FileName(name), fmt)
110 {
111  init();
112 }
113 
114 
115 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
116 
118 {
119  return allowUndef_;
120 }
121 
123 // float Foam::ensightFile::undefValue() noexcept
124 // {
125 // return undefValue_;
126 // }
127 
128 
130 {
131  bool old = allowUndef_;
132  allowUndef_ = on;
133  return old;
134 }
135 
136 
137 float Foam::ensightFile::undefValue(float value) noexcept
138 {
139  // enable its use too
140  allowUndef_ = true;
141 
142  float old = undefValue_;
143  undefValue_ = value;
144  return old;
145 }
146 
147 
148 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
149 
150 void Foam::ensightFile::writeString(const char* str, size_t len)
151 {
152  // Output 79 chars (ASCII) or 80 chars (BINARY)
153  char buf[80];
154  if (len > 80) len = 80;
155 
156  // TBD: truncate at newline? (shouldn't really occur anyhow)
157 
158  std::copy_n(str, len, buf);
159  std::fill_n(buf + len, (80 - len), '\0'); // Pad trailing with nul
160 
162  {
163  write(buf, 80);
164  }
165  else
166  {
167  buf[79] = 0; // Max 79 in ASCII
168 
169  // TBD: Extra safety - trap newline in ASCII?
170  // char* p = ::strchr(buf, '\n');
171  // if (p) *p = 0;
173  stdStream() << buf;
174  syncState();
175  }
176 }
177 
179 void Foam::ensightFile::writeString(const char* str)
180 {
181  writeString(str, strlen(str));
182 }
183 
185 void Foam::ensightFile::writeString(const std::string& str)
186 {
187  writeString(str.data(), str.size());
188 }
189 
190 
192 {
193  writeString(str, strlen(str));
194  return *this;
195 }
196 
197 
199 {
200  writeString(str.data(), str.size());
201  return *this;
202 }
203 
204 
205 Foam::Ostream& Foam::ensightFile::write(const std::string& str)
206 {
207  writeString(str.data(), str.size());
208  return *this;
209 }
210 
211 
213 (
214  const char* buf,
215  std::streamsize count
216 )
217 {
218  stdStream().write(buf, count);
219  syncState();
220  return *this;
221 }
222 
223 
224 Foam::Ostream& Foam::ensightFile::write(const int32_t val)
225 {
227  {
228  write
229  (
230  reinterpret_cast<const char *>(&val),
231  sizeof(int32_t)
232  );
233  }
234  else
235  {
236  stdStream().width(10);
237  stdStream() << val;
238  syncState();
239  }
240 
241  return *this;
242 }
243 
244 
245 Foam::Ostream& Foam::ensightFile::write(const int64_t val)
246 {
247  int32_t ivalue(narrowInt32(val));
248 
249  return write(ivalue);
250 }
251 
252 
253 Foam::Ostream& Foam::ensightFile::write(const float val)
254 {
256  {
257  write
258  (
259  reinterpret_cast<const char *>(&val),
260  sizeof(float)
261  );
262  }
263  else
264  {
265  stdStream().width(12);
266  stdStream() << val;
267  syncState();
268  }
269 
270  return *this;
271 }
272 
273 
274 Foam::Ostream& Foam::ensightFile::write(const double val)
275 {
276  float fvalue(narrowFloat(val));
277 
278  return write(fvalue);
279 }
280 
281 
283 (
284  const label value,
285  const label fieldWidth
286 )
287 {
289  {
290  write(value);
291  }
292  else
293  {
294  stdStream().width(fieldWidth);
295  stdStream() << value;
296  syncState();
297  }
298 
299  return *this;
300 }
301 
302 
304 {
305  if (format() == IOstreamOption::ASCII)
306  {
307  stdStream() << nl;
308  syncState();
309  }
310 }
311 
314 {
315  write(undefValue_);
316 }
317 
318 
320 {
321  if (allowUndef_)
322  {
323  writeString(key + " undef");
324  newline();
325  write(undefValue_);
326  newline();
327  }
328  else
329  {
330  writeString(key);
331  newline();
332  }
333 
334  return *this;
335 }
336 
337 
339 {
341  {
342  writeString("C Binary");
343  // Is binary: newline() is a no-op
344  }
345 }
346 
347 
349 {
350  writeString("BEGIN TIME STEP");
351  newline();
352 }
353 
354 
356 {
357  writeString("END TIME STEP");
358  newline();
359 }
360 
361 
362 //
363 // Convenience Output Methods
364 //
365 
366 void Foam::ensightFile::beginPart(const label index)
367 {
368  writeString("part");
369  newline();
370  write(index+1); // Ensight starts with 1
371  newline();
372 }
373 
374 
375 void Foam::ensightFile::beginParticleCoordinates(const label nparticles)
376 {
377  writeString("particle coordinates");
378  newline();
379  write(nparticles, 8); // unusual width
380  newline();
381 }
382 
383 
385 {
386  for (const label val : list)
387  {
388  write(val);
389  newline();
390  }
391 }
392 
393 
394 void Foam::ensightFile::writeList(const UList<label>& field)
395 {
396  for (const label val : field)
397  {
398  write(float(val));
399  newline();
400  }
401 }
402 
403 
404 void Foam::ensightFile::writeList(const UList<float>& field)
405 {
406  for (const float val : field)
407  {
408  if (std::isnan(val))
409  {
410  writeUndef();
411  }
412  else
413  {
414  write(val);
415  }
416  newline();
417  }
418 }
419 
420 
421 void Foam::ensightFile::writeList(const UList<double>& field)
422 {
423  for (const double val : field)
424  {
425  if (std::isnan(val))
426  {
427  writeUndef();
428  }
429  else
430  {
431  write(val);
432  }
433  newline();
434  }
435 }
436 
437 
438 // ************************************************************************* //
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
float narrowFloat(const double val)
Type narrowing from double to float.
Definition: scalar.H:240
Output to file stream, using an OSstream.
Definition: OFstream.H:49
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
"ascii" (normal default)
A simple container for options an IOstream can normally have.
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
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
int32_t narrowInt32(const int64_t val)
Type narrowing from int64_t to int32_t.
Definition: label.H:232
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
void writeUndef()
Write undef value.
Definition: ensightFile.C:306
A class for handling words, derived from Foam::string.
Definition: word.H:63
void beginTimeStep()
Write "BEGIN TIME STEP" string and newline.
Definition: ensightFile.C:341
Smanip< ios_base::fmtflags > setf(const ios_base::fmtflags flags)
Definition: IOmanip.H:169
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: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
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
word format(conversionProperties.get< word >("format"))
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)
constexpr floatScalar floatScalarVGREAT
Definition: floatScalar.H:56
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
static bool allowUndef() noexcept
Return setting for whether &#39;undef&#39; values are allowed in results.
Definition: ensightFile.C:110
IOstream & scientific(IOstream &io)
Definition: IOstream.H:563
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