setWriter.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::setWriter
26 
27 Description
28  Base class for writing coordinate sets with data
29 
30  Example:
31  \verbatim
32  setWriter::New("vtk")->write
33  (
34  "myDirectory",
35  "mySet",
36  coordSet(true, "position", points),
37  "p", p,
38  "U", U
39  );
40  \endverbatim
41 
42 SourceFiles
43  setWriter.C
44 
45 \*---------------------------------------------------------------------------*/
46 
47 #ifndef setWriter_H
48 #define setWriter_H
49 
50 #include "runTimeSelectionTables.H"
51 #include "autoPtr.H"
52 #include "fieldTypes.H"
53 #include "coordSet.H"
54 
55 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56 
57 namespace Foam
58 {
59 
60 /*---------------------------------------------------------------------------*\
61  Class setWriter Declaration
62 \*---------------------------------------------------------------------------*/
63 
64 class setWriter
65 {
66 public:
67 
68  // Public Static Functions
69 
70  //- Helper for variadic write
71  template<class Types, class Type>
72  static inline void appendTypeValueSet
73  (
74  UPtrList<const Field<Types>>& valueSets,
75  const Field<Type>& valueSet
76  )
77  {
78  valueSets.resize(valueSets.size() + 1);
79  valueSets.set(valueSets.size() - 1, nullptr);
80  }
81 
82  //- Helper for variadic write
83  template<class Type>
84  static inline void appendTypeValueSet
85  (
86  UPtrList<const Field<Type>>& valueSets,
87  const Field<Type>& valueSet
88  )
89  {
90  valueSets.resize(valueSets.size() + 1);
91  valueSets.set(valueSets.size() - 1, &valueSet);
92  }
93 
94  //- Helper for variadic write
95  static inline void unpackTypeValueSets
96  (
97  wordList& valueSetNames
98  #define TypeValueSetsNonConstArg(Type, nullArg) \
99  , UPtrList<const Field<Type>>& Type##ValueSets
102  )
103  {}
104 
105  //- Helper for variadic write
106  template<class Type, class ... Args>
107  static inline void unpackTypeValueSets
108  (
109  wordList& valueSetNames
110  #define TypeValueSetsNonConstArg(Type, nullArg) \
111  , UPtrList<const Field<Type>>& Type##ValueSets
114  const word& valueSetName,
115  const Field<Type>& valueSet,
116  Args& ... args
117  )
118  {
119  valueSetNames.append(valueSetName);
120  #define AppendTypeValueSet(Type, nullArg) \
121  appendTypeValueSet(Type##ValueSets, valueSet);
123  #undef AppendTypeValueSet
124 
126  (
127  valueSetNames
128  #define TypeValueSetsParameter(Type, nullArg) \
129  , Type##ValueSets
132  args ...
133  );
134  }
135 
136  //- Helper for variadic write
137  template<class Type, class ... Args>
138  static inline void unpackTypeValueSets
139  (
140  wordList& valueSetNames
141  #define TypeValueSetsNonConstArg(Type, nullArg) \
142  , UPtrList<const Field<Type>>& Type##ValueSets
145  const wordList& valueSetNamesPart,
146  const UPtrList<const Field<Type>>& valueSetsPart,
147  Args& ... args
148  )
149  {
150  forAll(valueSetNamesPart, i)
151  {
152  valueSetNames.append(valueSetNamesPart[i]);
153  #define AppendTypeValueSet(Type, nullArg) \
154  appendTypeValueSet(Type##ValueSets, valueSetsPart[i]);
156  #undef AppendTypeValueSet
157  }
158 
160  (
161  valueSetNames
162  #define TypeValueSetsParameter(Type, nullArg) \
163  , Type##ValueSets
166  args ...
167  );
168  }
169 
170  //- Helper for variadic write
171  template<class Type, class ... Args>
172  static inline void unpackTypeValueSets
173  (
174  wordList& valueSetNames
175  #define TypeValueSetsNonConstArg(Type, nullArg) \
176  , UPtrList<const Field<Type>>& Type##ValueSets
179  const wordList& valueSetNamesPart,
180  const PtrList<Field<Type>>& valueSetsPart,
181  Args& ... args
182  )
183  {
185  (
186  valueSetNames
187  #define TypeValueSetsParameter(Type, nullArg) \
188  , Type##ValueSets
191  valueSetNamesPart,
192  reinterpret_cast<const UPtrList<const Field<Type>>&>
193  (valueSetsPart),
194  args ...
195  );
196  }
197 
198 
199 protected:
200 
201  // Protected Data
202 
203  //- Write format
205 
206  //- Write compression
208 
209  //- Possible delimiters that need quoting. Constructed on demand from
210  // the write.*Separator functions.
212 
213 
214  // Protected Member Functions
215 
216  //- Write a value separator
217  virtual void writeValueSeparator(Ostream& os) const;
218 
219  //- Write a coordinate separator
220  virtual void writeCoordSeparator(Ostream& os) const;
221 
222  //- Write a segment separator
223  virtual void writeSegmentSeparator(Ostream& os) const;
224 
225  //- Possible delimiters that need quoting
226  const List<string>& delimiters() const;
227 
228  //- Width of columns in tabulated output
229  static inline unsigned long columnWidth(Ostream& os)
230  {
231  return os.precision() + 7;
232  }
233 
234  //- Write a word
235  inline Ostream& writeWord
236  (
237  const word& w,
238  Ostream& os,
239  const bool align = false,
240  const unsigned long alignPad = 0
241  ) const;
242 
243  //- Write a value
244  template<class Type>
246  (
247  const Type& value,
248  Ostream& os,
249  const bool align = false
250  ) const;
251 
252  //- Write multi-column table header
253  void writeTableHeader
254  (
255  const coordSet& set,
256  const wordList& valueSetNames,
257  #define TypeValueSetsConstArg(Type, nullArg) \
258  const UPtrList<const Field<Type>>& Type##ValueSets ,
260  #undef TypeValueSetsConstArg
261  Ostream& os,
262  const bool align = false,
263  const unsigned long alignPad = 0
264  ) const;
265 
266  //- Write multi-column table of data
267  void writeTable
268  (
269  const coordSet& set,
270  #define TypeValueSetsConstArg(Type, nullArg) \
271  const UPtrList<const Field<Type>>& Type##ValueSets ,
273  #undef TypeValueSetsConstArg
274  Ostream& os,
275  const bool align = false
276  ) const;
277 
278 
279 public:
280 
281  //- Runtime type information
282  TypeName("setWriter");
283 
284 
285  // Declare run-time constructor selection table
286 
288  (
289  autoPtr,
290  setWriter,
291  word,
292  (
293  const IOstream::streamFormat writeFormat,
294  const IOstream::compressionType writeCompression
295  ),
296  (writeFormat, writeCompression)
297  );
298 
300  (
301  autoPtr,
302  setWriter,
303  dict,
304  (
305  const dictionary& dict
306  ),
307  (dict)
308  );
309 
310 
311  // Selectors
312 
313  //- Select given write options
314  static autoPtr<setWriter> New
315  (
316  const word& writeType,
317  const IOstream::streamFormat writeFormat =
319  const IOstream::compressionType writeCompression =
321  );
322 
323  //- Select given a dictionary
324  static autoPtr<setWriter> New
325  (
326  const word& writeType,
327  const dictionary& dict
328  );
329 
330 
331  // Constructors
332 
333  //- Construct given write options
334  setWriter
335  (
336  const IOstream::streamFormat writeFormat,
337  const IOstream::compressionType writeCompression
338  );
339 
340  //- Construct from dictionary
341  setWriter(const dictionary& dict);
342 
343  //- Construct copy
344  setWriter(const setWriter& writer);
345 
346  //- Construct and return a clone
347  virtual autoPtr<setWriter> clone() const = 0;
348 
349 
350  //- Destructor
351  virtual ~setWriter() = 0;
352 
353 
354  // Member Functions
355 
356  //- Write a coordSet and associated data
357  virtual void write
358  (
359  const fileName& outputDir,
360  const fileName& setName,
361  const coordSet& set,
362  const wordList& valueSetNames
363  #define TypeValueSetsConstArg(Type, nullArg) \
364  , const UPtrList<const Field<Type>>& Type##ValueSets
366  #undef TypeValueSetsConstArg
367  ) const = 0;
368 
369  //- Write a coordSet and associated data
370  virtual void write
371  (
372  const fileName& outputDir,
373  const fileName& setName,
374  const coordSet& set,
375  const wordList& valueSetNames
376  #define TypeValueSetsConstArg(Type, nullArg) \
377  , const PtrList<Field<Type>>& Type##ValueSets
379  #undef TypeValueSetsConstArg
380  ) const
381  {
382  write
383  (
384  outputDir,
385  setName,
386  set,
387  valueSetNames
388  #define TypeValueSetsParameter(Type, nullArg) \
389  , reinterpret_cast<const UPtrList<const Field<Type>>&> \
390  (Type##ValueSets)
393  );
394  }
395 
396  //- Write fields for a single surface to file. For use in code where
397  // the fields that are to be written are known. Takes any number of
398  // name, values arguments at the end. E.g.:
399  //
400  // write
401  // (
402  // // Output options
403  // "myDirectory", "mySet",
404  //
405  // // Geometry
406  // coordSet(true, "position", points),
407  //
408  // // Fields
409  // "p", Field<scalar>(points.size(), ...),
410  // "U", Field<vector>(points.size(), ...)
411  // );
412  //
413  template<class ... Args>
414  void inline write
415  (
416  const fileName& outputDir,
417  const fileName& setName,
418  const coordSet& set,
419  const Args& ... args
420  ) const
421  {
422  wordList valueSetNames;
423  #define DeclareTypeValueSets(Type, nullArg) \
424  UPtrList<const Field<Type>> Type##ValueSets;
426  #undef DeclareTypeValueSets
427 
429  (
430  valueSetNames
431  #define TypeValueSetsParameter(Type, nullArg) \
432  , Type##ValueSets
435  args ...
436  );
437 
438  write
439  (
440  outputDir,
441  setName,
442  set,
443  valueSetNames
444  #define TypeValueSetsParameter(Type, nullArg) \
445  , Type##ValueSets
448  );
449  }
450 };
451 
452 
453 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
454 
455 } // End namespace Foam
456 
457 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
458 
459 #ifdef NoRepository
460  #include "setWriterTemplates.C"
461 #endif
462 
463 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
464 
465 #endif
466 
467 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Pre-declare SubField and related Field type.
Definition: Field.H:83
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:87
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:194
void append(const T &)
Append an element at the end of the list.
Definition: ListI.H:178
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
virtual int precision() const =0
Get precision of output field.
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: PtrList.H:75
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: UPtrList.H:66
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
Holds list of sampling positions.
Definition: coordSet.H:51
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
A class for handling file names.
Definition: fileName.H:82
Base class for writing coordinate sets with data.
Definition: setWriter.H:64
virtual void write(const fileName &outputDir, const fileName &setName, const coordSet &set, const wordList &valueSetNames #define TypeValueSetsConstArg(Type, nullArg)) const =0
Write a coordSet and associated data.
Ostream & writeValue(const Type &value, Ostream &os, const bool align=false) const
Write a value.
void writeTable(const coordSet &set, #define TypeValueSetsConstArg(Type, nullArg) Ostream &os, const bool align=false) const
Write multi-column table of data.
Definition: setWriter.C:193
virtual autoPtr< setWriter > clone() const =0
Construct and return a clone.
static void appendTypeValueSet(UPtrList< const Field< Types >> &valueSets, const Field< Type > &valueSet)
Helper for variadic write.
Definition: setWriter.H:72
virtual ~setWriter()=0
Destructor.
Definition: setWriter.C:347
const List< string > & delimiters() const
Possible delimiters that need quoting.
Definition: setWriter.C:61
virtual void writeSegmentSeparator(Ostream &os) const
Write a segment separator.
Definition: setWriter.C:55
static unsigned long columnWidth(Ostream &os)
Width of columns in tabulated output.
Definition: setWriter.H:228
IOstream::streamFormat writeFormat_
Write format.
Definition: setWriter.H:203
IOstream::compressionType writeCompression_
Write compression.
Definition: setWriter.H:206
autoPtr< List< string > > delimiters_
Possible delimiters that need quoting. Constructed on demand from.
Definition: setWriter.H:210
void writeTableHeader(const coordSet &set, const wordList &valueSetNames, #define TypeValueSetsConstArg(Type, nullArg) Ostream &os, const bool align=false, const unsigned long alignPad=0) const
Write multi-column table header.
Definition: setWriter.C:119
TypeName("setWriter")
Runtime type information.
static autoPtr< setWriter > New(const word &writeType, const IOstream::streamFormat writeFormat=IOstream::ASCII, const IOstream::compressionType writeCompression=IOstream::UNCOMPRESSED)
Select given write options.
Definition: setWriter.C:286
virtual void writeCoordSeparator(Ostream &os) const
Write a coordinate separator.
Definition: setWriter.C:49
Ostream & writeWord(const word &w, Ostream &os, const bool align=false, const unsigned long alignPad=0) const
Write a word.
Definition: setWriter.C:83
static void unpackTypeValueSets(wordList &valueSetNames #define TypeValueSetsNonConstArg(Type, nullArg))
Helper for variadic write.
Definition: setWriter.H:95
virtual void writeValueSeparator(Ostream &os) const
Write a value separator.
Definition: setWriter.C:43
declareRunTimeSelectionTable(autoPtr, setWriter, word,(const IOstream::streamFormat writeFormat, const IOstream::compressionType writeCompression),(writeFormat, writeCompression))
setWriter(const IOstream::streamFormat writeFormat, const IOstream::compressionType writeCompression)
Construct given write options.
Definition: setWriter.C:249
A class for handling words, derived from string.
Definition: word.H:62
Include the header files for all the primitive types that Fields are instantiated for.
Namespace for OpenFOAM.
FOR_ALL_FIELD_TYPES(makeFieldSourceTypedef)
Macros to ease declaration of run-time selection tables.
dictionary dict
Foam::argList args(argc, argv)
#define AppendTypeValueSet(Type, nullArg)
#define DeclareTypeValueSets(Type, nullArg)
#define TypeValueSetsParameter(Type, nullArg)
#define TypeValueSetsConstArg(Type, nullArg)
#define TypeValueSetsNonConstArg(Type, nullArg)