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-2023 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 "foamEndian.H" // For byte swapping
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 
56 (
57  std::ostream& os
58 )
59 :
60  vtk::formatter(os)
61 {}
62 
63 
64 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
65 
68 {
69  return opts_;
70 }
71 
72 
73 const char* Foam::vtk::legacyRawFormatter::name() const
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 
103 void Foam::vtk::legacyRawFormatter::write(const label val)
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 
123 void Foam::vtk::legacyRawFormatter::write(const float val)
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 
141 void Foam::vtk::legacyRawFormatter::write(const double val)
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 // ************************************************************************* //
virtual const char * encoding() const
Name for the XML append encoding (unused)
Abstract class for a VTK output stream formatter.
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
virtual bool writeSize(const uint64_t ignored)
Write leading size - a no-op for legacy binary output.
void write(const char *s, std::streamsize n)
Write.
static uint32_t swap32(uint32_t)
Byte endian swapping for 32-bits.
Definition: foamEndianI.H:54
virtual void flush()
Write a newline to the output.
Encapsulated combinations of output format options. This is primarily useful when defining the output...
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
std::ostream & os() noexcept
Access to the underlying output stream.
legacyRawFormatter(const legacyRawFormatter &)=delete
No copy construct.
OBJstream os(runTime.globalPath()/outputName)
virtual const char * name() const
Name for the legacy binary output type ("BINARY")
label n
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))
static uint64_t swap64(uint64_t)
Byte endian swapping for 64-bits.
Definition: foamEndianI.H:76
virtual const vtk::outputOptions & opts() const
The output is LEGACY_BINARY.