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-2020 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 "UList.H"
32 #include <cstring>
33 #include <sstream>
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 bool Foam::ensightFile::allowUndef_ = false;
38 
39 Foam::scalar Foam::ensightFile::undefValue_ = Foam::floatScalarVGREAT;
40 
41 // Default is width 8
42 Foam::string Foam::ensightFile::mask_ = "********";
43 Foam::string Foam::ensightFile::dirFmt_ = "%08d";
44 
45 const char* const Foam::ensightFile::coordinates = "coordinates";
46 
47 
48 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
49 
51 {
52  return mask_;
53 }
54 
55 
57 {
58  char buf[32];
59 
60  sprintf(buf, dirFmt_.c_str(), n);
61  return buf;
62 }
63 
64 
66 {
67  // enforce max limit to avoid buffer overflow in subDir()
68  if (n < 1 || n > 31)
69  {
70  return;
71  }
72 
73  // appropriate printf format
74  std::ostringstream oss;
75  oss << "%0" << n << "d";
76  dirFmt_ = oss.str();
77 
78  // set mask accordingly
79  mask_.resize(n, '*');
80 }
81 
82 
84 {
85  return mask_.size();
86 }
87 
88 
90 {
91  for (const scalar& val : field)
92  {
93  if (std::isnan(val))
94  {
95  return true;
96  }
97  }
98 
99  return true;
100 }
101 
102 
103 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
104 
105 void Foam::ensightFile::initialize()
106 {
107  // ascii formatting specs
108  setf
109  (
111  ios_base::floatfield
112  );
113  precision(5);
114 }
115 
116 
117 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
118 
119 Foam::ensightFile::ensightFile
120 (
121  const fileName& pathname,
123 )
124 :
125  OFstream(ensight::FileName(pathname), format)
126 {
127  initialize();
128 }
129 
130 
131 Foam::ensightFile::ensightFile
132 (
133  const fileName& path,
134  const fileName& name,
136 )
137 :
139 {
140  initialize();
141 }
142 
143 
144 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
145 
147 {
148  return allowUndef_;
149 }
150 
151 
153 {
154  bool old = allowUndef_;
155  allowUndef_ = enabled;
156  return old;
157 }
158 
159 
160 Foam::scalar Foam::ensightFile::undefValue(const scalar value)
161 {
162  // enable its use too
163  allowUndef_ = true;
164 
165  scalar old = undefValue_;
166  undefValue_ = value;
167  return old;
168 }
169 
170 
172 {
173  // Output 80 chars, but allocate for trailing nul character
174  // to avoid -Wstringop-truncation warnings/errors.
175 
176  char buf[80+1];
177  strncpy(buf, str, 80); // max 80 chars or padded with nul if smaller
178 
179  if (format() == IOstream::BINARY)
180  {
181  write(buf, 80);
182  }
183  else
184  {
185  buf[79] = 0; // max 79 in ASCII, ensure it is indeed nul-terminated
186  stdStream() << buf;
187  }
188 
189  return *this;
190 }
191 
192 
194 {
195  return writeString(str.c_str());
196 }
197 
198 
200 {
201  return writeString(str);
202 }
203 
204 
206 {
207  return writeString(str);
208 }
209 
210 
212 {
213  return writeString(str);
214 }
215 
216 
218 (
219  const char* buf,
220  std::streamsize count
221 )
222 {
223  stdStream().write(buf, count);
224  return *this;
225 }
226 
227 
229 {
230  if (format() == IOstream::BINARY)
231  {
232  write
233  (
234  reinterpret_cast<const char *>(&val),
235  sizeof(int32_t)
236  );
237  }
238  else
239  {
240  stdStream().width(10);
241  stdStream() << val;
242  }
243 
244  return *this;
245 }
246 
247 
249 {
250  int32_t ivalue(narrowInt32(val));
251 
252  return write(ivalue);
253 }
254 
255 
257 {
258  if (format() == IOstream::BINARY)
259  {
260  write
261  (
262  reinterpret_cast<const char *>(&val),
263  sizeof(floatScalar)
264  );
265  }
266  else
267  {
268  stdStream().width(12);
269  stdStream() << val;
270  }
271 
272  return *this;
273 }
274 
275 
277 {
278  float fvalue(narrowFloat(val));
279 
280  return write(fvalue);
281 }
282 
283 
285 (
286  const label value,
287  const label fieldWidth
288 )
289 {
290  if (format() == IOstream::BINARY)
291  {
292  write(value);
293  }
294  else
295  {
296  stdStream().width(fieldWidth);
297  stdStream() << value;
298  }
299 
300  return *this;
301 }
302 
303 
305 {
306  if (format() == IOstream::ASCII)
307  {
308  stdStream() << nl;
309  }
310 }
311 
312 
314 {
315  write(undefValue_);
316  return *this;
317 }
318 
319 
321 {
322  if (allowUndef_)
323  {
324  writeString(key + " undef");
325  newline();
326  write(undefValue_);
327  newline();
328  }
329  else
330  {
331  writeString(key);
332  newline();
333  }
334 
335  return *this;
336 }
337 
338 
340 {
341  if (format() == IOstream::BINARY)
342  {
343  writeString("C Binary");
344  }
345 
346  return *this;
347 }
348 
349 
350 //
351 // Convenience Output Methods
352 //
353 
354 void Foam::ensightFile::beginPart(const label index)
355 {
356  writeString("part");
357  newline();
358  write(index+1); // Ensight starts with 1
359  newline();
360 }
361 
362 
364 {
365  writeString("particle coordinates");
366  newline();
367  write(nparticles, 8); // unusual width
368  newline();
369 }
370 
371 
373 {
374  for (const label val : list)
375  {
376  write(val);
377  newline();
378  }
379 }
380 
381 
383 {
384  for (const label val : field)
385  {
386  write(scalar(val));
387  newline();
388  }
389 }
390 
391 
393 {
394  for (const scalar val : field)
395  {
396  if (std::isnan(val))
397  {
398  writeUndef();
399  }
400  else
401  {
402  write(val);
403  }
404  newline();
405  }
406 }
407 
408 
409 // ************************************************************************* //
Foam::setf
Smanip< ios_base::fmtflags > setf(const ios_base::fmtflags flags)
Definition: IOmanip.H:169
Foam::doubleScalar
double doubleScalar
A typedef for double.
Definition: scalarFwd.H:48
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::floatScalar
float floatScalar
A typedef for float.
Definition: scalarFwd.H:45
Foam::ensightFile::mask
static string mask()
The '*' mask appropriate for subDir.
Definition: ensightFile.C:50
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Foam::ensightFile::writeBinaryHeader
Ostream & writeBinaryHeader()
Write "C Binary" string for binary files (eg, geometry/measured)
Definition: ensightFile.C:339
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:76
Foam::ensightFile::writeKeyword
virtual Ostream & writeKeyword(const keyType &key)
Definition: ensightFile.C:320
Foam::ensightFile::writeLabels
void writeLabels(const UList< label > &list)
Write a list of integers.
Definition: ensightFile.C:372
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:68
n
label n
Definition: TABSMDCalcMethod2.H:31
format
word format(conversionProperties.get< word >("format"))
Foam::ensight::FileName
Specification of a valid Ensight file-name.
Definition: ensightFileName.H:60
Foam::ensightFile::beginParticleCoordinates
void beginParticleCoordinates(const label nparticles)
Begin a "particle coordinates" block (measured data)
Definition: ensightFile.C:363
error.H
ensightFile.H
Foam::ensightFile::newline
void newline()
Add carriage return to ascii stream.
Definition: ensightFile.C:304
Foam::ensightFile::writeUndef
Ostream & writeUndef()
Write undef value.
Definition: ensightFile.C:313
Foam::ensightFile::subDirWidth
static label subDirWidth()
Return current width of subDir and mask.
Definition: ensightFile.C:83
field
rDeltaTY field()
Foam::ensightFile::subDir
static string subDir(const label)
Consistent zero-padded numbers for subdirectories.
Definition: ensightFile.C:56
Foam::ensightFile::allowUndef
static bool allowUndef()
Return setting for whether 'undef' values are allowed in results.
Definition: ensightFile.C:146
Foam::ensightFile::coordinates
static const char *const coordinates
The keyword "coordinates".
Definition: ensightFile.H:88
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
Foam::ensightFile::write
virtual bool write(const token &)
Writing token does not make sense.
Definition: ensightFile.H:176
Foam::narrowFloat
float narrowFloat(const double val)
Type narrowing from double to float.
Definition: scalar.H:199
Foam::scientific
IOstream & scientific(IOstream &io)
Definition: IOstream.H:464
Foam::ensightFile::isUndef
static bool isUndef(const UList< scalar > &field)
Check for any NaN in the field.
Definition: ensightFile.C:89
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
Foam::ensightFile::undefValue
static scalar undefValue(const scalar value)
Assign the value to represent undef in the results.
Definition: ensightFile.C:160
Foam::IOstreamOption::BINARY
"binary"
Definition: IOstreamOption.H:73
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
Foam::ensightFile::writeString
Ostream & writeString(const char *str)
Write C-string as "%79s" or as binary (max 80 chars)
Definition: ensightFile.C:171
UList.H
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::UList
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:103
path
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
Foam::ensightFile::beginPart
void beginPart(const label index)
Begin a part (0-based index internally).
Definition: ensightFile.C:354
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Definition: foamVtkOutputTemplates.C:36
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::ensightFile::writeList
void writeList(const UList< label > &field)
Write a list of integers as float values.
Definition: ensightFile.C:382
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::floatScalarVGREAT
constexpr floatScalar floatScalarVGREAT
Definition: floatScalar.H:59
Foam::narrowInt32
int32_t narrowInt32(const int64_t val)
Type narrowing from int64_t to int32_t.
Definition: label.H:174