uniformInterpolationTable.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 | Copyright (C) 2011-2015 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 
27 #include "Time.H"
28 #include "IOstream.H"
29 
30 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
31 
32 template<class Type>
34 {
35  if (size() < 2)
36  {
38  << "Table " << name() << ": must have at least 2 values." << nl
39  << "Table size = " << size() << nl
40  << " min, interval width = " << x0_ << ", " << dx_ << nl
41  << exit(FatalError);
42  }
43 }
44 
45 
46 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
47 
48 template<class Type>
50 (
51  const IOobject& io,
52  bool readFields
53 )
54 :
55  IOobject(io),
56  List<scalar>(2, 0.0),
57  x0_(0.0),
58  dx_(1.0),
59  log10_(false),
60  bound_(false)
61 {
62  if (readFields)
63  {
64  IOdictionary dict(io);
65 
66  dict.lookup("data") >> *this;
67  dict.lookup("x0") >> x0_;
68  dict.lookup("dx") >> dx_;
69  dict.readIfPresent("log10", log10_);
70  dict.readIfPresent("bound", bound_);
71  }
72 
73  checkTable();
74 }
75 
76 
77 template<class Type>
79 (
80  const word& tableName,
81  const objectRegistry& db,
82  const dictionary& dict,
83  const bool initialiseOnly
84 )
85 :
86  IOobject
87  (
88  tableName,
89  db.time().constant(),
90  db,
91  IOobject::NO_READ,
92  IOobject::NO_WRITE,
93  false // if used in BCs, could be used by multiple patches
94  ),
95  List<scalar>(2, 0.0),
96  x0_(readScalar(dict.lookup("x0"))),
97  dx_(readScalar(dict.lookup("dx"))),
98  log10_(dict.lookupOrDefault<Switch>("log10", false)),
99  bound_(dict.lookupOrDefault<Switch>("bound", false))
100 {
101  if (initialiseOnly)
102  {
103  const scalar xMax = readScalar(dict.lookup("xMax"));
104  const label nIntervals = static_cast<label>(xMax - x0_)/dx_ + 1;
105  this->setSize(nIntervals);
106  }
107  else
108  {
109  dict.lookup("data") >> *this;
110  }
111 
112  checkTable();
113 }
114 
115 
116 template<class Type>
118 (
119  const uniformInterpolationTable& uit
120 )
121 :
122  IOobject(uit),
123  List<scalar>(uit),
124  x0_(uit.x0_),
125  dx_(uit.dx_),
126  log10_(uit.log10_),
127  bound_(uit.bound_)
128 {
129  checkTable();
130 }
131 
132 
133 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
134 
135 template<class Type>
137 {}
138 
139 
140 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
141 
142 template<class Type>
144 {
145  if (bound_)
146  {
147  x = max(min(xMax() - SMALL*dx_, x), x0_);
148  }
149  else
150  {
151  if (x < x0_)
152  {
154  << "Supplied value is less than minimum table value:" << nl
155  << "xMin=" << x0_ << ", xMax=" << xMax() << ", x=" << x << nl
156  << exit(FatalError);
157  }
158 
159  if (x > xMax())
160  {
162  << "Supplied value is greater than maximum table value:" << nl
163  << "xMin=" << x0_ << ", xMax=" << xMax() << ", x=" << x << nl
164  << exit(FatalError);
165  }
166  }
167 
168  const label i = static_cast<label>((x - x0_)/dx_);
169 
170  const scalar xLo = x0_ + i*dx_;
171 
172  Type fx = (x - xLo)/dx_*(operator[](i+1) - operator[](i)) + operator[](i);
173 
174  if (debug)
175  {
176  Info<< "Table: " << name() << ", x=" << x
177  << ", x_lo=" << xLo << ", x_hi=" << xLo + dx_
178  << ", f(x_lo)=" << operator[](i) << ", f(x_hi)=" << operator[](i+1)
179  << ", f(x)=" << fx << endl;
180  }
181 
182  return fx;
183 }
184 
185 
186 template<class Type>
188 (
189  scalar x
190 ) const
191 {
192  if (log10_)
193  {
194  if (x > 0)
195  {
196  x = ::log10(x);
197  }
198  else if (bound_ && (x <= 0))
199  {
200  x = x0_;
201  }
202  else
203  {
205  << "Table " << name() << nl
206  << "Supplied value must be greater than 0 when in log10 mode"
207  << nl << "x=" << x << nl << exit(FatalError);
208  }
209  }
210 
211  return interpolate(x);
212 }
213 
214 
215 template<class Type>
217 {
218  IOdictionary dict(*this);
219 
220  dict.add("data", static_cast<const List<scalar>&>(*this));
221  dict.add("x0", x0_);
222  dict.add("dx", dx_);
223  if (log10_)
224  {
225  dict.add("log10", log10_);
226  }
227  if (bound_)
228  {
229  dict.add("bound", bound_);
230  }
231 
232  dict.regIOobject::writeObject
233  (
234  IOstream::ASCII,
235  IOstream::currentVersion,
236  dict.time().writeCompression()
237  );
238 }
239 
240 
241 // ************************************************************************* //
Foam::IOdictionary
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:53
setSize
points setSize(newPointi)
Foam::IOobject
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
Foam::Switch
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition: Switch.H:60
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::uniformInterpolationTable
Table with uniform interval in independant variable, with linear interpolation.
Definition: uniformInterpolationTable.H:70
Foam::objectRegistry::time
const Time & time() const
Return time.
Definition: objectRegistry.H:117
Foam::MULES::interpolate
tmp< surfaceScalarField > interpolate(const RhoType &rho)
Definition: IMULESTemplates.C:40
uniformInterpolationTable.H
Foam::uniformInterpolationTable::bound_
Switch bound_
Bound x values.
Definition: uniformInterpolationTable.H:89
Foam::uniformInterpolationTable::x0_
scalar x0_
Lower limit.
Definition: uniformInterpolationTable.H:80
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::uniformInterpolationTable::interpolateLog10
Type interpolateLog10(scalar x) const
Interpolate - takes log10 flag into account.
Definition: uniformInterpolationTable.C:188
Foam::readFields
This function object reads fields from the time directories and adds them to the mesh database for fu...
Definition: readFields.H:104
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:50
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
Foam::log10
dimensionedScalar log10(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:254
IOstream.H
Foam::uniformInterpolationTable::log10_
Switch log10_
Flag to indicate that x data are given in log10(x) form.
Definition: uniformInterpolationTable.H:86
Foam::uniformInterpolationTable::dx_
scalar dx_
Fixed interval.
Definition: uniformInterpolationTable.H:83
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::uniformInterpolationTable::~uniformInterpolationTable
~uniformInterpolationTable()
Destructor.
Definition: uniformInterpolationTable.C:136
nIntervals
const label nIntervals(readLabel(pdfDictionary.lookup("nIntervals")))
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
readScalar
#define readScalar
Definition: doubleScalar.C:38
Foam::max
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
xMax
const scalar xMax
Definition: createFields.H:35
Foam::List< scalar >
Foam::uniformInterpolationTable::write
void write() const
Write.
Definition: uniformInterpolationTable.C:216
Foam::TimePaths::constant
const word & constant() const
Return constant name.
Definition: TimePaths.H:130
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::uniformInterpolationTable::checkTable
void checkTable() const
Check that the table is valid.
Definition: uniformInterpolationTable.C:33
Foam::uniformInterpolationTable::uniformInterpolationTable
uniformInterpolationTable(const IOobject &, const bool readFields)
Construct from IOobject and readFields flag.
Definition: uniformInterpolationTable.C:50
Foam::uniformInterpolationTable::interpolate
Type interpolate(scalar x) const
Interpolate.
Definition: uniformInterpolationTable.C:143
Foam::min
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47