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-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 "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  writeNormal_(false),
70  fieldScale_(),
71  writer_(nullptr)
72 {}
73 
74 
76 (
77  const vtk::outputOptions& opts
78 )
79 :
80  surfaceWriter(),
81  fmtType_(static_cast<unsigned>(opts.fmt())),
82  precision_(opts.precision()),
83  writeNormal_(false),
84  fieldScale_(),
85  writer_(nullptr)
86 {}
87 
88 
90 (
91  const dictionary& options
92 )
93 :
94  surfaceWriter(options),
95  fmtType_(static_cast<unsigned>(vtk::formatType::INLINE_BASE64)),
96  precision_
97  (
98  options.getOrDefault("precision", IOstream::defaultPrecision())
99  ),
100  writeNormal_(options.getOrDefault("normal", false)),
101  fieldScale_(options.subOrEmptyDict("fieldScale")),
102  writer_(nullptr)
103 {
104  // format: ascii | binary
105  // legacy: true | false
106 
108 
109  opts.ascii
110  (
112  == IOstream::formatEnum("format", options, IOstream::BINARY)
113  );
114 
115  opts.legacy(options.getOrDefault("legacy", false));
116 
117  // Convert back to raw data type
118  fmtType_ = static_cast<unsigned>(opts.fmt());
119 }
120 
121 
123 (
124  const meshedSurf& surf,
125  const fileName& outputPath,
126  bool parallel,
127  const dictionary& options
128 )
129 :
130  vtkWriter(options)
131 {
132  open(surf, outputPath, parallel);
133 }
134 
135 
137 (
138  const pointField& points,
139  const faceList& faces,
140  const fileName& outputPath,
141  bool parallel,
142  const dictionary& options
143 )
144 :
145  vtkWriter(options)
146 {
147  open(points, faces, outputPath, parallel);
148 }
149 
150 
151 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
152 
154 {
155  close();
156 }
157 
158 
159 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
160 
162 {
163  writer_.clear();
165 }
166 
167 
169 {
170  writer_.clear();
172 }
173 
174 
176 {
177  writer_.clear();
179 }
180 
181 
183 {
184  writer_.clear();
186 }
187 
188 
190 {
191  checkOpen();
192 
193  if (needsUpdate())
194  {
195  writer_.clear();
196  }
197  merge();
198 
199  // From raw unsigned values to vtk::outputOptions
200  vtk::outputOptions opts(static_cast<vtk::formatType>(fmtType_), precision_);
201 
202 
203  // Geometry: rootdir/<TIME>/surfaceName.{vtk|vtp}
204 
205  fileName outputFile = outputPath_;
206  if (useTimeDir() && !timeName().empty())
207  {
208  // Splice in time-directory
209  outputFile = outputPath_.path() / timeName() / outputPath_.name();
210  }
211  outputFile.ext(vtk::surfaceWriter::ext(opts));
212 
213  if (verbose_)
214  {
215  Info<< "Writing geometry to " << outputFile << endl;
216  }
217 
218  const meshedSurf& surf = surface();
219 
220  if (!writer_ && (Pstream::master() || !parallel_))
221  {
222  writer_.reset
223  (
225  (
226  surf.points(),
227  surf.faces(),
228  opts,
229  outputFile,
230  false // serial!
231  )
232  );
233 
234  if (this->hasTime())
235  {
236  // Time name in title
237  writer_->setTime(currTime_);
238  writer_->writeTimeValue();
239  }
240  else
241  {
242  // Surface name in title
243  writer_->beginFile(outputPath_.nameLessExt());
244  }
245 
246  writer_->writeGeometry();
247 
248  if (writeNormal_)
249  {
250  const faceList& fcs = surf.faces();
251  const pointField& pts = surf.points();
252 
253  Field<vector> normals(fcs.size());
254  forAll(fcs, facei)
255  {
256  normals[facei] = fcs[facei].areaNormal(pts);
257  }
258 
259  label nCellData = 1;
260 
261  if (!this->isPointData())
262  {
263  // Ill-defined with legacy() if nFields_ not properly set...
264  nCellData += nFields_;
265  }
266 
267  writer_->beginCellData(nCellData);
268  writer_->write("area-normal", normals);
269  }
270  }
271 
272  wroteGeom_ = true;
273  return outputFile;
274 }
275 
276 
277 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
278 
279 template<class Type>
280 Foam::fileName Foam::surfaceWriters::vtkWriter::writeTemplate
281 (
282  const word& fieldName,
283  const Field<Type>& localValues
284 )
285 {
286  // Field: rootdir/<TIME>/surfaceName.{vtk|vtp}
287 
288  // Open file, writing geometry (if required)
289  fileName outputFile = this->write();
290 
291 
292  // Output scaling for the variable, but not for integer types.
293  // could also solve with clever templating
294 
295  const scalar varScale =
296  (
297  std::is_integral<Type>::value
298  ? scalar(1)
299  : fieldScale_.getOrDefault<scalar>(fieldName, 1)
300  );
301 
302  if (verbose_)
303  {
304  Info<< "Writing field " << fieldName;
305  if (!equal(varScale, 1))
306  {
307  Info<< " (scaling " << varScale << ')';
308  }
309  Info<< " to " << outputFile << endl;
310  }
311 
312 
313  // Implicit geometry merge()
314  tmp<Field<Type>> tfield = mergeField(localValues) * varScale;
315 
316  if (Pstream::master() || !parallel_)
317  {
318  if (!nFields_ && writer_->legacy())
319  {
320  // Emit error message, but attempt to recover anyhow
321  nFields_ = 1;
322 
324  << "Using VTK legacy format, but did not define nFields!"
325  << nl
326  << "Assuming nFields=1 (may be incorrect) and continuing..."
327  << nl
328  << " Field " << fieldName << " to " << outputFile << nl;
329 
330  Info<< FatalError;
331  Info<< endl;
332  }
333 
334  if (this->isPointData())
335  {
336  writer_->beginPointData(nFields_);
337  }
338  else
339  {
340  writer_->beginCellData(nFields_);
341  }
342 
343  writer_->write(fieldName, tfield());
344  }
345 
346  wroteGeom_ = true;
347  return outputFile;
348 }
349 
350 
351 // Field writing methods
353 
354 
355 // ************************************************************************* //
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:65
Foam::surfaceWriter
Base class for surface writers.
Definition: surfaceWriter.H:114
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::surfaceWriters::defineTypeName
defineTypeName(abaqusWriter)
Foam::fileName::path
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:176
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
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:189
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:161
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:79
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:168
Foam::UPstream::master
static bool master(const label communicator=worldComm)
Am I the master process.
Definition: UPstream.H:457
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:369
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
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::surfaceWriter::endTime
virtual void endTime()
End a time-step.
Definition: surfaceWriter.C:244
Foam::surfaceWriters::addToRunTimeSelectionTable
addToRunTimeSelectionTable(surfaceWriter, abaqusWriter, word)
Foam::surfaceWriters::vtkWriter
A surfaceWriter for VTK legacy (.vtk) or XML (.vtp) format.
Definition: vtkSurfaceWriter.H:130
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:232
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 (stdout output on master, null elsewhere)
Foam::surfaceWriters::vtkWriter::endTime
virtual void endTime()
End time step. Clears existing backend.
Definition: vtkSurfaceWriter.C:182
Foam::meshedSurf::points
virtual const pointField & points() const =0
The points used for the surface.
timeName
word timeName
Definition: getTimeIndex.H:3
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::vtk::formatType::INLINE_BASE64
XML inline base64, base64Formatter.
Foam::Ostream::write
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Foam::fileName::ext
word ext() const
Return file name extension (part after last .)
Definition: fileNameI.H:218
Foam::surfaceWriters::vtkWriter::~vtkWriter
virtual ~vtkWriter()
Destructor. Calls close()
Definition: vtkSurfaceWriter.C:153
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::dictionary::subOrEmptyDict
dictionary subOrEmptyDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX, const bool mandatory=false) const
Definition: dictionary.C:540
Foam::IOstream::defaultPrecision
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition: IOstream.H:342
Foam::IOstreamOption::BINARY
"binary"
Definition: IOstreamOption.H:73
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::vtk::surfaceWriter
Write faces/points (optionally with fields) as a vtp file or a legacy vtk file.
Definition: foamVtkSurfaceWriter.H:65
Foam::IOstreamOption::formatEnum
static streamFormat formatEnum(const word &formatName, const streamFormat deflt=streamFormat::ASCII)
Definition: IOstreamOption.C:53
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:36
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:309
Foam::dictionary::getOrDefault
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:148
Foam::surfaceWriters::vtkWriter::vtkWriter
vtkWriter()
Default construct.
Definition: vtkSurfaceWriter.C:64
Foam::vtk::fileWriter::ext
word ext() const
File extension for current format type.
Definition: foamVtkFileWriterI.H:68
Foam::equal
bool equal(const T &s1, const T &s2)
Compare two values for equality.
Definition: doubleFloat.H:46
Foam::surfaceWriters::addNamedToRunTimeSelectionTable
addNamedToRunTimeSelectionTable(surfaceWriter, vtkWriter, word, vtp)