Loading...
Searching...
No Matches
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-2025 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
36template<class Type>
37Foam::labelList Foam::csvTableReader<Type>::getComponentColumns
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(IOstreamOption::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
69template<class Type>
70Type Foam::csvTableReader<Type>::readValue
71(
72 const UList<string>& strings
73) const
74{
75 Type result;
76
77 if constexpr (std::is_integral_v<Type>)
78 {
79 // nComponents == 1
80 setComponent(result, 0) = readLabel(strings[componentColumns_[0]]);
81 }
82 else
83 {
84 for (direction cmpt = 0; cmpt < pTraits<Type>::nComponents; ++cmpt)
85 {
86 setComponent(result, cmpt) =
87 readScalar(strings[componentColumns_[cmpt]]);
88 }
89 }
90
91 return result;
92}
93
94
95// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
96
97template<class Type>
99:
100 tableReader<Type>(dict),
101 headerLine_(dict.get<bool>("hasHeaderLine")),
102 refColumn_(dict.getCompat<label>("refColumn", {{"timeColumn", 1912}})),
103 componentColumns_
104 (
105 getComponentColumns("componentColumns", {{"valueColumns", 1912}}, dict)
106 ),
107 separator_(dict.getOrDefault<string>("separator", ",")[0])
108{}
109
110
111// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
112
113template<class Type>
115(
116 const fileName& fName,
118)
119{
120 autoPtr<ISstream> isPtr(fileHandler().NewIFstream(fName));
121 ISstream& is = isPtr();
122
123 const label maxEntry =
124 max(refColumn_, componentColumns_[findMax(componentColumns_)]);
125
126 string line;
127 label lineNo = 0;
128
129 // Skip header
130 if (headerLine_)
131 {
132 is.getLine(nullptr);
133 ++lineNo;
134 }
135
136 DynamicList<Tuple2<scalar, Type>> values;
137 DynamicList<string> strings(maxEntry+1); // reserve
138
139 while (is.good())
140 {
141 is.getLine(line);
142 ++lineNo;
143
144 strings.clear();
145
146 std::size_t pos = 0;
147
148 for
149 (
150 label n = 0;
151 (pos != std::string::npos) && (n <= maxEntry);
152 ++n
153 )
154 {
155 const auto nPos = line.find(separator_, pos);
156
157 if (nPos == std::string::npos)
158 {
159 strings.push_back(line.substr(pos));
160 pos = nPos;
161 }
162 else
163 {
164 strings.push_back(line.substr(pos, nPos-pos));
165 pos = nPos + 1;
166 }
167 }
168
169 if (strings.size() <= 1)
170 {
171 break;
172 }
173
174 if (strings.size() <= maxEntry)
175 {
177 << "Not enough columns near line " << lineNo
178 << ". Require " << (maxEntry+1) << " but found "
179 << strings << nl
180 << exit(FatalError);
181 }
182
183 scalar x = readScalar(strings[refColumn_]);
184 Type value = readValue(strings);
185
186 values.emplace_back(x, value);
188
189 data.transfer(values);
190}
191
192
193template<class Type>
195(
196 const fileName& fName,
197 List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>& data
199{
201}
202
203
204template<class Type>
206{
208
209 os.writeEntry("hasHeaderLine", headerLine_);
210 os.writeEntry("refColumn", refColumn_);
211
212 // Force writing labelList in ASCII
213 const auto oldFmt = os.format(IOstreamOption::ASCII);
214 os.writeEntry("componentColumns", componentColumns_);
215 os.format(oldFmt);
216
217 os.writeEntry("separator", string(separator_));
218}
219
220
221// ************************************************************************* //
Various functions to operate on Lists.
label n
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
void push_back(const T &val)
Copy append an element to the end of this list.
@ ASCII
"ascii" (normal default)
bool good() const noexcept
True if next operation might succeed.
Definition IOstream.H:281
Generic input stream using a standard (STL) stream.
Definition ISstream.H:54
ISstream & getLine(std::string &str, char delim='\n')
Raw, low-level getline (until delimiter) into a string.
Definition ISstreamI.H:69
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
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
Reads an interpolation table from a file - CSV-format.
virtual void write(Ostream &os) const
Write the remaining parameters.
csvTableReader(const dictionary &dict)
Construct from dictionary.
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
A line primitive.
Definition line.H:180
Base class to read table data for the interpolationTable.
Definition tableReader.H:57
virtual void write(Ostream &os) const
Write additional information.
Definition tableReader.C:65
tableReader()=default
Default construct.
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition error.H:688
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
OBJstream os(runTime.globalPath()/outputName)
auto & name
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition HashOps.H:164
dimensionedScalar pos(const dimensionedScalar &ds)
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:40
List< label > labelList
A List of labels.
Definition List.H:62
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler().
label & setComponent(label &val, const direction) noexcept
Non-const access to integer-type (has no components).
Definition label.H:160
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
Definition label.H:63
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
label findMax(const ListType &input, label start=0)
Linear search for the index of the max element, similar to std::max_element but for lists and returns...
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict