csvTableReader.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) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2020-2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "csvTableReader.H"
30 #include "fileOperation.H"
31 #include "DynamicList.H"
32 #include "ListOps.H"
33 
34 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
35 
36 template<class Type>
38 (
39  const word& name,
40  std::initializer_list<std::pair<const char*,int>> compat,
41  const dictionary& dict
42 )
43 {
44  // Writing of columns was forced to be ASCII,
45  // do the same when reading
46 
47  labelList cols;
48 
49  ITstream& is = dict.lookupCompat(name, compat);
50  is.format(IOstream::ASCII);
51  is >> cols;
52  dict.checkITstream(is, name);
53 
54  if (cols.size() != pTraits<Type>::nComponents)
55  {
57  << name << " with " << cols
58  << " does not have the expected length "
59  << pTraits<Type>::nComponents << nl
60  << exit(FatalIOError);
61  }
62 
63  return cols;
64 }
65 
66 
67 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
68 
69 namespace Foam
70 {
71  template<>
72  label csvTableReader<label>::readValue
73  (
74  const List<string>& strings
75  ) const
76  {
77  return readLabel(strings[componentColumns_[0]]);
78  }
79 
80 
81  template<>
82  scalar csvTableReader<scalar>::readValue
83  (
84  const List<string>& strings
85  ) const
86  {
87  return readScalar(strings[componentColumns_[0]]);
88  }
89 
90 } // End namespace Foam
91 
92 
93 template<class Type>
95 (
96  const List<string>& strings
97 ) const
98 {
99  Type result;
100 
101  for (label i = 0; i < pTraits<Type>::nComponents; ++i)
102  {
103  result[i] = readScalar(strings[componentColumns_[i]]);
104  }
105 
106  return result;
107 }
108 
109 
110 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
111 
112 template<class Type>
114 :
115  tableReader<Type>(dict),
116  headerLine_(dict.get<bool>("hasHeaderLine")),
117  refColumn_(dict.getCompat<label>("refColumn", {{"timeColumn", 1912}})),
118  componentColumns_
119  (
120  getComponentColumns("componentColumns", {{"valueColumns", 1912}}, dict)
121  ),
122  separator_(dict.getOrDefault<string>("separator", ",")[0])
123 {}
124 
125 
126 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
127 
128 template<class Type>
130 (
131  const fileName& fName,
133 )
134 {
135  autoPtr<ISstream> isPtr(fileHandler().NewIFstream(fName));
136  ISstream& is = isPtr();
137 
138  const label maxEntry =
139  max(refColumn_, componentColumns_[findMax(componentColumns_)]);
140 
141  string line;
142  label lineNo = 0;
143 
144  // Skip header
145  if (headerLine_)
146  {
147  is.getLine(line);
148  ++lineNo;
149  }
150 
152  DynamicList<string> strings(maxEntry+1); // reserve
153 
154  while (is.good())
155  {
156  is.getLine(line);
157  ++lineNo;
158 
159  strings.clear();
160 
161  std::size_t pos = 0;
162 
163  for
164  (
165  label n = 0;
166  (pos != std::string::npos) && (n <= maxEntry);
167  ++n
168  )
169  {
170  const auto nPos = line.find(separator_, pos);
171 
172  if (nPos == std::string::npos)
173  {
174  strings.append(line.substr(pos));
175  pos = nPos;
176  }
177  else
178  {
179  strings.append(line.substr(pos, nPos-pos));
180  pos = nPos + 1;
181  }
182  }
183 
184  if (strings.size() <= 1)
185  {
186  break;
187  }
188 
189  if (strings.size() <= maxEntry)
190  {
192  << "Not enough columns near line " << lineNo
193  << ". Require " << (maxEntry+1) << " but found "
194  << strings << nl
195  << exit(FatalError);
196  }
197 
198  scalar x = readScalar(strings[refColumn_]);
199  Type value = readValue(strings);
200 
201  values.append(Tuple2<scalar,Type>(x, value));
202  }
203 
205 }
206 
207 
208 template<class Type>
210 (
211  const fileName& fName,
213 )
214 {
216 }
217 
218 
219 template<class Type>
221 {
223 
224  os.writeEntry("hasHeaderLine", headerLine_);
225  os.writeEntry("refColumn", refColumn_);
226 
227  // Force writing labelList in ASCII
228  const auto oldFmt = os.format(IOstream::ASCII);
229  os.writeEntry("componentColumns", componentColumns_);
230  os.format(oldFmt);
231 
232  os.writeEntry("separator", string(separator_));
233 }
234 
235 
236 // ************************************************************************* //
csvTableReader.H
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:67
Foam::ISstream::getLine
ISstream & getLine(std::string &str, char delim='\n')
Raw, low-level getline (until delimiter) into a string.
Definition: ISstreamI.H:76
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:55
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:286
Foam::ISstream
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:55
Foam::fileHandler
const fileOperation & fileHandler()
Get current file handler.
Definition: fileOperation.C:1485
Foam::FatalIOError
IOerror FatalIOError
Foam::tableReader
Base class to read table data for the interpolationTable.
Definition: tableReader.H:60
Foam::csvTableReader::csvTableReader
csvTableReader(const dictionary &dict)
Construct from dictionary.
Definition: csvTableReader.C:113
Foam::DynamicList::clear
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:391
Foam::dictionary::transfer
void transfer(dictionary &dict)
Transfer the contents of the argument and annul the argument.
Definition: dictionary.C:866
Foam::IOstream::good
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
Foam::csvTableReader::write
virtual void write(Ostream &os) const
Write the remaining parameters.
Definition: csvTableReader.C:220
n
label n
Definition: TABSMDCalcMethod2.H:31
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:517
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
Definition: DynamicListI.H:511
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::csvTableReader
Reads an interpolation table from a file - CSV-format.
Definition: csvTableReader.H:53
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
fileOperation.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::readLabel
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
Definition: label.H:66
Foam::findMax
label findMax(const ListType &input, label start=0)
Foam::List< label >
bool
bool
Definition: EEqn.H:20
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::line
A line primitive.
Definition: line.H:53
Foam::Ostream::writeEntry
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:236
Foam::tableReader::write
virtual void write(Ostream &os) const
Write additional information.
Definition: tableReader.C:72
Foam::PtrListOps::get
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
DynamicList.H
ListOps.H
Various functions to operate on Lists.
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::Tuple2
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: stringOps.H:60
Foam::dictionary::getOrDefault
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:148
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:55
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177