foamVtkLagrangianWriter.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-------------------------------------------------------------------------------
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
29#include "Cloud.H"
30#include "passiveParticle.H"
31
32// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33
34Foam::fileName Foam::vtk::lagrangianWriter::cloudDir() const
35{
36 return (cloud::prefix/cloudName_);
37}
38
39
40Foam::pointField Foam::vtk::lagrangianWriter::positions() const
41{
42 Cloud<passiveParticle> parcels(mesh_, cloudName_, false);
43
44 pointField pts(parcels.size());
45
46 auto outIter = pts.begin();
47
48 for (const auto& p : parcels)
49 {
50 *outIter = p.position();
51 ++outIter;
52 }
53
54 return pts;
55}
56
57
58void Foam::vtk::lagrangianWriter::writeVerts()
59{
60 // No collectives - can skip on sub-processes
61 if (!format_) return;
62
63 const label nVerts = numberOfPoints_;
64
65 // Same payload for connectivity and offsets
66 const uint64_t payLoad = vtk::sizeofData<label>(nVerts);
67
68
70
71 //
72 // 'connectivity'
73 // = linear mapping onto points
74 //
75 {
76 format().beginDataArray<label>(vtk::dataArrayAttr::CONNECTIVITY);
77 format().writeSize(payLoad);
78
79 vtk::writeIdentity(format(), nVerts);
80
81 format().flush();
82 format().endDataArray();
83 }
84
85 //
86 // 'offsets' (connectivity offsets)
87 // = linear mapping onto points (with 1 offset)
88 //
89 {
90 format().beginDataArray<label>(vtk::dataArrayAttr::OFFSETS);
91 format().writeSize(payLoad);
92
93 vtk::writeIdentity(format(), nVerts, 1);
94
95 format().flush();
96 format().endDataArray();
97 }
98
100}
101
102
104{
105 return enter_CellData(numberOfPoints_, nFields);
106}
107
108
110{
111 return enter_PointData(numberOfPoints_, nFields);
112}
113
114
115// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
116
118(
119 const fvMesh& mesh,
120 const word& cloudName,
121 const vtk::outputOptions opts,
122 bool useVerts
123)
124:
125 vtk::fileWriter(vtk::fileTag::POLY_DATA, opts),
126 mesh_(mesh),
127 cloudName_(cloudName),
128 numberOfPoints_(0),
129 useVerts_(useVerts)
130{
131 opts_.append(false); // No append mode (horrible for streaming)
132 opts_.legacy(false); // Disallow legacy (see notes)
133}
134
135
137(
138 const fvMesh& mesh,
139 const word& cloudName,
140 const fileName& file,
141 bool parallel
142)
143:
145{
146 open(file, parallel);
147}
148
149
151(
152 const fvMesh& mesh,
153 const word& cloudName,
154 const vtk::outputOptions opts,
155 const fileName& file,
156 bool parallel
157)
158:
160{
161 open(file, parallel);
162}
163
164
165// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
166
168{
169 if (title.size())
170 {
171 return vtk::fileWriter::beginFile(title);
172 }
173
174 // Provide default title
175
176 // XML (inline)
177
179 (
180 "case='" + mesh_.time().globalCaseName()
181 + "' cloud='" + cloudName_
182 + "' time='" + mesh_.time().timeName()
183 + "' index='" + Foam::name(mesh_.time().timeIndex())
184 + "'"
185 );
186}
187
188
190{
191 enter_Piece();
192
193 if (isState(outputState::CELL_DATA))
194 {
195 ++nCellData_;
196 }
197 else if (isState(outputState::POINT_DATA))
198 {
199 ++nPointData_;
200 }
201
202 // Transcribe cloud into pointField
203 pointField cloudPoints(positions());
204
205 numberOfPoints_ = cloudPoints.size();
206
207 if (parallel_)
208 {
209 reduce(numberOfPoints_, sumOp<label>());
210 }
211
212
213 // beginPiece()
214 if (format_)
215 {
216 if (useVerts_)
217 {
218 format()
219 .tag
220 (
222 fileAttr::NUMBER_OF_POINTS, numberOfPoints_,
223 fileAttr::NUMBER_OF_VERTS, numberOfPoints_
224 );
225 }
226 else
227 {
228 format()
229 .tag
230 (
232 fileAttr::NUMBER_OF_POINTS, numberOfPoints_
233 );
234 }
235 }
236
237
238 // writePoints()
239 {
240 if (format_)
241 {
242 const uint64_t payLoad =
243 vtk::sizeofData<float,3>(numberOfPoints_);
244
246 .beginDataArray<float,3>(vtk::dataArrayAttr::POINTS);
247
248 format().writeSize(payLoad);
249 }
250
251 if (parallel_)
252 {
253 vtk::writeListParallel(format_.ref(), cloudPoints);
254 }
255 else
256 {
257 vtk::writeList(format(), cloudPoints);
258 }
259
260
261 if (format_)
262 {
263 format().flush();
264 format().endDataArray();
265
266 // Non-legacy
267 format()
268 .endTag(vtk::fileTag::POINTS);
269 }
270 }
271
272 if (useVerts_) writeVerts();
273
274 return true;
275}
276
277
279{
280 if (useVerts_)
281 {
282 return beginCellData();
283 }
284 else
285 {
286 return beginPointData();
287 }
288}
289
290
292{
293 if (useVerts_)
294 {
295 return endCellData();
296 }
297 else
298 {
299 return endPointData();
300 }
301}
302
303
304// ************************************************************************* //
writer beginPointData(1)
writer beginCellData(4)
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
static const word prefix
The prefix to local: lagrangian.
Definition: cloud.H:87
A class for handling file names.
Definition: fileName.H:76
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:91
Base class for VTK output writers that handle geometry and fields (eg, vtp, vtu data)....
bool parallel() const noexcept
Parallel output requested?
vtk::outputOptions opts_
Requested output options.
virtual bool open(const fileName &file, bool parallel=Pstream::parRun())
Open file for writing (creates parent directory).
virtual bool beginFile(std::string title="")
Write file header (non-collective)
formatter & beginPointData()
Begin "PointData" XML section.
formatter & beginCellData()
Begin "CellData" XML section.
Write lagrangian (cloud) positions and fields (as PointData) in VTP format. Legacy VTK format is inte...
bool beginParcelData()
Begin parcel (PointData) output section.
bool endParcelData()
Explicitly end parcel (PointData) output and switch to PIECE state.
virtual bool writeGeometry()
Write cloud positions.
virtual bool beginFile(std::string title="")
Write file header (non-collective)
Encapsulated combinations of output format options. This is primarily useful when defining the output...
bool legacy() const noexcept
True if writer uses legacy file format.
bool append() const noexcept
True if output format uses an append mode.
A class for handling words, derived from Foam::string.
Definition: word.H:68
volScalarField & p
dynamicFvMesh & mesh
@ NUMBER_OF_VERTS
"NumberOfVerts"
@ NUMBER_OF_POINTS
"NumberOfPoints"
void writeIdentity(vtk::formatter &fmt, const label len, label start=0)
Write an identity list of labels.
Definition: foamVtkOutput.C:96
@ CONNECTIVITY
"connectivity"
fileTag
Some common XML tags for vtk files.
Definition: foamVtkCore.H:114
@ POLY_DATA
"PolyData"
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.
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:44
void reduce(const List< UPstream::commsStruct > &comms, T &value, const BinaryOp &bop, const int tag, const label comm)
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
word format(conversionProperties.get< word >("format"))
const word cloudName(propsDict.get< word >("cloud"))