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 "cartesianCS.H"
70 #include "runTimeSelectionTables.H"
71 
72 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
73 
74 namespace Foam
75 {
76 
77 // Forward Declarations
78 class coordSetWriter;
79 class Time;
80 
81 Ostream& operator<<(Ostream& os, const InfoProxy<coordSetWriter>&);
82 
83 /*---------------------------------------------------------------------------*\
84  Class coordSetWriter Declaration
85 \*---------------------------------------------------------------------------*/
86 
87 class coordSetWriter
88 {
89 protected:
90 
91  // Protected Data
92 
93  //- Reference to coordinate set(s)
95 
96  //- Track times (eg, streamlines), one per coords_ entry
98 
99  //- The content is up-to-date?
100  mutable bool upToDate_;
102  //- Track if geometry has been written since the last open
103  mutable bool wroteGeom_;
104 
107 
108  //- Writer with buffering output
109  mutable bool buffering_;
110 
111  //- Prefer tracks to points during single set writing
112  bool useTracks_;
113 
114  //- Insert additional time sub-directory in the output path
115  bool useTimeDir_;
116 
117  //- Additional output verbosity
118  bool verbose_;
120  //- The number of fields
121  label nFields_;
122 
123  //- The current time value/name
125 
126  //- The full output directory and file (coords) name
128 
129  //- Output geometry scaling after rotate/translate
130  scalar geometryScale_;
131 
132  //- The centre of rotation (untranslate, translate)
135  //- Local coordinate system transformation
137 
138  //- Field level to remove (on output)
140 
141  //- Field scaling (on output)
143 
145  // Buffering
146 
147 #undef defineBufferMethod
148 #define defineBufferMethod(Type) \
149  \
150  /* Names of Type fields */ \
151  DynamicList<word> Type##Names_; \
152  \
153  /* Values of Type fields */ \
154  PtrDynList<Field<Type>> Type##Fields_; \
155  \
156  /* Add named Type field to buffering */ \
157  void appendField(const word& fieldName, const Field<Type>& vals) \
158  { \
159  Type##Names_.append(fieldName); \
160  Type##Fields_.append(vals.clone()); \
161  }
162 
163  defineBufferMethod(label);
170 #undef defineBufferMethod
171 
172  // Protected Member Functions
173 
174  // Buffering
176  //- Write line contents (eg, buffered)
177  static void writeLine(Ostream&, const UList<word>&, const char* sep);
178 
179  //- Write line contents (eg, buffered)
180  static void writeLine(Ostream&, const UList<scalar>&, const char* sep);
181 
182  //- Clear out buffering
183  void clearBuffers();
184 
185  //- The number of buffer data columns, after splitting into components
186  label nDataColumns() const;
187 
188  //- Write buffered data
189  virtual bool writeBuffered();
191  //- Write buffered data
193  (
195  const coordSet& coords,
196  const char* sep
197  ) const;
198 
199  //- Get buffered data line (components)
200  void getBufferLine
201  (
202  DynamicList<scalar>& buf,
203  const coordSet& coords,
204  const label pointi
205  ) const;
206 
207 
208  // File Operations
209 
210  //- Get expected (characteristic) output file name - information only
211  fileName getExpectedPath(const word& fileExt = word::null) const;
212 
213  //- Get field-prefixed output file name.
214  // Eg, dir/U_name.raw
216  (
217  const word& fieldName,
218  const word& fileExt = word::null
219  ) const;
220 
221  //- Verify that the outputPath_ has been set or FatalError
222  void checkOpen() const;
223 
224  //- Perform any merging if not already upToDate (parallel)
225  //- or simply mark as being up-to-date
226  virtual bool merge() const;
227 
228 
229  // Helpers
230 
231  template<class Type>
233  (
234  const word& fieldName,
235  const tmp<Field<Type>>& tfield
236  ) const;
237 
238  //- Repackage field into a UPtrList
239  template<class Type>
242  (
243  const Field<Type>& field
244  );
245 
246  //- Repackage multiple fields into a UPtrList
247  template<class Type>
250  (
251  const UList<Field<Type>>& fieldValues
252  );
253 
254  //- Write coordinates and values
255  template<class Type>
256  static void writeTable
257  (
258  Ostream& os,
259  const coordSet& coords,
260  const UList<Type>& values,
261  const char* sep
262  );
263 
264 
265  // Normal write templates
266 
267  //- Dummy templated write operation
268  template<class Type>
270  (
271  const word& fieldName,
272  const Field<Type>& values
273  )
274  {
279  return fileName::null;
280  }
281 
282  //- Dummy templated write operation. Multiple tracks
283  template<class Type>
285  (
286  const word& fieldName,
287  const List<Field<Type>>& fieldValues
288  )
289  {
290  return fileName::null;
291  }
292 
293 
294  //- No copy construct
295  coordSetWriter(const coordSetWriter&) = delete;
296 
297  //- No copy assignment
298  void operator=(const coordSetWriter&) = delete;
299 
300 
301 public:
302 
303  //- Runtime type information
304  TypeName("coordSetWriter");
305 
306  // Declare run-time constructor selection table
308  (
309  autoPtr,
311  word,
312  (),
313  ()
314  );
315 
317  (
318  autoPtr,
320  wordDict,
321  (
322  const dictionary& writeOptions
323  ),
324  (writeOptions)
325  );
326 
327 
328  // Helpers
329 
330  //- Same as fileFormats::getFormatOptions
332  (
333  const dictionary& dict,
334  const word& formatName,
335  const word& entryName = "formatOptions"
336  );
337 
338  //- Same as fileFormats::getFormatOptions
340  (
341  const dictionary& dict,
342  const dictionary& setDict,
343  const word& formatName,
344  const word& entryName = "formatOptions"
345  );
346 
347 
348  // Selectors
349 
350  //- True if New is likely to succeed for this writeType
351  static bool supportedType(const word& writeType);
352 
353  //- Return a reference to the selected writer
354  static autoPtr<coordSetWriter> New(const word& writeFormat);
355 
356  //- Return a reference to the selected writer
357  // Select with extra write option
359  (
360  const word& writeFormat,
361  const dictionary& writeOptions
362  );
363 
364 
365  // Constructors
366 
367  //- Default construct
368  coordSetWriter();
369 
370  //- Default construct with specified options
371  explicit coordSetWriter(const dictionary& options);
372 
373 
374  //- Destructor. Calls close()
375  virtual ~coordSetWriter();
376 
377 
378  // Member Functions
379 
380  // Helpers
381 
382  //- Name suffix based on fieldName (underscore separator)
383  static word suffix
384  (
385  const word& fldName,
386  const word& fileExt = word::null
387  );
388 
389  //- Name suffix based on fieldNames (underscore separator)
390  static word suffix
391  (
392  const wordList& fieldNames,
393  const word& fileExt = word::null
394  );
395 
396 
397  // Capability
398 
399  //- True if the format uses internal buffering (eg, column output)
400  virtual bool buffering() const;
401 
402  //- Turn internal buffering on/off (only if supported by the writer)
403  virtual bool buffering(const bool on);
404 
405  //- The writer is enabled. If the writer is not enabled, it may be
406  //- possible for the caller to skip various preparatory operations.
407  // This method is primarily useful for the null writer
408  virtual bool enabled() const
409  {
410  return true;
411  }
412 
413 
414  // Bookkeeping
415 
416  //- Does the writer need an update (eg, lagging behind other changes)
417  virtual bool needsUpdate() const;
418 
419  //- Geometry or fields written since the last open?
420  virtual bool wroteData() const;
421 
422  //- Mark that content changed and the writer will need an update,
423  //- and set nFields = 0.
424  // May also free up unneeded data.
425  // Return false if it was previously already expired.
426  virtual bool expire();
427 
428  //- Close any open output, remove coordSet associations and
429  //- expire the writer.
430  virtual void clear();
431 
432 
433  // Content Association
434 
435  //- Set coordinates, can also be nullptr
436  virtual void setCoordinates(const coordSet* coords);
437 
438  //- Set coordinates
439  virtual void setCoordinates(const coordSet& coords);
440 
441  //- Set track coordinates
442  virtual void setTracks(const UPtrList<coordSet>& tracks);
443 
444  //- Set track times
445  virtual void setTrackTimes(const UList<scalarField>& times);
446 
447 
448  // Queries, Access
449 
450  //- The number of associated points (local processor)
451  label numPoints() const;
452 
453  //- The number of coordinate tracks
454  label numTracks() const;
455 
456  //- Writer is associated with content
457  bool hasCoords() const;
458 
459  //- Writer is not associated with content
460  bool empty() const;
461 
462  //- Test if outputPath has been set
463  inline bool is_open() const noexcept;
464 
465  //- The number of expected output fields.
466  // Currently only used by the legacy VTK format.
467  inline label nFields() const noexcept;
468 
469  //- Set the number of expected output fields
470  // Currently only used by the legacy VTK format.
471  // \return old value
472  inline label nFields(const label n) noexcept;
473 
474  //- Prefer tracks to points during single set writing
475  inline bool useTracks() const noexcept;
476 
477  //- Enable/disable tracks preference
478  // \return old value
479  inline bool useTracks(const bool on) noexcept;
480 
481  //- Should a time directory be spliced into the output path?
482  inline bool useTimeDir() const noexcept;
483 
484  //- Enable/disable use of spliced output path
485  // \return old value
486  inline bool useTimeDir(const bool on) noexcept;
487 
488  //- Get output verbosity
489  inline bool verbose() const noexcept;
490 
491  //- Enable/disable verbose output
492  // \return old value
493  inline bool verbose(bool on) noexcept;
494 
495 
496  // Time
497 
498  //- True if there is a known time
499  inline bool hasTime() const;
500 
501  //- The current time value/name
502  inline const word& timeName() const;
503 
504  //- The current time value/name
505  inline scalar timeValue() const;
506 
507 
508  //- Set the current time
509  void setTime(const instant& inst);
510 
511  //- Set current time from timeValue, auto generating the name
512  void setTime(scalar timeValue);
513 
514  //- Set current time from timeValue and timeName
515  void setTime(scalar timeValue, const word& timeName);
516 
517  //- Clear the current time
518  void unsetTime();
519 
520 
521  //- Begin a time-step
522  virtual void beginTime(const Time& t);
523 
524  //- Begin a time-step
525  virtual void beginTime(const instant& inst);
526 
527  //- End a time-step
528  virtual void endTime();
529 
530 
531  // Output
532 
533  //- Expected (characteristic) output file name - information only.
534  //- Return empty when is_open() is false.
535  virtual fileName path() const = 0;
536 
537  //- Write separate geometry to file.
539 
540  //- Open for output on specified path, using existing content
541  virtual void open(const fileName& outputPath);
542 
543  //- Open from components
544  virtual void open
545  (
546  const coordSet& coords,
547  const fileName& outputPath
548  );
549 
550  //- Open from components
551  virtual void open
552  (
553  const UPtrList<coordSet>& tracks,
554  const fileName& outputPath
555  );
556 
557  //- Finish output, performing any necessary cleanup
558  // Optional force disassociation with any coordSet(s)
559  virtual void close(bool force = false);
560 
561 
562  // Other
563 
564  //- Return info proxy,
565  //- used to print information to a stream
566  virtual InfoProxy<coordSetWriter> info() const
567  {
568  return *this;
569  }
570 
571  //- Output info proxy
572  friend Ostream& operator<<
573  (
574  Ostream& os,
575  const InfoProxy<coordSetWriter>& iproxy
576  );
577 
578 
579  // Write methods
580 
581 #undef declareCoordSetWriterWriteMethod
582 #define declareCoordSetWriterWriteMethod(Type) \
583  \
584  \
585  virtual fileName write \
586  ( \
587  const word& fieldName, \
588  const Field<Type>& field \
589  ) = 0; \
590  \
591  \
592  virtual fileName write \
593  ( \
594  const word& fieldName, \
595  const List<Field<Type>>& fieldValues \
596  ) = 0;
597 
604 
605 
606 #undef declareCoordSetWriterWriteMethod
607 #define declareCoordSetWriterWriteMethod(Type) \
608  \
609  \
610  virtual fileName write \
611  ( \
612  const word& fieldName, \
613  const Field<Type>& values \
614  ); /* override */ \
615  \
616  \
617  virtual fileName write \
618  ( \
619  const word& fieldName, \
620  const List<Field<Type>>& fieldValues \
621  ); /* override */
622 };
623 
624 
625 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
626 
627 } // End namespace Foam
628 
629 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
630 
631 #include "coordSetWriterI.H"
632 
633 #ifdef NoRepository
634  #include "coordSetWriterTemplates.C"
635 #endif
636 
637 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
638 
639 #endif
640 
641 // ************************************************************************* //
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:72
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:129
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:111
void writeBufferContents(Ostream &os, const coordSet &coords, const char *sep) const
Write buffered data.
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.
A Cartesian coordinate system.
Definition: cartesianCS.H:65
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
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.
dictionary fieldScale_
Field scaling (on output)
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...
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:106
coordSetWriter()
Default construct.
dictionary fieldLevel_
Field level to remove (on output)
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:105
instant currTime_
The current time value/name.
void setTime(const instant &inst)
Set the current time.
scalar geometryScale_
Output geometry scaling after rotate/translate.
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:56
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.
coordSystem::cartesian geometryTransform_
Local coordinate system transformation.
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.
point geometryCentre_
The centre of rotation (untranslate, translate)
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.
fileName getExpectedPath(const word &fileExt=word::null) const
Get expected (characteristic) output file name - information only.
A class for managing temporary objects.
Definition: HashPtrTable.H:50
virtual void close(bool force=false)
Finish output, performing any necessary cleanup.
Tensor of scalars, i.e. Tensor<scalar>.
tmp< Field< Type > > adjustFieldTemplate(const word &fieldName, const tmp< Field< Type >> &tfield) const
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.