foamVtkOutputTemplates.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-2022 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
28#include "globalIndex.H"
29#include "Pstream.H"
30#include "ListOps.H"
31
32// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33
34template<class Type>
36(
37 vtk::formatter& fmt,
38 const Type& val,
39 const label n
40)
41{
43
44 for (label i=0; i < n; ++i)
45 {
46 for (direction cmpt=0; cmpt < nCmpt; ++cmpt)
47 {
48 fmt.write(component(val, cmpt));
49 }
50 }
51}
52
53
54template<class Type>
56(
57 vtk::formatter& fmt,
58 const UList<Type>& values
59)
60{
61 for (const Type& val : values)
62 {
63 vtk::write(fmt, val);
64 }
65}
66
67
68template<class Type, unsigned N>
70(
71 vtk::formatter& fmt,
72 const FixedList<Type, N>& values
73)
74{
75 for (const Type& val : values)
76 {
77 vtk::write(fmt, val);
78 }
79}
80
81
82template<class Type>
84(
85 vtk::formatter& fmt,
86 const UList<Type>& values,
87 const labelUList& addressing
88)
89{
90 for (const label idx : addressing)
91 {
92 vtk::write(fmt, values[idx]);
93 }
94}
95
96
97template<class Type>
99(
100 vtk::formatter& fmt,
101 const UList<Type>& values,
102 const bitSet& selected
103)
104{
105 for (const label idx : selected)
106 {
107 vtk::write(fmt, values[idx]);
108 }
109}
110
111
112template<class Type>
114(
115 vtk::formatter& fmt,
116 const UList<Type>& values,
117 const UList<Type>& indirect,
118 const labelUList& addressing
119)
120{
121 vtk::writeList(fmt, values);
122 vtk::writeList(fmt, indirect, addressing);
123}
124
125
126template<class Type>
128(
129 vtk::formatter& fmt,
130 const Type& val,
131 const label count
132)
133{
135 {
136 // Non-contiguous data does not make sense
138 << "Contiguous data only" << endl
140 }
141
142 // Gather [count, value] tuples, including from master
143 const List<label> counts(UPstream::listGatherValues(count));
144 const List<Type> values(UPstream::listGatherValues(val));
145
146 if (Pstream::master())
147 {
148 forAll(counts, i)
149 {
150 // Write [count, value] tuple
151 vtk::write(fmt, counts[i], values[i]);
152 }
153 }
154}
155
156
157template<class Type>
159(
160 vtk::formatter& fmt,
161 const UList<Type>& values
162)
163{
165 {
166 // Non-contiguous data does not make sense
168 << "Contiguous data only" << endl
170 }
171
172
173 // Gather sizes (offsets irrelevant)
174 const globalIndex procAddr(values.size(), globalIndex::gatherOnly{});
175
176
177 if (Pstream::master())
178 {
179 // Write master data
180 vtk::writeList(fmt, values);
181
182 // Receive and write
183 DynamicList<Type> recvData(procAddr.maxNonLocalSize());
184
185 for (const label proci : procAddr.subProcs())
186 {
187 recvData.resize_nocopy(procAddr.localSize(proci));
188 UIPstream::read
189 (
190 UPstream::commsTypes::scheduled,
191 proci,
192 recvData.data_bytes(),
193 recvData.size_bytes()
194 );
195 vtk::writeList(fmt, recvData);
196 }
197 }
198 else
199 {
200 // Send
201 UOPstream::write
202 (
203 UPstream::commsTypes::scheduled,
204 UPstream::masterNo(),
205 values.cdata_bytes(),
206 values.size_bytes()
207 );
208 }
209}
210
211
212template<class Type>
214(
215 vtk::formatter& fmt,
216 const UList<Type>& values,
217 const labelUList& addressing
218)
219{
221 {
222 // Non-contiguous data does not make sense
224 << "Contiguous data only" << endl
226 }
227
228
229 List<Type> sendData;
230 if (!Pstream::master())
231 {
232 sendData = UIndirectList<Type>(values, addressing);
233 }
234
235 // Gather sizes (offsets irrelevant)
237
238
239 if (Pstream::master())
240 {
241 // Write master data
242 vtk::writeList(fmt, values, addressing);
243
244 // Receive and write
245 DynamicList<Type> recvData(procAddr.maxNonLocalSize());
246
247 for (const label proci : procAddr.subProcs())
248 {
249 recvData.resize_nocopy(procAddr.localSize(proci));
250 UIPstream::read
251 (
252 UPstream::commsTypes::scheduled,
253 proci,
254 recvData.data_bytes(),
255 recvData.size_bytes()
256 );
257 vtk::writeList(fmt, recvData);
258 }
259 }
260 else
261 {
262 UOPstream::write
263 (
264 UPstream::commsTypes::scheduled,
265 UPstream::masterNo(),
266 sendData.cdata_bytes(),
267 sendData.size_bytes()
268 );
269 }
270}
271
272
273template<class Type>
275(
276 vtk::formatter& fmt,
277 const UList<Type>& values,
278 const bitSet& selected
279)
280{
282 {
283 // Non-contiguous data does not make sense
285 << "Contiguous data only" << endl
287 }
288
289
290 List<Type> sendData;
291 if (!Pstream::master())
292 {
293 sendData = subset(selected, values);
294 }
295
296 // Gather sizes (offsets irrelevant)
298
299
300 if (Pstream::master())
301 {
302 // Write master data
303 vtk::writeList(fmt, values, selected);
304
305 // Receive and write
306 DynamicList<Type> recvData(procAddr.maxNonLocalSize());
307
308 for (const label proci : procAddr.subProcs())
309 {
310 recvData.resize_nocopy(procAddr.localSize(proci));
311 UIPstream::read
312 (
313 UPstream::commsTypes::scheduled,
314 proci,
315 recvData.data_bytes(),
316 recvData.size_bytes()
317 );
318 vtk::writeList(fmt, recvData);
319 }
320 }
321 else
322 {
323 UOPstream::write
324 (
325 UPstream::commsTypes::scheduled,
326 UPstream::masterNo(),
327 sendData.cdata_bytes(),
328 sendData.size_bytes()
329 );
330 }
331}
332
333
334template<class Type>
336(
337 vtk::formatter& fmt,
338 const UList<Type>& values1,
339 const UList<Type>& values2
340)
341{
343 {
344 // Non-contiguous data does not make sense
346 << "Contiguous data only" << endl
348 }
349
350
351 // Gather sizes (offsets irrelevant)
352 const globalIndex procAddr1(values1.size(), globalIndex::gatherOnly{});
353 const globalIndex procAddr2(values2.size(), globalIndex::gatherOnly{});
354
355
356 if (Pstream::master())
357 {
358 // Write master data
359 vtk::writeList(fmt, values1);
360 vtk::writeList(fmt, values2);
361
362 // Receive and write
363 DynamicList<Type> recvData
364 (
365 max(procAddr1.maxNonLocalSize(), procAddr2.maxNonLocalSize())
366 );
367
368 for (const label proci : procAddr1.subProcs())
369 {
370 // values1
371 recvData.resize_nocopy(procAddr1.localSize(proci));
372 UIPstream::read
373 (
374 UPstream::commsTypes::scheduled,
375 proci,
376 recvData.data_bytes(),
377 recvData.size_bytes()
378 );
379 vtk::writeList(fmt, recvData);
380
381 // values2
382 recvData.resize_nocopy(procAddr2.localSize(proci));
383 UIPstream::read
384 (
385 UPstream::commsTypes::scheduled,
386 proci,
387 recvData.data_bytes(),
388 recvData.size_bytes()
389 );
390 vtk::writeList(fmt, recvData);
391 }
392 }
393 else
394 {
395 UOPstream::write
396 (
397 UPstream::commsTypes::scheduled,
398 UPstream::masterNo(),
399 values1.cdata_bytes(),
400 values1.size_bytes()
401 );
402
403 UOPstream::write
404 (
405 UPstream::commsTypes::scheduled,
406 UPstream::masterNo(),
407 values2.cdata_bytes(),
408 values2.size_bytes()
409 );
410 }
411}
412
413
414template<class Type>
416(
417 vtk::formatter& fmt,
418 const UList<Type>& values1,
419 const UList<Type>& values2,
420 const labelUList& addressing
421)
422{
424 {
425 // Non-contiguous data does not make sense
427 << "Contiguous data only" << endl
429 }
430
431
432 List<Type> sendData2;
433 if (!Pstream::master())
434 {
435 sendData2 = UIndirectList<Type>(values2, addressing);
436 }
437
438
439 // Gather sizes (offsets irrelevant)
440 const globalIndex procAddr1(values1.size(), globalIndex::gatherOnly{});
441 const globalIndex procAddr2(sendData2.size(), globalIndex::gatherOnly{});
442
443
444 if (Pstream::master())
445 {
446 // Write master data
447
448 vtk::writeList(fmt, values1);
449 vtk::writeList(fmt, values2, addressing);
450
451 // Receive and write
452 DynamicList<Type> recvData
453 (
454 max(procAddr1.maxNonLocalSize(), procAddr2.maxNonLocalSize())
455 );
456
457 for (const label proci : procAddr1.subProcs())
458 {
459 // values1
460 recvData.resize_nocopy(procAddr1.localSize(proci));
461 UIPstream::read
462 (
463 UPstream::commsTypes::scheduled,
464 proci,
465 recvData.data_bytes(),
466 recvData.size_bytes()
467 );
468 vtk::writeList(fmt, recvData);
469
470 // values2
471 recvData.resize_nocopy(procAddr2.localSize(proci));
472 UIPstream::read
473 (
474 UPstream::commsTypes::scheduled,
475 proci,
476 recvData.data_bytes(),
477 recvData.size_bytes()
478 );
479 vtk::writeList(fmt, recvData);
480 }
481 }
482 else
483 {
484 UOPstream::write
485 (
486 UPstream::commsTypes::scheduled,
487 UPstream::masterNo(),
488 values1.cdata_bytes(),
489 values1.size_bytes()
490 );
491
492 UOPstream::write
493 (
494 UPstream::commsTypes::scheduled,
495 UPstream::masterNo(),
496 sendData2.cdata_bytes(),
497 sendData2.size_bytes()
498 );
499 }
500}
501
502
503// ************************************************************************* //
Various functions to operate on Lists.
label n
Y[inertIndex] max(0.0)
globalIndex procAddr(aMesh.nFaces())
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:72
void resize_nocopy(const label len)
Definition: DynamicListI.H:363
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: FixedList.H:81
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 with indirect addressing. Like IndirectList but does not store addressing.
Definition: IndirectList.H:79
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
char * data_bytes() noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:251
const char * cdata_bytes() const noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:244
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
std::streamsize size_bytes() const noexcept
Number of contiguous bytes for the List data.
Definition: UListI.H:258
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:66
Calculates a unique integer (label so might not have enough room - 2G max) for processor + local inde...
Definition: globalIndex.H:68
label localSize() const
My local size.
Definition: globalIndexI.H:207
labelRange subProcs() const noexcept
Range of process indices for addressed sub-offsets (processes)
Definition: globalIndexI.H:159
label maxNonLocalSize() const
The max of localSizes, excluding current processor.
Definition: globalIndexI.H:220
A traits class, which is primarily used for primitives.
Definition: pTraits.H:59
Abstract class for a VTK output stream formatter.
virtual void write(const uint8_t val)=0
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
void writeListsParallel(vtk::formatter &fmt, const UList< Type > &values1, const UList< Type > &values2)
Write a list of values and another list of values.
void writeLists(vtk::formatter &fmt, const UList< Type > &values1, const UList< Type > &values2, const labelUList &addressing)
Write a list of values and a list of values via indirect addressing.
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.
void writeValueParallel(vtk::formatter &fmt, const Type &val, const label count=1)
Component-wise write of a value (N times) in parallel.
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
List< T > subset(const BoolListType &select, const UList< T > &input, const bool invert=false)
Extract elements of the input list when select is true.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
uint8_t direction
Definition: direction.H:56
error FatalError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333
A template class to specify that a data type can be considered as being contiguous in memory.
Definition: contiguous.H:78