Table.H
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-2024 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 Class
25  Foam::Function1s::Table
26 
27 Description
28  Templated interpolated tabulated data Function1.
29 
30  Items are stored in a list of Tuple2's. First column is always stored as
31  scalar entries. Data is read in Tuple2 form:
32 
33 Usage
34  \verbatim
35  <name> table
36  (
37  (0.0 (1 2 3))
38  (1.0 (4 5 6))
39  );
40  \endverbatim
41 
42  or in dictionary form which supports the setting of options, e.g.
43  \verbatim
44  <name> table;
45 
46  values
47  (
48  (0.0 (1 2 3))
49  (1.0 (4 5 6))
50  );
51 
52  outOfBounds clamp; // optional out-of-bounds handling
53  interpolationScheme linear; // optional interpolation method
54  \endverbatim
55 
56  or in sub-dictionary form which avoids clashes between table entries and
57  other entries in the dictionary:
58 
59  \verbatim
60  <name>
61  {
62  type table;
63 
64  values
65  (
66  (0.0 (1 2 3))
67  (1.0 (4 5 6))
68  );
69 
70  outOfBounds clamp; // optional out-of-bounds handling
71  interpolationScheme linear; // optional interpolation method
72  }
73  \endverbatim
74 
75  The data may be read from a separate file in either native or CSV format:
76 
77  \verbatim
78  <name>
79  {
80  type table;
81  file "<file path>"; // Name/path of thedata file
82  format foam; // data format (optional)
83  outOfBounds clamp; // optional out-of-bounds handling
84  interpolationScheme linear; // optional interpolation method
85  }
86  \endverbatim
87 
88 SourceFiles
89  Table.C
90 
91 See also
92  FoamTableReader.C
93  CsvTableReader.C
94 
95 \*---------------------------------------------------------------------------*/
96 
97 #ifndef Table_H
98 #define Table_H
99 
100 #include "tableBase.H"
101 #include "Function1.H"
102 #include "TableReader.H"
103 
104 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
105 
106 namespace Foam
107 {
108 
109 class interpolationWeights;
110 
111 namespace Function1s
112 {
113 
114 /*---------------------------------------------------------------------------*\
115  Class Table Declaration
116 \*---------------------------------------------------------------------------*/
117 
118 template<class Type>
119 class Table
120 :
121  public tableBase,
122  public FieldFunction1<Type, Table<Type>>
123 {
124  // Private Data
125 
126  //- Enumeration for handling out-of-bound values
127  const tableBase::boundsHandling boundsHandling_;
128 
129  //- Interpolation scheme
130  const word interpolationScheme_;
131 
132  //- Table reader
133  const autoPtr<TableReader<Type>> reader_;
134 
135  //- Table data
136  const List<Tuple2<scalar, Type>> values_;
137 
138  //- Extracted values
139  mutable autoPtr<scalarField> tableSamplesPtr_;
140 
141  //- Interpolator method
142  mutable autoPtr<interpolationWeights> interpolatorPtr_;
143 
144  //- Cached indices
145  mutable labelList indices_;
146 
147  //- Cached weights
148  mutable scalarField weights_;
149 
150 
151  // Private Member Functions
152 
153  //- Return (demand driven) interpolator
154  const interpolationWeights& interpolator() const;
155 
156  //- Check the table for size and consistency
157  void check() const;
158 
159  //- Bound the argument to the table. Errors or warns, or shifts the
160  // value if the table repeats. Does not clamp to the ends of the table
161  // as the interpolator already performs that function.
162  scalar bound(const scalar x) const;
163 
164 
165 public:
166 
167  //- Runtime type information
168  TypeName("table");
169 
170 
171  // Constructors
172 
173  //- Construct from components
174  Table
175  (
176  const word& name,
178  const word& interpolationScheme,
179  const autoPtr<TableReader<Type>>& reader,
180  const List<Tuple2<scalar, Type>>& table
181  );
182 
183  //- Construct from name and dictionary
184  Table
185  (
186  const word& name,
187  const unitConversions& units,
188  const dictionary& dict
189  );
190 
191  //- Construct from name and dictionary
192  Table
193  (
194  const word& name,
195  const unitConversion& xUnits,
196  const unitConversion& valueUnits,
197  const dictionary& dict
198  );
199 
200  //- Construct from name and Istream
201  Table
202  (
203  const word& name,
204  const unitConversions& units,
205  Istream& dict
206  );
207 
208  //- Copy constructor
209  Table(const Table<Type>& tbl);
210 
211 
212  //- Destructor
213  virtual ~Table();
214 
215 
216  // Member Functions
217 
218  //- Return the handling out-of-bound values
220  {
221  return boundsHandling_;
222  }
223 
224  //- Return the interpolation scheme
225  const word& interpolationScheme() const
226  {
227  return interpolationScheme_;
228  }
229 
230  //- Return table data
231  const List<Tuple2<scalar, Type>>& values() const
232  {
233  return values_;
234  }
235 
236  //- Return Table value as a function of scalar x
237  virtual Type value(const scalar x) const;
238 
239  //- Integrate between two scalars
240  virtual Type integral(const scalar x1, const scalar x2) const;
241 
242  //- Return the reference values
243  virtual tmp<scalarField> x() const;
244 
245  //- Return the dependent values
246  virtual tmp<Field<Type>> y() const;
247 
248  //- Write data to dictionary stream
249  virtual void write(Ostream& os, const unitConversions& units) const;
250 
251 
252  // Member Operators
253 
254  //- Disallow default bitwise assignment
255  void operator=(const Table<Type>&) = delete;
256 };
257 
258 
259 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
260 
261 } // End namespace Function1s
262 } // End namespace Foam
263 
264 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
265 
266 #ifdef NoRepository
267  #include "Table.C"
268 #endif
269 
270 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
271 
272 #endif
273 
274 // ************************************************************************* //
const word & name() const
Return the name of the entry.
Definition: Function1.C:78
Templated interpolated tabulated data Function1.
Definition: Table.H:122
TypeName("table")
Runtime type information.
const tableBase::boundsHandling & boundsHandling() const
Return the handling out-of-bound values.
Definition: Table.H:218
const word & interpolationScheme() const
Return the interpolation scheme.
Definition: Table.H:224
virtual tmp< scalarField > x() const
Return the reference values.
Definition: Table.C:301
virtual Type integral(const scalar x1, const scalar x2) const
Integrate between two scalars.
Definition: Table.C:258
Table(const word &name, const tableBase::boundsHandling boundsHandling, const word &interpolationScheme, const autoPtr< TableReader< Type >> &reader, const List< Tuple2< scalar, Type >> &table)
Construct from components.
Definition: Table.C:137
virtual void write(Ostream &os, const unitConversions &units) const
Write data to dictionary stream.
Definition: Table.C:333
void operator=(const Table< Type > &)=delete
Disallow default bitwise assignment.
const List< Tuple2< scalar, Type > > & values() const
Return table data.
Definition: Table.H:230
virtual tmp< Field< Type > > y() const
Return the dependent values.
Definition: Table.C:317
virtual Type value(const scalar x) const
Return Table value as a function of scalar x.
Definition: Table.C:238
virtual ~Table()
Destructor.
Definition: Table.C:230
boundsHandling
Enumeration for handling out-of-bound values.
Definition: tableBase.H:55
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:91
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Macros for creating standard TableReader-s.
Definition: TableReader.H:51
A 2-tuple for storing two objects of different types.
Definition: Tuple2.H:66
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
Abstract base class for interpolating in 1D.
A class for managing temporary objects.
Definition: tmp.H:55
Unit conversion structure. Contains the associated dimensions and the multiplier with which to conver...
A class for handling words, derived from string.
Definition: word.H:62
Namespace for OpenFOAM.
const HashTable< unitConversion > & units()
Get the table of unit conversions.
dictionary dict