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-2022 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 (offsets irrelevant)
133  const globalIndex procAddr(globalIndex::gatherOnly{}, values.size());
134 
135  if (Pstream::master())
136  {
137  // Write master data - with value offset
138  const label offsetId = procOffset.localStart(0);
139  for (const label val : values)
140  {
141  vtk::write(fmt, val + offsetId);
142  }
143 
144  // Receive and write
145  DynamicList<label> recvData(procAddr.maxNonLocalSize());
146 
147  for (const label proci : procAddr.subProcs())
148  {
149  const label procSize = procAddr.localSize(proci);
150 
151  if (procSize)
152  {
153  recvData.resize_nocopy(procSize);
155  (
156  UPstream::commsTypes::scheduled,
157  proci,
158  recvData.data_bytes(),
159  recvData.size_bytes()
160  );
161 
162  // With value offset
163  const label offsetId = procOffset.localStart(proci);
164  for (const label val : recvData)
165  {
166  vtk::write(fmt, val + offsetId);
167  }
168  }
169  }
170  }
171  else
172  {
173  if (values.size())
174  {
176  (
177  UPstream::commsTypes::scheduled,
178  UPstream::masterNo(),
179  values.cdata_bytes(),
180  values.size_bytes()
181  );
182  }
183  }
184 }
185 
186 
187 // * * * * * * * * * * * * * * Legacy Functions * * * * * * * * * * * * * * //
188 
190 (
191  std::ostream& os,
192  const std::string& title,
193  bool binary
194 )
195 {
196  // Line 1:
197  os << "# vtk DataFile Version 2.0" << nl;
198 
199  // OR
200  // os << "# vtk DataFile Version 5.1" << nl;
201 
202  // Line 2: title
203 
204  const auto truncate = title.find('\n');
205 
206  if (title.empty() || 0 == truncate)
207  {
208  // Avoid an empty title
209  os << "File generated by OpenFOAM " << foamVersion::api << nl;
210  }
211  else if (std::string::npos == truncate)
212  {
213  os << title << nl;
214  }
215  else
216  {
217  os << title.substr(0, truncate) << nl;
218  }
220  // Line 3: format
221  os << (binary ? "BINARY" : "ASCII") << nl;
222 }
223 
224 
226 (
227  vtk::formatter& fmt,
228  const std::string& title,
229  const std::string& contentType
230 )
231 {
232  std::ostream& os = fmt.os();
233 
234  legacy::fileHeader(os, title, isType<legacyRawFormatter>(fmt));
235  if (contentType.size())
236  {
237  os << "DATASET " << contentType.c_str() << nl;
238  }
239 }
240 
241 
242 // ************************************************************************* //
Abstract class for a VTK output stream formatter.
Formatting as per Foam::vtk::asciiFormatter, but with naming for legacy output.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
autoPtr< vtk::formatter > newFormatter(std::ostream &os, unsigned prec=IOstream::defaultPrecision())
Return a default asciiFormatter.
Definition: foamVtkOutput.C:41
UList< label > labelUList
A UList of labels.
Definition: UList.H:78
Binary output for the VTK legacy format, always written as big-endian and with 32-bit integers...
void reset(T *p=nullptr) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:37
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
std::ostream & os() noexcept
Access to the underlying output stream.
void fileHeader(std::ostream &os, const std::string &title, bool binary)
Emit header for legacy file (vtk DataFile Version 2.0)
void writeIdentity(vtk::formatter &fmt, const label len, label start=0)
Write an identity list of labels.
Definition: foamVtkOutput.C:89
formatType
The output format type for file contents.
Definition: foamVtkCore.H:66
const int api
OpenFOAM api number (integer) corresponding to the value of OPENFOAM at the time of compilation...
void writeList(vtk::formatter &fmt, const UList< uint8_t > &values)
Write a list of uint8_t values.
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
OBJstream os(runTime.globalPath()/outputName)
globalIndex procAddr(aMesh.nFaces())
void writeListParallel(vtk::formatter &fmt, const UList< Type > &values)
Write a list of values.
Appended raw binary output.
Inline base-64 encoded binary output. Uses an output filter layer to write base-64 encoded content...
Appended base-64 encoded binary output. Uses an output filter layer to write base-64 encoded content...
Inline ASCII output. Adds spaces between entries and a newline every 9 items (for consistency with wh...