foamVtkOutputI.H
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) 2017-2022 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 
28 // * * * * * * * * * * * * * * Specializations * * * * * * * * * * * * * * * //
29 
30 namespace Foam
31 {
32 namespace vtk
33 {
34 
35 //- Template specialization for label
36 template<>
37 inline void write<label>(vtk::formatter& fmt, const label& val, const label n)
38 {
39  for (label i=0; i < n; ++i)
40  {
41  fmt.write(val);
42  }
43 }
44 
45 
46 //- Template specialization for float
47 template<>
48 inline void write<float>(vtk::formatter& fmt, const float& val, const label n)
49 {
50  for (label i=0; i < n; ++i)
51  {
52  fmt.write(val);
53  }
54 }
55 
56 
57 //- Template specialization for double
58 template<>
59 inline void write<double>(vtk::formatter& fmt, const double& val, const label n)
60 {
61  for (label i=0; i < n; ++i)
62  {
63  fmt.write(val);
64  }
65 }
66 
67 
68 //- Template specialization for symmTensor ordering
69 // VTK order is (XX, YY, ZZ, XY, YZ, XZ)
70 template<>
71 inline void write(vtk::formatter& fmt, const symmTensor& val, const label n)
72 {
73  for (label i=0; i < n; ++i)
74  {
75  fmt.write(component(val, symmTensor::XX));
76  fmt.write(component(val, symmTensor::YY));
77  fmt.write(component(val, symmTensor::ZZ));
78  fmt.write(component(val, symmTensor::XY));
79  fmt.write(component(val, symmTensor::YZ));
80  fmt.write(component(val, symmTensor::XZ));
81  }
82 }
83 
84 
85 } // End namespace vtk
86 } // End namespace Foam
87 
88 
89 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
90 
92 (
93  vtk::formatter& fmt,
94  const std::string& title,
95  vtk::fileTag contentType
96 )
97 {
98  legacy::fileHeader(fmt, title, legacy::contentNames[contentType]);
99 }
100 
101 
102 template<Foam::vtk::fileTag ContentType>
104 (
105  vtk::formatter& fmt,
106  const std::string& title
107 )
108 {
109  legacy::fileHeader(fmt, title, legacy::contentNames[ContentType]);
110 }
111 
112 
113 inline void Foam::vtk::legacy::beginPoints(std::ostream& os, label nPoints)
114 {
115  os << nl
116  << legacy::fileTagNames[vtk::fileTag::POINTS]
117  << ' ' << nPoints
118  << " float" << nl;
119 }
120 
121 
123 (
124  std::ostream& os,
125  label nVerts,
126  label nConnectivity
127 )
128 {
129  if (!nConnectivity)
130  {
131  nConnectivity = nVerts;
132  }
133  os << nl
134  << legacy::fileTagNames[vtk::fileTag::VERTS]
135  << ' ' << nVerts
136  << ' ' << (nVerts + nConnectivity) << nl;
137 }
138 
139 
141 (
142  std::ostream& os,
143  label nLines,
144  label nConnectivity
145 )
146 {
147  if (!nConnectivity)
148  {
149  nConnectivity = 2*nLines;
150  }
151  os << nl
152  << legacy::fileTagNames[vtk::fileTag::LINES]
153  << ' ' << nLines
154  << ' ' << (nLines + nConnectivity) << nl;
155 }
156 
157 
159 (
160  std::ostream& os,
161  label nPolys,
162  label nConnectivity
163 )
164 {
165  os << nl
166  << legacy::fileTagNames[vtk::fileTag::POLYS]
167  << ' ' << nPolys
168  << ' ' << (nPolys + nConnectivity) << nl;
169 }
170 
171 
173 (
174  vtk::formatter& fmt,
175  label nFields
176 )
177 {
178  fmt.os()
179  << "FIELD FieldData " << nFields << nl;
180 }
181 
182 
184 (
185  vtk::formatter& fmt,
186  label nFields
187 )
188 {
189  legacy::fieldData(fmt, nFields);
190 }
191 
192 
194 (
195  vtk::formatter& fmt,
196  label nCells,
197  label nFields
198 )
199 {
200  fmt.os()
201  << nl
202  << legacy::fileTagNames[vtk::fileTag::CELL_DATA]
203  << ' ' << nCells << nl;
204  legacy::fieldData(fmt, nFields);
205 }
206 
207 
209 (
210  vtk::formatter& fmt,
211  label nPoints,
212  label nFields
213 )
214 {
215  fmt.os()
216  << nl
218  << ' ' << nPoints << nl;
219  legacy::fieldData(fmt, nFields);
220 }
221 
222 
224 (
225  vtk::formatter& fmt,
226  scalar timeValue
227 )
228 {
229  legacy::floatField<1>(fmt, "TimeValue", 1);
230  fmt.write(timeValue);
231  fmt.flush();
232 }
233 
234 
235 template<Foam::direction nComp>
237 (
238  vtk::formatter& fmt,
239  const word& fieldName,
240  label nEntries
241 )
242 {
243  fmt.os()
244  << fieldName << ' '
245  << int(nComp) << ' ' << nEntries << " double" << nl;
246 }
247 
248 
249 template<Foam::direction nComp>
251 (
252  vtk::formatter& fmt,
253  const word& fieldName,
254  label nEntries
255 )
256 {
257  fmt.os()
258  << fieldName << ' '
259  << int(nComp) << ' ' << nEntries << " float" << nl;
260 }
261 
262 
263 template<Foam::direction nComp>
264 inline void Foam::vtk::legacy::intField
265 (
266  vtk::formatter& fmt,
267  const word& fieldName,
268  label nEntries
269 )
270 {
271  fmt.os()
272  << fieldName << ' '
273  << int(nComp) << ' ' << nEntries << " int" << nl;
274 }
275 
276 
277 // ************************************************************************* //
void intField(vtk::formatter &fmt, const word &name, const label nEntries)
Start output of int field with the specified name.
Abstract class for a VTK output stream formatter.
void write< label >(vtk::formatter &fmt, const label &val, const label n)
Template specialization for label.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
void beginFieldData(vtk::formatter &fmt, label nFields)
Emit legacy FIELD FieldData nFields.
void beginPolys(std::ostream &os, label nPolys, label nConnectivity)
Emit header for POLYGONS (with trailing newline).
void beginPointData(vtk::formatter &fmt, label nPoints, label nFields)
Emit legacy POINT_DATA nPoints, FIELD FieldData nFields.
void fieldData(vtk::formatter &fmt, label nFields)
Emit "FIELD FieldData <n>".
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
void beginPoints(std::ostream &os, label nPoints)
Emit header for POINTS (with trailing newline).
std::ostream & os() noexcept
Access to the underlying output stream.
void fileHeader(std::ostream &os, const std::string &title, bool binary)
Emit header for legacy file (vtk DataFile Version 2.0)
A class for handling words, derived from Foam::string.
Definition: word.H:63
label nPoints
void writeTimeValue(vtk::formatter &fmt, scalar timeValue)
Emit "TimeValue" for a FIELD entry (name as per Catalyst output)
void beginCellData(vtk::formatter &fmt, label nCells, label nFields)
Emit legacy CELL_DATA nCells, FIELD FieldData nFields.
void beginLines(std::ostream &os, label nLines, label nConnectivity=0)
Emit header for LINES (with trailing newline).
OBJstream os(runTime.globalPath()/outputName)
const Foam::Enum< fileTag > fileTagNames
Strings corresponding to the vtk XML tags.
void write< double >(vtk::formatter &fmt, const double &val, const label n)
Template specialization for double.
fileTag
Some common XML tags for vtk files.
Definition: foamVtkCore.H:122
virtual void write(const uint8_t val)=0
void doubleField(vtk::formatter &fmt, const word &name, const label nEntries)
Start output of double field with the specified name.
label n
void write< float >(vtk::formatter &fmt, const float &val, const label n)
Template specialization for float.
const Foam::Enum< vtk::fileTag > contentNames
Legacy content names (POLYDATA, UNSTRUCTURED_GRID)
void floatField(vtk::formatter &fmt, const word &name, const label nEntries)
Start output of float field with the specified name.
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
void beginVerts(std::ostream &os, label nVerts, label nConnectivity=0)
Emit header for VERTICES (with trailing newline).
Namespace for OpenFOAM.