CSV.H
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) 2017-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 Class
28  Foam::Function1Types::CSV
29 
30 Description
31  Templated CSV function.
32 
33  Reference column is always a scalar, e.g. time.
34 
35  Usage:
36  \verbatim
37  <entryName> csvFile;
38  <entryName>Coeffs
39  {
40  nHeaderLine 4; // number of header lines
41  refColumn 0; // reference column index
42  componentColumns (1 2 3); // component column indices
43  separator ","; // optional (defaults to ",")
44  mergeSeparators no; // merge multiple separators
45  file "fileXYZ"; // name of csv data file
46  outOfBounds clamp; // optional out-of-bounds handling
47  interpolationScheme linear; // optional interpolation scheme
48  }
49  \endverbatim
50 
51 SourceFiles
52  CSV.C
53 
54 \*---------------------------------------------------------------------------*/
55 
56 #ifndef Function1Types_CSV_H
57 #define Function1Types_CSV_H
58 
59 #include "Function1.H"
60 #include "TableBase.H"
61 #include "Tuple2.H"
62 #include "labelList.H"
63 
64 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
65 
66 namespace Foam
67 {
68 namespace Function1Types
69 {
70 
71 /*---------------------------------------------------------------------------*\
72  Class CSV Declaration
73 \*---------------------------------------------------------------------------*/
74 
75 template<class Type>
76 class CSV
77 :
78  public TableBase<Type>
79 {
80  // Private Data
81 
82  //- Number header lines
83  const label nHeaderLine_;
84 
85  //- Column of the time
86  const label refColumn_;
87 
88  //- Labels of the components
89  const labelList componentColumns_;
90 
91  //- Separator character
92  const char separator_;
93 
94  //- Merge separators flag, e.g. ',,,' becomes ','
95  const bool mergeSeparators_;
96 
97  //- File name for csv table
98  fileName fName_;
99 
100 
101  // Private Member Functions
102 
103  //- Get component columns entry
104  static labelList getComponentColumns
105  (
106  const word& name,
107  const dictionary& dict
108  );
109 
110  //- Read csv data table
111  void read();
112 
113  //- Read component values from the split string
114  Type readValue(const List<string>& strings) const;
115 
116  //- No copy assignment
117  void operator=(const CSV<Type>&) = delete;
118 
119 
120 public:
121 
122  //- Declare type-name, virtual type (with debug switch)
123  TypeName("csvFile");
124 
125 
126  // Constructors
127 
128  //- Construct from entry name, dictionary and optional registry
129  CSV
130  (
131  const word& entryName,
132  const dictionary& dict,
133  const objectRegistry* obrPtr = nullptr,
134  const fileName& fName = fileName::null
135  );
136 
137  //- Copy construct
138  explicit CSV(const CSV<Type>& csv);
139 
140  //- Construct and return a clone
141  virtual tmp<Function1<Type>> clone() const
142  {
143  return tmp<Function1<Type>>(new CSV<Type>(*this));
144  }
145 
146 
147  //- Destructor
148  virtual ~CSV() = default;
149 
150 
151  // Member Functions
152 
153  //- Return const access to the file name
154  virtual const fileName& fName() const;
155 
156  //- Write in dictionary format
157  virtual void writeData(Ostream& os) const;
158 
159  //- Write coefficient entries in dictionary format
160  void writeEntries(Ostream& os) const;
161 };
162 
163 
164 // Template specialisations
165 template<>
166 label CSV<label>::readValue(const List<string>& strings) const;
167 
168 template<>
169 scalar CSV<scalar>::readValue(const List<string>& strings) const;
170 
171 
172 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
173 
174 } // End namespace Function1Types
175 } // End namespace Foam
176 
177 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
178 
179 #ifdef NoRepository
180  #include "CSV.C"
181 #endif
182 
183 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
184 
185 #endif
186 
187 // ************************************************************************* //
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Tuple2.H
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
Foam::Function1::entryName
const word & entryName
Definition: Function1.H:133
Function1.H
Foam::Function1Types::CSV
Templated CSV function.
Definition: CSV.H:75
Foam::Function1Types::CSV::writeEntries
void writeEntries(Ostream &os) const
Write coefficient entries in dictionary format.
Definition: CSV.C:254
Foam::Function1::dict
const word const dictionary & dict
Definition: Function1.H:134
Foam::Function1Types::CSV::~CSV
virtual ~CSV()=default
Destructor.
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
TableBase.H
labelList.H
Foam::Function1::obrPtr
const word const dictionary const objectRegistry * obrPtr
Definition: Function1.H:136
Foam::Function1Types::CSV::TypeName
TypeName("csvFile")
Declare type-name, virtual type (with debug switch)
Foam::Function1Types::CSV::writeData
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition: CSV.C:275
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)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
CSV.C
Foam::fileName::null
static const fileName null
An empty fileName.
Definition: fileName.H:101
Foam::Function1Types::CSV::clone
virtual tmp< Function1< Type > > clone() const
Construct and return a clone.
Definition: CSV.H:140
Foam::List< label >
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::Function1Types::CSV::CSV
CSV(const word &entryName, const dictionary &dict, const objectRegistry *obrPtr=nullptr, const fileName &fName=fileName::null)
Construct from entry name, dictionary and optional registry.
Definition: CSV.C:210
Foam::Function1Types::TableBase
Base class for table with bounds handling, interpolation and integration.
Definition: TableBase.H:60
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::Function1Types::CSV::fName
virtual const fileName & fName() const
Return const access to the file name.
Definition: CSV.C:247