vtkSurfaceWriter.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) 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 "vtkSurfaceWriter.H"
29 #include "foamVtkSurfaceWriter.H"
30 #include "surfaceWriterMethods.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 namespace surfaceWriters
38 {
39  defineTypeName(vtkWriter);
40  addToRunTimeSelectionTable(surfaceWriter, vtkWriter, word);
41  addToRunTimeSelectionTable(surfaceWriter, vtkWriter, wordDict);
42 
43  // Accept vtp ending as well
45  (
46  surfaceWriter,
47  vtkWriter,
48  word,
49  vtp
50  );
52  (
53  surfaceWriter,
54  vtkWriter,
55  wordDict,
56  vtp
57  );
58 }
59 }
60 
61 
62 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
63 
65 :
66  surfaceWriter(),
67  fmtType_(static_cast<unsigned>(vtk::formatType::INLINE_BASE64)),
68  precision_(IOstream::defaultPrecision()),
69  writer_(nullptr)
70 {}
71 
72 
74 (
75  const vtk::outputOptions& opts
76 )
77 :
78  surfaceWriter(),
79  fmtType_(static_cast<unsigned>(opts.fmt())),
80  precision_(opts.precision()),
81  writer_(nullptr)
82 {}
83 
84 
86 (
87  const dictionary& options
88 )
89 :
90  surfaceWriter(options),
91  fmtType_(static_cast<unsigned>(vtk::formatType::INLINE_BASE64)),
92  precision_
93  (
94  options.lookupOrDefaultCompat
95  (
96  "precision", {{"writePrecision", 1806}},
98  )
99  ),
100  writer_(nullptr)
101 {
102  // format: ascii | binary
103  // legacy: true | false
104 
106 
107  const word formatName = options.lookupOrDefault<word>("format", "");
108  if (formatName.size())
109  {
110  opts.ascii
111  (
113  );
114  }
115 
116  if (options.lookupOrDefault("legacy", false))
117  {
118  opts.legacy(true);
119  }
120 
121  // Convert back to raw data type
122  fmtType_ = static_cast<unsigned>(opts.fmt());
123 }
124 
125 
127 (
128  const meshedSurf& surf,
129  const fileName& outputPath,
130  bool parallel,
131  const dictionary& options
132 )
133 :
134  vtkWriter(options)
135 {
136  open(surf, outputPath, parallel);
137 }
138 
139 
141 (
142  const pointField& points,
143  const faceList& faces,
144  const fileName& outputPath,
145  bool parallel,
146  const dictionary& options
147 )
148 :
149  vtkWriter(options)
150 {
151  open(points, faces, outputPath, parallel);
152 }
153 
154 
155 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
156 
158 {
159  close();
160 }
161 
162 
163 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
164 
166 {
167  writer_.clear();
169 }
170 
171 
173 {
174  writer_.clear();
176 }
177 
178 
180 {
181  writer_.clear();
183 }
184 
185 
187 {
188  writer_.clear();
190 }
191 
192 
194 {
195  checkOpen();
196 
197  if (needsUpdate())
198  {
199  writer_.clear();
200  }
201  merge();
202 
203  // From raw unsigned values to vtk::outputOptions
204  vtk::outputOptions opts(static_cast<vtk::formatType>(fmtType_), precision_);
205 
206 
207  // Geometry: rootdir/<TIME>/surfaceName.{vtk|vtp}
208 
209  fileName outputFile = outputPath_;
210  if (useTimeDir() && !timeName().empty())
211  {
212  // Splice in time-directory
213  outputFile = outputPath_.path() / timeName() / outputPath_.name();
214  }
215  outputFile.ext(vtk::surfaceWriter::ext(opts));
216 
217  if (verbose_)
218  {
219  Info<< "Writing geometry to " << outputFile << endl;
220  }
221 
222  const meshedSurf& surf = surface();
223 
224  if (writer_.empty() && (Pstream::master() || !parallel_))
225  {
226  writer_.reset
227  (
229  (
230  surf.points(),
231  surf.faces(),
232  opts,
233  outputFile,
234  false // serial!
235  )
236  );
237 
238  if (this->hasTime())
239  {
240  // Time name in title
241  writer_->setTime(currTime_);
242  writer_->writeTimeValue();
243  }
244  else
245  {
246  // Surface name in title
247  writer_->beginFile(outputPath_.nameLessExt());
248  }
249 
250  writer_->writeGeometry();
251  }
252 
253  wroteGeom_ = true;
254  return outputFile;
255 }
256 
257 
258 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
259 
260 template<class Type>
261 Foam::fileName Foam::surfaceWriters::vtkWriter::writeTemplate
262 (
263  const word& fieldName,
264  const Field<Type>& localValues
265 )
266 {
267  // Field: rootdir/<TIME>/surfaceName.{vtk|vtp}
268 
269  // Open file, writing geometry (if required)
270  fileName outputFile = this->write();
271 
272  if (verbose_)
273  {
274  Info<< "Writing field " << fieldName << " to " << outputFile << endl;
275  }
276 
277  // geometry merge() implicit
278  tmp<Field<Type>> tfield = mergeField(localValues);
279 
280  if (Pstream::master() || !parallel_)
281  {
282  if (this->isPointData())
283  {
284  writer_->beginPointData(nFields_);
285  }
286  else
287  {
288  writer_->beginCellData(nFields_);
289  }
290 
291  writer_->write(fieldName, tfield());
292  }
293 
294  wroteGeom_ = true;
295  return outputFile;
296 }
297 
298 
299 // Field writing methods
301 
302 
303 // ************************************************************************* //
Foam::vtk::outputOptions
Encapsulated combinations of output format options. This is primarily useful when defining the output...
Definition: foamVtkOutputOptions.H:59
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::surfaceWriter
Base class for surface writers.
Definition: surfaceWriter.H:111
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::dictionary::lookupOrDefaultCompat
T lookupOrDefaultCompat(const word &keyword, std::initializer_list< std::pair< const char *, int >> compat, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionary.H:1277
Foam::fileName::path
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:186
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::vtk::outputOptions::precision
unsigned precision() const
Return the ASCII write precision.
Definition: foamVtkOutputOptionsI.H:126
Foam::surfaceWriters::vtkWriter::write
virtual fileName write()
Write surface geometry to file.
Definition: vtkSurfaceWriter.C:193
Foam::meshedSurf::faces
virtual const faceList & faces() const =0
The faces used for the surface.
Foam::surfaceWriters::vtkWriter::close
virtual void close()
Finish output, clears backend.
Definition: vtkSurfaceWriter.C:165
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:75
Foam::meshedSurf
Abstract definition of a meshed surface defined by faces and points.
Definition: meshedSurf.H:49
Foam::surfaceWriters::vtkWriter::beginTime
virtual void beginTime(const Time &t)
Begin time step. Clears existing backend.
Definition: vtkSurfaceWriter.C:172
Foam::surfaceWriters::defineTypeName
defineTypeName(boundaryDataWriter)
Foam::vtk::outputOptions::legacy
bool legacy() const
True if writer uses legacy file format.
Definition: foamVtkOutputOptionsI.H:88
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::dictionary::lookupOrDefault
T lookupOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionary.H:1241
foamVtkSurfaceWriter.H
surfaceWriterMethods.H
Convenience macros for instantiating surfaceWriter methods.
Foam::vtk::outputOptions::fmt
formatType fmt() const
The output format type.
Definition: foamVtkOutputOptionsI.H:71
Foam::surfaceWriter::endTime
virtual void endTime()
End a time-step.
Definition: surfaceWriter.C:241
Foam::surfaceWriters::vtkWriter
A surfaceWriter for VTK legacy (.vtk) or XML (.vtp) format.
Definition: vtkSurfaceWriter.H:114
Foam::vtk::formatType
formatType
The output format type for file contents.
Definition: foamVtkCore.H:65
Foam::surfaceWriter::beginTime
virtual void beginTime(const Time &t)
Begin a time-step.
Definition: surfaceWriter.C:229
Foam::vtk::outputOptions::ascii
bool ascii() const
True if output format is ASCII.
Definition: foamVtkOutputOptionsI.H:120
Foam::Field< vector >
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::surfaceWriters::vtkWriter::endTime
virtual void endTime()
End time step. Clears existing backend.
Definition: vtkSurfaceWriter.C:186
Foam::meshedSurf::points
virtual const pointField & points() const =0
The points used for the surface.
timeName
word timeName
Definition: getTimeIndex.H:3
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::vtk::formatType::INLINE_BASE64
XML inline base64, base64Formatter.
Foam::fileName::ext
word ext() const
Return file name extension (part after last .)
Definition: fileNameI.H:228
Foam::surfaceWriters::vtkWriter::~vtkWriter
virtual ~vtkWriter()
Destructor.
Definition: vtkSurfaceWriter.C:157
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:438
Foam::surfaceWriters::addToRunTimeSelectionTable
addToRunTimeSelectionTable(surfaceWriter, boundaryDataWriter, word)
Foam::IOstreamOption::ASCII
"ascii"
Definition: IOstreamOption.H:66
Foam::IOstream::defaultPrecision
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:325
Foam::IOstreamOption::formatEnum
static streamFormat formatEnum(const word &formatName)
The stream format enum corresponding to the string.
Definition: IOstreamOption.C:56
Foam::vtk::surfaceWriter
Write faces/points (optionally with fields) as a vtp file or a legacy vtk file.
Definition: foamVtkSurfaceWriter.H:68
vtkSurfaceWriter.H
Foam::List< face >
points
const pointField & points
Definition: gmvOutputHeader.H:1
defineSurfaceWriterWriteFields
defineSurfaceWriterWriteFields(Foam::surfaceWriters::vtkWriter)
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::instant
An instant of time. Contains the time value and name.
Definition: instant.H:52
Foam::surfaceWriter::close
virtual void close()
Finish output, performing any necessary cleanup.
Definition: surfaceWriter.C:306
Foam::surfaceWriters::vtkWriter::vtkWriter
vtkWriter()
Construct null.
Definition: vtkSurfaceWriter.C:64
Foam::vtk::fileWriter::ext
word ext() const
File extension for current format type.
Definition: foamVtkFileWriterI.H:68
Foam::surfaceWriters::addNamedToRunTimeSelectionTable
addNamedToRunTimeSelectionTable(surfaceWriter, vtkWriter, word, vtp)