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-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 \*---------------------------------------------------------------------------*/
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 
151 {
152  // Output 80 chars, but allocate for trailing nul character
153  // to avoid -Wstringop-truncation warnings/errors.
154 
155  char buf[80+1];
156  strncpy(buf, str, 80); // max 80 chars or padded with nul if smaller
157 
159  {
160  write(buf, 80);
161  }
162  else
163  {
164  buf[79] = 0; // max 79 in ASCII, ensure it is indeed nul-terminated
165  stdStream() << buf;
166  syncState();
167  }
168 
169  return *this;
170 }
171 
173 Foam::Ostream& Foam::ensightFile::writeString(const std::string& str)
174 {
175  return writeString(str.c_str());
176 }
177 
179 Foam::Ostream& Foam::ensightFile::write(const char* str)
180 {
181  return writeString(str);
182 }
183 
186 {
187  return writeString(str);
188 }
189 
190 
192 {
193  return writeString(str);
194 }
195 
196 
198 (
199  const char* buf,
200  std::streamsize count
201 )
202 {
203  stdStream().write(buf, count);
204  syncState();
205  return *this;
206 }
207 
208 
209 Foam::Ostream& Foam::ensightFile::write(const int32_t val)
210 {
212  {
213  write
214  (
215  reinterpret_cast<const char *>(&val),
216  sizeof(int32_t)
217  );
218  }
219  else
220  {
221  stdStream().width(10);
222  stdStream() << val;
223  syncState();
224  }
225 
226  return *this;
227 }
228 
229 
230 Foam::Ostream& Foam::ensightFile::write(const int64_t val)
231 {
232  int32_t ivalue(narrowInt32(val));
233 
234  return write(ivalue);
235 }
236 
237 
238 Foam::Ostream& Foam::ensightFile::write(const float val)
239 {
241  {
242  write
243  (
244  reinterpret_cast<const char *>(&val),
245  sizeof(float)
246  );
247  }
248  else
249  {
250  stdStream().width(12);
251  stdStream() << val;
252  syncState();
253  }
254 
255  return *this;
256 }
257 
258 
259 Foam::Ostream& Foam::ensightFile::write(const double val)
260 {
261  float fvalue(narrowFloat(val));
262 
263  return write(fvalue);
264 }
265 
266 
268 (
269  const label value,
270  const label fieldWidth
271 )
272 {
274  {
275  write(value);
276  }
277  else
278  {
279  stdStream().width(fieldWidth);
280  stdStream() << value;
281  syncState();
282  }
283 
284  return *this;
285 }
286 
287 
289 {
290  if (format() == IOstreamOption::ASCII)
291  {
292  stdStream() << nl;
293  syncState();
294  }
295 }
296 
297 
299 {
300  write(undefValue_);
301  return *this;
302 }
303 
304 
306 {
307  if (allowUndef_)
308  {
309  writeString(key + " undef");
310  newline();
311  write(undefValue_);
312  newline();
313  }
314  else
315  {
316  writeString(key);
317  newline();
318  }
319 
320  return *this;
321 }
322 
323 
325 {
327  {
328  writeString("C Binary");
329  }
330 
331  return *this;
332 }
333 
334 
335 //
336 // Convenience Output Methods
337 //
338 
339 void Foam::ensightFile::beginPart(const label index)
340 {
341  writeString("part");
342  newline();
343  write(index+1); // Ensight starts with 1
344  newline();
345 }
346 
347 
348 void Foam::ensightFile::beginParticleCoordinates(const label nparticles)
349 {
350  writeString("particle coordinates");
351  newline();
352  write(nparticles, 8); // unusual width
353  newline();
354 }
355 
356 
358 {
359  for (const label val : list)
360  {
361  write(val);
362  newline();
363  }
364 }
365 
366 
367 void Foam::ensightFile::writeList(const UList<label>& field)
368 {
369  for (const label val : field)
370  {
371  write(float(val));
372  newline();
373  }
374 }
375 
376 
377 void Foam::ensightFile::writeList(const UList<float>& field)
378 {
379  for (const float val : field)
380  {
381  if (std::isnan(val))
382  {
383  writeUndef();
384  }
385  else
386  {
387  write(val);
388  }
389  newline();
390  }
391 }
392 
393 
394 void Foam::ensightFile::writeList(const UList<double>& field)
395 {
396  for (const double val : field)
397  {
398  if (std::isnan(val))
399  {
400  writeUndef();
401  }
402  else
403  {
404  write(val);
405  }
406  newline();
407  }
408 }
409 
410 
411 // ************************************************************************* //
Ostream & writeUndef()
Write undef value.
Definition: ensightFile.C:291
A class for handling keywords in dictionaries.
Definition: keyType.H:66
rDeltaTY field()
A class for handling file names.
Definition: fileName.H:71
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:49
"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:332
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:208
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
A class for handling words, derived from Foam::string.
Definition: word.H:63
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:99
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
word format(conversionProperties.get< word >("format"))
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
constexpr floatScalar floatScalarVGREAT
Definition: floatScalar.H:55
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
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:566
Ostream & writeString(const char *str)
Write C-string as "%79s" or as binary (max 80 chars)
Definition: ensightFile.C:143