1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
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
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "interpolationTable.H"
30 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
33 void Foam::interpolationTable<Type>::readTable()
35 // preserve the original (unexpanded) fileName to avoid absolute paths
36 // appearing subsequently in the write() method
37 fileName fName(fileName_);
41 // Read data from file
42 IFstream(fName)() >> *this;
44 // Check that the data are okay
51 "Foam::interpolationTable<Type>::readTable()"
52 ) << "table is empty" << nl
58 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
61 Foam::interpolationTable<Type>::interpolationTable()
63 List<Tuple2<scalar, Type> >(),
64 boundsHandling_(interpolationTable::WARN),
65 fileName_("fileNameIsUndefined")
70 Foam::interpolationTable<Type>::interpolationTable
72 const List<Tuple2<scalar, Type> >& values,
73 const boundsHandling bounds,
77 List<Tuple2<scalar, Type> >(values),
78 boundsHandling_(bounds),
84 Foam::interpolationTable<Type>::interpolationTable(const fileName& fName)
86 List<Tuple2<scalar, Type> >(),
87 boundsHandling_(interpolationTable::WARN),
95 Foam::interpolationTable<Type>::interpolationTable(const dictionary& dict)
97 List<Tuple2<scalar, Type> >(),
98 boundsHandling_(wordToBoundsHandling(dict.lookup("outOfBounds"))),
99 fileName_(dict.lookup("fileName"))
106 Foam::interpolationTable<Type>::interpolationTable
108 const interpolationTable& interpTable
111 List<Tuple2<scalar, Type> >(interpTable),
112 boundsHandling_(interpTable.boundsHandling_),
113 fileName_(interpTable.fileName_)
118 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
121 Foam::word Foam::interpolationTable<Type>::boundsHandlingToWord
123 const boundsHandling& bound
126 word enumName("warn");
130 case interpolationTable::ERROR:
135 case interpolationTable::WARN:
140 case interpolationTable::CLAMP:
145 case interpolationTable::REPEAT:
157 typename Foam::interpolationTable<Type>::boundsHandling
158 Foam::interpolationTable<Type>::wordToBoundsHandling
163 if (bound == "error")
165 return interpolationTable::ERROR;
167 else if (bound == "warn")
169 return interpolationTable::WARN;
171 else if (bound == "clamp")
173 return interpolationTable::CLAMP;
175 else if (bound == "repeat")
177 return interpolationTable::REPEAT;
183 "Foam::interpolationTable<Type>::wordToBoundsHandling(const word&)"
184 ) << "bad outOfBounds specifier " << bound << " using 'warn'" << endl;
186 return interpolationTable::WARN;
192 typename Foam::interpolationTable<Type>::boundsHandling
193 Foam::interpolationTable<Type>::outOfBounds
195 const boundsHandling& bound
198 boundsHandling prev = boundsHandling_;
199 boundsHandling_ = bound;
205 void Foam::interpolationTable<Type>::check() const
207 label n = this->size();
208 scalar prevValue = List<Tuple2<scalar, Type> >::operator[](0).first();
210 for (label i=1; i<n; ++i)
212 const scalar currValue =
213 List<Tuple2<scalar, Type> >::operator[](i).first();
215 // avoid duplicate values (divide-by-zero error)
216 if (currValue <= prevValue)
220 "Foam::interpolationTable<Type>::checkOrder() const"
221 ) << "out-of-order value: "
222 << currValue << " at index " << i << nl
225 prevValue = currValue;
231 void Foam::interpolationTable<Type>::write(Ostream& os) const
233 os.writeKeyword("fileName")
234 << fileName_ << token::END_STATEMENT << nl;
235 os.writeKeyword("outOfBounds")
236 << boundsHandlingToWord(boundsHandling_) << token::END_STATEMENT << nl;
240 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
243 const Foam::Tuple2<Foam::scalar, Type>&
244 Foam::interpolationTable<Type>::operator[](const label i) const
247 label n = this->size();
255 switch (boundsHandling_)
257 case interpolationTable::ERROR:
261 "Foam::interpolationTable<Type>::operator[]"
262 "(const label) const"
263 ) << "index (" << ii << ") underflow" << nl
267 case interpolationTable::WARN:
271 "Foam::interpolationTable<Type>::operator[]"
272 "(const label) const"
273 ) << "index (" << ii << ") underflow" << nl
274 << " Continuing with the first entry"
276 // fall-through to 'CLAMP'
278 case interpolationTable::CLAMP:
283 case interpolationTable::REPEAT:
295 switch (boundsHandling_)
297 case interpolationTable::ERROR:
301 "Foam::interpolationTable<Type>::operator[]"
302 "(const label) const"
303 ) << "index (" << ii << ") overflow" << nl
307 case interpolationTable::WARN:
311 "Foam::interpolationTable<Type>::operator[]"
312 "(const label) const"
313 ) << "index (" << ii << ") overflow" << nl
314 << " Continuing with the last entry"
316 // fall-through to 'CLAMP'
318 case interpolationTable::CLAMP:
323 case interpolationTable::REPEAT:
334 return List<Tuple2<scalar, Type> >::operator[](ii);
339 Type Foam::interpolationTable<Type>::operator()(const scalar value) const
341 label n = this->size();
345 return List<Tuple2<scalar, Type> >::operator[](0).second();
348 scalar minLimit = List<Tuple2<scalar, Type> >::operator[](0).first();
349 scalar maxLimit = List<Tuple2<scalar, Type> >::operator[](n-1).first();
350 scalar lookupValue = value;
352 if (lookupValue < minLimit)
354 switch (boundsHandling_)
356 case interpolationTable::ERROR:
360 "Foam::interpolationTable<Type>::operator[]"
361 "(const scalar) const"
362 ) << "value (" << lookupValue << ") underflow" << nl
366 case interpolationTable::WARN:
370 "Foam::interpolationTable<Type>::operator[]"
371 "(const scalar) const"
372 ) << "value (" << lookupValue << ") underflow" << nl
373 << " Continuing with the first entry"
375 // fall-through to 'CLAMP'
377 case interpolationTable::CLAMP:
379 return List<Tuple2<scalar, Type> >::operator[](0).second();
382 case interpolationTable::REPEAT:
384 // adjust lookupValue to >= 0
385 while (lookupValue < 0)
387 lookupValue += maxLimit;
393 else if (lookupValue >= maxLimit)
395 switch (boundsHandling_)
397 case interpolationTable::ERROR:
401 "Foam::interpolationTable<Type>::operator[]"
402 "(const label) const"
403 ) << "value (" << lookupValue << ") overflow" << nl
407 case interpolationTable::WARN:
411 "Foam::interpolationTable<Type>::operator[]"
412 "(const label) const"
413 ) << "value (" << lookupValue << ") overflow" << nl
414 << " Continuing with the last entry"
416 // fall-through to 'CLAMP'
418 case interpolationTable::CLAMP:
420 return List<Tuple2<scalar, Type> >::operator[](n-1).second();
423 case interpolationTable::REPEAT:
425 // adjust lookupValue <= maxLimit
426 while (lookupValue > maxLimit)
428 lookupValue -= maxLimit;
438 // look for the correct range
439 for (label i = 0; i < n; ++i)
441 if (lookupValue >= List<Tuple2<scalar, Type> >::operator[](i).first())
454 // we are at the end of the table - or there is only a single entry
455 return List<Tuple2<scalar, Type> >::operator[](hi).second();
459 // this treatment should should only occur under these conditions:
460 // -> the 'REPEAT' treatment
461 // -> (0 <= value <= minLimit)
463 // Use the value at maxLimit as the value for value=0
468 List<Tuple2<scalar, Type> >::operator[](lo).second()
470 List<Tuple2<scalar, Type> >::operator[](hi).second()
471 - List<Tuple2<scalar, Type> >::operator[](lo).second()
473 *(lookupValue / minLimit)
478 // normal interpolation
481 List<Tuple2<scalar, Type> >::operator[](lo).second()
483 List<Tuple2<scalar, Type> >::operator[](hi).second()
484 - List<Tuple2<scalar, Type> >::operator[](lo).second()
488 - List<Tuple2<scalar, Type> >::operator[](lo).first()
491 List<Tuple2<scalar, Type> >::operator[](hi).first()
492 - List<Tuple2<scalar, Type> >::operator[](lo).first()
499 // ************************************************************************* //