foamVtkOutput.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-2022 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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
28#include "foamVtkOutput.H"
29
30#include "foamVtkFormatter.H"
37#include "foamVersion.H"
38#include "typeInfo.H"
39#include "globalIndex.H"
40#include "instant.H"
41#include "Fstream.H"
42#include "Pstream.H"
43#include "OSspecific.H"
44
45// * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
46
48Foam::vtk::newFormatter(std::ostream& os, unsigned prec)
49{
51}
52
53
56(
57 std::ostream& os,
58 const enum formatType fmtType,
59 unsigned prec
60)
61{
63
64 switch (fmtType)
65 {
66 case formatType::INLINE_ASCII:
67 fmt.reset(new vtk::asciiFormatter(os, prec));
68 break;
69
70 case formatType::INLINE_BASE64:
72 break;
73
74 case formatType::APPEND_BASE64:
76 break;
77
78 case formatType::APPEND_BINARY:
80 break;
81
82 case formatType::LEGACY_ASCII:
83 fmt.reset(new vtk::legacyAsciiFormatter(os, prec));
84 break;
85
86 case formatType::LEGACY_BINARY:
88 break;
89 }
90
91 return fmt;
92}
93
94
96(
97 vtk::formatter& fmt,
98 const label len,
99 label start
100)
101{
102 // No nComponents for label, can use fmt.write() directly
103 for (label i=0; i < len; ++i)
104 {
105 fmt.write(start);
106 ++start;
107 }
108}
109
110
112(
113 vtk::formatter& fmt,
114 const UList<uint8_t>& values
115)
116{
117 // No nComponents for char, can use fmt.write() directly
118 for (const uint8_t val : values)
119 {
120 fmt.write(val);
121 }
122}
123
124
126(
127 vtk::formatter& fmt,
128 const labelUList& values,
129 const globalIndex& procOffset
130)
131{
132 // Gather sizes (offsets irrelevant)
133 const globalIndex procAddr(values.size(), globalIndex::gatherOnly{});
134
135 if (Pstream::master())
136 {
137 // Write master data - with value offset
138 const label offsetId = procOffset.localStart(0);
139 for (const label val : values)
140 {
141 vtk::write(fmt, val + offsetId);
142 }
143
144 // Receive and write
145 DynamicList<label> recvData(procAddr.maxNonLocalSize());
146
147 for (const label proci : procAddr.subProcs())
148 {
149 recvData.resize_nocopy(procAddr.localSize(proci));
150 UIPstream::read
151 (
152 UPstream::commsTypes::scheduled,
153 proci,
154 recvData.data_bytes(),
155 recvData.size_bytes()
156 );
157
158 // With value offset
159 const label offsetId = procOffset.localStart(proci);
160 for (const label val : recvData)
161 {
162 vtk::write(fmt, val + offsetId);
163 }
164 }
165 }
166 else
167 {
168 // Send
169 UOPstream::write
170 (
171 UPstream::commsTypes::scheduled,
172 UPstream::masterNo(),
173 values.cdata_bytes(),
174 values.size_bytes()
175 );
176 }
177}
178
179
180// * * * * * * * * * * * * * * Legacy Functions * * * * * * * * * * * * * * //
181
183(
184 std::ostream& os,
185 const std::string& title,
186 bool binary
187)
188{
189 // Line 1:
190 os << "# vtk DataFile Version 2.0" << nl;
191
192 // OR
193 // os << "# vtk DataFile Version 5.1" << nl;
194
195 // Line 2: title
196
197 const auto truncate = title.find('\n');
198
199 if (title.empty() || 0 == truncate)
200 {
201 // Avoid an empty title
202 os << "File generated by OpenFOAM " << foamVersion::api << nl;
203 }
204 else if (std::string::npos == truncate)
205 {
206 os << title << nl;
207 }
208 else
209 {
210 os << title.substr(0, truncate) << nl;
211 }
212
213 // Line 3: format
214 os << (binary ? "BINARY" : "ASCII") << nl;
215}
216
217
219(
220 vtk::formatter& fmt,
221 const std::string& title,
222 const std::string& contentType
223)
224{
225 std::ostream& os = fmt.os();
226
227 legacy::fileHeader(os, title, isType<legacyRawFormatter>(fmt));
228 if (contentType.size())
229 {
230 os << "DATASET " << contentType.c_str() << nl;
231 }
232}
233
234
235// ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
globalIndex procAddr(aMesh.nFaces())
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:72
void resize_nocopy(const label len)
Definition: DynamicListI.H:363
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
char * data_bytes() noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:251
std::streamsize size_bytes() const noexcept
Number of contiguous bytes for the List data.
Definition: UListI.H:258
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
void reset(autoPtr< T > &&other) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:117
Calculates a unique integer (label so might not have enough room - 2G max) for processor + local inde...
Definition: globalIndex.H:68
label localStart() const
My local start.
Definition: globalIndexI.H:195
Appended base-64 encoded binary output. Uses an output filter layer to write base-64 encoded content.
Inline ASCII output. Adds spaces between entries and a newline every 9 items (for consistency with wh...
Inline base-64 encoded binary output. Uses an output filter layer to write base-64 encoded content.
Abstract class for a VTK output stream formatter.
virtual void write(const uint8_t val)=0
std::ostream & os() noexcept
Access to the underlying output stream.
Formatting as per Foam::vtk::asciiFormatter, but with naming for legacy output.
Binary output for the VTK legacy format, always written as big-endian and with 32-bit integers.
OBJstream os(runTime.globalPath()/outputName)
void fileHeader(std::ostream &os, const std::string &title, bool binary)
Emit header for legacy file (vtk DataFile Version 2.0)
void writeIdentity(vtk::formatter &fmt, const label len, label start=0)
Write an identity list of labels.
Definition: foamVtkOutput.C:96
formatType
The output format type for file contents.
Definition: foamVtkCore.H:66
autoPtr< vtk::formatter > newFormatter(std::ostream &os, unsigned prec=IOstream::defaultPrecision())
Return a default asciiFormatter.
Definition: foamVtkOutput.C:48
void writeList(vtk::formatter &fmt, const UList< uint8_t > &values)
Write a list of uint8_t values.
void writeListParallel(vtk::formatter &fmt, const UList< Type > &values)
Write a list of values.
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53