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 -------------------------------------------------------------------------------
10 License
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"
33 #include "coordSetWriterMethods.H"
35 
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 
38 namespace Foam
39 {
40 namespace coordSetWriters
41 {
42  defineTypeName(csvWriter);
43  addToRunTimeSelectionTable(coordSetWriter, csvWriter, word);
44  addToRunTimeSelectionTable(coordSetWriter, csvWriter, wordDict);
45 }
46 }
47 
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 // Implementation
52 #include "csvCoordSetWriterImpl.C"
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 )
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 
216 template<class Type>
217 Foam::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 
241 template<class Type>
242 Foam::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 // ************************************************************************* //
virtual bool writeBuffered()
Write buffered data.
defineCoordSetWriterWriteFields(Foam::coordSetWriters::csvWriter)
addToRunTimeSelectionTable(coordSetWriter, abaqusWriter, word)
A class for handling file names.
Definition: fileName.H:72
const word & axis() const
The sort axis name.
Definition: coordSet.H:160
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
static const fileName null
An empty fileName.
Definition: fileName.H:111
Tensor< scalar > tensor
Definition: symmTensor.H:57
virtual bool buffering() const
True if the format uses internal buffering (eg, column output)
defineTypeName(abaqusWriter)
A simple container for options an IOstream can normally have.
Macros for easy insertion into run-time selection tables.
bool hasVectorAxis() const noexcept
True if axis specification is a vector.
Definition: coordSet.C:128
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:860
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Convenience macros for instantiating coordSetWriter methods.
#define doLocalCode(Type)
Holds list of sampling positions.
Definition: coordSet.H:49
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:614
Generic templated field type.
Definition: Field.H:62
SymmTensor< scalar > symmTensor
SymmTensor of scalars, i.e. SymmTensor<scalar>.
Definition: symmTensor.H:55
A class for handling words, derived from Foam::string.
Definition: word.H:63
Base class for writing coordSet(s) and tracks with fields.
bool buffering_
Writer with buffering output.
Vector< scalar > vector
Definition: vector.H:57
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: HashTable.H:106
OBJstream os(runTime.globalPath()/outputName)
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
virtual ~csvWriter()
Destructor. Calls close()
An IOstream is an abstract base class for all input/output systems; be they streams, files, token lists etc.
Definition: IOstream.H:82
virtual fileName path() const
Characteristic output file name - information only.
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...
SphericalTensor< scalar > sphericalTensor
SphericalTensor of scalars, i.e. SphericalTensor<scalar>.
Write coordSet(s) in csv format.
Namespace for OpenFOAM.