foamVtkOutput.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 
28 #include "foamVtkOutput.H"
29 
30 #include "foamVtkFormatter.H"
31 #include "foamVtkAsciiFormatter.H"
32 #include "foamVtkBase64Formatter.H"
37 #include "foamVersion.H"
38 #include "typeInfo.H"
39 #include "globalIndex.H"
40 #include "instant.H"
41 #include "Fstream.H"
42 #include "Pstream.H"
43 #include "OSspecific.H"
44 
45 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
46 
48 Foam::vtk::newFormatter(std::ostream& os, unsigned prec)
49 {
51 }
52 
53 
56 (
57  std::ostream& os,
58  const enum formatType fmtType,
59  unsigned prec
60 )
61 {
63 
64  switch (fmtType)
65  {
66  case formatType::INLINE_ASCII:
67  fmt.reset(new vtk::asciiFormatter(os, prec));
68  break;
69 
70  case formatType::INLINE_BASE64:
71  fmt.reset(new vtk::base64Formatter(os));
72  break;
73 
74  case formatType::APPEND_BASE64:
76  break;
77 
78  case formatType::APPEND_BINARY:
80  break;
81 
82  case formatType::LEGACY_ASCII:
83  fmt.reset(new vtk::legacyAsciiFormatter(os, prec));
84  break;
85 
86  case formatType::LEGACY_BINARY:
88  break;
89  }
90 
91  return fmt;
92 }
93 
94 
96 (
97  vtk::formatter& fmt,
98  const label len,
99  label start
100 )
101 {
102  // No nComponents for label, can use fmt.write() directly
103  for (label i=0; i < len; ++i)
104  {
105  fmt.write(start);
106  ++start;
107  }
108 }
109 
110 
112 (
113  vtk::formatter& fmt,
114  const UList<uint8_t>& values
115 )
116 {
117  // No nComponents for char, can use fmt.write() directly
118  for (const uint8_t val : values)
119  {
120  fmt.write(val);
121  }
122 }
123 
124 
126 (
127  vtk::formatter& fmt,
128  const labelUList& values,
129  const globalIndex& procOffset
130 )
131 {
132  // Gather sizes - master information, offsets are irrelevant
133  const globalIndex procAddr
134  (
135  UPstream::listGatherValues<label>(values.size()),
136  globalIndex::SIZES
137  );
138 
139 
140  if (Pstream::master())
141  {
142  // Write master data - with value offset
143  const label offsetId = procOffset.offset(0);
144  for (const label val : values)
145  {
146  vtk::write(fmt, val + offsetId);
147  }
148 
149  // Receive and write
150  DynamicList<label> recvData(procAddr.maxNonLocalSize());
151 
152  for (const label proci : procAddr.subProcs())
153  {
154  recvData.resize_nocopy(procAddr.localSize(proci));
156  (
157  UPstream::commsTypes::scheduled,
158  proci,
159  recvData.data_bytes(),
160  recvData.size_bytes()
161  );
162 
163  // With value offset
164  const label offsetId = procOffset.offset(proci);
165  for (const label val : recvData)
166  {
167  vtk::write(fmt, val + offsetId);
168  }
169  }
170  }
171  else
172  {
173  // Send
175  (
176  UPstream::commsTypes::scheduled,
177  Pstream::masterNo(),
178  values.cdata_bytes(),
179  values.size_bytes()
180  );
181  }
182 }
183 
184 
185 // * * * * * * * * * * * * * * Legacy Functions * * * * * * * * * * * * * * //
186 
188 (
189  std::ostream& os,
190  const std::string& title,
191  bool binary
192 )
193 {
194  // Line 1:
195  os << "# vtk DataFile Version 2.0" << nl;
196 
197  // OR
198  // os << "# vtk DataFile Version 5.1" << nl;
199 
200  // Line 2: title
201 
202  const auto truncate = title.find('\n');
203 
204  if (title.empty() || 0 == truncate)
205  {
206  // Avoid an empty title
207  os << "File generated by OpenFOAM " << foamVersion::api << nl;
208  }
209  else if (std::string::npos == truncate)
210  {
211  os << title << nl;
212  }
213  else
214  {
215  os << title.substr(0, truncate) << nl;
216  }
217 
218  // Line 3: format
219  os << (binary ? "BINARY" : "ASCII") << nl;
220 }
221 
222 
224 (
225  vtk::formatter& fmt,
226  const std::string& title,
227  const std::string& contentType
228 )
229 {
230  std::ostream& os = fmt.os();
231 
232  legacy::fileHeader(os, title, isType<legacyRawFormatter>(fmt));
233  if (contentType.size())
234  {
235  os << "DATASET " << contentType.c_str() << nl;
236  }
237 }
238 
239 
240 // ************************************************************************* //
foamVtkOutput.H
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::vtk::appendBase64Formatter
Appended base-64 encoded binary output. Uses an output filter layer to write base-64 encoded content.
Definition: foamVtkAppendBase64Formatter.H:53
typeInfo.H
foamVtkLegacyAsciiFormatter.H
Foam::DynamicList< label >
foamVtkFormatter.H
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
globalIndex.H
Foam::vtk::asciiFormatter
Inline ASCII output. Adds spaces between entries and a newline every 9 items (for consistency with wh...
Definition: foamVtkAsciiFormatter.H:54
Foam::vtk::legacyAsciiFormatter
Formatting as per Foam::vtk::asciiFormatter, but with naming for legacy output.
Definition: foamVtkLegacyAsciiFormatter.H:53
Foam::vtk::writeIdentity
void writeIdentity(vtk::formatter &fmt, const label len, label start=0)
Write an identity list of labels.
Definition: foamVtkOutput.C:96
Foam::globalIndex::localSize
label localSize() const
My local size.
Definition: globalIndexI.H:187
instant.H
foamVtkLegacyRawFormatter.H
foamVtkAppendRawFormatter.H
Foam::blockMeshTools::read
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:57
Foam::vtk::formatter::os
std::ostream & os()
Access to the underlying output stream.
Definition: foamVtkFormatterI.H:70
Foam::vtk::formatType
formatType
The output format type for file contents.
Definition: foamVtkCore.H:65
Foam::foamVersion::api
const int api
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::legacyRawFormatter
Binary output for the VTK legacy format, always written as big-endian and with 32-bit integers.
Definition: foamVtkLegacyRawFormatter.H:55
Foam::vtk::legacy::fileHeader
void fileHeader(std::ostream &os, const std::string &title, bool binary)
Emit header for legacy file (vtk DataFile Version 2.0)
Definition: foamVtkOutput.C:188
os
OBJstream os(runTime.globalPath()/outputName)
Pstream.H
Foam::globalIndex
Calculates a unique integer (label so might not have enough room - 2G max) for processor + local inde...
Definition: globalIndex.H:68
Foam::globalIndex::subProcs
labelRange subProcs() const noexcept
Range of process indices for addressed sub-offsets (processes)
Definition: globalIndexI.H:121
Foam::autoPtr::reset
void reset(autoPtr< T > &&other) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:117
Foam::autoPtr< Foam::vtk::formatter >
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Fstream.H
Foam::vtk::appendRawFormatter
Appended raw binary output.
Definition: foamVtkAppendRawFormatter.H:52
foamVtkBase64Formatter.H
Foam::globalIndex::maxNonLocalSize
label maxNonLocalSize() const
The max of localSizes, excluding current processor.
Definition: globalIndexI.H:200
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Definition: foamVtkOutputTemplates.C:36
Foam::DynamicList::resize_nocopy
void resize_nocopy(const label len)
Definition: DynamicListI.H:363
Foam::globalIndex::offset
label offset(const label proci) const
Start of proci data.
Definition: globalIndexI.H:163
Foam::vtk::newFormatter
autoPtr< vtk::formatter > newFormatter(std::ostream &os, unsigned prec=IOstream::defaultPrecision())
Return a default asciiFormatter.
Definition: foamVtkOutput.C:48
Foam::vtk::writeListParallel
void writeListParallel(vtk::formatter &fmt, const UList< Type > &values)
Write a list of values.
Definition: foamVtkOutputTemplates.C:164
Foam::vtk::formatter::write
virtual void write(const uint8_t val)=0
foamVtkAsciiFormatter.H
foamVtkAppendBase64Formatter.H
Foam::vtk::base64Formatter
Inline base-64 encoded binary output. Uses an output filter layer to write base-64 encoded content.
Definition: foamVtkBase64Formatter.H:51
Foam::vtk::formatter
Abstract class for a VTK output stream formatter.
Definition: foamVtkFormatter.H:68
foamVersion.H