TableFileReader.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-2026 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 "TableFileReader.H"
27 #include "fileOperation.H"
28 
29 // * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
30 
31 template<class Coordinate, class Value>
34 (
35  const Function1s::unitSets& defaultUnits,
36  const dictionary& dict
37 ) const
38 {
39  if (dict.found("units"))
40  {
41  Function1s::unitSets units(defaultUnits);
42  units.read(dict.lookup("units"));
43  return
44  autoPtr<Function1s::unitSets>
45  (
46  new Function1s::unitSets(units)
47  );
48  }
49  else
50  {
51  return autoPtr<Function1s::unitSets>(nullptr);
52  }
53 }
54 
55 
56 template<class Coordinate, class Value>
58 (
59  const Function1s::unitSets& defaultUnits,
60  const dictionary& dict,
62 ) const
63 {
64  // Expand the file
65  fileName fNameExpanded(fName_);
66  fNameExpanded.expand();
67 
68  // Open a stream and check it
69  autoPtr<ISstream> isPtr(fileHandler().NewIFstream(fNameExpanded));
70  ISstream& is = isPtr();
71  if (!is.good())
72  {
74  << "Cannot open file " << fName_ << nl
75  << exit(FatalIOError);
76  }
77 
78  // Read data from the stream
79  read(is, table);
80 
81  // Check something was read
82  if (table.empty())
83  {
85  << "Table read from " << fName_ << " is empty" << nl
86  << exit(FatalIOError);
87  }
88 
89  // Convert units
91  (
92  unitsPtr_.valid() ? unitsPtr_() : defaultUnits,
93  table
94  );
95 }
96 
97 
98 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
99 
100 template<class Coordinate, class Value>
102 (
103  const Function1s::unitSets& defaultUnits,
104  const dictionary& dict
105 )
106 :
107  TableReader<Coordinate, Value>(),
108  fName_(dict.lookup("file")),
109  unitsPtr_(readUnits(defaultUnits, dict))
110 {}
111 
112 
113 template<class Coordinate, class Value>
115 (
117 )
118 :
119  TableReader<Coordinate, Value>(tfr),
120  fName_(tfr.fName_),
121  unitsPtr_
122  (
123  tfr.unitsPtr_.valid()
124  ? new Function1s::unitSets(tfr.unitsPtr_())
125  : nullptr
126  )
127 {}
128 
129 
130 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
131 
132 template<class Coordinate, class Value>
134 {}
135 
136 
137 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
138 
139 template<class Coordinate, class Value>
142 (
143  const Function1s::unitSets& units,
144  const dictionary& dict,
145  const word&
146 ) const
147 {
149  read(units, dict, data);
150  return data;
151 }
152 
153 
154 template<class Coordinate, class Value>
156 (
157  Ostream& os,
158  const Function1s::unitSets& units,
160  const word&
161 ) const
162 {
163  writeEntry(os, "format", this->type());
164  writeEntry(os, "file", fName_);
165 
166  if (unitsPtr_.valid())
167  {
168  writeEntry(os, "units", unitsPtr_());
169  }
170 }
171 
172 
173 // ************************************************************************* //
Generic input stream.
Definition: ISstream.H:55
bool good() const
Return true if next operation might succeed.
Definition: Istream.H:101
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
Base class to read table data for tables.
virtual ~TableFileReader()
Destructor.
TableFileReader(const Function1s::unitSets &defaultUnits, const dictionary &dict)
Construct from dictionary.
virtual void write(Ostream &os, const Function1s::unitSets &units, const List< Tuple2< Coordinate, Value >> &table, const word &valuesKeyword=word::null) const
Write additional information.
Base class to read table data for tables.
Definition: TableReader.H:51
A 2-tuple for storing two objects of different types.
Definition: Tuple2.H:66
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
A class for handling file names.
Definition: fileName.H:82
string & expand(const bool allowEmpty=false)
Expand initial tildes and all occurrences of environment variables.
Definition: string.C:125
A class for handling words, derived from string.
Definition: word.H:63
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
bool valid(const PtrList< ModelType > &l)
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
const HashTable< dimensionSet > table
Table of dimensions.
Definition: dimensions.C:74
const unitSet & lookup(const word &unitName)
Lookup and return the named unit from the table.
Definition: units.C:346
const fileOperation & fileHandler()
Get current file handler.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
bool read(const char *, int32_t &)
Definition: int32IO.C:85
HashTable< unitSet > * unitsPtr_(nullptr)
IOerror FatalIOError
void writeEntry(Ostream &os, const word &key, const DimensionedFieldFunction< DimensionedFieldType > &f)
static const char nl
Definition: Ostream.H:297
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
dictionary dict
Struct containing two unitSets for use in converting both the argument and the value of a Function1.