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-------------------------------------------------------------------------------
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 "emptyFvsPatchFields.H"
30#include "fvsPatchFields.H"
31#include "surfaceFields.H"
32
33// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
34
35Foam::List<Foam::vector> Foam::vtk::surfaceFieldWriter::flattenBoundary
36(
37 const surfaceVectorField& field
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
62(
63 const fvMesh& mesh,
64 const vtk::outputOptions opts
65)
66:
67 vtk::fileWriter(vtk::fileTag::POLY_DATA, opts),
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
77(
78 const fvMesh& mesh,
79 const fileName& file,
80 bool parallel
81)
82:
84{
85 open(file, parallel);
86}
87
88
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 {
216 reportBadState(FatalErrorInFunction, outputState::POINT_DATA)
217 << " for field " << field.name() << nl << endl
218 << exit(FatalError);
219 }
220
221 label nFaces = field.mesh().nFaces();
222
223 if (parallel_)
224 {
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// ************************************************************************* //
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
A List obtained as a section of another List.
Definition: SubList.H:70
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
A class for handling file names.
Definition: fileName.H:76
virtual bool write()
Write the output fields.
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:91
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:456
label nBoundaryFaces() const noexcept
Number of boundary faces (== nFaces - nInternalFaces)
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.
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.
Write surfaces fields (as PointData) in VTP format. Legacy VTK format is intentionally not supported.
virtual bool writeGeometry()
Write cloud positions.
virtual bool beginFile(std::string title="")
Write file header (non-collective)
rDeltaTY field()
faceListList boundary
dynamicFvMesh & mesh
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
const labelList nFaces(UPstream::listGatherValues< label >(aMesh.nFaces()))
@ NUMBER_OF_POINTS
"NumberOfPoints"
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.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
void reduce(const List< UPstream::commsStruct > &comms, T &value, const BinaryOp &bop, const int tag, const label comm)
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
word format(conversionProperties.get< word >("format"))
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333
Foam::surfaceFields.