OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

interpolationTable.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "interpolationTable.H"
00028 #include "IFstream.H"
00029 
00030 // * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //
00031 
00032 template<class Type>
00033 void Foam::interpolationTable<Type>::readTable()
00034 {
00035     // preserve the original (unexpanded) fileName to avoid absolute paths
00036     // appearing subsequently in the write() method
00037     fileName fName(fileName_);
00038 
00039     fName.expand();
00040 
00041     // Read data from file
00042     IFstream(fName)() >> *this;
00043 
00044     // Check that the data are okay
00045     check();
00046 
00047     if (this->empty())
00048     {
00049         FatalErrorIn
00050         (
00051             "Foam::interpolationTable<Type>::readTable()"
00052         )   << "table is empty" << nl
00053             << exit(FatalError);
00054     }
00055 }
00056 
00057 
00058 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00059 
00060 template<class Type>
00061 Foam::interpolationTable<Type>::interpolationTable()
00062 :
00063     List<Tuple2<scalar, Type> >(),
00064     boundsHandling_(interpolationTable::WARN),
00065     fileName_("fileNameIsUndefined")
00066 {}
00067 
00068 
00069 template<class Type>
00070 Foam::interpolationTable<Type>::interpolationTable
00071 (
00072     const List<Tuple2<scalar, Type> >& values,
00073     const boundsHandling bounds,
00074     const fileName& fName
00075 )
00076 :
00077     List<Tuple2<scalar, Type> >(values),
00078     boundsHandling_(bounds),
00079     fileName_(fName)
00080 {}
00081 
00082 
00083 template<class Type>
00084 Foam::interpolationTable<Type>::interpolationTable(const fileName& fName)
00085 :
00086     List<Tuple2<scalar, Type> >(),
00087     boundsHandling_(interpolationTable::WARN),
00088     fileName_(fName)
00089 {
00090     readTable();
00091 }
00092 
00093 
00094 template<class Type>
00095 Foam::interpolationTable<Type>::interpolationTable(const dictionary& dict)
00096 :
00097     List<Tuple2<scalar, Type> >(),
00098     boundsHandling_(wordToBoundsHandling(dict.lookup("outOfBounds"))),
00099     fileName_(dict.lookup("fileName"))
00100 {
00101     readTable();
00102 }
00103 
00104 
00105 template<class Type>
00106 Foam::interpolationTable<Type>::interpolationTable
00107 (
00108      const interpolationTable& interpTable
00109 )
00110 :
00111     List<Tuple2<scalar, Type> >(interpTable),
00112     boundsHandling_(interpTable.boundsHandling_),
00113     fileName_(interpTable.fileName_)
00114 {}
00115 
00116 
00117 
00118 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00119 
00120 template<class Type>
00121 Foam::word Foam::interpolationTable<Type>::boundsHandlingToWord
00122 (
00123      const boundsHandling& bound
00124 ) const
00125 {
00126     word enumName("warn");
00127 
00128     switch (bound)
00129     {
00130         case interpolationTable::ERROR:
00131         {
00132             enumName = "error";
00133             break;
00134         }
00135         case interpolationTable::WARN:
00136         {
00137             enumName = "warn";
00138             break;
00139         }
00140         case interpolationTable::CLAMP:
00141         {
00142             enumName = "clamp";
00143             break;
00144         }
00145         case interpolationTable::REPEAT:
00146         {
00147             enumName = "repeat";
00148             break;
00149         }
00150     }
00151 
00152     return enumName;
00153 }
00154 
00155 
00156 template<class Type>
00157 typename Foam::interpolationTable<Type>::boundsHandling
00158 Foam::interpolationTable<Type>::wordToBoundsHandling
00159 (
00160     const word& bound
00161 ) const
00162 {
00163     if (bound == "error")
00164     {
00165         return interpolationTable::ERROR;
00166     }
00167     else if (bound == "warn")
00168     {
00169         return interpolationTable::WARN;
00170     }
00171     else if (bound == "clamp")
00172     {
00173         return interpolationTable::CLAMP;
00174     }
00175     else if (bound == "repeat")
00176     {
00177         return interpolationTable::REPEAT;
00178     }
00179     else
00180     {
00181         WarningIn
00182         (
00183             "Foam::interpolationTable<Type>::wordToBoundsHandling(const word&)"
00184         )   << "bad outOfBounds specifier " << bound << " using 'warn'" << endl;
00185 
00186         return interpolationTable::WARN;
00187     }
00188 }
00189 
00190 
00191 template<class Type>
00192 typename Foam::interpolationTable<Type>::boundsHandling
00193 Foam::interpolationTable<Type>::outOfBounds
00194 (
00195     const boundsHandling& bound
00196 )
00197 {
00198     boundsHandling prev = boundsHandling_;
00199     boundsHandling_ = bound;
00200     return prev;
00201 }
00202 
00203 
00204 template<class Type>
00205 void Foam::interpolationTable<Type>::check() const
00206 {
00207     label n = this->size();
00208     scalar prevValue = List<Tuple2<scalar, Type> >::operator[](0).first();
00209 
00210     for (label i=1; i<n; ++i)
00211     {
00212         const scalar currValue =
00213             List<Tuple2<scalar, Type> >::operator[](i).first();
00214 
00215         // avoid duplicate values (divide-by-zero error)
00216         if (currValue <= prevValue)
00217         {
00218             FatalErrorIn
00219             (
00220                 "Foam::interpolationTable<Type>::checkOrder() const"
00221             )   << "out-of-order value: "
00222                 << currValue << " at index " << i << nl
00223                 << exit(FatalError);
00224         }
00225         prevValue = currValue;
00226     }
00227 }
00228 
00229 
00230 template<class Type>
00231 void Foam::interpolationTable<Type>::write(Ostream& os) const
00232 {
00233     os.writeKeyword("fileName")
00234         << fileName_ << token::END_STATEMENT << nl;
00235     os.writeKeyword("outOfBounds")
00236         << boundsHandlingToWord(boundsHandling_) << token::END_STATEMENT << nl;
00237 }
00238 
00239 
00240 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00241 
00242 template<class Type>
00243 const Foam::Tuple2<Foam::scalar, Type>&
00244 Foam::interpolationTable<Type>::operator[](const label i) const
00245 {
00246     label ii = i;
00247     label n  = this->size();
00248 
00249     if (n <= 1)
00250     {
00251         ii = 0;
00252     }
00253     else if (ii < 0)
00254     {
00255         switch (boundsHandling_)
00256         {
00257             case interpolationTable::ERROR:
00258             {
00259                 FatalErrorIn
00260                 (
00261                     "Foam::interpolationTable<Type>::operator[]"
00262                     "(const label) const"
00263                 )   << "index (" << ii << ") underflow" << nl
00264                     << exit(FatalError);
00265                 break;
00266             }
00267             case interpolationTable::WARN:
00268             {
00269                 WarningIn
00270                 (
00271                     "Foam::interpolationTable<Type>::operator[]"
00272                     "(const label) const"
00273                 )   << "index (" << ii << ") underflow" << nl
00274                     << "    Continuing with the first entry"
00275                     << endl;
00276                 // fall-through to 'CLAMP'
00277             }
00278             case interpolationTable::CLAMP:
00279             {
00280                 ii = 0;
00281                 break;
00282             }
00283             case interpolationTable::REPEAT:
00284             {
00285                 while (ii < 0)
00286                 {
00287                     ii += n;
00288                 }
00289                 break;
00290             }
00291         }
00292     }
00293     else if (ii >= n)
00294     {
00295         switch (boundsHandling_)
00296         {
00297             case interpolationTable::ERROR:
00298             {
00299                 FatalErrorIn
00300                 (
00301                     "Foam::interpolationTable<Type>::operator[]"
00302                     "(const label) const"
00303                 )   << "index (" << ii << ") overflow" << nl
00304                     << exit(FatalError);
00305                 break;
00306             }
00307             case interpolationTable::WARN:
00308             {
00309                 WarningIn
00310                 (
00311                     "Foam::interpolationTable<Type>::operator[]"
00312                     "(const label) const"
00313                 )   << "index (" << ii << ") overflow" << nl
00314                     << "    Continuing with the last entry"
00315                     << endl;
00316                 // fall-through to 'CLAMP'
00317             }
00318             case interpolationTable::CLAMP:
00319             {
00320                 ii = n - 1;
00321                 break;
00322             }
00323             case interpolationTable::REPEAT:
00324             {
00325                 while (ii >= n)
00326                 {
00327                     ii -= n;
00328                 }
00329                 break;
00330             }
00331         }
00332     }
00333 
00334     return List<Tuple2<scalar, Type> >::operator[](ii);
00335 }
00336 
00337 
00338 template<class Type>
00339 Type Foam::interpolationTable<Type>::operator()(const scalar value) const
00340 {
00341     label n = this->size();
00342 
00343     if (n <= 1)
00344     {
00345         return List<Tuple2<scalar, Type> >::operator[](0).second();
00346     }
00347 
00348     scalar minLimit = List<Tuple2<scalar, Type> >::operator[](0).first();
00349     scalar maxLimit = List<Tuple2<scalar, Type> >::operator[](n-1).first();
00350     scalar lookupValue = value;
00351 
00352     if (lookupValue < minLimit)
00353     {
00354         switch (boundsHandling_)
00355         {
00356             case interpolationTable::ERROR:
00357             {
00358                 FatalErrorIn
00359                 (
00360                     "Foam::interpolationTable<Type>::operator[]"
00361                     "(const scalar) const"
00362                 )   << "value (" << lookupValue << ") underflow" << nl
00363                     << exit(FatalError);
00364                 break;
00365             }
00366             case interpolationTable::WARN:
00367             {
00368                 WarningIn
00369                 (
00370                     "Foam::interpolationTable<Type>::operator[]"
00371                     "(const scalar) const"
00372                 )   << "value (" << lookupValue << ") underflow" << nl
00373                     << "    Continuing with the first entry"
00374                     << endl;
00375                 // fall-through to 'CLAMP'
00376             }
00377             case interpolationTable::CLAMP:
00378             {
00379                 return List<Tuple2<scalar, Type> >::operator[](0).second();
00380                 break;
00381             }
00382             case interpolationTable::REPEAT:
00383             {
00384                 // adjust lookupValue to >= 0
00385                 while (lookupValue < 0)
00386                 {
00387                     lookupValue += maxLimit;
00388                 }
00389                 break;
00390             }
00391         }
00392     }
00393     else if (lookupValue >= maxLimit)
00394     {
00395         switch (boundsHandling_)
00396         {
00397             case interpolationTable::ERROR:
00398             {
00399                 FatalErrorIn
00400                 (
00401                     "Foam::interpolationTable<Type>::operator[]"
00402                     "(const label) const"
00403                 )   << "value (" << lookupValue << ") overflow" << nl
00404                     << exit(FatalError);
00405                 break;
00406             }
00407             case interpolationTable::WARN:
00408             {
00409                 WarningIn
00410                 (
00411                     "Foam::interpolationTable<Type>::operator[]"
00412                     "(const label) const"
00413                 )   << "value (" << lookupValue << ") overflow" << nl
00414                     << "    Continuing with the last entry"
00415                     << endl;
00416                 // fall-through to 'CLAMP'
00417             }
00418             case interpolationTable::CLAMP:
00419             {
00420                 return List<Tuple2<scalar, Type> >::operator[](n-1).second();
00421                 break;
00422             }
00423             case interpolationTable::REPEAT:
00424             {
00425                 // adjust lookupValue <= maxLimit
00426                 while (lookupValue > maxLimit)
00427                 {
00428                     lookupValue -= maxLimit;
00429                 }
00430                 break;
00431             }
00432         }
00433     }
00434 
00435     label lo = 0;
00436     label hi = 0;
00437 
00438     // look for the correct range
00439     for (label i = 0; i < n; ++i)
00440     {
00441         if (lookupValue >= List<Tuple2<scalar, Type> >::operator[](i).first())
00442         {
00443             lo = hi = i;
00444         }
00445         else
00446         {
00447             hi = i;
00448             break;
00449         }
00450     }
00451 
00452     if (lo == hi)
00453     {
00454         // we are at the end of the table - or there is only a single entry
00455         return List<Tuple2<scalar, Type> >::operator[](hi).second();
00456     }
00457     else if (hi == 0)
00458     {
00459         // this treatment should should only occur under these conditions:
00460         //  -> the 'REPEAT' treatment
00461         //  -> (0 <= value <= minLimit)
00462         //  -> minLimit > 0
00463         // Use the value at maxLimit as the value for value=0
00464         lo = n - 1;
00465 
00466         return
00467         (
00468             List<Tuple2<scalar, Type> >::operator[](lo).second()
00469           + (
00470                 List<Tuple2<scalar, Type> >::operator[](hi).second()
00471               - List<Tuple2<scalar, Type> >::operator[](lo).second()
00472             )
00473            *(lookupValue / minLimit)
00474         );
00475     }
00476     else
00477     {
00478         // normal interpolation
00479         return
00480         (
00481             List<Tuple2<scalar, Type> >::operator[](lo).second()
00482           + (
00483                 List<Tuple2<scalar, Type> >::operator[](hi).second()
00484               - List<Tuple2<scalar, Type> >::operator[](lo).second()
00485             )
00486            *(
00487                 lookupValue
00488               - List<Tuple2<scalar, Type> >::operator[](lo).first()
00489             )
00490            /(
00491                 List<Tuple2<scalar, Type> >::operator[](hi).first()
00492               - List<Tuple2<scalar, Type> >::operator[](lo).first()
00493             )
00494         );
00495     }
00496 }
00497 
00498 
00499 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd