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-2021 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  // <Point>
163  this->beginPoints(numberOfPoints_);
164 
165  if (parallel_)
166  {
167  // Centres for internal faces
169  (
170  format_.ref(),
171  SubList<point>(centres, mesh_.nInternalFaces())
172  );
173 
174  // Centres for boundary faces
176  (
177  format_.ref(),
178  SubList<point>(centres, mesh_.boundaryMesh().range())
179  );
180  }
181  else
182  {
183  // Non-parallel: use a normal write
184 
185  vtk::writeList(format(), centres);
186  }
187 
188  this->endPoints();
189 
190  return true;
191 }
192 
193 
195 {
196  // No legacy, no CellData
197  return enter_CellData(0, 0);
198 }
199 
200 
202 {
203  // No legacy
204  return enter_PointData(numberOfPoints_, 0);
205 }
206 
207 
209 {
210  if (isState(outputState::POINT_DATA))
211  {
212  ++nPointData_;
213  }
214  else
215  {
217  << " for field " << field.name() << nl << endl
218  << exit(FatalError);
219  }
220 
221  label nFaces = field.mesh().nFaces();
222 
223  if (parallel_)
224  {
225  reduce(nFaces, sumOp<label>());
226  }
227 
228  if (nFaces != numberOfPoints_)
229  {
231  << "Expecting " << numberOfPoints_
232  << " faces, but found " << nFaces
233  << exit(FatalError);
234  }
235 
236  this->beginDataArray<vector>(field.name(), nFaces);
237 
238 
239  // Internal field
240  const SubList<vector> internal(field, mesh_.nInternalFaces());
241 
242  // Boundary field (flattened)
243  auto boundary(flattenBoundary(field));
244 
245 
246  if (parallel_)
247  {
248  // Internal field
249  vtk::writeListParallel(format_.ref(), internal);
250 
251  // Boundary field
252  vtk::writeListParallel(format_.ref(), boundary);
253  }
254  else
255  {
256  // Non-parallel
257 
258  // Internal field
259  vtk::writeList(format_.ref(), internal);
260 
261  // Boundary field
262  vtk::writeList(format_.ref(), boundary);
263  }
264 
265 
266  this->endDataArray();
267 }
268 
269 
270 // ************************************************************************* //
Foam::vtk::outputOptions
Encapsulated combinations of output format options. This is primarily useful when defining the output...
Definition: foamVtkOutputOptions.H:59
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:73
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:54
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:194
Foam::vtk::fileWriter::beginFile
virtual bool beginFile(std::string title="")
Write file header (non-collective)
Definition: foamVtkFileWriter.C:393
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:369
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:208
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:201
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::vtk::fileAttr::NUMBER_OF_POINTS
"NumberOfPoints"
Foam::vtk::surfaceFieldWriter::writeGeometry
virtual bool writeGeometry()
Write cloud positions.
Definition: foamVtkSurfaceFieldWriter.C:135
Foam::primitiveMesh::nBoundaryFaces
label nBoundaryFaces() const noexcept
Number of boundary faces (== nFaces - nInternalFaces)
Definition: primitiveMeshI.H:84
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::vtk::legacy::beginPoints
void beginPoints(std::ostream &os, label nPoints)
Emit header for POINTS (with trailing newline).
Definition: foamVtkOutputI.H:113
Foam::FatalError
error FatalError
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:85
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:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
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::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::writeListParallel
void writeListParallel(vtk::formatter &fmt, const UList< Type > &values)
Write a list of values.
Definition: foamVtkOutputTemplates.C:164
Foam::surfaceVectorField
GeometricField< vector, fvsPatchField, surfaceMesh > surfaceVectorField
Definition: surfaceFieldsFwd.H:59
Foam::GeometricField< vector, fvsPatchField, surfaceMesh >
boundary
faceListList boundary
Definition: createBlockMesh.H:4