coordSetWriter.C
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) 2022 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
26\*---------------------------------------------------------------------------*/
27
28#include "coordSet.H"
29#include "coordSetWriter.H"
30#include "Time.H"
31
32// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33
34namespace Foam
35{
39}
40
41
42// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
43
45(
46 const word& fldName,
47 const word& fileExt
48)
49{
50 word result;
51
52 if (!fldName.empty())
53 {
54 result += '_' + fldName;
55 }
56
57 return result.ext(fileExt);
58}
59
60
62(
63 const wordList& fieldNames,
64 const word& fileExt
65)
66{
67 word result;
68
69 for (const word& fldName : fieldNames)
70 {
71 if (!fldName.empty())
72 {
73 result += '_' + fldName;
74 }
75 }
76
77 return result.ext(fileExt);
78}
79
80
81// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
82
84:
85 coords_(),
86 trackTimes_(),
87 upToDate_(false),
88 wroteGeom_(false),
90 buffering_(false),
91 useTracks_(false),
92 useTimeDir_(false),
93 verbose_(false),
94 nFields_(0),
95 currTime_(),
96 outputPath_()
97{}
98
99
101:
103{
104 options.readIfPresent("verbose", verbose_);
105}
106
107
108// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
109
111{
112 close();
113}
114
115
116// * * * * * * * * * * * * * * * * * Controls * * * * * * * * * * * * * * * //
117
119{
120 currTime_ = inst;
121}
122
123
124void Foam::coordSetWriter::setTime(scalar timeValue)
125{
126 currTime_ = instant(timeValue);
127}
128
129
130void Foam::coordSetWriter::setTime(scalar timeValue, const word& timeName)
131{
132 currTime_.value() = timeValue;
133 currTime_.name() = timeName;
134}
135
136
138{
139 currTime_.value() = 0;
140 currTime_.name().clear();
141}
142
143
145{
146 setTime(t.value(), t.timeName());
147}
148
149
151{
152 setTime(inst);
153}
154
155
157{
158 // Flush bufferred data
159 if (nDataColumns())
160 {
161 writeBuffered();
162 }
163 clearBuffers();
164
165 unsetTime();
166}
167
168
169// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
170
172{
173 outputPath_ = outputPath;
174 wroteGeom_ = false;
175}
176
177
179(
180 const coordSet& coords,
181 const fileName& outputPath
182)
183{
184 close();
185 setCoordinates(coords);
186 open(outputPath);
187}
188
189
191(
192 const UPtrList<coordSet>& tracks,
193 const fileName& outputPath
194)
195{
196 close();
197 setTracks(tracks);
198 open(outputPath);
199}
200
201
203{
204 if (nDataColumns())
205 {
206 if (verbose_) Info<< "Flush buffered data:" << nl;
207 writeBuffered();
208 }
209 clearBuffers();
210
211 outputPath_.clear();
212 wroteGeom_ = false;
213
214 if (force)
215 {
216 coords_.clear();
217 trackTimes_.clear();
218 }
219}
220
221
223{
224 close();
225 expire();
226 coords_.clear();
227 trackTimes_.clear();
228 clearBuffers(); // Reset any buffering
229}
230
231
233{
234 expire();
235 clearBuffers(); // Reset any buffering
236
237 if (coords)
238 {
239 coords_.resize(1);
240 coords_.set(0, coords);
241 }
242 else
243 {
244 coords_.clear();
245 }
246 trackTimes_.clear();
247}
248
249
251{
252 setCoordinates(&coords);
253}
254
255
257{
258 expire();
259 clearBuffers(); // Reset any buffering
260
261 // Shallow copy (pointers)
262
263 coords_.resize(tracks.size());
264 forAll(coords_, tracki)
265 {
266 coords_.set(tracki, tracks.get(tracki));
267 }
268 trackTimes_.clear();
269 useTracks_ = true;
270}
271
272
274{
275 if (times.size() == coords_.size())
276 {
277 trackTimes_ = times;
278 }
279 else
280 {
281 trackTimes_.clear();
282 }
283}
284
285
287{
288 label nTotal = 0;
289
290 forAll(coords_, tracki)
291 {
292 const auto* ptr = coords_.get(tracki);
293 if (ptr) nTotal += ptr->size();
294 }
295
296 return nTotal;
297}
298
299
301{
302 return coords_.size();
303}
304
305
307{
308 return !upToDate_;
309}
310
311
313{
314 return wroteGeom_;
315}
316
317
319{
320 const bool changed = upToDate_;
321
322 upToDate_ = false;
323 wroteGeom_ = false;
324 coords_.clear();
325 trackTimes_.clear();
326
327 // Field count (nFields_) is a different type of accounting
328 // and is unaffected by geometry changes
329
330 return changed;
331}
332
333
335{
336 return !coords_.empty();
337}
338
339
341{
342 return coords_.empty();
343}
344
346(
347 const word& fileExt
348) const
349{
350 fileName file;
351
352 if (!outputPath_.empty())
353 {
354 if (useTimeDir() && !timeName().empty())
355 {
356 // Splice in time-directory
357 file = outputPath_.path() / timeName() / outputPath_.name();
358 }
359 else
360 {
361 file = outputPath_;
362 }
363
364 file.ext(fileExt); // Append extension - can also be empty
365 }
366
367 return file;
368}
369
370
372(
373 const word& fieldName,
374 const word& fileExt
375) const
376{
377 if (outputPath_.empty() || fieldName.empty())
378 {
379 return getExpectedPath(fileExt);
380 }
381
382 // Field: rootdir/<TIME>/<field>_NAME.ext
383
384 fileName file;
385 if (useTimeDir() && !timeName().empty())
386 {
387 // Splice in time-directory
388 file = outputPath_.path() / timeName();
389 }
390 else
391 {
392 file = outputPath_.path();
393 }
394
395 // Append <field>_NAME.EXT
396 file /= (fieldName + '_' + outputPath_.name());
397 file.ext(fileExt); // Append extension - can also be empty
398
399 return file;
400}
401
402
404{
405 if (!is_open())
406 {
408 << type() << " : Attempted to write without a path" << nl
409 << exit(FatalError);
410 }
411}
412
413
415{
416 bool changed = false;
417
418 // Possible future requirement...
419 //
420 // if (parallel_ && Pstream::parRun() && !upToDate_)
421 // {
422 // changed = merged_.merge(coords_, mergeDim_);
423 // }
424 upToDate_ = true;
425
426 if (changed)
427 {
428 wroteGeom_ = false;
429 }
430
431 return changed;
432}
433
434
435// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
436
437Foam::Ostream& Foam::operator<<
438(
439 Ostream& os,
441)
442{
443 const coordSetWriter& w = ip.t_;
444
445 os << "coordSetWriter:"
446 << " upToDate: " << w.upToDate_
447 << " nFields: " << w.nFields_
448 << " time: " << w.currTime_
449 << " path: " << w.outputPath_ << endl;
450
451 return os;
452}
453
454
455// ************************************************************************* //
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:52
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:80
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:780
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
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:71
label size() const noexcept
The number of elements in the list.
Definition: UPtrListI.H:106
T * get(const label i)
Definition: UPtrListI.H:127
Base class for writing coordSet(s) and tracks with fields.
fileName getExpectedPath(const word &fileExt=word::null) const
Get expected (characteristic) output file name - information only.
virtual void endTime()
End a time-step.
coordSetWriter()
Default construct.
void checkOpen() const
Verify that the outputPath_ has been set or FatalError.
void unsetTime()
Clear the current time.
virtual void beginTime(const Time &t)
Begin a time-step.
bool hasCoords() const
Writer is associated with content.
label nFields_
The number of fields.
bool empty() const
Writer is not associated with content.
bool upToDate_
The content is up-to-date?
label numPoints() const
The number of associated points (local processor)
void setTime(const instant &inst)
Set the current time.
virtual bool merge() const
bool verbose_
Additional output verbosity.
virtual bool expire()
virtual bool needsUpdate() const
Does the writer need an update (eg, lagging behind other changes)
static word suffix(const word &fldName, const word &fileExt=word::null)
Name suffix based on fieldName (underscore separator)
instant currTime_
The current time value/name.
virtual void clear()
virtual void setCoordinates(const coordSet *coords)
Set coordinates, can also be nullptr.
fileName getFieldPrefixedPath(const word &fieldName, const word &fileExt=word::null) const
Get field-prefixed output file name.
virtual bool wroteData() const
Geometry or fields written since the last open?
virtual void setTrackTimes(const UList< scalarField > &times)
Set track times.
virtual void setTracks(const UPtrList< coordSet > &tracks)
Set track coordinates.
virtual ~coordSetWriter()
Destructor. Calls close()
fileName outputPath_
The full output directory and file (coords) name.
label numTracks() const
The number of coordinate tracks.
Holds list of sampling positions.
Definition: coordSet.H:56
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
const Type & value() const
Return const reference to value.
A class for handling file names.
Definition: fileName.H:76
word ext() const
Return file name extension (part after last .)
Definition: fileNameI.H:218
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:176
An instant of time. Contains the time value and name. Uses Foam::Time when formatting the name.
Definition: instant.H:56
A class for handling words, derived from Foam::string.
Definition: word.H:68
word ext() const
Return file name extension (part after last .)
Definition: word.C:126
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
runTimeSource setTime(sourceTimes[sourceTimeIndex], sourceTimeIndex)
word timeName
Definition: getTimeIndex.H:3
Namespace for OpenFOAM.
messageStream Info
Information stream (stdout output on master, null elsewhere)
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:598
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
error FatalError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
#define defineRunTimeSelectionTable(baseType, argNames)
Define run-time selection table.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333
static constexpr char close
Definition: FlatOutput.H:74
static constexpr char open
Definition: FlatOutput.H:73