Loading...
Searching...
No Matches
csvCoordSetWriter.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 "csvCoordSetWriter.H"
29#include "coordSet.H"
30#include "fileName.H"
31#include "OFstream.H"
32#include "OSspecific.H"
36// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38namespace Foam
39{
40namespace coordSetWriters
41{
45}
46}
47
48
49// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51// Implementation
53
54
55// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
56
58:
60 streamOpt_(),
61 precision_(IOstream::defaultPrecision())
62{
63 buffering_ = true;
64}
65
66
68:
69 coordSetWriter(options),
70 streamOpt_
71 (
72 IOstreamOption::ASCII,
73 IOstreamOption::compressionEnum("compression", options)
74 ),
75 precision_
76 (
77 options.getOrDefault("precision", IOstream::defaultPrecision())
78 )
79{
80 buffering_ = options.getOrDefault("buffer", true);
81}
82
83
85(
86 const coordSet& coords,
87 const fileName& outputPath,
88 const dictionary& options
89)
91 csvWriter(options)
92{
93 open(coords, outputPath);
94}
95
96
98(
99 const UPtrList<coordSet>& tracks,
100 const fileName& outputPath,
101 const dictionary& options
102)
103:
104 csvWriter(options)
106 open(tracks, outputPath);
107}
108
109
110// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
111
114 close();
115}
116
117
118// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
119
122 const bool old(buffering_);
123 buffering_ = on;
124 return old;
125}
126
127
129{
130 // Assume !useTracks_, otherwise too fragile
131
132 // 1) rootdir/<TIME>/setName.csv
133 // 2) rootdir/setName.csv
134
135 return getExpectedPath("csv");
136}
137
138
140{
141 if (coords_.empty())
142 {
143 clearBuffers();
144 return false;
145 }
146 const auto& coords = coords_[0];
147
148
149 DynamicList<word> headCols(3 + nDataColumns());
150
151 do
152 {
153 if (coords.hasVectorAxis())
154 {
155 // x, y, z
156 headCols.append("x");
157 headCols.append("y");
158 headCols.append("z");
159 }
160 else
161 {
162 headCols.append(coords.axis());
163 }
164
165 // label, scalar
166 headCols.append(labelNames_);
167 headCols.append(scalarNames_);
168
169 // vector space
170 #undef doLocalCode
171 #define doLocalCode(Type) \
172 \
173 for (const word& fldName : Type##Names_) \
174 { \
175 for (direction d=0; d < pTraits<Type>::nComponents; ++d) \
176 { \
177 headCols.append(fldName + '_' + Foam::name(d)); \
178 } \
179 }
180
185 #undef doLocalCode
186 }
187 while (false);
188
189
190 // Field:
191 // 1) rootdir/<TIME>/setName.csv
192 // 2) rootdir/setName.csv
193
194 fileName outputFile = path();
195
196 if (!isDir(outputFile.path()))
197 {
198 mkDir(outputFile.path());
199 }
200
201 OFstream os(outputFile, streamOpt_);
202 os.precision(precision_);
203
204 writeLine(os, headCols, ",");
205
206 writeBufferContents(os, coords, ",");
207
208 clearBuffers();
209
210 return true;
211}
212
213
214// * * * * * * * * * * * * * * * Implementation * * * * * * * * * * * * * * * //
215
216template<class Type>
217Foam::fileName Foam::coordSetWriters::csvWriter::writeTemplate
218(
219 const word& fieldName,
220 const Field<Type>& values
221)
222{
223 checkOpen();
224 if (coords_.empty())
225 {
226 return fileName::null;
227 }
228
229 if (useTracks_ || !buffering_)
230 {
231 UPtrList<const Field<Type>> fieldPtrs(repackageFields(values));
232 return writeTemplate(fieldName, fieldPtrs);
233 }
234
235 // Buffering version
236 appendField(fieldName, values);
237 return path();
238}
239
240
241template<class Type>
242Foam::fileName Foam::coordSetWriters::csvWriter::writeTemplate
243(
244 const word& fieldName,
245 const List<Field<Type>>& fieldValues
246)
247{
248 checkOpen();
249 if (coords_.empty())
250 {
251 return fileName::null;
252 }
253 useTracks_ = true; // Extra safety
254
255 UPtrList<const Field<Type>> fieldPtrs(repackageFields(fieldValues));
256 return writeTemplate(fieldName, fieldPtrs);
257}
258
259
260// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
261
262// Field writing methods
263defineCoordSetWriterWriteFields(Foam::coordSetWriters::csvWriter);
264
265
266// ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
void append(const T &val)
Copy append an element to the end of this list.
Generic templated field type that is much like a Foam::List except that it is expected to hold numeri...
Definition Field.H:172
A simple container for options an IOstream can normally have.
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition IOstream.H:85
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
Output to file stream as an OSstream, normally using std::ofstream for the actual output.
Definition OFstream.H:75
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition UPtrList.H:101
Base class for writing coordSet(s) and tracks with fields.
void appendField(const word &fieldName, const Field< label > &vals)
virtual void open(const fileName &outputPath)
Write separate geometry to file.
fileName getExpectedPath(const word &fileExt=word::null) const
Get expected (characteristic) output file name - information only.
DynamicList< word > labelNames_
void writeBufferContents(Ostream &os, const coordSet &coords, const char *sep) const
Write buffered data.
label nDataColumns() const
The number of buffer data columns, after splitting into components.
bool buffering_
Writer with buffering output.
virtual bool buffering() const
True if the format uses internal buffering (eg, column output).
static UPtrList< const Field< Type > > repackageFields(const Field< Type > &field)
Repackage field into a UPtrList.
void checkOpen() const
Verify that the outputPath_ has been set or FatalError.
DynamicList< word > scalarNames_
coordSetWriter(const coordSetWriter &)=delete
No copy construct.
bool useTracks_
Prefer tracks to points during single set writing.
void clearBuffers()
Clear out buffering.
UPtrList< const coordSet > coords_
Reference to coordinate set(s).
static void writeLine(Ostream &, const UList< word > &, const char *sep)
Write line contents (eg, buffered).
virtual void close(bool force=false)
Finish output, performing any necessary cleanup.
Write coordSet(s) in csv format.
virtual ~csvWriter()
Destructor. Calls close().
virtual bool writeBuffered()
Write buffered data.
virtual fileName path() const
Characteristic output file name - information only.
Holds list of sampling positions.
Definition coordSet.H:52
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T, or return the given default value. FatalIOError if it is found and the number of...
A class for handling file names.
Definition fileName.H:75
static const fileName null
An empty fileName.
Definition fileName.H:111
static std::string path(const std::string &str)
Return directory path name (part before last /).
Definition fileNameI.H:169
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeName(Type)
Define the typeName.
Definition className.H:113
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
Convenience macros for instantiating coordSetWriter methods.
#define defineCoordSetWriterWriteFields(ThisClass)
OBJstream os(runTime.globalPath()/outputName)
#define doLocalCode(FieldType, Variable)
Namespace for OpenFOAM.
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition POSIX.C:616
Tensor< scalar > tensor
Definition symmTensor.H:57
Vector< scalar > vector
Definition vector.H:57
SphericalTensor< scalar > sphericalTensor
SphericalTensor of scalars, i.e. SphericalTensor<scalar>.
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition POSIX.C:862
SymmTensor< scalar > symmTensor
SymmTensor of scalars, i.e. SymmTensor<scalar>.
Definition symmTensor.H:55