1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend 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 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "interpolationTable.H"
29 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
32 void Foam::interpolationTable<Type>::readTable()
34 // preserve the original (unexpanded) fileName to avoid absolute paths
35 // appearing subsequently in the write() method
36 fileName fName(fileName_);
40 // Read data from file
41 IFstream(fName)() >> *this;
43 // Check that the data are okay
50 "Foam::interpolationTable<Type>::readTable()"
51 ) << "table is empty" << nl
57 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
60 Foam::interpolationTable<Type>::interpolationTable()
62 List<Tuple2<scalar, Type> >(),
63 boundsHandling_(interpolationTable::WARN),
64 fileName_("fileNameIsUndefined")
69 Foam::interpolationTable<Type>::interpolationTable
71 const List<Tuple2<scalar, Type> >& values,
72 const boundsHandling bounds,
76 List<Tuple2<scalar, Type> >(values),
77 boundsHandling_(bounds),
83 Foam::interpolationTable<Type>::interpolationTable(const fileName& fName)
85 List<Tuple2<scalar, Type> >(),
86 boundsHandling_(interpolationTable::WARN),
94 Foam::interpolationTable<Type>::interpolationTable(const dictionary& dict)
96 List<Tuple2<scalar, Type> >(),
97 boundsHandling_(wordToBoundsHandling(dict.lookup("outOfBounds"))),
98 fileName_(dict.lookup("fileName"))
105 Foam::interpolationTable<Type>::interpolationTable
107 const interpolationTable& interpTable
110 List<Tuple2<scalar, Type> >(interpTable),
111 boundsHandling_(interpTable.boundsHandling_),
112 fileName_(interpTable.fileName_)
117 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
120 Foam::word Foam::interpolationTable<Type>::boundsHandlingToWord
122 const boundsHandling& bound
125 word enumName("warn");
129 case interpolationTable::EXIT:
134 case interpolationTable::WARN:
139 case interpolationTable::CLAMP:
144 case interpolationTable::REPEAT:
156 typename Foam::interpolationTable<Type>::boundsHandling
157 Foam::interpolationTable<Type>::wordToBoundsHandling
164 return interpolationTable::EXIT;
166 else if (bound == "warn")
168 return interpolationTable::WARN;
170 else if (bound == "clamp")
172 return interpolationTable::CLAMP;
174 else if (bound == "repeat")
176 return interpolationTable::REPEAT;
182 "Foam::interpolationTable<Type>::wordToBoundsHandling(const word&)"
183 ) << "bad outOfBounds specifier " << bound << " using 'warn'" << endl;
185 return interpolationTable::WARN;
191 typename Foam::interpolationTable<Type>::boundsHandling
192 Foam::interpolationTable<Type>::outOfBounds
194 const boundsHandling& bound
197 boundsHandling prev = boundsHandling_;
198 boundsHandling_ = bound;
204 void Foam::interpolationTable<Type>::check() const
206 label n = this->size();
207 scalar prevValue = List<Tuple2<scalar, Type> >::operator[](0).first();
209 for (label i=1; i<n; ++i)
211 const scalar currValue =
212 List<Tuple2<scalar, Type> >::operator[](i).first();
214 // avoid duplicate values (divide-by-zero error)
215 if (currValue <= prevValue)
219 "Foam::interpolationTable<Type>::checkOrder() const"
220 ) << "out-of-order value: "
221 << currValue << " at index " << i << nl
224 prevValue = currValue;
230 void Foam::interpolationTable<Type>::write(Ostream& os) const
232 os.writeKeyword("fileName")
233 << fileName_ << token::END_STATEMENT << nl;
234 os.writeKeyword("outOfBounds")
235 << boundsHandlingToWord(boundsHandling_) << token::END_STATEMENT << nl;
239 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
242 const Foam::Tuple2<Foam::scalar, Type>&
243 Foam::interpolationTable<Type>::operator[](const label i) const
246 label n = this->size();
254 switch (boundsHandling_)
256 case interpolationTable::EXIT:
260 "Foam::interpolationTable<Type>::operator[]"
261 "(const label) const"
262 ) << "index (" << ii << ") underflow" << nl
266 case interpolationTable::WARN:
270 "Foam::interpolationTable<Type>::operator[]"
271 "(const label) const"
272 ) << "index (" << ii << ") underflow" << nl
273 << " Continuing with the first entry"
275 // fall-through to 'CLAMP'
277 case interpolationTable::CLAMP:
282 case interpolationTable::REPEAT:
294 switch (boundsHandling_)
296 case interpolationTable::EXIT:
300 "Foam::interpolationTable<Type>::operator[]"
301 "(const label) const"
302 ) << "index (" << ii << ") overflow" << nl
306 case interpolationTable::WARN:
310 "Foam::interpolationTable<Type>::operator[]"
311 "(const label) const"
312 ) << "index (" << ii << ") overflow" << nl
313 << " Continuing with the last entry"
315 // fall-through to 'CLAMP'
317 case interpolationTable::CLAMP:
322 case interpolationTable::REPEAT:
333 return List<Tuple2<scalar, Type> >::operator[](ii);
338 Type Foam::interpolationTable<Type>::operator()(const scalar value) const
340 label n = this->size();
344 return List<Tuple2<scalar, Type> >::operator[](0).second();
347 scalar minLimit = List<Tuple2<scalar, Type> >::operator[](0).first();
348 scalar maxLimit = List<Tuple2<scalar, Type> >::operator[](n-1).first();
349 scalar lookupValue = value;
351 if (lookupValue < minLimit)
353 switch (boundsHandling_)
355 case interpolationTable::EXIT:
359 "Foam::interpolationTable<Type>::operator[]"
360 "(const scalar) const"
361 ) << "value (" << lookupValue << ") underflow" << nl
365 case interpolationTable::WARN:
369 "Foam::interpolationTable<Type>::operator[]"
370 "(const scalar) const"
371 ) << "value (" << lookupValue << ") underflow" << nl
372 << " Continuing with the first entry"
374 // fall-through to 'CLAMP'
376 case interpolationTable::CLAMP:
378 return List<Tuple2<scalar, Type> >::operator[](0).second();
381 case interpolationTable::REPEAT:
383 // adjust lookupValue to >= 0
384 while (lookupValue < 0)
386 lookupValue += maxLimit;
392 else if (lookupValue >= maxLimit)
394 switch (boundsHandling_)
396 case interpolationTable::EXIT:
400 "Foam::interpolationTable<Type>::operator[]"
401 "(const label) const"
402 ) << "value (" << lookupValue << ") overflow" << nl
406 case interpolationTable::WARN:
410 "Foam::interpolationTable<Type>::operator[]"
411 "(const label) const"
412 ) << "value (" << lookupValue << ") overflow" << nl
413 << " Continuing with the last entry"
415 // fall-through to 'CLAMP'
417 case interpolationTable::CLAMP:
419 return List<Tuple2<scalar, Type> >::operator[](n-1).second();
422 case interpolationTable::REPEAT:
424 // adjust lookupValue <= maxLimit
425 while (lookupValue > maxLimit)
427 lookupValue -= maxLimit;
437 // look for the correct range
438 for (label i = 0; i < n; ++i)
440 if (lookupValue >= List<Tuple2<scalar, Type> >::operator[](i).first())
453 // we are at the end of the table - or there is only a single entry
454 return List<Tuple2<scalar, Type> >::operator[](hi).second();
458 // this treatment should should only occur under these conditions:
459 // -> the 'REPEAT' treatment
460 // -> (0 <= value <= minLimit)
462 // Use the value at maxLimit as the value for value=0
467 List<Tuple2<scalar, Type> >::operator[](lo).second()
469 List<Tuple2<scalar, Type> >::operator[](hi).second()
470 - List<Tuple2<scalar, Type> >::operator[](lo).second()
472 *(lookupValue / minLimit)
477 // normal interpolation
480 List<Tuple2<scalar, Type> >::operator[](lo).second()
482 List<Tuple2<scalar, Type> >::operator[](hi).second()
483 - List<Tuple2<scalar, Type> >::operator[](lo).second()
487 - List<Tuple2<scalar, Type> >::operator[](lo).first()
490 List<Tuple2<scalar, Type> >::operator[](hi).first()
491 - List<Tuple2<scalar, Type> >::operator[](lo).first()
498 // ************************************************************************* //