ensightCase.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) 2016-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::ensightCase
28 
29 Description
30  Supports writing of ensight cases as well as providing common factory
31  methods to open new files.
32 
33 SourceFiles
34  ensightCase.C
35  ensightCaseI.H
36  ensightCaseOptions.C
37  ensightCaseTemplates.C
38 
39 \*---------------------------------------------------------------------------*/
40 
41 #ifndef Foam_ensightCase_H
42 #define Foam_ensightCase_H
43 
44 #include "autoPtr.H"
45 #include "HashSet.H"
46 #include "InfoProxy.H"
47 #include "Map.H"
48 #include "HashSet.H"
49 #include "Pstream.H"
50 #include "ensightGeoFile.H"
51 #include <memory>
52 
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 
55 namespace Foam
56 {
57 
58 // Forward Declarations
59 class bitSet;
60 class ensightCase;
61 class instant;
62 class OSstream;
63 class Time;
64 
65 /*---------------------------------------------------------------------------*\
66  Class ensightCase Declaration
67 \*---------------------------------------------------------------------------*/
68 
70 {
71 public:
72 
73  // Forward Declarations
74  class options;
75 
76  // Public Data
77 
78  //- The name for data subdirectory: "data"
79  static const char* dataDirName;
80 
81  //- The name for geometry files: "geometry"
82  static const char* geometryName;
83 
84 
85 private:
86 
87  // Private Data
88 
89  //- Case writing options
90  const std::unique_ptr<options> options_;
91 
92  //- Output stream (master only)
93  mutable std::unique_ptr<OFstream> os_;
94 
95  //- Output path (absolute)
96  fileName ensightDir_;
97 
98  //- Case name (with ".case" ending)
99  word caseName_;
100 
101  //- Track state changes since last write
102  mutable bool changed_;
103 
104  //- Time index (timeset 1)
105  label timeIndex_;
106 
107  //- Time value (timeset 1)
108  scalar timeValue_;
109 
110  //- Record of time index/value used (eg, field values).
111  // These values will be used for timeset 1.
112  Map<scalar> timesUsed_;
113 
114  //- Record time indices when geometry is written.
115  // These values will be used to decide if timeset 1
116  // or a separate timeset are used.
117  // The special index '-1' is used for static geometry.
118  mutable labelHashSet geomTimes_;
119 
120  //- Record time indices when clouds are written.
121  // These values will be used to decide if timeset 1
122  // or a separate timeset are used.
123  mutable labelHashSet cloudTimes_;
124 
125  //- Fields/Variables with the ensight type
126  mutable HashTable<string> variables_;
127 
128  //- Remember fields that are to be treated as point data
129  mutable HashSet<string> nodeVariables_;
130 
131  //- Cloud names and variables
132  mutable HashTable<HashTable<string>> cloudVars_;
133 
134 
135  // Private Member Functions
136 
137  //- The data directory
138  fileName dataDir() const;
139 
140  //- Initial file management (master only)
141  void initialize();
142 
143  //- Check if timeset uses different times than from time-set 1
144  label checkTimeset(const labelHashSet& lookup) const;
145 
146  //- Write the header into the case file.
147  void writeHeader() const;
148 
149  //- Write the timeset 1 into the case file.
150  // Return the time correction in effect
151  scalar writeTimeset() const;
152 
153  //- Write the timeset into the case file.
154  void writeTimeset
155  (
156  const label ts,
157  const labelHashSet& lookup,
158  const scalar timeCorrection = 0
159  ) const;
160 
161 
162  //- Note the geometry being used
163  void noteGeometry(const bool moving) const;
164 
165  //- Note the cloud being used
166  void noteCloud(const word& cloudName) const;
167 
168  //- Note the cloud/variable being used
169  void noteCloud
170  (
171  const word& cloudName,
172  const word& varName,
173  const char* ensightType
174  ) const;
175 
176  //- Note the field variable being used
177  void noteVariable
178  (
179  const word& varName,
180  const char* ensightType
181  ) const;
182 
183 
184  //- Open stream for new data file (on master), using the current index.
185  // File is without initial description lines.
186  autoPtr<ensightFile> createDataFile(const word& name) const;
187 
188  //- Open stream for new cloud file (on master).
189  // File is without initial description lines.
190  autoPtr<ensightFile> createCloudFile
191  (
192  const word& cloudName,
193  const word& name
194  ) const;
195 
196 
197  //- No copy construct
198  ensightCase(const ensightCase&) = delete;
199 
200  //- No copy assignment
201  void operator=(const ensightCase&) = delete;
202 
203 
204 public:
205 
206  // Constructors
207 
208  //- Construct from components
210  (
211  const fileName& ensightDir,
212  const word& caseName,
213  const options& opts
214  );
215 
216  //- Construct from components with all default options
218  (
219  const fileName& ensightDir,
220  const word& caseName,
222  );
223 
224 
225  //- Destructor
226  ~ensightCase() = default;
227 
228 
229  // Static Functions
230 
231  //- Stringified zero-padded integer value
232  static word padded(const int nwidth, const label value);
233 
234 
235  // Member Functions
236 
237  // Access
238 
239  //- Reference to the case options
240  inline const ensightCase::options& option() const;
241 
242  //- The output file format (ascii/binary)
243  inline IOstreamOption::streamFormat format() const;
244 
245  //- The nominal path to the case file
246  inline const fileName& path() const noexcept;
247 
248  //- The output '*' mask
249  inline const word& mask() const;
250 
251  //- Consistent zero-padded integer value
252  inline word padded(const label i) const;
253 
254  //- Force use of values per node instead of per element
255  inline bool nodeValues() const;
256 
257  //- Write clouds into their own directory instead in "data" directory
258  inline bool separateCloud() const;
259 
260 
261  // Edit
262 
263  //- Set time for time-set 1, using next available index.
264  // Create corresponding sub-directory.
265  // Do not mix between nextTime and setTime in an application.
266  void nextTime(const scalar t);
267 
268  //- Set time for time-set 1, using next available index.
269  // Create corresponding sub-directory.
270  // Do not mix between nextTime and setTime in an application.
271  void nextTime(const instant& t);
272 
273  //- Set current index and time for time-set 1.
274  // Create corresponding sub-directory
275  // \note do not mix between nextTime and setTime in an application.
276  void setTime(const scalar t, const label index);
277 
278  //- Set current index and time for time-set 1.
279  // Create corresponding sub-directory
280  // \note do not mix between nextTime and setTime in an application.
281  void setTime(const instant& t, const label index);
282 
283 
284  // Addition of entries to case file
285 
286  //- Open stream for new geometry file (on master).
287  autoPtr<ensightGeoFile> newGeometry(bool moving = false) const;
288 
289  //- Open stream for new cloud positions (on master).
290  // Note the use of ensightFile, not ensightGeoFile.
292  (
293  const word& cloudName
294  ) const;
295 
296  //- Open stream for new data file (on master), with current index.
297  // Optionally marking as containing POINT_DATA
298  template<class Type>
300  (
301  const word& varName,
302  const bool isPointData = false
303  ) const;
304 
305  //- Open stream for new data file (on master), with current index
306  //- and marking as containing POINT_DATA
307  template<class Type>
308  autoPtr<ensightFile> newPointData(const word& varName) const;
309 
310  //- Open stream for new cloud data file (on master), with current index.
311  template<class Type>
313  (
314  const word& cloudName,
315  const word& varName
316  ) const;
317 
318 
319  // Output
320 
321  //- Rewind the output stream (master only).
322  void rewind() const;
323 
324  //- Write the case file
325  void write() const;
326 
327  //- Output stream (master only).
328  inline Ostream& operator()() const;
329 
330  //- Print some general information.
331  Ostream& printInfo(Ostream& os) const;
332 
333 
334  // Output Helpers
335 
336  //- Print time-set for ensight case file with a single time
337  static void printTimeset
338  (
339  OSstream& os,
340  const label ts,
341  const scalar timeValue
342  );
343 
344  //- Print time-set for ensight case file, with N times and 0-based
345  //- file numbering
346  //
347  // \verbatim
348  // TIME
349  // time set: ts
350  // number of steps: ns
351  // filename start number: 0
352  // filename increment: 1
353  // time values: time_1 time_2 ... time_ns
354  // \endverbatim
355  static void printTimeset
356  (
357  OSstream& os,
358  const label ts,
359  const UList<scalar>& times
360  );
361 
362  //- Print time-set for ensight case file, with N times, 0-based
363  //- file numbering but perhaps non-contiguous
364  //
365  // \verbatim
366  // TIME
367  // time set: ts
368  // number of steps: ns
369  // filename numbers: idx_1 idx_2 ... idx_ns
370  // time values: time_1 time_2 ... time_ns
371  // \endverbatim
372  static void printTimeset
373  (
374  OSstream& os,
375  const label ts,
376  const UList<scalar>& times,
377  const bitSet& indices
378  );
379 };
380 
381 
382 /*---------------------------------------------------------------------------*\
383  Class ensightCase::options Declaration
384 \*---------------------------------------------------------------------------*/
385 
386 //- Configuration options for the ensightCase
387 class ensightCase::options
388 {
389  // Private Data
390 
391  //- Ascii/Binary file output
393 
394  //- Remove existing directory and sub-directories on creation
395  bool overwrite_;
396 
397  //- Force use of values per node instead of per element
398  bool nodeValues_;
399 
400  //- Write clouds into their own directory
401  bool separateCloud_;
402 
403  //- Width of mask for subdirectories
404  label width_;
405 
406  //- The '*' mask appropriate for subdirectories
407  word mask_;
408 
409  //- The printf format for zero-padded subdirectory numbers
410  string printf_;
411 
412 
413 public:
414 
415  // Constructors
416 
417  //- Construct with the specified format (default is binary)
419 
420 
421  // Member Functions
422 
423  // Access
424 
425  //- Ascii/Binary file output
426  IOstreamOption::streamFormat format() const noexcept { return format_; }
427 
428  //- The '*' mask appropriate for sub-directories
429  const word& mask() const noexcept { return mask_; }
430 
431  //- Consistent zero-padded integer value
432  word padded(const label i) const;
433 
434  //- Return current width of mask and padded.
435  label width() const noexcept { return width_; }
436 
437  //- Remove existing directory and sub-directories on creation
438  bool overwrite() const noexcept { return overwrite_; }
439 
440  //- Write clouds into their own directory instead in "data" directory
441  bool separateCloud() const noexcept { return separateCloud_; }
442 
443 
444  // Edit
445 
446  //- Set width of mask and padded.
447  // Default width is 8 digits, max width is 31 digits.
448  void width(const label i);
449 
450  //- Remove existing directory and sub-directories on creation
451  void overwrite(bool on) noexcept { overwrite_ = on; }
452 
453  //- Write clouds into their own directory instead in "data" directory
454  void separateCloud(bool on) noexcept { separateCloud_ = on; }
455 
456 
457  // Housekeeping
458 
459  //- Forced use of values per node instead of per element
460  bool nodeValues() const noexcept { return nodeValues_; }
461 
462  //- Force use of values per node instead of per element
463  // Deprecated(2020-02) - The newData() method with a second parameter
464  // is more flexible.
465  // \deprecated(2020-02) - newData() with second parameter
466  void nodeValues(bool on) noexcept { nodeValues_ = on; }
467 };
468 
469 
470 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
471 
472 } // End namespace Foam
473 
474 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
475 
476 #include "ensightCaseI.H"
477 
478 #ifdef NoRepository
479  #include "ensightCaseTemplates.C"
480 #endif
481 
482 #endif
483 
484 // ************************************************************************* //
static void printTimeset(OSstream &os, const label ts, const scalar timeValue)
Print time-set for ensight case file with a single time.
Definition: ensightCase.C:53
Ensight output with specialized write() for strings, integers and floats. Correctly handles binary wr...
Definition: ensightFile.H:46
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:50
A class for handling file names.
Definition: fileName.H:71
static void writeHeader(Ostream &os, const word &fieldName)
autoPtr< ensightFile > newCloudData(const word &cloudName, const word &varName) const
Open stream for new cloud data file (on master), with current index.
static word padded(const int nwidth, const label value)
Stringified zero-padded integer value.
Definition: ensightCase.C:38
Supports writing of ensight cases as well as providing common factory methods to open new files...
Definition: ensightCase.H:64
Specialized Ensight output with extra geometry file header.
void nextTime(const scalar t)
Set time for time-set 1, using next available index.
Definition: ensightCase.C:562
void rewind() const
Rewind the output stream (master only).
Definition: ensightCase.C:858
Lookup type of boundary radiation properties.
Definition: lookup.H:57
Configuration options for the ensightCase.
Definition: ensightCase.H:510
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
const word cloudName(propsDict.get< word >("cloud"))
autoPtr< ensightFile > newData(const word &varName, const bool isPointData=false) const
Open stream for new data file (on master), with current index.
A class for handling words, derived from Foam::string.
Definition: word.H:63
static const char * geometryName
The name for geometry files: "geometry".
Definition: ensightCase.H:81
IOstreamOption::streamFormat format() const
The output file format (ascii/binary)
Definition: ensightCaseI.H:29
A HashTable similar to std::unordered_map.
Definition: HashTable.H:102
const fileName & path() const noexcept
The nominal path to the case file.
Definition: ensightCaseI.H:35
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
bool separateCloud() const
Write clouds into their own directory instead in "data" directory.
Definition: ensightCaseI.H:59
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
const ensightCase::options & option() const
Reference to the case options.
Definition: ensightCaseI.H:23
autoPtr< ensightGeoFile > newGeometry(bool moving=false) const
Open stream for new geometry file (on master).
Definition: ensightCase.C:800
OBJstream os(runTime.globalPath()/outputName)
bool nodeValues() const
Force use of values per node instead of per element.
Definition: ensightCaseI.H:53
An instant of time. Contains the time value and name. Uses Foam::Time when formatting the name...
Definition: instant.H:53
void write() const
Write the case file.
Definition: ensightCase.C:606
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:59
~ensightCase()=default
Destructor.
streamFormat
Data format (ascii | binary)
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
static const char * dataDirName
The name for data subdirectory: "data".
Definition: ensightCase.H:69
const word & mask() const
The output &#39;*&#39; mask.
Definition: ensightCaseI.H:41
Ostream & printInfo(Ostream &os) const
Print some general information.
Definition: ensightCase.C:867
autoPtr< ensightFile > newCloud(const word &cloudName) const
Open stream for new cloud positions (on master).
Definition: ensightCase.C:834
autoPtr< ensightFile > newPointData(const word &varName) const
Open stream for new data file (on master), with current index and marking as containing POINT_DATA...
void setTime(const scalar t, const label index)
Set current index and time for time-set 1.
Definition: ensightCase.C:575
Namespace for OpenFOAM.