foamVtkSurfaceFieldWriter.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 "emptyFvsPatchFields.H"
30 #include "fvsPatchFields.H"
31 #include "surfaceFields.H"
32 
33 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
34 
35 Foam::List<Foam::vector> Foam::vtk::surfaceFieldWriter::flattenBoundary
36 (
38 ) const
39 {
40  // Boundary field - flatten
41 
42  List<vector> flat(mesh_.nBoundaryFaces(), Zero);
43 
44  forAll(field.boundaryField(), patchi)
45  {
46  const polyPatch& pp = mesh_.boundaryMesh()[patchi];
47  const auto& pfld = field.boundaryField()[patchi];
48 
49  if (!isA<emptyFvsPatchVectorField>(pfld))
50  {
51  SubList<vector>(flat, pp.size(), pp.offset()) = pfld;
52  }
53  }
54 
55  return flat;
56 }
57 
58 
59 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
60 
61 Foam::vtk::surfaceFieldWriter::surfaceFieldWriter
62 (
63  const fvMesh& mesh,
64  const vtk::outputOptions opts
65 )
66 :
68  mesh_(mesh),
69  numberOfPoints_(0)
70 {
71  opts_.append(false); // No append mode (horrible for streaming)
72  opts_.legacy(false); // Disallow legacy (inconvenient)
73 }
74 
75 
76 Foam::vtk::surfaceFieldWriter::surfaceFieldWriter
77 (
78  const fvMesh& mesh,
79  const fileName& file,
80  bool parallel
81 )
82 :
84 {
85  open(file, parallel);
86 }
87 
88 
89 Foam::vtk::surfaceFieldWriter::surfaceFieldWriter
90 (
91  const fvMesh& mesh,
92  const vtk::outputOptions opts,
93  const fileName& file,
94  bool parallel
95 )
96 :
98 {
99  open(file, parallel);
100 }
101 
102 
103 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
104 
106 {
107  if (title.size())
108  {
109  return vtk::fileWriter::beginFile(title);
110  }
111 
112 
113  // Provide Default title
114 
115  if (legacy())
116  {
117  return vtk::fileWriter::beginFile("surfaceFields");
118  }
119 
120 
121  // XML (inline)
122 
124  (
125  "surfaceFields "
126  "case='" + mesh_.time().globalCaseName()
127  + "' region='" + mesh_.name()
128  + "' time='" + mesh_.time().timeName()
129  + "' index='" + Foam::name(mesh_.time().timeIndex())
130  + "'"
131  );
132 }
133 
134 
136 {
137  enter_Piece();
138 
139  // Output
140 
141  const pointField& centres = mesh_.faceCentres();
142 
143  // PointData for each face.
144  numberOfPoints_ = centres.size();
145 
146  if (parallel_)
147  {
148  reduce(numberOfPoints_, sumOp<label>());
149  }
150 
151  // <Piece>
152  if (format_)
153  {
154  format()
155  .tag
156  (
158  fileAttr::NUMBER_OF_POINTS, numberOfPoints_
159  );
160  }
161 
162 
163  // <Point>
164  if (format_)
165  {
166  const uint64_t payLoad =
167  vtk::sizeofData<float,3>(numberOfPoints_);
168 
170  .beginDataArray<float,3>(vtk::dataArrayAttr::POINTS);
171 
172  format().writeSize(payLoad);
173  }
174 
175  if (parallel_)
176  {
177  // Internal faces
179  (
180  format_.ref(),
181  SubList<point>(centres, mesh_.nInternalFaces())
182  );
183 
184  // Boundary faces
186  (
187  format_.ref(),
188  SubList<point>(centres, mesh_.boundaryMesh().range())
189  );
190  }
191  else
192  {
193  // Non-parallel: use a normal write
194 
195  vtk::writeList(format(), centres);
196  }
197 
198  if (format_)
199  {
200  format().flush();
201 
202  // Non-legacy
203  format()
204  .endDataArray()
205  .endTag(vtk::fileTag::POINTS);
206  }
207 
208  return true;
209 }
210 
211 
213 {
214  // No legacy, no CellData
215  return enter_CellData(0, 0);
216 }
217 
218 
220 {
221  // No legacy
222  return enter_PointData(numberOfPoints_, 0);
223 }
224 
225 
227 {
228  if (isState(outputState::POINT_DATA))
229  {
230  ++nPointData_;
231  }
232  else
233  {
235  << "Bad writer state (" << stateNames[state_]
236  << ") - should be (" << stateNames[outputState::POINT_DATA]
237  << ") for field " << field.name() << nl << endl
238  << exit(FatalError);
239  }
240 
241  label nFaces = field.mesh().nFaces();
242 
243  if (parallel_)
244  {
245  reduce(nFaces, sumOp<label>());
246  }
247 
248  if (nFaces != numberOfPoints_)
249  {
251  << "Expecting " << numberOfPoints_
252  << " faces, but found " << nFaces
253  << exit(FatalError);
254  }
255 
256  if (format_)
257  {
258  // Non-legacy
259  const uint64_t payLoad =
260  vtk::sizeofData<float, 3>(nFaces);
261 
262  format().beginDataArray<float, 3>(field.name());
263  format().writeSize(payLoad);
264  }
265 
266 
267  // Internal field
268  const SubList<vector> internal(field, mesh_.nInternalFaces());
269 
270  // Boundary field (flattened)
271  auto boundary(flattenBoundary(field));
272 
273 
274  if (parallel_)
275  {
276  // Internal field
277  vtk::writeListParallel(format_.ref(), internal);
278 
279  // Boundary field
280  vtk::writeListParallel(format_.ref(), boundary);
281  }
282  else
283  {
284  // Non-parallel
285 
286  // Internal field
287  vtk::writeList(format_.ref(), internal);
288 
289  // Boundary field
290  vtk::writeList(format_.ref(), boundary);
291  }
292 
293 
294  if (format_)
295  {
296  format().flush();
297  format().endDataArray();
298  }
299 }
300 
301 
302 // ************************************************************************* //
Foam::vtk::outputOptions
Encapsulated combinations of output format options. This is primarily useful when defining the output...
Definition: foamVtkOutputOptions.H:59
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::vtk::fileWriter
Base class for VTK output writers that handle geometry and fields (eg, vtp, vtu data)....
Definition: foamVtkFileWriter.H:66
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::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::SubList
A List obtained as a section of another List.
Definition: SubList.H:53
Foam::vtk::fileTag::POLY_DATA
"PolyData"
Foam::vtk::surfaceFieldWriter::beginCellData
virtual bool beginCellData(label nFields=0)
Begin CellData output section for specified number of fields.
Definition: foamVtkSurfaceFieldWriter.C:212
Foam::vtk::fileWriter::beginFile
virtual bool beginFile(std::string title="")
Write file header (non-collective)
Definition: foamVtkFileWriter.C:317
Foam::polyMesh::boundaryMesh
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:444
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
surfaceFields.H
Foam::surfaceFields.
Foam::vtk::surfaceFieldWriter::beginFile
virtual bool beginFile(std::string title="")
Write file header (non-collective)
Definition: foamVtkSurfaceFieldWriter.C:105
Foam::vtk::surfaceFieldWriter::write
void write(const surfaceVectorField &field)
Write field.
Definition: foamVtkSurfaceFieldWriter.C:226
Foam::sumOp
Definition: ops.H:213
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::vtk::surfaceFieldWriter::beginPointData
virtual bool beginPointData(label nFields=0)
Begin PointData output section.
Definition: foamVtkSurfaceFieldWriter.C:219
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< vector >
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::vtk::fileAttr::NUMBER_OF_POINTS
"NumberOfPoints"
Foam::vtk::surfaceFieldWriter::writeGeometry
virtual bool writeGeometry()
Write cloud positions.
Definition: foamVtkSurfaceFieldWriter.C:135
emptyFvsPatchFields.H
field
rDeltaTY field()
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::surfaceFieldWriter
Write surfaces fields (as PointData) in VTP format. Legacy VTK format is intentionally not supported.
Definition: foamVtkSurfaceFieldWriter.H:71
Foam::FatalError
error FatalError
Foam::primitiveMesh::nBoundaryFaces
label nBoundaryFaces() const
Number of boundary faces (== nFaces - nInternalFaces)
Definition: primitiveMeshI.H:84
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:83
foamVtkSurfaceFieldWriter.H
Foam::vtk::fileTag::POINT_DATA
"PointData"
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
fvsPatchFields.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::vtk::fileTag::PIECE
"Piece"
Foam::vtk::dataArrayAttr::POINTS
"Points"
Foam::surfaceVectorField
GeometricField< vector, fvsPatchField, surfaceMesh > surfaceVectorField
Definition: surfaceFieldsFwd.H:59
Foam::GeometricField< vector, fvsPatchField, surfaceMesh >
boundary
faceListList boundary
Definition: createBlockMesh.H:4