foamVtkLegacyRawFormatter.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) 2016-2018 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
29 #include "foamVtkOutputOptions.H"
30 #include "endian.H"
31 #include <limits>
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 const char* Foam::vtk::legacyRawFormatter::legacyName_ = "BINARY";
36 
38 Foam::vtk::legacyRawFormatter::opts_(formatType::LEGACY_BINARY);
39 
40 
41 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
42 
44 (
45  const char* s,
46  std::streamsize n
47 )
48 {
49  os().write(s, n);
50 }
51 
52 
53 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
54 
55 Foam::vtk::legacyRawFormatter::legacyRawFormatter
56 (
57  std::ostream& os
58 )
59 :
60  formatter(os)
61 {}
62 
63 
64 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
65 
68 {
69  return opts_;
70 }
71 
72 
74 {
75  return legacyName_;
76 }
77 
78 
80 {
81  return legacyName_;
82 }
83 
84 
86 {
87  return false;
88 }
89 
90 
92 (
93  const uint8_t val
94 )
95 {
96  // Legacy can only handle 32-bit integers.
97  // Nonetheless promote to 'label' (32 or 64 bit) and deal with it later
98  label copy(val);
99  write(copy);
100 }
101 
102 
104 {
105  // std::cerr<<"label is:" << sizeof(val) << '\n';
106 
107  // Not entirely correct: the legacy format only supports 32-bit integers.
108  // Either limit size for 64-bit label, or simply do not support for 64-bit.
109 #ifdef WM_LITTLE_ENDIAN
110 # if WM_LABEL_SIZE == 32
111  uint32_t swapped = endian::swap32(val);
112  write(reinterpret_cast<const char*>(&swapped), sizeof(uint32_t));
113 # elif WM_LABEL_SIZE == 64
114  uint64_t swapped = endian::swap64(val);
115  write(reinterpret_cast<const char*>(&swapped), sizeof(uint64_t));
116 #endif
117 #else
118  write(reinterpret_cast<const char*>(&val), sizeof(label));
119 #endif
120 }
121 
122 
124 {
125  // std::cerr<<"float is:" << sizeof(val) << '\n';
126 
127 #ifdef WM_LITTLE_ENDIAN
128  // De-reference in two stages to avoid the warning
129  // dereferencing type-punned pointer will break strict-aliasing rules
130  // [-Wstrict-aliasing]
131 
132  const uint32_t* ptr = reinterpret_cast<const uint32_t*>(&val);
133  uint32_t swapped = endian::swap32(*ptr);
134  write(reinterpret_cast<const char*>(&swapped), sizeof(uint32_t));
135 #else
136  write(reinterpret_cast<const char*>(&val), sizeof(float));
137 #endif
138 }
139 
140 
142 {
143  // Legacy cannot support Float64 anyhow.
144  // std::cerr<<"write double as float:" << val << '\n';
145 
146  // Limit range of double to float conversion
147  if (val >= std::numeric_limits<float>::max())
148  {
150  }
151  else if (val <= std::numeric_limits<float>::lowest())
152  {
153  write(std::numeric_limits<float>::lowest());
154  }
155  else
156  {
157  float copy(val);
158  write(copy);
159  }
160 }
161 
162 
164 {
165  os()<< '\n';
166 }
167 
168 
169 // ************************************************************************* //
Foam::vtk::outputOptions
Encapsulated combinations of output format options. This is primarily useful when defining the output...
Definition: foamVtkOutputOptions.H:59
s
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputSpray.H:25
Foam::vtk::legacyRawFormatter::encoding
virtual const char * encoding() const
Name for the XML append encoding (unused)
Definition: foamVtkLegacyRawFormatter.C:79
foamVtkLegacyRawFormatter.H
Foam::vtk::legacyRawFormatter::write
void write(const char *s, std::streamsize n)
Write.
Definition: foamVtkLegacyRawFormatter.C:44
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::vtk::legacyRawFormatter::writeSize
virtual bool writeSize(const uint64_t ignored)
Write leading size - a no-op for legacy binary output.
Definition: foamVtkLegacyRawFormatter.C:85
Foam::vtk::legacyRawFormatter::write
virtual void write(const label val)
Definition: foamVtkLegacyRawFormatter.C:103
Foam::vtk::legacyRawFormatter::name
virtual const char * name() const
Name for the legacy binary output type ("BINARY")
Definition: foamVtkLegacyRawFormatter.C:73
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
os
OBJstream os(runTime.globalPath()/outputName)
Foam::endian::swap32
static uint32_t swap32(uint32_t)
Byte endian swapping for 32-bits.
Definition: endianI.H:61
Foam::vtk::legacyRawFormatter::flush
virtual void flush()
Write a newline to the output.
Definition: foamVtkLegacyRawFormatter.C:163
Foam::vtk::legacyRawFormatter::opts
virtual const outputOptions & opts() const
The output is LEGACY_BINARY.
Definition: foamVtkLegacyRawFormatter.C:67
foamVtkOutputOptions.H
Foam::endian::swap64
static uint64_t swap64(uint64_t)
Byte endian swapping for 64-bits.
Definition: endianI.H:83
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
endian.H
Help with architecture-specific aspects.
Foam::vtk::formatter
Abstract class for a VTK output stream formatter.
Definition: foamVtkFormatter.H:68