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

interpolationLookUpTable.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) 2008-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 "IFstream.H"
00028 
00029 // * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //
00030 
00031 template <class Type>
00032 Foam::label Foam::interpolationLookUpTable<Type>::index
00033 (
00034     const List<scalar>& indices,
00035     const bool lastDim
00036 ) const
00037 {
00038     label totalIndex = 0;
00039 
00040     forAll(dim_, i)
00041     {
00042         label dim = 1;
00043         for (int j = i + 1; j < dim_.size(); j++)
00044         {
00045             dim *= dim_[j] + 1;
00046         }
00047 
00048         totalIndex +=
00049             dim
00050            *min
00051             (
00052                 max(label((indices[i] - min_[i])/delta_[i]), 0),
00053                 dim_[i]
00054             );
00055     }
00056 
00057     if (lastDim)
00058     {
00059         label iLastdim = dim_.size() - 1;
00060         totalIndex += Foam::min
00061         (
00062             max
00063             (
00064                 label((indices[iLastdim] - min_[iLastdim])/delta_[iLastdim]),
00065                 0
00066             ),
00067             dim_[iLastdim]
00068         );
00069     }
00070 
00071     return totalIndex;
00072 }
00073 
00074 
00075 template <class Type>
00076 Foam::label Foam::interpolationLookUpTable<Type>::index
00077 (
00078     const scalar indice
00079 ) const
00080 {
00081     label i = 0;
00082     label totalIndex =
00083         Foam::min
00084         (
00085             Foam::max
00086             (
00087                 label((indice - min_[i])/delta_[i]),
00088                 0
00089             ),
00090             dim_[i]
00091         );
00092 
00093     return totalIndex;
00094 }
00095 
00096 
00097 template<class Type>
00098 bool Foam::interpolationLookUpTable<Type>::checkRange
00099 (
00100     const scalar lookUpValue,
00101     const label interfield
00102 ) const
00103 {
00104     if (lookUpValue >= min_[interfield] &&  lookUpValue <= max_[interfield])
00105     {
00106         return true;
00107     }
00108     else
00109     {
00110         return false;
00111     }
00112 }
00113 
00114 
00115 template<class Type>
00116 Foam::scalar Foam::interpolationLookUpTable<Type>::interpolate
00117 (
00118     const label lo,
00119     const label hi,
00120     const scalar lookUpValue,
00121     const label ofield,
00122     const label interfield
00123 ) const
00124 {
00125         if
00126         (
00127             List<scalarField>::operator[](interfield).operator[](hi)
00128          != List<scalarField>::operator[](interfield).operator[](lo)
00129         )
00130         {
00131             scalar output
00132             (
00133                 List<scalarField>::operator[](ofield).operator[](lo)
00134               + (
00135                     List<scalarField>::operator[](ofield).operator[](hi)
00136                   - List<scalarField>::operator[](ofield).operator[](lo)
00137                 )
00138                *(
00139                     lookUpValue
00140                   - List<scalarField>::operator[](interfield).operator[](lo)
00141                 )
00142                /(
00143                     List<scalarField>::operator[](interfield).operator[](hi)
00144                   - List<scalarField>::operator[](interfield).operator[](lo)
00145                 )
00146             );
00147             return output;
00148         }
00149         else
00150         {
00151             return List<scalarField>::operator[](ofield).operator[](lo);
00152         }
00153 }
00154 
00155 
00156 template<class Type>
00157 void Foam::interpolationLookUpTable<Type>::dimensionTable()
00158 {
00159     min_.setSize(entries_.size());
00160     dim_.setSize(entries_.size());
00161     delta_.setSize(entries_.size());
00162     max_.setSize(entries_.size());
00163     entryIndices_.setSize(entries_.size());
00164     outputIndices_.setSize(output_.size());
00165     label index = 0;
00166     label tableDim = 1;
00167 
00168     forAll(entries_,i)
00169     {
00170         dim_[i] = readLabel(entries_[i].lookup("N"));
00171         max_[i] = readScalar(entries_[i].lookup("max"));
00172         min_[i] = readScalar(entries_[i].lookup("min"));
00173         delta_[i] = (max_[i] - min_[i])/dim_[i];
00174         tableDim *= dim_[i] + 1;
00175         fieldIndices_.insert(entries_[i].lookup("name"), index);
00176         entryIndices_[i] = index;
00177         index++;
00178     }
00179 
00180     forAll(output_,i)
00181     {
00182         fieldIndices_.insert(output_[i].lookup("name"), index);
00183         outputIndices_[i] = index;
00184         index++;
00185     }
00186 
00187     List<scalarField>& internal = *this;
00188 
00189     internal.setSize(entries_.size() + output_.size());
00190 
00191     interpOutput_.setSize(entries_.size() + output_.size());
00192 
00193     forAll(internal, i)
00194     {
00195         internal[i].setSize(tableDim);
00196     }
00197 }
00198 
00199 
00200 template<class Type>
00201 void Foam::interpolationLookUpTable<Type>::readTable
00202 (
00203     const word& instance,
00204     const fvMesh& mesh
00205 )
00206 {
00207     IOdictionary control
00208     (
00209         IOobject
00210         (
00211             fileName_,
00212             instance,
00213             mesh,
00214             IOobject::MUST_READ,
00215             IOobject::NO_WRITE
00216         )
00217     );
00218 
00219     control.lookup("fields") >> entries_;
00220     control.lookup("output") >> output_;
00221     control.lookup("values") >> *this;
00222 
00223     dimensionTable();
00224 
00225     check();
00226 
00227     if (this->size() == 0)
00228     {
00229         FatalErrorIn
00230         (
00231             "Foam::interpolationLookUpTable<Type>::readTable()"
00232         )   << "table is empty" << nl << exit(FatalError);
00233     }
00234 }
00235 
00236 
00237 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00238 
00239 template<class Type>
00240 Foam::interpolationLookUpTable<Type>::interpolationLookUpTable()
00241 :
00242     List<scalarField>(),
00243     fileName_("fileNameIsUndefined")
00244 {}
00245 
00246 
00247 template<class Type>
00248 Foam::interpolationLookUpTable<Type>::interpolationLookUpTable
00249 (
00250     const fileName& fn, const word& instance, const fvMesh& mesh
00251 )
00252 :
00253     List<scalarField>(),
00254     fileName_(fn),
00255     dim_(0),
00256     min_(0),
00257     delta_(0.0),
00258     max_(0.0),
00259     entries_(0),
00260     output_(0),
00261     entryIndices_(0),
00262     outputIndices_(0),
00263     interpOutput_(0)
00264 {
00265     readTable(instance, mesh);
00266 }
00267 
00268 
00269 template<class Type>
00270 Foam::interpolationLookUpTable<Type>::interpolationLookUpTable
00271 (
00272      const interpolationLookUpTable& interpTable
00273 )
00274 :
00275     List<scalarField>(interpTable),
00276     fileName_(interpTable.fileName_),
00277     entryIndices_(interpTable.entryIndices_),
00278     outputIndices_(interpTable.outputIndices_),
00279     dim_(interpTable.dim_),
00280     min_(interpTable.min_),
00281     delta_(interpTable.delta_),
00282     max_(interpTable.max_),
00283     entries_(0),
00284     output_(0),
00285     interpOutput_(interpTable.interpOutput_)
00286 {}
00287 
00288 
00289 template<class Type>
00290 Foam::interpolationLookUpTable<Type>::interpolationLookUpTable
00291 (
00292     const dictionary& dict
00293 )
00294 :
00295     List<scalarField>(),
00296     fileName_(fileName(dict.lookup("fileName")).expand()),
00297     dim_(0),
00298     min_(0.0),
00299     delta_(0.0),
00300     max_(0.0),
00301     entries_(dict.lookup("fields")),
00302     output_(dict.lookup("output")),
00303     entryIndices_(0),
00304     outputIndices_(0),
00305     fieldIndices_(0),
00306     interpOutput_(0)
00307 {
00308     dimensionTable();
00309 }
00310 
00311 
00312 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00313 
00314 template<class Type>
00315 void Foam::interpolationLookUpTable<Type>::check() const
00316 {
00317     // check order in the first dimension.
00318     scalar prevValue = List<scalarField>::operator[](0).operator[](0);
00319     label dim = 1;
00320     for (int j = 1; j < dim_.size(); j++)
00321     {
00322         dim *= dim_[j] + 1;
00323     }
00324 
00325     for (label i = 1; i < dim_[0]; i++)
00326     {
00327         label index = i*dim;
00328         const scalar currValue =
00329             List<scalarField>::operator[](0).operator[](index);
00330 
00331         // avoid duplicate values (divide-by-zero error)
00332         if (currValue <= prevValue)
00333         {
00334             FatalErrorIn
00335             (
00336                 "Foam::interpolationLookUpTable<Type>::checkOrder() const"
00337             )   << "out-of-order value: " << currValue
00338                 << " at index " << index << nl << exit(FatalError);
00339         }
00340         prevValue = currValue;
00341     }
00342 }
00343 
00344 
00345 template<class Type>
00346 void Foam::interpolationLookUpTable<Type>::write
00347 (
00348     Ostream& os,
00349     const fileName& fn,
00350     const word& instance,
00351     const fvMesh& mesh
00352 ) const
00353 {
00354     IOdictionary control
00355     (
00356         IOobject
00357         (
00358             fn,
00359             instance,
00360             mesh,
00361             IOobject::NO_READ,
00362             IOobject::NO_WRITE
00363         )
00364     );
00365 
00366     control.writeHeader(os);
00367 
00368     os.writeKeyword("fields");
00369     os << entries_ << token::END_STATEMENT << nl;
00370 
00371     os.writeKeyword("output");
00372     os << output_ << token::END_STATEMENT << nl;
00373 
00374     if (this->size() == 0)
00375     {
00376         FatalErrorIn
00377         (
00378             "Foam::interpolationTable<Type>::write()"
00379         )   << "table is empty" << nl << exit(FatalError);
00380     }
00381     os.writeKeyword("values");
00382     os << *this << token::END_STATEMENT << nl;
00383 }
00384 
00385 
00386 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00387 
00388 template<class Type>
00389 Foam::scalarField&
00390 Foam::interpolationLookUpTable<Type>::operator[](const label i)
00391 {
00392     label ii = i;
00393     label n  = this->size();
00394 
00395     if (n <= 1)
00396     {
00397         FatalErrorIn
00398         (
00399             "Foam::interpolationLookUpTable<Type>::operator[](const label)"
00400         )   << "table has (" << n << ") columns" << nl << exit(FatalError);
00401     }
00402     else if (ii < 0)
00403     {
00404         FatalErrorIn
00405         (
00406             "Foam::interpolationLookUpTable<Type>::operator[](const label)"
00407         )   << "index (" << ii << ") underflow" << nl << exit(FatalError);
00408     }
00409     else if (ii > n)
00410     {
00411         FatalErrorIn
00412         (
00413             "Foam::interpolationLookUpTable<Type>::operator[](const label)"
00414         )   << "index (" << ii << ") overflow" << nl << exit(FatalError);
00415     }
00416 
00417     return List<scalarField>::operator[](ii);
00418 }
00419 
00420 
00421 template<class Type>
00422 const Foam::scalarField&
00423 Foam::interpolationLookUpTable<Type>::operator[](const label i) const
00424 {
00425     label ii = i;
00426     label n  = this->size();
00427 
00428     if (n <= 1)
00429     {
00430         FatalErrorIn
00431         (
00432             "Foam::interpolationLookUpTable<Type>::operator[]"
00433             "(const label) const"
00434         )   << "table has (" << n << ") columns" << nl << exit(FatalError);
00435     }
00436     else if (ii < 0)
00437     {
00438         FatalErrorIn
00439         (
00440             "Foam::interpolationLookUpTable<Type>::operator[]"
00441             "(const label) const"
00442         )   << "index (" << ii << ") underflow" << nl << exit(FatalError);
00443     }
00444 
00445     else if (ii > n)
00446     {
00447         FatalErrorIn
00448         (
00449             "Foam::interpolationLookUpTable<Type>::operator[]"
00450             "(const label) const"
00451         )   << "index (" << ii << ") overflow" << nl
00452             << exit(FatalError);
00453     }
00454 
00455     return List<scalarField>::operator[](ii);
00456 }
00457 
00458 
00459 template<class Type>
00460 bool Foam::interpolationLookUpTable<Type>::found(const word& fieldName) const
00461 {
00462     return fieldIndices_.found(fieldName);
00463 }
00464 
00465 
00466 template<class Type>
00467 const Foam::scalarList&
00468 Foam::interpolationLookUpTable<Type>::lookUp(const scalar retvals)
00469 {
00470     const label lo = index(retvals);
00471     findHi(lo, retvals);
00472     return interpOutput_;
00473 }
00474 
00475 
00476 template<class Type>
00477 void Foam::interpolationLookUpTable<Type>::findHi
00478 (
00479     const label lo,
00480     const scalar retvals
00481 )
00482 {
00483     forAll(outputIndices_,j)
00484     {
00485         scalar tmp = 0;
00486         label ofield = outputIndices_[j];
00487         scalar baseValue = List<scalarField>::operator[](ofield).operator[](lo);
00488 
00489         forAll(entryIndices_,i)
00490         {
00491             if (checkRange(retvals, entryIndices_[i]))
00492             {
00493                 label dim = 1;
00494 
00495                 label hi = Foam::min(lo + dim, (*this)[0].size() - 1);
00496 
00497                 tmp += interpolate(lo, hi, retvals, ofield, entryIndices_[i])
00498                      - baseValue;
00499             }
00500             interpOutput_[entryIndices_[i]] = retvals;
00501         }
00502 
00503         tmp += baseValue;
00504         interpOutput_[outputIndices_[j]] = tmp;
00505     }
00506 }
00507 
00508 
00509 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd