foamVtkAppendRawFormatter.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 <limits>
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 const char* Foam::vtk::appendRawFormatter::name_ = "append";
35 const char* Foam::vtk::appendRawFormatter::encoding_ = "raw";
36 
38 Foam::vtk::appendRawFormatter::opts_(formatType::APPEND_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  vtk::formatter(os),
58  offset_(0)
59 {}
60 
61 
62 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
63 
66 {
67  return opts_;
68 }
69 
70 
71 const char* Foam::vtk::appendRawFormatter::name() const
72 {
73  return name_;
74 }
75 
76 
78 {
79  return encoding_;
80 }
81 
82 
83 uint64_t Foam::vtk::appendRawFormatter::offset(const uint64_t numbytes)
84 {
85  uint64_t prev = offset_;
86 
87  if (formatter::npos != numbytes)
88  {
89  offset_ += this->encodedLength(sizeof(uint64_t) + numbytes);
90  }
91  return prev;
92 }
93 
94 
95 bool Foam::vtk::appendRawFormatter::writeSize(const uint64_t numbytes)
96 {
97  write(reinterpret_cast<const char*>(&numbytes), sizeof(uint64_t));
98  return true;
99 }
100 
102 void Foam::vtk::appendRawFormatter::write(const uint8_t val)
103 {
104  write(reinterpret_cast<const char*>(&val), sizeof(uint8_t));
105 }
106 
107 
109 {
110  // std::cerr<<"label:" << sizeof(val) << "=" << val << '\n';
111  write(reinterpret_cast<const char*>(&val), sizeof(label));
112 }
113 
114 
116 {
117  // std::cerr<<"float:" << sizeof(val) << "=" << val << '\n';
118  write(reinterpret_cast<const char*>(&val), sizeof(float));
119 }
120 
121 
122 void Foam::vtk::appendRawFormatter::write(const double val)
123 {
124  // std::cerr<<"double as float=" << val << '\n';
125 
126  // Limit range of double to float conversion
127  if (val >= std::numeric_limits<float>::max())
128  {
130  }
131  else if (val <= std::numeric_limits<float>::lowest())
132  {
133  write(std::numeric_limits<float>::lowest());
134  }
135  else
136  {
137  float copy(val);
138  write(copy);
139  }
140 }
141 
142 
144 {/*nop*/}
145 
146 
147 // ************************************************************************* //
Abstract class for a VTK output stream formatter.
virtual const char * name() const
Output name for XML type ("append")
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
appendRawFormatter(const appendRawFormatter &)=delete
No copy construct.
virtual void flush()
A no-op for this format.
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.
virtual uint64_t offset(const uint64_t numbytes)
Increase the append data offset by numbytes and sizeof(uint64_t).
static constexpr uint64_t npos
Out of range position or size.
virtual const vtk::outputOptions & opts() const
The output is APPEND_BINARY.
virtual bool writeSize(const uint64_t numbytes)
Write leading size for binary output.
OBJstream os(runTime.globalPath()/outputName)
label n
virtual const char * encoding() const
Output name for append encoding type ("raw")
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))
void write(const char *s, std::streamsize n)
Write.