Loading...
Searching...
No Matches
foamVtkSeriesWriter.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) 2018-2019 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::vtk::seriesWriter
28
29Description
30 Provides a means of accumulating and generating VTK file series.
31
32 The VTK file series format is a simple JSON format with the following
33 type of content:
34 \verbatim
35 {
36 "file-series-version" : "1.0",
37 "files": [
38 { "name" : "file1.vtk", "time" : 10 },
39 { "name" : "file2.vtk", "time" : 20 },
40 { "name" : "file3.vtk", "time" : 30 },
41 ]
42 }
43 \endverbatim
44
45 The append() operations include various sanity checks.
46 Entries with an empty name are ignored.
47 If an entry with an identical name already exists, its place
48 will be overwritten with the new time value.
49
50SourceFiles
51 foamVtkSeriesWriter.cxx
52 foamVtkSeriesWriterI.H
53
54\*---------------------------------------------------------------------------*/
55
56#ifndef Foam_vtk_seriesWriter_H
57#define Foam_vtk_seriesWriter_H
58
59#include <fstream>
60
62#include "instant.H"
63#include "fileNameInstant.H"
64#include "DynamicList.H"
65#include "HashSet.H"
66
67// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
68
69namespace Foam
70{
71namespace vtk
72{
74/*---------------------------------------------------------------------------*\
75 Class vtk::seriesWriter Declaration
76\*---------------------------------------------------------------------------*/
77
78class seriesWriter
79{
80 // Data Types
81
82 //- Select between "series" (default for vtk) and "json" endings
83 enum class endingType : unsigned char { SERIES, JSON };
84
85
86 // Private Member Data
87
88 //- A list of file/time entries
90
91 //- Hash of existing (known) file names
92 HashSet<fileName> existing_;
93
94 //- The preferred ending name
95 endingType extType_ = endingType::SERIES;
96
97
98 // Private Member Functions
99
100 //- Append the specified file/time instant.
101 // Overwrites existing entry that has the same name,
102 // does not append empty names.
103 bool appendCheck(fileNameInstant inst);
104
105 //- Remove duplicate filename entries. Keeping the last one seen.
106 bool removeDuplicates();
107
108
109public:
110
111 // Constructors
112
113 //- Construct an empty series (.series ending)
114 inline seriesWriter() noexcept;
115
116 //- Copy construct
117 seriesWriter(const seriesWriter&) = default;
118
119 //- Move construct
120 seriesWriter(seriesWriter&&) = default;
121
122 //- Copy assignment
123 seriesWriter& operator=(const seriesWriter&) = default;
124
125 //- Move assignment
126 seriesWriter& operator=(seriesWriter&&) = default;
127
128
129 //- Destructor
130 ~seriesWriter() = default;
131
132
133 // Static Member Functions
135 //- Extract the base name for a file series
136 //
137 // \param outputName The name of the data output file
138 // Eg, "somefile_0001.vtk" would extract to "somefile.vtk"
139 // \param sep The separator used between file stem and suffix.
140 static fileName base(const fileName& outputName, char sep = '_');
141
142 //- Extract the time-varying ending of files
143 //
144 // \param file The name of the file
145 // Eg, "somefile_0001.vtk" would extract to "0001"
146 // \param sep The separator used between file stem and suffix.
147 static word suffix(const fileName& file, char sep = '_');
148
149 //- Print file series (JSON format) for specified time instances
150 //
151 // \param os The output stream
152 // \param base The name for the series (eg, "path/file.vtk")
153 // \param series The list of suffix/value entries
154 // \param sep The separator used between file stem and suffix.
155 static Ostream& print
156 (
157 Ostream& os,
158 const fileName& seriesName,
159 const UList<instant>& series,
160 const char sep = '_'
161 );
162
163 //- Write file series (JSON format) to disk, for specified instances
164 //
165 // \param base The name for the series (eg, "path/file.vtk")
166 // \param series The list of suffix/value entries
167 // \param sep The separator used between file stem and suffix.
168 static void write
169 (
170 const fileName& base,
171 const UList<instant>& series,
172 const char sep = '_'
173 );
174
175 //- Print file series (JSON format) for specified time instances.
176 // Since the VTK file series does not currently (OCT-2018) support
177 // sub-directories, these will be stripped on output.
178 //
179 // \param os The output stream
180 // \param series The list of filename/value entries
181 static Ostream& print
182 (
183 Ostream& os,
185 );
186
187 //- Write file series (JSON format) to disk, for specified instances
188 //
189 // \param seriesName The name for the series (eg, "path/file.vtk")
190 // \param series The list of filename/value entries
191 static void write
192 (
193 const fileName& seriesName,
194 const UList<fileNameInstant>& series
195 );
196
197
198 // Member Functions
199
200 //- True if there are no data sets
201 inline bool empty() const noexcept;
202
203 //- The number of data sets
204 inline label size() const noexcept;
205
206 //- Preferred extension is "json" instead of "series"
207 inline bool json() const noexcept;
208
209 //- Toggle the preferred extension to "json" on/off.
210 //- Return the old json state.
211 inline bool json(bool on) noexcept;
212
213
214 // Content Management
215
216 //- Clear entries
217 inline void clear();
218
219 //- Append the specified file instant
220 inline bool append(const fileNameInstant& inst);
221
222 //- Append the specified file instant
223 inline bool append(fileNameInstant&& inst);
224
225 //- Append the specified file instant.
226 inline bool append(scalar timeValue, const fileName& file);
227
228 //- Append the specified file instant.
229 inline bool append(scalar timeValue, fileName&& file);
230
231 //- Clear contents and reload by parsing the specified file.
232 //
233 // \return the number of entries
234 label load
235 (
237 const fileName& seriesName,
239 const bool checkFiles = false,
242 const scalar restartTime = ROOTVGREAT
243 );
244
245 //- Clear contents and scan directory for files.
246 //
247 // The expected xml header content is a comment with the following:
248 // \verbatim
249 // <!-- ... time='3.14159' ... -->
250 // \endverbatim
251 //
252 // \return the number of entries
253 label scan
254 (
256 const fileName& seriesName,
259 const scalar restartTime = ROOTVGREAT
260 );
261
262 //- Remove entries that are greater_equal the time value.
263 //
264 // \return True if the contents changed
265 bool removeNewer(const scalar timeValue);
266
267 //- Sort by time value and by file name
268 void sort();
269
270
271 // Writing
272
273 //- Print file series as (JSON format)
274 inline void print(Ostream& os) const;
275
276 //- Write file series as (JSON format) to disk
277 void write(const fileName& seriesName) const;
278};
279
280
281// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
282
283} // End namespace vtk
284} // End namespace Foam
285
286// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
287
288#include "foamVtkSeriesWriterI.H"
289
290
291// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
292
293#endif
294
295// ************************************************************************* //
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
A HashTable with keys but without contents that is similar to std::unordered_set.
Definition HashSet.H:96
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
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:89
A class for handling file names.
Definition fileName.H:75
static fileName base(const fileName &outputName, char sep='_')
Extract the base name for a file series.
seriesWriter(seriesWriter &&)=default
Move construct.
static Ostream & print(Ostream &os, const UList< fileNameInstant > &series)
Print file series (JSON format) for specified time instances.
static word suffix(const fileName &file, char sep='_')
Extract the time-varying ending of files.
seriesWriter & operator=(seriesWriter &&)=default
Move assignment.
bool append(const fileNameInstant &inst)
Append the specified file instant.
bool empty() const noexcept
True if there are no data sets.
label scan(const fileName &seriesName, const scalar restartTime=ROOTVGREAT)
Clear contents and scan directory for files.
void sort()
Sort by time value and by file name.
label load(const fileName &seriesName, const bool checkFiles=false, const scalar restartTime=ROOTVGREAT)
Clear contents and reload by parsing the specified file.
bool removeNewer(const scalar timeValue)
Remove entries that are greater_equal the time value.
seriesWriter & operator=(const seriesWriter &)=default
Copy assignment.
bool json() const noexcept
Preferred extension is "json" instead of "series".
static void write(const fileName &base, const UList< instant > &series, const char sep='_')
Write file series (JSON format) to disk, for specified instances.
~seriesWriter()=default
Destructor.
label size() const noexcept
The number of data sets.
static void write(const fileName &seriesName, const UList< fileNameInstant > &series)
Write file series (JSON format) to disk, for specified instances.
void clear()
Clear entries.
seriesWriter(const seriesWriter &)=default
Copy construct.
seriesWriter() noexcept
Construct an empty series (.series ending).
static Ostream & print(Ostream &os, const fileName &seriesName, const UList< instant > &series, const char sep='_')
Print file series (JSON format) for specified time instances.
A class for handling words, derived from Foam::string.
Definition word.H:66
word outputName("finiteArea-edges.obj")
OBJstream os(runTime.globalPath()/outputName)
Namespace for handling VTK output. Contains classes and functions for writing VTK file content.
Namespace for OpenFOAM.
Instant< fileName > fileNameInstant
A tuple of value and fileName.
const direction noexcept
Definition scalarImpl.H:265
runTime write()