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-------------------------------------------------------------------------------
11License
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
37bool Foam::ensightFile::allowUndef_ = false;
38
39Foam::scalar Foam::ensightFile::undefValue_ = Foam::floatScalarVGREAT;
40
41// Default is width 8
42Foam::string Foam::ensightFile::mask_ = "********";
43Foam::string Foam::ensightFile::dirFmt_ = "%08d";
44
45const 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
105void Foam::ensightFile::initialize()
106{
107 // ascii formatting specs
108 setf
109 (
110 ios_base::scientific,
111 ios_base::floatfield
112 );
113 precision(5);
114}
115
116
117// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
118
120(
121 const fileName& pathname,
123)
124:
125 OFstream(ensight::FileName(pathname), format)
126{
127 initialize();
128}
129
130
132(
133 const fileName& path,
134 const fileName& name,
136)
137:
138 OFstream(path/ensight::FileName(name), format)
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
160Foam::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
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{
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{
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{
291 {
292 write(value);
293 }
294 else
295 {
296 stdStream().width(fieldWidth);
297 stdStream() << value;
298 }
299
300 return *this;
301}
302
303
305{
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{
342 {
343 writeString("C Binary");
344 }
345
346 return *this;
347}
348
349
350//
351// Convenience Output Methods
352//
353
354void 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// ************************************************************************* //
label n
streamFormat
Data format (ascii | binary)
@ ASCII
"ascii" (normal default)
Output to file stream, using an OSstream.
Definition: OFstream.H:57
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
Ensight output with specialized write() for strings, integers and floats. Correctly handles binary wr...
Definition: ensightFile.H:55
Ostream & writeBinaryHeader()
Write "C Binary" string for binary files (eg, geometry/measured)
Definition: ensightFile.C:339
static const char *const coordinates
The keyword "coordinates".
Definition: ensightFile.H:88
static bool isUndef(const UList< scalar > &field)
Check for any NaN in the field.
Definition: ensightFile.C:89
virtual Ostream & writeKeyword(const keyType &key)
Definition: ensightFile.C:320
void beginPart(const label index)
Begin a part (0-based index internally).
Definition: ensightFile.C:354
static bool allowUndef()
Return setting for whether 'undef' values are allowed in results.
Definition: ensightFile.C:146
static label subDirWidth()
Return current width of subDir and mask.
Definition: ensightFile.C:83
void writeList(const UList< label > &field)
Write a list of integers as float values.
Definition: ensightFile.C:382
void writeLabels(const UList< label > &list)
Write a list of integers.
Definition: ensightFile.C:372
Ostream & writeUndef()
Write undef value.
Definition: ensightFile.C:313
static scalar undefValue(const scalar value)
Assign the value to represent undef in the results.
Definition: ensightFile.C:160
static string subDir(const label)
Consistent zero-padded numbers for subdirectories.
Definition: ensightFile.C:56
void beginParticleCoordinates(const label nparticles)
Begin a "particle coordinates" block (measured data)
Definition: ensightFile.C:363
Ostream & writeString(const char *str)
Write C-string as "%79s" or as binary (max 80 chars)
Definition: ensightFile.C:171
void newline()
Add carriage return to ascii stream.
Definition: ensightFile.C:304
static string mask()
The '*' mask appropriate for subDir.
Definition: ensightFile.C:50
A class for handling file names.
Definition: fileName.H:76
virtual bool write()
Write the output fields.
A class for handling keywords in dictionaries.
Definition: keyType.H:71
A class for handling character strings derived from std::string.
Definition: string.H:79
A class for handling words, derived from Foam::string.
Definition: word.H:68
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
rDeltaTY field()
Smanip< ios_base::fmtflags > setf(const ios_base::fmtflags flags)
Definition: IOmanip.H:169
constexpr floatScalar floatScalarVGREAT
Definition: floatScalar.H:59
float narrowFloat(const double val)
Type narrowing from double to float.
Definition: scalar.H:199
double doubleScalar
A typedef for double.
Definition: scalarFwd.H:48
int32_t narrowInt32(const int64_t val)
Type narrowing from int64_t to int32_t.
Definition: label.H:175
float floatScalar
A typedef for float.
Definition: scalarFwd.H:45
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
runTime write()
word format(conversionProperties.get< word >("format"))