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.C
52
53\*---------------------------------------------------------------------------*/
54
55#ifndef Foam_vtk_seriesWriter_H
56#define Foam_vtk_seriesWriter_H
57
58#include <fstream>
60#include "instant.H"
61#include "fileNameInstant.H"
62#include "DynamicList.H"
63#include "HashSet.H"
64
65// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
66
67namespace Foam
68{
69namespace vtk
70{
71
72/*---------------------------------------------------------------------------*\
73 Class vtk::seriesWriter Declaration
74\*---------------------------------------------------------------------------*/
76class seriesWriter
77{
78 // Private Member Data
79
80 //- A list of file/time entries
82
83 //- Hash of existing (known) file names
84 HashSet<fileName> existing_;
85
86 //- Append the specified file/time instant.
87 // Overwrites existing entry that has the same name,
88 // does not append empty names.
89 bool appendCheck(fileNameInstant inst);
90
91 //- Remove duplicate filename entries. Keeping the last one seen.
92 bool removeDuplicates();
93
94
95public:
96
97 // Constructors
98
99 //- Construct an empty series
100 seriesWriter() = default;
101
102 //- Copy construct
103 seriesWriter(const seriesWriter&) = default;
104
105 //- Move construct
106 seriesWriter(seriesWriter&&) = default;
107
108 //- Copy assignment
109 seriesWriter& operator=(const seriesWriter&) = default;
110
111 //- Move assignment
112 seriesWriter& operator=(seriesWriter&&) = default;
113
114
115 //- Destructor
116 ~seriesWriter() = default;
117
118
119 // Static Member Functions
120
121 //- Extract the base name for a file series
122 //
123 // \param outputName The name of the data output file
124 // Eg, "somefile_0001.vtk" would extract to "somefile.vtk"
125 // \param sep The separator used between file stem and suffix.
126 static fileName base(const fileName& outputName, char sep = '_');
127
128 //- Extract the time-varying ending of files
129 //
130 // \param file The name of the file
131 // Eg, "somefile_0001.vtk" would extract to "0001"
132 // \param sep The separator used between file stem and suffix.
133 static word suffix(const fileName& file, char sep = '_');
134
135 //- Print file series (JSON format) for specified time instances
136 //
137 // \param os The output stream
138 // \param base The name for the series (eg, "path/file.vtk")
139 // \param series The list of suffix/value entries
140 // \param sep The separator used between file stem and suffix.
141 static Ostream& print
142 (
143 Ostream& os,
144 const fileName& seriesName,
145 const UList<instant>& series,
146 const char sep = '_'
147 );
148
149 //- Write file series (JSON format) to disk, for specified instances
150 //
151 // \param base The name for the series (eg, "path/file.vtk")
152 // \param series The list of suffix/value entries
153 // \param sep The separator used between file stem and suffix.
154 static void write
155 (
156 const fileName& base,
157 const UList<instant>& series,
158 const char sep = '_'
159 );
160
161 //- Print file series (JSON format) for specified time instances.
162 // Since the VTK file series does not currently (OCT-2018) support
163 // sub-directories, these will be stripped on output.
164 //
165 // \param os The output stream
166 // \param series The list of filename/value entries
167 static Ostream& print
168 (
169 Ostream& os,
170 const UList<fileNameInstant>& series
171 );
172
173 //- Write file series (JSON format) to disk, for specified instances
174 //
175 // \param seriesName The name for the series (eg, "path/file.vtk")
176 // \param series The list of filename/value entries
177 static void write
178 (
179 const fileName& seriesName,
180 const UList<fileNameInstant>& series
181 );
182
183
184 // Member Functions
185
186 //- True if there are no data sets
187 inline bool empty() const noexcept;
188
189 //- The number of data sets
190 inline label size() const noexcept;
191
192
193 // Content Management
194
195 //- Clear entries
196 inline void clear();
197
198 //- Append the specified file instant
199 inline bool append(const fileNameInstant& inst);
200
201 //- Append the specified file instant
202 inline bool append(fileNameInstant&& inst);
203
204 //- Append the specified file instant.
205 inline bool append(scalar timeValue, const fileName& file);
206
207 //- Append the specified file instant.
208 inline bool append(scalar timeValue, fileName&& file);
209
210 //- Clear contents and reload by parsing the specified file.
211 //
212 // \param seriesName the base name of the series to scan, without
213 // the ".series" ending.
214 // \param checkFiles verify that the files also exist
215 // \param restartTime ignore entries with a time greater/equal
216 // to the specified restart time.
217 //
218 // \return the number of entries
219 label load
220 (
221 const fileName& seriesName,
222 const bool checkFiles = false,
223 const scalar restartTime = ROOTVGREAT
224 );
225
226 //- Clear contents and scan directory for files.
227 //
228 // The expected xml header content is a comment with the following:
229 // \verbatim
230 // <!-- ... time='3.14159' ... -->
231 // \endverbatim
232 //
233 // \param seriesName the base name of the series to scan, without
234 // the ".series" ending.
235 // \param restartTime ignore entries with a time greater/equal
236 // to the specified restart time.
237 //
238 // \return the number of entries
239 label scan
240 (
241 const fileName& seriesName,
242 const scalar restartTime = ROOTVGREAT
243 );
244
245 //- Remove entries that are greater_equal the time value.
246 //
247 // \return True if the contents changed
248 bool removeNewer(const scalar timeValue);
249
250 //- Sort by time value and by file name
251 void sort();
252
253
254 // Writing
255
256 //- Print file series as (JSON format)
257 inline void print(Ostream& os) const;
258
259 //- Write file series as (JSON format) to disk
260 inline void write(const fileName& seriesName) const;
261};
262
263
264// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
265
266} // End namespace vtk
267} // End namespace Foam
268
269// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
270
271#include "foamVtkSeriesWriterI.H"
272
273
274// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
275
276#endif
277
278// ************************************************************************* //
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:72
A HashTable with keys but without contents that is similar to std::unordered_set.
Definition: HashSet.H:96
A tuple of scalar value and key. The value often corresponds to a time value, thus the naming of the ...
Definition: Instant.H:52
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
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:94
A class for handling file names.
Definition: fileName.H:76
Provides a means of accumulating and generating VTK file series.
seriesWriter()=default
Construct an empty series.
seriesWriter(seriesWriter &&)=default
Move construct.
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.
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.
static fileName base(const fileName &outputName, char sep='_')
Extract the base name for a file series.
~seriesWriter()=default
Destructor.
label size() const noexcept
The number of data sets.
label scan(const fileName &seriesName, const scalar restartTime=ROOTVGREAT)
Clear contents and scan directory for files.
void clear()
Clear entries.
seriesWriter(const seriesWriter &)=default
Copy construct.
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:68
word outputName("finiteArea-edges.obj")
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
const direction noexcept
Definition: Scalar.H:223
runTime write()