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-2024 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 "InfoProxy.H"
46 #include "HashSet.H"
47 #include "Map.H"
48 #include "Pstream.H"
49 #include "ensightGeoFile.H"
50 
51 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52 
53 namespace Foam
54 {
55 
56 // Forward Declarations
57 class bitSet;
58 class dictionary;
59 class ensightCase;
60 class instant;
61 class OSstream;
62 class Time;
63 
64 /*---------------------------------------------------------------------------*\
65  Class ensightCase Declaration
66 \*---------------------------------------------------------------------------*/
67 
69 {
70 public:
71 
72  // Forward Declarations
73  class options;
74 
75  // Public Data
76 
77  //- The name for data subdirectory: "data"
78  static const char* dataDirName;
79 
80  //- The name for geometry files: "geometry"
81  static const char* geometryName;
82 
83 
84 private:
85 
86  // Private Data
87 
88  //- Case writing options
89  const std::unique_ptr<options> options_;
90 
91  //- Output stream (master only)
92  mutable std::unique_ptr<OFstream> os_;
93 
94  //- Output path (absolute)
95  fileName ensightDir_;
96 
97  //- Case name (with ".case" ending)
98  word caseName_;
99 
100  //- Track state changes since last write
101  mutable bool changed_;
102 
103  //- Time index (timeset 1)
104  label timeIndex_;
105 
106  //- Time value (timeset 1)
107  scalar timeValue_;
108 
109  //- Record of time index/value used (eg, field values).
110  // These values will be used for timeset 1.
111  Map<scalar> timesUsed_;
112 
113  //- Record time indices when geometry is written.
114  // These values will be used to decide if timeset 1
115  // or a separate timeset are used.
116  // The special index '-1' is used for static geometry.
117  mutable labelHashSet geomTimes_;
118 
119  //- Record time indices when clouds are written.
120  // These values will be used to decide if timeset 1
121  // or a separate timeset are used.
122  mutable labelHashSet cloudTimes_;
123 
124  //- Fields/Variables with the ensight type
125  mutable HashTable<string> variables_;
126 
127  //- Remember fields that are to be treated as point data
128  mutable HashSet<string> nodeVariables_;
129 
130  //- Cloud names and variables
131  mutable HashTable<HashTable<string>> cloudVars_;
132 
133 
134  // Private Member Functions
135 
136  //- The data directory
137  fileName dataDir() const;
138 
139  //- Initial file management (master only)
140  void initialize();
141 
142  //- Check if timeset uses different times than from time-set 1
143  label checkTimeset(const labelHashSet& lookup) const;
144 
145  //- Write the header into the case file.
146  void writeHeader() const;
147 
148  //- Write the timeset 1 into the case file.
149  // Return the time correction in effect
150  scalar writeTimeset() const;
151 
152  //- Write the timeset into the case file.
153  void writeTimeset
154  (
155  const label ts,
156  const labelHashSet& lookup,
157  const scalar timeCorrection = 0
158  ) const;
159 
160 
161  //- Note the geometry being used
162  void noteGeometry(const bool moving) const;
163 
164  //- Note the cloud being used
165  void noteCloud(const word& cloudName) const;
166 
167  //- Note the cloud/variable being used
168  void noteCloud
169  (
170  const word& cloudName,
171  const word& varName,
172  const char* ensightType
173  ) const;
174 
175  //- Note the field variable being used
176  void noteVariable
177  (
178  const word& varName,
179  const char* ensightType
180  ) const;
181 
182 
183  //- Open stream for new data file (on master), using the current index.
184  // File is without initial description lines.
185  autoPtr<ensightFile> createDataFile(const word& name) const;
186 
187  //- Open stream for new cloud file (on master).
188  // File is without initial description lines.
189  autoPtr<ensightFile> createCloudFile
190  (
191  const word& cloudName,
192  const word& name
193  ) const;
194 
195 
196 public:
197 
198  // Generated Methods
199 
200  //- No copy construct
201  ensightCase(const ensightCase&) = delete;
202 
203  //- No copy assignment
204  void operator=(const ensightCase&) = delete;
205 
206 
207  // Constructors
208 
209  //- Construct from components
211  (
212  const fileName& ensightDir,
213  const word& caseName,
214  const options& opts
215  );
216 
217  //- Construct from components with all default options
219  (
220  const fileName& ensightDir,
221  const word& caseName,
223  );
224 
225 
226  //- Destructor
227  ~ensightCase() = default;
228 
229 
230  // Static Functions
231 
232  //- A '*' mask of specified width
233  static word mask(const int nwidth);
234 
235  //- Stringified zero-padded integer value of specified width
236  static word padded(const int nwidth, const label index);
237 
238  //- Replace the '*' mask chars with zero-padded integer value
239  template<class StringType>
240  static StringType expand_mask
241  (
242  const StringType& input,
243  const label index
244  );
245 
246 
247  // Member Functions
248 
249  // Access
250 
251  //- Reference to the case options
252  inline const ensightCase::options& option() const;
253 
254  //- The output file format (ascii/binary)
255  inline IOstreamOption::streamFormat format() const;
256 
257  //- The nominal path to the case file
258  inline const fileName& path() const noexcept;
259 
260  //- The output '*' mask
261  inline const word& mask() const;
262 
263  //- Consistent zero-padded integer value
264  inline word padded(const label i) const;
265 
266  //- Force use of values per node instead of per element
267  inline bool nodeValues() const;
268 
269  //- Write clouds into their own directory instead in "data" directory
270  inline bool separateCloud() const;
271 
272 
273  // Edit
274 
275  //- Set time for time-set 1, using next available index.
276  // Create corresponding sub-directory.
277  // Do not mix between nextTime and setTime in an application.
278  void nextTime(const scalar t);
279 
280  //- Set time for time-set 1, using next available index.
281  // Create corresponding sub-directory.
282  // Do not mix between nextTime and setTime in an application.
283  void nextTime(const instant& t);
284 
285  //- Set current index and time for time-set 1.
286  // Create corresponding sub-directory
287  // \note do not mix between nextTime and setTime in an application.
288  void setTime(const scalar t, const label index);
289 
290  //- Set current index and time for time-set 1.
291  // Create corresponding sub-directory
292  // \note do not mix between nextTime and setTime in an application.
293  void setTime(const instant& t, const label index);
294 
295 
296  // Addition of entries to case file
297 
298  //- Open stream for new geometry file (on master).
299  //- Does not include beginGeometry() marker.
300  autoPtr<ensightGeoFile> newGeometry(bool moving = false) const;
301 
302  //- Open stream for new cloud positions (on master).
303  // Note the use of ensightFile, not ensightGeoFile.
305  (
306  const word& cloudName
307  ) const;
308 
309  //- Open stream for new data file (on master), with current index.
310  // Optionally marking as containing POINT_DATA
311  template<class Type>
313  (
314  const word& varName,
315  const bool isPointData = false
316  ) const;
317 
318  //- Open stream for new data file (on master), with current index
319  //- and marking as containing POINT_DATA
320  template<class Type>
321  autoPtr<ensightFile> newPointData(const word& varName) const;
322 
323  //- Open stream for new cloud data file (on master), with current index.
324  template<class Type>
326  (
327  const word& cloudName,
328  const word& varName
329  ) const;
330 
331 
332  // Output
333 
334  //- Rewind the output stream (master only).
335  void rewind() const;
336 
337  //- Write the case file
338  void write() const;
339 
340  //- Output stream (master only).
341  inline Ostream& operator()() const;
342 
343  //- Print some general information.
344  Ostream& printInfo(Ostream& os) const;
345 
346 
347  // Output Helpers
348 
349  //- Set output time format for ensight case file
350  static void setTimeFormat
351  (
352  OSstream& os,
353  IOstreamOption::floatFormat timeFmt,
354  const int timePrec
355  );
356 
357  //- Set output time format for ensight case file
358  static void setTimeFormat
359  (
360  OSstream& os,
361  const ensightCase::options& opts
362  );
363 
364  //- Print time-set for ensight case file with a single time
365  static void printTimeset
366  (
367  OSstream& os,
368  const label ts,
369  const scalar timeValue
370  );
371 
372  //- Print time-set for ensight case file, with N times and 0-based
373  //- file numbering
374  //
375  // \verbatim
376  // TIME
377  // time set: ts
378  // number of steps: ns
379  // filename start number: 0
380  // filename increment: 1
381  // time values: time_1 time_2 ... time_ns
382  // \endverbatim
383  static void printTimeset
384  (
385  OSstream& os,
386  const label ts,
387  const UList<scalar>& times
388  );
389 
390  //- Print time-set for ensight case file, with N times, 0-based
391  //- file numbering but perhaps non-contiguous
392  //
393  // \verbatim
394  // TIME
395  // time set: ts
396  // number of steps: ns
397  // filename numbers: idx_1 idx_2 ... idx_ns
398  // time values: time_1 time_2 ... time_ns
399  // \endverbatim
400  static void printTimeset
401  (
402  OSstream& os,
403  const label ts,
404  const UList<scalar>& times,
405  const bitSet& indices
406  );
407 };
408 
409 
410 /*---------------------------------------------------------------------------*\
411  Class ensightCase::options Declaration
412 \*---------------------------------------------------------------------------*/
413 
414 //- Configuration options for the ensightCase
415 class ensightCase::options
416 {
417  // Private Data
418 
419  //- The output file format (ascii/binary)
421 
422  //- Remove existing directory and sub-directories on creation
423  bool overwrite_;
424 
425  //- Force use of values per node instead of per element
426  bool nodeValues_;
427 
428  //- Write clouds into their own directory
429  bool separateCloud_;
430 
431  //- Time format for case file (default: scientific)
432  IOstreamOption::floatFormat timeFormat_;
433 
434  //- Time precision for case file (default: 5)
435  int timePrecision_;
436 
437  //- Width of mask for subdirectories
438  int width_;
439 
440  //- The '*' mask appropriate for subdirectories
441  word mask_;
442 
443  //- The printf format for zero-padded subdirectory numbers
444  string printf_;
445 
446 
447 public:
448 
449  // Constructors
450 
451  //- Construct with the specified format (default is binary)
452  explicit options
453  (
455  );
456 
457  //- If present, construct with the format specified in the dictionary
458  //- or use default (binary)
459  options
460  (
463  const word& formatKeyword,
464  const dictionary& dict,
466  );
467 
468 
469  // Member Functions
470 
471  // Access
472 
473  //- The output file format (ascii/binary)
474  IOstreamOption::streamFormat format() const noexcept { return format_; }
475 
476  //- Time format for case file (general/fixed/scientific)
477  IOstreamOption::floatFormat timeFormat() const noexcept
478  {
479  return timeFormat_;
480  }
481 
482  //- Time precision for case file
483  int timePrecision() const noexcept { return timePrecision_; }
484 
485  //- The '*' mask appropriate for sub-directories
486  const word& mask() const noexcept { return mask_; }
487 
488  //- Consistent zero-padded integer value
489  word padded(const label i) const;
490 
491  //- Return current width of mask and padded.
492  int width() const noexcept { return width_; }
493 
494  //- Remove existing directory and sub-directories on creation
495  bool overwrite() const noexcept { return overwrite_; }
496 
497  //- Write clouds into their own directory instead in "data" directory
498  bool separateCloud() const noexcept { return separateCloud_; }
499 
500 
501  // Edit
502 
503  //- Set width of mask and padded.
504  // Default width is 8 digits, max width is 31 digits.
505  void width(const int i);
506 
507  //- Set the time format for case file
508  void timeFormat(IOstreamOption::floatFormat fmt) noexcept
509  {
510  timeFormat_ = fmt;
511  }
512 
513  //- Set the time precision for case file
514  void timePrecision(int prec) noexcept { timePrecision_ = prec; }
515 
516  //- Set the time format for case file
517  void timeFormat(const word& key, const dictionary& dict);
518 
519  //- Set the time precision for case file
520  void timePrecision(const word& key, const dictionary& dict);
521 
522  //- Remove existing directory and sub-directories on creation
523  void overwrite(bool on) noexcept { overwrite_ = on; }
524 
525  //- Write clouds into their own directory instead in "data" directory
526  void separateCloud(bool on) noexcept { separateCloud_ = on; }
527 
528 
529  // Housekeeping
530 
531  //- Forced use of values per node instead of per element
532  bool nodeValues() const noexcept { return nodeValues_; }
533 
534  //- Force use of values per node instead of per element
535  // Deprecated(2020-02) - The newData() method with a second parameter
536  // is more flexible.
537  // \deprecated(2020-02) - newData() with second parameter
538  void nodeValues(bool on) noexcept { nodeValues_ = on; }
539 };
540 
541 
542 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
543 
544 } // End namespace Foam
545 
546 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
547 
548 #include "ensightCaseI.H"
549 
550 #ifdef NoRepository
551  #include "ensightCaseTemplates.C"
552 #endif
553 
554 #endif
555 
556 // ************************************************************************* //
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:101
A variant of OFstream with specialised handling for Ensight writing of strings, integers and floats (...
Definition: ensightFile.H:47
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:50
dictionary dict
A class for handling file names.
Definition: fileName.H:72
static void setTimeFormat(OSstream &os, IOstreamOption::floatFormat timeFmt, const int timePrec)
Set output time format for ensight case file.
Definition: ensightCase.C:63
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.
Supports writing of ensight cases as well as providing common factory methods to open new files...
Definition: ensightCase.H:63
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
static StringType expand_mask(const StringType &input, const label index)
Replace the &#39;*&#39; mask chars with zero-padded integer value.
ensightCase(const ensightCase &)=delete
No copy construct.
floatFormat
Float formats (eg, time directory name formats)
A variant of ensightFile (Ensight writing) that includes the extra geometry file header information...
void nextTime(const scalar t)
Set time for time-set 1, using next available index.
Definition: ensightCase.C:594
void rewind() const
Rewind the output stream (master only).
Definition: ensightCase.C:893
A simple container for options an IOstream can normally have.
Lookup type of boundary radiation properties.
Definition: lookup.H:57
Configuration options for the ensightCase.
Definition: ensightCase.H:546
void operator=(const ensightCase &)=delete
No copy assignment.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
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:80
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:33
IOstreamOption::streamFormat format() const
The output file format (ascii/binary)
Definition: ensightCaseI.H:29
A HashTable similar to std::unordered_map.
Definition: HashTable.H:108
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:105
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:56
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). Does not include beginGeometry() marker.
Definition: ensightCase.C:832
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:638
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:59
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
~ensightCase()=default
Destructor.
static word padded(const int nwidth, const label index)
Stringified zero-padded integer value of specified width.
Definition: ensightCase.C:48
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:68
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:902
autoPtr< ensightFile > newCloud(const word &cloudName) const
Open stream for new cloud positions (on master).
Definition: ensightCase.C:868
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:607
Namespace for OpenFOAM.