csvSetWriter.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "csvSetWriter.H"
27 #include "coordSet.H"
28 #include "fileName.H"
29 #include "OFstream.H"
30 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
33 template<class Type>
35 :
36  writer<Type>()
37 {}
38 
39 
40 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
41 
42 template<class Type>
44 {}
45 
46 
47 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
48 
49 template<class Type>
51 (
52  const coordSet& points,
53  const wordList& valueSetNames
54 ) const
55 {
56  return this->getBaseName(points, valueSetNames) + ".csv";
57 }
58 
59 
60 template<class Type>
62 (
63  const coordSet& points,
64  const wordList& valueSetNames,
65  const List<const Field<Type>*>& valueSets,
66  Ostream& os
67 ) const
68 {
69  writeHeader(points,valueSetNames,os);
70 
71  // Collect sets into columns
72  List<const List<Type>*> columns(valueSets.size());
73 
74  forAll(valueSets, i)
75  {
76  columns[i] = valueSets[i];
77  }
78 
79  this->writeTable(points, columns, os);
80 }
81 
82 
83 template<class Type>
85 (
86  const bool writeTracks,
87  const PtrList<coordSet>& points,
88  const wordList& valueSetNames,
89  const List<List<Field<Type>>>& valueSets,
90  Ostream& os
91 ) const
92 {
93  writeHeader(points[0],valueSetNames,os);
94 
95  if (valueSets.size() != valueSetNames.size())
96  {
98  << "Number of variables:" << valueSetNames.size() << endl
99  << "Number of valueSets:" << valueSets.size()
100  << exit(FatalError);
101  }
102 
103  List<const List<Type>*> columns(valueSets.size());
104 
105  forAll(points, trackI)
106  {
107  // Collect sets into columns
108  forAll(valueSets, i)
109  {
110  columns[i] = &valueSets[i][trackI];
111  }
112 
113  this->writeTable(points[trackI], columns, os);
114  os << nl << nl;
115  }
116 }
117 
118 
119 template<class Type>
121 {
122  os << token::COMMA;
123 }
124 
125 
126 namespace Foam
127 {
128  // otherwise compiler complains about specialization
129  template<>
131  (
132  const coordSet& points,
133  const wordList& valueSetNames,
134  Ostream& os
135  ) const
136  {
137  writeCoordHeader(points, os);
138 
139  forAll(valueSetNames, i)
140  {
141  if (i > 0)
142  {
143  writeSeparator(os);
144  }
145  os << valueSetNames[i];
146  }
147 
148  os << nl;
149  }
150 }
151 
152 
153 template<class Type>
155 (
156  const coordSet& points,
157  const wordList& valueSetNames,
158  Ostream& os
159 ) const
160 {
161  writeCoordHeader(points, os);
162 
163  forAll(valueSetNames, i)
164  {
165  for (label j=0; j<Type::nComponents; j++)
166  {
167  if (i>0 || j>0)
168  {
169  writeSeparator(os);
170  }
171  os << valueSetNames[i] << "_" << j;
172  }
173  }
174 
175  os << nl;
176 }
177 
178 
179 template<class Type>
181 (
182  const coordSet& points,
183  Ostream& os
184 ) const
185 {
186  const word axisName(points.axis());
187 
188  if (points.hasVectorAxis())
189  {
190  for
191  (
192  word::const_iterator iter = axisName.begin();
193  iter != axisName.end();
194  ++iter
195  )
196  {
197  os << *iter;
198  writeSeparator(os);
199  }
200  }
201  else
202  {
203  os << axisName;
204  writeSeparator(os);
205  }
206 }
207 
208 
209 // ************************************************************************* //
virtual ~csvSetWriter()
Destructor.
Definition: csvSetWriter.C:43
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
word axis() const
Definition: coordSet.H:116
fileName getBaseName(const coordSet &, const wordList &) const
Generates filename from coordSet and sampled fields.
Definition: writer.C:60
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
A class for handling file names.
Definition: fileName.H:69
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
virtual void write(const coordSet &, const wordList &, const List< const Field< Type > *> &, Ostream &) const
General entry point for writing.
Definition: csvSetWriter.C:62
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
bool hasVectorAxis() const
Is axis specification a vector.
Definition: coordSet.C:84
Base class for graphics format writing. Entry points are.
Definition: writer.H:78
virtual fileName getFileName(const coordSet &, const wordList &) const
Generate file name with correct extension.
Definition: csvSetWriter.C:51
Holds list of sampling positions.
Definition: coordSet.H:49
csvSetWriter()
Construct null.
Definition: csvSetWriter.C:34
Pre-declare SubField and related Field type.
Definition: Field.H:57
A class for handling words, derived from string.
Definition: word.H:59
Write set in csv format.
Definition: csvSetWriter.H:49
void writeTable(const coordSet &, const List< Type > &, Ostream &) const
Writes single-column ascii write. Column 1 is coordSet coordinate,.
Definition: writer.C:97
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
static const char nl
Definition: Ostream.H:265
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:63
virtual void writeSeparator(Ostream &) const
Writes a separator. Used by write functions.
Definition: csvSetWriter.C:120
Namespace for OpenFOAM.