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"
35
36// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37
38namespace Foam
39{
40namespace coordSetWriters
41{
45}
46}
47
48
49// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50
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 IOstream::ASCII,
73 IOstream::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)
90:
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)
105{
106 open(tracks, outputPath);
107}
108
109
110// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
111
113{
114 close();
115}
116
117
118// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
119
121{
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
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:72
void append(const T &val)
Copy append an element to the end of this list.
Definition: DynamicListI.H:503
Generic templated field type.
Definition: Field.H:82
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:82
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
Output to file stream, using an OSstream.
Definition: OFstream.H:57
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:71
Base class for writing coordSet(s) and tracks with fields.
virtual void open(const fileName &outputPath)
Write separate geometry to file.
bool buffering_
Writer with buffering output.
virtual bool buffering() const
True if the format uses internal buffering (eg, column output)
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:56
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
A class for handling file names.
Definition: fileName.H:76
static const fileName null
An empty fileName.
Definition: fileName.H:102
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:176
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define defineTypeName(Type)
Define the typeName.
Definition: className.H:96
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
Convenience macros for instantiating coordSetWriter methods.
#define defineCoordSetWriterWriteFields(ThisClass)
OBJstream os(runTime.globalPath()/outputName)
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: MSwindows.C:515
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: MSwindows.C:651
#define doLocalCode(GeoField)