interpolationTable.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "interpolationTable.H"
00028 #include "IFstream.H"
00029
00030
00031
00032 template<class Type>
00033 void Foam::interpolationTable<Type>::readTable()
00034 {
00035
00036
00037 fileName fName(fileName_);
00038
00039 fName.expand();
00040
00041
00042 IFstream(fName)() >> *this;
00043
00044
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
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
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
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
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
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
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
00376 }
00377 case interpolationTable::CLAMP:
00378 {
00379 return List<Tuple2<scalar, Type> >::operator[](0).second();
00380 break;
00381 }
00382 case interpolationTable::REPEAT:
00383 {
00384
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
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
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
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
00455 return List<Tuple2<scalar, Type> >::operator[](hi).second();
00456 }
00457 else if (hi == 0)
00458 {
00459
00460
00461
00462
00463
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
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