coordSetWriter.H
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) 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 Class
27  Foam::coordSetWriter
28 
29 Description
30  Base class for writing coordSet(s) and tracks with fields.
31 
32  Example:
33  \verbatim
34  coordSet coords(...);
35 
36  // Construct writer of xmgr type
37  autoPtr<coordSetWriter> writer(coordSetWriter::New("vtk"));
38 
39  writer.open(coords, path/name);
40 
41  writer.write("density", rho);
42  writer.write("velocity", U);
43 
44  \endverbatim
45 
46 SourceFiles
47  coordSetWriterI.H
48  coordSetWriter.C
49  coordSetWriterBuffers.C
50  coordSetWriterNew.C
51 
52 \*---------------------------------------------------------------------------*/
53 
54 #ifndef Foam_coordSetWriter_H
55 #define Foam_coordSetWriter_H
56 
57 #include "coordSet.H"
58 #include "typeInfo.H"
59 #include "vector.H"
60 #include "tensor.H"
61 #include "fileName.H"
62 #include "wordList.H"
63 #include "Field.H"
64 #include "DynamicList.H"
65 #include "PtrDynList.H"
66 #include "UPtrList.H"
67 #include "instant.H"
68 #include "InfoProxy.H"
69 #include "runTimeSelectionTables.H"
70 
71 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
72 
73 namespace Foam
74 {
75 
76 // Forward Declarations
77 class coordSetWriter;
78 class Time;
79 
80 Ostream& operator<<(Ostream& os, const InfoProxy<coordSetWriter>&);
81 
82 /*---------------------------------------------------------------------------*\
83  Class coordSetWriter Declaration
84 \*---------------------------------------------------------------------------*/
85 
86 class coordSetWriter
87 {
88 protected:
89 
90  // Protected Data
91 
92  //- Reference to coordinate set(s)
94 
95  //- Track times (eg, streamlines), one per coords_ entry
97 
98  //- The content is up-to-date?
99  mutable bool upToDate_;
101  //- Track if geometry has been written since the last open
102  mutable bool wroteGeom_;
103 
106 
107  //- Writer with buffering output
108  mutable bool buffering_;
109 
110  //- Prefer tracks to points during single set writing
111  bool useTracks_;
112 
113  //- Insert additional time sub-directory in the output path
114  bool useTimeDir_;
115 
116  //- Additional output verbosity
117  bool verbose_;
119  //- The number of fields
120  label nFields_;
121 
122  //- The current time value/name
124 
125  //- The full output directory and file (coords) name
127 
129  // Buffering
130 
131 #undef defineBufferMethod
132 #define defineBufferMethod(Type) \
133  \
134  /* Names of Type fields */ \
135  DynamicList<word> Type##Names_; \
136  \
137  /* Values of Type fields */ \
138  PtrDynList<Field<Type>> Type##Fields_; \
139  \
140  /* Add named Type field to buffering */ \
141  void appendField(const word& fieldName, const Field<Type>& vals) \
142  { \
143  Type##Names_.append(fieldName); \
144  Type##Fields_.append(vals.clone()); \
145  }
146 
147  defineBufferMethod(label);
148  defineBufferMethod(scalar);
153 
154 #undef defineBufferMethod
155 
156  // Protected Member Functions
157 
158  // Buffering
159 
160  //- Write line contents (eg, buffered)
161  static void writeLine(Ostream&, const UList<word>&, const char* sep);
162 
163  //- Write line contents (eg, buffered)
164  static void writeLine(Ostream&, const UList<scalar>&, const char* sep);
166  //- Clear out buffering
167  void clearBuffers();
169  //- The number of buffer data columns, after splitting into components
170  label nDataColumns() const;
171 
172  //- Write buffered data
173  virtual bool writeBuffered();
174 
175  //- Write buffered data
177  (
178  Ostream& os,
179  const coordSet& coords,
180  const char* sep
181  ) const;
182 
183  //- Get buffered data line (components)
184  void getBufferLine
185  (
186  DynamicList<scalar>& buf,
187  const coordSet& coords,
188  const label pointi
189  ) const;
190 
191 
192  // File Operations
193 
194  //- Get expected (characteristic) output file name - information only
195  fileName getExpectedPath(const word& fileExt = word::null) const;
196 
197  //- Get field-prefixed output file name.
198  // Eg, dir/U_name.raw
200  (
201  const word& fieldName,
202  const word& fileExt = word::null
203  ) const;
204 
205  //- Verify that the outputPath_ has been set or FatalError
206  void checkOpen() const;
207 
208  //- Perform any merging if not already upToDate (parallel)
209  //- or simply mark as being up-to-date
210  virtual bool merge() const;
211 
212 
213  // Helpers
214 
215  //- Repackage field into a UPtrList
216  template<class Type>
219  (
220  const Field<Type>& field
221  );
222 
223  //- Repackage multiple fields into a UPtrList
224  template<class Type>
227  (
228  const UList<Field<Type>>& fieldValues
229  );
230 
231  //- Write coordinates and values
232  template<class Type>
233  static void writeTable
234  (
235  Ostream& os,
236  const coordSet& coords,
237  const UList<Type>& values,
238  const char* sep
239  );
240 
241 
242  // Normal write templates
243 
244  //- Dummy templated write operation
245  template<class Type>
247  (
248  const word& fieldName,
249  const Field<Type>& values
250  )
251  {
256  return fileName::null;
257  }
258 
259  //- Dummy templated write operation. Multiple tracks
260  template<class Type>
262  (
263  const word& fieldName,
264  const List<Field<Type>>& fieldValues
265  )
266  {
267  return fileName::null;
268  }
269 
270 
271  //- No copy construct
272  coordSetWriter(const coordSetWriter&) = delete;
273 
274  //- No copy assignment
275  void operator=(const coordSetWriter&) = delete;
276 
277 
278 public:
279 
280  //- Runtime type information
281  TypeName("coordSetWriter");
282 
283  // Declare run-time constructor selection table
285  (
286  autoPtr,
288  word,
289  (),
290  ()
291  );
292 
294  (
297  wordDict,
298  (
299  const dictionary& writeOptions
300  ),
301  (writeOptions)
302  );
303 
304 
305  // Helpers
306 
307  //- Same as fileFormats::getFormatOptions
309  (
310  const dictionary& dict,
311  const word& formatName,
312  const word& entryName = "formatOptions"
313  );
314 
315  //- Same as fileFormats::getFormatOptions
317  (
318  const dictionary& dict,
319  const dictionary& setDict,
320  const word& formatName,
321  const word& entryName = "formatOptions"
322  );
323 
324 
325  // Selectors
326 
327  //- True if New is likely to succeed for this writeType
328  static bool supportedType(const word& writeType);
329 
330  //- Return a reference to the selected writer
331  static autoPtr<coordSetWriter> New(const word& writeFormat);
332 
333  //- Return a reference to the selected writer
334  // Select with extra write option
336  (
337  const word& writeFormat,
338  const dictionary& writeOptions
339  );
340 
341 
342  // Constructors
343 
344  //- Default construct
345  coordSetWriter();
346 
347  //- Default construct with specified options
348  explicit coordSetWriter(const dictionary& options);
349 
350 
351  //- Destructor. Calls close()
352  virtual ~coordSetWriter();
353 
354 
355  // Member Functions
356 
357  // Helpers
358 
359  //- Name suffix based on fieldName (underscore separator)
360  static word suffix
361  (
362  const word& fldName,
363  const word& fileExt = word::null
364  );
365 
366  //- Name suffix based on fieldNames (underscore separator)
367  static word suffix
368  (
369  const wordList& fieldNames,
370  const word& fileExt = word::null
371  );
372 
373 
374  // Capability
375 
376  //- True if the format uses internal buffering (eg, column output)
377  virtual bool buffering() const;
378 
379  //- Turn internal buffering on/off (only if supported by the writer)
380  virtual bool buffering(const bool on);
381 
382  //- The writer is enabled. If the writer is not enabled, it may be
383  //- possible for the caller to skip various preparatory operations.
384  // This method is primarily useful for the null writer
385  virtual bool enabled() const
386  {
387  return true;
388  }
389 
390 
391  // Bookkeeping
392 
393  //- Does the writer need an update (eg, lagging behind other changes)
394  virtual bool needsUpdate() const;
395 
396  //- Geometry or fields written since the last open?
397  virtual bool wroteData() const;
398 
399  //- Mark that content changed and the writer will need an update,
400  //- and set nFields = 0.
401  // May also free up unneeded data.
402  // Return false if it was previously already expired.
403  virtual bool expire();
404 
405  //- Close any open output, remove coordSet associations and
406  //- expire the writer.
407  virtual void clear();
408 
409 
410  // Content Association
411 
412  //- Set coordinates, can also be nullptr
413  virtual void setCoordinates(const coordSet* coords);
414 
415  //- Set coordinates
416  virtual void setCoordinates(const coordSet& coords);
417 
418  //- Set track coordinates
419  virtual void setTracks(const UPtrList<coordSet>& tracks);
420 
421  //- Set track times
422  virtual void setTrackTimes(const UList<scalarField>& times);
423 
424 
425  // Queries, Access
426 
427  //- The number of associated points (local processor)
428  label numPoints() const;
429 
430  //- The number of coordinate tracks
431  label numTracks() const;
432 
433  //- Writer is associated with content
434  bool hasCoords() const;
435 
436  //- Writer is not associated with content
437  bool empty() const;
438 
439  //- Test if outputPath has been set
440  inline bool is_open() const noexcept;
441 
442  //- The number of expected output fields.
443  // Currently only used by the legacy VTK format.
444  inline label nFields() const noexcept;
445 
446  //- Set the number of expected output fields
447  // Currently only used by the legacy VTK format.
448  // \return old value
449  inline label nFields(const label n) noexcept;
450 
451  //- Prefer tracks to points during single set writing
452  inline bool useTracks() const noexcept;
453 
454  //- Enable/disable tracks preference
455  // \return old value
456  inline bool useTracks(const bool on) noexcept;
457 
458  //- Should a time directory be spliced into the output path?
459  inline bool useTimeDir() const noexcept;
460 
461  //- Enable/disable use of spliced output path
462  // \return old value
463  inline bool useTimeDir(const bool on) noexcept;
464 
465  //- Get output verbosity
466  inline bool verbose() const noexcept;
467 
468  //- Enable/disable verbose output
469  // \return old value
470  inline bool verbose(bool on) noexcept;
471 
472 
473  // Time
474 
475  //- True if there is a known time
476  inline bool hasTime() const;
477 
478  //- The current time value/name
479  inline const word& timeName() const;
480 
481  //- The current time value/name
482  inline scalar timeValue() const;
483 
484 
485  //- Set the current time
486  void setTime(const instant& inst);
487 
488  //- Set current time from timeValue, auto generating the name
489  void setTime(scalar timeValue);
490 
491  //- Set current time from timeValue and timeName
492  void setTime(scalar timeValue, const word& timeName);
493 
494  //- Clear the current time
495  void unsetTime();
496 
497 
498  //- Begin a time-step
499  virtual void beginTime(const Time& t);
500 
501  //- Begin a time-step
502  virtual void beginTime(const instant& inst);
503 
504  //- End a time-step
505  virtual void endTime();
506 
507 
508  // Output
509 
510  //- Expected (characteristic) output file name - information only.
511  //- Return empty when is_open() is false.
512  virtual fileName path() const = 0;
513 
514  //- Write separate geometry to file.
516 
517  //- Open for output on specified path, using existing content
518  virtual void open(const fileName& outputPath);
519 
520  //- Open from components
521  virtual void open
522  (
523  const coordSet& coords,
524  const fileName& outputPath
525  );
526 
527  //- Open from components
528  virtual void open
529  (
530  const UPtrList<coordSet>& tracks,
531  const fileName& outputPath
532  );
533 
534  //- Finish output, performing any necessary cleanup
535  // Optional force disassociation with any coordSet(s)
536  virtual void close(bool force = false);
537 
538 
539  // Other
540 
541  //- Return info proxy,
542  //- used to print information to a stream
543  virtual InfoProxy<coordSetWriter> info() const
544  {
545  return *this;
546  }
547 
548  //- Output info proxy
549  friend Ostream& operator<<
550  (
551  Ostream& os,
552  const InfoProxy<coordSetWriter>& iproxy
553  );
554 
555 
556  // Write methods
557 
558 #undef declareCoordSetWriterWriteMethod
559 #define declareCoordSetWriterWriteMethod(Type) \
560  \
561  \
562  virtual fileName write \
563  ( \
564  const word& fieldName, \
565  const Field<Type>& field \
566  ) = 0; \
567  \
568  \
569  virtual fileName write \
570  ( \
571  const word& fieldName, \
572  const List<Field<Type>>& fieldValues \
573  ) = 0;
574 
581 
582 
583 #undef declareCoordSetWriterWriteMethod
584 #define declareCoordSetWriterWriteMethod(Type) \
585  \
586  \
587  virtual fileName write \
588  ( \
589  const word& fieldName, \
590  const Field<Type>& values \
591  ); /* override */ \
592  \
593  \
594  virtual fileName write \
595  ( \
596  const word& fieldName, \
597  const List<Field<Type>>& fieldValues \
598  ); /* override */
599 };
600 
601 
602 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
603 
604 } // End namespace Foam
605 
606 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
607 
608 #include "coordSetWriterI.H"
609 
610 #ifdef NoRepository
611  #include "coordSetWriterTemplates.C"
612 #endif
613 
614 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
615 
616 #endif
617 
618 // ************************************************************************* //
virtual void open(const fileName &outputPath)
Write separate geometry to file.
virtual bool writeBuffered()
Write buffered data.
dictionary dict
virtual void setCoordinates(const coordSet *coords)
Set coordinates, can also be nullptr.
static void writeLine(Ostream &, const UList< word > &, const char *sep)
Write line contents (eg, buffered)
rDeltaTY field()
A class for handling file names.
Definition: fileName.H:71
virtual bool expire()
Mark that content changed and the writer will need an update, and set nFields = 0.
TypeName("coordSetWriter")
Runtime type information.
bool wroteGeom_
Track if geometry has been written since the last open.
void checkOpen() const
Verify that the outputPath_ has been set or FatalError.
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:120
label nDataColumns() const
The number of buffer data columns, after splitting into components.
bool useTracks_
Prefer tracks to points during single set writing.
static const fileName null
An empty fileName.
Definition: fileName.H:110
void writeBufferContents(Ostream &os, const coordSet &coords, const char *sep) const
Write buffered data.
Tensor< scalar > tensor
Definition: symmTensor.H:57
virtual bool buffering() const
True if the format uses internal buffering (eg, column output)
virtual ~coordSetWriter()
Destructor. Calls close()
bool useTracks() const noexcept
Prefer tracks to points during single set writing.
bool hasTime() const
True if there is a known time.
virtual bool enabled() const
The writer is enabled. If the writer is not enabled, it may be possible for the caller to skip variou...
void unsetTime()
Clear the current time.
bool hasCoords() const
Writer is associated with content.
fileName outputPath_
The full output directory and file (coords) name.
virtual void setTracks(const UPtrList< coordSet > &tracks)
Set track coordinates.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
List< scalarField > trackTimes_
Track times (eg, streamlines), one per coords_ entry.
virtual bool wroteData() const
Geometry or fields written since the last open?
bool useTimeDir_
Insert additional time sub-directory in the output path.
static word suffix(const word &fldName, const word &fileExt=word::null)
Name suffix based on fieldName (underscore separator)
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
virtual void beginTime(const Time &t)
Begin a time-step.
#define defineBufferMethod(Type)
static autoPtr< coordSetWriter > New(const word &writeFormat)
Return a reference to the selected writer.
static UPtrList< const Field< Type > > repackageFields(const Field< Type > &field)
Repackage field into a UPtrList.
Holds list of sampling positions.
Definition: coordSet.H:49
Generic templated field type.
Definition: Field.H:62
SymmTensor< scalar > symmTensor
SymmTensor of scalars, i.e. SymmTensor<scalar>.
Definition: symmTensor.H:55
A class for handling words, derived from Foam::string.
Definition: word.H:63
fileName writeTemplate(const word &fieldName, const Field< Type > &values)
Dummy templated write operation.
bool useTimeDir() const noexcept
Should a time directory be spliced into the output path?
bool is_open() const noexcept
Test if outputPath has been set.
Base class for writing coordSet(s) and tracks with fields.
static const word null
An empty word.
Definition: word.H:84
scalar timeValue() const
The current time value/name.
void clearBuffers()
Clear out buffering.
bool buffering_
Writer with buffering output.
declareRunTimeSelectionTable(autoPtr, coordSetWriter, word,(),())
void operator=(const coordSetWriter &)=delete
No copy assignment.
virtual fileName path() const =0
Expected (characteristic) output file name - information only. Return empty when is_open() is false...
Vector< scalar > vector
Definition: vector.H:57
static dictionary formatOptions(const dictionary &dict, const word &formatName, const word &entryName="formatOptions")
Same as fileFormats::getFormatOptions.
bool empty() const
Writer is not associated with content.
label nFields_
The number of fields.
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: HashTable.H:100
coordSetWriter()
Default construct.
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:99
instant currTime_
The current time value/name.
void setTime(const instant &inst)
Set the current time.
label nFields() const noexcept
The number of expected output fields.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
const direction noexcept
Definition: Scalar.H:258
label numTracks() const
The number of coordinate tracks.
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
virtual void clear()
Close any open output, remove coordSet associations and expire the writer.
bool upToDate_
The content is up-to-date?
OBJstream os(runTime.globalPath()/outputName)
static void writeTable(Ostream &os, const coordSet &coords, const UList< Type > &values, const char *sep)
Write coordinates and values.
fileName getFieldPrefixedPath(const word &fieldName, const word &fileExt=word::null) const
Get field-prefixed output file name.
UPtrList< const coordSet > coords_
Reference to coordinate set(s)
const word & timeName() const
The current time value/name.
virtual void endTime()
End a time-step.
static bool supportedType(const word &writeType)
True if New is likely to succeed for this writeType.
An instant of time. Contains the time value and name. Uses Foam::Time when formatting the name...
Definition: instant.H:53
bool verbose_
Additional output verbosity.
virtual bool needsUpdate() const
Does the writer need an update (eg, lagging behind other changes)
A helper class for outputting values to Ostream.
Definition: ensightCells.H:43
virtual void setTrackTimes(const UList< scalarField > &times)
Set track times.
label numPoints() const
The number of associated points (local processor)
virtual bool merge() const
Perform any merging if not already upToDate (parallel) or simply mark as being up-to-date.
label n
#define declareCoordSetWriterWriteMethod(Type)
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
bool verbose() const noexcept
Get output verbosity.
Macros to ease declaration of run-time selection tables.
SphericalTensor< scalar > sphericalTensor
SphericalTensor of scalars, i.e. SphericalTensor<scalar>.
fileName getExpectedPath(const word &fileExt=word::null) const
Get expected (characteristic) output file name - information only.
virtual void close(bool force=false)
Finish output, performing any necessary cleanup.
Tensor of scalars, i.e. Tensor<scalar>.
void getBufferLine(DynamicList< scalar > &buf, const coordSet &coords, const label pointi) const
Get buffered data line (components)
virtual InfoProxy< coordSetWriter > info() const
Return info proxy, used to print information to a stream.
Namespace for OpenFOAM.