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 -------------------------------------------------------------------------------
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 "Cloud.H"
30 #include "passiveParticle.H"
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 Foam::fileName Foam::vtk::lagrangianWriter::cloudDir() const
35 {
36  return (cloud::prefix/cloudName_);
37 }
38 
39 
40 Foam::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 
58 void 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 
99  format().endTag(vtk::fileTag::VERTS);
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 
117 Foam::vtk::lagrangianWriter::lagrangianWriter
118 (
119  const fvMesh& mesh,
120  const word& cloudName,
121  const vtk::outputOptions opts,
122  bool useVerts
123 )
124 :
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 
136 Foam::vtk::lagrangianWriter::lagrangianWriter
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 
150 Foam::vtk::lagrangianWriter::lagrangianWriter
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 // ************************************************************************* //
Foam::vtk::outputOptions
Encapsulated combinations of output format options. This is primarily useful when defining the output...
Definition: foamVtkOutputOptions.H:59
Foam::pointField
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:44
Foam::vtk::lagrangianWriter
Write lagrangian (cloud) positions and fields (as PointData) in VTP format. Legacy VTK format is inte...
Definition: foamVtkLagrangianWriter.H:70
Foam::cloud::prefix
static const word prefix
The prefix to local: lagrangian.
Definition: cloud.H:87
Foam::vtk::fileWriter
Base class for VTK output writers that handle geometry and fields (eg, vtp, vtu data)....
Definition: foamVtkFileWriter.H:66
p
volScalarField & p
Definition: createFieldRefs.H:8
cloudName
const word cloudName(propsDict.get< word >("cloud"))
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
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:73
Foam::vtk::lagrangianWriter::beginCellData
virtual bool beginCellData(label nFields=0)
Begin CellData output section for specified number of fields.
Definition: foamVtkLagrangianWriter.C:103
Foam::vtk::dataArrayAttr::OFFSETS
"offsets"
Foam::vtk::fileTag::POLY_DATA
"PolyData"
Foam::vtk::writeIdentity
void writeIdentity(vtk::formatter &fmt, const label len, label start=0)
Write an identity list of labels.
Definition: foamVtkOutput.C:96
Cloud.H
Foam::vtk::legacy::beginPointData
void beginPointData(vtk::formatter &fmt, label nPoints, label nFields)
Emit legacy POINT_DATA nPoints, FIELD FieldData nFields.
Definition: foamVtkOutputI.H:187
Foam::vtk::fileWriter::beginFile
virtual bool beginFile(std::string title="")
Write file header (non-collective)
Definition: foamVtkFileWriter.C:393
Foam::vtk::lagrangianWriter::beginParcelData
bool beginParcelData()
Begin parcel (PointData) output section.
Definition: foamVtkLagrangianWriter.C:278
foamVtkLagrangianWriter.H
Foam::sumOp
Definition: ops.H:213
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::Field
Generic templated field type.
Definition: Field.H:63
Foam::vtk::fileAttr::NUMBER_OF_POINTS
"NumberOfPoints"
passiveParticle.H
Foam::vtk::fileTag::VERTS
"Verts"
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::lagrangianWriter::endParcelData
bool endParcelData()
Explicitly end parcel (PointData) output and switch to PIECE state.
Definition: foamVtkLagrangianWriter.C:291
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::vtk::fileTag::POINTS
"Points"
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:85
Foam::vtk::fileTag::POINT_DATA
"PointData"
Foam::vtk::legacy::beginCellData
void beginCellData(vtk::formatter &fmt, label nCells, label nFields)
Emit legacy CELL_DATA nCells, FIELD FieldData nFields.
Definition: foamVtkOutputI.H:172
Foam::vtk::lagrangianWriter::writeGeometry
virtual bool writeGeometry()
Write cloud positions.
Definition: foamVtkLagrangianWriter.C:189
Foam::vtk::lagrangianWriter::beginFile
virtual bool beginFile(std::string title="")
Write file header (non-collective)
Definition: foamVtkLagrangianWriter.C:167
Foam::vtk::dataArrayAttr::CONNECTIVITY
"connectivity"
Foam::vtk::fileTag::CELL_DATA
"CellData"
Foam::vtk::fileAttr::NUMBER_OF_VERTS
"NumberOfVerts"
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::vtk::fileTag::PIECE
"Piece"
Foam::vtk::dataArrayAttr::POINTS
"Points"
Foam::vtk::writeListParallel
void writeListParallel(vtk::formatter &fmt, const UList< Type > &values)
Write a list of values.
Definition: foamVtkOutputTemplates.C:164
Foam::vtk::lagrangianWriter::beginPointData
virtual bool beginPointData(label nFields=0)
Begin PointData for specified number of fields.
Definition: foamVtkLagrangianWriter.C:109