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-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 "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:
75  fmt.reset(new vtk::appendBase64Formatter(os));
76  break;
77 
78  case formatType::APPEND_BINARY:
79  fmt.reset(new vtk::appendRawFormatter(os));
80  break;
81 
82  case formatType::LEGACY_ASCII:
83  fmt.reset(new vtk::legacyAsciiFormatter(os, prec));
84  break;
85 
86  case formatType::LEGACY_BINARY:
87  fmt.reset(new vtk::legacyRawFormatter(os));
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, so 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, so 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 UList<uint8_t>& values
129 )
130 {
131  if (Pstream::master())
132  {
133  vtk::writeList(fmt, values);
134 
135  List<uint8_t> recv;
136 
137  // Receive and write
138  for
139  (
140  int slave=Pstream::firstSlave();
141  slave<=Pstream::lastSlave();
142  ++slave
143  )
144  {
145  IPstream fromSlave(Pstream::commsTypes::blocking, slave);
146 
147  fromSlave >> recv;
148 
149  vtk::writeList(fmt, recv);
150  }
151  }
152  else
153  {
154  // Send to master
155  OPstream toMaster
156  (
157  Pstream::commsTypes::blocking,
158  Pstream::masterNo()
159  );
160 
161  toMaster << values;
162  }
163 }
164 
165 
167 (
168  vtk::formatter& fmt,
169  const labelUList& values,
170  const globalIndex& procOffset
171 )
172 {
173  if (Pstream::master())
174  {
175  // Write with offset
176  const label offsetId = procOffset.offset(0);
177 
178  for (const label val : values)
179  {
180  vtk::write(fmt, val + offsetId);
181  }
182 
183  labelList recv;
184 
185  // Receive and write
186  for
187  (
188  int slave=Pstream::firstSlave();
189  slave<=Pstream::lastSlave();
190  ++slave
191  )
192  {
193  IPstream fromSlave(Pstream::commsTypes::blocking, slave);
194 
195  fromSlave >> recv;
196 
197  const label offsetId = procOffset.offset(slave);
198 
199  // Write with offset
200  for (const label val : recv)
201  {
202  vtk::write(fmt, val + offsetId);
203  }
204  }
205  }
206  else
207  {
208  // Send to master
209  OPstream toMaster
210  (
211  Pstream::commsTypes::blocking,
212  Pstream::masterNo()
213  );
214 
215  toMaster << values;
216  }
217 }
218 
219 
220 // * * * * * * * * * * * * * * Legacy Functions * * * * * * * * * * * * * * //
221 
222 
224 (
225  std::ostream& os,
226  const std::string& title,
227  bool binary
228 )
229 {
230  // Line 1:
231  os << "# vtk DataFile Version 2.0" << nl;
232 
233  // Line 2: title
234 
235  const auto truncate = title.find('\n');
236 
237  if (title.empty() || 0 == truncate)
238  {
239  // Avoid an empty title
240  os << "File generated by OpenFOAM " << foamVersion::api << nl;
241  }
242  else if (std::string::npos == truncate)
243  {
244  os << title << nl;
245  }
246  else
247  {
248  os << title.substr(0, truncate) << nl;
249  }
250 
251  // Line 3: format
252  os << (binary ? "BINARY" : "ASCII") << nl;
253 }
254 
255 
257 (
258  vtk::formatter& fmt,
259  const std::string& title,
260  const std::string& contentType
261 )
262 {
263  std::ostream& os = fmt.os();
264 
265  legacy::fileHeader(os, title, isType<legacyRawFormatter>(fmt));
266  if (contentType.size())
267  {
268  os << "DATASET " << contentType.c_str() << nl;
269  }
270 }
271 
272 
273 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:74
Foam::vtk::writeListParallel
void writeListParallel(vtk::formatter &fmt, const UList< uint8_t > &values)
Write a list of uint8_t values.
Definition: foamVtkOutput.C:126
Foam::autoPtr::reset
void reset(T *p=nullptr) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:158
foamVtkOutput.H
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
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
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
Foam::OPstream
Output inter-processor communications stream.
Definition: OPstream.H:52
globalIndex.H
Foam::vtk::asciiFormatter
Inline ASCII output. Adds spaces between entries and a newline every 6 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
instant.H
foamVtkLegacyRawFormatter.H
foamVtkAppendRawFormatter.H
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::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::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.
Definition: foamVtkOutput.C:224
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::autoPtr< Foam::vtk::formatter >
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Fstream.H
Input/output from file streams.
Foam::vtk::appendRawFormatter
Appended raw binary output.
Definition: foamVtkAppendRawFormatter.H:52
Foam::List< uint8_t >
foamVtkBase64Formatter.H
Foam::start
label ListType::const_reference const label start
Definition: ListOps.H:408
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:35
Foam::globalIndex::offset
label offset(const label proci) const
Start of proci data.
Definition: globalIndexI.H:100
Foam::vtk::newFormatter
autoPtr< vtk::formatter > newFormatter(std::ostream &os, unsigned prec=IOstream::defaultPrecision())
Return a default asciiFormatter.
Definition: foamVtkOutput.C:48
Foam::IPstream
Input inter-processor communications stream.
Definition: IPstream.H:52
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