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-2019 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 
33 #include <cstring>
34 #include <sstream>
35 
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 
38 bool Foam::ensightFile::allowUndef_ = false;
39 
40 Foam::scalar Foam::ensightFile::undefValue_ = Foam::floatScalarVGREAT;
41 
42 // default is width 8
43 Foam::string Foam::ensightFile::mask_ = "********";
44 Foam::string Foam::ensightFile::dirFmt_ = "%08d";
45 
46 
47 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
48 
50 {
51  return mask_;
52 }
53 
54 
56 {
57  char buf[32];
58 
59  sprintf(buf, dirFmt_.c_str(), n);
60  return buf;
61 }
62 
63 
65 {
66  // enforce max limit to avoid buffer overflow in subDir()
67  if (n < 1 || n > 31)
68  {
69  return;
70  }
71 
72  // appropriate printf format
73  std::ostringstream oss;
74  oss << "%0" << n << "d";
75  dirFmt_ = oss.str();
76 
77  // set mask accordingly
78  mask_.resize(n, '*');
79 }
80 
81 
83 {
84  return mask_.size();
85 }
86 
87 
88 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
89 
90 void Foam::ensightFile::initialize()
91 {
92  // ascii formatting specs
93  setf
94  (
96  ios_base::floatfield
97  );
98  precision(5);
99 }
100 
101 
102 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
103 
104 Foam::ensightFile::ensightFile
105 (
106  const fileName& pathname,
108 )
109 :
110  OFstream(ensight::FileName(pathname), format)
111 {
112  initialize();
113 }
114 
115 
116 Foam::ensightFile::ensightFile
117 (
118  const fileName& path,
119  const fileName& name,
121 )
122 :
124 {
125  initialize();
126 }
127 
128 
129 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
130 
132 {
133  return allowUndef_;
134 }
135 
136 
138 {
139  bool old = allowUndef_;
140  allowUndef_ = enabled;
141  return old;
142 }
143 
144 
145 Foam::scalar Foam::ensightFile::undefValue(const scalar value)
146 {
147  // enable its use too
148  allowUndef_ = true;
149 
150  scalar old = undefValue_;
151  undefValue_ = value;
152  return old;
153 }
154 
155 
157 (
158  const char* buf,
159  std::streamsize count
160 )
161 {
162  stdStream().write(buf, count);
163  return *this;
164 }
165 
166 
168 {
169  // Parentheses around strncpy to silence the GCC -Wstringop-truncation
170  // warning, which is spurious here.
171  // The max-size and buffer-size *are* identical, which means the buffer
172  // may not have a nul terminator. However, this is properly handled in
173  // the subsequent binary write and the ASCII write explicitly adds
174  // a nul terminator.
175 
176  char buf[80];
177  (strncpy(buf, value, 80)); // max 80 chars or padded with nul if smaller
178 
179  if (format() == IOstream::BINARY)
180  {
181  write(buf, sizeof(buf));
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 write(value.c_str());
196 }
197 
198 
200 {
201  if (format() == IOstream::BINARY)
202  {
203  unsigned int ivalue(value);
204 
205  write
206  (
207  reinterpret_cast<const char *>(&ivalue),
208  sizeof(ivalue)
209  );
210  }
211  else
212  {
213  stdStream().width(10);
214  stdStream() << value;
215  }
216 
217  return *this;
218 }
219 
220 
222 (
223  const label value,
224  const label fieldWidth
225 )
226 {
227  if (format() == IOstream::BINARY)
228  {
229  unsigned int ivalue(value);
230 
231  write
232  (
233  reinterpret_cast<const char *>(&ivalue),
234  sizeof(ivalue)
235  );
236  }
237  else
238  {
239  stdStream().width(fieldWidth);
240  stdStream() << value;
241  }
242 
243  return *this;
244 }
245 
246 
248 {
249  float fvalue(value);
250 
251  if (format() == IOstream::BINARY)
252  {
253  write
254  (
255  reinterpret_cast<const char *>(&fvalue),
256  sizeof(fvalue)
257  );
258  }
259  else
260  {
261  stdStream().width(12);
262  stdStream() << fvalue;
263  }
264 
265  return *this;
266 }
267 
268 
270 {
271  if (format() == IOstream::ASCII)
272  {
273  stdStream() << nl;
274  }
275 }
276 
277 
279 {
280  write(undefValue_);
281  return *this;
282 }
283 
284 
286 {
287  if (allowUndef_)
288  {
289  write(string(static_cast<const string&>(key) + " undef"));
290  newline();
291  write(undefValue_);
292  newline();
293  }
294  else
295  {
296  // ensure we get ensightFile::write(const string&)
297  write(static_cast<const string&>(key));
298  newline();
299  }
300  return *this;
301 }
302 
303 
305 {
306  if (format() == IOstream::BINARY)
307  {
308  write("C Binary");
309  }
310 
311  return *this;
312 }
313 
314 
315 //
316 // Convenience Output Methods
317 //
318 
320 {
321  write("part");
322  newline();
323  write(index+1); // Ensight starts with 1
324  newline();
325 }
326 
327 
329 {
330  write("particle coordinates");
331  newline();
332  write(nparticles, 8); // unusual width
333  newline();
334 }
335 
336 
338 {
339  for (const label val : field)
340  {
341  write(scalar(val));
342  newline();
343  }
344 }
345 
346 
348 {
349  for (const scalar& val : field)
350  {
351  if (std::isnan(val))
352  {
353  writeUndef();
354  }
355  else
356  {
357  write(val);
358  }
359 
360  newline();
361  }
362 }
363 
364 
365 
367 (
368  const UList<scalar>& field,
369  const labelUList& addr
370 )
371 {
372  for (const label id : addr)
373  {
374  if (id < 0 || id >= field.size() || std::isnan(field[id]))
375  {
376  writeUndef();
377  }
378  else
379  {
380  write(field[id]);
381  }
382 
383  newline();
384  }
385 }
386 
387 
388 // ************************************************************************* //
Foam::setf
Smanip< ios_base::fmtflags > setf(const ios_base::fmtflags flags)
Definition: IOmanip.H:169
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::ensightFile::mask
static string mask()
The '*' mask appropriate for subDir.
Definition: ensightFile.C:49
Foam::ensightFile::writeBinaryHeader
Ostream & writeBinaryHeader()
Write "C Binary" for binary files (eg, geometry/measured)
Definition: ensightFile.C:304
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:73
Foam::ensightFile::writeKeyword
virtual Ostream & writeKeyword(const keyType &key)
Write element keyword with trailing newline, optionally with undef.
Definition: ensightFile.C:285
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:60
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:56
Foam::ensightFile::beginParticleCoordinates
void beginParticleCoordinates(const label nparticles)
Begin a "particle coordinates" block (measured data)
Definition: ensightFile.C:328
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
error.H
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
ensightFile.H
Foam::ensightFile::newline
void newline()
Add carriage return to ascii stream.
Definition: ensightFile.C:269
Foam::ensightFile::writeUndef
Ostream & writeUndef()
Write undef value.
Definition: ensightFile.C:278
Foam::ensightFile::subDirWidth
static label subDirWidth()
Return current width of subDir and mask.
Definition: ensightFile.C:82
Foam::ensightFile::write
virtual Ostream & write(const char *buf, std::streamsize count)
Binary write.
Definition: ensightFile.C:157
field
rDeltaTY field()
Foam::ensightFile::subDir
static string subDir(const label)
Consistent zero-padded numbers for subdirectories.
Definition: ensightFile.C:55
Foam::ensightFile::allowUndef
static bool allowUndef()
Return setting for whether 'undef' values are allowed in results.
Definition: ensightFile.C:131
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:64
Foam::Ostream::write
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Foam::scientific
IOstream & scientific(IOstream &io)
Definition: IOstream.H:447
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:99
Foam::ensightFile::undefValue
static scalar undefValue(const scalar value)
Assign the value to represent undef in the results.
Definition: ensightFile.C:145
Foam::IOstreamOption::BINARY
"binary"
Definition: IOstreamOption.H:67
Foam::IOstreamOption::ASCII
"ascii"
Definition: IOstreamOption.H:66
UList.H
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:74
Foam::UList< label >
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:319
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:35
Foam::ensightFile::writeList
void writeList(const UList< label > &field)
Write a list of integers as float values.
Definition: ensightFile.C:337
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:57