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-------------------------------------------------------------------------------
10License
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
26Class
27 Foam::coordSetWriter
28
29Description
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
46SourceFiles
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"
70
71// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
72
73namespace Foam
74{
75
76// Forward Declarations
77class coordSetWriter;
78class Time;
79
80Ostream& operator<<(Ostream& os, const InfoProxy<coordSetWriter>&);
81
82/*---------------------------------------------------------------------------*\
83 Class coordSetWriter Declaration
84\*---------------------------------------------------------------------------*/
87{
88protected:
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_;
100
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_;
118
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
128
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 }
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);
165
166 //- Clear out buffering
167 void clearBuffers();
168
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 (
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
278public:
279
280 //- Runtime type information
281 TypeName("coordSetWriter");
282
283 // Declare run-time constructor selection table
285 (
286 autoPtr,
288 word,
289 (),
290 ()
291 );
294 (
295 autoPtr,
297 wordDict,
298 (
299 const dictionary& writeOptions
300 ),
301 (writeOptions)
302 );
303
304
305 // Selectors
306
307 //- True if New is likely to succeed for this writeType
308 static bool supportedType(const word& writeType);
309
310 //- Return a reference to the selected writer
311 static autoPtr<coordSetWriter> New(const word& writeFormat);
312
313 //- Return a reference to the selected writer
314 // Select with extra write option
316 (
317 const word& writeFormat,
318 const dictionary& writeOptions
319 );
320
321
322 // Constructors
323
324 //- Default construct
326
327 //- Default construct with specified options
328 explicit coordSetWriter(const dictionary& options);
329
330
331 //- Destructor. Calls close()
332 virtual ~coordSetWriter();
333
334
335 // Member Functions
336
337 // Helpers
338
339 //- Name suffix based on fieldName (underscore separator)
340 static word suffix
341 (
342 const word& fldName,
343 const word& fileExt = word::null
344 );
345
346 //- Name suffix based on fieldNames (underscore separator)
347 static word suffix
348 (
349 const wordList& fieldNames,
350 const word& fileExt = word::null
351 );
352
353
354 // Capability
355
356 //- True if the format uses internal buffering (eg, column output)
357 virtual bool buffering() const;
358
359 //- Turn internal buffering on/off (only if supported by the writer)
360 virtual bool buffering(const bool on);
361
362 //- The writer is enabled. If the writer is not enabled, it may be
363 //- possible for the caller to skip various preparatory operations.
364 // This method is primarily useful for the null writer
365 virtual bool enabled() const
366 {
367 return true;
368 }
369
370
371 // Bookkeeping
372
373 //- Does the writer need an update (eg, lagging behind other changes)
374 virtual bool needsUpdate() const;
375
376 //- Geometry or fields written since the last open?
377 virtual bool wroteData() const;
378
379 //- Mark that content changed and the writer will need an update,
380 //- and set nFields = 0.
381 // May also free up unneeded data.
382 // Return false if it was previously already expired.
383 virtual bool expire();
384
385 //- Close any open output, remove coordSet associations and
386 //- expire the writer.
387 virtual void clear();
388
389
390 // Content Association
391
392 //- Set coordinates, can also be nullptr
393 virtual void setCoordinates(const coordSet* coords);
394
395 //- Set coordinates
396 virtual void setCoordinates(const coordSet& coords);
397
398 //- Set track coordinates
399 virtual void setTracks(const UPtrList<coordSet>& tracks);
400
401 //- Set track times
402 virtual void setTrackTimes(const UList<scalarField>& times);
403
404
405 // Queries, Access
406
407 //- The number of associated points (local processor)
408 label numPoints() const;
409
410 //- The number of coordinate tracks
411 label numTracks() const;
412
413 //- Writer is associated with content
414 bool hasCoords() const;
415
416 //- Writer is not associated with content
417 bool empty() const;
418
419 //- Test if outputPath has been set
420 inline bool is_open() const noexcept;
421
422 //- The number of expected output fields.
423 // Currently only used by the legacy VTK format.
424 inline label nFields() const noexcept;
425
426 //- Set the number of expected output fields
427 // Currently only used by the legacy VTK format.
428 // \return old value
429 inline label nFields(const label n) noexcept;
430
431 //- Prefer tracks to points during single set writing
432 inline bool useTracks() const noexcept;
433
434 //- Enable/disable tracks preference
435 // \return old value
436 inline bool useTracks(const bool on) noexcept;
437
438 //- Should a time directory be spliced into the output path?
439 inline bool useTimeDir() const noexcept;
440
441 //- Enable/disable use of spliced output path
442 // \return old value
443 inline bool useTimeDir(const bool on) noexcept;
444
445 //- Get output verbosity
446 inline bool verbose() const noexcept;
447
448 //- Enable/disable verbose output
449 // \return old value
450 inline bool verbose(bool on) noexcept;
451
452
453 // Time
454
455 //- True if there is a known time
456 inline bool hasTime() const;
457
458 //- The current time value/name
459 inline const word& timeName() const;
460
461 //- The current time value/name
462 inline scalar timeValue() const;
463
464
465 //- Set the current time
466 void setTime(const instant& inst);
467
468 //- Set current time from timeValue, auto generating the name
469 void setTime(scalar timeValue);
470
471 //- Set current time from timeValue and timeName
472 void setTime(scalar timeValue, const word& timeName);
473
474 //- Clear the current time
475 void unsetTime();
476
477
478 //- Begin a time-step
479 virtual void beginTime(const Time& t);
480
481 //- Begin a time-step
482 virtual void beginTime(const instant& inst);
483
484 //- End a time-step
485 virtual void endTime();
486
487
488 // Output
489
490 //- Expected (characteristic) output file name - information only.
491 //- Return empty when is_open() is false.
492 virtual fileName path() const = 0;
493
494 //- Write separate geometry to file.
496
497 //- Open for output on specified path, using existing content
498 virtual void open(const fileName& outputPath);
499
500 //- Open from components
501 virtual void open
502 (
503 const coordSet& coords,
504 const fileName& outputPath
505 );
506
507 //- Open from components
508 virtual void open
509 (
510 const UPtrList<coordSet>& tracks,
511 const fileName& outputPath
512 );
513
514 //- Finish output, performing any necessary cleanup
515 // Optional force disassociation with any coordSet(s)
516 virtual void close(bool force = false);
517
518
519 // Other
520
521 //- Return info proxy.
522 virtual InfoProxy<coordSetWriter> info() const
523 {
524 return *this;
525 }
526
527 //- Output info proxy
528 friend Ostream& operator<<
529 (
530 Ostream& os,
532 );
533
534
535 // Write methods
536
537#undef declareCoordSetWriterWriteMethod
538#define declareCoordSetWriterWriteMethod(Type) \
539 \
540 \
541 virtual fileName write \
542 ( \
543 const word& fieldName, \
544 const Field<Type>& field \
545 ) = 0; \
546 \
547 \
548 virtual fileName write \
549 ( \
550 const word& fieldName, \
551 const List<Field<Type>>& fieldValues \
552 ) = 0;
560
561
562#undef declareCoordSetWriterWriteMethod
563#define declareCoordSetWriterWriteMethod(Type) \
564 \
565 \
566 virtual fileName write \
567 ( \
568 const word& fieldName, \
569 const Field<Type>& values \
570 ); /* override */ \
571 \
572 \
573 virtual fileName write \
574 ( \
575 const word& fieldName, \
576 const List<Field<Type>>& fieldValues \
577 ); /* override */
578};
579
580
581// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
582
583} // End namespace Foam
584
585// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
586
587#include "coordSetWriterI.H"
588
589#ifdef NoRepository
591#endif
592
593// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
594
595#endif
596
597// ************************************************************************* //
label n
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:72
Generic templated field type.
Definition: Field.H:82
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:52
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:80
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:71
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
Base class for writing coordSet(s) and tracks with fields.
void getBufferLine(DynamicList< scalar > &buf, const coordSet &coords, const label pointi) const
Get buffered data line (components)
virtual void open(const fileName &outputPath)
Write separate geometry to file.
fileName getExpectedPath(const word &fileExt=word::null) const
Get expected (characteristic) output file name - information only.
virtual void endTime()
End a time-step.
virtual bool enabled() const
void writeBufferContents(Ostream &os, const coordSet &coords, const char *sep) const
Write buffered data.
bool useTimeDir_
Insert additional time sub-directory in the output path.
coordSetWriter()
Default construct.
label nDataColumns() const
The number of buffer data columns, after splitting into components.
bool wroteGeom_
Track if geometry has been written since the last open.
bool useTracks() const noexcept
Prefer tracks to points during single set writing.
bool useTimeDir() const noexcept
Should a time directory be spliced into the output path?
bool buffering_
Writer with buffering output.
virtual bool buffering() const
True if the format uses internal buffering (eg, column output)
static UPtrList< const Field< Type > > repackageFields(const Field< Type > &field)
Repackage field into a UPtrList.
declareRunTimeSelectionTable(autoPtr, coordSetWriter, word,(),())
static autoPtr< coordSetWriter > New(const word &writeFormat)
Return a reference to the selected writer.
const word & timeName() const
The current time value/name.
void checkOpen() const
Verify that the outputPath_ has been set or FatalError.
void unsetTime()
Clear the current time.
virtual void beginTime(const Time &t)
Begin a time-step.
bool hasCoords() const
Writer is associated with content.
label nFields_
The number of fields.
static UPtrList< const Field< Type > > repackageFields(const UList< Field< Type > > &fieldValues)
Repackage multiple fields into a UPtrList.
coordSetWriter(const coordSetWriter &)=delete
No copy construct.
bool empty() const
Writer is not associated with content.
void operator=(const coordSetWriter &)=delete
No copy assignment.
bool upToDate_
The content is up-to-date?
virtual bool writeBuffered()
Write buffered data.
label numPoints() const
The number of associated points (local processor)
static bool supportedType(const word &writeType)
True if New is likely to succeed for this writeType.
bool is_open() const noexcept
Test if outputPath has been set.
void setTime(const instant &inst)
Set the current time.
bool useTracks_
Prefer tracks to points during single set writing.
virtual bool merge() const
static void writeTable(Ostream &os, const coordSet &coords, const UList< Type > &values, const char *sep)
Write coordinates and values.
bool verbose_
Additional output verbosity.
void clearBuffers()
Clear out buffering.
virtual bool expire()
declareRunTimeSelectionTable(autoPtr, coordSetWriter, wordDict,(const dictionary &writeOptions),(writeOptions))
UPtrList< const coordSet > coords_
Reference to coordinate set(s)
virtual bool needsUpdate() const
Does the writer need an update (eg, lagging behind other changes)
static word suffix(const word &fldName, const word &fileExt=word::null)
Name suffix based on fieldName (underscore separator)
instant currTime_
The current time value/name.
bool verbose() const noexcept
Get output verbosity.
List< scalarField > trackTimes_
Track times (eg, streamlines), one per coords_ entry.
virtual fileName path() const =0
TypeName("coordSetWriter")
Runtime type information.
virtual void clear()
virtual void setCoordinates(const coordSet *coords)
Set coordinates, can also be nullptr.
fileName getFieldPrefixedPath(const word &fieldName, const word &fileExt=word::null) const
Get field-prefixed output file name.
scalar timeValue() const
The current time value/name.
virtual bool wroteData() const
Geometry or fields written since the last open?
virtual void setTrackTimes(const UList< scalarField > &times)
Set track times.
virtual void setTracks(const UPtrList< coordSet > &tracks)
Set track coordinates.
virtual InfoProxy< coordSetWriter > info() const
Return info proxy.
bool hasTime() const
True if there is a known time.
static void writeLine(Ostream &, const UList< word > &, const char *sep)
Write line contents (eg, buffered)
fileName writeTemplate(const word &fieldName, const Field< Type > &values)
Dummy templated write operation.
fileName writeTemplate(const word &fieldName, const List< Field< Type > > &fieldValues)
Dummy templated write operation. Multiple tracks.
virtual void close(bool force=false)
Finish output, performing any necessary cleanup.
virtual ~coordSetWriter()
Destructor. Calls close()
fileName outputPath_
The full output directory and file (coords) name.
label numTracks() const
The number of coordinate tracks.
label nFields() const noexcept
The number of expected output fields.
Holds list of sampling positions.
Definition: coordSet.H:56
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
A class for handling file names.
Definition: fileName.H:76
static const fileName null
An empty fileName.
Definition: fileName.H:102
An instant of time. Contains the time value and name. Uses Foam::Time when formatting the name.
Definition: instant.H:56
A class for handling words, derived from Foam::string.
Definition: word.H:68
rDeltaTY field()
#define declareCoordSetWriterWriteMethod(Type)
#define defineBufferMethod(Type)
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
const direction noexcept
Definition: Scalar.H:223
Macros to ease declaration of run-time selection tables.
#define declareRunTimeSelectionTable(ptrWrapper, baseType, argNames, argList, parList)
Declare a run-time selection (variables and adder classes)
#define TypeName(TypeNameString)
Declare a ClassName() with extra virtual type info.
Definition: typeInfo.H:73