All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2022 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(points, true),
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
100  FOR_ALL_FIELD_TYPES(TypeValueSetsNonConstArg)
101  #undef TypeValueSetsNonConstArg
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
112  FOR_ALL_FIELD_TYPES(TypeValueSetsNonConstArg),
113  #undef TypeValueSetsNonConstArg
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
143  FOR_ALL_FIELD_TYPES(TypeValueSetsNonConstArg),
144  #undef TypeValueSetsNonConstArg
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
177  FOR_ALL_FIELD_TYPES(TypeValueSetsNonConstArg),
178  #undef TypeValueSetsNonConstArg
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 ,
259  FOR_ALL_FIELD_TYPES(TypeValueSetsConstArg)
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 ,
272  FOR_ALL_FIELD_TYPES(TypeValueSetsConstArg)
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 
344  //- Destructor
345  virtual ~setWriter() = 0;
346 
347 
348  // Member Functions
349 
350  //- Write a coordSet and associated data
351  virtual void write
352  (
353  const fileName& outputDir,
354  const fileName& setName,
355  const coordSet& set,
356  const wordList& valueSetNames
357  #define TypeValueSetsConstArg(Type, nullArg) \
358  , const UPtrList<const Field<Type>>& Type##ValueSets
359  FOR_ALL_FIELD_TYPES(TypeValueSetsConstArg)
360  #undef TypeValueSetsConstArg
361  ) const = 0;
362 
363  //- Write a coordSet and associated data
364  virtual void write
365  (
366  const fileName& outputDir,
367  const fileName& setName,
368  const coordSet& set,
369  const wordList& valueSetNames
370  #define TypeValueSetsConstArg(Type, nullArg) \
371  , const PtrList<Field<Type>>& Type##ValueSets
372  FOR_ALL_FIELD_TYPES(TypeValueSetsConstArg)
373  #undef TypeValueSetsConstArg
374  ) const
375  {
376  write
377  (
378  outputDir,
379  setName,
380  set,
381  valueSetNames
382  #define TypeValueSetsParameter(Type, nullArg) \
383  , reinterpret_cast<const UPtrList<const Field<Type>>&> \
384  (Type##ValueSets)
387  );
388  }
389 
390  //- Write fields for a single surface to file. For use in code where
391  // the fields that are to be written are known. Takes any number of
392  // name, values arguments at the end. E.g.:
393  //
394  // write
395  // (
396  // // Output options
397  // "myDirectory", "mySet",
398  //
399  // // Geometry
400  // coordSet(points, true),
401  //
402  // // Fields
403  // "p", Field<scalar>(points.size(), ...),
404  // "U", Field<vector>(points.size(), ...)
405  // );
406  //
407  template<class ... Args>
408  void inline write
409  (
410  const fileName& outputDir,
411  const fileName& setName,
412  const coordSet& set,
413  const Args& ... args
414  ) const
415  {
416  wordList valueSetNames;
417  #define DeclareTypeValueSets(Type, nullArg) \
418  UPtrList<const Field<Type>> Type##ValueSets;
420  #undef DeclareTypeValueSets
421 
423  (
424  valueSetNames
425  #define TypeValueSetsParameter(Type, nullArg) \
426  , Type##ValueSets
429  args ...
430  );
431 
432  write
433  (
434  outputDir,
435  setName,
436  set,
437  valueSetNames
438  #define TypeValueSetsParameter(Type, nullArg) \
439  , Type##ValueSets
442  );
443  }
444 };
445 
446 
447 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
448 
449 } // End namespace Foam
450 
451 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
452 
453 #ifdef NoRepository
454  #include "setWriterTemplates.C"
455 #endif
456 
457 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
458 
459 #endif
460 
461 // ************************************************************************* //
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:279
autoPtr< List< string > > delimiters_
Possible delimiters that need quoting. Constructed on demand from.
Definition: setWriter.H:210
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
A class for handling file names.
Definition: fileName.H:79
Ostream & writeWord(const word &w, Ostream &os, const bool align=false, const unsigned long alignPad=0) const
Write a word.
Definition: setWriter.C:83
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.
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
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
IOstream::compressionType writeCompression_
Write compression.
Definition: setWriter.H:206
Base class for writing coordinate sets with data.
Definition: setWriter.H:63
declareRunTimeSelectionTable(autoPtr, setWriter, word,(const IOstream::streamFormat writeFormat, const IOstream::compressionType writeCompression),(writeFormat, writeCompression))
Ostream & writeValue(const Type &value, Ostream &os, const bool align=false) const
Write a value.
const List< string > & delimiters() const
Possible delimiters that need quoting.
Definition: setWriter.C:61
#define TypeValueSetsConstArg(Type, nullArg)
virtual void writeValueSeparator(Ostream &os) const
Write a value separator.
Definition: setWriter.C:43
virtual ~setWriter()=0
Destructor.
Definition: setWriter.C:340
static void unpackTypeValueSets(wordList &valueSetNames #define TypeValueSetsNonConstArg(Type, nullArg))
Helper for variadic write.
Definition: setWriter.H:95
#define TypeValueSetsParameter(Type, nullArg)
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
Holds list of sampling positions.
Definition: coordSet.H:50
Include the header files for all the primitive types that Fields are instantiated for...
Pre-declare SubField and related Field type.
Definition: Field.H:56
A class for handling words, derived from string.
Definition: word.H:59
virtual void writeSegmentSeparator(Ostream &os) const
Write a segment separator.
Definition: setWriter.C:55
void append(const T &)
Append an element at the end of the list.
Definition: ListI.H:178
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
static void appendTypeValueSet(UPtrList< const Field< Types >> &valueSets, const Field< Type > &valueSet)
Helper for variadic write.
Definition: setWriter.H:72
static unsigned long columnWidth(Ostream &os)
Width of columns in tabulated output.
Definition: setWriter.H:228
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: UPtrList.H:54
virtual void writeCoordSeparator(Ostream &os) const
Write a coordinate separator.
Definition: setWriter.C:49
setWriter(const IOstream::streamFormat writeFormat, const IOstream::compressionType writeCompression)
Construct given write options.
Definition: setWriter.C:249
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
TypeName("setWriter")
Runtime type information.
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:70
IOstream::streamFormat writeFormat_
Write format.
Definition: setWriter.H:203
FOR_ALL_FIELD_TYPES(DefineFvWallInfoType)
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
Macros to ease declaration of run-time selection tables.
#define AppendTypeValueSet(Type, nullArg)
#define TypeValueSetsNonConstArg(Type, nullArg)
#define DeclareTypeValueSets(Type, nullArg)
Namespace for OpenFOAM.
virtual int precision() const =0
Get precision of output field.