foamVtkIndPatchWriter.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) 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 
28 #include "foamVtkIndPatchWriter.H"
29 #include "foamVtkOutput.H"
30 #include "globalIndex.H"
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 void Foam::vtk::indirectPatchWriter::beginPiece()
35 {
36  // Basic sizes
37  nLocalPoints_ = pp_.nPoints();
38  nLocalFaces_ = pp_.size();
39  nLocalVerts_ = 0;
40 
41  for (const face& f : pp_)
42  {
43  nLocalVerts_ += f.size();
44  }
45 
46  numberOfPoints_ = nLocalPoints_;
47  numberOfCells_ = nLocalFaces_;
48 
49  if (parallel_)
50  {
51  reduce(numberOfPoints_, sumOp<label>());
52  reduce(numberOfCells_, sumOp<label>());
53  }
54 
55 
56  // Nothing else to do for legacy
57  if (legacy()) return;
58 
59  if (format_)
60  {
61  format().tag
62  (
64  vtk::fileAttr::NUMBER_OF_POINTS, numberOfPoints_,
65  vtk::fileAttr::NUMBER_OF_POLYS, numberOfCells_
66  );
67  }
68 }
69 
70 
71 void Foam::vtk::indirectPatchWriter::writePoints()
72 {
73  if (format_)
74  {
75  if (legacy())
76  {
77  legacy::beginPoints(os_, numberOfPoints_);
78  }
79  else
80  {
81  const uint64_t payLoad = vtk::sizeofData<float, 3>(numberOfPoints_);
82 
83  format()
85  .beginDataArray<float,3>(vtk::dataArrayAttr::POINTS);
86 
87  format().writeSize(payLoad);
88  }
89  }
90 
91 
92  if (parallel_ ? Pstream::master() : true)
93  {
94  {
95  vtk::writeList(format(), pp_.localPoints());
96  }
97  }
98 
99 
100  if (parallel_)
101  {
102  if (Pstream::master())
103  {
104  pointField recv;
105 
106  // Receive each point field and write
107  for
108  (
109  int slave=Pstream::firstSlave();
110  slave<=Pstream::lastSlave();
111  ++slave
112  )
113  {
114  IPstream fromSlave(Pstream::commsTypes::blocking, slave);
115 
116  {
117  fromSlave >> recv;
118 
119  vtk::writeList(format(), recv);
120  }
121  }
122  }
123  else
124  {
125  // Send to master
126  OPstream toMaster
127  (
130  );
131 
132  {
133  toMaster << pp_.localPoints();
134  }
135  }
136  }
137 
138 
139  if (format_)
140  {
141  format().flush();
142  format().endDataArray();
143 
144  if (!legacy())
145  {
146  format()
147  .endTag(vtk::fileTag::POINTS);
148  }
149  }
150 }
151 
152 
153 void Foam::vtk::indirectPatchWriter::writePolysLegacy(const label pointOffset)
154 {
155  // Connectivity count without additional storage (done internally)
156 
157  label nFaces = nLocalFaces_;
158  label nVerts = nLocalVerts_;
159 
160  if (parallel_)
161  {
162  reduce(nFaces, sumOp<label>());
163  reduce(nVerts, sumOp<label>());
164  }
165 
166  if (nFaces != numberOfCells_)
167  {
169  << "Expecting " << numberOfCells_
170  << " faces, but found " << nFaces
171  << exit(FatalError);
172  }
173 
174  legacy::beginPolys(os_, nFaces, nVerts);
175 
176  labelList vertLabels(nLocalFaces_ + nLocalVerts_);
177 
178  {
179  // Legacy: size + connectivity together
180  // [nPts, id1, id2, ..., nPts, id1, id2, ...]
181 
182  auto iter = vertLabels.begin();
183 
184  label off = pointOffset;
185 
186  {
187  for (const face& f : pp_.localFaces())
188  {
189  *iter = f.size(); // The size prefix
190  ++iter;
191 
192  for (const label pfi : f)
193  {
194  *iter = pfi + off; // Face vertex label
195  ++iter;
196  }
197  }
198  // off += pp_.nPoints();
199  }
200  }
201 
202 
203  if (parallel_)
204  {
205  vtk::writeListParallel(format_.ref(), vertLabels);
206  }
207  else
208  {
209  vtk::writeList(format(), vertLabels);
210  }
211 
212  if (format_)
213  {
214  format().flush();
215  }
216 }
217 
218 
219 void Foam::vtk::indirectPatchWriter::writePolys(const label pointOffset)
220 {
221  if (format_)
222  {
224  }
225 
226  //
227  // 'connectivity'
228  //
229  {
230  labelList vertLabels(nLocalVerts_);
231 
232  label nVerts = nLocalVerts_;
233 
234  if (parallel_)
235  {
236  reduce(nVerts, sumOp<label>());
237  }
238 
239  if (format_)
240  {
241  const uint64_t payLoad = vtk::sizeofData<label>(nVerts);
242 
243  format().beginDataArray<label>(vtk::dataArrayAttr::CONNECTIVITY);
244  format().writeSize(payLoad * sizeof(label));
245  }
246 
247  {
248  // XML: connectivity only
249  // [id1, id2, ..., id1, id2, ...]
250 
251  auto iter = vertLabels.begin();
252 
253  label off = pointOffset;
254 
255  {
256  for (const face& f : pp_.localFaces())
257  {
258  for (const label pfi : f)
259  {
260  *iter = pfi + off; // Face vertex label
261  ++iter;
262  }
263  }
264  // off += pp_.nPoints();
265  }
266  }
267 
268 
269  if (parallel_)
270  {
271  vtk::writeListParallel(format_.ref(), vertLabels);
272  }
273  else
274  {
275  vtk::writeList(format(), vertLabels);
276  }
277 
278  if (format_)
279  {
280  format().flush();
281  format().endDataArray();
282  }
283  }
284 
285 
286  //
287  // 'offsets' (connectivity offsets)
288  //
289  {
290  labelList vertOffsets(nLocalFaces_);
291  label nOffs = vertOffsets.size();
292 
293  if (parallel_)
294  {
295  reduce(nOffs, sumOp<label>());
296  }
297 
298  if (format_)
299  {
300  const uint64_t payLoad = vtk::sizeofData<label>(nOffs);
301 
302  format().beginDataArray<label>(vtk::dataArrayAttr::OFFSETS);
303  format().writeSize(payLoad);
304  }
305 
306 
307  // processor-local connectivity offsets
308  label off =
309  (
310  parallel_ ? globalIndex(nLocalVerts_).localStart() : 0
311  );
312 
313  auto iter = vertOffsets.begin();
314 
315  {
316  for (const face& f : pp_)
317  {
318  off += f.size(); // End offset
319  *iter = off;
320  ++iter;
321  }
322  }
323 
324 
325  if (parallel_)
326  {
327  vtk::writeListParallel(format_.ref(), vertOffsets);
328  }
329  else
330  {
331  vtk::writeList(format_.ref(), vertOffsets);
332  }
333 
334 
335  if (format_)
336  {
337  format().flush();
338  format().endDataArray();
339  }
340  }
341 
342  if (format_)
343  {
344  format().endTag(vtk::fileTag::POLYS);
345  }
346 }
347 
348 
349 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
350 
351 Foam::vtk::indirectPatchWriter::indirectPatchWriter
352 (
353  const indirectPrimitivePatch& pp,
354  const vtk::outputOptions opts
355 )
356 :
358  pp_(pp),
359  numberOfPoints_(0),
360  numberOfCells_(0),
361  nLocalPoints_(0),
362  nLocalFaces_(0),
363  nLocalVerts_(0)
364 {
365  // We do not currently support append mode
366  opts_.append(false);
367 }
368 
369 
370 Foam::vtk::indirectPatchWriter::indirectPatchWriter
371 (
372  const indirectPrimitivePatch& pp,
373  const fileName& file,
374  bool parallel
375 )
376 :
378 {
379  open(file, parallel);
380 }
381 
382 
383 Foam::vtk::indirectPatchWriter::indirectPatchWriter
384 (
385  const indirectPrimitivePatch& pp,
386  const vtk::outputOptions opts,
387  const fileName& file,
388  bool parallel
389 )
390 :
391  indirectPatchWriter(pp, opts)
392 {
393  open(file, parallel);
394 }
395 
396 
397 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
398 
400 {
401  if (title.size())
402  {
403  return vtk::fileWriter::beginFile(title);
404  }
405 
406  // Provide default title
407  return vtk::fileWriter::beginFile("surfaces");
408 }
409 
410 
412 {
413  enter_Piece();
414 
415  beginPiece();
416 
417  writePoints();
418 
419  const label pointOffset =
420  (
421  parallel_ ? globalIndex(nLocalPoints_).localStart() : 0
422  );
423 
424  if (legacy())
425  {
426  writePolysLegacy(pointOffset);
427  }
428  else
429  {
430  writePolys(pointOffset);
431  }
432 
433  return true;
434 }
435 
436 
438 {
439  return enter_CellData(numberOfCells_, nFields);
440 }
441 
442 
444 {
445  return enter_PointData(numberOfPoints_, nFields);
446 }
447 
448 
449 // ************************************************************************* //
Foam::vtk::outputOptions
Encapsulated combinations of output format options. This is primarily useful when defining the output...
Definition: foamVtkOutputOptions.H:59
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:71
Foam::vtk::indirectPatchWriter
Write indirectPrimitivePatch faces/points (optionally with fields) as a vtp file or a legacy vtk file...
Definition: foamVtkIndPatchWriter.H:65
Foam::pointField
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:44
Foam::vtk::writeListParallel
void writeListParallel(vtk::formatter &fmt, const UList< uint8_t > &values)
Write a list of uint8_t values.
Definition: foamVtkOutput.C:126
Foam::UPstream::commsTypes::blocking
foamVtkOutput.H
Foam::vtk::fileWriter
Base class for VTK output writers that handle geometry and fields (eg, vtp, vtu data)....
Definition: foamVtkFileWriter.H:66
Foam::UPstream::masterNo
static constexpr int masterNo() noexcept
Process index of the master.
Definition: UPstream.H:433
Foam::vtk::fileWriter::legacy
bool legacy() const
Commonly used query.
Definition: foamVtkFileWriterI.H:74
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::vtk::indirectPatchWriter::writeGeometry
virtual bool writeGeometry()
Write patch topology.
Definition: foamVtkIndPatchWriter.C:411
Foam::globalIndex::localStart
label localStart() const
My local start.
Definition: globalIndexI.H:112
Foam::vtk::dataArrayAttr::OFFSETS
"offsets"
foamVtkIndPatchWriter.H
globalIndex.H
Foam::vtk::fileTag::POLY_DATA
"PolyData"
Foam::vtk::legacy::beginPolys
void beginPolys(std::ostream &os, label nPolys, label nConnectivity)
Emit header for POLYGONS (with trailing newline).
Definition: foamVtkOutputI.H:121
Foam::vtk::fileWriter::beginFile
virtual bool beginFile(std::string title="")
Write file header (non-collective)
Definition: foamVtkFileWriter.C:317
Foam::vtk::fileTag::POLYS
"Polys"
Foam::vtk::fileWriter::format_
autoPtr< vtk::formatter > format_
The VTK formatter in use (master process)
Definition: foamVtkFileWriter.H:110
format
word format(conversionProperties.get< word >("format"))
Foam::reduce
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
Definition: PstreamReduceOps.H:51
Foam::vtk::indirectPatchWriter::beginCellData
virtual bool beginCellData(label nFields=0)
Begin CellData output section for specified number of fields.
Definition: foamVtkIndPatchWriter.C:437
Foam::UPstream::lastSlave
static int lastSlave(const label communicator=0)
Process index of last slave.
Definition: UPstream.H:468
Foam::vtk::fileAttr::NUMBER_OF_POINTS
"NumberOfPoints"
Foam::vtk::writeList
void writeList(vtk::formatter &fmt, const UList< uint8_t > &values)
Write a list of uint8_t values.
Definition: foamVtkOutput.C:112
Foam::vtk::legacy::beginPoints
void beginPoints(std::ostream &os, label nPoints)
Emit header for POINTS (with trailing newline).
Definition: foamVtkOutputI.H:113
Foam::PrimitivePatch::nPoints
label nPoints() const
Return number of points supporting patch faces.
Definition: PrimitivePatch.H:316
Foam::vtk::fileWriter::parallel_
bool parallel_
Writing in parallel (via master)
Definition: foamVtkFileWriter.H:95
Foam::FatalError
error FatalError
Foam::vtk::fileTag::POINTS
"Points"
Foam::globalIndex
Calculates a unique integer (label so might not have enough room - 2G max) for processor + local inde...
Definition: globalIndex.H:68
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:439
Foam::UPstream::firstSlave
static constexpr int firstSlave() noexcept
Process index of first slave.
Definition: UPstream.H:462
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:372
Foam::vtk::indirectPatchWriter::beginPointData
virtual bool beginPointData(label nFields=0)
Begin PointData for specified number of fields.
Definition: foamVtkIndPatchWriter.C:443
f
labelList f(nPoints)
Foam::vtk::formatter::tag
formatter & tag(const word &t, Args &&... args)
Write XML tag without any attributes. Combines openTag/closeTag.
Foam::vtk::dataArrayAttr::CONNECTIVITY
"connectivity"
Foam::vtk::fileTag::PIECE
"Piece"
Foam::vtk::fileWriter::format
vtk::formatter & format()
The VTK formatter in use.
Definition: foamVtkFileWriterI.H:36
Foam::vtk::dataArrayAttr::POINTS
"Points"
Foam::vtk::indirectPatchWriter::beginFile
virtual bool beginFile(std::string title="")
Write file header (non-collective)
Definition: foamVtkIndPatchWriter.C:399
Foam::vtk::fileAttr::NUMBER_OF_POLYS
"NumberOfPolys"
Foam::PrimitivePatch
A list of faces which address into the list of points.
Definition: PrimitivePatch.H:85