foamVtkPatchWriterTemplates.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-2019 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 "foamVtkPatchWriter.H"
29 #include "foamVtkOutput.H"
30 
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 
33 template<class Type>
35 (
36  const word& fieldName,
37  const Type& val
38 )
39 {
40  if (isState(outputState::CELL_DATA))
41  {
42  ++nCellData_;
43  vtk::fileWriter::writeUniform<Type>(fieldName, val, numberOfCells_);
44  }
45  else if (isState(outputState::POINT_DATA))
46  {
47  ++nPointData_;
48  vtk::fileWriter::writeUniform<Type>(fieldName, val, numberOfPoints_);
49  }
50  else
51  {
53  << "Ignore bad writer state (" << stateNames[state_]
54  << ") for field " << fieldName << nl << endl
55  << exit(FatalError);
56  }
57 }
58 
59 
60 template<class Type, template<class> class PatchField>
62 (
64 )
65 {
66  if (isState(outputState::POINT_DATA))
67  {
68  ++nPointData_;
69  }
70  else
71  {
73  << "Bad writer state (" << stateNames[state_]
74  << ") - should be (" << stateNames[outputState::POINT_DATA]
75  << ") for field " << field.name() << nl << endl
76  << exit(FatalError);
77  }
78 
80 
81  label nPoints = nLocalPoints_;
82 
83  if (parallel_)
84  {
86  }
87 
88 
89  if (format_)
90  {
91  if (legacy())
92  {
93  legacy::floatField<nCmpt>(format(), field.name(), nPoints);
94  }
95  else
96  {
97  const uint64_t payLoad =
98  vtk::sizeofData<float, nCmpt>(nPoints);
99 
100  format().beginDataArray<float, nCmpt>(field.name());
101  format().writeSize(payLoad);
102  }
103  }
104 
105 
106  if (parallel_ ? Pstream::master() : true)
107  {
108  for (const label patchId : patchIDs_)
109  {
110  const auto& pfld = field.boundaryField()[patchId];
111 
112  vtk::writeList(format(), pfld.patchInternalField()());
113  }
114  }
115 
116 
117  if (parallel_)
118  {
119  // Patch Ids are identical across all processes
120  const label nPatches = patchIDs_.size();
121 
122  if (Pstream::master())
123  {
124  Field<Type> recv;
125 
126  // Receive each patch field and write
127  for
128  (
129  int slave=Pstream::firstSlave();
130  slave<=Pstream::lastSlave();
131  ++slave
132  )
133  {
134  IPstream fromSlave(Pstream::commsTypes::blocking, slave);
135 
136  for (label i=0; i < nPatches; ++i)
137  {
138  fromSlave >> recv;
139 
140  vtk::writeList(format(), recv);
141  }
142  }
143  }
144  else
145  {
146  // Send each patch field to master
147  OPstream toMaster
148  (
149  Pstream::commsTypes::blocking,
150  Pstream::masterNo()
151  );
152 
153  for (const label patchId : patchIDs_)
154  {
155  const auto& pfld = field.boundaryField()[patchId];
156 
157  toMaster << pfld.patchInternalField()();
158  }
159  }
160  }
161 
162 
163  if (format_)
164  {
165  format().flush();
166  format().endDataArray();
167  }
168 }
169 
170 
171 template<class Type, template<class> class PatchField>
173 (
175 )
176 {
177  if (isState(outputState::CELL_DATA))
178  {
179  ++nCellData_;
180  }
181  else
182  {
184  << "Bad writer state (" << stateNames[state_]
185  << ") - should be (" << stateNames[outputState::CELL_DATA]
186  << ") for field " << field.name() << nl << endl
187  << exit(FatalError);
188  }
189 
191 
192  label nFaces = nLocalFaces_;
193 
194  if (parallel_)
195  {
196  reduce(nFaces, sumOp<label>());
197  }
198 
199 
200  if (format_)
201  {
202  if (legacy())
203  {
204  legacy::floatField<nCmpt>(format(), field.name(), nFaces);
205  }
206  else
207  {
208  const uint64_t payLoad =
209  vtk::sizeofData<float, nCmpt>(nFaces);
210 
211  format().beginDataArray<float, nCmpt>(field.name());
212  format().writeSize(payLoad);
213  }
214  }
215 
216 
217  if (parallel_ ? Pstream::master() : true)
218  {
219  for (const label patchId : patchIDs_)
220  {
221  const auto& pfld = field.boundaryField()[patchId];
222 
223  if (useNearCellValue_)
224  {
225  vtk::writeList(format(), pfld.patchInternalField()());
226  }
227  else
228  {
229  vtk::writeList(format(), pfld);
230  }
231  }
232  }
233 
234  if (parallel_)
235  {
236  // Patch Ids are identical across all processes
237  const label nPatches = patchIDs_.size();
238 
239  if (Pstream::master())
240  {
241  Field<Type> recv;
242 
243  // Receive each patch field and write
244  for
245  (
246  int slave=Pstream::firstSlave();
247  slave<=Pstream::lastSlave();
248  ++slave
249  )
250  {
251  IPstream fromSlave(Pstream::commsTypes::blocking, slave);
252 
253  for (label i=0; i < nPatches; ++i)
254  {
255  fromSlave >> recv;
256 
257  vtk::writeList(format(), recv);
258  }
259  }
260  }
261  else
262  {
263  // Send each patch field to master
264  OPstream toMaster
265  (
266  Pstream::commsTypes::blocking,
267  Pstream::masterNo()
268  );
269 
270  for (const label patchId : patchIDs_)
271  {
272  const auto& pfld = field.boundaryField()[patchId];
273 
274  if (useNearCellValue_)
275  {
276  toMaster << pfld.patchInternalField()();
277  }
278  else
279  {
280  toMaster << static_cast<const Field<Type>&>(pfld);
281  }
282  }
283  }
284  }
285 
286 
287  if (format_)
288  {
289  format().flush();
290  format().endDataArray();
291  }
292 }
293 
294 
295 template<class Type>
297 (
300 )
301 {
302  if (isState(outputState::POINT_DATA))
303  {
304  ++nPointData_;
305  }
306  else
307  {
309  << "Bad writer state (" << stateNames[state_]
310  << ") - should be (" << stateNames[outputState::POINT_DATA]
311  << ") for field " << field.name() << nl << endl
312  << exit(FatalError);
313  }
314 
316 
317  label nPoints = nLocalPoints_;
318 
319  if (parallel_)
320  {
322  }
323 
324 
325  if (format_)
326  {
327  if (legacy())
328  {
329  legacy::floatField<nCmpt>(format(), field.name(), nPoints);
330  }
331  else
332  {
333  const uint64_t payLoad =
334  vtk::sizeofData<float, nCmpt>(nPoints);
335 
336  format().beginDataArray<float, nCmpt>(field.name());
337  format().writeSize(payLoad);
338  }
339  }
340 
341 
342  if (parallel_ ? Pstream::master() : true)
343  {
344  for (const label patchId : patchIDs_)
345  {
346  const auto& pfld = field.boundaryField()[patchId];
347 
348  if (useNearCellValue_)
349  {
350  auto tfield =
352  (
353  pfld.patchInternalField()()
354  );
355 
356  vtk::writeList(format(), tfield());
357  }
358  else
359  {
360  auto tfield = pInter.faceToPointInterpolate(pfld);
361 
362  vtk::writeList(format(), tfield());
363  }
364  }
365  }
366 
367 
368  if (parallel_)
369  {
370  // Patch Ids are identical across all processes
371  const label nPatches = patchIDs_.size();
372 
373  if (Pstream::master())
374  {
375  Field<Type> recv;
376 
377  // Receive each patch field and write
378  for
379  (
380  int slave=Pstream::firstSlave();
381  slave<=Pstream::lastSlave();
382  ++slave
383  )
384  {
385  IPstream fromSlave(Pstream::commsTypes::blocking, slave);
386 
387  for (label i=0; i < nPatches; ++i)
388  {
389  fromSlave >> recv;
390 
391  vtk::writeList(format(), recv);
392  }
393  }
394  }
395  else
396  {
397  // Send each patch field to master
398  OPstream toMaster
399  (
400  Pstream::commsTypes::blocking,
401  Pstream::masterNo()
402  );
403 
404  for (const label patchId : patchIDs_)
405  {
406  const auto& pfld = field.boundaryField()[patchId];
407 
408  if (useNearCellValue_)
409  {
410  auto tfield =
412  (
413  pfld.patchInternalField()()
414  );
415 
416  toMaster << tfield();
417  }
418  else
419  {
420  auto tfield = pInter.faceToPointInterpolate(pfld);
421 
422  toMaster << tfield();
423  }
424  }
425  }
426  }
427 
428 
429  if (format_)
430  {
431  format().flush();
432  format().endDataArray();
433  }
434 }
435 
436 
437 // ************************************************************************* //
foamVtkOutput.H
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::vtk::patchWriter::writeUniform
void writeUniform(const word &fieldName, const Type &val)
Write a uniform field of Cell (Face) or Point values.
Definition: foamVtkPatchWriterTemplates.C:35
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
nPatches
label nPatches
Definition: readKivaGrid.H:396
Foam::OPstream
Output inter-processor communications stream.
Definition: OPstream.H:52
Foam::expressions::patchExpr::POINT_DATA
Point data.
Definition: patchExprFwd.H:60
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::sumOp
Definition: ops.H:213
nPoints
label nPoints
Definition: gmvOutputHeader.H:2
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::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::Field
Generic templated field type.
Definition: Field.H:63
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::FatalError
error FatalError
Foam::PrimitivePatchInterpolation::faceToPointInterpolate
tmp< Field< Type > > faceToPointInterpolate(const Field< Type > &ff) const
Interpolate from faces to points.
Definition: PrimitivePatchInterpolation.C:176
foamVtkPatchWriter.H
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::PrimitivePatchInterpolation
Interpolation class within a primitive patch. Allows interpolation from points to faces and vice vers...
Definition: PrimitivePatchInterpolation.H:53
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:52
Foam::vtk::patchWriter::write
void write(const GeometricField< Type, PatchField, pointMesh > &field)
Write point field.
Definition: foamVtkPatchWriterTemplates.C:62
Foam::direction
uint8_t direction
Definition: direction.H:47
Foam::IPstream
Input inter-processor communications stream.
Definition: IPstream.H:52
patchId
label patchId(-1)
Foam::GeometricField
Generic GeometricField class.
Definition: areaFieldsFwd.H:53
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:294